由于有网友私信我,怎么批量对音频文件进行音频数据增强处理,于是我就看了一些网上的资料~

音频数据增强主要有以下几种方法:
增加噪声、波形位移、波形拉伸、音高修正
话不多说,看代码吧~

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

再运行,就可以了~

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐