Debian Codegraph @

src/pyj/read_book/tts.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import bound_methods, hash_literals
4
5 from elementmaker import E
6 from gettext import gettext as _
7
8 from book_list.globals import get_session_data
9 from dom import unique_id
10 from modals import create_custom_dialog, error_dialog
11 from session import session_defaults
12 from widgets import create_button
13
14
15 class Tracker:
16 def __init__(self):
17 self.clear()
18
19 def clear(self):
20 self.positions = []
21 self.last_pos = 0
22 self.queue = []
23
24 def parse_marked_text(self, marked_text):
25 self.clear()
26 text = []
27 text_len = chunk_len = index_in_positions = offset_in_text = 0
28 limit = 2048
29
30 def commit():
31 self.queue.push({
32 'text': ''.join(text), 'index_in_positions': index_in_positions,
33 'offset_in_text': offset_in_text, 'reached_offset': 0})
34
35 for x in marked_text:
36 if jstype(x) is 'number':
37 self.positions.push({'mark': x, 'offset_in_text': text_len})
38 else:
39 text_len += x.length
40 chunk_len += x.length
41 text.push(x)
42 if chunk_len > limit:
43 commit()
44 chunk_len = 0
45 text = []
46 index_in_positions = max(0, self.positions.length - 1)
47 offset_in_text = text_len
48 if text.length:
49 commit()
50 self.marked_text = marked_text
51 return self.current_text()
52
53 def pop_first(self):
54 self.queue.splice(0, 1)
55 self.last_pos = 0
56 if self.queue.length:
57 self.last_pos = self.queue[0].index_in_positions
58
59 def current_text(self):
60 if self.queue.length:
61 return self.queue[0].text
62 return ''
63
64 def resume(self):
65 self.last_pos = 0
66 if self.queue.length:
67 self.last_pos = self.queue[0].index_in_positions
68 if self.queue[0].reached_offset:
69 o = self.queue[0].reached_offset
70 # make sure positions remain the same for word tracking
71 self.queue[0].text = (' ' * o) + self.queue[0].text[o:]
72 return self.current_text()
73
74 def boundary_reached(self, start):
75 if self.queue.length:
76 self.queue[0].reached_offset = start
77
78 def mark_word(self, start, length):
79 if not self.queue.length:
80 return
81 start += self.queue[0].offset_in_text
82 end = start + length
83 matches = []
84 while self.last_pos < self.positions.length:
85 pos = self.positions[self.last_pos]
86 if start <= pos.offset_in_text < end:
87 matches.push(pos)
88 elif pos.offset_in_text >= end:
89 break
90 self.last_pos += 1
91 if matches.length:
92 return matches[0].mark, matches[-1].mark
93 return None
94
95
96 class Client:
97 min_rate = 0.1
98 max_rate = 2
99
100 def __init__(self):
101 self.stop_requested_at = None
102 self.status = {'synthesizing': False, 'paused': False}
103 self.tracker = Tracker([])
104 self.last_reached_mark = None
105 self.onevent = def ():
106 pass
107 data = get_session_data().get('tts_backend')
108 self.current_voice_uri = data.voice or ''
109 self.current_rate = data.rate or None
110
111 def create_utterance(self, text):
112 ut = new window.SpeechSynthesisUtterance(text)
113 ut.onstart = self.utterance_started
114 ut.onpause = self.utterance_paused
115 ut.onend = self.utterance_ended
116 ut.onerror = self.utterance_failed
117 ut.onresume = self.utterance_resumed
118 ut.addEventListener('boundary', self.utterance_boundary_reached)
119 if self.current_voice_uri:
120 for voice in window.speechSynthesis.getVoices():
121 if voice.voiceURI is self.current_voice_uri:
122 ut.voice = voice
123 break
124 if self.current_rate:
125 ut.rate = self.current_rate
126 return ut
127
128 def utterance_started(self, event):
129 self.status = {'synthesizing': True, 'paused': False}
130 self.onevent('begin')
131
132 def utterance_paused(self, event):
133 self.status = {'synthesizing': True, 'paused': True}
134 self.onevent('pause')
135
136 def speak(self, text):
137 ut, self.current_utterance = self.current_utterance, None
138 if ut:
139 ut.onstart = ut.onpause = ut.onend = ut.onerror = ut.onresume = None
140 ut.removeEventListener('boundary', self.utterance_boundary_reached)
141 if text and text.length:
142 self.current_utterance = self.create_utterance(text)
143 window.speechSynthesis.speak(self.current_utterance)
144
145 def utterance_ended(self, event):
146 self.status = {'synthesizing': False, 'paused': False}
147 if self.stop_requested_at? and window.performance.now() - self.stop_requested_at < 1000:
148 self.stop_requested_at = None
149 return
150 self.tracker.pop_first()
151 text = self.tracker.current_text()
152 if text and text.length:
153 self.speak(text)
154 else:
155 self.onevent('end')
156
157 def utterance_failed(self, event):
158 self.status = {'synthesizing': False, 'paused': False}
159 self.tracker.clear()
160 if event.error is not 'interrupted' and event.error is not 'canceled':
161 if event.error is 'synthesis-unavailable':
162 msg = _('Text-to-Speech not available in this browser. You may need to install some Text-to-Speech software.')
163 else:
164 msg = _('An error has occurred with speech synthesis: ') + event.error
165 error_dialog(_('Speaking failed'), msg)
166 self.onevent('cancel')
167
168 def utterance_boundary_reached(self, event):
169 self.tracker.boundary_reached(event.charIndex)
170 if event.name is 'word':
171 x = self.tracker.mark_word(event.charIndex, event.charLength or 2)
172 if x:
173 first, last = x[0], x[1]
174 self.onevent('mark', {'first': first, 'last': last})
175
176 def utterance_resumed(self, event):
177 self.status = {'synthesizing': True, 'paused': False}
178 self.onevent('resume')
179
180 def pause(self):
181 window.speechSynthesis.pause()
182
183 def resume(self):
184 window.speechSynthesis.resume()
185
186 def pause_for_configure(self):
187 if self.current_utterance:
188 ut = self.current_utterance
189 self.current_utterance = None
190 ut.onstart = ut.onpause = ut.onend = ut.onerror = ut.onresume = None
191 ut.removeEventListener('boundary', self.utterance_boundary_reached)
192 window.speechSynthesis.cancel()
193
194 def resume_after_configure(self):
195 text = self.tracker.resume()
196 if text and text.length:
197 self.speak(text)
198
199 def stop(self):
200 self.tracker.clear()
201 self.stop_requested_at = window.performance.now()
202 window.speechSynthesis.cancel()
203 self.status = {'synthesizing': False, 'paused': False}
204
205 def speak_simple_text(self, text):
206 self.stop()
207 text = self.tracker.parse_marked_text([text])
208 if text and text.length:
209 self.speak(text)
210
211 def speak_marked_text(self, text_segments, onevent):
212 self.stop()
213 self.onevent = onevent
214 text = self.tracker.parse_marked_text(text_segments)
215 if text and text.length:
216 self.speak(text)
217
218 def faster(self):
219 self.change_rate(steps=1)
220
221 def slower(self):
222 self.change_rate(steps=-1)
223
224 def save_settings(self):
225 sd = get_session_data()
226 sd.set('tts_backend', {'voice': self.current_voice_uri, 'rate': self.current_rate})
227
228 def change_rate(self, steps=1):
229 rate = current_rate = (self.current_rate or 1) * 10
230 rate += steps
231 rate /= 10
232 rate = max(self.min_rate, min(rate, self.max_rate))
233 if rate is not current_rate:
234 is_speaking = bool(window.speechSynthesis.speaking)
235 if is_speaking:
236 self.pause_for_configure()
237 self.current_rate = rate
238 self.save_settings()
239 if is_speaking:
240 self.resume_after_configure()
241
242 def configure(self):
243 voice_id = unique_id()
244 rate_id = unique_id()
245 select_id = unique_id()
246 sd = get_session_data()
247 current_bar_pos = sd.get('tts_bar_position')
248
249 def restore_defaults():
250 document.getElementById(voice_id).selectedIndex = -1
251 document.getElementById(rate_id).value = 10
252 document.getElementById(select_id).value = session_defaults().tts_bar_position
253
254 create_custom_dialog(_('Configure Text-to-Speech'), def (parent_div, close_modal):
255 pos_select = E.select(id=select_id)
256 def apos(text, name):
257 option = E.option(text, value=name)
258 if name is current_bar_pos:
259 option.setAttribute('selected', 'selected')
260 pos_select.appendChild(option)
261 apos(_('Floating with help text'), 'float')
262 apos(_('Top'), 'top')
263 apos(_('Bottom'), 'bottom')
264 apos(_('Top right'), 'top-right')
265 apos(_('Top left'), 'top-left')
266 apos(_('Bottom right'), 'bottom-right')
267 apos(_('Bottom left'), 'bottom-left')
268 select = E.select(size='5', id=voice_id, style='display:block')
269 voices = window.speechSynthesis.getVoices()
270 voices.sort(def (a, b):
271 a = a.name.toLowerCase()
272 b = b.name.toLowerCase()
273 return -1 if a < b else (0 if a is b else 1)
274 )
275 voice_filter = E.input(type='text', placeholder=_('Filter voices'), style='display: block')
276 seen = {}
277 for voice in voices:
278 if not seen[voice.voiceURI]:
279 cloud = ''
280 if not voice.localService:
281 cloud = ' -- ' + _('cloud based')
282 option = E.option(f'{voice.name} ({voice.lang}){cloud}', value=voice.voiceURI)
283 if self.current_voice_uri and voice.voiceURI is self.current_voice_uri:
284 option.setAttribute('selected', 'selected')
285 select.appendChild(option)
286 seen[voice.voiceURI] = True
287 if len(voices) < 1:
288 select.appendChild(E.option(_('No voices available')))
289 select.lastChild.disabled = True
290 else:
291 original = Array.from (select.options).map(def (o): return {'text': o.text, 'value': o.value};)
292 voice_filter.addEventListener('input', def (ev):
293 q = voice_filter.value.trim().toLowerCase()
294 filtered = original.filter(def (item): return item.text.toLowerCase().includes(q);)
295 frag = document.createDocumentFragment()
296 if filtered.length is 0:
297 frag.appendChild(E.option(_('No matches'), disabled=True))
298 else:
299 for item in filtered:
300 frag.appendChild(E.option(item.text, value=item.value))
301 select.replaceChildren(frag)
302 if select.selectedIndex > -1:
303 select.options[select.selectedIndex].scrollIntoView()
304 )
305 parent_div.appendChild(E.div(_('Speed of speech:') + '\xa0'))
306 parent_div.lastChild.appendChild(
307 E.input(type='range', id=rate_id, min=(self.min_rate * 10) + '', max=(self.max_rate * 10) + '', value=((self.current_rate or 1) * 10) + ''))
308 parent_div.appendChild(E.div(_('Voice:') + '\xa0'))
309 voice_div = E.div(select, voice_filter, style='display: inline-block; vertical-align: middle')
310 parent_div.lastChild.appendChild(voice_div)
311 if select.selectedIndex > -1 and select.options[select.selectedIndex]:
312 select.options[select.selectedIndex].scrollIntoView()
313 parent_div.appendChild(E.div(_('Position of bar:') + '\xa0', pos_select, style='margin-top: 1rem'))
314 parent_div.appendChild(E.div(
315 class_='button-box',
316 style='margin: 1rem',
317 create_button(_('Close'), action=close_modal),
318 create_button(_('Restore defaults'), action=restore_defaults),
319 ))
320 , on_close = def ():
321 voice = document.getElementById(voice_id).value or ''
322 bar_pos = document.getElementById(select_id).value
323 rate = int(document.getElementById(rate_id).value) / 10
324 if rate is 1:
325 rate = None
326 changed = voice is not self.current_voice_uri or rate is not self.current_rate or bar_pos is not current_bar_pos
327 if changed:
328 self.current_voice_uri = voice
329 self.current_rate = rate
330 if bar_pos is not current_bar_pos:
331 sd.set('tts_bar_position', bar_pos)
332 is_speaking = bool(window.speechSynthesis.speaking)
333 if is_speaking:
334 self.pause_for_configure()
335 self.save_settings()
336 if is_speaking:
337 self.resume_after_configure()
338 self.onevent('configured')
339 )