1010BLOCKSIZE = 480 # 10ms chunks at 48kHz
1111CHANNELS = 1
1212
13+
1314class AudioBuffer :
1415 def __init__ (self , blocksize = BLOCKSIZE ):
1516 self .blocksize = blocksize
@@ -20,8 +21,8 @@ def add_frame(self, frame_data):
2021
2122 def get_chunk (self ):
2223 if len (self .buffer ) >= self .blocksize :
23- chunk = self .buffer [:self .blocksize ]
24- self .buffer = self .buffer [self .blocksize :]
24+ chunk = self .buffer [: self .blocksize ]
25+ self .buffer = self .buffer [self .blocksize :]
2526 return chunk
2627 return None
2728
@@ -34,6 +35,7 @@ def get_padded_chunk(self):
3435 return chunk
3536 return np .zeros (self .blocksize , dtype = np .int16 )
3637
38+
3739async def audio_player (queue : asyncio .Queue ):
3840 """Pull from the queue and stream audio using sounddevice."""
3941 buffer = AudioBuffer (BLOCKSIZE )
@@ -62,14 +64,15 @@ def callback(outdata, frames, time, status):
6264 samplerate = SAMPLERATE ,
6365 channels = CHANNELS ,
6466 blocksize = BLOCKSIZE ,
65- dtype = ' int16' ,
67+ dtype = " int16" ,
6668 callback = callback ,
67- latency = ' low'
69+ latency = " low" ,
6870 )
6971 with stream :
7072 while True :
7173 await asyncio .sleep (0.1 ) # keep the loop alive
7274
75+
7376async def rtc_session (room , queue : asyncio .Queue ):
7477 track : rtc .RemoteAudioTrack | None = None
7578 while not track :
@@ -88,7 +91,7 @@ async def rtc_session(room, queue: asyncio.Queue):
8891 track = track ,
8992 sample_rate = SAMPLERATE ,
9093 num_channels = 1 ,
91- noise_cancellation = noise_cancellation .BVC (), # or NC()
94+ noise_cancellation = noise_cancellation .BVC (), # or NC()
9295 )
9396
9497 print ("playing stream" )
@@ -110,6 +113,7 @@ async def rtc_session(room, queue: asyncio.Queue):
110113 # Clean up the stream when done
111114 await stream .aclose ()
112115
116+
113117async def main ():
114118 queue = asyncio .Queue (maxsize = 50 )
115119 player_task = asyncio .create_task (audio_player (queue ))
@@ -150,4 +154,5 @@ async def main():
150154 except asyncio .CancelledError :
151155 pass
152156
157+
153158asyncio .run (main ())
0 commit comments