1 | from aubioclass import * |
---|
2 | |
---|
3 | def get_onset_mode(nvalue): |
---|
4 | """ utility function to convert a string to aubio_onsetdetection_type """ |
---|
5 | if nvalue == 'complexdomain' or nvalue == 'complex' : |
---|
6 | return aubio_onset_complex |
---|
7 | elif nvalue == 'hfc' : |
---|
8 | return aubio_onset_hfc |
---|
9 | elif nvalue == 'phase' : |
---|
10 | return aubio_onset_phase |
---|
11 | elif nvalue == 'specdiff' : |
---|
12 | return aubio_onset_specdiff |
---|
13 | elif nvalue == 'energy' : |
---|
14 | return aubio_onset_energy |
---|
15 | elif nvalue == 'kl' : |
---|
16 | return aubio_onset_kl |
---|
17 | elif nvalue == 'mkl' : |
---|
18 | return aubio_onset_mkl |
---|
19 | elif nvalue == 'dual' : |
---|
20 | return 'dual' |
---|
21 | else: |
---|
22 | import sys |
---|
23 | print "unknown onset detection function selected" |
---|
24 | sys.exit(1) |
---|
25 | |
---|
26 | def check_onset_mode(option, opt, value, parser): |
---|
27 | """ wrapper function to convert a list of modes to |
---|
28 | aubio_onsetdetection_type """ |
---|
29 | nvalues = parser.rargs[0].split(',') |
---|
30 | val = [] |
---|
31 | for nvalue in nvalues: |
---|
32 | val.append(get_onset_mode(nvalue)) |
---|
33 | setattr(parser.values, option.dest, val) |
---|
34 | |
---|
35 | def check_pitch_mode(option, opt, value, parser): |
---|
36 | """ utility function to convert a string to aubio_pitchdetection_type""" |
---|
37 | nvalues = parser.rargs[0].split(',') |
---|
38 | val = [] |
---|
39 | for nvalue in nvalues: |
---|
40 | if nvalue == 'mcomb' : |
---|
41 | val.append(aubio_pitch_mcomb) |
---|
42 | elif nvalue == 'yin' : |
---|
43 | val.append(aubio_pitch_yin) |
---|
44 | elif nvalue == 'fcomb' : |
---|
45 | val.append(aubio_pitch_fcomb) |
---|
46 | elif nvalue == 'schmitt': |
---|
47 | val.append(aubio_pitch_schmitt) |
---|
48 | else: |
---|
49 | import sys |
---|
50 | print "error: unknown pitch detection function selected" |
---|
51 | sys.exit(1) |
---|
52 | setattr(parser.values, option.dest, val) |
---|
53 | |
---|
54 | def check_pitchm_mode(option, opt, value, parser): |
---|
55 | """ utility function to convert a string to aubio_pitchdetection_mode """ |
---|
56 | nvalue = parser.rargs[0] |
---|
57 | if nvalue == 'freq' : |
---|
58 | setattr(parser.values, option.dest, aubio_pitchm_freq) |
---|
59 | elif nvalue == 'midi' : |
---|
60 | setattr(parser.values, option.dest, aubio_pitchm_midi) |
---|
61 | elif nvalue == 'cent' : |
---|
62 | setattr(parser.values, option.dest, aubio_pitchm_cent) |
---|
63 | elif nvalue == 'bin' : |
---|
64 | setattr(parser.values, option.dest, aubio_pitchm_bin) |
---|
65 | else: |
---|
66 | import sys |
---|
67 | print "error: unknown pitch detection output selected" |
---|
68 | sys.exit(1) |
---|
69 | |
---|
70 | |
---|
71 | def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512, |
---|
72 | mode='dual',localmin=False,storefunc=False,derivate=False): |
---|
73 | frameread = 0 |
---|
74 | filei = sndfile(filein) |
---|
75 | channels = filei.channels() |
---|
76 | myvec = fvec(hopsize,channels) |
---|
77 | readsize = filei.read(hopsize,myvec) |
---|
78 | opick = onsetpick(bufsize,hopsize,channels,myvec,threshold, |
---|
79 | mode=mode,derivate=derivate) |
---|
80 | mylist = list() |
---|
81 | if localmin: |
---|
82 | ovalist = [0., 0., 0., 0., 0.] |
---|
83 | ofunclist = [] |
---|
84 | while(readsize): |
---|
85 | readsize = filei.read(hopsize,myvec) |
---|
86 | isonset,val = opick.do(myvec) |
---|
87 | if (aubio_silence_detection(myvec(),silence)): |
---|
88 | isonset=0 |
---|
89 | if localmin: |
---|
90 | if val > 0: ovalist.append(val) |
---|
91 | else: ovalist.append(0) |
---|
92 | ovalist.pop(0) |
---|
93 | if storefunc: |
---|
94 | ofunclist.append(val) |
---|
95 | if (isonset == 1): |
---|
96 | if localmin: |
---|
97 | i=len(ovalist)-1 |
---|
98 | # find local minima before peak |
---|
99 | while ovalist[i-1] < ovalist[i] and i > 0: |
---|
100 | i -= 1 |
---|
101 | now = (frameread+1-i) |
---|
102 | else: |
---|
103 | now = frameread |
---|
104 | if now > 0 : |
---|
105 | mylist.append(now) |
---|
106 | else: |
---|
107 | now = 0 |
---|
108 | mylist.append(now) |
---|
109 | frameread += 1 |
---|
110 | return mylist, ofunclist |
---|
111 | |
---|
112 | def cutfile(filein,slicetimes,zerothres=0.008,bufsize=1024,hopsize=512): |
---|
113 | frameread = 0 |
---|
114 | readsize = hopsize |
---|
115 | filei = sndfile(filein) |
---|
116 | framestep = hopsize/(filei.samplerate()+0.) |
---|
117 | channels = filei.channels() |
---|
118 | newname = "%s%s%09.5f%s%s" % (filein.split(".")[0].split("/")[-1],".", |
---|
119 | frameread*framestep,".",filein.split(".")[-1]) |
---|
120 | fileo = sndfile(newname,model=filei) |
---|
121 | myvec = fvec(hopsize,channels) |
---|
122 | mycopy = fvec(hopsize,channels) |
---|
123 | while(readsize==hopsize): |
---|
124 | readsize = filei.read(hopsize,myvec) |
---|
125 | # write to current file |
---|
126 | if len(slicetimes) and frameread >= slicetimes[0]: |
---|
127 | slicetimes.pop(0) |
---|
128 | # write up to 1st zero crossing |
---|
129 | zerocross = 0 |
---|
130 | while ( abs( myvec.get(zerocross,0) ) > zerothres ): |
---|
131 | zerocross += 1 |
---|
132 | writesize = fileo.write(zerocross,myvec) |
---|
133 | fromcross = 0 |
---|
134 | while (zerocross < readsize): |
---|
135 | for i in range(channels): |
---|
136 | mycopy.set(myvec.get(zerocross,i),fromcross,i) |
---|
137 | fromcross += 1 |
---|
138 | zerocross += 1 |
---|
139 | del fileo |
---|
140 | fileo = sndfile("%s%s%09.5f%s%s" % |
---|
141 | (filein.split(".")[0].split("/")[-1],".", |
---|
142 | frameread*framestep,".",filein.split(".")[-1]),model=filei) |
---|
143 | writesize = fileo.write(fromcross,mycopy) |
---|
144 | else: |
---|
145 | writesize = fileo.write(readsize,myvec) |
---|
146 | frameread += 1 |
---|
147 | del fileo |
---|
148 | |
---|
149 | |
---|
150 | def getsilences(filein,hopsize=512,silence=-70): |
---|
151 | frameread = 0 |
---|
152 | filei = sndfile(filein) |
---|
153 | srate = filei.samplerate() |
---|
154 | channels = filei.channels() |
---|
155 | myvec = fvec(hopsize,channels) |
---|
156 | readsize = filei.read(hopsize,myvec) |
---|
157 | mylist = [] |
---|
158 | wassilence = 0 |
---|
159 | while(readsize==hopsize): |
---|
160 | readsize = filei.read(hopsize,myvec) |
---|
161 | if (aubio_silence_detection(myvec(),silence)==1): |
---|
162 | if wassilence == 0: |
---|
163 | mylist.append(frameread) |
---|
164 | wassilence == 1 |
---|
165 | else: wassilence = 0 |
---|
166 | frameread += 1 |
---|
167 | return mylist |
---|
168 | |
---|
169 | def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_pitchm_freq, |
---|
170 | samplerate=44100.,silence=-70): |
---|
171 | frameread = 0 |
---|
172 | filei = sndfile(filein) |
---|
173 | srate = filei.samplerate() |
---|
174 | channels = filei.channels() |
---|
175 | myvec = fvec(hopsize,channels) |
---|
176 | readsize = filei.read(hopsize,myvec) |
---|
177 | pitchdet = pitchdetection(mode=mode,bufsize=bufsize,hopsize=hopsize, |
---|
178 | channels=channels,samplerate=srate,omode=omode) |
---|
179 | mylist = [] |
---|
180 | while(readsize==hopsize): |
---|
181 | readsize = filei.read(hopsize,myvec) |
---|
182 | freq = pitchdet(myvec) |
---|
183 | #print "%.3f %.2f" % (now,freq) |
---|
184 | if (aubio_silence_detection(myvec(),silence)!=1): |
---|
185 | mylist.append(freq) |
---|
186 | else: |
---|
187 | mylist.append(-1.) |
---|
188 | frameread += 1 |
---|
189 | return mylist |
---|
190 | |
---|