Skip to content

Python example suggestion: Demo showing how to play generated .wav #101

@davidarchanjo

Description

@davidarchanjo
# pip install sounddevice
from supertonic import TTS
import sounddevice as sd

tts = TTS(auto_download=True)
style = tts.get_voice_style(voice_name='M1')

text = "This morning, I took a walk in the park, and the sound of the birds and the breeze was so pleasant that I stopped for a long time just to listen."
wav, duration = tts.synthesize(text, voice_style=style, lang='en')

print(f'Generated audio (duration: {duration[0]:.2f} seconds)')

# # Inspect the waveform shape
# print(f"Waveform shape: {wav.shape}")
# print(f"Waveform dtype: {wav.dtype}")

# Ensures the array is 1D, which is the standard format for mono audio playback in libraries like `sounddevice`. This prevents shape mismatches during audio output, where sounddevice expects a 1D array for single-channel audio.
# Without squeezing, sounddevice is unable to interpret the shape correctly, leading to errors or unexpected behavior during playback.
wav = wav.squeeze()

# Play the audio using sounddevice
sd.play(wav.astype("float32"))
sd.wait()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions