(no commit message)
[aubio.git] / python / aubiocut
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   = 512
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                         #del fileo
37                         #fileo = sndfile("%s%f%s" % ("/tmp/",now,filein[-4:]),model=filei)
38                         mylist.append(now)
39                 #writesize = fileo.write(readsize,myoldvec)
40                 frameread += 1
41         return mylist
42
43 def cutfile(filein,onsets):
44         frameread = 0
45         readsize  = hopsize 
46         filei     = sndfile(filein)
47         srate     = filei.samplerate()
48         channels  = filei.channels()
49         newname   = "%s%f%s" % ("/tmp/",0.0000000,filein[-4:])
50         fileo     = sndfile(newname,model=filei)
51         myvec     = fvec(hopsize,channels)
52         mycopy    = fvec(hopsize,channels)
53         while(readsize==hopsize):
54                 readsize = filei.read(hopsize,myvec)
55                 now = (frameread)*hopsize/(srate+0.)
56                 # write to current file
57                 if len(onsets) and now >= onsets[0]:
58                     onsets.pop(0)
59                     # write up to 1st zero crossing
60                     zerocross = 0
61                     while ( abs( myvec.get(zerocross,0) ) > 0.002 ):
62                         zerocross += 1
63                     writesize = fileo.write(zerocross,myvec)
64                     fromcross = 0
65                     while (zerocross < readsize):
66                         mycopy.set(myvec.get(zerocross,0),fromcross,0)
67                         fromcross += 1
68                         zerocross += 1
69                     del fileo
70                     fileo = sndfile("%s%s%f%s%s" % 
71                         (filein.split(".")[0].split("/")[-1],".",now,".",filein.split(".")[-1]),model=filei)
72                     # write after 1st zero crossing to new file
73                     writesize = fileo.write(fromcross,mycopy)
74                 else:
75                     writesize = fileo.write(readsize,myvec)
76                 frameread += 1
77         del fileo
78
79 filename  = sys.argv[1]
80 threshold = sys.argv[2]
81 onsets    = getonsets(filename,threshold)
82 cutfile(filename,onsets)