-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
61 lines (46 loc) · 2.13 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
print('Hello Kaare')
print(__name__)
import os
from PIL import Image
import librosa
import numpy as np
# Define a function that splits an audio file,
# extracts spectrograms and saves them in a working directory
def get_spectrograms(filepath, primary_label, output_dir, SAMPLE_RATE, SIGNAL_LENGTH, SPEC_SHAPE, FMIN, FMAX):
# Open the file with librosa (limited to the first 15 seconds)
sig, rate = librosa.load(filepath, sr=SAMPLE_RATE, offset=None, duration=15)
# Split signal into five second chunks
sig_splits = []
for i in range(0, len(sig), int(SIGNAL_LENGTH * SAMPLE_RATE)):
split = sig[i:i + int(SIGNAL_LENGTH * SAMPLE_RATE)]
# End of signal?
if len(split) < int(SIGNAL_LENGTH * SAMPLE_RATE):
break
sig_splits.append(split)
# Extract mel spectrograms for each audio chunk
s_cnt = 0
saved_samples = []
for chunk in sig_splits:
hop_length = int(SIGNAL_LENGTH * SAMPLE_RATE / (SPEC_SHAPE[1] - 1))
mel_spec = librosa.feature.melspectrogram(y=chunk,
sr=SAMPLE_RATE,
n_fft=1024,
hop_length=hop_length,
n_mels=SPEC_SHAPE[0],
fmin=FMIN,
fmax=FMAX)
mel_spec = librosa.power_to_db(mel_spec, ref=np.max)
# Normalize
mel_spec -= mel_spec.min()
mel_spec /= mel_spec.max()
# Save as image file
save_dir = os.path.join(output_dir, primary_label)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
save_path = os.path.join(save_dir, filepath.rsplit(os.sep, 1)[-1].rsplit('.', 1)[0] +
'_' + str(s_cnt) + '.png')
im = Image.fromarray(mel_spec * 255.0).convert("L")
im.save(save_path)
saved_samples.append(save_path)
s_cnt += 1
return saved_samples