How can I use the silero vad plugin but at a lower...
# ask-ai
b
How can I use the silero vad plugin but at a lower level with audio streams. I want to use the model directly on frames in a for loop like this: async for audio_frame_event in audio_stream:
t
Thanks for reaching out, Jacob. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to guide you through using the Silero VAD plugin at a lower level with audio streams. You can process individual audio frames in a loop, and here’s a friendly step-by-step on how to do it: 1. Load the VAD Model: Start by using
silero.VAD.load()
to initialize your model. You can set parameters like
min_speech_duration
and
sample_rate
to make sure everything runs smoothly and efficiently. 2. Create a VAD Stream: Next, create a
VADStream
instance to handle processing your audio frames. 3. Push Frames to the Stream: Inside your loop, simply use
stream.push_frame(frame)
to send each audio frame for inference. Just double-check that your frames are in the right format, such as
rtc.AudioFrame
. 4. Consume VAD Events: Finally, use an
async for
loop to listen for events like
START_OF_SPEECH
and
END_OF_SPEECH
. These events will give you helpful metadata and buffered audio frames that you can use for further processing. This method offers you great control over frame-level processing, which is perfect if you’re aiming for custom real-time audio analysis. If you’d like, I’d be more than happy to provide a detailed example or help you with any other questions you might have! Sources: livekit-agents/livekit/agents/voice/agent.py | Silero VAD plugin | LiveKit docs | examples/src/realtime_turn_detector.ts | Turn detection and interruptions | LiveKit Docs
b
can I create multiple VADstreams or should I share it between streams?
t
Great question! You can definitely create multiple
VADStream
instances for different audio streams. Each time you call
vad.stream()
, it gives you a new, independent
VADStream
. This way, you can comfortably process separate audio streams at the same time. It’s actually the best way to go, especially if you’re working with multiple participants or audio inputs, because it keeps each stream nicely independent from the others. If you need any help setting this up, I’m here for you! Sources: agents/src/vad.ts | livekit-agents/livekit/agents/stt/stream_adapter.py
b
it wont load the model multiple times correct?
t
I'm glad you asked! When you create multiple
VADStream
instances from the same
VAD
instance, rest assured that the Silero VAD model is only loaded once. This happens when you call
VAD.load()
. After that, all the streams share the same underlying inference session, which helps keep things memory-efficient. Each stream still keeps its own processing state, so they work independently without any unnecessary duplication. If you have any more questions or need further clarification, I'm here to help! Sources: examples/voice_agents/silent_function_call.py | plugins/silero/src/vad.ts | Silero VAD plugin | LiveKit docs
b
show me how to do this: 1. Consume VAD Events: Finally, use an
async for
loop to listen for events like
START_OF_SPEECH
and
END_OF_SPEECH
. These events will give you helpful metadata and buffered audio frames that you can use for further processing.
t
Here's a friendly example to help you get started with consuming VAD events using an
async for
loop:
Copy code
async def process_vad_events(vad_stream):
    async for event in vad_stream:
        if event.type == vad.VADEventType.START_OF_SPEECH:
            print("🎤 Speech started")
            print(f"Speech duration so far: {event.speech_duration:.2f}s")
            
            # You can access the audio frames that triggered speech detection here
            for frame in event.frames:
                print(f"Frame sample rate: {frame.sample_rate}")

        elif event.type == vad.VADEventType.INFERENCE_DONE:
            print("🔄 Inference completed")
            print(f"Speech probability: {event.probability:.3f}")

        elif event.type == vad.VADEventType.END_OF_SPEECH:
            print("🔇 Speech ended")
            print(f"Total speech duration: {event.speech_duration:.2f}s")
            
            # This is a great spot to process the complete speech segment
            for frame in event.frames:
                # For example: Save or analyze the audio frames
                pass
In this example, the
vad_stream
sends events like
START_OF_SPEECH
,
INFERENCE_DONE
, and
END_OF_SPEECH
. Each event comes with useful metadata and audio frames for you to work with. If you'd like, I’m here to provide more details or tailor the example to your specific needs—just let me know! Sources: livekit-agents/livekit/agents/voice/audio_recognition.py
b
in a class like this show me how I'd create a task to process vad events: class AudioProcessor: def __init__(self): self._vad_stream = silero.VADStream() async def process_audio_frame(self, frame: rtc.AudioFrame): self._vad_stream.push_frame(frame)
t
I'd love to help you create a task within your
AudioProcessor
class to handle VAD events smoothly. Here's a simple way to do it:
Copy code
import asyncio
from livekit.plugins import silero

class AudioProcessor:
    def __init__(self):
        self._vad = silero.VAD.load()
        self._vad_stream = self._vad.stream()

    async def process_audio_frame(self, frame):
        self._vad_stream.push_frame(frame)

    async def process_vad_events(self):
        async for event in self._vad_stream:
            if event.type == silero.VADEventType.START_OF_SPEECH:
                print("🎤 Speech started")
            elif event.type == silero.VADEventType.END_OF_SPEECH:
                print("🔇 Speech ended")
                print(f"Total speech duration: {event.speech_duration:.2f}s")
            elif event.type == silero.VADEventType.INFERENCE_DONE:
                print(f"🔄 Inference completed - Probability: {event.probability:.3f}")

    def start_vad_task(self):
        # Create a background task to process VAD events
        asyncio.create_task(self.process_vad_events())
In this example, the
start_vad_task
method helps you kick off a background task that listens for VAD events asynchronously. Once you initialize your class, calling this method will let you start processing events while you continue pushing audio frames using
process_audio_frame
. I hope this makes things clearer and easier for you! Sources: livekit-plugins/livekit-plugins-silero/livekit/plugins/silero/__init__.py | Silero VAD plugin | LiveKit docs | examples/src/raw_function_description.ts | metrics/metrics_vad.py | Turn detection and interruptions | LiveKit Docs
b
what does inference done mean? how can I continuously run speech start/stop checks on the stream
t
The
INFERENCE_DONE
event lets you know that the VAD has finished analyzing a segment of audio to figure out how likely it is that someone is speaking. It gives you: • Probability: A confidence score (from 0 to 1) showing how sure we are that speech is present. • Speaking: A simple true/false indicating if speech is currently detected. • Frames: The specific audio frames that were processed during this check. • Timing: Helpful details like how long speech and silence lasted. If you want to keep track of when speech starts and stops in a stream, here’s a friendly step-by-step: 1. Create a
VAD
instance with the thresholds that suit your needs (for example, setting
min_speech_duration
). 2. Use the
.stream()
method to get a
VADStream
. 3. Push your audio frames into the stream by calling
push_frame(frame)
. 4. Keep an eye out for
START_OF_SPEECH
,
END_OF_SPEECH
, and
INFERENCE_DONE
events inside an
async for
loop to watch speech activity as it happens. If you’d like, I’d be happy to share a code example to help you set this up smoothly! Sources: plugins/silero/src/vad.ts | livekit-agents/livekit/agents/voice/audio_recognition.py
b
can I just use the model to check if a frame is speech or not?
t
Thank you for your question! The Silero VAD model is actually designed to work best when processing audio in fixed-size windows (like 256 samples for 8kHz or 512 samples for 16kHz) rather than checking a single audio frame for speech. It uses the surrounding audio context to make more accurate decisions, so it buffers audio until it has a full window and even uses overlapping segments from earlier audio to improve its accuracy. While it might seem possible to run inference on a single frame, the model’s design relies on streaming context, which means single-frame decisions can be a bit noisy and less reliable. To get the best and most accurate results, I’d recommend using the VAD in its intended streaming mode. This way, it can continuously process frames and notify you with events like
START_OF_SPEECH
and
END_OF_SPEECH
. I hope this helps clarify things! Feel free to ask if you have any more questions. Sources: livekit-agents/livekit/agents/voice/agent.py | README.md
b
Im not getting those events, only inference done. why
t
It sounds like you're only seeing
INFERENCE_DONE
events and not the
START_OF_SPEECH
or
END_OF_SPEECH
ones. Let's explore a few reasons this might be happening: • Speech Thresholds Not Met: The
START_OF_SPEECH
event happens only when the speech probability goes above the
activation_threshold
and the speech duration reaches at least
min_speech_duration
. Likewise,
END_OF_SPEECH
requires that silence lasts longer than
min_silence_duration
. • Insufficient Speech Activity: Sometimes, the audio might not have speech segments long enough to meet these thresholds. • Incorrect Sample Rate: It’s important to make sure your audio sample rate is either 8000 Hz or 16000 Hz, as these are the supported rates. • Configuration: Double-check that your
activation_threshold
,
min_speech_duration
, and
min_silence_duration
settings fit what you need for your specific situation. Take a moment to review your audio input and VAD configuration to see if they match these points. If you'd like, I’m here to help you adjust these settings step-by-step! Sources: livekit-agents/livekit/agents/voice/audio_recognition.py
b
got event VADEvent(type=<VADEventType.INFERENCE_DONE: 'inference_done'>, samples_index=512, timestamp=0.032, speech_duration=0.0, silence_duration=0.032, frames=[rtc.AudioFrame(sample_rate=48000, num_channels=1, samples_per_channel=1536, duration=0.032)], probability=0.012780606746673584, inference_duration=0.0007277889999386389, speaking=False, raw_accumulated_silence=0.0, raw_accumulated_speech=0.0)
t
Thank you for sharing the details! The
INFERENCE_DONE
event you received indicates a low speech probability (0.012), which is below the default
activation_threshold
(usually 0.5). That’s why you’re not seeing any
START_OF_SPEECH
or
END_OF_SPEECH
events triggered. Let’s work through some steps that might help: • Lower the Activation Threshold: You can try adjusting the
activation_threshold
to make the VAD more sensitive to softer or quieter speech. • Check Audio Sample Rate: It’s important to have the input audio sample rate at 8kHz or 16kHz since those are the supported rates. I noticed your frame shows 48kHz, which could be causing some issues. • Verify Audio Quality: Make sure the input audio is clear and doesn’t have too much background noise, as that can affect detection. • Adjust Duration Thresholds: You might also want to tweak the
min_speech_duration
and
min_silence_duration
settings to better fit your specific situation. If you’d like, I’m here to guide you through adjusting these settings or help troubleshoot further. Just let me know! Sources: livekit-agents/livekit/agents/voice/audio_recognition.py | agents/src/voice/audio_recognition.ts