How to access VADEvent from inside a agent?
# ask-ai
s
How to access VADEvent from inside a agent?
t
Thanks for reaching out, Daniel. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're exploring how to access
VADEvent
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:
Copy code
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 docs
s
Actually, let's just save the speech probability and audio for debugging now. We can do this in main.
t
To help you save the speech probability and audio for debugging, you can tweak the main logic to capture and store these details easily. Here’s a friendly way to approach it: 1. Subscribe to the
metrics_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:
Copy code
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!
s
can we do this logic on @session.on("metrics_collected")?
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!