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