talkback.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python
  2. """
  3. talkback.py - Version 1.1 2013-12-20
  4. Use the sound_play client to say back what is heard by the pocketsphinx recognizer.
  5. Created for the Pi Robot Project: http://www.pirobot.org
  6. Copyright (c) 2012 Patrick Goebel. All rights reserved.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.5
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details at:
  15. http://www.gnu.org/licenses/gpl.htmlPoint
  16. """
  17. import rospy
  18. from std_msgs.msg import String
  19. from sound_play.libsoundplay import SoundClient
  20. import sys
  21. class TalkBack:
  22. def __init__(self, script_path):
  23. rospy.init_node('talkback')
  24. rospy.on_shutdown(self.cleanup)
  25. # Set the default TTS voice to use
  26. self.voice = rospy.get_param("~voice", "voice_don_diphone")
  27. # Set the wave file path if used
  28. self.wavepath = rospy.get_param("~wavepath", script_path + "/../sounds")
  29. # Create the sound client object
  30. self.soundhandle = SoundClient()
  31. # Wait a moment to let the client connect to the
  32. # sound_play server
  33. rospy.sleep(1)
  34. # Make sure any lingering sound_play processes are stopped.
  35. self.soundhandle.stopAll()
  36. # Announce that we are ready for input
  37. self.soundhandle.playWave(self.wavepath + "/R2D2a.wav")
  38. rospy.sleep(1)
  39. self.soundhandle.say("Ready", self.voice)
  40. rospy.loginfo("Say one of the navigation commands...")
  41. # Subscribe to the recognizer output and set the callback function
  42. rospy.Subscriber('/recognizer/output', String, self.talkback)
  43. def talkback(self, msg):
  44. # Print the recognized words on the screen
  45. rospy.loginfo(msg.data)
  46. # Speak the recognized words in the selected voice
  47. self.soundhandle.say(msg.data, self.voice)
  48. # Uncomment to play one of the built-in sounds
  49. #rospy.sleep(2)
  50. #self.soundhandle.play(5)
  51. # Uncomment to play a wave file
  52. #rospy.sleep(2)
  53. #self.soundhandle.playWave(self.wavepath + "/R2D2a.wav")
  54. def cleanup(self):
  55. self.soundhandle.stopAll()
  56. rospy.loginfo("Shutting down talkback node...")
  57. if __name__=="__main__":
  58. try:
  59. TalkBack(sys.path[0])
  60. rospy.spin()
  61. except rospy.ROSInterruptException:
  62. rospy.loginfo("Talkback node terminated.")