Go to the documentation of this file.00001
00002
00003 import urllib
00004 import urllib2
00005 import socket
00006 import subprocess
00007 import traceback
00008 import logging
00009
00010
00011
00012 from rospeex_core.validators import accepts
00013 from rospeex_core.validators import check_language
00014 from rospeex_core.exceptions import InvalidRequestException
00015 from rospeex_core.exceptions import InvalidResponseException
00016 from rospeex_core.exceptions import SpeechSynthesisException
00017 from rospeex_core.exceptions import RequestTimeoutException
00018 from rospeex_core.ss.client_base import SpeechSynthesisClient
00019 from rospeex_core.ss.client_nict import SpeechSynthesisClient_NICT
00020
00021
00022 logger = logging.getLogger(__name__)
00023
00024
00025 class SpeechSynthesisClient_Google(SpeechSynthesisClient):
00026 """ SpeechSynthesisCient_Google class """
00027 LANGUAGES = ['ja', 'en', '*']
00028
00029 def __init__(self):
00030 """ init function """
00031 self.URL = 'http://translate.google.com/translate_tts?'
00032
00033 @accepts(message=basestring, language=str, voice_font=str, timeout=int)
00034 def request(self, message, language='ja', voice_font='', timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
00035 """
00036 Send speech synthesis request to server,
00037 and get speech synthesis result.
00038 @param message: message
00039 @type message: str
00040 @param language: speech synthesis language
00041 @type language: str
00042 @param voice_font: taraget voice font
00043 @type voice_font: str
00044 @param timeout: request timeout time (second)
00045 @type timeout: float
00046 @return: voice data (wav format binary)
00047 @rtype: str
00048 @raise SpeechSynthesisException:
00049 """
00050 check_language(language, self.LANGUAGES)
00051
00052 query = {
00053 'q': message.encode('utf-8'),
00054 'tl': language,
00055 }
00056 values = urllib.urlencode(query)
00057 headers = {'User-Agent':'Mozilla/5.0'}
00058
00059 try:
00060 client = SpeechSynthesisClient_NICT()
00061 client.request(message, language, voice_font, 10)
00062 except:
00063 pass
00064
00065 voice = None
00066 try:
00067 request = urllib2.Request(self.URL+values, None, headers)
00068 response = urllib2.urlopen(request, timeout=timeout)
00069 voice = response.read()
00070
00071 except urllib2.URLError as err:
00072 if isinstance(err.reason, socket.timeout):
00073 msg = 'request time out. Exception: %s' % str(err)
00074 raise RequestTimeoutException(msg)
00075 msg = 'request url error. Exception: %s' % str(err)
00076 raise InvalidRequestException(msg)
00077
00078 except urllib2.HTTPError as err:
00079 msg = 'http error. %s Exception:%s' % (err.code, err.msg)
00080 raise InvalidResponseException(msg)
00081
00082 except socket.timeout as err:
00083 msg = 'request time out. Exception: %s' % str(err)
00084 raise RequestTimeoutException(msg)
00085
00086 except:
00087 msg = 'unknown exception. Traceback: %s' % traceback.format_exc()
00088 raise SpeechSynthesisException(msg)
00089
00090 conv_voice = self._convert_mp3_to_wav(voice)
00091 return conv_voice
00092
00093
00094 def _convert_mp3_to_wav(self, data):
00095 """
00096 convert mp3 to waf file
00097 @param data: mp3 binary data
00098 @type data: str
00099 @return: wav binary data
00100 @rtype: str
00101 @raises SpeechSynthesisException:
00102 """
00103
00104 try:
00105 input_file_name = 'ss_tmp.mp3'
00106 output_file_name = 'ss_tmp.wav'
00107
00108 fo = open(input_file_name, 'wb')
00109 fo.write(data)
00110 fo.close()
00111
00112 subprocess.check_output(
00113 ['ffmpeg', '-y', '-i', input_file_name, output_file_name],
00114 stderr=subprocess.STDOUT
00115 )
00116
00117 fi = open(output_file_name, 'rb')
00118 data = fi.read()
00119 fi.close()
00120
00121 except IOError as err:
00122 msg = 'file io error. Exception:%s' % str(err)
00123 raise SpeechSynthesisException(msg)
00124
00125 except OSError as err:
00126 msg = 'ffmpeg is not installed. Exception:%s' % str(err)
00127 raise SpeechSynthesisException(msg)
00128
00129 except subprocess.CalledProcessError as err:
00130 msg = 'ffmpeg return error value. Exception:%s' % str(err)
00131 raise SpeechSynthesisException(msg)
00132
00133 except:
00134 msg = 'unknown exception. Traceback: %s' % traceback.format_exc()
00135 raise SpeechSynthesisException(msg)
00136
00137 return data