7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PythonでAzure Text to Speechで合成音声の作成

Last updated at Posted at 2019-04-10

なに?

以下の環境下でPython(Jupyter)でAzure Text to Speechを使って合成音声の音声ファイルを作るコードです。
Gistとかにでもあげればよいものですが誰かの役に立つかな?と自分のメモ含めQiitaにあげておきます。

  • Jupyter notebook
  • Python3
  • Azure Text to Speechの東日本リージョン

Azureの契約や設定方法は検索したらすぐに出てきます!リージョンの指定を 東日本 にしておくと下記コードがそのまま動きます

さっそくコード

text = '音声ファイルにしたい日本語文章を入れてください'
subscription_key = 'xxxxxxxxxx' # APIキーをいれてください

import requests
import xml.etree.ElementTree as ElementTree

fetch_token_url = 'https://japaneast.api.cognitive.microsoft.com/sts/v1.0/issuetoken'
headers = {
    'Ocp-Apim-Subscription-Key': subscription_key
}
response = requests.post(fetch_token_url, headers=headers)
access_token = str(response.text)
print(access_token)

constructed_url = 'https://japaneast.tts.speech.microsoft.com/cognitiveservices/v1'

headers = {
    'Authorization': 'Bearer ' + access_token,
    'Content-Type': 'application/ssml+xml',
    'X-Microsoft-OutputFormat': 'audio-16khz-128kbitrate-mono-mp3',
}

xml_body = ElementTree.Element('speak', version='1.0')
xml_body.set('{http://www.w3.org/XML/1998/namespace}lang', 'ja-JP')
voice = ElementTree.SubElement(xml_body, 'voice')
voice.set('{http://www.w3.org/XML/1998/namespace}lang', 'ja-JP')
voice.set('name', 'Microsoft Server Speech Text to Speech Voice (ja-JP, Ayumi, Apollo)')
prosody = ElementTree.SubElement(voice, 'prosody')
prosody.set('pitch','medium') # high
prosody.set('rate','medium') # fast
prosody.text = text
body = ElementTree.tostring(xml_body)

response = requests.post(constructed_url, headers=headers, data=body)
if response.status_code == 200:
    with open('sample.mp3', 'wb') as audio:
        audio.write(response.content)
        print("\nStatus code: " + str(response.status_code) + "\nYour TTS is ready for playback.\n")
else:
    print("\nStatus code: " + str(response.status_code) + "\nSomething went wrong. Check your subscription key and headers.\n")

基本的にはAzureサイト内にあるリファレンスと一緒ですが、Jupyter用に少し書き換えたのとリージョンを変えているくらいです。
上記のコードをJupyterのセルに入力して実行してくれればsample.mp3という音声ファイルができあがります。

*注意点*

音声系はいわゆる**SSML(Speech Synthesis Markup Language)**というスキーマですので色々調べるといろいろ出てきます。慣れておくとAmazon EchoんどVUIでも応用可能です。

7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?