tts_server.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import roslib
00004 roslib.load_manifest('tts_server')
00005 import rospy
00006 from tts_server.srv import *
00007 import glob
00008 import os
00009 import os.path as pt
00010 
00011 class TTSServer:
00012     def __init__(self):
00013         self.avs = rospy.Service('available_voices', AvailableVoices, self.available_voices_cb)
00014         self.ss = rospy.Service('say', Say, self.say_cb)
00015         self.rcommander_home = pt.join(os.getenv("HOME"), '.rcommander')
00016         if not pt.exists(self.rcommander_home):
00017             os.mkdir(self.rcommander_home)
00018 
00019     def available_voices_cb(self, req):
00020         voice_list = []
00021         voices = glob.glob('/usr/share/festival/voices/us/*')
00022         voices += glob.glob('/usr/share/festival/voices/english/*')
00023         for vname in voices:
00024             voice_list.append(pt.split(vname)[1])
00025         return AvailableVoicesResponse(voice_list)
00026 
00027     def say_cb(self, req):
00028         txt_file_name = pt.join(self.rcommander_home, 'tts_server_temp.txt')
00029         wav_file_name = pt.join(self.rcommander_home, 'tts_server_temp.wav')
00030         try:
00031             txt_file = open(txt_file_name, 'w')
00032             txt_file.write(req.text)
00033             txt_file.flush()
00034         finally:
00035             txt_file.close()
00036 
00037         os.system("text2wave -eval '(%s)' %s -o %s" % ('voice_' + req.voice, txt_file_name, wav_file_name))
00038         os.system("aplay %s" % (wav_file_name))
00039         return SayResponse()
00040 
00041 if __name__ == '__main__':
00042     rospy.init_node('tts_server')
00043     server = TTSServer()
00044     rospy.loginfo('Text-To-Speech Server Up!')
00045     rospy.spin()
00046 
00047 
00048 


tts_server
Author(s): haidai
autogenerated on Thu Dec 12 2013 12:09:34