首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google TTS客户端库中文参数设置

问Google TTS客户端库中文参数设置
EN

Stack Overflow用户
提问于 2018-04-10 13:25:53
回答 1查看 713关注 0票数 3

我尝试使用Google cloud API的texttospeech,这是示例代码,它在英文中运行良好;但当我将language_ code ='en-US‘更改为'zh-CN’并将输入文本设置为中文单词时,显示错误: 400 No TTS voice matching the TTS request。请更正语音选择参数,然后重试。

代码语言:javascript
复制
import argparse
# [START tts_synthesize_text]
def synthesize_text(text):
    """Synthesizes speech from the input string of text."""
    from google.cloud import texttospeech
    client = texttospeech.TextToSpeechClient()

    input_text = texttospeech.types.SynthesisInput(text=text)

# Note: the voice can also be specified by name.
# Names of voices can be retrieved with client.list_voices().
    voice = texttospeech.types.VoiceSelectionParams(
        language_code='en-US',
        ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)

    audio_config = texttospeech.types.AudioConfig(
        audio_encoding=texttospeech.enums.AudioEncoding.MP3)

    response = client.synthesize_speech(input_text, voice, audio_config)

    # The response's audio_content is binary.
    with open('output.mp3', 'wb') as out:
        out.write(response.audio_content)
        print('Audio content written to file "output.mp3"')
# [END tts_synthesize_text]

请帮助我在使用中文时如何设置参数?

EN

回答 1

Stack Overflow用户

发布于 2018-05-29 09:00:01

我使用gTTS包来解决这个问题,它对我很有效,代码如下:

代码语言:javascript
复制
def text_to_speech_gtts(res_ans):
    from gtts import gTTS
    volume=1.0
    music_file="ans01.mp3"
    tts = gTTS(text=res_ans, lang='zh-tw')
    tts.save(music_file)
    freq = 25000    # audio CD quality
    bitsize = -16    # unsigned 16 bit
    channels = 2     # 1 is mono, 2 is stereo
    buffer = 2048    # number of samples (experiment to get best sound)
    pg.mixer.init(freq, bitsize, channels, buffer)
    # volume value 0.0 to 1.0
    pg.mixer.music.set_volume(volume)
    clock = pg.time.Clock()
    try:
        pg.mixer.music.load(music_file)
    except pg.error:
        print("File {} not found! ({})".format(music_file, pg.get_error()))

    pg.mixer.music.play()
    while pg.mixer.music.get_busy():
        # check if playback has finished
        clock.tick(30) #
    pg.mixer.music.stop()
    pg.mixer.quit()  
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49745935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档