From 4944df6e9d90b22d9dbdbf844c9e0187286e6253 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 18 Sep 2024 16:28:00 +0800 Subject: [PATCH] bugfix --- rtcllm/vad.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/rtcllm/vad.py b/rtcllm/vad.py index c85d2b8..e381a28 100644 --- a/rtcllm/vad.py +++ b/rtcllm/vad.py @@ -27,22 +27,6 @@ def frames_write_wave(frames): output_container.close() return path -def to16000_160_frames(frame): - remind_byts = b'' - frames = resample(frame, sample_rate=16000) - ret_frames = [] - for f in frames: - if f.samples == 160: - return frames - for f in frames: - b1 = remind_byts + frame2bytes(f) - while len(b1) >= 320: - b = b1[:320] - b1 = b1[320:] - ret_frames.append(bytes2frame(b)) - remind_byts = b1 - return ret_frames - def bytes2frame(byts, channels=1, sample_rate=16000): audio_data = np.frombuffer(byts, np.int16) audio_data = audio_data.reshape((channels, -1)) @@ -162,13 +146,28 @@ class AudioTrackVad(MediaStreamTrack): def stop(self): self.running = False + def to16000_160_frames(frame): + frames = resample(frame, sample_rate=16000) + ret_frames = [] + for f in frames: + if f.samples == 160: + return frames + for f in frames: + b1 = self.remind_byts + frame2bytes(f) + while len(b1) >= 320: + b = b1[:320] + b1 = b1[320:] + ret_frames.append(bytes2frame(b)) + self.remind_byts = b1 + return ret_frames + async def recv(self): frame = await self.track.recv() self.sample_rate = frame.sample_rate duration = (frame.samples * 1000) / frame.sample_rate # print(f'{self.__class__.__name__}.recv(): {duration=}, {frame.samples=}, {frame.sample_rate=}') try: - frames = to16000_160_frames(frame) + frames = self.to16000_160_frames(frame) for frame in frames: await self.vad.vad_check(frame) except Exception as e: