From fa124ab696346873b46a541bf5cfec8abfb1020e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 31 Aug 2024 15:29:10 +0530 Subject: [PATCH] Fix stopping when paused in speechd --- src/calibre/gui2/tts2/develop.py | 23 ++++++++++++----------- src/calibre/gui2/tts2/speechd.py | 12 ++++++++---- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/calibre/gui2/tts2/develop.py b/src/calibre/gui2/tts2/develop.py index 3bdd3f9704..07d0f04ae2 100644 --- a/src/calibre/gui2/tts2/develop.py +++ b/src/calibre/gui2/tts2/develop.py @@ -48,8 +48,7 @@ class MainWindow(MainWindow): self.marked_text = marked_text self.play_action = pa = QAction('Play') pa.setShortcut(QKeySequence(Qt.Key.Key_Space)) - pa.setCheckable(True) - pa.toggled.connect(self.toggled) + pa.triggered.connect(self.toggled) self.toolbar.addAction(pa) self.stop_action = sa = QAction('Stop') sa.setShortcut(QKeySequence(Qt.Key.Key_Escape)) @@ -79,18 +78,20 @@ class MainWindow(MainWindow): else: self.play_action.setChecked(True) self.stop_action.setEnabled(state in (QTextToSpeech.State.Speaking, QTextToSpeech.State.Synthesizing, QTextToSpeech.State.Paused)) + if self.tts.state is QTextToSpeech.State.Paused: + self.play_action.setText('Resume') + elif self.tts.state is QTextToSpeech.State.Speaking: + self.play_action.setText('Pause') + else: + self.play_action.setText('Play') def toggled(self): - if self.play_action.isChecked(): - self.play_action.setText('Pause') - if self.tts.state is QTextToSpeech.State.Paused: - self.tts.resume() - elif self.tts.state in (QTextToSpeech.State.Ready, QTextToSpeech.State.Error): - self.tts.speak_marked_text(self.marked_text) + if self.tts.state is QTextToSpeech.State.Paused: + self.tts.resume() + elif self.tts.state is QTextToSpeech.State.Speaking: + self.tts.pause() else: - if self.tts.state in (QTextToSpeech.State.Speaking, QTextToSpeech.State.Synthesizing): - self.tts.pause() - self.play_action.setText('Play') + self.tts.speak_marked_text(self.marked_text) def saying(self, first, last): c = self.display.textCursor() diff --git a/src/calibre/gui2/tts2/speechd.py b/src/calibre/gui2/tts2/speechd.py index 873f2c0702..cc9571c0bd 100644 --- a/src/calibre/gui2/tts2/speechd.py +++ b/src/calibre/gui2/tts2/speechd.py @@ -76,10 +76,14 @@ class SpeechdTTSBackend(TTSBackend): def stop(self) -> None: self._last_mark = self._last_text = '' if self._ssip_client is not None: - try: - self._ssip_client.stop() - except Exception as e: - self._set_error(str(e)) + if self._status['paused'] and self._status['synthesizing']: + self._status = {'synthesizing': False, 'paused': False} + self._set_state(QTextToSpeech.State.Ready) + else: + try: + self._ssip_client.stop() + except Exception as e: + self._set_error(str(e)) def say(self, text: str) -> None: self.stop()