-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
89 lines (65 loc) · 2.84 KB
/
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
from os import listdir
from os.path import isfile, join
import numpy as np
import librosa
import matplotlib.pyplot as plt
from scipy import signal
"""
a frame-length of 25 ms and a frame-shift of 10 ms are
extracted. Since the utterances in the VoxCeleb dataset are
of varying duration (up to 144.92 s), we x the length of
the input sequence to 3 seconds. These end up as log-mel
lterbank of size 40300 for a 3-second utterance.
frame-shift = hop time ???
n_fft=length of the FFT window(?=frame length)(# Size of the FFT may be used as the window length)
win_length= The window will be of length n_fft if not specified
hop_length = number of samples between successive frames
( Step or stride between windows. If the step is smaller than the window lenght, the windows will overlap)
https://librosa.org/doc/latest/generated/librosa.feature.melspectrogram.html
"""
def return_files(directory):
all_files = []
for file in os.listdir(os.getcwd()+'/'+directory):
if file.endswith(".wav"):
all_files.append(os.getcwd()+'/'+directory+'/'+file)
print(all_files)
return all_files
def return_spectograms(directory):
files = return_files(directory)
spectograms = []
for file in files:
spectograms.append(apply_melspectrogram_to_file(file))
return spectograms
def apply_melspectrogram_to_file(filename):
target_len = 66150
y, sample_rate = librosa.load(filename, duration=3)
while(y.shape[0] != target_len):
y = np.append(y, y[:target_len - y.shape[0]])
duration = len(y) / sample_rate
#librosa.display.waveplot(y=y, sr=sample_rate)
if y.shape[0] == 0:
print("y.shape[0] == 0")
return None
else:
window_time = .025
hop_time = .01
n_fft = sample_rate * window_time
hop_len = sample_rate*hop_time
# print(int(n_fft))
melspectrogram = librosa.feature.melspectrogram(y=librosa.effects.preemphasis(
y), sr=sample_rate, n_mels=40, n_fft=int(n_fft), hop_length=int(hop_len), window=signal.windows.hamming)
#melspectrogram = librosa.feature.melspectrogram(y=librosa.effects.preemphasis(y), sr=sample_rate,n_mels=40,window=signal.windows.hamming)
log_melspectrogram = librosa.power_to_db(melspectrogram, ref=np.max)
#normalized_melspectrogram = (log_melspectrogram - log_melspectrogram.mean()) / log_melspectrogram.std()
melspectrogram = log_melspectrogram.transpose()[:-1]
return melspectrogram
def display_all_spectograms(spectrograms):
for i in range(0, len(spectrograms)):
display_spectrogram(spectrograms[i])
def display_spectrogram(spectrogram):
librosa.display.specshow(spectrogram.transpose(),
y_axis='mel', fmax=8000, x_axis='s')
plt.title('Mel Spectrogram')
plt.colorbar(format='%+2.0f dB')
plt.show()