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 | 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() |
---|
24 | ovalist = [0., 0., 0., 0., 0., 0.] |
---|
25 | while(readsize==hopsize): |
---|
26 | readsize = filei.read(hopsize,myvec) |
---|
27 | isonset,val = opick.do(myvec) |
---|
28 | ovalist.append(val) |
---|
29 | ovalist.pop(0) |
---|
30 | if (isonset == 1): |
---|
31 | i=len(ovalist)-1 |
---|
32 | # find local minima |
---|
33 | while ovalist[i-1] < ovalist[i] and i > 0: |
---|
34 | i -= 1 |
---|
35 | now = (frameread+1-i)*hopsize/(srate+0.) |
---|
36 | mylist.append(now) |
---|
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) |
---|
49 | mycopy = fvec(hopsize,channels) |
---|
50 | while(readsize==hopsize): |
---|
51 | readsize = filei.read(hopsize,myvec) |
---|
52 | now = (frameread)*hopsize/(srate+0.) |
---|
53 | # write to current file |
---|
54 | if len(onsets) and now >= onsets[0]: |
---|
55 | onsets.pop(0) |
---|
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): |
---|
63 | for i in range(channels): |
---|
64 | mycopy.set(myvec.get(zerocross,i),fromcross,i) |
---|
65 | fromcross += 1 |
---|
66 | zerocross += 1 |
---|
67 | del fileo |
---|
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) |
---|
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) |
---|