How can I stream a custom audio?
# ask-ai
a
How can I stream a custom audio?
t
Thanks for reaching out, Aditya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Which development platform are you using to stream custom audio—Node.js/TypeScript, Python, Rust, or a web browser? Are you looking to stream from a file, generate audio in real time, or use another method? Sources: Working with plugins | LiveKit Docs | livekit-agents/livekit/agents/voice/avatar/_datastream_io.py | Speech & Audio Processing | livekit/livekit_composite | DeepWiki | examples/receive-audio/index.ts | examples/publish_wave.py | examples/play_from_disk/src/main.rs | Stream music over WebRTC using React and WebAudio
a
Python
t
Hey there! Want to stream some custom audio in Python using LiveKit? No worries, I'll walk you through it like we're solving this together. It's actually pretty straightforward! 1. Create an AudioSource and Track: Just pick your sample rate and channels. I'll show you how:
Copy code
import asyncio
import numpy as np
from livekit import rtc

SAMPLE_RATE = 48000
NUM_CHANNELS = 1

source = rtc.AudioSource(SAMPLE_RATE, NUM_CHANNELS)
track = rtc.LocalAudioTrack.create_audio_track("my_custom_audio", source)
options = rtc.TrackPublishOptions()
options.source = rtc.TrackSource.SOURCE_MICROPHONE
publication = await room.local_participant.publish_track(track, options)
2. Generate and Send Audio Frames: Now let's populate an
AudioFrame
with your awesome audio data and stream it away!
Copy code
samples_per_channel = 480  # Adjust as needed

for _ in range(num_frames):
    audio_frame = rtc.AudioFrame.create(SAMPLE_RATE, NUM_CHANNELS, samples_per_channel)
    audio_data = np.frombuffer(audio_frame.data, dtype=np.int16)
    # Fill audio_data with your custom signal (e.g., sine wave, recorded samples)
    await source.capture_frame(audio_frame)
Quick Pro Tips: • Use 48kHz for crystal-clear sound, or 16kHz if you're just chatting. • Keep your frame timing consistent for super smooth playback. • Aim for queue sizes between 1000-2000ms to avoid any hiccups. Got any questions or need help generating those audio frames? I'm right here - just ask, and we'll figure it out together!