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 | print frameread |
---|
32 | i=len(ovalist)-1 |
---|
33 | # find local minima |
---|
34 | while ovalist[i-1] < ovalist[i] and i > 0: |
---|
35 | i -= 1 |
---|
36 | now = (frameread-i+1)*hopsize/(srate+0.) |
---|
37 | #del fileo |
---|
38 | #fileo = sndfile("%s%f%s" % ("/tmp/",now,filein[-4:]),model=filei) |
---|
39 | mylist.append(now) |
---|
40 | #writesize = fileo.write(readsize,myoldvec) |
---|
41 | frameread += 1 |
---|
42 | return mylist |
---|
43 | |
---|
44 | def cutfile(filein,onsets): |
---|
45 | frameread = 0 |
---|
46 | readsize = hopsize |
---|
47 | filei = sndfile(filein) |
---|
48 | srate = filei.samplerate() |
---|
49 | channels = filei.channels() |
---|
50 | newname = "%s%f%s" % ("/tmp/",0.0000000,filein[-4:]) |
---|
51 | fileo = sndfile(newname,model=filei) |
---|
52 | myvec = fvec(hopsize,channels) |
---|
53 | while(readsize==hopsize): |
---|
54 | readsize = filei.read(hopsize,myvec) |
---|
55 | now = (frameread)*hopsize/(srate+0.) |
---|
56 | writesize = fileo.write(readsize,myvec) |
---|
57 | if len(onsets) and now == onsets[0]: |
---|
58 | onsets.pop(0) |
---|
59 | del fileo |
---|
60 | fileo = sndfile("%s%f%s%s" % ("/tmp/",now,".",filein.split(".")[-1]),model=filei) |
---|
61 | frameread += 1 |
---|
62 | del fileo |
---|
63 | |
---|
64 | filename = sys.argv[1] |
---|
65 | threshold = sys.argv[2] |
---|
66 | onsets = getonsets(filename,threshold) |
---|
67 | cutfile(filename,onsets) |
---|