-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_dataset.py
92 lines (69 loc) · 2.54 KB
/
create_dataset.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
90
91
92
from __future__ import absolute_import, division, print_function, unicode_literals
#import tensorflow as tf
#from tensorflow.keras import layers
import random
import numpy as np
import os, sys, argparse, time
from multiprocessing import Pool
import librosa
from pathlib import Path
import configparser
import pdb
#Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, default ='./default.ini' , help='path to the config file')
args = parser.parse_args()
#Get configs
config_path = args.config
config = configparser.ConfigParser(allow_no_value=True)
config.read(config_path)
#audio configs
sampling_rate = config['audio'].getint('sampling_rate')
hop_length = config['audio'].getint('hop_length')
bins_per_octave = config['audio'].getint('bins_per_octave')
num_octaves = config['audio'].getint('num_octaves')
n_bins = num_octaves * bins_per_octave
n_iter=config['audio'].getint('n_iter')
cqt_bit_depth = config['audio'].get('cqt_bit_depth')
#dataset
dataset = Path(config['dataset'].get('datapath'))
if not dataset.exists():
raise FileNotFoundError(dataset.resolve())
cqt_dataset = config['dataset'].get('cqt_dataset')
my_cqt = dataset / cqt_dataset
os.makedirs(my_cqt,exist_ok=True)
my_audio_folder = dataset / 'audio'
audio_files = [f for f in my_audio_folder.glob('*.wav')]
print('TOTAL FILES: {}'.format(len(audio_files)))
config_path = my_cqt / 'config.ini'
with open( config_path, 'w') as configfile:
config.write(configfile)
def calculate_cqt(f):
outfile = my_cqt.joinpath(f.stem + '.npy')
try:
s, fs = librosa.load(f, sr=None)
if fs != sampling_rate:
print("Resampling {}".format(f))
s, fs = librosa.load(f, sr=sampling_rate)
# Get the CQT magnitude
C_complex = librosa.cqt(y=s, sr=sampling_rate, hop_length= hop_length, bins_per_octave=bins_per_octave, n_bins=n_bins)
C = np.abs(C_complex)
# pytorch expects the transpose of librosa's output
C = np.transpose(C)
# Choose the datatype
if cqt_bit_depth == 'float32':
C = C.astype('float32')
elif cqt_bit_depth == 'float64':
C = C.astype('float64')
else:
raise TypeError('cqt_bit_depth datatype is unknown. Choose either float32 or float64')
print('writing: {}'.format(outfile))
np.save(outfile, C)
except:
print('There was an issue with '+f)
return
if __name__ == '__main__':
pool = Pool()
pool.map(calculate_cqt, audio_files)
pool.close()
pool.join()