import requests
def synthesize_stream(text, voice="aero-vayu", callback=None):
"""Stream audio chunks as they are generated."""
response = requests.post(
f"{BASE_URL}/synthesize",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
},
json={
"text": text,
"voice": voice,
"model": "aero",
"format": "pcm",
},
stream=True,
)
response.raise_for_status()
for chunk in response.iter_content(chunk_size=4096):
if chunk and callback:
callback(chunk)
# Example: stream to file
with open("stream.pcm", "wb") as f:
synthesize_stream(
"This audio is streamed chunk by chunk.",
callback=lambda chunk: f.write(chunk),
)