[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 | |
---|
| 14 | def cutfile(filein,onsets): |
---|
| 15 | frameread = 0 |
---|
[19b56b0] | 16 | zerothres = 0.002 |
---|
[96fb8ad] | 17 | readsize = hopsize |
---|
| 18 | filei = sndfile(filein) |
---|
[19b56b0] | 19 | framestep = hopsize/(filei.samplerate()+0.) |
---|
[96fb8ad] | 20 | channels = filei.channels() |
---|
| 21 | newname = "%s%f%s" % ("/tmp/",0.0000000,filein[-4:]) |
---|
| 22 | fileo = sndfile(newname,model=filei) |
---|
| 23 | myvec = fvec(hopsize,channels) |
---|
[c0ec39c] | 24 | mycopy = fvec(hopsize,channels) |
---|
[96fb8ad] | 25 | while(readsize==hopsize): |
---|
| 26 | readsize = filei.read(hopsize,myvec) |
---|
[c0ec39c] | 27 | # write to current file |
---|
[19b56b0] | 28 | if len(onsets) and frameread >= onsets[0]: |
---|
[96fb8ad] | 29 | onsets.pop(0) |
---|
[c0ec39c] | 30 | # write up to 1st zero crossing |
---|
| 31 | zerocross = 0 |
---|
[19b56b0] | 32 | while ( abs( myvec.get(zerocross,0) ) > zerothres ): |
---|
[c0ec39c] | 33 | zerocross += 1 |
---|
| 34 | writesize = fileo.write(zerocross,myvec) |
---|
| 35 | fromcross = 0 |
---|
| 36 | while (zerocross < readsize): |
---|
[77494e7] | 37 | for i in range(channels): |
---|
| 38 | mycopy.set(myvec.get(zerocross,i),fromcross,i) |
---|
[c0ec39c] | 39 | fromcross += 1 |
---|
| 40 | zerocross += 1 |
---|
[96fb8ad] | 41 | del fileo |
---|
[c0ec39c] | 42 | fileo = sndfile("%s%s%f%s%s" % |
---|
[19b56b0] | 43 | (filein.split(".")[0].split("/")[-1],".", |
---|
| 44 | frameread*framestep,".",filein.split(".")[-1]),model=filei) |
---|
[c0ec39c] | 45 | writesize = fileo.write(fromcross,mycopy) |
---|
| 46 | else: |
---|
| 47 | writesize = fileo.write(readsize,myvec) |
---|
[96fb8ad] | 48 | frameread += 1 |
---|
| 49 | del fileo |
---|
| 50 | |
---|
| 51 | filename = sys.argv[1] |
---|
| 52 | threshold = sys.argv[2] |
---|
| 53 | onsets = getonsets(filename,threshold) |
---|
| 54 | cutfile(filename,onsets) |
---|