16 """A very simple Action Server that does TTS. 18 It is a combination of a synthesizer and a player. Being an action server, it can be used in two different manners. 20 1. Play and wait for it to finish 21 --------------------------------- 23 A user can choose to be blocked until the audio playing is done. This is especially useful in interactive scenarios. 27 rospy.init_node('tts_action_client') 28 client = actionlib.SimpleActionClient('tts', SpeechAction) 29 client.wait_for_server() 31 goal.text = 'Let me ask you a question, please give me your answer.' 32 client.send_goal(goal) 33 client.wait_for_result() 35 # start listening to a response or waiting for some input to continue the interaction 40 A user can also choose not to wait:: 42 rospy.init_node('tts_action_client') 43 client = actionlib.SimpleActionClient('tts', SpeechAction) 44 client.wait_for_server() 46 goal.text = 'Let me talk, you can to something else in the meanwhile.' 47 client.send_goal(goal) 49 This is useful when the robot wants to do stuff while the audio is being played. For example, a robot may start to 50 read some instructions and immediately get ready for any input. 57 from tts.msg
import SpeechAction, SpeechResult
58 from tts.srv
import Synthesizer
64 """plays the wav or ogg file using sound_play""" 69 """calls synthesizer service to do the job""" 70 rospy.wait_for_service(
'synthesizer')
71 synthesize = rospy.ServiceProxy(
'synthesizer', Synthesizer)
72 return synthesize(goal.text, goal.metadata)
76 """responds the client""" 77 tts_server_result = SpeechResult(s)
78 server.set_succeeded(tts_server_result)
79 rospy.loginfo(tts_server_result)
83 """The action handler. 85 Note that although it responds to client after the audio play is finished, a client can choose 86 not to wait by not calling ``SimpleActionClient.waite_for_result()``. 88 rospy.loginfo(
'speech goal: {}'.format(goal))
91 rospy.loginfo(
'synthesizer returns: {}'.format(res))
94 r = json.loads(res.result)
95 except Exception
as e:
96 s =
'Expecting JSON from synthesizer but got {}'.format(res.result)
97 rospy.logerr(
'{}. Exception: {}'.format(s, e))
103 if 'Audio File' in r:
104 audio_file = r[
'Audio File']
105 rospy.loginfo(
'Will play {}'.format(audio_file))
110 result =
'[ERROR] {}'.format(r)
116 if __name__ ==
'__main__':
117 rospy.init_node(
'tts_node')
def finish_with_result(s)