MACOSX: Fix TTS when stopping speech and immediately starting another

This commit is contained in:
Thierry Crozat 2020-06-21 19:13:02 +01:00
parent 6c2aaba04f
commit 025dd965f5

View file

@ -33,20 +33,29 @@
@interface MacOSXTextToSpeechManagerDelegate : NSObject<NSSpeechSynthesizerDelegate> {
MacOSXTextToSpeechManager *_ttsManager;
BOOL _ignoreNextFinishedSpeaking;
}
- (id)initWithManager:(MacOSXTextToSpeechManager*)ttsManager;
- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)finishedSpeaking;
- (void)ignoreNextFinishedSpeaking:(BOOL)ignore;
@end
@implementation MacOSXTextToSpeechManagerDelegate
- (id)initWithManager:(MacOSXTextToSpeechManager*)ttsManager {
self = [super init];
_ttsManager = ttsManager;
_ignoreNextFinishedSpeaking = NO;
return self;
}
- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)finishedSpeaking {
_ttsManager->startNextSpeech();
if (!_ignoreNextFinishedSpeaking)
_ttsManager->startNextSpeech();
_ignoreNextFinishedSpeaking = NO;
}
- (void)ignoreNextFinishedSpeaking:(BOOL)ignore {
_ignoreNextFinishedSpeaking = ignore;
}
@end
@ -133,9 +142,15 @@ bool MacOSXTextToSpeechManager::startNextSpeech() {
bool MacOSXTextToSpeechManager::stop() {
_messageQueue.clear();
_currentSpeech.clear(); // so that it immediately reports that it is no longer speeking
// Stop as soon as possible
[synthesizer stopSpeakingAtBoundary:NSSpeechImmediateBoundary];
if (isSpeaking()) {
_currentSpeech.clear(); // so that it immediately reports that it is no longer speeking
// Stop as soon as possible
// Also tell the MacOSXTextToSpeechManagerDelegate to ignore the next finishedSpeaking as
// it has already been handled, but we might have started another speach by the time we
// receive it, and we don't want to stop that one.
[synthesizerDelegate ignoreNextFinishedSpeaking:YES];
[synthesizer stopSpeakingAtBoundary:NSSpeechImmediateBoundary];
}
return true;
}