tts_node.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2018, Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License").
6 # You may not use this file except in compliance with the License.
7 # A copy of the License is located at
8 #
9 # http://aws.amazon.com/apache2.0
10 #
11 # or in the "license" file accompanying this file. This file is distributed
12 # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 # express or implied. See the License for the specific language governing
14 # permissions and limitations under the License.
15 
16 """A very simple Action Server that does TTS.
17 
18 It is a combination of a synthesizer and a player. Being an action server, it can be used in two different manners.
19 
20 1. Play and wait for it to finish
21 ---------------------------------
22 
23 A user can choose to be blocked until the audio playing is done. This is especially useful in interactive scenarios.
24 
25 Example::
26 
27  rospy.init_node('tts_action_client')
28  client = actionlib.SimpleActionClient('tts', SpeechAction)
29  client.wait_for_server()
30  goal = SpeechGoal()
31  goal.text = 'Let me ask you a question, please give me your answer.'
32  client.send_goal(goal)
33  client.wait_for_result()
34 
35  # start listening to a response or waiting for some input to continue the interaction
36 
37 2. Play and forget
38 ------------------
39 
40 A user can also choose not to wait::
41 
42  rospy.init_node('tts_action_client')
43  client = actionlib.SimpleActionClient('tts', SpeechAction)
44  client.wait_for_server()
45  goal = SpeechGoal()
46  goal.text = 'Let me talk, you can to something else in the meanwhile.'
47  client.send_goal(goal)
48 
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.
51 """
52 
53 import json
54 
55 import actionlib
56 import rospy
57 from tts.msg import SpeechAction, SpeechResult
58 from tts.srv import Synthesizer
59 
60 from sound_play.libsoundplay import SoundClient
61 
62 
63 def play(filename):
64  """plays the wav or ogg file using sound_play"""
65  SoundClient(blocking=True).playWave(filename)
66 
67 
68 def do_synthesize(goal):
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)
73 
74 
76  """responds the client"""
77  tts_server_result = SpeechResult(s)
78  server.set_succeeded(tts_server_result)
79  rospy.loginfo(tts_server_result)
80 
81 
82 def do_speak(goal):
83  """The action handler.
84 
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()``.
87  """
88  rospy.loginfo('speech goal: {}'.format(goal))
89 
90  res = do_synthesize(goal)
91  rospy.loginfo('synthesizer returns: {}'.format(res))
92 
93  try:
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))
99  return
100 
101  result = ''
102 
103  if 'Audio File' in r:
104  audio_file = r['Audio File']
105  rospy.loginfo('Will play {}'.format(audio_file))
106  play(audio_file)
107  result = audio_file
108 
109  if 'Exception' in r:
110  result = '[ERROR] {}'.format(r)
111  rospy.logerr(result)
112 
113  finish_with_result(result)
114 
115 
116 if __name__ == '__main__':
117  rospy.init_node('tts_node')
118  server = actionlib.SimpleActionServer('tts', SpeechAction, do_speak, False)
119  server.start()
120  rospy.spin()
def do_synthesize(goal)
Definition: tts_node.py:68
def play(filename)
Definition: tts_node.py:63
def finish_with_result(s)
Definition: tts_node.py:75
def do_speak(goal)
Definition: tts_node.py:82


tts
Author(s): AWS RoboMaker
autogenerated on Fri Mar 5 2021 03:06:38