# Skip all sessions from 3 to 16 - these will go through dehissing pipeline
toskip = ['3_1', '3_2', '4_1', '4_2', '5_1', '5_2', '6_1', '6_2',
'7_1', '7_2', '8_1', '8_2', '9_1', '9_2', '10_1', '10_2',
'11_1', '11_2', '12_1', '12_2', '13_1', '13_2', '14_1', '14_2',
'15_1', '15_2', '16_1', '16_2']
# Here we specify strength of the filter that each session (upon manual auditory inspection) needs
verylittle = ['38_2', '39_1']
little = ['36_2', '37_1', '37_2', '38_1', '39_1',
'40_2', '41_1', '41_2', '43_1', '43_2', '44_1', '44_2',
'45_1', '45_2', '46_1', '46_2', '47_1', '47_2', '48_1', '48_2',
'49_1', '49_2', '50_1', '50_2', '51_1', '51_2', '52_1', '52_2',
'53_1', '53_2', '54_2', '55_1', '55_2', '56_1', '56_2',
'57_1', '57_2', '58_1', '58_2', '59_1', '59_2', '60_2',
'61_1', '61_2', '62_1', '62_2', '63_1', '63_2', '64_1', '64_2',
'65_1', '65_2', '66_1', '66_2', '67_1', '69_1', '69_2',
'71_1', '71_2', '72_1', '72_2']
more = ['27_1', '28_1', '28_2', '29_1', '29_2', '30_1', '30_2',
'31_1', '31_2', '32_1', '33_1', '33_2', '34_1', '34_2',
'35_1', '35_2', '36_1']
big = ['1_1', '1_2', '2_1', '2_2','54_1', '67_2', '27_2','32_2', '42_1','42_2', '40_1', # nonstanionary
'17_1', '17_2', '18_1', '18_2', '19_1', '19_2', '20_1', '20_2',
'21_1', '21_2', '22_1', '22_2', '23_1', '23_2', '24_1', '24_2',
'26_1']
for alignedfile in alignedfiles:
basename = os.path.basename(alignedfile).split('.')[0]
if basename.startswith(tuple(toskip)):
print(f'Skipping denoising for {alignedfile}')
continue
denoised_path = alignedfile.replace('_aligned.wav', '_aligned_denoised.wav')
if os.path.exists(denoised_path):
print(f'Denoised file already exists for {alignedfile}, skipping.')
continue
print(f'Denoising {alignedfile}')
data, rate = sf.read(alignedfile)
# Create 1 second noise sample from end
noise_sample = data[-int(1 * rate):]
# Perform noise reduction
if basename.startswith(tuple(verylittle)):
print('Denoising level 0')
reduced_noise = nr.reduce_noise(y=data, sr=rate, prop_decrease=0.65, n_std_thresh_stationary=1.8, stationary=True)
elif basename.startswith(tuple(little)):
print('Denoising level 1')
reduced_noise = nr.reduce_noise(y=data, sr=rate, prop_decrease=0.85, n_std_thresh_stationary=1.7, stationary=True)
elif basename.startswith(tuple(more)):
print('Denoising level 2')
reduced_noise = nr.reduce_noise(y=data, sr=rate, prop_decrease=0.87, n_std_thresh_stationary=1.5, stationary=True)
elif basename.startswith(tuple(big)):
print('Denoising level 3')
reduced_noise = nr.reduce_noise(y=data, sr=rate, prop_decrease=0.9, n_std_thresh_stationary=1.2, stationary=False)
else:
print(f'No specific denoising parameters for {alignedfile}.')
reduced_noise = nr.reduce_noise(y=data, sr=rate, prop_decrease=0.85, n_std_thresh_stationary=1.7, stationary=True)
# Save denoised audio
sf.write(denoised_path, reduced_noise, rate)