问题描述
https://github.com/nateshmbhat/pyttsx3 通过查看 Github 上 Pyttsx3 模块的源代码,我能够理解每次通过 TTS 说出一个词时如何触发 onWord 回调。
engine.py
def connect(self,topic,cb):
"""
Registers a callback for an event topic. Valid topics and their
associated values:
started-utterance: name=<str>
started-word: name=<str>,location=<int>,length=<int>
finished-utterance: name=<str>,completed=<bool>
error: name=<str>,exception=<exception>
@param topic: Event topic name
@type topic: str
@param cb: Callback function
@type cb: callable
@return: Token to use to unregister
@rtype: dict
"""
arr = self._connects.setdefault(topic,[])
arr.append(cb)
return {'topic': topic,'cb': cb}
当用户调用 Engine.connect("topic",callback) 时,他或她将给定主题的字典作为键和回调函数作为值附加到 _connects 数组。主题指的是 TTS 可以处理的“事件”。
engine.py
def _notify(self,**kwargs):
"""
Invokes callbacks for an event topic.
@param topic: String event name
@type topic: str
@param kwargs: Values associated with the event
@type kwargs: dict
"""
for cb in self._connects.get(topic,[]):
try:
cb(**kwargs)
except Exception:
if self._debug:
traceback.print_exc()
driver.py
def notify(self,**kwargs):
'''
Sends a notification to the engine from the driver.
@param topic: Notification topic
@type topic: str
@param kwargs: Arbitrary keyword arguments
@type kwargs: dict
'''
kwargs['name'] = self._name
self._engine._notify(topic,**kwargs)
被调用
nsss.py
def speechSynthesizer_willSpeakWord_ofString_(self,tts,rng,text):
self._proxy.notify('started-word',location=rng.location,length=rng.length)
或在 SAPI5 或 espeak 取决于系统。
现在我意识到每次通过 TTS 说出一个词时都会调用 SpeechSynthesizer_willSpeakWord_ofString_,我想知道如何修改 pyttsx3 以便它还可以添加 SpeechSynthesizer_willSpeakphoneme 作为对“started-phoneme”的有效回调事件。由于此方法已存在于 NsspeechSynthesizer https://developer.apple.com/documentation/appkit/nsspeechsynthesizerdelegate/1448442-speechsynthesizer?language=objc 中,我相信可以将此函数添加为 Pyttsx3 中的回调。有人有想法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)