-
Notifications
You must be signed in to change notification settings - Fork 12
/
process_data.py
74 lines (58 loc) · 2.42 KB
/
process_data.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
import pickle
from glob import iglob
from shutil import rmtree
import numpy as np
from constants import *
from model_data import read_audio_from_filename
def mkdir_p(path):
import errno
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def del_folder(path):
try:
rmtree(path)
except:
pass
del_folder(OUTPUT_DIR_TRAIN)
del_folder(OUTPUT_DIR_TEST)
mkdir_p(OUTPUT_DIR_TRAIN)
mkdir_p(OUTPUT_DIR_TEST)
def extract_class_id(wav_filename):
"""
The name of the audio file. The name takes the following format: [fsID]-[classID]-[occurrenceID]-[sliceID].wav, where:
[fsID] = the Freesound ID of the recording from which this excerpt (slice) is taken
[classID] = a numeric identifier of the sound class (see description of classID below for further details)
[occurrenceID] = a numeric identifier to distinguish different occurrences of the sound within the original recording
[sliceID] = a numeric identifier to distinguish different slices taken from the same occurrence
"""
return wav_filename.split('-')[1]
def convert_data():
for i, wav_filename in enumerate(iglob(os.path.join(DATA_AUDIO_DIR, '**/**.wav'), recursive=True)):
class_id = extract_class_id(wav_filename)
audio_buf = read_audio_from_filename(wav_filename, target_sr=TARGET_SR)
# normalize mean 0, variance 1
audio_buf = (audio_buf - np.mean(audio_buf)) / np.std(audio_buf)
original_length = len(audio_buf)
print(i, wav_filename, original_length, np.round(np.mean(audio_buf), 4), np.std(audio_buf))
if original_length < AUDIO_LENGTH:
audio_buf = np.concatenate((audio_buf, np.zeros(shape=(AUDIO_LENGTH - original_length, 1))))
print('PAD New length =', len(audio_buf))
elif original_length > AUDIO_LENGTH:
audio_buf = audio_buf[0:AUDIO_LENGTH]
print('CUT New length =', len(audio_buf))
output_folder = OUTPUT_DIR_TRAIN
if 'fold10' in wav_filename:
output_folder = OUTPUT_DIR_TEST
output_filename = os.path.join(output_folder, str(i) + '.pkl')
out = {'class_id': class_id,
'audio': audio_buf,
'sr': TARGET_SR}
with open(output_filename, 'wb') as w:
pickle.dump(out, w)
if __name__ == '__main__':
convert_data()