2022-08-22

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

參考資料 ----

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

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

tts.py
  1.  
  2. import azure.cognitiveservices.speech as speechsdk
  3.  
  4. speech_config = speechsdk.SpeechConfig(subscription="您申請的金鑰", region="eastasia") # eastasia 是我在微軟申請服務選取的區域,請置換為您申請的區域
  5. # audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True) # 即時由喇叭播放
  6. audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=False, filename="king.mp3") # 存成 mp3
  7.  
  8. # The language of the voice that speaks.
  9. speech_config.speech_synthesis_voice_name='zh-TW-Yating'    // 語音角色: 雅婷
  10.  
  11. speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
  12.  
  13. # Get text from the console and synthesize to the default speaker.
  14. text = "有一位國王,他擁有一座美麗的花園,花園裡有一棵結著金蘋果的樹。國王派人每天清點樹上的蘋果有幾顆。"
  15.  
  16. speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()
  17.  
  18. if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
  19. print("Speech synthesized for text [{}]".format(text))
  20. elif speech_synthesis_result.reason == speechsdk.ResultReason.Canceled:
  21. cancellation_details = speech_synthesis_result.cancellation_details
  22. print("Speech synthesis canceled: {}".format(cancellation_details.reason))
  23. if cancellation_details.reason == speechsdk.CancellationReason.Error:
  24. if cancellation_details.error_details:
  25. print("Error details: {}".format(cancellation_details.error_details))
  26. print("Did you set the speech resource key and region values?")
  27.  
  28.  


相關筆記 ----

沒有留言:

張貼留言