This commit is contained in:
yumoqing 2024-09-13 11:55:51 +08:00
parent 3ea9187d93
commit bd8aefd87a

View File

@ -50,8 +50,8 @@ class AudioTrackVad(MediaStreamTrack):
oldf = await self.track.recv() oldf = await self.track.recv()
self.sample_rate = oldf.sample_rate self.sample_rate = oldf.sample_rate
duration = (oldf.samples * 1000) / oldf.sample_rate duration = (oldf.samples * 1000) / oldf.sample_rate
print(f'{self.__class__.__name__}.recv(): {duration=}, {oldf.samples=}, {oldf.sample_rate=}') # print(f'{self.__class__.__name__}.recv(): {duration=}, {oldf.samples=}, {oldf.sample_rate=}')
frames = self.resample(oldf) try:
for f in frames: for f in frames:
if self.debug: if self.debug:
self.debug = False self.debug = False
@ -75,10 +75,13 @@ class AudioTrackVad(MediaStreamTrack):
frame = r.resample(frame) frame = r.resample(frame)
return frame return frame
async def vad_check(self, frame): async def vad_check(self, inframe):
is_speech = self.vad.is_speech(self.frame2bytes(frame), self.sample_rate) frames = self.resample(inframe)
frame = frames[0]:
is_speech = self.vad.is_speech(self.frame2bytes(frame),
self.sample_rate)
if not self.triggered: if not self.triggered:
self.ring_buffer.append((frame, is_speech)) self.ring_buffer.append((inframe, is_speech))
num_voiced = len([f for f, speech in self.ring_buffer if speech]) num_voiced = len([f for f, speech in self.ring_buffer if speech])
# If we're NOTTRIGGERED and more than 90% of the frames in # If we're NOTTRIGGERED and more than 90% of the frames in
# the ring buffer are voiced frames, then enter the # the ring buffer are voiced frames, then enter the
@ -95,7 +98,7 @@ class AudioTrackVad(MediaStreamTrack):
else: else:
# We're in the TRIGGERED state, so collect the audio data # We're in the TRIGGERED state, so collect the audio data
# and add it to the ring buffer. # and add it to the ring buffer.
self.voiced_frames.append(frame) self.voiced_frames.append(inframe)
self.ring_buffer.append((frame, is_speech)) self.ring_buffer.append((frame, is_speech))
num_unvoiced = len([f for f, speech in self.ring_buffer if not speech]) num_unvoiced = len([f for f, speech in self.ring_buffer if not speech])
# If more than 90% of the frames in the ring buffer are # If more than 90% of the frames in the ring buffer are