Go to the documentation of this file.00001
00002
00003
00004
00005 import rospy
00006 import unittest
00007 import rostest
00008
00009
00010 from rospeex_if import ROSpeexInterface
00011
00012
00013 import os
00014 import sys
00015
00016
00017 class TestRospeexSR(unittest.TestCase):
00018 """ Speech Synthesis Test class """
00019 def setUp(self):
00020 self._message = ''
00021 self._ready = 1
00022 self._interface = None
00023 self._wait_flag = True
00024 self._wait_count = 0
00025
00026 def _sr_response(self, message):
00027 """
00028 speech synthesis response callback
00029
00030 @param message: message class
00031 @type message: str
00032 """
00033 self._message = message
00034 self._ready = 1
00035 rospy.loginfo('Message Received.')
00036 self._wait_count -= 1
00037
00038 def _read_input_data(self, target_file):
00039 """
00040 read input data
00041
00042 @param target_file: target test data
00043 @type target_file: str
00044 """
00045 data = None
00046 with open(target_file, 'rb') as f_log:
00047 data = f_log.read()
00048
00049 if data is None:
00050 rospy.logwarn('[%s] is not found.' % target_file)
00051 return data
00052
00053 def _execute_case(self, voice_data, target_lang, target_engine):
00054 """
00055 execute test case
00056
00057 @param message: voice data (binary wav)
00058 @type message: str
00059 @param target_lang: recognition ranguage
00060 @type target_lang: str
00061 @param target_engine: target speech recognition engine
00062 @type target_engine: str (nict | google | microsoft)
00063 """
00064 self._ready = 0
00065 self._interface.recognize(voice_data, target_lang, target_engine)
00066 self._wait_count += 1
00067 while not self._ready and self._wait_flag:
00068 rospy.sleep(0.1)
00069
00070 def test_run(self):
00071 self._ready = 1
00072 rospy.init_node('test_rospeex_sr')
00073 self._interface = ROSpeexInterface()
00074 self._interface.init(ss=False, sr=True, spi=False)
00075 self._interface.register_sr_response(self._sr_response)
00076 rospy.sleep(0.5)
00077
00078 target_dir = rospy.get_param('~target_dir', '../testdata')
00079 target_engine = rospy.get_param('~target_engine', 'nict')
00080 target_engine_list = list(rospy.get_param('~target_engine_list', ''))
00081 target_language = rospy.get_param('~target_language', 'ja')
00082 target_files = list(rospy.get_param('~target_files', ''))
00083 self._wait_flag = rospy.get_param('~wait_flag', True)
00084
00085 if len(target_engine_list) == 0:
00086 target_engine_list.append(target_engine)
00087
00088 for engine in target_engine_list:
00089 if not rospy.is_shutdown():
00090 for target_file in target_files:
00091 rospy.loginfo(target_file)
00092 filename = os.path.join(target_dir, target_file)
00093 voice_data = self._read_input_data(filename)
00094 if voice_data:
00095 self._execute_case(voice_data, target_language, engine)
00096 else:
00097 rospy.logerror('invalid voice data')
00098
00099 while self._wait_count > 0:
00100 rospy.sleep(0.1)
00101
00102
00103 if __name__ == '__main__':
00104 rostest.rosrun('rospeex_if', 'rospeex_sr_test', TestRospeexSR, sys.argv)