#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!')