Real-time streaming
Telephony: Twilio Media Streams
Bridge phone calls to Shihab Voice with mu-law 8 kHz audio.
How it works
Phone networks carry 8 kHz mu-law (North America, Japan) or a-law (most other countries) audio. Shihab Voice accepts both natively, so a bridge only has to pass the bytes through: no resampling, no transcoding. With Twilio, a <Stream> in your TwiML forks the call audio to your WebSocket server as base64 JSON messages; your server decodes each payload and forwards it to Shihab Voice.
Caller ──► Twilio ──(wss, base64 mu-law JSON)──► your bridge ──(wss, binary mu-law)──► Shihab Voice
◄──────────── JSON transcripts ─────────────Start a media stream
Point your Twilio number (or a call you create) at TwiML like this. <Start> streams in the background while the rest of the call continues; track="inbound_track" sends the caller's audio.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Start>
<Stream url="wss://your-host.example.com/twilio" track="inbound_track" />
</Start>
<Say>Hello, how can I help?</Say>
<Pause length="60" />
</Response>The bridge server
The bridge accepts Twilio's WebSocket, opens one Shihab Voice stream per call with encoding=mulaw&sample_rate=8000, and prints the caller's words as they are finalised. Feed the transcripts to your agent, CRM or QA tooling instead of printing them.
# pip install fastapi uvicorn shihab-asr
# uvicorn twilio_bridge:app --port 8080
import asyncio
import base64
import json
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from shihab_asr import AsyncShihabASR, StreamError
app = FastAPI()
client = AsyncShihabASR() # reads SHIHAB_API_KEY
@app.websocket("/twilio")
async def twilio(ws: WebSocket) -> None:
await ws.accept()
call_sid = None
# Twilio sends 8 kHz mu-law: forward it untouched
async with client.stream(encoding="mulaw", sample_rate=8000, endpointing=600, tags=["twilio"]) as asr:
async def print_transcripts() -> None:
try:
async for event in asr:
if event.type == "Results" and event.is_final and event.transcript:
print(f"[{call_sid}] caller: {event.transcript}")
except StreamError as e:
print(f"[{call_sid}] stream ended: {e}")
reader = asyncio.create_task(print_transcripts())
try:
while True:
msg = json.loads(await ws.receive_text())
if msg["event"] == "start":
call_sid = msg["start"]["callSid"]
elif msg["event"] == "media":
await asr.send(base64.b64decode(msg["media"]["payload"]))
elif msg["event"] == "stop":
break
except WebSocketDisconnect:
pass
finished = await asr.close()
await reader
print(f"[{call_sid}] {finished.audio_seconds:.1f} s transcribed, ${finished.cost_usd}")Other telephony stacks
Anything that can give you the raw call audio works the same way: Asterisk or FreeSWITCH media forks, SIP media servers, or recordings from your PBX. Use encoding=alaw for G.711 a-law, encoding=mulaw for G.711 mu-law, or linear16 for PCM, with the sample rate your system produces. For recorded calls, send the files to /v1/listen or async jobs.
Need help? Email [email protected] or message us on WhatsApp.