Here is the code that I wrote earlier on Python3.11. `
import pyttsx3
engine = pyttsx3.init('nsss') # object creation
TTS_RATE = 150 # speed of tts
TTS_VOLUME = 1.0 # volume of tts
TTS_GENDER = "Male" # gender of tts (Male or Female)
"""RATE"""
rate = engine.getProperty('rate') # getting details of current speaking rate
engine.setProperty('rate', TTS_RATE) # setting up new voice rate
"""VOLUME"""
volume = engine.getProperty('volume') # getting to know current volume level (min=0 and max=1)
engine.setProperty('volume', TTS_VOLUME) # setting up volume level between 0 and 1
"""VOICES"""
voices = engine.getProperty('voices')
if TTS_GENDER == "Male":
voiceGenders = filter(lambda voices: voices.gender == 'VoiceGenderMale', voices)
elif TTS_GENDER == "Female":
voiceGenders = filter(lambda voices: voices.gender == 'VoiceGenderFemale', voices)
for voice in voiceGenders:
if voice.languages == ['en_US']:
engine.setProperty('voice', voice.id)
def va_speak(phrase):
engine.say(phrase)
engine.runAndWait()
engine.stop()
va_speak("Hello there")
Originally I ran this code on Monterey and it worked perfectly fine. As the update installed, it threw me this error:
File "./voice assistant/tts.py", line 18, in <module>
voices = engine.getProperty('voices')
File "./voice assistant/venv/lib/python3.10/site-packages/pyttsx3/engine.py", line 146, in getProperty
return self.proxy.getProperty(name)
File "./voice assistant/venv/lib/python3.10/site-packages/pyttsx3/driver.py", line 173, in getProperty
return self._driver.getProperty(name)
File "./voice assistant/venv/lib/python3.10/site-packages/pyttsx3/drivers/nsss.py", line 69, in getProperty
return [self._toVoice(NSSpeechSynthesizer.attributesForVoice_(v))
File "./voice assistant/venv/lib/python3.10/site-packages/pyttsx3/drivers/nsss.py", line 69, in <listcomp>
return [self._toVoice(NSSpeechSynthesizer.attributesForVoice_(v))
File "./voice assistant/venv/lib/python3.10/site-packages/pyttsx3/drivers/nsss.py", line 64, in _toVoice
attr['VoiceAge'])
File "./voice assistant/venv/lib/python3.10/site-packages/objc/_convenience_mapping.py", line 18, in __getitem__objectForKey_
return container_unwrap(res, KeyError, key)
File "./voice assistant/venv/lib/python3.10/site-packages/objc/_convenience.py", line 122, in container_unwrap
raise exc_type(*exc_args)
KeyError: 'VoiceAge'
`
The initializing of the pyttsx3's engine goes without any errors, but if I try to get voices, it shows me the same error as described earlier `
import pyttsx3
engine = pyttsx3.init('nsss')
voices = engine.getProperty('voices')
` Already tried reinstalling pyttsx3, launching the code with sudo, which didn't help a lot Changing the argument in init (pyttsx3.init('dummy')), passed through without any errors but there was no sound what so ever.
I think this is due to the OS upgrade to Ventura. I encountered the same issue after upgrading from montery to ventura. While waiting for pyttsx3
to fix the issue, you can update the following line. You can find the file to be updated with the following snippet:
$ python -c 'import inspect; from pyttsx3.drivers.nsss import NSSpeechDriver; print(inspect.getsourcefile(NSSpeechDriver))'
It gaves me something like :
/Users/myuser/virtualenvs/myvenv/lib/python3.10/site-packages/pyttsx3/drivers/nsss.py
Once you have located the file, update it using your preferred code editor to remove attr['VoiceAge']
. Once you have done this, it should work as before.