This commit is contained in:
yumoqing 2024-09-18 16:28:00 +08:00
parent 73d12cba4a
commit 4944df6e9d

View File

@ -27,22 +27,6 @@ def frames_write_wave(frames):
output_container.close() output_container.close()
return path 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): def bytes2frame(byts, channels=1, sample_rate=16000):
audio_data = np.frombuffer(byts, np.int16) audio_data = np.frombuffer(byts, np.int16)
audio_data = audio_data.reshape((channels, -1)) audio_data = audio_data.reshape((channels, -1))
@ -162,13 +146,28 @@ class AudioTrackVad(MediaStreamTrack):
def stop(self): def stop(self):
self.running = False 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): 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 = to16000_160_frames(frame) frames = self.to16000_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: