-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpythonrize.py
63 lines (50 loc) · 1.36 KB
/
pythonrize.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
import os, sys
import numpy as np
from scipy.io import wavfile
try:
import cPickle as pickle
except:
print 'cPickle not available'
import pickle
SOURCE_DIR = 'genres_processed'
def readGenre(path, genre):
path = os.path.join(path, genre)
fileNames = os.listdir(path)
ret = {}
print 'Reading: {0}'.format(genre)
stdout = sys.stdout
stdout.write('[' + '-' * 100 + ']')
progress = 0
for f in fileNames:
progress += 1
start, end = f.find('.'), f.find('_')
idx = int(f[start+1:end])
start, end = f.find('clip'), f.find('.wav')
clipNum = int(f[start+4:end])
p = int(float(progress) / len(fileNames) * 100)
s = ('#' * p).ljust(100, '-')
stdout.write('\r[{0}] {1}%'.format(s, p))
stdout.flush()
_, data = wavfile.read(os.path.join(path, f))
try:
ret[idx].append((clipNum, data))
except:
ret[idx] = [(clipNum, data)]
stdout.write('\n')
def extractArray(tup):
return tup[1]
for k in ret.keys():
arrays = sorted(ret[k], key=lambda x: x[0])
ret[k] = map(extractArray, arrays)
return ret
if __name__ == '__main__':
currPath = os.getcwd()
sourcePath = os.path.join(currPath, SOURCE_DIR)
genres = os.listdir(sourcePath)
data = {}
for g in genres:
data[g] = readGenre(sourcePath, g)
print 'Writing into file: data.in'
o = open('data.in', 'w')
pickle.dump(data, o)
o.close()