transports(tk): fix pyaudio output stream cleanup

This commit is contained in:
Aleix Conchillo Flaqué
2024-06-04 12:11:07 -07:00
parent 5cdb8a79a1
commit 4057fbbcfd
2 changed files with 4 additions and 28 deletions

View File

@@ -29,7 +29,7 @@ class LocalAudioInputTransport(BaseInputTransport):
super().__init__(params)
sample_rate = self._params.audio_in_sample_rate
num_frames = int(sample_rate / 100) # 10ms of audio
num_frames = int(sample_rate / 100) * 2 # 20ms of audio
self._in_stream = py_audio.open(
format=py_audio.get_format_from_width(2),
@@ -81,21 +81,9 @@ class LocalAudioOutputTransport(BaseOutputTransport):
def write_raw_audio_frames(self, frames: bytes):
self._out_stream.write(frames)
async def start(self, frame: StartFrame):
await super().start(frame)
self._out_stream.start_stream()
async def stop(self):
await super().stop()
self._out_stream.stop_stream()
async def cleanup(self):
# This is not very pretty (taken from PyAudio docs).
while self._out_stream.is_active():
await asyncio.sleep(0.1)
self._out_stream.close()
await super().cleanup()
self._out_stream.close()
class LocalAudioTransport(BaseTransport):

View File

@@ -39,7 +39,7 @@ class TkInputTransport(BaseInputTransport):
super().__init__(params)
sample_rate = self._params.audio_in_sample_rate
num_frames = int(sample_rate / 100) # 10ms of audio
num_frames = int(sample_rate / 100) * 2 # 20ms of audio
self._in_stream = py_audio.open(
format=py_audio.get_format_from_width(2),
@@ -100,21 +100,9 @@ class TkOutputTransport(BaseOutputTransport):
def write_frame_to_camera(self, frame: ImageRawFrame):
self.get_event_loop().call_soon(self._write_frame_to_tk, frame)
async def start(self, frame: StartFrame):
await super().start(frame)
self._out_stream.start_stream()
async def stop(self):
await super().stop()
self._out_stream.stop_stream()
async def cleanup(self):
# This is not very pretty (taken from PyAudio docs).
while self._out_stream.is_active():
await asyncio.sleep(0.1)
self._out_stream.close()
await super().cleanup()
self._out_stream.close()
def _write_frame_to_tk(self, frame: ImageRawFrame):
width = frame.size[0]