-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_mfccs.py
52 lines (43 loc) · 1.22 KB
/
get_mfccs.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
#!/usr/bin/python
"""get_mfccs.py
Extract raw MFCCs from a list of WAV files. Dumps data in JSON format.
Usage: $ python get_mfccs.py audiofile_1.wav audiofile_2.wav ... audiofile_n.wav
Use --help or -h to see this message.
"""
import sys
import getopt
import json
import scipy.io.wavfile as wav
from features import mfcc
def extract(audiofile):
(rate,sig) = wav.read(audiofile)
return (audiofile, mfcc(sig,rate).tolist())
def get_mfccs(files):
features = dict(map(extract, files))
print json.dumps(features)
return 0
# Boilerplate code below
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
"""Handle command line arguments"""
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "h", ["help"])
except getopt.error, msg:
raise Usage(msg)
# process options
for o, a in opts:
if o in ("-h", "--help"):
print __doc__
return 0
return get_mfccs(args)
except Usage, err:
print >>sys.stderr, err.msg
print >>sys.stderr, "for help use --help"
return 2
if __name__ == "__main__":
sys.exit(main())