I am implementing key note translation experience ...
# ask-ai
s
I am implementing key note translation experience where one user will continue speaking and other participants will listen translation in taget language. I was enabling parallel input audio and output audio so that I can have continues conversation. How can I enable that?
t
Thanks for reaching out, Ankit. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! To get parallel input and output audio working smoothly for continuous conversation in LiveKit, here’s a simple rundown you can follow: 1. Enable Audio Input and Output: Make sure your session is set up to handle both audio input and output streams. Don’t forget to pick the right sample rate and number of channels that fit your needs. 2. Implement Echo Cancellation: It’s super helpful to use an Audio Processing Module that includes echo cancellation, noise suppression, high-pass filtering, and auto-gain control—this way, you avoid those annoying feedback loops. 3. Dynamic Audio Control: You’ve got the flexibility to turn audio input and output on or off during the session using methods like
set_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 Docs
s
Yes please share me example code to implement this.
t
Here's a friendly example to help you get started with enabling parallel input and output audio along with echo cancellation in LiveKit:
Copy code
import 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.py
s
How can I enable this through AgentSession and livekit UI?
t
Hey there! To get parallel input and output audio working smoothly with echo cancellation using AgentSession and LiveKit UI, here’s a simple step-by-step guide for you: 1. Using AgentSession:
Copy code
from 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:
Copy code
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.pyi
s
can you give me full code for same?
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!