More work on browser tts backend

This commit is contained in:
Kovid Goyal 2024-08-25 22:16:28 +05:30
parent 640193a52f
commit 89fd105fe3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -114,6 +114,12 @@ class Client:
self.status = {'synthesizing': True, 'paused': True} self.status = {'synthesizing': True, 'paused': True}
self.onevent('pause') self.onevent('pause')
def speak(self, text):
self.current_utterance = None
if text and text.length:
self.current_utterance = self.create_utterance(text)
window.speechSynthesis.speak(self.current_utterance)
def utterance_ended(self, event): def utterance_ended(self, event):
self.status = {'synthesizing': False, 'paused': False} self.status = {'synthesizing': False, 'paused': False}
if self.stop_requested_at? and window.performance.now() - self.stop_requested_at < 1000: if self.stop_requested_at? and window.performance.now() - self.stop_requested_at < 1000:
@ -122,7 +128,7 @@ class Client:
self.tracker.pop_first() self.tracker.pop_first()
text = self.tracker.current_text() text = self.tracker.current_text()
if text and text.length: if text and text.length:
window.speechSynthesis.speak(text) self.speak(text)
else: else:
self.onevent('end') self.onevent('end')
@ -154,7 +160,7 @@ class Client:
def resume_after_configure(self): def resume_after_configure(self):
text = self.tracker.resume() text = self.tracker.resume()
if text and text.length: if text and text.length:
window.speechSynthesis.speak(text) self.speak(text)
def stop(self): def stop(self):
self.tracker.clear() self.tracker.clear()
@ -166,14 +172,14 @@ class Client:
self.stop() self.stop()
text = self.tracker.parse_marked_text(v'[text]') text = self.tracker.parse_marked_text(v'[text]')
if text and text.length: if text and text.length:
window.speechSynthesis.speak(self.create_utterance(text)) self.speak(text)
def speak_marked_text(self, text_segments, onevent): def speak_marked_text(self, text_segments, onevent):
self.stop() self.stop()
self.onevent = onevent self.onevent = onevent
text = self.tracker.parse_marked_text(text_segments) text = self.tracker.parse_marked_text(text_segments)
if text and text.length: if text and text.length:
window.speechSynthesis.speak(self.create_utterance(text)) self.speak(text)
def faster(self): def faster(self):
self.change_rate(steps=1) self.change_rate(steps=1)
@ -181,20 +187,18 @@ class Client:
def slower(self): def slower(self):
self.change_rate(steps=-1) self.change_rate(steps=-1)
def apply_settings(self): def save_settings(self):
sd = get_session_data() sd = get_session_data()
sd.set('tts_backend', {'voice': self.current_voice_uri, 'rate': self.current_rate}) sd.set('tts_backend', {'voice': self.current_voice_uri, 'rate': self.current_rate})
def change_rate(self, steps=1): def change_rate(self, steps=1):
rate = current_rate = (self.current_rate or 1) * 10 rate = current_rate = (self.current_rate or 1) * 10
step_size = 2 rate += steps
rate += steps * step_size
rate /= 10 rate /= 10
rate = max(self.min_rate, min(rate, self.max_rate)) rate = max(self.min_rate, min(rate, self.max_rate))
if rate is not current_rate: if rate is not current_rate:
self.current_rate = rate self.current_rate = rate
self.apply_settings() self.save_settings()
self.resume_after_configure()
def configure(self): def configure(self):
voice_id = unique_id() voice_id = unique_id()
@ -246,7 +250,7 @@ class Client:
if changed: if changed:
self.current_voice_uri = voice self.current_voice_uri = voice
self.current_rate = rate self.current_rate = rate
self.apply_settings() self.save_settings()
self.onevent('configured') self.onevent('configured')
) )