-
Notifications
You must be signed in to change notification settings - Fork 0
/
novel-speaker-mode.el
369 lines (311 loc) · 11.1 KB
/
novel-speaker-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
;; -*- Emacs-lisp -*-
;; -*- coding: utf8; -*-
;; version 1.1
;; auther: xiang wang ([email protected])
(require 'url)
(require 'json)
(require 'url-http)
(require 'helm-files)
(require 'epc)
(defgroup novel-speaker nil
"Novel speaker mode group."
:group 'entertainment)
(defcustom novel-speaker-use-rpc-player nil
"default nil to use rpc player"
:type 'boolean
:group 'novel-speaker)
(defcustom novel-speaker-rpc-player-host nil
"default rpyc player host"
:type 'string
:group 'novel-speaker
)
(defcustom novel-speaker-rpc-player-port nil
"default rpyc plyaer port"
:type 'integer
:group 'novel-speaker
)
(defcustom novel-speaker-cmd "/usr/local/bin/mplayer"
"default novel speaker command."
:type 'string
:group 'novel-speaker)
(defcustom novel-speaker-language "zh-CN"
"default speaker language is zh-CN"
:type 'string
:group 'novel-speaker)
(defcustom novel-speaker-app-id "3258863"
"baidu app id"
:type 'string
:group 'novel-speaker)
(defcustom novel-speaker-app-key "U2bNAXUG0XuZ88gv9bEcGT5H"
"baidu app key"
:type 'string
:group 'novel-speaker)
(defcustom novel-speaker-app-secret-key "A4oC5gxvK6GCdCEfntAgEECXP74RLgib"
"baidu app secrte key"
:type 'string
:group 'novel-speaker)
(defcustom novel-speaker-use-proxy nil
"default novel speaker proxy use"
:type 'boolean
:group 'novel-speaker)
(defcustom novel-speaker-proxy-host nil
"novel speaker proxy host. only support http proxy."
:type 'string
:group 'novel-speaker)
(defcustom novel-speaker-proxy-port nil
"novel speaker proxy port"
:type 'number
:group 'novel-speaker)
(defvar novel-speaker-current-status nil "novel speaker current status.")
(defvar novel-speaker-current-process nil "novel speaker current process.")
(defvar novel-speaker-current-buffer-name nil "novel speaker current buffer name.")
(defvar novel-speaker-oauth-token nil "baidu oauth token")
(defconst novel-speaker-oauth-url (concat
"https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id="
novel-speaker-app-key
"&client_secret="
novel-speaker-app-secret-key
)
)
(defcustom novel-speaker-rpc-player-script nil
"novel speaker rpc player script path"
:type 'string
:group 'novel-speaker
)
(defvar novl-speaker-rpc-player-epc nil "novel speaker rpc player epc")
(defun novel-speaker-open-file ()
(interactive)
(helm-find-files-1 "~/" )
(setq novel-speaker-current-buffer-name (current-buffer))
)
(defun novel-speaker-enable-proxy ()
(interactive)
(if (not (and novel-speaker-proxy-host novel-speaker-proxy-port))
(progn
(message "novel speaker proxy is not set yet.")
)
(let ((schema (concat novel-speaker-proxy-host ":" (number-to-string novel-speaker-proxy-port)) ))
(message schema)
(setq url-proxy-services (cons (cons "https" schema) nil))
(setq novel-speaker-use-proxy t)
(novel-speaker-parse-oauth (novel-speaker-send-url novel-speaker-oauth-url))
)
)
)
(defun novel-speaker-enable-rpc-player ()
(interactive)
(setq novel-speaker-rpc-player-epc (epc:start-epc "python" (list novel-speaker-rpc-player-script)))
(setq novel-speaker-use-rpc-player t)
)
(defun novel-speaker-disable-rpc-player ()
(interactive)
(epc:stop-epc novel-speaker-rpc-player-epc)
(setq novel-speaker-use-rpc-player nil)
)
(defun novel-speaker-set-rpyc-plyaer ()
(interactive "sRpyc Host:\nnRpyc Port:")
(setq novel-speaker-rpyc-player-host)
(setq novel-speaker-rpyc-plyaer-port)
)
(defun novel-speaker-disable-proxy ()
(interactive)
(setq novel-speaker-use-proxy nil))
(defun novel-speaker-set-proxy (host port)
(interactive "sProxy Host:\nnProxy Port:" )
(setq novel-speaker-proxy-host host)
(setq novel-speaker-proxy-port port)
(message "novel speaker proxy is %s:%d" host port))
(defun novel-speaker-send-url (url &optional url-args callback callback-args)
(let ((url-request-method "GET"))
(if url-args
(setq url-request-data (mapconcat #'(lambda (arg)
(concat (url-hexify-string (car arg))
"="
(url-hexify-string (cdr arg))))
url-args "&")))
(if callback
(url-retrieve url callback callback-args)
(url-retrieve-synchronously url)))
)
(defun novel-speaker-parse-oauth (json-buffer)
(let (json-start
json-end
json
(json-object-type 'hash-table)
)
(with-current-buffer json-buffer
(goto-char (point-min))
( if (not (search-forward "200 OK"))
(message "oath failed.")
(progn
(goto-char (point-max))
(backward-sentence)
(setq json-start (line-beginning-position))
(setq json-end (line-end-position))
(let (
(my-hash (json-read-from-string (decode-coding-string
(buffer-substring json-start json-end)
'utf-8))))
(if (hash-table-p my-hash)
(progn
;;
(message "oauth success.")
(setq novel-speaker-oauth-token (gethash "access_token" my-hash))
)
(message "Invalid oauth response data."))
)
)
)
)
)
)
(defun novel-speaker-select-sentence ()
(unless novel-speaker-current-buffer-name
(setq novel-speaker-current-buffer-name (current-buffer))
)
(with-current-buffer novel-speaker-current-buffer-name
(forward-char)
(forward-sentence)
(backward-sentence)
(push-mark)
(forward-sentence)
(buffer-substring (region-beginning) (region-end))
)
)
(defun novel-speaker-show-next-sentence ()
;; when text to audio server is not running,
;;then just show sentence in min buffer
(interactive)
(if (not
novel-speaker-current-buffer-name)
(message "No text file selected to read.")
(if (and
novel-speaker-current-buffer-name
( or (eq nil novel-speaker-current-status)
(not (string-match "running" novel-speaker-current-status)
)
)
)
(message "%s" (propertize (novel-speaker-select-sentence) 'face
'(:foreground "green")))
)
)
)
(defun novel-speaker-say-sentence-loop (sentence)
(unless (and novel-speaker-current-process
(process-live-p novel-speaker-current-process))
(message "%s" sentence)
(setq novel-speaker-current-process
(start-process "novel-speaker-process"
nil
novel-speaker-cmd
(concat (if novel-speaker-use-proxy
(concat "http_proxy://"
novel-speaker-proxy-host ":"
(number-to-string
novel-speaker-proxy-port)
"/")
"")
(url-encode-url (concat
"http://tsn.baidu.com/text2audio?tex="
sentence
"&lan=zh&cuid=xxxxxx&ctp=1&tok="
novel-speaker-oauth-token
"&spd=7"
))
)
))
(set-process-sentinel novel-speaker-current-process
'novel-speaker-process-sentinel)
)
)
(defun novel-speaker-get-sentence-uri (sentence)
(let ((retrive-uri
(concat (if novel-speaker-use-proxy
(concat "http_proxy://"
novel-speaker-proxy-host ":"
(number-to-string
novel-speaker-proxy-port)
"/")
"")
(url-encode-url (concat
"http://tsn.baidu.com/text2audio?tex="
sentence
"&lan=zh&cuid=xxxxxx&ctp=1&tok="
novel-speaker-oauth-token
"&spd=7"
))
)
)
)
retrive-uri
)
)
(defun novel-speaker-rpc-say-sentence-loop ()
(let ((sentence (novel-speaker-select-sentence))
)
(when
(and (string-equal novel-speaker-current-status "running")
(not (string-empty-p sentence)))
(message "%s" sentence)
(deferred:$
(epc:call-deferred novel-speaker-rpc-player-epc 'remote_play_mp3
(list novel-speaker-rpc-player-host
novel-speaker-rpc-player-port
(novel-speaker-get-sentence-uri sentence)))
(deferred:nextc it
(lambda ()
(novel-speaker-rpc-say-sentence-loop)
)
)
)
)
)
)
(defun novel-speaker-process-sentinel (proc change)
(when (string-match "\\(finished\\|Exiting\\)" change)
(let ((sentence (novel-speaker-select-sentence)))
(if (and (string-match "running" novel-speaker-current-status)
(not (eq "" sentence)))
(novel-speaker-say-sentence-loop sentence)
))
)
)
(defun novel-speaker-start ()
"start novel speaker"
(interactive)
(let ((sentence (novel-speaker-select-sentence)))
(novel-speaker-parse-oauth (novel-speaker-send-url novel-speaker-oauth-url))
(setq novel-speaker-current-status "running")
(if (not novel-speaker-use-rpc-player)
(novel-speaker-say-sentence-loop sentence)
(novel-speaker-rpc-say-sentence-loop)
)
)
)
(defun novel-speaker-kill-process ()
(when (and novel-speaker-current-process (process-live-p
novel-speaker-current-process))
(delete-process novel-speaker-current-process)
(setq novel-speaker-current-process nil)
))
(defun novel-speaker-stop ()
"stop novel speaker"
(interactive)
(setq novel-speaker-current-status "stop")
(when (not novel-speaker-use-rpc-player)
(novel-speaker-kill-process))
)
(define-minor-mode novel-speaker-mode
"This is the novel-speaker mode for mac os."
:lighter "NovelSpeak"
:group 'novel-speaker
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "\C-c \C-n b" ) 'novel-speaker-start)
(define-key map (kbd "\C-c \C-n e" ) 'novel-speaker-stop)
;;(define-key map (kbd "\C-c \C-n p" ) 'novel-speaker-set-proxy)
map)
)
(global-set-key (kbd "<f8>") 'novel-speaker-show-next-sentence)
(add-hook 'ereader-mode-hook 'novel-speaker-mode)
(provide 'novel-speaker-mode)