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 | |
---|
10 | bufsize = 1024 |
---|
11 | hopsize = bufsize/2 |
---|
12 | |
---|
13 | |
---|
14 | def cutfile(filein,onsets): |
---|
15 | frameread = 0 |
---|
16 | zerothres = 0.002 |
---|
17 | readsize = hopsize |
---|
18 | filei = sndfile(filein) |
---|
19 | framestep = hopsize/(filei.samplerate()+0.) |
---|
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) |
---|
24 | mycopy = fvec(hopsize,channels) |
---|
25 | while(readsize==hopsize): |
---|
26 | readsize = filei.read(hopsize,myvec) |
---|
27 | # write to current file |
---|
28 | if len(onsets) and frameread >= onsets[0]: |
---|
29 | onsets.pop(0) |
---|
30 | # write up to 1st zero crossing |
---|
31 | zerocross = 0 |
---|
32 | while ( abs( myvec.get(zerocross,0) ) > zerothres ): |
---|
33 | zerocross += 1 |
---|
34 | writesize = fileo.write(zerocross,myvec) |
---|
35 | fromcross = 0 |
---|
36 | while (zerocross < readsize): |
---|
37 | for i in range(channels): |
---|
38 | mycopy.set(myvec.get(zerocross,i),fromcross,i) |
---|
39 | fromcross += 1 |
---|
40 | zerocross += 1 |
---|
41 | del fileo |
---|
42 | fileo = sndfile("%s%s%f%s%s" % |
---|
43 | (filein.split(".")[0].split("/")[-1],".", |
---|
44 | frameread*framestep,".",filein.split(".")[-1]),model=filei) |
---|
45 | writesize = fileo.write(fromcross,mycopy) |
---|
46 | else: |
---|
47 | writesize = fileo.write(readsize,myvec) |
---|
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) |
---|