tie*_*uer 5 audio numpy pcm webrtc librosa
情况
我通过使用Python适配器WebRTC-VAD从WebRTC使用VAD(语音活动检测)。GitHub存储库中的示例实现使用Python的wave模块从文件中读取PCM数据。请注意,根据注释,该模块仅适用于单声道音频,采样速率为8000、16000或32000 Hz。
我想做的事
从具有不同采样率的任意音频文件(MP3和WAV文件)中读取音频数据,将它们转换为WebRTC-VAD正在使用的PCM表示形式,应用WebRTC-VAD来检测语音活动,最后通过产生Numpy-Arrays处理结果再次从PCM数据中获取数据,因为使用Librosa时最容易使用它们
我的问题
WebRTC-VAD模块仅在使用该wave模块时才能正常工作。该模块将PCM数据作为bytes对象返回。当将其馈入已使用的Numpy数组时,它不起作用librosa.load(...)。我还没有找到在两种表示形式之间进行转换的方法。
到目前为止我做了什么
我编写了以下函数来从音频文件中读取音频数据并自动将它们转换:
使用Librosa读取/转换任何音频数据的通用函数(->返回Numpy数组):
def read_audio(file_path, sample_rate=None, mono=False):
return librosa.load(file_path, sr=sample_rate, mono=mono)
Run Code Online (Sandbox Code Playgroud)
读取任意数据作为PCM数据的功能(->返回字节):
def read_audio_vad(file_path):
audio, rate = librosa.load(file_path, sr=16000, mono=True)
tmp_file = 'tmp.wav'
sf.write(tmp_file, audio, rate, subtype='PCM_16')
audio, rate = read_pcm16_wave(tmp_file)
remove(tmp_file)
return audio, rate
def read_pcm16_wave(file_path):
with wave.open(file_path, 'rb') as wf:
sample_rate = wf.getframerate()
pcm_data = wf.readframes(wf.getnframes())
return pcm_data, sample_rate
Run Code Online (Sandbox Code Playgroud)
如您所见,我先绕过librosa读取/转换音频数据。这是必需的,因此我可以读取具有任意编码的MP3文件或WAV文件,并使用Librosa自动将其重新采样为16kHz单声道。然后,我正在写一个临时文件。在删除文件之前,我会再次读取内容,但这一次使用wave模块。这给了我PCM数据。
现在,我有以下代码来提取语音活动并生成Numpy数组:
def webrtc_voice(audio, rate):
voiced_frames = webrtc_split(audio, rate)
tmp_file = 'tmp.wav'
for frames in voiced_frames:
voice_audio = b''.join([f.bytes for f in frames])
write_pcm16_wave(tmp_file, voice_audio, rate)
voice_audio, rate = read_audio(tmp_file)
remove(tmp_file)
start_time = frames[0].timestamp
end_time = (frames[-1].timestamp + frames[-1].duration)
start_frame = int(round(start_time * rate / 1e3))
end_frame = int(round(end_time * rate / 1e3))
yield voice_audio, rate, start_frame, end_frame
def write_pcm16_wave(path, audio, sample_rate):
with wave.open(path, 'wb') as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(sample_rate)
wf.writeframes(audio)
Run Code Online (Sandbox Code Playgroud)
如您所见,我再次绕过临时文件,先写PCM数据,然后再用Librosa读出临时文件,得到一个Numpy数组。该webrtc_split功能是示例实现中的实现,只做了很少的更改。为了完整起见,我在这里发布:
def webrtc_split(audio, rate, aggressiveness=3, frame_duration_ms=30, padding_duration_ms=300):
vad = Vad(aggressiveness)
num_padding_frames = int(padding_duration_ms / frame_duration_ms)
ring_buffer = collections.deque(maxlen=num_padding_frames)
triggered = False
voiced_frames = []
for frame in generate_frames(audio, rate):
is_speech = vad.is_speech(frame.bytes, rate)
if not triggered:
ring_buffer.append((frame, is_speech))
num_voiced = len([f for f, speech in ring_buffer if speech])
if num_voiced > 0.9 * ring_buffer.maxlen:
triggered = True
for f, s in ring_buffer:
voiced_frames.append(f)
ring_buffer.clear()
else:
voiced_frames.append(frame)
ring_buffer.append((frame, is_speech))
num_unvoiced = len([f for f, speech in ring_buffer if not speech])
if num_unvoiced > 0.9 * ring_buffer.maxlen:
triggered = False
yield voiced_frames
ring_buffer.clear()
voiced_frames = []
if voiced_frames:
yield voiced_frames
class Frame(object):
"""
object holding the audio signal of a fixed time interval (30ms) inside a long audio signal
"""
def __init__(self, bytes, timestamp, duration):
self.bytes = bytes
self.timestamp = timestamp
self.duration = duration
def generate_frames(audio, sample_rate, frame_duration_ms=30):
frame_length = int(sample_rate * frame_duration_ms / 1000) * 2
offset = 0
timestamp = 0.0
duration = (float(frame_length) / sample_rate)
while offset + frame_length < len(audio):
yield Frame(audio[offset:offset + frame_length], timestamp, duration)
timestamp += duration
offset += frame_length
Run Code Online (Sandbox Code Playgroud)
我的问题
对于我来说,使用wave模块写入/读取临时文件并使用Librosa读取/写入这些文件以获得Numpy数组的实现对我来说过于复杂。但是,尽管花了一整天时间,但我没有找到直接在两种编码之间进行转换的方法。我承认我不完全了解PCM和WAVE文件的所有细节,也不了解使用16/24/32位存储PCM数据或字节序的影响。我希望上面的解释足够详细,不要太多。有没有更简单的方法可以在内存中的两种表示形式之间进行转换?
看来 WebRTC-VAD 和 Python 包装器py-webrtcvad期望音频数据为 16 位 PCM 小尾数法 - 这是 WAV 文件中最常见的存储格式。
librosa然而,它的底层 I/O 库pysoundfile总是返回范围内的浮点数组[-1.0, 1.0]。要将其转换为包含 16 位 PCM 的字节,您可以使用以下float_to_pcm16函数。
def float_to_pcm16(audio):
import numpy
ints = (audio * 32767).astype(numpy.int16)
little_endian = ints.astype('<u2')
buf = little_endian.tostring()
return buf
def read_pcm16(path):
import soundfile
audio, sample_rate = soundfile.read(path)
assert sample_rate in (8000, 16000, 32000, 48000)
pcm_data = float_to_pcm16(audio)
return pcm_data, sample_rate
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
582 次 |
| 最近记录: |