Debian Codegraph @

src/pyj/read_book/read_audio_ebook.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2023, DO LE DUY <duy.dole.00ece at gmail.com>
3
4
5 # Public domain audio eBooks can be found on https://www.readbeyond.it/ebooks.html.
6 # ReadBeyond also offers Aeneas (https://github.com/readbeyond/aeneas),
7 # an open-source tool for force-alignment of audio and text to generate smil files.
8 # Another notable tool is https://github.com/r4victor/syncabook,
9 # builds upon Aeneas to complete a workflow for creating EPUB3 with Media Overlays.
10
11 from __python__ import bound_methods, hash_literals
12
13 from elementmaker import E
14 from gettext import gettext as _
15
16 from book_list.theme import get_color
17 from dom import change_icon_image, svgicon
18 from modals import error_dialog
19 from read_book.globals import ui_operations
20 from read_book.highlights import ICON_SIZE
21 from read_book.selection_bar import BUTTON_MARGIN, get_margins, map_to_iframe_coords
22 from read_book.shortcuts import shortcut_for_key_event
23 from read_book.smil import (
24 find_next_audio_in_spine, get_smil_id_for_timestamp, next_audio_file_for_spine_item
25 )
26
27 HIDDEN, PLAYING, PAUSED = 'HIDDEN', 'PLAYING', 'PAUSED'
28
29
30 def seconds_to_ms(seconds):
31 minutes = Math.floor(seconds / 60)
32 remaining_seconds = int(seconds % 60)
33 return str(minutes) + ':' + (str(remaining_seconds).zfill(2))
34
35
36 class ReadAudioEbook:
37 dont_hide_on_content_loaded = True
38 prevent_navigation = True
39 CONTAINER_DISPLAY = 'flex'
40
41 def __init__(self, view):
42 self.view = view
43 self._state = HIDDEN
44 self.initialized = False
45 self.current_audio_src = ''
46
47 def initialize(self):
48 if self.initialized:
49 return
50 self.initialized = True
51
52 container = self.container
53 container.style.backgroundColor = 'rgba(0, 0, 0, 0)'
54 container.style.justifyContent = 'center'
55 container.style.alignItems = 'flex-end'
56 container.setAttribute('tabindex', '0')
57 container.appendChild(E.div(
58 data_component='bar',
59 style=f'position: absolute; bottom: 0; width: min(600px, 80vw); height: 2em;'
60 + f' border-radius: 1em; padding:0.5em; display: flex; justify-content: center;'
61 + f' align-items: center; background-color: {get_color("window-background")};'
62 ))
63 bar_container = container.lastChild
64 container.appendChild(E.style(
65 f'#{container.id} .speaking ' + '{ opacity: 0.5 }\n\n',
66 f'#{container.id} .speaking:hover ' + '{ opacity: 1.0 }\n\n',
67 ))
68 container.addEventListener('keydown', self.on_keydown, {'passive': False})
69 container.addEventListener('click', self.on_container_clicked, {'passive': False})
70 container.addEventListener('contextmenu', self.toggle, {'passive': False})
71
72 def create_button(name, icon, text):
73 ans = svgicon(icon, ICON_SIZE, ICON_SIZE, text)
74 if name:
75 ans.addEventListener('click', def (ev):
76 ev.stopPropagation(), ev.preventDefault()
77 self[name](ev)
78 self.view.focus_iframe()
79 )
80 ans.dataset.button = name
81 ans.classList.add('simple-link')
82 ans.style.marginLeft = ans.style.marginRight = BUTTON_MARGIN
83 return ans
84
85 for x in [
86 E.div(style='height: 3ex; display: flex; align-items: center; justify-content: center', create_button('toggle', 'pause', _('Pause audio')),),
87 E.div(data_component='time_display', E.text('')),
88 E.div(
89 data_component='progress_bar',
90 style=f'height:1.5em; border-radius: 0.75em; overflow: hidden; flex-grow: 100; display:block;'
91 + f' background-color:{get_color("window-background2")}; margin:1em; cursor: pointer',
92 E.div(style=f'display:block; width: 0%; background-color:{get_color("window-foreground")}; height:100%'),
93 ),
94 E.div(
95 style='height: 3ex; display: flex; align-items: center; justify-content: center',
96 create_button('slower', 'slower', _('Slow down audio')),
97 create_button('faster', 'faster', _('Speed up audio')),
98 create_button('hide', 'off', _('Close Read aloud')),
99 ),
100 ]:
101 bar_container.appendChild(x)
102
103 bar_container.addEventListener('click', self.on_bar_clicked, {'passive': False})
104 self.container.appendChild(E.audio(data_component='audio', style='display:none'))
105 ap = self.container.lastChild
106
107 ap.addEventListener('timeupdate', def ():
108 if self.state is not HIDDEN:
109 ap = self.audio_player
110 if ap.duration:
111 audio_current_time = ap.currentTime
112 progress = (audio_current_time / ap.duration) * 100
113 self.progress_bar.firstChild.style.width = progress + '%'
114 self.time_display.textContent = f'{seconds_to_ms(audio_current_time)}/{seconds_to_ms(ap.duration)}'
115 self.mark_for_timeupdate(audio_current_time)
116 else:
117 self.time_display.textContent = '00:00'
118 self.progress_bar.firstChild.style.width = '0%'
119 )
120
121 ap.addEventListener('ended', def ():
122 self.play_next_audio_file()
123 )
124
125 self.progress_bar.addEventListener('click', def (event):
126 ap = self.audio_player
127 if ap.duration:
128 rect = self.progress_bar.getBoundingClientRect()
129 x = event.clientX - rect.left
130 total_width = rect.width
131 if total_width:
132 skip_time = (x / total_width) * ap.duration
133 ap.currentTime = skip_time
134 )
135
136 @property
137 def bar(self):
138 return self.container.querySelector('[data-component=bar]')
139
140 @property
141 def time_display(self):
142 return self.container.querySelector('[data-component=time_display]')
143
144 @property
145 def progress_bar(self):
146 return self.container.querySelector('[data-component=progress_bar]')
147
148 def mark_for_timeupdate(self, audio_time):
149 sam = self.view.currently_showing.smil_audio_map
150 if sam:
151 smil_id, idx = get_smil_id_for_timestamp(self.current_audio_src, audio_time, sam, self.last_marked_smil_idx)
152 if smil_id and smil_id is not self.last_marked_smil_id:
153 self.send_message('mark', anchor=smil_id, idx=idx)
154
155 def play_next_audio_file(self):
156 next_audio_file, par = next_audio_file_for_spine_item(self.current_audio_src, self.view.currently_showing.smil_audio_map)
157 if next_audio_file:
158 self.set_audio_src(next_audio_file, def ():
159 self.audio_player.currentTime = par.audio?.start or 0
160 self.play()
161 )
162 return
163 spine_name, par = find_next_audio_in_spine(self.view.currently_showing.spine_index, self.view.book.manifest)
164 if spine_name:
165 self.view.show_name(spine_name, initial_position={'type': 'smil_id', 'anchor': par.anchor})
166 else:
167 self.hide()
168
169 def set_audio_src(self, name, proceed):
170 if self.current_audio_src is name:
171 proceed()
172 return
173 self.last_marked_smil_id = self.last_marked_smil_idx = None
174 self.current_audio_src = name
175 ap = self.audio_player
176 if ui_operations.get_url_for_book_file_name:
177 ap.onloadeddata = def ():
178 proceed()
179 ap.onerror = def (evt):
180 console.error(evt)
181 error_dialog(_('Could not load audio'), _(
182 'Could not load the audio file: {} with error: {}').format(name, evt.message))
183 ap.src = ui_operations.get_url_for_book_file_name(name)
184 return
185 if ap.src:
186 ap.onloadeddata = def ():
187 pass
188 ap.onerror = def ():
189 pass
190 window.URL.revokeObjectURL(ap.src)
191 ap.src = ''
192 ui_operations.get_file(self.view.book, name, def (blob, name, mimetype):
193 ap = self.audio_player
194 ap.onloadeddata = def ():
195 proceed()
196 ap.onerror = def (evt):
197 console.error(evt)
198 error_dialog(_('Could not load audio'), _(
199 'Could not load the audio file: {} with error: {}').format(name, evt.message))
200 ap.src = window.URL.createObjectURL(blob)
201 )
202
203 def start_playback(self):
204 cn = self.view.currently_showing.name
205 if self.view.book.manifest.files[cn]?.smil_map:
206 self.send_message('play')
207 return
208 spine_name, par = find_next_audio_in_spine(self.view.currently_showing.spine_index, self.view.book.manifest)
209 if not spine_name:
210 spine_name, par = find_next_audio_in_spine(-1, self.view.book.manifest)
211 if spine_name:
212 self.view.show_name(spine_name, initial_position={'type': 'smil_id', 'anchor': par.anchor})
213 else:
214 self.hide()
215 error_dialog(_('No audio'), _('No audio found in this book'))
216
217 @property
218 def container(self):
219 return document.getElementById('audio-ebooks-overlay')
220
221 @property
222 def audio_player(self):
223 return self.container.querySelector('[data-component=audio]')
224
225 @property
226 def is_visible(self):
227 return self.container.style.display is not 'none'
228
229 @property
230 def state(self):
231 return self._state
232
233 @state.setter
234 def state(self, val):
235 if val is not self._state:
236 speaking = False
237 if val is HIDDEN:
238 self._state = HIDDEN
239 elif val is PLAYING:
240 self._state = PLAYING
241 speaking = True
242 elif val is PAUSED:
243 self._state = PAUSED
244 if speaking:
245 self.bar.classList.add('speaking')
246 else:
247 self.bar.classList.remove('speaking')
248
249 def hide(self):
250 if self.state is not HIDDEN:
251 self.send_message('mark')
252 self.pause()
253 self.container.style.display = 'none'
254 self.view.focus_iframe()
255 self.state = HIDDEN
256 if ui_operations.read_aloud_state_changed:
257 ui_operations.read_aloud_state_changed(False)
258
259 @property
260 def play_pause_button(self):
261 return self.container.querySelector('[data-button="toggle"]')
262
263 def show(self):
264 if self.state is HIDDEN:
265 self.initialize()
266 self.state = PLAYING
267 change_icon_image(self.play_pause_button, 'pause', _('Pause audio'))
268 self.start_playback()
269 self.container.style.display = self.CONTAINER_DISPLAY
270 self.focus()
271 if ui_operations.read_aloud_state_changed:
272 ui_operations.read_aloud_state_changed(True)
273
274 def focus(self):
275 self.container.focus()
276
277 def slower(self):
278 self.audio_player.playbackRate -= 0.1
279
280 def faster(self):
281 self.audio_player.playbackRate += 0.1
282
283 def play(self):
284 self.state = PLAYING
285 change_icon_image(self.play_pause_button, 'pause', _('Pause audio'))
286 ap = self.audio_player
287 if ap.getAttribute('src'):
288 ap.play()
289
290 def pause(self):
291 self.state = PAUSED
292 change_icon_image(self.play_pause_button, 'play', _('Play audio'))
293 ap = self.audio_player
294 if ap.getAttribute('src'):
295 ap.pause()
296
297 def toggle(self):
298 if self.state is PLAYING:
299 self.pause()
300 elif self.state is PAUSED:
301 self.play()
302
303 def on_bar_clicked(self, ev):
304 # prevent the container click handler from handling this
305 ev.stopPropagation(), ev.preventDefault()
306
307 def on_container_clicked(self, ev):
308 if ev.button is not 0:
309 return
310 ev.stopPropagation(), ev.preventDefault()
311 margins = get_margins()
312 pos = {'x': ev.clientX, 'y': ev.clientY}
313 pos = map_to_iframe_coords(pos, margins)
314 self.send_message('play', pos=pos)
315
316 def on_keydown(self, ev):
317 ev.stopPropagation(), ev.preventDefault()
318 if ev.key is 'Escape':
319 self.hide()
320 return
321 if ev.key is ' ' or ev.key is 'MediaPlayPause' or ev.key is 'PlayPause':
322 self.toggle()
323 return
324 if ev.key is 'Play' or ev.key is 'MediaPlay':
325 self.play()
326 return
327 if ev.key is 'Pause' or ev.key is 'MediaPause':
328 self.pause()
329 return
330 sc_name = shortcut_for_key_event(ev, self.view.keyboard_shortcut_map)
331 if not sc_name:
332 return
333 if sc_name is 'show_chrome':
334 self.hide()
335 elif sc_name is 'quit':
336 self.hide()
337
338 def send_message(self, message_type, **kw):
339 self.view.iframe_wrapper.send_message('audio-ebook', type=message_type, **kw)
340
341 def handle_message(self, message):
342 if message.type is 'start_play_at':
343 if message.par:
344 self.set_audio_src(
345 message.par.audio, def ():
346 self.audio_player.currentTime = message.par.start or 0
347 self.play()
348 )
349 else:
350 if message.anchor:
351 # start playing from where we are
352 self.send_message('play')
353 return
354 if message.pos:
355 # this is a click ignore it as no smil element was found at
356 # click location
357 pass
358 else:
359 self.pause()
360 error_dialog(_('Audio element not found'), _(
361 'Could not play audio as no associated audio was found'))
362 elif message.type is 'marked':
363 if message.anchor:
364 self.last_marked_smil_id = message.anchor
365 self.last_marked_smil_idx = message.idx
366 else:
367 self.last_marked_smil_id = self.last_marked_smil_idx = None
368 else:
369 console.error(f'Unknown audio ebook message type from iframe: {message.type}')