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 | |
---|
9 | bufsize = 1024 |
---|
10 | hopsize = bufsize/2 |
---|
11 | |
---|
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 |
---|
47 | |
---|
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) |
---|