soundclient_example.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 """
00004 Simple example showing how to use the SoundClient provided by libsoundplay,
00005 in blocking, non-blocking, and explicit usage.
00006 """
00007 
00008 import rospy
00009 from sound_play.libsoundplay import SoundClient
00010 from sound_play.msg import SoundRequest
00011 
00012 
00013 def play_explicit():
00014     rospy.loginfo('Example: SoundClient play methods can take in an explicit'
00015                   ' blocking parameter')
00016     soundhandle = SoundClient()  # blocking = False by default
00017     rospy.sleep(0.5)  # Ensure publisher connection is successful.
00018 
00019     sound_beep = soundhandle.waveSound("say-beep.wav", volume=0.5)
00020     # Play the same sound twice, once blocking and once not. The first call is
00021     # blocking (explicitly specified).
00022     sound_beep.play(blocking=True)
00023     # This call is not blocking (uses the SoundClient's setting).
00024     sound_beep.play()
00025     rospy.sleep(0.5)  # Let sound complete.
00026 
00027     # Play a blocking sound.
00028     soundhandle.play(SoundRequest.NEEDS_UNPLUGGING, blocking=True)
00029 
00030     # Create a new SoundClient where the default behavior *is* to block.
00031     soundhandle = SoundClient(blocking=True)
00032     soundhandle.say('Say-ing stuff while block-ing')
00033     soundhandle.say('Say-ing stuff without block-ing', blocking=False)
00034     rospy.sleep(1)
00035 
00036 
00037 def play_blocking():
00038     """
00039     Play various sounds, blocking until each is completed before going to the
00040     next.
00041     """
00042     rospy.loginfo('Example: Playing sounds in *blocking* mode.')
00043     soundhandle = SoundClient(blocking=True)
00044 
00045     rospy.loginfo('Playing say-beep at full volume.')
00046     soundhandle.playWave('say-beep.wav')
00047 
00048     rospy.loginfo('Playing say-beep at volume 0.3.')
00049     soundhandle.playWave('say-beep.wav', volume=0.3)
00050 
00051     rospy.loginfo('Playing sound for NEEDS_PLUGGING.')
00052     soundhandle.play(SoundRequest.NEEDS_PLUGGING)
00053 
00054     rospy.loginfo('Speaking some long string.')
00055     soundhandle.say('It was the best of times, it was the worst of times.')
00056 
00057 
00058 def play_nonblocking():
00059     """
00060     Play the same sounds with manual pauses between them.
00061     """
00062     rospy.loginfo('Example: Playing sounds in *non-blocking* mode.')
00063     # NOTE: you must sleep at the beginning to let the SoundClient publisher
00064     # establish a connection to the soundplay_node.
00065     soundhandle = SoundClient(blocking=False)
00066     rospy.sleep(1)
00067 
00068     # In the non-blocking version you need to sleep between calls.
00069     rospy.loginfo('Playing say-beep at full volume.')
00070     soundhandle.playWave('say-beep.wav')
00071     rospy.sleep(1)
00072 
00073     rospy.loginfo('Playing say-beep at volume 0.3.')
00074     soundhandle.playWave('say-beep.wav', volume=0.3)
00075     rospy.sleep(1)
00076 
00077     rospy.loginfo('Playing sound for NEEDS_PLUGGING.')
00078     soundhandle.play(SoundRequest.NEEDS_PLUGGING)
00079     rospy.sleep(1)
00080 
00081     rospy.loginfo('Speaking some long string.')
00082     soundhandle.say('It was the best of times, it was the worst of times.')
00083     # Note we will return before the string has finished playing.
00084 
00085 
00086 if __name__ == '__main__':
00087     rospy.init_node('soundclient_example', anonymous=False)
00088     play_explicit()
00089     play_blocking()
00090     play_nonblocking()
00091     rospy.loginfo('Finished')


sound_play
Author(s): Blaise Gassend
autogenerated on Mon Sep 26 2016 03:31:16