Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Sintetizar fala com streaming bidirecional
Este documento orienta sobre o processo de sintetização de áudio com
streaming bidirecional.
Com o streaming bidirecional, é possível enviar entradas de texto e receber dados de áudio
simultaneamente. Isso significa que você pode começar a sintetizar a fala antes que o
texto de entrada completo seja enviado, o que reduz a latência e permite interações
em tempo real. Assistentes de voz e jogos interativos usam streaming bidirecional
para criar aplicativos mais dinâmicos e responsivos.
Para saber mais sobre os conceitos básicos do Text-to-Speech, leia
Princípios básicos do Text-to-Speech.
Antes de começar
Antes de enviar uma solicitação para a API Text-to-Speech, é preciso concluir
as ações a seguir. Consulte a
página antes de começar para ver os detalhes.
Sintetizar fala com streaming bidirecional
Instale a biblioteca de cliente
Enviar um stream de texto e receber um stream de áudio
A API aceita um fluxo de solicitações com o tipo StreamingSynthesizeRequest
,
que contém StreamingSynthesisInput
ou StreamingSynthesizeConfig
.
Antes de enviar um stream StreamingSynthesizeRequest
com
StreamingSynthesisInput
, que fornece entrada de texto, envie exatamente um
StreamingSynthesizeRequest
com um StreamingSynthesizeConfig
.
A transmissão da Text-to-Speech é compatível apenas com as vozes em alta definição do Chirp 3.
Limpar
Para evitar cobranças desnecessárias do Google Cloud Platform, use o
Google Cloud console para excluir o projeto se ele não for mais necessário.
Exceto em caso de indicação contrária, o conteúdo desta página é licenciado de acordo com a Licença de atribuição 4.0 do Creative Commons, e as amostras de código são licenciadas de acordo com a Licença Apache 2.0. Para mais detalhes, consulte as políticas do site do Google Developers. Java é uma marca registrada da Oracle e/ou afiliadas.
Última atualização 2025-08-11 UTC.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-08-11 UTC."],[],[],null,["# Quickstart: Synthesize speech with bidirectional streaming quickstart\n\nSynthesize speech with bidirectional streaming\n==============================================\n\nThis document walks you through the process of synthesizing audio using\nbidirectional streaming.\n\nBidirectional streaming lets you send text input and receive audio data\nsimultaneously. This means that you can start synthesizing speech before the\ncomplete input text is sent, which reduces latency and enables real-time\ninteractions. Voice assistants and interactive games use bidirectional streaming\nto create more dynamic and responsive applications.\n\nTo learn more about the fundamental concepts in Text-to-Speech, read\n[Text-to-Speech Basics](/text-to-speech/docs/basics).\n\nBefore you begin\n----------------\n\nBefore you can send a request to the Text-to-Speech API, you must have completed\nthe following actions. See the\n[before you begin](/text-to-speech/docs/before-you-begin) page for details.\n\n- Enable Text-to-Speech on a Google Cloud project.\n 1. Make sure billing is enabled for Text-to-Speech.\n-\n [Install](/sdk/docs/install) the Google Cloud CLI, and then\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n After signing in,\n [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n ```bash\n gcloud init\n ```\n\nSynthesize speech with bidirectional streaming\n----------------------------------------------\n\n### Install the client library\n\n### Python\n\nBefore installing the library, make sure you've [prepared your environment for Python development](/python/docs/setup). \n\n```\npip install --upgrade google-cloud-texttospeech\n```\n\n\u003cbr /\u003e\n\n### Send a stream of text and receive a stream of audio\n\nThe API accepts a stream of requests with type `StreamingSynthesizeRequest`,\nwhich contain either `StreamingSynthesisInput` or `StreamingSynthesizeConfig`.\n\nBefore sending a stream `StreamingSynthesizeRequest` with\n`StreamingSynthesisInput`, which provides text input, send exactly one\n`StreamingSynthesizeRequest` with a `StreamingSynthesizeConfig`.\n\nStreaming Text-to-Speech is only compatible with [Chirp 3: HD voices](/text-to-speech/docs/chirp3-hd). \n\n### Python\n\nBefore running the example, make sure you've [prepared your environment for Python development](/python/docs/setup). \n\n #!/usr/bin/env python\n # Copyright 2024 Google LLC\n #\n # Licensed under the Apache License, Version 2.0 (the \"License\");\n # you may not use this file except in compliance with the License.\n # You may obtain a copy of the License at\n #\n # http://www.apache.org/licenses/LICENSE-2.0\n #\n # Unless required by applicable law or agreed to in writing, software\n # distributed under the License is distributed on an \"AS IS\" BASIS,\n # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n # See the License for the specific language governing permissions and\n # limitations under the License.\n #\n\n \"\"\"Google Cloud Text-To-Speech API streaming sample application .\n\n Example usage:\n python streaming_tts_quickstart.py\n \"\"\"\n\n\n def run_streaming_tts_quickstart():\n \"\"\"Synthesizes speech from a stream of input text.\"\"\"\n from google.cloud import texttospeech\n\n client = texttospeech.https://cloud.google.com/python/docs/reference/texttospeech/latest/google.cloud.texttospeech_v1.services.text_to_speech.TextToSpeechClient.html()\n\n # See https://cloud.google.com/text-to-speech/docs/voices for all voices.\n streaming_config = texttospeech.https://cloud.google.com/python/docs/reference/texttospeech/latest/google.cloud.texttospeech_v1.types.StreamingSynthesizeConfig.html(\n voice=texttospeech.https://cloud.google.com/python/docs/reference/texttospeech/latest/google.cloud.texttospeech_v1.types.VoiceSelectionParams.html(\n name=\"en-US-Chirp3-HD-Charon\",\n language_code=\"en-US\",\n )\n )\n\n # Set the config for your stream. The first request must contain your config, and then each subsequent request must contain text.\n config_request = texttospeech.https://cloud.google.com/python/docs/reference/texttospeech/latest/google.cloud.texttospeech_v1.types.StreamingSynthesizeRequest.html(\n streaming_config=streaming_config\n )\n\n text_iterator = [\n \"Hello there. \",\n \"How are you \",\n \"today? It's \",\n \"such nice weather outside.\",\n ]\n\n # Request generator. Consider using Gemini or another LLM with output streaming as a generator.\n def request_generator():\n yield config_request\n for text in text_iterator:\n yield texttospeech.https://cloud.google.com/python/docs/reference/texttospeech/latest/google.cloud.texttospeech_v1.types.StreamingSynthesizeRequest.html(\n input=texttospeech.https://cloud.google.com/python/docs/reference/texttospeech/latest/google.cloud.texttospeech_v1.types.StreamingSynthesisInput.html(text=text)\n )\n\n streaming_responses = client.https://cloud.google.com/python/docs/reference/texttospeech/latest/google.cloud.texttospeech_v1.services.text_to_speech.TextToSpeechClient.html#google_cloud_texttospeech_v1_services_text_to_speech_TextToSpeechClient_streaming_synthesize(request_generator())\n\n for response in streaming_responses:\n print(f\"Audio content size in bytes is: {len(response.audio_content)}\")\n\n\n if __name__ == \"__main__\":\n run_streaming_tts_quickstart()\n\n\u003cbr /\u003e\n\nClean up\n--------\n\nTo avoid unnecessary Google Cloud Platform charges, use the\n[Google Cloud console](https://console.cloud.google.com/) to delete your project if you do not need it.\n\nWhat's next\n-----------\n\n\n- Learn more about Cloud Text-to-Speech by reading the [basics](/text-to-speech/docs/basics).\n- Review the list of [available voices](/text-to-speech/docs/voices) you can use for synthetic speech.\n\n\u003cbr /\u003e"]]