由于有网友私信我,怎么批量对音频文件进行音频数据增强处理,于是我就看了一些网上的资料~
音频数据增强主要有以下几种方法:
增加噪声、波形位移、波形拉伸、音高修正
话不多说,看代码吧~
1.代码如下:
import os
import librosa
import numpy as np
#####1.增加噪声#####
def add_noise(data):
wn = np.random.normal(0, 1, len(data))
return np.where(data != 0.0, data.astype('float64') + 0.02 * wn, 0.0).astype(np.float32)
#####2.波形位移#####
def time_shift(x, shift):
# shift:移动的长度
return np.roll(x, int(shift))
#####3.波形拉伸#####
def time_stretch(x, rate):
# rate:拉伸的尺寸,
# rate > 1 加快速度
# rate < 1 放慢速度
return librosa.effects.time_stretch(x, rate)
#####4.音高修正#####
def pitch_shifting(x, sr, n_steps, bins_per_octave=12):
# sr: 音频采样率
# n_steps: 要移动多少步
# bins_per_octave: 每个八度音阶(半音)多少步
return librosa.effects.pitch_shift(x, sr, n_steps, bins_per_octave=bins_per_octave)
#####批处理##########
input_path = r"E:\untitled1\audio_test\input" #原始文件路径
output_path = r"E:\untitled1\audio_test\output"#处理后的文件路径
for file in os.listdir(input_path):
file1 = input_path + '\\' + file
file2 = output_path + '\\' + 'noise_'+file
file3 = output_path + '\\' + 'shift_' + file
file4 = output_path + '\\' + 'stretch_' + file
file5 = output_path + '\\' + 'pitch1_' + file
file6 = output_path + '\\' + 'pitch2_' + file
file7 = output_path + '\\' + 'pitch3_' + file
data, fs = librosa.load(file1)
data_noise = add_noise(data)
data_shift = time_shift(data, shift=fs // 2)
data_stretch = time_stretch(data, rate=2)
data_pitch1 = pitch_shifting(data, fs, n_steps=6, bins_per_octave=12) # 向上移三音(如果bins_per_octave为12,则六步)
data_pitch2 = pitch_shifting(data, fs, n_steps=3, bins_per_octave=24) # 向上移三音(如果bins_per_octave为24,则3步)
data_pitch3 = pitch_shifting(data, fs, n_steps=-6, bins_per_octave=12) # 向下移三音(如果bins_per_octave为12,则六步)
librosa.output.write_wav(file2, data_noise, fs)
librosa.output.write_wav(file3, data_shift, fs)
librosa.output.write_wav(file4, data_stretch, fs)
librosa.output.write_wav(file5, data_pitch1, fs)
librosa.output.write_wav(file6, data_pitch2, fs)
librosa.output.write_wav(file7, data_pitch3, fs)
2.运行结果,如下:
3.音频内容对比:
原始音频波形如下:
增加噪声处理后的音频:
波形位移处理后的音频:
波形拉伸处理后的音频:
三组音高修正处理后的音频:
4.多说点~
由于我的librosa默认版本是0.8.0,所以报了下面的错
因此,我们需要降低librosa的版本,在终端输入
pip install librosa==0.7.2
然后后面再次运行,又报了一个的错误
在终端输入
pip install numba==0.48.0
再运行,就可以了~