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 | mycopy.set(myvec.get(zerocross,0),fromcross,0) |
---|
64 | fromcross += 1 |
---|
65 | zerocross += 1 |
---|
66 | del fileo |
---|
67 | fileo = sndfile("%s%s%f%s%s" % |
---|
68 | (filein.split(".")[0].split("/")[-1],".",now,".",filein.split(".")[-1]),model=filei) |
---|
69 | # write after 1st zero crossing to new file |
---|
70 | writesize = fileo.write(fromcross,mycopy) |
---|
71 | else: |
---|
72 | writesize = fileo.write(readsize,myvec) |
---|
73 | frameread += 1 |
---|
74 | del fileo |
---|
75 | |
---|
76 | filename = sys.argv[1] |
---|
77 | threshold = sys.argv[2] |
---|
78 | onsets = getonsets(filename,threshold) |
---|
79 | cutfile(filename,onsets) |
---|