opendata/20220814/Source/denoise_wav.py

40 lines
1.5 KiB
Python

from scipy.io import wavfile
import noisereduce as nr
import numpy as np
import time
import wave
import os
for label in os.listdir(f'class'):
if label != '_desktop.ini':
for audio in os.listdir(f'class/{label}'):
if audio.split('.')[-1] == 'wav' and audio != '_desktop.ini':
audio_path = f'class/{label}/{audio}'
reduced_path = f'D:/reduce_audio/{label}'
print(audio_path)
rate, data = wavfile.read(audio_path)
data1 = data[:, 0]
data2 = data[0:, 1]
# perform noise reduction
reduced_noise1 = nr.reduce_noise(y=data1, sr=rate,stationary=True)
reduced_noise2 = nr.reduce_noise(y=data2, sr=rate,stationary=True)
reduced_noise = np.stack((reduced_noise1, reduced_noise2), axis=1)
if not os.path.exists(reduced_path):
os.mkdir(reduced_path)
wavfile.write(reduced_path + f'/{audio}', rate, reduced_noise)
# _,noisy_part = wavfile.read("noise.wav")
# SAMPLING_FREQUENCY=16000
# reduced_noise = nr.reduce_noise(y=data.T, y_noise=noisy_part, sr=SAMPLING_FREQUENCY)
#
# FORMAT = pyaudio.paInt16
# CHANNELS = 1
# RATE = 16000
# RECORD_SECONDS = time
# WAVE_OUTPUT_FILENAME = "out_file.wav"
#
# with wave.open(WAVE_OUTPUT_FILENAME, 'wb') as wf:
# wf.setnchannels(CHANNELS)
# wf.setsampwidth(2)
# wf.setframerate(RATE)
# wf.writeframes(b''.join(reduced_noise))
# load data