Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

python - How do I connect a new callback, NSSpeechSynthesizer.willSpeakPhoneme(), to a new started-phoneme event in Pyttsx3?

https://github.com/nateshmbhat/pyttsx3 By looking at the source code for the Pyttsx3 module on Github, I was able to understand how the onWord callback can be triggered every time a word is spoken through TTS.

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}

When a user calls Engine.connect("topic", callback), he or she appends to the _connects array a dictionary of the given topic as the key and a callback function as the value. The topic refers to the "events" that the TTS can process.

engine.py
def _notify(self, topic, **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()

The above function actually does the callback function call, and it gets called by

driver.py
def notify(self, topic, **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)

which gets called by

nsss.py
def speechSynthesizer_willSpeakWord_ofString_(self, tts, rng, text):
        self._proxy.notify('started-word', location=rng.location,
                           length=rng.length)

or in SAPI5 or espeak depending on the system.

Now that I realize that speechSynthesizer_willSpeakWord_ofString_ is what actually gets called every time a word is spoken through TTS, I want to know how I can modify pyttsx3 so that it can also add speechSynthesizer_willSpeakPhoneme as a valid callback to the "started-phoneme" event. Since this method already exists in NSSpeechSynthesizer https://developer.apple.com/documentation/appkit/nsspeechsynthesizerdelegate/1448442-speechsynthesizer?language=objc I believe it's possible to add this function as a callback in Pyttsx3. Does anyone have ideas?

question from:https://stackoverflow.com/questions/65840666/how-do-i-connect-a-new-callback-nsspeechsynthesizer-willspeakphoneme-to-a-ne

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...