| 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.trim().length: |
| 216 |
self.speak(text) |
| 217 |
else: |
| 218 |
# no speakable text, fire the end event so the client knows |
| 219 |
# speaking is done, see https://bugs.launchpad.net/bugs/2165923 |
| 220 |
onevent('end') |
| 221 |
|
| 222 |
def faster(self): |
| 223 |
self.change_rate(steps=1) |
| 224 |
|
| 225 |
def slower(self): |
| 226 |
self.change_rate(steps=-1) |
| 227 |
|
| 228 |
def save_settings(self): |
| 229 |
sd = get_session_data() |
| 230 |
sd.set('tts_backend', {'voice': self.current_voice_uri, 'rate': self.current_rate}) |
| 231 |
|
| 232 |
def change_rate(self, steps=1): |
| 233 |
rate = current_rate = (self.current_rate or 1) * 10 |
| 234 |
rate += steps |
| 235 |
rate /= 10 |
| 236 |
rate = max(self.min_rate, min(rate, self.max_rate)) |
| 237 |
if rate is not current_rate: |
| 238 |
is_speaking = bool(window.speechSynthesis.speaking) |
| 239 |
if is_speaking: |
| 240 |
self.pause_for_configure() |
| 241 |
self.current_rate = rate |
| 242 |
self.save_settings() |
| 243 |
if is_speaking: |
| 244 |
self.resume_after_configure() |
| 245 |
|
| 246 |
def configure(self): |
| 247 |
voice_id = unique_id() |
| 248 |
rate_id = unique_id() |
| 249 |
select_id = unique_id() |
| 250 |
sd = get_session_data() |
| 251 |
current_bar_pos = sd.get('tts_bar_position') |
| 252 |
|
| 253 |
def restore_defaults(): |
| 254 |
document.getElementById(voice_id).selectedIndex = -1 |
| 255 |
document.getElementById(rate_id).value = 10 |
| 256 |
document.getElementById(select_id).value = session_defaults().tts_bar_position |
| 257 |
|
| 258 |
create_custom_dialog(_('Configure Text-to-Speech'), def (parent_div, close_modal): |
| 259 |
pos_select = E.select(id=select_id) |
| 260 |
def apos(text, name): |
| 261 |
option = E.option(text, value=name) |
| 262 |
if name is current_bar_pos: |
| 263 |
option.setAttribute('selected', 'selected') |
| 264 |
pos_select.appendChild(option) |
| 265 |
apos(_('Floating with help text'), 'float') |
| 266 |
apos(_('Top'), 'top') |
| 267 |
apos(_('Bottom'), 'bottom') |
| 268 |
apos(_('Top right'), 'top-right') |
| 269 |
apos(_('Top left'), 'top-left') |
| 270 |
apos(_('Bottom right'), 'bottom-right') |
| 271 |
apos(_('Bottom left'), 'bottom-left') |
| 272 |
select = E.select(size='5', id=voice_id, style='display:block') |
| 273 |
voices = window.speechSynthesis.getVoices() |
| 274 |
voices.sort(def (a, b): |
| 275 |
a = a.name.toLowerCase() |
| 276 |
b = b.name.toLowerCase() |
| 277 |
return -1 if a < b else (0 if a is b else 1) |
| 278 |
) |
| 279 |
voice_filter = E.input(type='text', placeholder=_('Filter voices'), style='display: block') |
| 280 |
seen = {} |
| 281 |
for voice in voices: |
| 282 |
if not seen[voice.voiceURI]: |
| 283 |
cloud = '' |
| 284 |
if not voice.localService: |
| 285 |
cloud = ' -- ' + _('cloud based') |
| 286 |
option = E.option(f'{voice.name} ({voice.lang}){cloud}', value=voice.voiceURI) |
| 287 |
if self.current_voice_uri and voice.voiceURI is self.current_voice_uri: |
| 288 |
option.setAttribute('selected', 'selected') |
| 289 |
select.appendChild(option) |
| 290 |
seen[voice.voiceURI] = True |
| 291 |
if len(voices) < 1: |
| 292 |
select.appendChild(E.option(_('No voices available'))) |
| 293 |
select.lastChild.disabled = True |
| 294 |
else: |
| 295 |
original = Array.from (select.options).map(def (o): return {'text': o.text, 'value': o.value};) |
| 296 |
voice_filter.addEventListener('input', def (ev): |
| 297 |
q = voice_filter.value.trim().toLowerCase() |
| 298 |
filtered = original.filter(def (item): return item.text.toLowerCase().includes(q);) |
| 299 |
frag = document.createDocumentFragment() |
| 300 |
if filtered.length is 0: |
| 301 |
frag.appendChild(E.option(_('No matches'), disabled=True)) |
| 302 |
else: |
| 303 |
for item in filtered: |
| 304 |
frag.appendChild(E.option(item.text, value=item.value)) |
| 305 |
select.replaceChildren(frag) |
| 306 |
if select.selectedIndex > -1: |
| 307 |
select.options[select.selectedIndex].scrollIntoView() |
| 308 |
) |
| 309 |
parent_div.appendChild(E.div(_('Speed of speech:') + '\xa0')) |
| 310 |
parent_div.lastChild.appendChild( |
| 311 |
E.input(type='range', id=rate_id, min=(self.min_rate * 10) + '', max=(self.max_rate * 10) + '', value=((self.current_rate or 1) * 10) + '')) |
| 312 |
parent_div.appendChild(E.div(_('Voice:') + '\xa0')) |
| 313 |
voice_div = E.div(select, voice_filter, style='display: inline-block; vertical-align: middle') |
| 314 |
parent_div.lastChild.appendChild(voice_div) |
| 315 |
if select.selectedIndex > -1 and select.options[select.selectedIndex]: |
| 316 |
select.options[select.selectedIndex].scrollIntoView() |
| 317 |
parent_div.appendChild(E.div(_('Position of bar:') + '\xa0', pos_select, style='margin-top: 1rem')) |
| 318 |
parent_div.appendChild(E.div( |
| 319 |
class_='button-box', |
| 320 |
style='margin: 1rem', |
| 321 |
create_button(_('Close'), action=close_modal), |
| 322 |
create_button(_('Restore defaults'), action=restore_defaults), |
| 323 |
)) |
| 324 |
, on_close = def (): |
| 325 |
voice = document.getElementById(voice_id).value or '' |
| 326 |
bar_pos = document.getElementById(select_id).value |
| 327 |
rate = int(document.getElementById(rate_id).value) / 10 |
| 328 |
if rate is 1: |
| 329 |
rate = None |
| 330 |
changed = voice is not self.current_voice_uri or rate is not self.current_rate or bar_pos is not current_bar_pos |
| 331 |
if changed: |
| 332 |
self.current_voice_uri = voice |
| 333 |
self.current_rate = rate |
| 334 |
if bar_pos is not current_bar_pos: |
| 335 |
sd.set('tts_bar_position', bar_pos) |
| 336 |
is_speaking = bool(window.speechSynthesis.speaking) |
| 337 |
if is_speaking: |
| 338 |
self.pause_for_configure() |
| 339 |
self.save_settings() |
| 340 |
if is_speaking: |
| 341 |
self.resume_after_configure() |
| 342 |
self.onevent('configured') |
| 343 |
) |