Streaming Audio
Real-time Microphone Integration
Capture live microphone input using sounddevice and stream to the WebSocket.
Install extras
terminal
pip install shunyalabs[extras] # includes sounddeviceFull microphone example
python
import asyncio, sounddevice as sd, numpy as np
from shunyalabs import AsyncShunyaClient
from shunyalabs.asr import StreamingConfig, StreamingMessageType
SAMPLE_RATE = 16000
CHUNK_SIZE = 4096
async def main():
async with AsyncShunyaClient() as client:
conn = await client.asr.stream(
config=StreamingConfig(language="hi", sample_rate=SAMPLE_RATE)
)
@conn.on(StreamingMessageType.FINAL_SEGMENT)
def on_seg(msg): print(f">> {msg.text}")
def audio_callback(indata, frames, time, status):
pcm = (indata * 32767).astype(np.int16).tobytes()
asyncio.run_coroutine_threadsafe(
conn.send_audio(pcm), asyncio.get_event_loop()
)
with sd.InputStream(samplerate=SAMPLE_RATE, channels=1,
dtype='float32', blocksize=CHUNK_SIZE,
callback=audio_callback):
print("Recording... Press Ctrl+C to stop.")
await asyncio.sleep(30)
await conn.end()
await conn.close()
asyncio.run(main())