Skip to content

Instantly share code, notes, and snippets.

@5st7
Last active December 11, 2017 16:12
Show Gist options
  • Save 5st7/9d606d218c0063d7e62dcd241c804ebe to your computer and use it in GitHub Desktop.
Save 5st7/9d606d218c0063d7e62dcd241c804ebe to your computer and use it in GitHub Desktop.
docomoの音声合成でウェイってする感じのアレです
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import requests
import json
import types
import ffmpy
import struct
import subprocess
def say(text, rate, pitch, volume):
return "<?xml version=\"1.0\" encoding=\"utf-8\" ?><speak version=\"1.1\"><voice name=\"sumire\"><prosody rate=\"%d\" pitch=\"%d\" volume=\"%d\">%s</prosody></voice></speak>" %(rate, pitch,volume, text)
# 出力する言葉を受け取る
say_content = raw_input('>>')
#送信する
KEY = 'API_KEY'
endpoint = 'https://api.apigw.smt.docomo.ne.jp/aiTalk/v1/textToSpeech?APIKEY=REGISTER_KEY'
url = endpoint.replace('REGISTER_KEY', KEY)
headers = {'Content-type': 'application/ssml+xml','Accept': 'audio/L16'}
r = requests.post(url, data=say(say_content,1,1,1), headers=headers)
# 受け取ったPCMファイルを一時的に保存しておく
fp = open('temp.raw', 'wb')
fp.write(r.content)
fp.close()
# soxを使ってPCMファイルをWAVに変換する
cmd = "sox -t raw -r 16k -e signed -b 16 -B -c 1 temp.raw output.wav"
subprocess.check_output(cmd, shell=True)
# temp.rawを削除する
os.remove('temp.raw')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment