-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.py
94 lines (73 loc) · 3.46 KB
/
filter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import noisereduce as nr
import librosa
import soundfile as sf
import numpy as np
import concurrent.futures
import threading
#define the input directory
input_dir = 'wavs/'
#lock to safely print debug messages in multithreaded environment
print_lock = threading.Lock()
def apply_dynamic_noise_reduction(audio_data, sample_rate, frame_length=2048, hop_length=512):
#calculate short-term energy for each frame
energy = np.array([
sum(abs(audio_data[i:i+frame_length]**2))
for i in range(0, len(audio_data), hop_length)
])
#normalize energy
max_energy = max(energy)
normalized_energy = energy / max_energy
#threshold for detecting silence/background noise (tuneable parameter)
silence_threshold = 0.1
#assume that the quieter sections are dominated by noise and calculate the noise profile
noise_frames = [audio_data[i:i+frame_length] for i in range(0, len(audio_data), hop_length) if normalized_energy[i // hop_length] < silence_threshold]
#if noise frames were found, calculate a noise profile
if len(noise_frames) > 0:
noise_profile = np.concatenate(noise_frames)
else:
noise_profile = audio_data[:frame_length] #default to the first frame if no quiet sections are found
#apply noise reduction
reduced_audio = np.array(audio_data)
for i in range(0, len(audio_data), hop_length):
start_idx = i
end_idx = min(i + frame_length, len(audio_data))
frame = audio_data[start_idx:end_idx]
#determine how much noise reduction to apply based on frame energy
if normalized_energy[i // hop_length] < silence_threshold:
#apply more aggressive noise reduction in quieter sections
reduced_frame = nr.reduce_noise(y=frame, sr=sample_rate, y_noise=noise_profile, prop_decrease=1.0)
else:
#apply less aggressive reduction in louder sections to avoid cutting off the main signal
reduced_frame = nr.reduce_noise(y=frame, sr=sample_rate, y_noise=noise_profile, prop_decrease=0.5)
#replace the original frame with the reduced version
reduced_audio[start_idx:end_idx] = reduced_frame
return reduced_audio
def process_single_audio_file(file):
file_path = os.path.join(input_dir, file)
#load the audio file
audio_data, sample_rate = librosa.load(file_path, sr=None)
with print_lock:
print(f"[DEBUG] Processing {file_path}.")
#apply dynamic noise reduction
reduced_noise = apply_dynamic_noise_reduction(audio_data, sample_rate)
#create new filename with _cleaned suffix
new_filename = file.replace('.wav', '_cleaned.wav')
new_file_path = os.path.join(input_dir, new_filename)
#save the cleaned audio
sf.write(new_file_path, reduced_noise, sample_rate)
#remove the original file
os.remove(file_path)
def process_audio_files():
#list all .wav files in the input directory
files = [f for f in os.listdir(input_dir) if f.endswith('.wav') and not f.endswith('_cleaned.wav')]
#use ThreadPoolExecutor to process files concurrently
with concurrent.futures.ThreadPoolExecutor() as executor:
#submit tasks to the thread pool
futures = [executor.submit(process_single_audio_file, file) for file in files]
#wait for all tasks to complete
concurrent.futures.wait(futures)
with print_lock:
print(f"[DEBUG] Processed and cleaned {len(files)} files.")
if __name__ == '__main__':
process_audio_files()