Showing the changes to python (3 of 3 files). Show all
Unified · Split
| @@ -70,22 +70,121 @@ class peakpick: · 69 unmodified lines | ||
| 70 | 70 | del_aubio_peakpicker(self.pp) |
| 71 | 71 | |
| 72 | 72 | class onsetpick: |
| 73 | − def __init__(self,bufsize,hopsize,channels,myvec,threshold): | |
| 73 | + def __init__(self,bufsize,hopsize,channels,myvec,threshold,mode='dual'): | |
| 74 | 74 | self.myfft = cvec(bufsize,channels) |
| 75 | 75 | self.pv = pvoc(bufsize,hopsize,channels) |
| 76 | − self.myod = onsetdetection(hfc,bufsize,channels) | |
| 77 | − self.myod2 = onsetdetection(complexdomain,bufsize,channels) | |
| 78 | − self.myonset = fvec(1,channels) | |
| 79 | − self.myonset2 = fvec(1,channels) | |
| 76 | + if mode in [complexdomain,hfc,phase,energy,specdiff] : | |
| 77 | + self.myod = onsetdetection(mode,bufsize,channels) | |
| 78 | + self.myonset = fvec(1,channels) | |
| 79 | + else: | |
| 80 | + self.myod = onsetdetection(hfc,bufsize,channels) | |
| 81 | + self.myod2 = onsetdetection(complexdomain,bufsize,channels) | |
| 82 | + self.myonset = fvec(1,channels) | |
| 83 | + self.myonset2 = fvec(1,channels) | |
| 84 | + self.mode = mode | |
| 80 | 85 | self.pp = peakpick(float(threshold)) |
| 81 | 86 | |
| 82 | 87 | def do(self,myvec): |
| 83 | 88 | self.pv.do(myvec,self.myfft) |
| 84 | 89 | self.myod.do(self.myfft,self.myonset) |
| 85 | − self.myod2.do(self.myfft,self.myonset2) | |
| 86 | − self.myonset.set(self.myonset.get(0,0)*self.myonset2.get(0,0),0,0) | |
| 90 | + if self.mode == 'dual': | |
| 91 | + self.myod2.do(self.myfft,self.myonset2) | |
| 92 | + self.myonset.set(self.myonset.get(0,0)*self.myonset2.get(0,0),0,0) | |
| 87 | 93 | return self.pp.do(self.myonset),self.myonset.get(0,0) |
| 88 | 94 | |
| 95 | +def getonsetsfunc(filein,threshold,silence,bufsize=1024,hopsize=512,mode='dual'): | |
| 96 | + #bufsize = 1024 | |
| 97 | + #hopsize = bufsize/2 | |
| 98 | + frameread = 0 | |
| 99 | + filei = sndfile(filein) | |
| 100 | + channels = filei.channels() | |
| 101 | + myvec = fvec(hopsize,channels) | |
| 102 | + readsize = filei.read(hopsize,myvec) | |
| 103 | + opick = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode) | |
| 104 | + mylist = list() | |
| 105 | + #ovalist = [0., 0., 0., 0., 0., 0.] | |
| 106 | + ovalist = [0., 0., 0., 0., 0.] | |
| 107 | + ofunclist = [] | |
| 108 | + while(readsize): | |
| 109 | + readsize = filei.read(hopsize,myvec) | |
| 110 | + isonset,val = opick.do(myvec) | |
| 111 | + if (aubio_silence_detection(myvec(),silence)): | |
| 112 | + isonset=0 | |
| 113 | + ovalist.append(val) | |
| 114 | + ovalist.pop(0) | |
| 115 | + ofunclist.append(val) | |
| 116 | + if (isonset == 1): | |
| 117 | + i=len(ovalist)-1 | |
| 118 | + # find local minima before peak | |
| 119 | + while ovalist[i-1] < ovalist[i] and i > 0: | |
| 120 | + i -= 1 | |
| 121 | + now = (frameread+1-i) | |
| 122 | + if now > 0 : | |
| 123 | + mylist.append(now) | |
| 124 | + else: | |
| 125 | + now = 0 | |
| 126 | + mylist.append(now) | |
| 127 | + frameread += 1 | |
| 128 | + return mylist, ofunclist | |
| 129 | + | |
| 130 | + | |
| 131 | +def getonsetscausal(filein,threshold,silence,bufsize=1024,hopsize=512,mode='dual'): | |
| 132 | + frameread = 0 | |
| 133 | + filei = sndfile(filein) | |
| 134 | + channels = filei.channels() | |
| 135 | + myvec = fvec(hopsize,channels) | |
| 136 | + readsize = filei.read(hopsize,myvec) | |
| 137 | + opick = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode) | |
| 138 | + mylist = list() | |
| 139 | + while(readsize): | |
| 140 | + readsize = filei.read(hopsize,myvec) | |
| 141 | + isonset,val = opick.do(myvec) | |
| 142 | + if (aubio_silence_detection(myvec(),silence)): | |
| 143 | + isonset=0 | |
| 144 | + if (isonset == 1): | |
| 145 | + now = frameread | |
| 146 | + if now > 0 : | |
| 147 | + mylist.append(now) | |
| 148 | + else: | |
| 149 | + now = 0 | |
| 150 | + mylist.append(now) | |
| 151 | + frameread += 1 | |
| 152 | + return mylist | |
| 153 | + | |
| 154 | + | |
| 155 | +def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512,mode='dual'): | |
| 156 | + frameread = 0 | |
| 157 | + filei = sndfile(filein) | |
| 158 | + channels = filei.channels() | |
| 159 | + samplerate= filei.samplerate() | |
| 160 | + myvec = fvec(hopsize,channels) | |
| 161 | + readsize = filei.read(hopsize,myvec) | |
| 162 | + opick = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode) | |
| 163 | + mylist = list() | |
| 164 | + #ovalist = [0., 0., 0., 0., 0., 0.] | |
| 165 | + ovalist = [0., 0., 0., 0., 0.] | |
| 166 | + while(readsize): | |
| 167 | + readsize = filei.read(hopsize,myvec) | |
| 168 | + isonset,val = opick.do(myvec) | |
| 169 | + if (aubio_silence_detection(myvec(),silence)): | |
| 170 | + isonset=0 | |
| 171 | + ovalist.append(val) | |
| 172 | + ovalist.pop(0) | |
| 173 | + if (isonset == 1): | |
| 174 | + i=len(ovalist)-1 | |
| 175 | + # find local minima before peak | |
| 176 | + while ovalist[i-1] < ovalist[i] and i > 0: | |
| 177 | + i -= 1 | |
| 178 | + now = (frameread+1-i) | |
| 179 | + if now > 0 : | |
| 180 | + mylist.append(now) | |
| 181 | + else: | |
| 182 | + now = 0 | |
| 183 | + mylist.append(now) | |
| 184 | + frameread += 1 | |
| 185 | + return mylist | |
| 186 | + | |
| 187 | + | |
| 89 | 188 | class pitchpick: |
| 90 | 189 | def __init__(self,bufsize,hopsize,channels,myvec,srate): |
| 91 | 190 | self.myfft = cvec(bufsize,channels) |
| @@ -10,38 +10,13 @@ import sys · 9 unmodified lines | ||
| 10 | 10 | bufsize = 1024 |
| 11 | 11 | hopsize = bufsize/2 |
| 12 | 12 | |
| 13 | −def getonsets(filein,threshold): | |
| 14 | − frameread = 0 | |
| 15 | − filei = sndfile(filein) | |
| 16 | − srate = filei.samplerate() | |
| 17 | − channels = filei.channels() | |
| 18 | − myvec = fvec(hopsize,channels) | |
| 19 | − readsize = filei.read(hopsize,myvec) | |
| 20 | − opick = onsetpick(bufsize,hopsize,channels,myvec,threshold) | |
| 21 | − #newname = "%s%.8f%s" % ("/tmp/",0.0000000,filein[-4:]) | |
| 22 | − #fileo = sndfile(newname,model=filei) | |
| 23 | − mylist = list() | |
| 24 | − ovalist = [0., 0., 0., 0., 0., 0.] | |
| 25 | − while(readsize==hopsize): | |
| 26 | − readsize = filei.read(hopsize,myvec) | |
| 27 | − isonset,val = opick.do(myvec) | |
| 28 | − ovalist.append(val) | |
| 29 | − ovalist.pop(0) | |
| 30 | − if (isonset == 1): | |
| 31 | − i=len(ovalist)-1 | |
| 32 | − # find local minima | |
| 33 | − while ovalist[i-1] < ovalist[i] and i > 0: | |
| 34 | − i -= 1 | |
| 35 | − now = (frameread+1-i)*hopsize/(srate+0.) | |
| 36 | − mylist.append(now) | |
| 37 | − frameread += 1 | |
| 38 | − return mylist | |
| 39 | 13 | |
| 40 | 14 | def cutfile(filein,onsets): |
| 41 | 15 | frameread = 0 |
| 16 | + zerothres = 0.002 | |
| 42 | 17 | readsize = hopsize |
| 43 | 18 | filei = sndfile(filein) |
| 44 | − srate = filei.samplerate() | |
| 19 | + framestep = hopsize/(filei.samplerate()+0.) | |
| 45 | 20 | channels = filei.channels() |
| 46 | 21 | newname = "%s%f%s" % ("/tmp/",0.0000000,filein[-4:]) |
| 47 | 22 | fileo = sndfile(newname,model=filei) |
| @@ -49,13 +24,12 @@ def cutfile(filein,onsets): · 1 unmodified line | ||
| 49 | 24 | mycopy = fvec(hopsize,channels) |
| 50 | 25 | while(readsize==hopsize): |
| 51 | 26 | readsize = filei.read(hopsize,myvec) |
| 52 | − now = (frameread)*hopsize/(srate+0.) | |
| 53 | 27 | # write to current file |
| 54 | − if len(onsets) and now >= onsets[0]: | |
| 28 | + if len(onsets) and frameread >= onsets[0]: | |
| 55 | 29 | onsets.pop(0) |
| 56 | 30 | # write up to 1st zero crossing |
| 57 | 31 | zerocross = 0 |
| 58 | − while ( abs( myvec.get(zerocross,0) ) > 0.002 ): | |
| 32 | + while ( abs( myvec.get(zerocross,0) ) > zerothres ): | |
| 59 | 33 | zerocross += 1 |
| 60 | 34 | writesize = fileo.write(zerocross,myvec) |
| 61 | 35 | fromcross = 0 |
| @@ -66,8 +40,8 @@ def cutfile(filein,onsets): · 4 unmodified lines | ||
| 66 | 40 | zerocross += 1 |
| 67 | 41 | del fileo |
| 68 | 42 | fileo = sndfile("%s%s%f%s%s" % |
| 69 | − (filein.split(".")[0].split("/")[-1],".",now,".",filein.split(".")[-1]),model=filei) | |
| 70 | − # write after 1st zero crossing to new file | |
| 43 | + (filein.split(".")[0].split("/")[-1],".", | |
| 44 | + frameread*framestep,".",filein.split(".")[-1]),model=filei) | |
| 71 | 45 | writesize = fileo.write(fromcross,mycopy) |
| 72 | 46 | else: |
| 73 | 47 | writesize = fileo.write(readsize,myvec) |
| @@ -1,25 +1,52 @@ | ||
| 1 | −#!/usr/bin/python | |
| 1 | +#! /usr/bin/python | |
| 2 | 2 | |
| 3 | −def do(filein,threshold): | |
| 4 | − import aubio.aubioclass as aubio | |
| 5 | − hopsize = 512 | |
| 6 | − bufsize = 1024 | |
| 7 | − frameread = 0 | |
| 8 | − filei = aubio.sndfile(filein) | |
| 9 | − srate = filei.samplerate() | |
| 10 | − channels = filei.channels() | |
| 11 | − myvec = aubio.fvec(hopsize,channels) | |
| 12 | − readsize = filei.read(hopsize,myvec) | |
| 13 | − opick = aubio.onsetpick(bufsize,hopsize,channels,myvec,threshold) | |
| 14 | − mylist = list() | |
| 15 | − while(readsize==hopsize): | |
| 16 | − readsize = filei.read(hopsize,myvec) | |
| 17 | − isonset,val = opick.do(myvec) | |
| 18 | − if (isonset == 1): | |
| 19 | − now = (frameread-3)*hopsize/(srate+0.) | |
| 20 | − print "%.8f\t%.2f"% (now,val) | |
| 21 | − frameread += 1 | |
| 3 | +import sys | |
| 4 | +import numarray | |
| 5 | +from aubio.aubioclass import * | |
| 22 | 6 | |
| 23 | −if __name__ == "__main__": | |
| 24 | − import sys | |
| 25 | − do(sys.argv[1],sys.argv[2]) | |
| 7 | +usage = "usage: %s [options] soundfile" % sys.argv[0] | |
| 8 | + | |
| 9 | +def parse_args(): | |
| 10 | + from optparse import OptionParser | |
| 11 | + parser = OptionParser(usage=usage) | |
| 12 | + parser.add_option("-v","--verbose", | |
| 13 | + action="store_true", dest="verbose", default=False, | |
| 14 | + help="make lots of noise") | |
| 15 | + parser.add_option("-q","--quiet", | |
| 16 | + action="store_false", dest="verbose", default=True, | |
| 17 | + help="be quiet [default]") | |
| 18 | + parser.add_option("-t","--threshold", | |
| 19 | + action="store", dest="threshold", default=0.3, | |
| 20 | + help="onset detection threshold [default=0.3]") | |
| 21 | + parser.add_option("-s","--silence", | |
| 22 | + action="store", dest="silence", default=-70, | |
| 23 | + help="silence [default=-70]") | |
| 24 | + def check_mode(option, opt, value, parser): | |
| 25 | + nvalue = parser.rargs[0] | |
| 26 | + if nvalue == 'complexdomain' : setattr(parser.values, option.dest, complexdomain) | |
| 27 | + elif nvalue == 'hfc' : setattr(parser.values, option.dest, hfc) | |
| 28 | + elif nvalue == 'phase' : setattr(parser.values, option.dest, phase) | |
| 29 | + elif nvalue == 'specdiff' : setattr(parser.values, option.dest, specdiff) | |
| 30 | + elif nvalue == 'energy' : setattr(parser.values, option.dest, energy) | |
| 31 | + elif nvalue == 'dual' : setattr(parser.values, option.dest, 'dual') | |
| 32 | + parser.add_option("-m","--mode", | |
| 33 | + action="callback", callback=check_mode, dest="mode", default='dual', | |
| 34 | + help="onsetdetection mode [default=dual]") | |
| 35 | + parser.add_option("-o","--outplot", | |
| 36 | + action="store", dest="outplot", default=None, | |
| 37 | + help="be quiet [default=None]") | |
| 38 | + (options, args) = parser.parse_args() | |
| 39 | + if not len(args): | |
| 40 | + print "no file name given\n", usage | |
| 41 | + sys.exit(1) | |
| 42 | + return options, args | |
| 43 | + | |
| 44 | +options, args = parse_args() | |
| 45 | + | |
| 46 | +filename = args[0] | |
| 47 | +threshold = float(options.threshold) | |
| 48 | +silence = float(options.silence) | |
| 49 | + | |
| 50 | +#onsets = getonsets(filename,threshold,silence,mode=options.mode) | |
| 51 | +onsets = getonsetscausal(filename,threshold,silence,mode=options.mode) | |
| 52 | +for i in onsets: print i*512./44100. | |