How can I use event 'ALTextToSpeech/Status' to get the value in naoqi?

607 views Asked by At

I want to get the value of "value"? How should I use the event in Python?

    a = self.memoryProxy.subscribeToEvent("ALTextToSpeech/Status", "value",str)
    print "value"
    self.tts.setParameter("pitchShift", 1.1)        
    self.tts.post.say(str) 
1

There are 1 answers

0
LeBorgne On

Depending on your naoqi version, you might use one of the following patterns.

The old way goes like this (see Naoqi doc):

from naoqi import ALProxy, ALBroker, ALModule
import argparse

# Global variable to store the TTSEventWatcher module instance
tts_event_watcher = None
memory = None

class TTSEventWatcher(ALModule):
    """ An ALModule to react to the ALTextToSpeech/Status event """

    def __init__(self, ip_robot, port_robot):
        super(TTSEventWatcher, self).__init__("tts_event_watcher")
        global memory
        memory = ALProxy("ALMemory", ip_robot, port_robot)
        self.tts = ALProxy("ALTextToSpeech", ip_robot, port_robot)
        memory.subscribeToEvent("ALTextToSpeech/Status",
                                "tts_event_watcher",  # module instance
                                "on_tts_status")  # callback name

    def on_tts_status(self, key, value, message):
        """ callback for event ALTextToSpeech/Status """
        print "TTS Status value:", value


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--ip", type=str, default="127.0.0.1",
                        help="Robot IP address. On robot or Local Naoqi: use '127.0.0.1'.")
    parser.add_argument("--port", type=int, default=9559,
                        help="Naoqi port number")
    args = parser.parse_args()

    event_broker = ALBroker("event_broker", "0.0.0.0", 0,
                            args.ip, args.port)
    global tts_event_watcher
    tts_event_watcher = TTSEventWatcher(args.ip, args.port)
    while True:
        raw_input("Watch events be triggered...")
        tts_event_watcher.tts.say("Hello!")

Careful, there are a few hidden tricks in there, that for instance force you to put the event management code in your main file.

In newer versions, you can use this more functional-friendly pattern:

import qi
import argparse

class HumanTrackedEventWatcher(object):
    """ A class to react to the ALTextToSpeech/Status event """

    def __init__(self, app):
        super(HumanTrackedEventWatcher, self).__init__()
        app.start()
        session = app.session
        self.memory = session.service("ALMemory")
        self.tts = session.service("ALTextToSpeech")
        self.subscriber = self.memory.subscriber("ALTextToSpeech/Status")
        self.subscriber.signal.connect(self.on_tts_status)
        # keep this variable in memory, else the callback will be disconnected

    def on_tts_status(self, value):
        """ callback for event ALTextToSpeech/Status """
        print "TTS Status value:", value


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--ip", type=str, default="127.0.0.1",
                        help="Robot IP address. On robot or Local Naoqi: use '127.0.0.1'.")
    parser.add_argument("--port", type=int, default=9559,
                        help="Naoqi port number")
    args = parser.parse_args()

    # Initialize qi framework
    connection_url = "tcp://" + args.ip + ":" + str(args.port)
    app = qi.Application(["HumanTrackedEventWatcher", "--qi-url=" + connection_url])
    tts_event_watcher = HumanTrackedEventWatcher(app)   

    while True:
        raw_input("Watch TTS events be triggered...")
        tts_event_watcher.tts.say("Hello!")