前回HOYA版を手抜きの英文で音声合成APIを試したけど、NTTアイティ版だといろいろエラーが出るというコメントを頂いたので、、NTTアイティ版を使ってみた。また、今回は日本語(漢字文字列)を使って音声合成してみた。
漢字コード体系をutf-8に整えるのに、ShiftJIS->unicode->utf-8とまどろっこしいことをしてますが、ファイルのエンコーディングが最初からutf-8ならこんなことしなくてもいいかもしれません。自分のPCの環境(Editorとか)ではutf-8が正しく扱えているか自信がないので、Windowsでは安定動作するShiftJISでコーディングしてます。linux環境なら標準の文字コード体系がutf-8(unicode?)なので、utf-8のエンコードですんなりいけるかも。
#!/usr/bin/python # coding: Shift-JIS # このPythonソースコードの文字体系はShift-JISであると宣言 import urllib2 import urllib DOCOMO_API_URL = "https://api.apigw.smt.docomo.ne.jp/crayon/v1/textToSpeech" API_KEY="XXXXXXXXXXX" # set your API KEY # Shift-JISで表現された文字列(str型)をPythonの #(マルチバイト文字用の型である)unicode型に変換し、 # Pythonのunicode型からutf-8で表現されたstr型に変換 msg = "今日は晴れです" msg_utf8 = unicode(msg,encoding='shift-jis').encode('utf-8') headers ={ 'Content-Type' : 'application/x-www-form-urlencoded', } data = { "Command":"AP_Synth", "TextData":msg_utf8 } values = urllib.urlencode(data) url=DOCOMO_API_URL+"?APIKEY="+API_KEY req = urllib2.Request(url, values ,headers ) res = urllib2.urlopen(req) header = res.info() data = res.read() code = res.getcode() print "speech synth" print "message:" + msg print "response code[%s]" % code if code == 200: f=open('speech.wav','w') f.write(data) f.close() print "out file:[speech.wav]" else: print "Error!! response is not 200"
DoCoMo Developper Support / 音声合成
https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_name=text_to_speech&p_name=api_reference
【Powered by NTTアイティ】音声合成API 仕様書 v1.0.0 (公開日 : 2016/2/15)
https://devsite-pro.s3.amazonaws.com/contents_file/Text_to_Speech_API_spec_latest_crayon.pdf