2022-08-22

【Python3】微軟的 文字轉換語音(Text To Speach, TTS)

參考資料 ----

申請 "微軟語音服務" 請參考最下方的筆記

系統需求:
Python 3.7(含)↑
 
# 目前(2022.08.22) 尚不支援 Ubuntu22
# Ubuntu20 / Linuxmint20 必須安裝這些套件
[user]$ sudo  apt  install  python3-pip  build-essential  libssl-dev  libasound2 wget

[user]$ pip  install  azure-cognitiveservices-speech
 

tts.py
 
import azure.cognitiveservices.speech as speechsdk

speech_config = speechsdk.SpeechConfig(subscription="您申請的金鑰", region="eastasia")    # eastasia 是我在微軟申請服務選取的區域,請置換為您申請的區域
# audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)    # 即時由喇叭播放
audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=False, filename="king.mp3")    # 存成 mp3

# The language of the voice that speaks.
speech_config.speech_synthesis_voice_name='zh-TW-Yating'    // 語音角色: 雅婷

speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)

# Get text from the console and synthesize to the default speaker.
text = "有一位國王,他擁有一座美麗的花園,花園裡有一棵結著金蘋果的樹。國王派人每天清點樹上的蘋果有幾顆。"

speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()

if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
    print("Speech synthesized for text [{}]".format(text))
elif speech_synthesis_result.reason == speechsdk.ResultReason.Canceled:
    cancellation_details = speech_synthesis_result.cancellation_details
    print("Speech synthesis canceled: {}".format(cancellation_details.reason))
    if cancellation_details.reason == speechsdk.CancellationReason.Error:
        if cancellation_details.error_details:
            print("Error details: {}".format(cancellation_details.error_details))
            print("Did you set the speech resource key and region values?")
           

 


相關筆記 ----

沒有留言:

張貼留言