def sound_play_client(volume=1.0): client = actionlib.SimpleActionClient('sound_play', SoundRequestAction) client.wait_for_server() print "Need Unplugging" goal = SoundRequestGoal() goal.sound_request.sound = SoundRequest.NEEDS_UNPLUGGING goal.sound_request.command = SoundRequest.PLAY_ONCE goal.sound_request.volume = volume client.send_goal(goal) client.wait_for_result() print client.get_result() print "End Need Unplugging" print print "Need Plugging" goal = SoundRequestGoal() goal.sound_request.sound = SoundRequest.NEEDS_PLUGGING goal.sound_request.command = SoundRequest.PLAY_ONCE goal.sound_request.volume = volume client.send_goal(goal) client.wait_for_result() print client.get_result() print "End Need Plugging" print print "Say" goal = SoundRequestGoal() goal.sound_request.sound = SoundRequest.SAY goal.sound_request.command = SoundRequest.PLAY_ONCE goal.sound_request.arg = "Testing the actionlib interface A P I" goal.sound_request.volume = volume client.send_goal(goal) client.wait_for_result() print client.get_result() print "End Say" print print "Wav" goal = SoundRequestGoal() goal.sound_request.sound = SoundRequest.PLAY_FILE goal.sound_request.command = SoundRequest.PLAY_ONCE goal.sound_request.arg = os.path.join( roslib.packages.get_pkg_dir('sound_play'), 'sounds') + "/say-beep.wav" goal.sound_request.volume = volume client.send_goal(goal) client.wait_for_result() print client.get_result() print "End wav" print
def sendMsg(self, snd, cmd, s, arg2="", vol=1.0, prior = 1, **kwargs): """ Internal method that publishes the sound request, either directly as a SoundRequest to the soundplay_node or through the actionlib interface (which blocks until the sound has finished playing). The blocking behavior is nominally the class-wide setting unless it has been explicitly specified in the play call. """ # Use the passed-in argument if it exists, otherwise fall back to the # class-wide setting. blocking = kwargs.get('blocking', self._blocking) msg = SoundRequest() msg.sound = snd # Threshold volume between 0 and 1. msg.volume = max(0, min(1, vol)) msg.command = cmd msg.arg = s msg.arg2 = arg2 msg.priority = prior rospy.logdebug('Sending sound request with volume = {}' ' and blocking = {}'.format(msg.volume, blocking)) # Defensive check for the existence of the correct communicator. if not blocking and not self.pub: rospy.logerr('Publisher for SoundRequest must exist') return if blocking and not self.actionclient: rospy.logerr('Action client for SoundRequest does not exist.') return if not blocking: # Publish message directly and return immediately self.pub.publish(msg) if self.pub.get_num_connections() < 1: rospy.logwarn("Sound command issued, but no node is subscribed" " to the topic. Perhaps you forgot to run" " soundplay_node.py?") else: # Block until result comes back. assert self.actionclient, 'Actionclient must exist' rospy.logdebug('Sending action client sound request [blocking]') self.actionclient.wait_for_server() goal = SoundRequestGoal() goal.sound_request = msg self.actionclient.send_goal(goal) self.actionclient.wait_for_result() rospy.logdebug('sound request response received') return
def sendMsg(self, snd, cmd, s, arg2="", vol=1.0, **kwargs): """ Internal method that publishes the sound request, either directly as a SoundRequest to the soundplay_node or through the actionlib interface (which blocks until the sound has finished playing). The blocking behavior is nominally the class-wide setting unless it has been explicitly specified in the play call. """ # Use the passed-in argument if it exists, otherwise fall back to the # class-wide setting. blocking = kwargs.get('blocking', self._blocking) msg = SoundRequest() msg.sound = snd # Threshold volume between 0 and 1. msg.volume = max(0, min(1, vol)) msg.command = cmd msg.arg = s msg.arg2 = arg2 rospy.logdebug('Sending sound request with volume = {}' ' and blocking = {}'.format(msg.volume, blocking)) # Defensive check for the existence of the correct communicator. if not blocking and not self.pub: rospy.logerr('Publisher for SoundRequest must exist') return if blocking and not self.actionclient: rospy.logerr('Action client for SoundRequest does not exist.') return if not blocking: # Publish message directly and return immediately self.pub.publish(msg) if self.pub.get_num_connections() < 1: rospy.logwarn("Sound command issued, but no node is subscribed" " to the topic. Perhaps you forgot to run" " soundplay_node.py?") else: # Block until result comes back. assert self.actionclient, 'Actionclient must exist' rospy.logdebug('Sending action client sound request [blocking]') self.actionclient.wait_for_server() goal = SoundRequestGoal() goal.sound_request = msg self.actionclient.send_goal(goal) self.actionclient.wait_for_result() rospy.logdebug('sound request response received') return
def play_sound(sound, lang='', topic_name='robotsound', volume=1.0, wait=False): """ Plays sound using sound_play server Args: sound: if sound is pathname, plays sound file located at given path if it is number, server plays builtin sound otherwise server plays sound as speech sentence topic-name: namespace of sound_play server wait: wait until sound is played """ msg = SoundRequest(command=SoundRequest.PLAY_ONCE) if isinstance(sound, int): msg.sound = sound elif isinstance(sound, str) and Path(sound).exists(): msg.sound = SoundRequest.PLAY_FILE msg.arg = sound elif isinstance(sound, str): msg.sound = SoundRequest.SAY msg.arg = sound else: raise ValueError if hasattr(msg, 'volume'): msg.volume = volume if topic_name in _sound_play_clients: client = _sound_play_clients[topic_name] else: client = actionlib.SimpleActionClient(topic_name, SoundRequestAction) client.wait_for_server() goal = SoundRequestGoal() if client.get_state() == actionlib_msgs.msg.GoalStatus.ACTIVE: client.cancel_goal() client.wait_for_result(timeout=rospy.Duration(10)) goal.sound_request = msg _sound_play_clients[topic_name] = client client.send_goal(goal) if wait is True: client.wait_for_result(timeout=rospy.Duration(10)) return client
def say(text, wait=True): msg = SoundRequestGoal() msg.sound_request.sound = -3 msg.sound_request.command = 1 msg.sound_request.arg = text sound_client.send_goal(msg) if wait: sound_client.wait_for_result()
def play(path, wait=True): msg = SoundRequestGoal() msg.sound_request.sound = -2 msg.sound_request.command = 1 msg.sound_request.arg = path sound_client.send_goal(msg) if wait: sound_client.wait_for_result()
def play_sound(self, key, timeout=5.0): if self.act_sound is None: return req = SoundRequest() req.sound = SoundRequest.PLAY_FILE req.command = SoundRequest.PLAY_ONCE req.arg = self.signals[key] goal = SoundRequestGoal(sound_request=req) self.act_sound.send_goal_and_wait(goal, rospy.Duration(timeout))
def _speak(self, sentence): req = SoundRequest() req.command = SoundRequest.PLAY_ONCE req.sound = SoundRequest.SAY req.arg = sentence req.arg2 = 'ja' req.volume = 0.8 self.speak_client.send_goal(SoundRequestGoal(sound_request=req)) self.speak_client.wait_for_result(timeout=rospy.Duration(10))
def loopOnce(self, event): speak_msg = SoundRequestGoal() speak_msg.sound_request.volume = self.volume speak_msg.sound_request.command = self.command speak_msg.sound_request.sound = self.sound speak_msg.sound_request.arg = "さっき、しゃべったのは、" + self.recognized_str + "、でしたね" speak_msg.sound_request.arg2 = self.arg2 print("Fetch says {}".format(speak_msg.sound_request.arg)) rospy.loginfo(speak_msg) self.actionlib_part.send_goal(speak_msg)
def speak_result(self, result): if self.sound_action is None: return msg = SoundRequest(command=SoundRequest.PLAY_ONCE, sound=SoundRequest.SAY, volume=1.0, arg=result.fulfillment_text.encode('utf-8'), arg2=self.language) self.sound_action.send_goal_and_wait( SoundRequestGoal(sound_request=msg), rospy.Duration(10.0))
def do_x(self, text): client = actionlib.SimpleActionClient('sound_play', SoundRequestAction) client.wait_for_server() goal = SoundRequestGoal() goal.sound_request.sound = SoundRequest.SAY goal.sound_request.command = SoundRequest.PLAY_ONCE goal.sound_request.arg = "Blah blah I am saying something" client.send_goal(goal) print "Goal sent" client.wait_for_result() print client.get_result()
def speak(self, sentence): if self.speech_history[sentence] + rospy.Duration(self.warn_repeat_rate) > rospy.Time.now(): return self.speech_history[sentence] = rospy.Time.now() req = SoundRequest() req.command = SoundRequest.PLAY_ONCE req.sound = SoundRequest.SAY req.arg = sentence req.arg2 = "ja" self.speak_client.send_goal(SoundRequestGoal(sound_request=req)) self.speak_client.wait_for_result(timeout=rospy.Duration(10))
def speak(self, client, speech_text, lang=None): client.wait_for_server(timeout=rospy.Duration(1.0)) sound_goal = SoundRequestGoal() sound_goal.sound_request.sound = -3 sound_goal.sound_request.command = 1 sound_goal.sound_request.volume = 1.0 if lang is not None: sound_goal.sound_request.arg2 = lang sound_goal.sound_request.arg = speech_text client.send_goal(sound_goal) client.wait_for_result() return client.get_result()
def _play(self, sound, blocking, **kwargs): # Send the command rospy.logdebug( "Sending sound action with (sound, command, arg): {}, {}, {}". format(sound.sound, sound.command, sound.arg)) goal = SoundRequestGoal(sound_request=sound) self.sound_client.send_goal(goal) # If blocking, wait until the sound is done if blocking: self.sound_client.wait_for_result() rospy.logdebug('Response to sound action received')
def speak(self, audioPath): self.client.wait_for_server() try: goal = SoundRequestGoal() goal.sound_request.sound = SoundRequest.PLAY_FILE goal.sound_request.command = SoundRequest.PLAY_ONCE goal.sound_request.arg = self.soundPath + audioPath + ".wav" goal.sound_request.volume = 1.0 self.client.send_goal(goal) self.client.wait_for_result() except: print "print " + audioPath + "fails"
def sigint_handler(signum, frame): import actionlib from sound_play.msg import SoundRequest, SoundRequestAction, SoundRequestGoal client = actionlib.SimpleActionClient('sound_play', SoundRequestAction) client.wait_for_server() goal = SoundRequestGoal() goal.sound_request.sound = SoundRequest.PLAY_FILE goal.sound_request.command = SoundRequest.PLAY_ONCE goal.sound_request.arg = "" goal.sound_request.volume = 1.0 self.client.send_goal(goal) exit(0)
def play_sound(self, path, timeout=5.0): if self.act_sound is None: return req = SoundRequest() req.sound = SoundRequest.PLAY_FILE req.command = SoundRequest.PLAY_ONCE if hasattr( SoundRequest, 'volume' ): # volume is added from 0.3.0 https://github.com/ros-drivers/audio_common/commit/da9623414f381642e52f59701c09928c72a54be7#diff-fe2d85580f1ccfed4e23a608df44a7f7 sound.volume = 1.0 req.arg = path goal = SoundRequestGoal(sound_request=req) self.act_sound.send_goal_and_wait(goal, rospy.Duration(timeout))
def callbackfunc(self, msg): if msg.transcript: recognized_str = msg.transcript[0] print("coded_str={}".format(recognized_str)) speak_msg = SoundRequestGoal() speak_msg.sound_request.volume = self.volume speak_msg.sound_request.command = self.command speak_msg.sound_request.sound = self.sound speak_msg.sound_request.arg = "いま、しゃべったのは、" + recognized_str + "、ですね" speak_msg.sound_request.arg2 = self.arg2 print("Fetch says {}".format(speak_msg.sound_request.arg)) rospy.loginfo(speak_msg) self.actionlib_part.send_goal(speak_msg)
def callbackfunc(msg): if msg.transcript: recognized_str = msg.transcript[0] print("coded_str={}".format(recognized_str)) speak_msg = SoundRequestGoal() speak_msg.sound_request.volume = 1.0 speak_msg.sound_request.command = 1 speak_msg.sound_request.sound = -3 speak_msg.sound_request.arg = "いま、しゃべったのは、" + recognized_str + "、ですね" speak_msg.sound_request.arg2 = 'ja' print("Fetch says {}".format(speak_msg.sound_request.arg)) rospy.loginfo(speak_msg) client.send_goal(speak_msg)
def loopOnce(self, event): speak_msg = SoundRequestGoal() speak_msg.sound_request.volume = self.volume speak_msg.sound_request.command = self.command speak_msg.sound_request.sound = self.sound speak_msg.sound_request.arg = "さっき、しゃべったのは、" + self.recognized_str + "、でしたね" speak_msg.sound_request.arg2 = self.arg2 print("Fetch says {}".format(speak_msg.sound_request.arg)) rospy.loginfo(speak_msg) #self.actionlib_part.send_goal(speak_msg) atohayoroshiku_msg = Int16() if self.flag_of_decision == False: if self.recognized_str in str_list[0]: self.flag_of_decision = True self.decided_str = self.recognized_str print("list of red : sweet") atohayoroshiku_msg.data = 1 rospy.loginfo(atohayoroshiku_msg) self.pub.publish(atohayoroshiku_msg) elif self.recognized_str in str_list[1]: self.flag_of_decision = True self.decided_str = self.recognized_str print("list of blue : gpu") atohayoroshiku_msg.data = 2 rospy.loginfo(atohayoroshiku_msg) self.pub.publish(atohayoroshiku_msg) elif self.recognized_str in str_list[2]: self.flag_of_decision = True self.decided_str = self.recognized_str print("list of green : nuigurumi") atohayoroshiku_msg.data = 3 rospy.loginfo(atohayoroshiku_msg) self.pub.publish(atohayoroshiku_msg) else: if self.decided_str in str_list[0]: print("list of red : sweet") atohayoroshiku_msg.data = 1 rospy.loginfo(atohayoroshiku_msg) self.pub.publish(atohayoroshiku_msg) elif self.decided_str in str_list[1]: print("list of blue : gpu") atohayoroshiku_msg.data = 2 rospy.loginfo(atohayoroshiku_msg) self.pub.publish(atohayoroshiku_msg) elif self.decided_str in str_list[2]: print("list of green : nuigurumi") atohayoroshiku_msg.data = 3 rospy.loginfo(atohayoroshiku_msg) self.pub.publish(atohayoroshiku_msg)
def speak(self, sentence): # Pick first 4 characters as a keyword instead of using whole sentence # because sentence can have variables like 100%, 90%, etc. key = sentence[:4] if self.speech_history[key] + rospy.Duration(self.warn_repeat_rate) > rospy.Time.now(): return self.speech_history[key] = rospy.Time.now() req = SoundRequest() req.command = SoundRequest.PLAY_ONCE req.sound = SoundRequest.SAY req.arg = sentence req.arg2 = "ja" req.volume = 1.0 self.speak_client.send_goal(SoundRequestGoal(sound_request=req)) self.speak_client.wait_for_result(timeout=rospy.Duration(10))
def on_enter(self, userdata): Logger.loginfo('Speech output, talking') goal = SoundRequestGoal() goal.sound_request.arg = self._text_to_speak goal.sound_request.command = SoundRequest.PLAY_ONCE goal.sound_request.sound = SoundRequest.SAY goal.sound_request.volume = 1.0 self._error = False try: self._client.send_goal(self._topic, goal) # Logger.logwarn('V try') except Exception as e: Logger.logwarn('Failed to send the Speech command:\n%s' % str(e)) self._error = True
def move_base_result_callback(self, msg): # Wait if other node is speaking while self.is_speaking is True: time.sleep(1) text = "{}: {}".format(goal_status(msg.status.status), msg.status.text) rospy.loginfo(text) if self.lang == "japanese": # speak japanese sound_goal = SoundRequestGoal() sound_goal.sound_request.sound = -3 sound_goal.sound_request.command = 1 sound_goal.sound_request.volume = self.volume sound_goal.sound_request.arg2 = "jp" if msg.status.status == GoalStatus.SUCCEEDED: self.sound.play(1, volume=self.volume) time.sleep(1) if self.lang == "japanese": sound_goal.sound_request.arg = "到着しました" self.client.send_goal(sound_goal) else: self.sound.say(text, volume=self.volume) elif msg.status.status == GoalStatus.PREEMPTED: self.sound.play(2, volume=self.volume) time.sleep(1) if self.lang == "japanese": sound_goal.sound_request.arg = "別のゴールがセットされました" self.client.send_goal(sound_goal) else: self.sound.say(text, volume=self.volume) elif msg.status.status == GoalStatus.ABORTED: self.sound.play(3, volume=self.volume) time.sleep(1) if self.lang == "japanese": sound_goal.sound_request.arg = "中断しました" self.client.send_goal(sound_goal) else: self.sound.say(text, volume=self.volume) else: self.sound.play(4, volume=self.volume) time.sleep(1) self.sound.say(text, volume=self.volume)
def reply_result(self, result, conversation_type, conversation_id): if conversation_type == 'speech': if self.sound_action is None: return msg = SoundRequest( command=SoundRequest.PLAY_ONCE, sound=SoundRequest.SAY, volume=1.0, arg=result.fulfillment_text.encode('utf-8'), arg2=self.language) self.sound_action.send_goal_and_wait( SoundRequestGoal(sound_request=msg), rospy.Duration(10.0)) elif conversation_type == 'text': msg = ConversationText( conversation_type=conversation_type, conversation_id=conversation_id, text=result.fulfillment_text.encode('utf-8')) self.pub_text.publish(msg) else: raise RuntimeError('Unknown conversation type :{}'.format(speech))
def sound_play_client(volume=1.0): global client client = actionlib.SimpleActionClient('sound_play', SoundRequestAction) client.wait_for_server() sub = rospy.Subscriber('/status', Asv_state, state_cb) goal = SoundRequestGoal() goal.sound_request.sound = SoundRequest.PLAY_FILE goal.sound_request.command = SoundRequest.PLAY_ONCE goal.sound_request.arg = os.path.join( roslib.packages.get_pkg_dir('sound_play'), 'sounds') + "/classic.wav" goal.sound_request.volume = volume client.send_goal(goal, feedback_cb=feedback_cb) result = SoundRequestResult() while 1: client.wait_for_result() result = client.get_result() if result.playing == False: client.send_goal(goal, feedback_cb=feedback_cb)
def goal_CONTROLLER_SPEAKER_callback(userdata, default_goal): #print "in robot speaker" goal = SoundRequestGoal() soundPath = os.path.dirname( os.path.dirname(os.path.abspath(__file__))) + "/storyAudio/" if len(userdata.CONTROLLER_SPEAKER_input) == 0: #print "userdata len = 0" volume = 0.0 soundPath = "" else: soundPath = soundPath + userdata.CONTROLLER_SPEAKER_input + ".wav" volume = 1.0 ''' with open(soundPath, 'wb') as audio_file: audio_file.write(text_to_speech.synthesize( speech, 'audio/wav', 'en-US_MichaelVoice').content) ''' print soundPath goal.sound_request.arg = soundPath goal.sound_request.sound = SoundRequest.PLAY_FILE goal.sound_request.command = SoundRequest.PLAY_ONCE goal.sound_request.volume = volume return goal
# -*- coding: utf-8 -*- import actionlib from collections import defaultdict import os.path as osp import math import rospy import pandas as pd from sound_play.msg import SoundRequestAction from sound_play.msg import SoundRequest, SoundRequestGoal from power_msgs.msg import BatteryState from diagnostic_msgs.msg import DiagnosticArray rospy.init_node("battery_warning") speak_client = actionlib.SimpleActionClient("/robotsound_jp", SoundRequestAction) waitEnough = speak_client.wait_for_server(rospy.Duration(10)) req = SoundRequest() req.command = SoundRequest.PLAY_ONCE req.sound = SoundRequest.SAY req.arg = "おーい。きたがわ" req.arg2 = "ja" req.volume = 1.0 speak_client.send_goal(SoundRequestGoal(sound_request=req)) speak_client.wait_for_result(timeout=rospy.Duration(10))
def __init__(self, matter): self.client = actionlib.SimpleActionClient('sound_play', SoundRequestAction) self.client.wait_for_server() self.goal = SoundRequestGoal()
def do_speak(self, goal): """The action handler. Note that although it responds to client after the audio play is finished, a client can choose not to wait by not calling ``SimpleActionClient.waite_for_result()``. """ self.goal_text = strip_tags(goal.text) self.state_pub.publish(state=TTSState.SYNTHESIZING, text=self.goal_text) res = do_synthesize(goal) rospy.loginfo('synthesizer returns: %s', res) try: res = json.loads(res.result) except ValueError as err: syn = 'Expecting JSON from synthesizer but got {}'.format( res.result) rospy.logerr('%s. Exception: %s', syn, err) self.state_pub.publish(state=TTSState.ERROR, text=syn) self.finish_with_result(syn) return if 'Audio File' in res: audio_file = res['Audio File'] rospy.loginfo('Will play %s', audio_file) mut_d = mutagen.File(audio_file) self.length = mut_d.info.length self.utterance_pub.publish(self.goal_text) msg = SoundRequest() # self.sendMsg(SoundRequest.PLAY_FILE, SoundRequest.PLAY_ONCE, sound, # vol=volume, **kwargs) msg.sound = SoundRequest.PLAY_FILE msg.volume = rospy.get_param("/flo_hum_vol") msg.command = SoundRequest.PLAY_ONCE msg.arg = audio_file msg.arg2 = "" goal = SoundRequestGoal() goal.sound_request = msg self.done = False self.sound_client.send_goal(goal, active_cb=self.sound_received, feedback_cb=self.sound_fb, done_cb=self.sound_done) # self.result = audio_file t_rate = rospy.Rate(10) success = True while not self.done: if self.server.is_preempt_requested(): self.sound_client.cancel_goal() self.server.set_preempted() success = False break t_rate.sleep() self.state_pub.publish(state=TTSState.WAITING) if success: self.finish_with_result('completed sound play in') else: rospy.logerr('No audio file in synthesize voice result') self.state_pub.publish(state=TTSState.ERROR, text='no audio file') self.finish_with_result('no audio file')