Commit 53f420ec authored by LiniEisha's avatar LiniEisha

Reformatting noise.py

parent 12d23d82
...@@ -10,18 +10,18 @@ def read_file(file_name): ...@@ -10,18 +10,18 @@ def read_file(file_name):
sample_path = sample_directory + sample_file sample_path = sample_directory + sample_file
# generating audio time series and a sampling rate (int) # generating audio time series and a sampling rate (int)
y, sr = librosa.load(sample_path) a, sr = librosa.load(sample_path)
return y, sr return a, sr
'''MFCC''' '''MFCC'''
def mffc_highshelf(y, sr): def mffc_highshelf(a, sr):
mfcc = python_speech_features.base.mfcc(y) mfcc = python_speech_features.base.mfcc(a)
mfcc = python_speech_features.base.logfbank(y) mfcc = python_speech_features.base.logfbank(a)
mfcc = python_speech_features.base.lifter(mfcc) mfcc = python_speech_features.base.lifter(mfcc)
sum_of_squares = [] sum_of_squares = []
...@@ -39,22 +39,22 @@ def mffc_highshelf(y, sr): ...@@ -39,22 +39,22 @@ def mffc_highshelf(y, sr):
min_hz = min(hz) min_hz = min(hz)
speech_booster = AudioEffectsChain().highshelf(frequency=min_hz*(-1)*1.2, gain=-12.0, slope=0.6).limiter(gain=8.0) speech_booster = AudioEffectsChain().highshelf(frequency=min_hz*(-1)*1.2, gain=-12.0, slope=0.6).limiter(gain=8.0)
y_speach_boosted = speech_booster(y) a_speach_boosted = speech_booster(a)
return (y_speach_boosted) return (a_speach_boosted)
def mfcc_lowshelf(y, sr): def mfcc_lowshelf(a, sr):
mfcc = python_speech_features.base.mfcc(y) mfcc = python_speech_features.base.mfcc(a)
mfcc = python_speech_features.base.logfbank(y) mfcc = python_speech_features.base.logfbank(a)
mfcc = python_speech_features.base.lifter(mfcc) mfcc = python_speech_features.base.lifter(mfcc)
sum_of_squares = [] sum_of_squares = []
index = -1 index = -1
for r in mfcc: for x in mfcc:
sum_of_squares.append(0) sum_of_squares.append(0)
index = index + 1 index = index + 1
for n in r: for n in x:
sum_of_squares[index] = sum_of_squares[index] + n**2 sum_of_squares[index] = sum_of_squares[index] + n**2
strongest_frame = sum_of_squares.index(max(sum_of_squares)) strongest_frame = sum_of_squares.index(max(sum_of_squares))
...@@ -64,49 +64,49 @@ def mfcc_lowshelf(y, sr): ...@@ -64,49 +64,49 @@ def mfcc_lowshelf(y, sr):
min_hz = min(hz) min_hz = min(hz)
speech_booster = AudioEffectsChain().lowshelf(frequency=min_hz*(-1), gain=12.0, slope=0.5) speech_booster = AudioEffectsChain().lowshelf(frequency=min_hz*(-1), gain=12.0, slope=0.5)
y_speach_boosted = speech_booster(y) a_speach_boosted = speech_booster(a)
return (y_speach_boosted) return (a_speach_boosted)
def trim_silence(y): def trim_silence(y):
y_trimmed, index = librosa.effects.trim(y, top_db=20, frame_length=2, hop_length=500) a_trimmed, index = librosa.effects.trim(y, top_db=20, frame_length=2, hop_length=500)
trimmed_length = librosa.get_duration(y) - librosa.get_duration(y_trimmed) trimmed_length = librosa.get_duration(y) - librosa.get_duration(a_trimmed)
return y_trimmed, trimmed_length return a_trimmed, trimmed_length
def enhance(y): def enhance(y):
apply_audio_effects = AudioEffectsChain().lowshelf(gain=10.0, frequency=260, slope=0.1).reverb(reverberance=25, hf_damping=5, room_scale=5, stereo_depth=50, pre_delay=20, wet_gain=0, wet_only=False)#.normalize() apply_audio_effects = AudioEffectsChain().lowshelf(gain=10.0, frequency=260, slope=0.1).reverb(reverberance=25, hf_damping=5, room_scale=5, stereo_depth=50, pre_delay=20, wet_gain=0, wet_only=False)#.normalize()
y_enhanced = apply_audio_effects(y) a_enhanced = apply_audio_effects(y)
return y_enhanced return a_enhanced
def output_file(destination ,filename, y, sr, ext=""): def output_file(destination ,filename, a, sr, ext=""):
destination = destination + filename[:-4] + ext + '.wav' destination = destination + filename[:-4] + ext + '.wav'
librosa.output.write_wav(destination, y, sr) librosa.output.write_wav(destination, a, sr)
lectures = ['Lecture01.wav'] lectures = ['Lecture01.wav']
for s in lectures: for s in lectures:
filename = s filename = s
y, sr = read_file(filename) a, sr = read_file(filename)
# y_reduced_centroid_s = reduce_noise_centroid_s(y, sr) # a_reduced_centroid_s = reduce_noise_centroid_s(a, sr)
y_reduced_mfcc_lowshelf = mfcc_lowshelf(y, sr) a_reduced_mfcc_lowshelf = mfcc_lowshelf(a, sr)
y_reduced_mfcc_highshelf = mffc_highshelf(y, sr) a_reduced_mfcc_highshelf = mffc_highshelf(a, sr)
# trimming silences # trimming silences
# y_reduced_centroid_s, time_trimmed = trim_silence(y_reduced_centroid_s) # a_reduced_centroid_s, time_trimmed = trim_silence(a_reduced_centroid_s)
y_reduced_mfcc_up, time_trimmed = trim_silence(mfcc_lowshelf) a_reduced_mfcc_up, time_trimmed = trim_silence(mfcc_lowshelf)
y_reduced_mfcc_down, time_trimmed = trim_silence(mffc_highshelf) a_reduced_mfcc_down, time_trimmed = trim_silence(mffc_highshelf)
# output_file('lectures_trimmed_noise_reduced/' ,filename, y_reduced_centroid_s, sr, '_ctr_s') # output_file('lectures_trimmed_noise_reduced/' ,filename, y_reduced_centroid_s, sr, '_ctr_s')
output_file('lectures_trimmed_noise_reduced/' ,filename, y_reduced_mfcc_up, sr, '_mfcc_up') output_file('lectures_trimmed_noise_reduced/' ,filename, a_reduced_mfcc_up, sr, '_mfcc_up')
# output_file('lectures_trimmed_noise_reduced/' ,filename, y_reduced_mfcc_down, sr, '_mfcc_down') # output_file('lectures_trimmed_noise_reduced/' ,filename, a_reduced_mfcc_down, sr, '_mfcc_down')
# output_file('lectures_trimmed_noise_reduced/' ,filename, y, sr, '_org') # output_file('lectures_trimmed_noise_reduced/' ,filename, a, sr, '_org')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment