can i know in the Agent how many samples were sent...
# ask-ai
a
can i know in the Agent how many samples were sent after finishing playback?
t
Thanks for reaching out, Sebastián. 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 find the number of audio samples sent after playback finishes in the Agent! You can achieve this by using the
PlaybackFinishedEvent
emitted by the
AudioOutput
object. This event carries a handy
playback_position
field, which tells you the total duration of audio played back in seconds. To get the number of samples, simply multiply the
playback_position
by the
sample_rate
from the
AudioOutput
configuration. Here's a little example to guide you:
Copy code
from livekit.agents.voice.io import PlaybackFinishedEvent

@session.output.audio.on("playback_finished")
def _on_playback_finished(ev: PlaybackFinishedEvent):
    sample_rate = session.output.audio.sample_rate  # Final output sample rate
    playback_position = ev.playback_position       # Duration in seconds

    if sample_rate is not None:
        total_samples = int(playback_position * sample_rate)
        print(f"Number of samples sent: {total_samples}")
This method helps ensure you get an accurate count of the total samples sent, even if there’s any intermediate resampling during processing. If you have any questions or need further help with this, feel free to ask—I'm here to support you! Sources: Bringing AI avatars to voice agents | livekit-agents/livekit/agents/voice/transcription/_speaking_rate.py
a
and how can i know when the agent started sending audio?
t
To help you figure out exactly when the Agent starts sending audio, you can use the
onFirstFrame
callback. This event is triggered as soon as the very first audio frame is captured and sent, which means audio output has just begun. Here’s a simple example to illustrate how you might implement it:
Copy code
def _on_first_frame(future: asyncio.Future[None]) -> None:
    self._session._update_agent_state("speaking")
    print("Agent started sending audio")
Usually, this callback is connected to the audio output process during tasks like TTS or real-time audio generation. When the first audio frame gets processed, the callback activates, letting you know that audio transmission is underway. If you’d like any more help setting this up or have other questions, please don’t hesitate to reach out—I’m here to assist you! Sources: agents/src/voice/agent_activity.ts | How to detect when an agent has finished speaking | examples/avatar_agents/bey/README.md