is there a way to save the user speaking as audio ...
# ask-ai
n
is there a way to save the user speaking as audio file whenever user speaks and send them in stt_node
t
Thanks for reaching out, Hwuiwon. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
n
I want to use my own deepgram wrapper instead of websocket implementation of livekit
t
Hey there! If you want to save user audio as a file and send it in
stt_node
, a great way is to override the
stt_node
method in a custom agent. This lets you gather audio frames, save them as a WAV file, and then handle them with your own STT setup. Here’s a simple rundown: 1. Override the
stt_node
method to grab audio frames while still sending them for transcription. 2. Buffer those audio frames and use
rtc.combine_audio_frames()
to stitch them into a WAV file. 3. Save the WAV file at the right moments, like when the user stops talking. If you’re thinking about using your own Deepgram wrapper, you can swap out the default STT process in
stt_node
with your custom code. Just give me a shout if you want some detailed examples or a hand with anything! Sources: Pipeline nodes and hooks | LiveKit Docs | livekit-plugins/livekit-plugins-fal/livekit/plugins/fal/stt.py | Recording agent sessions | LiveKit Docs | Session recording and transcripts | LiveKit Docs
n
show me the sample code
t
Here's a handy example of a custom agent that saves user audio as a WAV file while transcribing:
Copy code
import asyncio
from livekit import rtc
from livekit.agents import Agent, ModelSettings, stt
from typing import AsyncIterable

class RecordingAgent(Agent):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.audio_buffer = []  # Buffer to store audio frames

    async def stt_node(
        self, audio: AsyncIterable[rtc.AudioFrame], model_settings: ModelSettings
    ) -> AsyncIterable[stt.SpeechEvent | str]:
        async def capture_and_forward():
            async for frame in audio:
                self.audio_buffer.append(frame)  # Capture audio frame
                yield frame  # Forward frame for transcription

        # Process audio through default STT pipeline
        async for event in Agent.default.stt_node(self, capture_and_forward(), model_settings):
            yield event

    async def on_user_turn_completed(self, turn_ctx, new_message):
        # When the user finishes speaking, save the buffered audio as a WAV file
        if self.audio_buffer:
            combined_audio = rtc.combine_audio_frames(self.audio_buffer)
            wav_data = combined_audio.to_wav_bytes()

            with open("user_audio.wav", "wb") as f:
                f.write(wav_data)

            print("Audio saved as user_audio.wav")
            self.audio_buffer.clear()  # Clear the buffer for the next turn
Here’s what this does: • It collects audio frames during transcription inside the
stt_node
method. • Once the user finishes speaking, it saves all those buffered frames as a WAV file using the
on_user_turn_completed
lifecycle hook. If you’d like me to help customize this further or integrate it with your Deepgram wrapper, just let me know—I’m here to help!
n
i would like to use that result in the llm_node. how can i do it
t
When the
stt_node
finishes, it automatically passes its result to the
llm_node
as part of the conversation context. But if you want to tweak how that transcription result is used, you can override the
on_user_turn_completed
lifecycle hook. This hook fires right after the user’s speech is transcribed, but before the LLM gets to process the input. Here’s a quick example to show you how it works:
Copy code
async def on_user_turn_completed(
    self, turn_ctx: ChatContext, new_message: ChatMessage
) -> None:
    # Modify the user’s message before it’s sent to the LLM
    new_message.content = f"User said: {new_message.text_content()}"

    # Optionally, add additional context for the LLM
    turn_ctx.add_message(
        role="assistant",
        content="Relevant context retrieved from database."
    )
This way, you can easily adjust the transcription or add some extra context before the LLM replies. If you want me to walk you through it more or need other examples, just let me know—I’m happy to help! Sources: Pipeline nodes and hooks | LiveKit Docs