@lyubomyr83, I made this class to make it easier to work with the reference @ccc is pointing to, all could be done thanks to the work of @JonB
import objc_util
import re
class SpeechModule(object):
def __init__(self):
self.__AVSpeechUtterance = objc_util.ObjCClass('AVSpeechUtterance')
self.__AVSpeechSynthesizer = objc_util.ObjCClass('AVSpeechSynthesizer')
self.__AVSpeechSynthesisVoice = objc_util.ObjCClass('AVSpeechSynthesisVoice')
self.__synthesizer = self.__AVSpeechSynthesizer.new()
def get_synthesis_languages(self) -> list:
return self.__AVSpeechSynthesisVoice.speechVoices()
def say(self, msg: str, language: objc_util.ObjCInstance, rate=0.5):
utterance = self.__AVSpeechUtterance.speechUtteranceWithString_(msg)
utterance.rate = rate
utterance.voice = voice
utterance.useCompactVoice = False
self.__synthesizer.speakUtterance_(utterance)
def is_speaking(self) -> bool:
return self.__synthesizer.isSpeaking()
def is_paused(self) -> bool:
return self.__synthesizer.isPaused()
def pause(self) -> bool:
return self.__synthesizer.pauseSpeakingAtBoundary_(True)
def stop(self) -> bool:
return self.__synthesizer.stopSpeakingAtBoundary_(True)
def continue_speaking(self):
self.__synthesizer.continueSpeaking()
Here is an example how to use it
import time
speech_ = SpeechModule()
available_voices = speech_.get_synthesis_languages()
us_english_voices = tuple(filter(lambda voice: "en-US" in str(voice), available_voices))
voice = us_english_voices[0]
speech_.say('Hello friend! how are you?', voice, 0.3)
time.sleep(1)
speech_.stop()
print(speech_.is_speaking())
speech_.say("nevermind", voice)
For some reason I can’t make that pause function to work correctly, anyway, it is an idea for your project and maybe my class could be buggy, so objc gurus invocation (@cvp @mikael @JonB @ccc )