i want to stream the audio of different candidates...
# ask-ai
n
i want to stream the audio of different candidates to BE with a single sample rate. how to do this?? when i create an AudioContext with a constant sample rate, im getting sampleRate mismatch error in firefox. I can use native sample rate, but that also changes when the input device changes. So how to ensure that i get the audio with a fixed sample rate even when the input device changes.
t
Thanks for reaching out, Rajat. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks so much for reaching out! To better assist you, could you kindly share a bit more about your setup? For instance: • Are you using LiveKit's SDK for audio streaming? • What sample rate are you hoping to enforce? • Are you doing any audio processing on the client-side before sending it to the backend? Getting these details will really help me guide you toward the best solution. Looking forward to your reply!
n
• im capturing the audio using
getTrackPublications()
and streaming it to BE. • any sample rate between 16k to 48k is fine, provided it remains constant through out the session. • no addl processing is done in the client side
t
I'm glad you're looking to maintain a constant sample rate for audio streaming using
getTrackPublications()
. Let me walk you through the steps to make this as smooth as possible: 1. Start by using
getTrackPublications()
to find and access your audio tracks. You'll want to filter these to get the microphone (audio) tracks specifically. 2. Next, create an
AudioStream
from the track, then use the
resampleStream()
utility to ensure a consistent output sample rate. Here's a simple example to guide you:
Copy code
import { resampleStream } from '@livekit/agents';

const audioStream = new AudioStream(track);
const resampledStream = resampleStream({
  stream: audioStream,
  outputRate: 16000 // Set your desired constant sample rate
});

for await (const event of resampledStream) {
  const frame = event.frame;
  console.log(`Frame sample rate: ${frame.sampleRate}`); // Will always be 16000
}
1. Don't forget to handle track lifecycle events like
track_subscribed
, so you can dynamically apply resampling whenever new tracks are published. By following these steps, you'll help ensure your audio stream keeps a steady sample rate—like 16kHz—throughout your session, no matter what input device is used. If you have any questions or need further assistance, feel free to ask—I'm here to help! Sources: Receiving and publishing tracks | LiveKit Docs | Speech & Audio Processing | livekit/livekit_composite | DeepWiki | livekit-agents/livekit/agents/voice/room_io/_output.py | Runtime/Scripts/Room/Participant/Participant.cs | packages/core/src/components/mediaTrack.ts
n
resampling is causing significant quality issues. its not clear when listening to the resampled audio. let be upsampling or downsampling
t
I understand that resampling can sometimes lead to quality issues, especially if the default settings don’t quite meet your needs. To help improve audio quality during resampling, you have the option to adjust the resampling quality setting in the underlying
AudioResampler
. Choosing higher quality settings like
HIGH
or
VERY_HIGH
can give you better audio fidelity, though they do require a bit more processing power. Right now, the
resampleStream
function in the LiveKit SDK doesn’t offer a quality parameter, so it defaults to
MEDIUM
quality. Here are a couple of ways you might approach this: 1. You could modify the
resampleStream
function to accept a quality parameter and then pass that along to the
AudioResampler
constructor. 2. Alternatively, you might consider doing the resampling outside of LiveKit using tools like
soxr
or
pydub
, which give you explicit control over the resampling quality. If you’d like, I’m more than happy to guide you through implementing a custom
resampleStream
function with configurable quality—just let me know, and I can provide more details! Sources: packages/livekit-rtc/src/audio_resampler.ts | agents/src/utils.test.ts | livekit-rtc/tests/test_resampler.py | livekit-ffi/protocol/audio_frame.proto
n
i need someway to enforce livekit to use a given sample rate.
t
I understand you're looking to enforce a specific sample rate without resampling in LiveKit. While LiveKit doesn’t offer a direct setting for this, the best way to ensure a fixed sample rate throughout your session is to have your audio source operate at the desired sample rate from the start (like 16kHz or 48kHz). If you have control over the audio source, setting it to use the target sample rate natively will help avoid any internal resampling in LiveKit’s media processing pipeline. Please feel free to reach out if you’d like some help configuring your audio source or if you have any other questions—I’m here to assist you! Sources: README.md | packages/livekit-rtc/src/audio_stream.ts | webrtc-sys/include/livekit/audio_track.h