Skip to content

[Feat]: google tts streaming with async client #1924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
jayeshp19 opened this issue Apr 2, 2025 · 1 comment
Open
1 task done

[Feat]: google tts streaming with async client #1924

jayeshp19 opened this issue Apr 2, 2025 · 1 comment
Assignees

Comments

@jayeshp19
Copy link

jayeshp19 commented Apr 2, 2025

Saw this example using Google TTS streaming with Chirp3. I'm trying to do something similar with the async client but running into issues. Only the first sentence plays from iterator

Here’s a minimal repro:

import asyncio

from google.cloud import texttospeech


async def process_streaming_synthesis():
    client = texttospeech.TextToSpeechAsyncClient()

    streaming_config = texttospeech.StreamingSynthesizeConfig(
        voice=texttospeech.VoiceSelectionParams(
            name="en-US-Chirp3-HD-Charon",
            language_code="en-US",
        ),
        streaming_audio_config=texttospeech.StreamingAudioConfig(
            audio_encoding=texttospeech.AudioEncoding.OGG_OPUS
        ),
    )

    config_request = texttospeech.StreamingSynthesizeRequest(streaming_config=streaming_config)

    text_iterator = [
        "Hello there.",
        "How are you today?",
        "It's such nice weather outside.",
    ]

    async def request_generator():
        yield config_request
        for text in text_iterator:
            await asyncio.sleep(0)  # yield control to event loop
            yield texttospeech.StreamingSynthesizeRequest(
                input=texttospeech.StreamingSynthesisInput(text=text)
            )

    with open("output.ogg", "wb") as audio_file:
        stream = await client.streaming_synthesize(request_generator())
        async for response in stream:
            audio_file.write(response.audio_content)

    print("Complete audio saved to output.ogg")


def main():
    asyncio.run(process_streaming_synthesis())


if __name__ == "__main__":
    main()

Would really appreciate any pointers or working example. Thankss!

PTAL: @inardini @holtskinner

Code of Conduct

  • I agree to follow this project's Code of Conduct
@jayeshp19
Copy link
Author

Bumping this @holtskinner @inardini

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants