This commit is contained in:
yumoqing 2024-09-14 20:53:38 +08:00
parent 7d4cf9c494
commit 2452f9f84b

View File

@ -8,6 +8,7 @@ from aiortc import MediaStreamTrack
from aiortc.contrib.media import MediaBlackhole, MediaPlayer, MediaRecorder, MediaRelay from aiortc.contrib.media import MediaBlackhole, MediaPlayer, MediaRecorder, MediaRelay
import webrtcvad import webrtcvad
import wave import wave
from scipy.io.wavfile import write
import numpy as np import numpy as np
from av import AudioLayout, AudioResampler, AudioFrame, AudioFormat from av import AudioLayout, AudioResampler, AudioFrame, AudioFormat
@ -130,6 +131,19 @@ class AudioTrackVad(MediaStreamTrack):
duration = f.samples * 1000 / f.sample_rate + duration duration = f.samples * 1000 / f.sample_rate + duration
return 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): async def write_wave(self):
"""Writes a .wav file. """Writes a .wav file.
@ -137,6 +151,9 @@ class AudioTrackVad(MediaStreamTrack):
""" """
""" """
############
# Method:1
############
audio_data = self.to_mono16000_data() audio_data = self.to_mono16000_data()
path = temp_file(suffix='.wav') path = temp_file(suffix='.wav')
# print(f'temp_file={path}') # print(f'temp_file={path}')
@ -152,8 +169,9 @@ class AudioTrackVad(MediaStreamTrack):
await self.onvoiceend(path) await self.onvoiceend(path)
# print('************over*******') # print('************over*******')
return return
""" ############
# Method:2
############
path = temp_file(suffix='.wav') path = temp_file(suffix='.wav')
output_container = av.open(path, 'w') output_container = av.open(path, 'w')
out_stream = output_container.add_stream('pcm_s16le', rate=16000, layout='mono') 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.mux(out_stream.encode())
output_container.close() output_container.close()
return path 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