volume

Audio volume tracking over a rolling window of recent audio.

class pipecat.audio.volume.AudioVolumeTracker[source]

Bases: object

Tracks the volume of an audio stream over a rolling window.

Audio is fed in chunks of any size with update(), which retains the most recent VOLUME_WINDOW_SECS of it, and volume measures that window. Volume reads 0 until the window holds enough audio for loudness to be measurable, and the window is discarded if the sample rate changes.

Measuring costs a few hundred microseconds and grows with the sample rate, so it happens on read and the result is cached until more audio arrives. Callers that report volume less often than they receive audio pay only for the reads.

Audio is expected to be mono; interleaved channels would be measured as if they were consecutive samples.

__init__()[source]

Initialize the volume tracker.

property volume: float

Get the volume of the audio in the rolling window.

Returns:

Volume between 0 (quiet) and 1 (loud). Reads 0 until the window holds a measurable amount of audio.

update(audio: bytes, sample_rate: int)[source]

Add audio to the rolling window.

Parameters:
  • audio – Audio data as raw bytes (16-bit signed integers, mono).

  • sample_rate – Sample rate of the audio in Hz.

reset()[source]

Clear the rolling window and the tracked volume.