1 | #!/usr/bin/python |
---|
2 | |
---|
3 | def do(filein,threshold): |
---|
4 | |
---|
5 | import aubio.aubioclass |
---|
6 | import aubio.median |
---|
7 | #from numarray import around |
---|
8 | from math import floor |
---|
9 | hopsize = 512 |
---|
10 | bufsize = 4096 |
---|
11 | channels = 1 |
---|
12 | frameread = 0 |
---|
13 | silthres = -80. |
---|
14 | filei = aubio.aubioclass.sndfile(filein) |
---|
15 | srate = filei.samplerate() |
---|
16 | myvec = aubio.aubioclass.fvec(hopsize,channels) |
---|
17 | readsize = filei.read(hopsize,myvec) |
---|
18 | ppick = aubio.aubioclass.pitchpick(bufsize,hopsize,channels,myvec,srate) |
---|
19 | opick = aubio.aubioclass.onsetpick(bufsize,hopsize,channels,myvec,threshold) |
---|
20 | mylist = list() |
---|
21 | |
---|
22 | wassilence = 0 |
---|
23 | lastpitch = 0 |
---|
24 | starttime = 0 |
---|
25 | while(readsize==hopsize): |
---|
26 | readsize = filei.read(hopsize,myvec) |
---|
27 | val = ppick.do(myvec) |
---|
28 | midival = aubio.aubioclass.bintomidi(val,srate,bufsize) |
---|
29 | isonset,onset = opick.do(myvec) |
---|
30 | now = (frameread)*hopsize/(srate+0.) |
---|
31 | issilence = aubio.aubioclass.aubio_silence_detection(myvec.vec,silthres) |
---|
32 | |
---|
33 | estmidival = 0 |
---|
34 | if (issilence == 1): |
---|
35 | if (wassilence == 0): |
---|
36 | #outputnow |
---|
37 | endtime = (frameread-3)*hopsize/(srate+0.) |
---|
38 | if len(mylist) > 5 : |
---|
39 | estmidival = aubio.median.percental(mylist,len(mylist)/2) |
---|
40 | print "sil", starttime, endtime, estmidival |
---|
41 | #resetnow |
---|
42 | mylist = list() |
---|
43 | else: |
---|
44 | wassilence = 1 |
---|
45 | else: |
---|
46 | if isonset == 1: |
---|
47 | if (wassilence == 0): |
---|
48 | #outputnow |
---|
49 | endtime = (frameread-3)*hopsize/(srate+0.) |
---|
50 | #estmidival = aubio.median.percental(around(array(mylist)),len(mylist)//2) |
---|
51 | if len(mylist) > 5 : |
---|
52 | estmidival = aubio.median.percental(mylist,len(mylist)/2) |
---|
53 | print starttime, endtime, estmidival |
---|
54 | #resetnow |
---|
55 | mylist = list() |
---|
56 | #store start time |
---|
57 | starttime = (frameread-3)*hopsize/(srate+0.) |
---|
58 | else: |
---|
59 | """ |
---|
60 | if(listfull): |
---|
61 | #outputnow |
---|
62 | endtime = (frameread-3)*hopsize/(srate+0.) |
---|
63 | print starttime, endtime, estimmidival |
---|
64 | else: |
---|
65 | """ |
---|
66 | #bufferize |
---|
67 | if midival > 50 and midival < 75: |
---|
68 | mylist.append(floor(midival)) |
---|
69 | wassilence = 0 |
---|
70 | |
---|
71 | |
---|
72 | #elif( midival > 45 ): |
---|
73 | # mylist.append(( now , midival+12 )) |
---|
74 | #mylist.append(toappend) |
---|
75 | frameread += 1 |
---|
76 | |
---|
77 | |
---|
78 | if __name__ == "__main__": |
---|
79 | import sys |
---|
80 | do(sys.argv[1],sys.argv[2]) |
---|