This commit is contained in:
yumoqing 2024-09-18 15:55:15 +08:00
parent 865543cf56
commit 515b107d86

View File

@ -157,28 +157,13 @@ class AudioTrackVad(MediaStreamTrack):
def stop(self): def stop(self):
self.running = False self.running = False
def to16000_160_frames(self, 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
async def recv(self): async def recv(self):
frame = await self.track.recv() frame = await self.track.recv()
self.sample_rate = frame.sample_rate self.sample_rate = frame.sample_rate
duration = (frame.samples * 1000) / frame.sample_rate duration = (frame.samples * 1000) / frame.sample_rate
# print(f'{self.__class__.__name__}.recv(): {duration=}, {frame.samples=}, {frame.sample_rate=}') # print(f'{self.__class__.__name__}.recv(): {duration=}, {frame.samples=}, {frame.sample_rate=}')
try: try:
frames = self.to16000_160_frames(frame) frames = o16000_160_frames(frame)
for frame in frames: for frame in frames:
await self.vad.vad_check(frame) await self.vad.vad_check(frame)
except Exception as e: except Exception as e: