pyttsx3 icon indicating copy to clipboard operation
pyttsx3 copied to clipboard

'Started-word' event does not work correctly

Open MauroPfister opened this issue 5 years ago • 16 comments

In the latest version of pyttsx3 the 'started-word' event does not seem to work correctly anymore. Running the following sample code from the documentation produces the output below.

# Listening for events from https://pyttsx3.readthedocs.io

import pyttsx3

def onStart(name):
    print('starting', name)
def onWord(name, location, length):
    print('word', name, location, length)
def onEnd(name, completed):
    print('finishing', name, completed)

engine = pyttsx3.init()
engine.connect('started-utterance', onStart)
engine.connect('started-word', onWord)
engine.connect('finished-utterance', onEnd)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

Output from newest pyttsx3:

starting None
word None 1 0
finishing None True

Running the same code with the pyttsx3 version from pip results in the following output:

starting None
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 32 3
word None 36 4
word None 41 3
finishing None True

I suspect it has something to do with the recent changes in #75 but unfortunately I was not able to fix it myself.

Edit 1 Checking the documentation of sapi I found that _ISpeechVoiceEvents_StartStream(self, char, length) in sapi5.py should probably be _ISpeechVoiceEvents_Word(self, stream, pos, char, length). However, the callback of the 'started-word' event is still not executed when starting a word but rather at the very end of the sentence.

Edit 2 By digging around in the documentation I learned about the SpeechVoiceSpeakFlags . It seems like the following changes fix the problem, but I'm not sure if this is the proper way to do it. Since I am unsure about the changes and because I have never done a pull request before, I just listed them below.

Speak the given text string asynchronously (SpVoice Flag 1):

def say(self, text):
    self._proxy.setBusy(True)
    self._proxy.notify('started-utterance')
    self._speaking = True
    # self._tts.Speak(fromUtf8(toUtf8(text)))
    self._tts.Speak(fromUtf8(toUtf8(text)), 1)

Purge all pending speak requests prior to this speak call to stop engine (SpVoice Flag 2):

def stop(self):
    if not self._speaking:
        return
    self._proxy.setBusy(True)
    self._stopping = True
    # self._tts.Speak('', 3)
    self._tts.Speak('', 2)

MauroPfister avatar Dec 06 '19 13:12 MauroPfister

I just installed with pip3 and am getting the same error! What version does this work in? How do I get my pip3 to install that version? Help!

wjkoontz avatar Apr 09 '20 00:04 wjkoontz

can u please install 2.71 version and report back here if its working ?

nateshmbhat avatar Apr 09 '20 00:04 nateshmbhat

Tried that but then I get a bunch of these: pi@raspberrypi:~ $ python3 stop.py Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 924 Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 924 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device' ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory ALSA lib conf.c:5036:(snd_config_expand) Args evaluate error: No such file or directory ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM bluealsa ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device' ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory ALSA lib conf.c:5036:(snd_config_expand) Args evaluate error: No such file or directory ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM bluealsa Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 924 Cannot connect to server socket err = No such file or directory Cannot connect to server request channel jack server is not running or cannot be started JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock Expression 'paInvalidSampleRate' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2048 Expression 'PaAlsaStreamComponent_InitialConfigure( &self->playback, outParams, self->primeBuffers, hwParamsPlayback, &realSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2722 Expression 'PaAlsaStream_Configure( stream, inputParameters, outputParameters, sampleRate, framesPerBuffer, &inputLatency, &outputLatency, &hostBufferSizeMode )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2843 wave_open_sound > Pa_OpenStream : err=-9997 (Invalid sample rate) Expression 'paInvalidSampleRate' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2048 Expression 'PaAlsaStreamComponent_InitialConfigure( &self->playback, outParams, self->primeBuffers, hwParamsPlayback, &realSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2722 Expression 'PaAlsaStream_Configure( stream, inputParameters, outputParameters, sampleRate, framesPerBuffer, &inputLatency, &outputLatency, &hostBufferSizeMode )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2843 Expression 'paInvalidSampleRate' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2048 Expression 'PaAlsaStreamComponent_InitialConfigure( &self->playback, outParams, self->primeBuffers, hwParamsPlayback, &realSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2722 Expression 'PaAlsaStream_Configure( stream, inputParameters, outputParameters, sampleRate, framesPerBuffer, &inputLatency, &outputLatency, &hostBufferSizeMode )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2843 wave_open_sound > Pa_OpenStream : err=-9997 (Invalid sample rate) Expression 'paInvalidSampleRate' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2048

wjkoontz avatar Apr 09 '20 00:04 wjkoontz

It looks like something related to ur sound card. I think there was some similar issue raised related to sound card. I don't remember the title of it unfortunately.

nateshmbhat avatar Apr 09 '20 04:04 nateshmbhat

Its a raspberry pi 3 b+, sound card works fine, but all versions of this software older than 2.8 or so give those alsa errors, and the 2.8 or above ones don't respond correctly to started-word, but seem to work fine in all other respects. Unfortunately this renders started-word event unusable with pyttsx3 on raspbian buster unless I want to figure out what versions of what softwares and libraries I have to roll back in order to get older versions of pyttsx3 to work. Perhaps if I run it before running apt-get upgrade for example, not sure, just trying this out but unfortunately can't use started-word right now.

wjkoontz avatar Apr 10 '20 02:04 wjkoontz

Update ALSA errors seem to be related to https://www.raspberrypi.org/forums/viewtopic.php?f=66&t=241814 and trying that fix got it sorta working. I still get a bunch of ALSA errors but not nearly as many and it actually speaks, and actually stops, so yea started-word works in 2.7 if that helps.

wjkoontz avatar Apr 10 '20 03:04 wjkoontz

Spoke too soon on ALSA working but started-word seems to function fine. At this point for me I'm in over my head on this so going back to later version and waiting for fix for started-word.

wjkoontz avatar Apr 10 '20 03:04 wjkoontz

Is this working now in the latest version?

nateshmbhat avatar Apr 10 '20 07:04 nateshmbhat

Sorry If I confused the issues, the started-word event does not work with Python3 and pyttsx3 2.87. The event seems to work with Python2.7 which is using pyttsx3 2.7 however with that combination I get ALSA errors, and inconsistent behavior at best.

wjkoontz avatar Apr 10 '20 14:04 wjkoontz

same issue: python 3.8.3 pyttsx3 2.88 windwos 10

laundmo avatar Jun 19 '20 17:06 laundmo

same problem here, it's not fixed yet. python 3.8.3 pyttsx3 2.88 windwos 10

fghp avatar Jun 25 '20 16:06 fghp

same problem here, it's not fixed yet.

uEternali avatar Aug 22 '20 13:08 uEternali

I am using python 3.8.3 pyttsx3 2.88 windows 10

I was also having issues where my tts would not use the connect "started-word" callback. After edits 1 and 2, that fixed it for me changing the voice flags and sapi.py file were needed.

I can now properly use the connect() function to break mid utterance but struggle to get it to work after 1 break.

I am stuck on restarting the tts properly. If at anypoint I use engine. endloop() or engine.stop() I can't get it to start up again

somewhatrobotic avatar Sep 14 '20 19:09 somewhatrobotic

This is still not fixed in pyttsx3 v2.90 on Windows 11!

Edit: I was able to get it to work by manually pasting the latest version of sapi5.py into my version. Pip installed an out dated version of it for some reason.

DedFishy avatar Aug 02 '22 17:08 DedFishy

There are two problems in code:

  1. spvoice.Speak is blocking. So the program can not entry in startLoop function and PumpWaitingMessages function is never called (Windows function with manage event) Proposal fix> add async flag in Speak sapi5.py:56: self._tts.Speak(fromUtf8(toUtf8(text)), 1) like there is in line.63

  2. It miss the 'Word' event in 'Sink' object Necessary to have progression of speech proposal fix> add this member in class SAPI5DriverEventSink def _ISpeechVoiceEvents_Word(self, streamNum, streamPos, charPos, charLen):

Bonus stage 3) _ISpeechVoiceEvents_StartStream callback uses wrong argument It is not (self, char, length) but (self, streamNumber, streamPosition)

fgras avatar Aug 18 '22 14:08 fgras

This issue still exists in the latest version. I've also tried the aforementioned solutions, but it still does not work.

Windows 10 Python 3.8

EDIT: I fixed the issue by replacing my pyttsx3 external library with a downloaded version from here

Ncohen10 avatar Jun 09 '23 18:06 Ncohen10