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 | def getonsets(filein,threshold): |
---|
11 | bufsize = 1024 |
---|
12 | hopsize = bufsize/2 |
---|
13 | frameread = 0 |
---|
14 | filei = sndfile(filein) |
---|
15 | srate = filei.samplerate() |
---|
16 | channels = filei.channels() |
---|
17 | myvec = fvec(hopsize,channels) |
---|
18 | readsize = filei.read(hopsize,myvec) |
---|
19 | opick = onsetpick(bufsize,hopsize,channels,myvec,threshold) |
---|
20 | #newname = "%s%.8f%s" % ("/tmp/",0.0000000,filein[-4:]) |
---|
21 | #fileo = sndfile(newname,model=filei) |
---|
22 | mylist = list() |
---|
23 | while(readsize==hopsize): |
---|
24 | readsize = filei.read(hopsize,myvec) |
---|
25 | isonset,val = opick.do(myvec) |
---|
26 | if (isonset == 1): |
---|
27 | now = (frameread-4)*hopsize/(srate+0.) |
---|
28 | #del fileo |
---|
29 | #fileo = sndfile("%s%f%s" % ("/tmp/",now,filein[-4:]),model=filei) |
---|
30 | mylist.append(now) |
---|
31 | #writesize = fileo.write(readsize,myoldvec) |
---|
32 | frameread += 1 |
---|
33 | return mylist |
---|
34 | |
---|
35 | def cutfile(filein,onsets): |
---|
36 | hopsize = 512 |
---|
37 | frameread = 0 |
---|
38 | readsize = hopsize |
---|
39 | filei = sndfile(filein) |
---|
40 | srate = filei.samplerate() |
---|
41 | channels = filei.channels() |
---|
42 | newname = "%s%f%s" % ("/tmp/",0.0000000,filein[-4:]) |
---|
43 | fileo = sndfile(newname,model=filei) |
---|
44 | myvec = fvec(hopsize,channels) |
---|
45 | while(readsize==hopsize): |
---|
46 | readsize = filei.read(hopsize,myvec) |
---|
47 | now = (frameread)*hopsize/(srate+0.) |
---|
48 | writesize = fileo.write(readsize,myvec) |
---|
49 | if len(onsets) and now == onsets[0]: |
---|
50 | onsets.pop(0) |
---|
51 | del fileo |
---|
52 | fileo = sndfile("%s%f%s%s" % ("/tmp/",now,".",filein.split(".")[-1]),model=filei) |
---|
53 | frameread += 1 |
---|
54 | del fileo |
---|
55 | |
---|
56 | filename = sys.argv[1] |
---|
57 | threshold = sys.argv[2] |
---|
58 | onsets = getonsets(filename,threshold) |
---|
59 | cutfile(filename,onsets) |
---|