Python对批量wav文件加入高斯白噪声

#coding=gbk
import os
import wave
import librosa
import numpy as np

def add_noise(data):
    wn = np.random.normal(0,1,len(data))
    data_noise = np.where(data != 0.0, data.astype('float64') + 0.02 * wn, 0.0).astype(np.float32)
    return data_noise


path = r"..\test"
files = os.listdir(path)
files = [path + "\\" + f for f in files if f.endswith('.wav')]

for i in range(len(files)):
    FileName = files[i]
    print("add noise File Name is ", FileName)
    data, fs = librosa.core.load(files[i])
    path_noise=".\\test_addnoise\\" + files[i][-17:-4]+'-noise.wav'
    data_noise = add_noise(data)
    librosa.output.write_wav(path_noise, data_noise, fs)

print('run over!')

 

你可能感兴趣的:(python,音频处理,工具使用)