From 20f08f9791f468ed104aa1d01a77042306ec1893 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 22 Nov 2020 19:06:50 +0530 Subject: [PATCH] Implement pause/resume/stop for winsapi --- src/calibre/gui2/tts/windows.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/calibre/gui2/tts/windows.py b/src/calibre/gui2/tts/windows.py index 9506931527..a264b38dae 100644 --- a/src/calibre/gui2/tts/windows.py +++ b/src/calibre/gui2/tts/windows.py @@ -69,7 +69,7 @@ class Client: SPF_ASYNC, SPF_IS_NOT_XML, SPF_PURGEBEFORESPEAK ) self.current_callback = None - self.current_stream_number = self.sp_voice.speak(text, SPF_ASYNC | SPF_PURGEBEFORESPEAK | SPF_IS_NOT_XML) + self.current_stream_number = self.sp_voice.speak(text, SPF_ASYNC | SPF_PURGEBEFORESPEAK | SPF_IS_NOT_XML, True) def speak_marked_text(self, text, callback): from calibre_extensions.winsapi import ( @@ -77,3 +77,27 @@ class Client: ) self.current_callback = callback self.current_stream_number = self.sp_voice.speak(text, SPF_ASYNC | SPF_PURGEBEFORESPEAK | SPF_IS_XML, True) + + def stop(self): + from calibre_extensions.winsapi import SPF_PURGEBEFORESPEAK + if self.status['paused']: + self.sp_voice.resume() + self.sp_voice.speak('', SPF_PURGEBEFORESPEAK, False) + self.status = {'synthesizing': False, 'paused': False} + if self.current_callback is not None: + self.current_callback(Event(EventType.cancel)) + self.current_callback = None + + def pause(self): + if self.status['synthesizing'] and not self.status['paused']: + self.sp_voice.pause() + self.status = {'synthesizing': True, 'paused': True} + if self.current_callback is not None: + self.current_callback(Event(EventType.pause)) + + def resume(self): + if self.status['paused']: + self.sp_voice.resume() + self.status = {'synthesizing': True, 'paused': False} + if self.current_callback is not None: + self.current_callback(Event(EventType.resume))