This repository has been archived by the owner on Oct 10, 2019. It is now read-only.
/
features.py
executable file
·320 lines (264 loc) · 10.5 KB
/
features.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ------------------------------------
# file: features.py
# date: Tue April 22 18:31 2014
# author:
# Maarten Versteegh
# github.com/mwv
# maartenversteegh AT gmail DOT com
#
# Licensed under GPLv3
# ------------------------------------
"""features:
"""
from __future__ import division
import argparse
import glob
import json
import logging
import numpy as np
import oct2py
import os.path as path
import os
import scipy.io
import shutil
import spectral
import struct
import sys
import tempfile
import wave
import npz2h5features
import npz2csv
def resample(sig, ratio):
try:
import scikits.samplerate
return scikits.samplerate.resample(sig, ratio, 'sinc_best')
except ImportError:
import scipy.signal
return scipy.signal.resample(sig, int(round(sig.shape[0] * ratio)))
def parse_args(args=sys.argv):
parser = argparse.ArgumentParser(
prog='features.py',
formatter_class=argparse.RawDescriptionHelpFormatter,
description='Extract Mel spectral features from audio files.',
epilog="""Example usage:
$ python features.py test/wavs/*.wav -c mel_config.json
extracts features from audio files in current directory.\n
The output format is binary .npz. Each file contains two arrays, one holding
the features, the other the center times (in seconds) of the frames. To load
these files in python:
>>> import numpy as np
>>> data = np.load('/path/to/file.npz')
>>> features = data['features']
>>> center_times = data['time']
""")
parser.add_argument('files', metavar='WAV',
nargs='+',
help='input audio files')
parser.add_argument('-h5',
action='store',
dest='h5_output',
required=False,
help='output file in h5 format.\n'
' This is the default output format')
parser.add_argument('-csv',
action='store',
dest='csv_output',
required=False,
help='output file in csv format.\n')
parser.add_argument('-npz',
action='store',
dest='npz_output',
required=False,
help='output directory in npz format.\n'
'only precise it if you want to use the numpy '
'matrices directly')
parser.add_argument('-mat',
action='store',
dest='mat_output',
required=False,
help='output directory in matlab format.\n'
'Only precise it if you want to use the matlab '
'matrices directly')
parser.add_argument('-c', '--config',
action='store',
dest='config',
required=True,
help='configuration file.\n'
'Contains the type of features to extract and the'
' parameters in json format. See the default '
'config files for examples:\n'
'mel_config.json, mfcc_config.json, '
'rasta_config.json, lyon_config.json, '
'drnl_config.json')
parser.add_argument('-f', '--force',
action='store_true',
dest='force',
default=False,
help='force resampling in case of samplerate mismatch.'
'\nUse this option with parcimony, only to '
'make the samplerate uniform among the files. '
'If you want to resample every file, you should '
'do it before running the program.')
parser.add_argument('-v', '--verbose',
action='store_true',
help='display some log messages to stdout')
parser.add_argument('--tempdir', default='/tmp',
help='directory where to write temp files (erased after execution). '
'Default is to write to /tmp')
return vars(parser.parse_args(args))
def convert(files, outdir, encoder, force, verbose=False):
if verbose:
print 'computing features for {} wav files'.format(len(files))
for n, f in enumerate(files):
try:
fid = wave.open(f, 'r')
_, _, fs, nframes, _, _ = fid.getparams()
sig = np.array(struct.unpack_from("%dh" % nframes,
fid.readframes(nframes)))
fid.close()
except IOError:
print('No such file:', f)
exit()
if fs != encoder.config['fs']:
if force:
sig = resample(sig, encoder.config['fs'] / fs)
else:
print ('Samplerate mismatch, expected {0}, got {1}, in {2}.\n'
'Use option -f to force resampling of the audio file. '
'Note that you should use force with parsimony, it is '
'better to adjust the sampling rate to your wav files'
.format(encoder.config['fs'], fs, f))
exit()
feats_tmp = encoder.transform(sig)
feats = feats_tmp[0]
for i in range(1, len(feats_tmp)):
feats = np.hstack((feats, feats_tmp[i]))
wshift_smp = encoder.config['fs'] / encoder.config['frate']
wlen_smp = encoder.config['wlen'] * encoder.config['fs']
nframes = int(sig.shape[0] / wshift_smp + 1)
if nframes != feats.shape[0]:
raise ValueError('nframes mismatch. expected {0}, got {1}'
.format(feats.shape[0], nframes))
center_times = np.zeros((nframes,))
for fr in range(nframes):
start_smp = round(fr * wshift_smp)
end_smp = min(sig.shape[0], start_smp + wlen_smp)
start_ms = start_smp / fs
end_ms = end_smp / fs
center_times[fr] = (start_ms + end_ms) / 2
bname = path.splitext(path.basename(f))[0]
np.savez(path.join(outdir, bname + '.npz'),
features=feats,
time=center_times)
if verbose:
print 'done {}/{} : {}'.format(n+1, len(files), bname)
def center_times(fs, wshift_smp, wlen_smp, sig_len):
nframes = int(sig_len / wshift_smp + 1)
center_times = np.zeros((nframes,))
for fr in range(nframes):
start_smp = round(fr * wshift_smp)
end_smp = min(sig_len,
start_smp + wlen_smp)
start_ms = start_smp / fs
end_ms = end_smp / fs
center_times[fr] = (start_ms + end_ms) / 2
def mat2npz(indir, outdir):
for f in glob.glob(indir + '*.mat'):
mat = scipy.io.loadmat(f)
np.savez(path.join(outdir, os.path.basename(f) + '.npz'),
features=mat['features'],
time=np.ravel(mat['center_times']))
def main(args=sys.argv):
args = parse_args(args)
config_file = args['config']
try:
with open(config_file, 'r') as fid:
config = json.load(fid)
except IOError:
print('No such file: ', config_file)
exit()
force = args['force']
feat = config['features']
h5file = args['h5_output']
csvfile = args['csv_output']
npzdir = args['npz_output']
matdir = args['mat_output']
files = args['files']
verbose = args['verbose']
tempdir = args['tempdir']
# TODO strange bug from parsing where files[0] == __file__
if files[0][-3:] == '.py':
del files[0]
# print('\n'.join(files))
if not (h5file or csvfile or npzdir or matdir):
h5file = feat + '.features'
if h5file and os.path.exists(h5file):
print('The output file already exists: {}'.format(h5file))
exit()
if csvfile and os.path.exists(csvfile):
print('The output file already exists: {}'.format(csvfile))
exit()
if npzdir and not os.path.exists(npzdir):
print('No such directory: {}'.format(npzdir))
exit()
if matdir and not os.path.exists(matdir):
print('No such directory: {}'.format(matdir))
exit()
tmp = False
python = (feat == 'mel' or feat == 'mfcc')
octave = (feat == 'rasta' or feat == 'lyon' or feat == 'drnl')
try:
if (python and not npzdir) or (octave and not matdir):
outdir = tempfile.mkdtemp(dir=tempdir)
tmp = True
if verbose:
print 'writing to tempdir {}'.format(outdir)
elif python:
outdir = npzdir
elif matdir:
outdir = matdir
if feat == 'mel':
del config['features']
encoder = spectral.CubicMel(**config)
convert(files, outdir, encoder, force, verbose)
elif feat == 'mfcc':
del config['features']
encoder = spectral.MFCC(**config)
convert(files, outdir, encoder, force, verbose)
else:
if verbose:
print 'Delegation to GNU octave: ', feat
# from oct2py import Oct2Py, get_log
# oc = Oct2Py(logger=get_log())
# oc.logger = get_log('new_log')
# oc.logger.setLevel(logging.INFO)
oc = oct2py.Oct2Py()
if verbose:
print 'octave version', oc.version()
oc.addpath('./features_extraction',
'./features_extraction/ltfat',
'./features_extraction/amtoolbox',
'./features_extraction/jsonlab',
'./features_extraction/rastamat',
'./features_extraction/AuditoryToolbox')
oc.features(files, outdir, feat, config_file, force, verbose=verbose)
if verbose:
print 'Done with GNU octave'
if npzdir:
outdir2 = npzdir
else:
outdir2 = outdir
mat2npz(outdir, outdir2)
if h5file:
npz2h5features.convert(outdir, h5file)
if csvfile:
npz2csv.convert(outdir, csvfile)
except oct2py.Oct2PyError as err:
print err
finally:
if tmp:
shutil.rmtree(outdir)
if __name__ == '__main__':
main()