Python Streaming Example
1 import asyncio 2 import base64 3 import json 4 import os 5 import time 6 7 import pyaudio 8 import websockets 9 from dotenv import load_dotenv 10 11 load_dotenv() 12 13 API_KEY = os.getenv("RESEMBLE_API_KEY") 14 STREAMING_URL = "wss://websocket.cluster.resemble.ai/stream" 15 VOICE_UUID = "<voice_uuid>" 16 17 p = pyaudio.PyAudio() 18 stream = p.open( 19 format=pyaudio.paInt16, 20 channels=1, 21 rate=48000, 22 output=True, 23 ) 24 25 async def connect_websocket(): 26 return await websockets.connect( 27 STREAMING_URL, 28 extra_headers={"Authorization": f"Bearer {API_KEY}"}, 29 ping_interval=5, 30 ping_timeout=10, 31 ) 32 33 async def listen(): 34 while True: 35 websocket = None 36 try: 37 websocket = await connect_websocket() 38 while True: 39 text = input("Text: ") 40 if not text: 41 await websocket.ping() 42 continue 43 44 request = { 45 "voice_uuid": VOICE_UUID, 46 "data": text, 47 "precision": "PCM_16", 48 "no_audio_header": True, 49 "sample_rate": 48000, 50 } 51 await websocket.send(json.dumps(request)) 52 53 first_chunk = True 54 start = time.time() 55 56 while True: 57 message = await websocket.recv() 58 data = json.loads(message) 59 60 if data["type"] == "audio": 61 audio = base64.b64decode(data["audio_content"]) 62 if first_chunk: 63 print(f"TTFS: {time.time() - start:.3f}s") 64 first_chunk = False 65 stream.write(audio) 66 67 if data["type"] == "audio_end": 68 break 69 except websockets.exceptions.ConnectionClosedError: 70 print("Connection closed. Reconnecting...") 71 except Exception as exc: 72 print(f"Error: {exc}") 73 finally: 74 if websocket: 75 await websocket.close() 76 await asyncio.sleep(1) 77 78 asyncio.get_event_loop().run_until_complete(listen())
Replace <voice_uuid> with a streaming-enabled voice. This example prints time-to-first-sound (TTFS) and plays PCM chunks via PyAudio. Adjust buffer handling or output device configuration to suit your environment.
