from daily import Daily, CallClient, CustomAudioSource, CustomAudioTrack
import wave
import threading
import time
class AudioStreamer:
def __init__(self, audio_source: CustomAudioSource, audio_file: str):
self.audio_source = audio_source
self.audio_file = audio_file
self.running = False
self.thread = None
def start(self):
self.running = True
self.thread = threading.Thread(target=self._stream_audio)
self.thread.start()
def stop(self):
self.running = False
if self.thread:
self.thread.join()
def _stream_audio(self):
with wave.open(self.audio_file, 'rb') as wav_file:
chunk_size = 320 # 20ms at 16kHz
while self.running:
frames = wav_file.readframes(chunk_size)
if not frames:
# Loop audio
wav_file.rewind()
continue
self.audio_source.write_frames(frames)
time.sleep(0.02)
# Usage
Daily.init()
client = CallClient()
client.join("https://your-domain.daily.co/room-name")
# Set up custom audio
audio_source = CustomAudioSource(sample_rate=16000, channels=1)
audio_track = CustomAudioTrack(audio_source)
client.add_custom_audio_track("background-music", audio_track)
# Start streaming
streamer = AudioStreamer(audio_source, "background.wav")
streamer.start()
# Do other work...
time.sleep(60)
# Stop streaming
streamer.stop()
client.leave()
Daily.deinit()