[96fb8ad] | 1 | #!/usr/bin/python |
---|
| 2 | |
---|
| 3 | """ this file was written by Paul Brossier |
---|
| 4 | it is released under the GNU/GPL license. |
---|
| 5 | """ |
---|
| 6 | |
---|
| 7 | from aubio.aubioclass import * |
---|
| 8 | import sys |
---|
| 9 | |
---|
[e997b1a] | 10 | bufsize = 1024 |
---|
[65f1edc] | 11 | hopsize = bufsize/2 |
---|
| 12 | |
---|
[96fb8ad] | 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() |
---|
[65f1edc] | 24 | ovalist = [0., 0., 0., 0., 0., 0.] |
---|
[96fb8ad] | 25 | while(readsize==hopsize): |
---|
| 26 | readsize = filei.read(hopsize,myvec) |
---|
| 27 | isonset,val = opick.do(myvec) |
---|
[65f1edc] | 28 | ovalist.append(val) |
---|
| 29 | ovalist.pop(0) |
---|
[96fb8ad] | 30 | if (isonset == 1): |
---|
[65f1edc] | 31 | i=len(ovalist)-1 |
---|
| 32 | # find local minima |
---|
| 33 | while ovalist[i-1] < ovalist[i] and i > 0: |
---|
| 34 | i -= 1 |
---|
[c0ec39c] | 35 | now = (frameread+1-i)*hopsize/(srate+0.) |
---|
[65f1edc] | 36 | mylist.append(now) |
---|
[96fb8ad] | 37 | frameread += 1 |
---|
| 38 | return mylist |
---|
| 39 | |
---|
| 40 | def cutfile(filein,onsets): |
---|
| 41 | frameread = 0 |
---|
| 42 | readsize = hopsize |
---|
| 43 | filei = sndfile(filein) |
---|
| 44 | srate = filei.samplerate() |
---|
| 45 | channels = filei.channels() |
---|
| 46 | newname = "%s%f%s" % ("/tmp/",0.0000000,filein[-4:]) |
---|
| 47 | fileo = sndfile(newname,model=filei) |
---|
| 48 | myvec = fvec(hopsize,channels) |
---|
[c0ec39c] | 49 | mycopy = fvec(hopsize,channels) |
---|
[96fb8ad] | 50 | while(readsize==hopsize): |
---|
| 51 | readsize = filei.read(hopsize,myvec) |
---|
| 52 | now = (frameread)*hopsize/(srate+0.) |
---|
[c0ec39c] | 53 | # write to current file |
---|
| 54 | if len(onsets) and now >= onsets[0]: |
---|
[96fb8ad] | 55 | onsets.pop(0) |
---|
[c0ec39c] | 56 | # write up to 1st zero crossing |
---|
| 57 | zerocross = 0 |
---|
| 58 | while ( abs( myvec.get(zerocross,0) ) > 0.002 ): |
---|
| 59 | zerocross += 1 |
---|
| 60 | writesize = fileo.write(zerocross,myvec) |
---|
| 61 | fromcross = 0 |
---|
| 62 | while (zerocross < readsize): |
---|
[77494e7] | 63 | for i in range(channels): |
---|
| 64 | mycopy.set(myvec.get(zerocross,i),fromcross,i) |
---|
[c0ec39c] | 65 | fromcross += 1 |
---|
| 66 | zerocross += 1 |
---|
[96fb8ad] | 67 | del fileo |
---|
[c0ec39c] | 68 | 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 |
---|
| 71 | writesize = fileo.write(fromcross,mycopy) |
---|
| 72 | else: |
---|
| 73 | writesize = fileo.write(readsize,myvec) |
---|
[96fb8ad] | 74 | frameread += 1 |
---|
| 75 | del fileo |
---|
| 76 | |
---|
| 77 | filename = sys.argv[1] |
---|
| 78 | threshold = sys.argv[2] |
---|
| 79 | onsets = getonsets(filename,threshold) |
---|
| 80 | cutfile(filename,onsets) |
---|