#!/usr/bin/python """ this file was written by Paul Brossier it is released under the GNU/GPL license. """ from aubio.aubioclass import * import sys bufsize = 1024 hopsize = bufsize/2 def getonsets(filein,threshold): frameread = 0 filei = sndfile(filein) srate = filei.samplerate() channels = filei.channels() myvec = fvec(hopsize,channels) readsize = filei.read(hopsize,myvec) opick = onsetpick(bufsize,hopsize,channels,myvec,threshold) #newname = "%s%.8f%s" % ("/tmp/",0.0000000,filein[-4:]) #fileo = sndfile(newname,model=filei) mylist = list() ovalist = [0., 0., 0., 0., 0., 0.] while(readsize==hopsize): readsize = filei.read(hopsize,myvec) isonset,val = opick.do(myvec) ovalist.append(val) ovalist.pop(0) if (isonset == 1): i=len(ovalist)-1 # find local minima while ovalist[i-1] < ovalist[i] and i > 0: i -= 1 now = (frameread+1-i)*hopsize/(srate+0.) mylist.append(now) frameread += 1 return mylist def cutfile(filein,onsets): frameread = 0 readsize = hopsize filei = sndfile(filein) srate = filei.samplerate() channels = filei.channels() newname = "%s%f%s" % ("/tmp/",0.0000000,filein[-4:]) fileo = sndfile(newname,model=filei) myvec = fvec(hopsize,channels) mycopy = fvec(hopsize,channels) while(readsize==hopsize): readsize = filei.read(hopsize,myvec) now = (frameread)*hopsize/(srate+0.) # write to current file if len(onsets) and now >= onsets[0]: onsets.pop(0) # write up to 1st zero crossing zerocross = 0 while ( abs( myvec.get(zerocross,0) ) > 0.002 ): zerocross += 1 writesize = fileo.write(zerocross,myvec) fromcross = 0 while (zerocross < readsize): for i in range(channels): mycopy.set(myvec.get(zerocross,i),fromcross,i) fromcross += 1 zerocross += 1 del fileo fileo = sndfile("%s%s%f%s%s" % (filein.split(".")[0].split("/")[-1],".",now,".",filein.split(".")[-1]),model=filei) # write after 1st zero crossing to new file writesize = fileo.write(fromcross,mycopy) else: writesize = fileo.write(readsize,myvec) frameread += 1 del fileo filename = sys.argv[1] threshold = sys.argv[2] onsets = getonsets(filename,threshold) cutfile(filename,onsets)