-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_utils.py
65 lines (48 loc) · 1.97 KB
/
audio_utils.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
import numpy as np
import sounddevice as sd
import librosa
import matplotlib.pyplot as plt
from constants import MAX_FRAME_LENGTH, MIC_ID
def add_noise(audio_data):
# Gauss
noise_amp = 0.005 * np.random.uniform() * np.amax(audio_data)
audio_data = audio_data + noise_amp * np.random.normal(size=audio_data.shape)
return audio_data
class AudioRecorder:
def __init__(self, duration, samplerate):
self.duration = duration
self.samplerate = samplerate
def record_audio(self):
print("Rec...")
audio_data = sd.rec(int(self.samplerate * self.duration), samplerate=self.samplerate, channels=1,
dtype='float64', device=MIC_ID)
sd.wait()
print("End rec.")
audio_data_with_noise = add_noise(audio_data)
return audio_data_with_noise
class FeatureExtractor:
"""
Extracts audio features using MFCC.
MFCC is widely used in voice recognition systems and is based on human hearing perceptions.
"""
def extract_features(self, filename, plot=True):
y, sr = librosa.load(filename)
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
delta_mfccs = librosa.feature.delta(mfccs)
delta2_mfccs = librosa.feature.delta(mfccs, order=2)
combined_mfccs = np.concatenate((mfccs, delta_mfccs, delta2_mfccs), axis=0)
if plot:
librosa.display.specshow(combined_mfccs, x_axis='time')
plt.colorbar()
plt.title('MFCC with Deltas')
plt.tight_layout()
plt.show()
return combined_mfccs
def pad_mfcc(self, mfcc):
if mfcc.shape[1] < MAX_FRAME_LENGTH:
return np.pad(mfcc, ((0, 0), (0, MAX_FRAME_LENGTH - mfcc.shape[1])))
return mfcc[:, :MAX_FRAME_LENGTH]
def extract_mfcc_features(self, signal, sr):
mfccs = librosa.feature.mfcc(y=signal, sr=sr, n_mfcc=13)
return self.pad_mfcc(mfccs).T
feature_extractor = FeatureExtractor()