Call legs
RTPCall is the base class for all call leg handlers.
Audio Handling
voip.audio.AudioCall
dataclass
Bases: RTPCall
RTP call handler with audio buffering, codec negotiation, decoding, and encoding.
Codec selection is driven by PREFERRED_CODECS.
Override that list in a subclass to change priority. The selected codec
class is stored on codec after __post_init__ and used for all
encode/decode operations.
Source code in voip/audio.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
payload_type
property
Negotiated RTP payload type number.
sample_rate
property
SDP-negotiated audio sample rate in Hz.
Reflects the value from the remote a=rtpmap line. For G.722 this
is 8000 per RFC 3551 even though the codec runs at 16000 Hz
internally; use codec.sample_rate_hz to get the actual audio rate.
audio_received(*, audio, rms)
Handle decoded audio. Override in subclasses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
ndarray
|
Float32 mono PCM array at |
required |
rms
|
float
|
Root mean square of the decoded PCM, as a proxy for signal strength. |
required |
Source code in voip/audio.py
212 213 214 215 216 217 218 219 | |
decode_payload(payload)
Decode an RTP payload to float32 PCM at RESAMPLING_RATE_HZ.
Delegates to the negotiated codec,
passing the SDP-negotiated sample_rate as the input rate hint so
that non-standard variants (e.g. wideband PCMA at 16 000 Hz) are
handled correctly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
bytes
|
Raw RTP payload bytes. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Float32 mono PCM array at |
Source code in voip/audio.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
emit_audio(packet)
async
Decode packet and call audio_received.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
packet
|
RTPPacket
|
Parsed RTP packet whose payload will be decoded. |
required |
Source code in voip/audio.py
181 182 183 184 185 186 187 188 189 190 191 192 | |
negotiate_codec(remote_media)
classmethod
Select the best codec from the remote SDP offer.
Iterates PREFERRED_CODECS
in priority order, matching first by payload type number and then by
encoding name for dynamic payload types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remote_media
|
MediaDescription
|
The |
required |
Returns:
| Type | Description |
|---|---|
MediaDescription
|
A |
MediaDescription
|
chosen codec. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
When no offered codec is in |
Source code in voip/audio.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
next_rtp_packet(payload)
Create the next outbound RTP packet, incrementing sequence and timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
bytes
|
Encoded audio payload bytes. |
required |
Returns:
| Type | Description |
|---|---|
RTPPacket
|
RTP packet ready for transmission. |
Source code in voip/audio.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
packet_received(packet, addr)
Schedule audio decoding and delivery for packet.
Ignores packets with an empty payload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
packet
|
RTPPacket
|
Parsed RTP packet. |
required |
addr
|
tuple[str, int]
|
Remote |
required |
Source code in voip/audio.py
169 170 171 172 173 174 175 176 177 178 179 | |
resample(audio, source_rate_hz, destination_rate_hz)
classmethod
Resample audio from source_rate_hz to destination_rate_hz.
Delegates to RTPCodec.resample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
ndarray
|
Float32 mono PCM array. |
required |
source_rate_hz
|
int
|
Sample rate of audio in Hz. |
required |
destination_rate_hz
|
int
|
Target sample rate in Hz. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Resampled float32 array at destination_rate_hz Hz. |
Source code in voip/audio.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
send_rtp_audio(audio)
async
Encode audio with the negotiated codec and transmit via RTP.
Looks up the caller's remote RTP address from the shared
RealtimeTransportProtocol call
registry and transmits encoded audio as 20 ms RTP packets, sleeping
RTP_PACKET_DURATION_SECS between each packet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
ndarray
|
Float32 mono PCM at |
required |
Source code in voip/audio.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
Voice Activity Detection
voip.audio.VoiceActivityCall
dataclass
Bases: AudioCall
AudioCall with energy-based voice activity detection (VAD) and speech buffering.
Accumulates audio frames into speech_buffer based on the result of
collect_audio. A debounce
timer is armed on silence and fires
flush_speech_buffer
after silence_gap seconds of sustained quiet. Subclasses implement
speech_buffer_ready
to handle the buffered utterance.
Override collect_audio to
change which frames are accumulated. The default implementation buffers
only speech frames (RMS above speech_threshold). To buffer all frames
(e.g. for transcription that needs the full utterance including silent
pauses), override to always return True.
Attributes:
| Name | Type | Description |
|---|---|---|
speech_threshold |
float
|
RMS level below which audio is treated as silence. |
silence_gap |
float
|
Seconds of sustained silence required to flush the buffer. |
Source code in voip/audio.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |
collect_audio(audio, rms)
Return whether to buffer this audio frame.
The default implementation buffers speech frames only (RMS above
speech_threshold). Override to change the buffering strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
ndarray
|
Decoded float32 PCM frame. |
required |
rms
|
float
|
Root mean square of audio. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in voip/audio.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
flush_speech_buffer()
Concatenate buffered audio and schedule speech_buffer_ready.
Resets speech state so the next utterance starts with a clean buffer.
Source code in voip/audio.py
353 354 355 356 357 358 359 360 361 362 363 | |
on_audio_silence()
Arm the silence debounce timer when speech is buffered.
Source code in voip/audio.py
344 345 346 347 348 349 350 351 | |
on_audio_speech()
Cancel any pending silence timer when speech is detected.
Source code in voip/audio.py
338 339 340 341 342 | |
speech_buffer_ready(audio)
async
Handle the flushed speech buffer. Override in subclasses.
This base implementation is a no-op. Subclasses must override this method to process the buffered utterance (e.g. echo it back, transcribe it, etc.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
ndarray
|
Float32 mono PCM array at |
required |
Source code in voip/audio.py
365 366 367 368 369 370 371 372 373 374 375 | |
Echo Call
voip.audio.EchoCall
dataclass
Bases: VoiceActivityCall
RTP call handler that echoes the caller's speech back after they finish speaking.
Accumulates speech audio frames (RMS above speech_threshold) via the
VoiceActivityCall VAD machinery and
replays them once a sustained silence lasting silence_gap seconds is
detected. This gives the caller a natural echo of their own voice,
useful for network latency testing and call-flow demonstrations.
Example
class MySession(SessionInitiationProtocol):
def call_received(self, request: Request) -> None:
self.answer(request=request, call_class=EchoCall)
Source code in voip/audio.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
speech_buffer_ready(audio)
async
Resample and transmit buffered speech audio back to the caller.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
ndarray
|
Float32 mono PCM array at |
required |
Source code in voip/audio.py
396 397 398 399 400 401 402 403 404 405 | |
AI / Agentic Calls
voip.ai.TranscribeCall
dataclass
Bases: VoiceActivityCall
RTP call handler that transcribes audio with faster-whisper.
Audio is decoded by AudioCall on a per-packet
basis and delivered to audio_received,
which applies an energy-based voice activity detector (VAD) from
VoiceActivityCall. All audio frames
(speech and silence) are accumulated until silence is sustained for
silence_gap seconds, then the entire utterance is sent to Whisper as
one chunk. This avoids cutting sentences in the middle and prevents
background microphone noise from being passed to Whisper as spurious audio.
Override transcription_received
to handle the resulting text:
class MySession(SessionInitiationProtocol):
def call_received(self, request: Request) -> None:
self.answer(request=request, call_class=MyCall)
To share one model instance across multiple calls (recommended to avoid
loading it multiple times) pass a pre-loaded WhisperModel:
shared_model = WhisperModel("base")
class MyCall(TranscribeCall):
model = shared_model
Source code in voip/ai.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
collect_audio(audio, rms)
Buffer all audio frames (speech and silence) for transcription.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
ndarray
|
Decoded float32 PCM frame. |
required |
rms
|
float
|
Root mean square of audio. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Always |
Source code in voip/ai.py
74 75 76 77 78 79 80 81 82 83 84 | |
run_transcription(audio)
Transcribe a float32 PCM array using the Whisper model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
ndarray
|
Float32 mono PCM array at |
required |
Returns:
| Type | Description |
|---|---|
str
|
Concatenated transcription text from all segments. |
Source code in voip/ai.py
110 111 112 113 114 115 116 117 118 119 120 121 122 | |
speech_buffer_ready(audio)
async
Transcribe the buffered utterance when it meets the minimum length.
Skips utterances shorter than one second to avoid passing fragments to Whisper that would produce low-quality transcriptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
ndarray
|
Float32 mono PCM array at |
required |
Source code in voip/ai.py
86 87 88 89 90 91 92 93 94 95 96 97 | |
transcribe(audio)
async
Transcribe decoded audio and deliver non-empty text to the handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
ndarray
|
Float32 mono PCM array at |
required |
Source code in voip/ai.py
99 100 101 102 103 104 105 106 107 108 | |
transcription_received(text)
Handle a transcription result. Override in subclasses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Transcribed text for this audio chunk (already stripped). |
required |
Source code in voip/ai.py
124 125 126 127 128 129 | |
voip.ai.AgentCall
dataclass
Bases: TranscribeCall
RTP call handler that responds to caller speech using Ollama and Pocket TTS.
Extends TranscribeCall by feeding each
transcription to an Ollama language model, then synthesising the reply as
speech with Pocket TTS and streaming it back to the caller via RTP.
Chat history is maintained across turns so the language model can follow the conversation. A built-in system prompt informs the model that it is on a phone call.
To share the TTS model across multiple calls pass a pre-loaded
TTSModel:
shared_tts = TTSModel.load_model()
AgentCall(rtp=..., sip=..., tts_model=shared_tts)
Source code in voip/ai.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
respond()
async
Fetch an Ollama reply for pending text and stream it as speech via RTP.
On cancellation (human started speaking) the partial user turn is removed from the chat history so the history stays consistent.
Source code in voip/ai.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
send_speech(text)
async
Stream synthesised speech from Pocket TTS and send via RTP.
Yields audio chunks from
TTSModel.generate_audio_stream as soon as they are decoded,
enabling low-latency real-time delivery to the caller.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to synthesise and transmit. |
required |
Source code in voip/ai.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |