icy-country-64454
05/12/2025, 12:49 PMtall-airline-16756
05/12/2025, 12:49 PMtall-airline-16756
05/12/2025, 12:49 PMicy-country-64454
05/12/2025, 12:51 PMtall-airline-16756
05/12/2025, 12:52 PMimport numpy as np
from livekit import agents, rtc
SAMPLE_RATE = 48000
NUM_CHANNELS = 1 # mono audio
AMPLITUDE = 2 ** 8 - 1
SAMPLES_PER_CHANNEL = 480 # 10 ms at 48kHz
async def entrypoint(ctx: agents.JobContext):
await ctx.connect()
# Create audio source and track
source = rtc.AudioSource(SAMPLE_RATE, NUM_CHANNELS)
track = rtc.LocalAudioTrack.create_audio_track("example-track", source)
# Set options for publishing
options = rtc.TrackPublishOptions(source=rtc.TrackSource.SOURCE_MICROPHONE)
# Publish the track
publication = await ctx.agent.publish_track(track, options)
# Example of sending audio frames (sine wave)
frequency = 440
async def _sinewave():
audio_frame = rtc.AudioFrame.create(SAMPLE_RATE, NUM_CHANNELS, SAMPLES_PER_CHANNEL)
audio_data = np.frombuffer(audio_frame.data, dtype=np.int16)
total_samples = 0
while True:
time = (total_samples + np.arange(SAMPLES_PER_CHANNEL)) / SAMPLE_RATE
sinewave = (AMPLITUDE * np.sin(2 * np.pi * frequency * time)).astype(np.int16)
np.copyto(audio_data, sinewave)
# Send this frame to the track
await source.capture_frame(audio_frame)
total_samples += SAMPLES_PER_CHANNEL
await _sinewave()
This script creates an audio source, generates a sine wave, and publishes the audio track to the room. Let me know if you need further assistance! Sources: Publishing tracks | LiveKit Docs | Audio and video | LiveKit Docs