square-airport-30159
11/03/2025, 5:21 PMtall-airline-16756
11/03/2025, 5:21 PMtall-airline-16756
11/03/2025, 5:22 PMVADEvent from inside an agent! To do this, you’ll want to listen for events emitted by the VAD instance, especially the metrics_collected event. This event gives you detailed insights into voice activity, including the event type, timestamp, speech duration, and related audio frames.
Here’s a simple way to get started:
• First, initialize a VAD instance (for example, silero.VAD.load()).
• Next, register an event listener for the metrics_collected event.
• Then, handle the VADEvent inside the callback to work with the voice activity data.
Here’s a friendly example in Python:
from livekit import agents
from livekit.plugins import silero
class MyAgent(agents.Agent):
def __init__(self):
super().__init__(
vad=silero.VAD.load(), # Initialize the VAD
)
# Listen for metrics_collected events
self.vad.on("metrics_collected", self.on_vad_metrics)
async def on_vad_metrics(self, event: agents.vad.VADEvent):
print(f"VAD Event Type: {event.type}")
print(f"Timestamp: {event.timestamp}")
print(f"Speech Duration: {event.speech_duration}s")
print(f"Silence Duration: {event.silence_duration}s")
print(f"Speaking: {event.speaking}")
if event.type == "inference_done":
print(f"Probability: {event.probability}")
print(f"Inference Duration: {event.inference_duration}s")
# Process audio frames as needed
for frame in event.frames:
pass
This example should help you subscribe to metrics_collected and access all the key properties of VADEvent. If you have any questions or want me to walk you through any part of this, just let me know—I’m here to help!
Sources: livekit-agents/livekit/agents/voice/__init__.py | agents/src/voice/audio_recognition.ts | metrics/metrics_vad.py | Silero VAD plugin | LiveKit docssquare-airport-30159
11/03/2025, 5:32 PMtall-airline-16756
11/03/2025, 5:33 PMmetrics_collected event from the VAD instance.
2. Inside the event handler, grab the probability and audio frames from the VADEvent.
3. Save this extracted data to a file or a logging system so you can review it later for debugging.
Here’s a simple example in Python to guide you through it:
from livekit.plugins import silero
import json
# Initialize VAD instance
vad = silero.VAD.load()
# Event handler to save probability and audio
def save_debug_data(event):
if event.type == "inference_done":
# Save speech probability
with open("speech_probability.json", "a") as prob_file:
json.dump({"timestamp": event.timestamp, "probability": event.probability}, prob_file)
prob_file.write("\n")
# Save audio frames
with open("audio_frames.raw", "ab") as audio_file:
for frame in event.frames:
audio_file.write(frame.data)
# Register the event handler
vad.on("metrics_collected", save_debug_data)
# Main logic to process audio
while True:
# Push audio frames to VAD (replace with actual audio input)
vad.push_frame(get_next_audio_frame())
This example should help you log the speech probability and save audio frames smoothly for debugging. If you have any questions or need a hand with adjustments, just let me know—I’m here to help!square-airport-30159
11/03/2025, 6:09 PMtall-airline-16756
11/03/2025, 6:09 PM