busy-lunch-15701
11/28/2025, 8:32 AMtall-airline-16756
11/28/2025, 8:32 AMtall-airline-16756
11/28/2025, 8:32 AMMediaStreamTrack, and publish it with localParticipant.publishTrack().
Would you like a short example for the server SDK you use (Node/Python/Go) or a browser (WebAudio/React) snippet to get started?
Sources: Camera & microphone | LiveKit docs | Stream music over WebRTC using React and WebAudiobusy-lunch-15701
11/28/2025, 8:33 AMtall-airline-16756
11/28/2025, 8:33 AMAudioSource (e.g., with the Node/Python/Go/Rust SDKs), synthesize PCM frames (sine waves, melodies, synthesized samples) and call the SDK’s capture/send API to stream those frames to a LocalAudioTrack, then publish that track to the room.
• Browser (client) side: synthesize audio via the WebAudio API (OscillatorNode, ScriptProcessor/AudioWorklet, etc.), route the output to a MediaStreamDestination, get the MediaStreamTrack and publish it with localParticipant.publishTrack().
Would you like a short code example for a specific platform (Node, Python, browser/WebAudio)?
Sources: Camera & microphone | LiveKit docsbusy-lunch-15701
11/28/2025, 8:34 AMServer-side SDK / Agent: create an AudioSource (e.g., with the Node/Python/Go/Rust SDKs), synthesize PCM frames (sine waves, melodies, synthesized samples) and call the SDK's capture/send API to stream those frames to a LocalAudioTrack, then publish that track to the room.
show me the python codetall-airline-16756
11/28/2025, 8:34 AMAudioSource and LocalAudioTrack
• Synthesizes a sine-wave tone (and a simple sequence of notes as a melody)
• Sends PCM frames to the source with capture_frame()
• Publishes the track to the room
Adjust sample rate, frame size, and volume as needed.
import asyncio
import math
import numpy as np
from livekit import Room, rtc, ParticipantPermission # adjust imports per your SDK install
# Note: import paths may vary slightly by SDK package version.
LIVEKIT_URL = "https://your-livekit-server"
API_KEY = "your_api_key"
API_SECRET = "your_api_secret"
ROOM_NAME = "example-room"
TOKEN = "participant_jwt_token" # or generate via server auth flow
SAMPLE_RATE = 48000
CHANNELS = 1
FRAME_DURATION_MS = 10 # 10ms frames
SAMPLES_PER_FRAME = SAMPLE_RATE * FRAME_DURATION_MS // 1000
AMP = 0.2 # volume (0.0 - 1.0)
def generate_sine_frame(frequency: float, phase: float) -> (bytes, float):
t = (np.arange(SAMPLES_PER_FRAME) + phase) / SAMPLE_RATE
wave = AMP * np.sin(2 * math.pi * frequency * t)
pcm = (wave * 32767).astype(np.int16)
return pcm.tobytes(), (phase + SAMPLES_PER_FRAME) % SAMPLE_RATE
async def publish_generated_audio():
# Connect to LiveKit room (adjust to match your SDK constructor)
room = Room(url=LIVEKIT_URL, token=TOKEN) # replace with the correct connection call
await room.connect()
# Create audio source & local track
source = rtc.AudioSource(SAMPLE_RATE, CHANNELS)
track = rtc.LocalAudioTrack.create_audio_track("generated-tone", source)
await room.local_participant.publish_track(track)
try:
# Example: play a simple melody (sequence of frequencies, each for 0.5s)
melody = [440.0, 494.0, 523.25, 587.33] # A4, B4, C5, D5
note_duration_s = 0.5
samples_per_note = int(SAMPLE_RATE * note_duration_s)
phase = 0
for freq in melody:
frames_to_send = samples_per_note // SAMPLES_PER_FRAME
for _ in range(frames_to_send):
frame_bytes, phase = generate_sine_frame(freq, phase)
# build AudioFrame - adjust constructor to your SDK's AudioFrame API
frame = rtc.AudioFrame(
sample_rate=SAMPLE_RATE,
num_channels=CHANNELS,
samples_per_channel=SAMPLES_PER_FRAME,
data=frame_bytes
)
await source.capture_frame(frame)
await asyncio.sleep(FRAME_DURATION_MS / 1000.0)
# Keep running if you want continuous tone; here we stop after melody
finally:
await room.disconnect()
if __name__ == "__main__":
asyncio.run(publish_generated_audio())
Notes:
• Replace connection/token code to match your LiveKit Python SDK usage (constructor names may vary).
• AudioFrame and AudioSource constructors/parameters should match the SDK version you use; the snippet shows the typical fields: sample_rate, num_channels, samples_per_channel, and raw data.
• For continuous tones, run a loop that repeatedly generates frames for the chosen frequency(s).
• For more advanced synthesis, use oscillators, envelopes, or external libraries (NumPy, scipy) to synthesize waveforms.
Would you like this adapted to your exact SDK version or a Node/Go example as well?busy-lunch-15701
11/28/2025, 8:38 AMtall-airline-16756
11/28/2025, 8:38 AMimport asyncio
import math
import numpy as np
from livekit import rtc, Room # adjust imports if your SDK package differs
LIVEKIT_URL = "https://your-livekit-server"
TOKEN = "YOUR_TOKEN"
SAMPLE_RATE = 48000
CHANNELS = 1
FRAME_MS = 10
SAMPLES_PER_FRAME = SAMPLE_RATE * FRAME_MS // 1000
VOLUME = 0.2
def sine_frame(frequency, phase):
t = (np.arange(SAMPLES_PER_FRAME) + phase) / SAMPLE_RATE
wave = VOLUME * np.sin(2 * math.pi * frequency * t)
pcm = (wave * 32767).astype(np.int16)
return pcm.tobytes(), (phase + SAMPLES_PER_FRAME) % SAMPLE_RATE
async def play_melody():
room = Room(url=LIVEKIT_URL, token=TOKEN) # replace with your connect call
await room.connect()
source = rtc.AudioSource(SAMPLE_RATE, CHANNELS)
track = rtc.LocalAudioTrack.create_audio_track("tone", source)
await room.local_participant.publish_track(track)
try:
melody = [440.0, 494.0, 523.25] # A4, B4, C5
note_ms = 500
frames_per_note = (note_ms // FRAME_MS)
for freq in melody:
phase = 0
for _ in range(frames_per_note):
data, phase = sine_frame(freq, phase)
frame = rtc.AudioFrame(
sample_rate=SAMPLE_RATE,
num_channels=CHANNELS,
samples_per_channel=SAMPLES_PER_FRAME,
data=data
)
await source.capture_frame(frame)
await asyncio.sleep(FRAME_MS / 1000.0)
finally:
await room.disconnect()
if __name__ == "__main__":
asyncio.run(play_melody())
Would you like a version that plays the three notes in a loop or different note durations?busy-lunch-15701
11/28/2025, 8:40 AMtall-airline-16756
11/28/2025, 8:40 AMAudioSource published as a LocalAudioTrack. Below is a very minimal LiveKit agent-tool Python snippet that plays three notes (A4, B4, C5) and returns immediately after playing them.
Replace imports/connect code to match your agent runtime.
import asyncio
import math
import numpy as np
from livekit import rtc
from livekit.agents import function_tool, RunContext
SAMPLE_RATE = 48000
CHANNELS = 1
FRAME_MS = 10
SAMPLES_PER_FRAME = SAMPLE_RATE * FRAME_MS // 1000
VOLUME = 0.2
def make_frame(freq, phase):
t = (np.arange(SAMPLES_PER_FRAME) + phase) / SAMPLE_RATE
wave = VOLUME * np.sin(2 * math.pi * freq * t)
pcm = (wave * 32767).astype(np.int16)
return pcm.tobytes(), (phase + SAMPLES_PER_FRAME) % SAMPLE_RATE
@function_tool()
async def play_three_notes(self, ctx: RunContext):
# create source/track and publish to the room participant
source = rtc.AudioSource(SAMPLE_RATE, CHANNELS)
track = rtc.LocalAudioTrack.create_audio_track("melody", source)
await ctx.room.local_participant.publish_track(track)
try:
notes = [440.0, 494.0, 523.25] # A4, B4, C5
note_ms = 400
frames_per_note = note_ms // FRAME_MS
for freq in notes:
phase = 0
for _ in range(frames_per_note):
data, phase = make_frame(freq, phase)
frame = rtc.AudioFrame(
data=data,
sample_rate=SAMPLE_RATE,
num_channels=CHANNELS,
samples_per_channel=SAMPLES_PER_FRAME,
)
await source.capture_frame(frame)
await asyncio.sleep(FRAME_MS / 1000.0)
finally:
await source.close()
If you want it even simpler, I can:
• provide a version that loops the three notes continuously, or
• give a snippet that uses fewer dependencies (no NumPy) — which is slightly longer but pure Python. Which would you prefer? Sources: Agent speech and audio | LiveKit docs | External data and RAG | LiveKit Docs | Camera & microphone | LiveKit docs | Job lifecycle | LiveKit docs