speech_segment_buffer

A buffer that keeps one stretch of speech, with the audio just before it.

class pipecat.audio.speech_segment_buffer.SpeechSegmentBuffer(*, pre_speech_secs: float = 1.0, max_secs: float | None = None)[source]

Bases: object

Keeps the audio of one stretch of speech, with the audio just before it.

Between stretches of speech it keeps only the last pre_speech_secs of audio, which holds the start of the speech the VAD reports late. From start() to stop() it keeps everything, or the last max_secs of it, and stop() returns the segment. The audio is 16-bit mono PCM.

Example:

buffer = SpeechSegmentBuffer()
buffer.set_sample_rate(16000)

buffer.append(audio)  # every input audio chunk
buffer.start()  # when the VAD reports speech
segment = buffer.stop()  # when the VAD reports silence
__init__(*, pre_speech_secs: float = 1.0, max_secs: float | None = None)[source]

Initialize the buffer.

Parameters:
  • pre_speech_secs – Seconds of audio kept from before speech starts. The VAD can report speech most of a second after it starts.

  • max_secs – The most seconds a segment keeps, its last ones, or None to keep it all.

set_sample_rate(sample_rate: int)[source]

Set the sample rate of the audio to come.

Parameters:

sample_rate – Audio sample rate in Hz.

append(audio: bytes)[source]

Add audio, dropping the oldest beyond what the buffer keeps.

Parameters:

audio – 16-bit mono PCM audio.

start()[source]

Start a segment, with the audio kept from before it.

stop() → bytes[source]

End the segment and return its audio, emptying the buffer.

Returns:

The segment’s 16-bit mono PCM audio.