-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfeatures.py
More file actions
99 lines (71 loc) · 2.62 KB
/
features.py
File metadata and controls
99 lines (71 loc) · 2.62 KB
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
import librosa
import numpy
from numpy import genfromtxt
import matplotlib.pyplot as plt
import sys
#/private/etc/apache2/httpd.conf: uncomment #loadmodule cgi
#Library/WebServer/Documents"
#sudo apachectl start (or restart)
#python -m SimpleHTTPServer 8000
#python -W ignore b.py
#brew install ffmpeg, pip install librosa
#from pyspark.mllib.tree import RandomForest, RandomForestModel
#from pyspark.mllib.util import MLUtils
numpy.set_printoptions(threshold=numpy.nan)
#y, sr = librosa.load("hello.mp3")
#filename = librosa.util.example_audio_file()
#print(filename);
chordList1 = ["N","C:maj","C:min","C#:maj","C#:min","D:maj","D:min","D#:maj","D#:min","E:maj","E:min","F:maj","F:min","F#:maj","F#:min","G:maj","G:min","G#:maj","G#min","A:maj","A:min","A#:maj", "A#:min", "B:maj","B:min"];
chordList2 = ["N","C:maj","C:min","Db:maj","Db:min","D:maj","D:min","Eb:maj","Eb:min","E:maj","E:min","F:maj","F:min","Gb:maj","Gb:min","G:maj","G:min","Ab:maj","Ab:min","A:maj","A:min","Bb:maj","Bb:min","B:maj","B:min"];
import glob
filelist = glob.glob("music/*.lab")
#filelist = ['music/b1.2']
for file in filelist:
file = file.replace(".lab","")
if '.' in file:
delim = ' '
else:
delim = '\t'
labellist = [];
f = open(file+".lab", 'r')
for line in f:
#print line.rstrip().split(' ')[2]
try:
labellist.append(line.rstrip().split(delim)[2]);
except:
a=1
arr = []
arr = genfromtxt(file+".lab", delimiter=delim)
r, c = arr.shape;
for i in range(0, r):
try:
off = arr[i,0]
dur =arr[i,1]-arr[i,0]
y, sr = librosa.load(file+".ogg",sr=22000,offset=off,duration=dur)
y_harmonic, y_percussive = librosa.effects.hpss(y)
chroma = librosa.feature.chroma_stft(y_harmonic,sr)
if i==-1:
librosa.display.specshow(chroma, y_axis='chroma', x_axis='time')
plt.show()
#plt.savefig("temp.png")
exit()
cr,cc = chroma.shape;
try:
sys.stdout.write(str(chordList1.index(labellist[i])));
except:
try:
sys.stdout.write(str(chordList2.index(labellist[i])));
except:
try:
sys.stdout.write(str(chordList1.index(labellist[i]+":maj")));
except:
sys.stdout.write(str(chordList2.index(labellist[i]+":maj")));
for j in range(0, cr):
sys.stdout.write(" ")
sys.stdout.write(str(j+1))
sys.stdout.write(":")
#print(j+1,end=":")
sys.stdout.write(str(numpy.mean(chroma[j])))
sys.stdout.write("\n");
except:
one=1