[ Linkit Smart 7688 ] 透過 Python 使用 Microsoft 的 Cognitive Services Text To Speech 服務

Microsoft 提供的雲端服務為數眾多,例如:Bing SpeechLanguage Understanding Intelligent ServiceText AnalyticsSpeaker Recognition,將語音傳送給 Microsoft,並透過 Cognitive Services Text To Speech 的服務將文字轉換為語音後傳送回 Linkit Smart 7688 。


前置準備作業

  1. 準備一片 Linkit Smart 7688 開發板

  2. 將 Linkit Smart 7688 連接至電腦

  3. 更新 Firmware 為 0.9.3
    http://goo.gl/dVLQ2Y

  4. 將 USB 音效卡透過 OTG Cable 安裝於 Linkit Smart 7688

  5. 安裝 MIC 與 Speaker 於外接 USB 音效卡上

  6. 安裝 USB 音效卡相關套件
    REF: http://goo.gl/D5rHtu


Linkit Smart 7688 與 USB 外接音效卡連接圖


Microsoft 端

Step 1. 到 Microsoft 網站申請帳號

https://www.microsoft.com/cognitive-services/


Step 2. 登入 Microsoft Cognitive Services 網站

https://www.microsoft.com/cognitive-services/


Step 3. 點擊 Get started for free


Step 4. 點選 Bing Speech - PreviewI agree to the Microsoft Cognitive Services Terms and Microsoft Privacy StatementSubscribe


Linkit Smart 7688 端

Step 1. SSH 進入 Linkit Smart 7688 中

Imgur


Step 2. 於 Linkit Smart 7688 中取得 Microsoft Token

請參考 取得 Microsoft 的 Cognitive Services Token


Step 3. 於 Linkit Smart 7688 中於 Linkit Smart 7688 中進行 Text To Speech
import get_microsoft_token  
import httplib, json, urllib  
import requests  
import uuid  
from requests.packages.urllib3.exceptions import InsecureRequestWarning  
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

getUUID = uuid.uuid4()  
accessToken = get_microsoft_token.getAccessToke()  
lang = "en-US"  
female = True  
text = "I come from Taiwan"  
template = """  
        <speak version='1.0' xml:lang='{0}'>
            <voice xml:lang='{0}' xml:gender='{1}' name='{2}'>
                {3}
            </voice>
        </speak>
"""

speechHost = "speech.platform.bing.com"  
headers = {  
    "Content-type": "application/ssml+xml",
    "X-Microsoft-OutputFormat": "riff-16khz-16bit-mono-pcm",
    "Authorization": "Bearer " + accessToken,
    "X-Search-AppId": str(getUUID).replace('-', ''),
    "X-Search-ClientID": str(getUUID).replace('-', ''),
    "User-Agent": "TTSForPython"
}
name = "Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)"  
data = template.format(lang, "Female" if female else "Male", name, text)

conn = httplib.HTTPSConnection(speechHost)  
conn.request("POST", "/synthesize", data, headers)  
response = conn.getresponse()  
ttsResponse = response.read()  
conn.close()  
print(response.status, response.reason)  
if response.status == 200:  
    f = open('test.wav','wb')
    f.write(ttsResponse)
    f.close()


Step 4. 執行 Python Code
python microsoft_text_to_speech.py  


Github
參考資料
List of blogs

Archer

Having being a full stack engineer. Experience with C, Python, Objective-C, Swift, Node.js, Lua, Linkit Smart 7688, Raspberry Pi, ARM mbed, Arduino, IoT solutions. Contact us : [email protected]

ALL RIGHTS RESERVED. COPYRIGHT © 2016. Designed and Coded by Makee.io