Skip to content

Commit

Permalink
fix issue "'Started-word' event does not work correctly nateshmbhat#78"…
Browse files Browse the repository at this point in the history
… in sapi5 driver for win.
  • Loading branch information
dr0id committed Mar 24, 2021
1 parent 1e2cd7f commit 447a473
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pyttsx3/drivers/sapi5.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def say(self, text):
self._proxy.setBusy(True)
self._proxy.notify('started-utterance')
self._speaking = True
self._tts.Speak(fromUtf8(toUtf8(text)))
# call this async otherwise this blocks the callbacks
# see SpeechVoiceSpeakFlags: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms720892%28v%3dvs.85%29
# and Speak : https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms723609(v=vs.85)
self._tts.Speak(fromUtf8(toUtf8(text)), 1) # -> stream_number as described in the remarks of the documentation

def stop(self):
if not self._speaking:
Expand Down Expand Up @@ -148,14 +151,18 @@ def __init__(self):
def setDriver(self, driver):
self._driver = driver

def _ISpeechVoiceEvents_StartStream(self, char, length):
def _ISpeechVoiceEvents_StartStream(self, stream_number, stream_position):
self._driver._proxy.notify(
'started-word', location=char, length=length)
'started-word', location=stream_number, length=stream_position)

def _ISpeechVoiceEvents_EndStream(self, stream, pos):
def _ISpeechVoiceEvents_EndStream(self, stream_number, stream_position):
d = self._driver
if d._speaking:
d._proxy.notify('finished-utterance', completed=not d._stopping)
d._speaking = False
d._stopping = False
d._proxy.setBusy(False)

def _ISpeechVoiceEvents_Word(self, stream_number, stream_position, char, length):
self._driver._proxy.notify(
'started-word', location=char, length=length)

1 comment on commit 447a473

@skywalker0301
Copy link

@skywalker0301 skywalker0301 commented on 447a473 Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the below are:
"stream_number" is not accessed
"stream_position" is not accessed

Can that be the reason that the output of:
"import pyttsx3

def onWord(name, location, length):
print ('word', name, location, length)
if location > 10:
engine.stop()
engine = pyttsx3.init()
engine.connect('started-word', onWord)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()"

is still:
"word None 1 0
word None 0 3
word None 4 5
word None 10 5
word None 16 3
word None 20 6
word None 27 4
word None 1 40480
word None 1 40480"

def _ISpeechVoiceEvents_EndStream(self, stream_number, stream_position):
d = self._driver
if d._speaking:
d._proxy.notify('finished-utterance', completed=not d._stopping)
d._speaking = False
d._stopping = False
d._proxy.setBusy(False)
def _ISpeechVoiceEvents_Word(self, stream_number, stream_position, char, length):
self._driver._proxy.notify(
'started-word', location=char, length=length)

Please sign in to comment.