Commit dc8d49fd authored by I.K Seneviratne's avatar I.K Seneviratne

Merge branch 'IT17100908' into 'QA_RELEASE'

It17100908

See merge request !28
parents 49a7fec1 53f420ec
import librosa import librosa
from pysndfx import AudioEffectsChain from pysndfx import AudioEffectsChain
import numpy as np
import math
import python_speech_features import python_speech_features
import scipy as sp
from scipy import signal
import soundfile
def read_file(file_name): def read_file(file_name):
...@@ -14,31 +10,18 @@ def read_file(file_name): ...@@ -14,31 +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
# '''CENTROID''' return a, sr
#
# def reduce_noise_centroid_s(y, sr):
#
# cent = librosa.feature.spectral_centroid(y=y, sr=sr)
# threshold_h = np.max(cent)
# threshold_l = np.min(cent)
# less_noise = AudioEffectsChain().lowshelf(gain=-12.0, frequency=threshold_l, slope=0.5).highshelf(gain=-12.0, frequency=threshold_h, slope=0.5).limiter(gain=6.0)
# y_cleaned = less_noise(y)
# return y_cleaned
#jijoijij
'''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 = []
...@@ -56,22 +39,22 @@ def mffc_highshelf(y, sr): ...@@ -56,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))
...@@ -81,49 +64,49 @@ def mfcc_lowshelf(y, sr): ...@@ -81,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')
import numpy as nump
import scipy as sip
from scipy.io.wavfile import read
from scipy.io.wavfile import write
from scipy import signal
import matplotlib.pyplot as mplt
#get_ipython().magic('matplotlib inline')
(Frequency, array) = read('lectures/Lecture01.wav')
len(array)
mplt.plot(array)
mplt.title('Original Signal Spectrum')
mplt.xlabel('Frequency(Hz)')
mplt.ylabel('Amplitude')
fourierTransformation = sip.fft(array)
scale = sip.linspace(0, Frequency, len(array))
mplt.stem(scale[0:5000], nump.abs(fourierTransformation[0:5000]), 'r')
mplt.title('Signal spectrum after FFT')
mplt.xlabel('Frequency(Hz)')
mplt.ylabel('Amplitude')
guassianNoise = nump.random.rand(len(fourierTransformation))
NewSound = guassianNoise + array
write("New-Sound-Added-With-Guassian-Noise.wav", Frequency, NewSound)
u,v = signal.butter(5, 1000/(Frequency/2), btype='highpass')
filteredSignal = signal.lfilter(u,v,NewSound)
# plotting the signal.
mplt.plot(filteredSignal)
mplt.title('Highpass Filter')
mplt.xlabel('Frequency(Hz)')
mplt.ylabel('Amplitude')
# ButterWorth low-filter
x,y = signal.butter(5, 380/(Frequency/2), btype='lowpass')
# Applying the filter to the signal
newFilteredSignal = signal.lfilter(x,y,filteredSignal)
# plotting the signal.
mplt.plot(newFilteredSignal)
mplt.title('Lowpass Filter')
mplt.xlabel('Frequency(Hz)')
mplt.ylabel('Amplitude')
write("removed.wav", Frequency, nump.int16(newFilteredSignal/nump.max(nump.abs(newFilteredSignal)) * 32767))
\ No newline at end of file
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