是否可以在不编写新文件的情况下将文本合成为语音?

Is it possible to synthesize text to speech without writing a new file?

我想使用 GCP 文本到语音 API 将文本合成为语音,几乎我能找到的每个示例都会写入一个新文件,我想在函数输入文本并让它读取电脑音箱。我一直在尝试转换 GCP 上传的代码,上面写着 hello world。我一直没能找到一种在转换后立即阅读它的方法。 Watson 和 Azure 似乎有这项服务,但 GCP 没有?

client = texttospeech.TextToSpeechClient(credentials=credentials)


synthesis_input = texttospeech.types.SynthesisInput(text=string)


voice = texttospeech.types.VoiceSelectionParams(
    language_code='en-US',
    ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL)


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


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

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

非常感谢任何帮助,我想我缺少一些文档或简单的配置。

GCP Text To Speech APIs returns 包含音频数据的响应。您如何处理 return 上的数据由您决定。在上面的示例中,数据被写入文件。如果您愿意,您大概可以将该数据通过管道传输到音频播放器,以便在不涉及文件的情况下立即播放。数据格式的选择是 WAV、MP3 或 OGG ...请参阅 https://cloud.google.com/text-to-speech/docs/reference/rest/v1beta1/text/synthesize#AudioEncoding

至于一个API播放音频数据...Play audio with Python