-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
44 lines (32 loc) · 1.18 KB
/
preprocess.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
import json
import os
import librosa
DATASET = "languages"
JSON_FILE = "data.json"
def extract_mfcc(dataset, json_file, num_mfcc=13, n_fft=2048, hop_length=512):
mfcc_data = {
"languages": [],
"mfcc": [],
"labels": []
}
# loop through all languages
for i, language in enumerate(os.listdir(dataset)):
# save language
mfcc_data["languages"].append(language)
print(language)
# process all audio files in the language sub folder
language_folder = DATASET + '/' + language
for audio_clip in os.listdir(language_folder):
# load audio clip
file_path = language_folder + '/' + audio_clip
signal, sample_rate = librosa.load(file_path, sr=22050)
# extract mfccs
mfcc = librosa.feature.mfcc(signal, sample_rate,
n_mfcc=num_mfcc, n_fft=n_fft, hop_length=hop_length)
mfcc = mfcc.T
mfcc_data["mfcc"].append(mfcc.tolist())
mfcc_data["labels"].append(i)
# save MFCCs to file
with open(json_file, "w") as fp:
json.dump(mfcc_data, fp, indent=4)
extract_mfcc(DATASET, JSON_FILE)