powerful-dog-59275
12/02/2025, 3:00 PMtall-airline-16756
12/02/2025, 3:00 PMtall-airline-16756
12/02/2025, 3:00 PMfrom livekit.agents import BackgroundAudioPlayer, AudioConfig, BuiltinAudioClip
background_audio = BackgroundAudioPlayer(
thinking_sound=[
AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8),
AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING2, volume=0.7),
],
)
await background_audio.start(room=ctx.room, agent_session=session)
This supports built-in and custom clips in Python; thinking sounds are not yet supported in Node.js. Would you like an example using a custom audio clip or steps to integrate this into your agent flow?
Sources: Agent speech and audio | LiveKit docs | External data and RAG | LiveKit Docspowerful-dog-59275
12/02/2025, 3:01 PMtall-airline-16756
12/02/2025, 3:01 PMcuddly-baker-42941
12/05/2025, 7:32 PMtall-airline-16756
12/05/2025, 7:32 PMcuddly-baker-42941
12/05/2025, 7:40 PMtall-airline-16756
12/05/2025, 7:40 PMcuddly-baker-42941
12/05/2025, 7:40 PMtall-airline-16756
12/05/2025, 7:41 PM// Dependencies
const { Room, LocalParticipant, createLocalTracks, LocalAudioTrack } = require('livekit-server-sdk'); // adjust import to actual SDK exports
const { spawn } = require('child_process');
const fs = require('fs');
// Helper: spawn ffmpeg to output signed 16-bit little-endian PCM, 48kHz mono
function spawnFfmpegMp3ToPcmStream(mp3Path) {
const args = [
'-re', // read at native rate (helpful for streaming files)
'-i', mp3Path,
'-ar', '48000', // sample rate 48kHz
'-ac', '1', // mono
'-f', 's16le', // signed 16-bit little-endian
'pipe:1'
];
return spawn('ffmpeg', args, { stdio: ['ignore', 'pipe', 'inherit'] });
}
// Pseudocode: connect a bot participant to room and publish audio from PCM stream
async function publishThinkingAudioToRoom(lkServerUrl, apiKey, apiSecret, roomName, botName, mp3Path) {
// 1) Create a grant/token for bot to join (implementation depends on your auth flow).
// Example: generate an Access Token with appropriate room grants (not shown).
const accessToken = generateAccessTokenForBot(apiKey, apiSecret, roomName, botName);
// 2) Connect the bot to the LiveKit room as a participant.
// Use a WebRTC-capable client or the server SDK that supports publishing from raw audio.
// Many users create a short-lived headless WebRTC client that publishes the track.
const botConnection = await connectBotClient(lkServerUrl, accessToken); // implement per your client
// 3) Spawn ffmpeg
const ff = spawnFfmpegMp3ToPcmStream(mp3Path);
const pcmStream = ff.stdout;
// 4) Create a LocalAudioTrack from the PCM stream and publish.
// Exact API varies by SDK. Conceptually:
const audioTrack = await createAudioTrackFromPcmStream(pcmStream, {
sampleRate: 48000,
channels: 1,
format: 's16'
});
const pub = await botConnection.publishTrack(audioTrack, { name: 'thinking-sfx', muted: false });
return { botConnection, audioTrack, ff, pub };
}
// Stop and cleanup
async function stopThinkingAudio(publishHandle) {
try {
await publishHandle.audioTrack.stop();
await publishHandle.botConnection.unpublishTrack(publishHandle.pub);
publishHandle.ff.kill('SIGINT');
await publishHandle.botConnection.disconnect();
} catch (e) {
console.error('cleanup error', e);
}
}
Key implementation notes
• You need a bot participant that can publish an audio track into the room. This is typically done by spawning a headless WebRTC client on the server that uses the generated access token.
• The LiveKit client libraries expect audio input as a MediaStream / track. Converting ffmpeg PCM output into a publishable LocalAudioTrack requires a bridge that creates a MediaStreamTrack from raw PCM—some libraries provide utilities for that; otherwise, create a headless WebRTC PeerConnection and feed PCM frames into an AudioWorklet or use node-webrtc to create a MediaStreamTrack.
• Using -re with ffmpeg streams the file in real time. For looped playback, restart ffmpeg or use ffmpeg looping options.
• Ensure you stop/unpublish the track promptly when thinking ends to avoid orphan audio.
If you'd like, I can:
• Provide a more concrete example using node-webrtc to feed ffmpeg PCM into a MediaStreamTrack and join LiveKit as a bot, or
• Show how to generate the LiveKit access token for the bot participant.
Which follow-up would you prefer?