iT邦幫忙

2021 iThome 鐵人賽

DAY 8
0
AI & Data

AI ninja project系列 第 8

AI ninja project [day 8] 文字轉語音

  • 分享至 

  • xImage
  •  

除了有語音轉文字的服務,
GCP 也提供了文字轉語音的服務。
可以想像過去我們在遊戲或是動畫中需要很多配音員,
現在我們只要有文字,就能生成該角色的聲音。

首先是安裝:

pip install  google-cloud-texttospeech

下面程式碼,是使用的方式:

import os
from google.cloud import texttospeech

credential_path = "cred.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path

# Instantiates a client
client = texttospeech.TextToSpeechClient()

# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(text="Hello, World!")

# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
    language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)

# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(
    audio_encoding=texttospeech.AudioEncoding.MP3
)

# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(
    input=synthesis_input, voice=voice, audio_config=audio_config
)

with open("output.mp3", "wb") as out:
    out.write(response.audio_content)
    print('Audio content written to file "output.mp3"')

synthesis_input為輸入的台詞,
而texttospeech.VoiceSelectionParams參數的部分,為聲音的調整,
可以從下面的function列出可選用的語音參數:

import os

credential_path = "cred.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path

def list_voices():
    """Lists the available voices."""
    from google.cloud import texttospeech

    client = texttospeech.TextToSpeechClient()

    # Performs the list voices request
    voices = client.list_voices()

    for voice in voices.voices:
        # Display the voice's name. Example: tpc-vocoded
        print(f"Name: {voice.name}")

        # Display the supported language codes for this voice. Example: "en-US"
        for language_code in voice.language_codes:
            print(f"Supported language: {language_code}")

        ssml_gender = texttospeech.SsmlVoiceGender(voice.ssml_gender)

        # Display the SSML Voice Gender
        print(f"SSML Voice Gender: {ssml_gender.name}")

        # Display the natural sample rate hertz for this voice. Example: 24000
        print(f"Natural Sample Rate Hertz: {voice.natural_sample_rate_hertz}\n")

list_voices()

https://ithelp.ithome.com.tw/upload/images/20210908/20122678Vajf3nsABe.png

選定之後,我們可以從官網看一下VoiceSelectionParams的參數:
https://cloud.google.com/text-to-speech/docs/reference/rpc/google.cloud.texttospeech.v1#voiceselectionparams

可以發現給定language_code、name、ssml_gender可以完成設定。

而audio_encoding選擇格式為mp3,其他格式也能從文件做查詢:
https://cloud.google.com/text-to-speech/docs/reference/rpc/google.cloud.texttospeech.v1#google.cloud.texttospeech.v1.AudioEncoding

儲存mp3,完成這天的進度。

價格的話也是不貴,免費空間也是很大的,可以從圖片中做參考:

https://ithelp.ithome.com.tw/upload/images/20210908/201226788SCwcBX4nO.png


上一篇
AI ninja project [day 7] 語音轉文字
下一篇
AI ninja project [day 9] 先驗演算法
系列文
AI ninja project30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言