- Timestamp:
- May 28, 2005, 11:06:33 PM (20 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
- Children:
- 6b384f3
- Parents:
- ead2920
- Location:
- python
- Files:
-
- 1 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/aubio/aubioclass.py
read2920 r98df9f4 96 96 return self.pp.do(self.myonset),self.myonset.get(0,0) 97 97 98 def getonsetsfunc(filein,threshold,silence,bufsize=1024,hopsize=512,mode='dual'): 99 #bufsize = 1024 100 #hopsize = bufsize/2 98 def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512,mode='dual',localmin=False,storefunc=False): 101 99 frameread = 0 102 100 filei = sndfile(filein) … … 106 104 opick = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode) 107 105 mylist = list() 108 #ovalist = [0., 0., 0., 0., 0., 0.] 109 ovalist = [0., 0., 0., 0., 0.] 110 ofunclist = [] 106 if localmin: 107 ovalist = [0., 0., 0., 0., 0.] 108 if storefunc: 109 ofunclist = [] 111 110 while(readsize): 112 111 readsize = filei.read(hopsize,myvec) … … 114 113 if (aubio_silence_detection(myvec(),silence)): 115 114 isonset=0 116 ovalist.append(val) 117 ovalist.pop(0) 118 ofunclist.append(val) 115 if localmin: 116 ovalist.append(val) 117 ovalist.pop(0) 118 if storefunc: 119 ofunclist.append(val) 119 120 if (isonset == 1): 120 i=len(ovalist)-1 121 # find local minima before peak 122 while ovalist[i-1] < ovalist[i] and i > 0: 123 i -= 1 124 now = (frameread+1-i) 121 if localmin: 122 i=len(ovalist)-1 123 # find local minima before peak 124 while ovalist[i-1] < ovalist[i] and i > 0: 125 i -= 1 126 now = (frameread+1-i) 127 else: 128 now = frameread 125 129 if now > 0 : 126 130 mylist.append(now) … … 129 133 mylist.append(now) 130 134 frameread += 1 131 return mylist, ofunclist 135 if storefunc: return mylist, ofunclist 136 else: return mylist 132 137 138 def cutfile(filein,slicetimes,zerothres=0.002,bufsize=1024,hopsize=512): 139 frameread = 0 140 readsize = hopsize 141 filei = sndfile(filein) 142 framestep = hopsize/(filei.samplerate()+0.) 143 channels = filei.channels() 144 newname = "%s%f%s" % ("/tmp/",0.0000000,filein[-4:]) 145 fileo = sndfile(newname,model=filei) 146 myvec = fvec(hopsize,channels) 147 mycopy = fvec(hopsize,channels) 148 while(readsize==hopsize): 149 readsize = filei.read(hopsize,myvec) 150 # write to current file 151 if len(slicetimes) and frameread >= slicetimes[0]: 152 slicetimes.pop(0) 153 # write up to 1st zero crossing 154 zerocross = 0 155 while ( abs( myvec.get(zerocross,0) ) > zerothres ): 156 zerocross += 1 157 writesize = fileo.write(zerocross,myvec) 158 fromcross = 0 159 while (zerocross < readsize): 160 for i in range(channels): 161 mycopy.set(myvec.get(zerocross,i),fromcross,i) 162 fromcross += 1 163 zerocross += 1 164 del fileo 165 fileo = sndfile("%s%s%f%s%s" % 166 (filein.split(".")[0].split("/")[-1],".", 167 frameread*framestep,".",filein.split(".")[-1]),model=filei) 168 writesize = fileo.write(fromcross,mycopy) 169 else: 170 writesize = fileo.write(readsize,myvec) 171 frameread += 1 172 del fileo 133 173 134 def getonsetscausal(filein,threshold,silence,bufsize=1024,hopsize=512,mode='dual'):135 frameread = 0136 filei = sndfile(filein)137 channels = filei.channels()138 myvec = fvec(hopsize,channels)139 readsize = filei.read(hopsize,myvec)140 opick = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode)141 mylist = list()142 while(readsize):143 readsize = filei.read(hopsize,myvec)144 isonset,val = opick.do(myvec)145 if (aubio_silence_detection(myvec(),silence)):146 isonset=0147 if (isonset == 1):148 now = frameread149 if now > 0 :150 mylist.append(now)151 else:152 now = 0153 mylist.append(now)154 frameread += 1155 return mylist156 157 def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512,mode='dual'):158 frameread = 0159 filei = sndfile(filein)160 channels = filei.channels()161 samplerate= filei.samplerate()162 myvec = fvec(hopsize,channels)163 readsize = filei.read(hopsize,myvec)164 opick = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode)165 mylist = list()166 #ovalist = [0., 0., 0., 0., 0., 0.]167 ovalist = [0., 0., 0., 0., 0.]168 while(readsize):169 readsize = filei.read(hopsize,myvec)170 isonset,val = opick.do(myvec)171 if (aubio_silence_detection(myvec(),silence)):172 isonset=0173 ovalist.append(val)174 ovalist.pop(0)175 if (isonset == 1):176 i=len(ovalist)-1177 # find local minima before peak178 while ovalist[i-1] < ovalist[i] and i > 0:179 i -= 1180 now = (frameread+1-i)181 if now > 0 :182 mylist.append(now)183 else:184 now = 0185 mylist.append(now)186 frameread += 1187 return mylist188 174 189 175 class pitchpick: -
python/aubiocut
read2920 r98df9f4 1 #! /usr/bin/python1 #! /usr/bin/python 2 2 3 3 """ this file was written by Paul Brossier … … 5 5 """ 6 6 7 import sys 8 import numarray 7 9 from aubio.aubioclass import * 8 10 9 bufsize = 1024 10 hopsize = bufsize/2 11 usage = "usage: %s [options] -i soundfile" % sys.argv[0] 11 12 12 def cutfile(filein,slicetimes,zerothres=0.002): 13 frameread = 0 14 readsize = hopsize 15 filei = sndfile(filein) 16 framestep = hopsize/(filei.samplerate()+0.) 17 channels = filei.channels() 18 newname = "%s%f%s" % ("/tmp/",0.0000000,filein[-4:]) 19 fileo = sndfile(newname,model=filei) 20 myvec = fvec(hopsize,channels) 21 mycopy = fvec(hopsize,channels) 22 while(readsize==hopsize): 23 readsize = filei.read(hopsize,myvec) 24 # write to current file 25 if len(slicetimes) and frameread >= slicetimes[0]: 26 slicetimes.pop(0) 27 # write up to 1st zero crossing 28 zerocross = 0 29 while ( abs( myvec.get(zerocross,0) ) > zerothres ): 30 zerocross += 1 31 writesize = fileo.write(zerocross,myvec) 32 fromcross = 0 33 while (zerocross < readsize): 34 for i in range(channels): 35 mycopy.set(myvec.get(zerocross,i),fromcross,i) 36 fromcross += 1 37 zerocross += 1 38 del fileo 39 fileo = sndfile("%s%s%f%s%s" % 40 (filein.split(".")[0].split("/")[-1],".", 41 frameread*framestep,".",filein.split(".")[-1]),model=filei) 42 writesize = fileo.write(fromcross,mycopy) 43 else: 44 writesize = fileo.write(readsize,myvec) 45 frameread += 1 46 del fileo 13 def check_mode(option, opt, value, parser): 14 nvalue = parser.rargs[0] 15 if nvalue == 'complexdomain' : 16 setattr(parser.values, option.dest, complexdomain) 17 elif nvalue == 'hfc' : 18 setattr(parser.values, option.dest, hfc) 19 elif nvalue == 'phase' : 20 setattr(parser.values, option.dest, phase) 21 elif nvalue == 'specdiff' : 22 setattr(parser.values, option.dest, specdiff) 23 elif nvalue == 'energy' : 24 setattr(parser.values, option.dest, energy) 25 elif nvalue == 'dual' : 26 setattr(parser.values, option.dest, 'dual') 47 27 48 import sys 49 filename = sys.argv[1] 50 threshold = 0.2 51 if (len(sys.argv) > 2): threshold = sys.argv[2] 52 onsets = getonsets(filename,threshold) 53 cutfile(filename,onsets) 28 def parse_args(): 29 from optparse import OptionParser 30 parser = OptionParser(usage=usage) 31 parser.add_option("-i","--input", 32 action="store", dest="filename", 33 help="input sound file") 34 parser.add_option("-m","--mode", action="callback", 35 callback=check_mode, dest="mode", default='dual', 36 help="onsetdetection mode [default=dual] \ 37 complexdomain|hfc|phase|specdiff|energy|dual") 38 parser.add_option("-B","--bufsize", 39 action="store", dest="bufsize", default=1024, 40 help="buffer size [default=1024]") 41 parser.add_option("-H","--hopsize", 42 action="store", dest="hopsize", default=512, 43 help="overlap size [default=512]") 44 parser.add_option("-t","--threshold", 45 action="store", dest="threshold", default=0.3, 46 help="onset peak picking threshold [default=0.3]") 47 parser.add_option("-s","--silence", 48 action="store", dest="silence", default=-70, 49 help="silence threshold [default=-70]") 50 parser.add_option("-M","--mintol", 51 action="store", dest="mintol", default=0.048, 52 help="minimum inter onset interval [default=0.048]") 53 parser.add_option("-D","--delay", 54 action="store", dest="delay", default=0.022, 55 help="number of seconds to take back [default=0.022]") 56 parser.add_option("-L","--localmin", 57 action="store_true", dest="localmin", default=False, 58 help="use local minima after peak detection") 59 parser.add_option("-c","--cut", 60 action="store_true", dest="cut", default=False, 61 help="cut input sound file at detected labels \ 62 best used with option -L") 63 # to be implemented 64 # plotting functions 65 parser.add_option("-d","--derivative", 66 action="store_true", dest="derivative", default=False, 67 help="NOT IMPLEMENTED derivate onset detection function") 68 parser.add_option("-p","--plot", 69 action="store_true", dest="doplot", default=False, 70 help="NOT IMPLEMENTED draw plot") 71 parser.add_option("-O","--outplot", 72 action="store", dest="output-plot", default=None, 73 help="NOT IMPLEMENTED save plot to output.{ps,png}") 74 parser.add_option("-z","--zerocross", 75 action="store_true", dest="zerocross", default=False, 76 help="NOT IMPLEMENTED zero crossing matching") 77 parser.add_option("-b","--beat", 78 action="store_true", dest="beat", default=False, 79 help="NOT IMPLEMENTED output beat locations") 80 parser.add_option("-v","--verbose", 81 action="store_true", dest="verbose", default=False, 82 help="make lots of noise [default]") 83 parser.add_option("-q","--quiet", 84 action="store_false", dest="verbose", default=False, 85 help="be quiet") 86 (options, args) = parser.parse_args() 87 if not options.filename: 88 print "no file name given\n", usage 89 sys.exit(1) 90 return options, args 91 92 options, args = parse_args() 93 94 filename = options.filename 95 samplerate = float(sndfile(filename).samplerate()) 96 hopsize = options.hopsize 97 bufsize = options.bufsize 98 threshold = float(options.threshold) 99 silence = float(options.silence) 100 mintol = float(options.mintol)*samplerate/hopsize 101 delay = float(options.delay) 102 103 if options.beat: 104 #onsets = getbeats(filename,threshold,silence,mode=options.mode) 105 exit("not implemented yet") 106 else: 107 onsets = getonsets(filename,threshold,silence, 108 mode=options.mode,localmin=options.localmin, 109 bufsize=bufsize,hopsize=hopsize) 110 111 # take back system delay 112 if delay != 0: 113 for i in range(len(onsets)): 114 onsets[i] -= delay*samplerate/hopsize 115 116 # prune doubled 117 if mintol > 0: 118 last = -2*mintol 119 newonsets = [] 120 for new in onsets: 121 if (new - last > mintol): 122 newonsets.append(new) 123 last = new 124 onsets = newonsets 125 126 # print times in second 127 if options.verbose: 128 for i in onsets: print "%f" % (i*hopsize/samplerate) 129 130 if options.cut: 131 cutfile(filename,onsets,bufsize=bufsize,hopsize=hopsize)
Note: See TracChangeset
for help on using the changeset viewer.