salmon-elephant-7819
08/21/2025, 4:46 PMtall-airline-16756
08/21/2025, 4:46 PMtall-airline-16756
08/21/2025, 4:47 PMset_audio_enabled(True) for both streams.
4. Buffer and Sample Rate Management: Keeping your sample rates consistent (like 24kHz or 48kHz) and choosing the right buffer sizes (around 100ms blocks) helps everything stay in sync and sound smooth.
If you want me to walk you through any part of this or share some examples, just give me a shout—I’m here to help!
Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/beta/realtime... | stream_audio.py | Building voice agents | LiveKit Docssalmon-elephant-7819
08/21/2025, 4:50 PMtall-airline-16756
08/21/2025, 4:52 PMimport asyncio
import sounddevice as sd
import numpy as np
from livekit import rtc
from livekit.rtc import apm
class AudioStreamer:
def __init__(self, enable_aec: bool = True):
self.enable_aec = enable_aec
self.running = True
# Audio processing with echo cancellation
self.audio_processor = apm.AudioProcessingModule(
echo_cancellation=True,
noise_suppression=True,
high_pass_filter=True,
auto_gain_control=True
) if enable_aec else None
# Audio streams
self.input_stream = None
self.output_stream = None
def start_audio_devices(self):
# Start input stream
self.input_stream = sd.InputStream(
callback=self._input_callback,
dtype='int16',
channels=1,
samplerate=48000,
blocksize=4800,
)
self.input_stream.start()
# Start output stream
self.output_stream = sd.OutputStream(
callback=self._output_callback,
dtype='int16',
channels=1,
samplerate=48000,
blocksize=4800,
)
self.output_stream.start()
def _input_callback(self, indata, frames, time, status):
if self.audio_processor:
# Process input audio with echo cancellation
audio_frame = rtc.AudioFrame(
data=indata.tobytes(),
sample_rate=48000,
num_channels=1,
samples_per_channel=frames
)
self.audio_processor.process_stream(audio_frame)
def _output_callback(self, outdata, frames, time, status):
if self.audio_processor:
# Process output audio for echo cancellation
audio_frame = rtc.AudioFrame(
data=outdata.tobytes(),
sample_rate=48000,
num_channels=1,
samples_per_channel=frames
)
self.audio_processor.process_reverse_stream(audio_frame)
This little example shows you how to:
• Set up both input and output audio streams
• Turn on echo cancellation using the AudioProcessingModule
• Handle audio frames for input and output to keep things clear
If anything’s unclear or you’d like me to walk you through more examples, just let me know—I’m here to help!
Sources: README.md | livekit-rtc/livekit/rtc/apm.py | livekit-agents/livekit/agents/voice/chat_cli.pysalmon-elephant-7819
08/21/2025, 5:01 PMtall-airline-16756
08/21/2025, 5:02 PMfrom livekit.agents.voice import AgentSession, room_io
from livekit.plugins import noise_cancellation
session = AgentSession()
await session.start(
agent=agent,
room=ctx.room,
room_input_options=room_io.RoomInputOptions(
noise_cancellation=noise_cancellation.BVC(), # This includes noise cancellation
audio_sample_rate=48000,
audio_num_channels=1,
)
)
2. Using AudioProcessingModule:
from livekit import rtc
# Initialize APM with echo cancellation enabled
apm = rtc.AudioProcessingModule(
echo_cancellation=True,
noise_suppression=True,
high_pass_filter=True,
auto_gain_control=True,
)
3. Just a few things to keep in mind:
◦ Make sure your audio frames are exactly 10ms long so everything processes nicely.
◦ Set the correct stream delay to get the best echo cancellation results.
◦ When testing, using headphones helps avoid any feedback issues.
If you run into any questions or want me to walk you through some examples, just give me a shout—I’m here to help!
Sources: livekit-agents/livekit/agents/voice/room_io/room_io.py | README.md | basics/echo_transcriber_agent.py | livekit-rtc/livekit/rtc/_proto/audio_frame_pb2.pyisalmon-elephant-7819
08/21/2025, 5:04 PMtall-airline-16756
08/21/2025, 5:05 PM