How can I play a looping sound while my agent is w...
# ask-ai
b
How can I play a looping sound while my agent is working?
g
To play a looping sound while your agent is working, you can publish a separate audio track dedicated to the looping sound. This approach allows you to control the playback independently from the main audio track used by the agent. According to LiveKit documentation, you can publish an audio track by creating a
LocalAudioTrack
and using the
publish_track
method. Here's a basic outline of the steps: 1. Create an Audio Source: Prepare your audio data, ensuring it is in the correct format (e.g.,
int16
for LiveKit). 2. Publish the Track: Use the
publish_track
method to publish the audio track to the room. This track will be used to play your looping sound. 3. Control Playback: You can programmatically control the playback, such as starting, stopping, or looping the sound, by managing the audio frames you send to the track. Would you like more detailed guidance or a code example? Sources: - https://livekit-users.slack.com/archives/C07FY8WHGPM/p1731400051.109939 - https://livekit-users.slack.com/archives/C07FY8WHGPM/p1730699560.810679 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
b
@millions-hair-24956 A code example would be great.
g
To play a looping sound using LiveKit, you can publish a separate audio track and continuously send audio frames to it. Here's a Python example using the LiveKit SDK:
Copy code
import asyncio
import numpy as np
from livekit import rtc

async def publish_looping_audio(room, audio_source, sample_rate, num_channels, audio_data):
    track = rtc.LocalAudioTrack.create_audio_track("looping_sound", audio_source)
    await room.local_participant.publish_track(track)

    samples_per_channel = len(audio_data) // num_channels
    while True:
        for start in range(0, len(audio_data), samples_per_channel):
            end = start + samples_per_channel
            frame_data = audio_data[start:end].tobytes()
            audio_frame = rtc.AudioFrame(
                data=frame_data,
                sample_rate=sample_rate,
                num_channels=num_channels,
                samples_per_channel=samples_per_channel
            )
            await audio_source.capture_frame(audio_frame)
            await asyncio.sleep(samples_per_channel / sample_rate)

async def main():
    # Connect to the room
    room = rtc.Room()
    await room.connect("LIVEKIT_URL", "PARTICIPANT_TOKEN")

    # Prepare audio data
    sample_rate = 48000
    num_channels = 1
    # Example: Generate a sine wave
    duration = 1  # seconds
    frequency = 440  # Hz
    t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
    audio_data = (np.sin(2 * np.pi * frequency * t) * 32767).astype(np.int16)

    audio_source = rtc.AudioSource(sample_rate, num_channels)
    await publish_looping_audio(room, audio_source, sample_rate, num_channels, audio_data)

if __name__ == '__main__':
    asyncio.run(main())
This example creates a sine wave and continuously sends it as audio frames to the LiveKit room. Adjust the
audio_data
to use your desired sound. Let me know if you need further assistance! Sources: - https://docs.livekit.io/agents/build/tracks/#Publishing-audio 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.