how ot stream audio from wave file
# ask-ai
q
how ot stream audio from wave file
g
To stream audio from a WAV file using LiveKit, you can follow these steps: 1. Prepare the Audio Source: Convert the WAV file into a format suitable for streaming. This involves reading the WAV file and extracting PCM data. 2. Create an Audio Source and Track: Use LiveKit's
rtc.AudioSource
and
rtc.LocalAudioTrack
to create an audio track from the PCM data. 3. Publish the Track: Connect to a LiveKit room and publish the audio track. Here's a basic example in Python:
Copy code
import wave
import numpy as np
from livekit import rtc

# Open the WAV file
with wave.open("your_audio_file.wav", "rb") as wav_file:
    sample_rate = wav_file.getframerate()
    num_channels = wav_file.getnchannels()
    frames = wav_file.readframes(wav_file.getnframes())
    pcm_data = np.frombuffer(frames, dtype=np.int16)

# Create an audio source and track
source = rtc.AudioSource(sample_rate, num_channels)
track = rtc.LocalAudioTrack.create_audio_track("audio", source)

# Connect to the room and publish the track
async def publish_audio():
    room = rtc.Room()
    await room.connect("your_livekit_url", "your_participant_token")
    await room.local_participant.publish_track(track)

    # Stream the audio
    frame_duration_ms = 20
    samples_per_frame = int(sample_rate * frame_duration_ms / 1000)
    for start in range(0, len(pcm_data), samples_per_frame):
        end = start + samples_per_frame
        frame_data = pcm_data[start:end].tobytes()
        samples_per_channel = len(frame_data) // (num_channels * 2)
        if samples_per_channel == 0:
            continue
        audio_frame = rtc.AudioFrame(
            data=frame_data,
            sample_rate=sample_rate,
            num_channels=num_channels,
            samples_per_channel=samples_per_channel
        )
        await source.capture_frame(audio_frame)
        await asyncio.sleep(frame_duration_ms / 1000)

# Run the publish function
asyncio.run(publish_audio())
This code reads a WAV file, creates an audio track, and streams it to a LiveKit room. Would you like more details on any specific part of this process? Sources: - LiveKit Slack Discussion 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.