diff --git a/rtcllm/vad.py b/rtcllm/vad.py index 415a654..57b26d9 100644 --- a/rtcllm/vad.py +++ b/rtcllm/vad.py @@ -8,6 +8,7 @@ from aiortc import MediaStreamTrack from aiortc.contrib.media import MediaBlackhole, MediaPlayer, MediaRecorder, MediaRelay import webrtcvad import wave +from scipy.io.wavfile import write import numpy as np from av import AudioLayout, AudioResampler, AudioFrame, AudioFormat @@ -130,6 +131,19 @@ class AudioTrackVad(MediaStreamTrack): duration = f.samples * 1000 / f.sample_rate + duration return duration + def frames_resample(self, frames, sr=None): + fs = [] + for f in frames: + fs1 = self.resample(f, sample_rate=sr) + fs += fs1 + return fs + + def frames_write_wave(self, frames): + ndarr = np.frombuffer(b''.join(frames), dtype=np.int16) + fn = temp_file(suffix='.wav') + write(fn, frames[0].sample_rate, ndarr) + return fn + async def write_wave(self): """Writes a .wav file. @@ -137,6 +151,9 @@ class AudioTrackVad(MediaStreamTrack): """ """ + ############ + # Method:1 + ############ audio_data = self.to_mono16000_data() path = temp_file(suffix='.wav') # print(f'temp_file={path}') @@ -152,8 +169,9 @@ class AudioTrackVad(MediaStreamTrack): await self.onvoiceend(path) # print('************over*******') return - """ - + ############ + # Method:2 + ############ path = temp_file(suffix='.wav') output_container = av.open(path, 'w') out_stream = output_container.add_stream('pcm_s16le', rate=16000, layout='mono') @@ -164,4 +182,9 @@ class AudioTrackVad(MediaStreamTrack): output_container.mux(out_stream.encode()) output_container.close() return path - + """ + f1 = self.frames_write_wave(self.voiced_frames) + frames = self.frames_resample(self.voiced_frames, sr=16000) + fn = self.frames_write_wave(frames) + print(f'source wave filename={f1}, mono 16000 wave filename={fn}') + return fn