updated beattracking.c ad aubiotrack.c to support variable hopsize
[aubio.git] / examples / aubiotrack.c
1 /*
2    Copyright (C) 2003 Paul Brossier
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "utils.h"
20 #include "beattracking.h"
21
22 unsigned int pos          = 0;    /* frames%dspblocksize */
23 unsigned int pos2         = 0;    /* frames%dspblocksize */
24 uint_t usepitch           = 0;
25 fvec_t * dfframe          = NULL;
26 fvec_t * out              = NULL;
27 aubio_beattracking_t * bt = NULL;
28 uint_t winlen             = 512;
29 uint_t step               = 0;
30 uint_t istactus           = 0;
31
32 int aubio_process(float **input, float **output, int nframes);
33 int aubio_process(float **input, float **output, int nframes) {
34   unsigned int i;       /*channels*/
35   unsigned int j;       /*frames*/
36   smpl_t * btoutput = out->data[0];
37   for (j=0;j<nframes;j++) {
38     if(usejack) {
39       for (i=0;i<channels;i++) {
40         /* write input to datanew */
41         fvec_write_sample(ibuf, input[i][j], i, pos);
42         /* put synthnew in output */
43         output[i][j] = fvec_read_sample(obuf, i, pos);
44       }
45     }
46     /*time for fft*/
47     if (pos == overlap_size-1) {         
48       /* block loop */
49       aubio_pvoc_do (pv,ibuf, fftgrain);
50       aubio_onsetdetection(o,fftgrain, onset);
51       if (usedoubled) {
52         aubio_onsetdetection(o2,fftgrain, onset2);
53         onset->data[0][0] *= onset2->data[0][0];
54       }
55       /* execute every overlap_size*step */
56       if (pos2 == step -1 ) {
57               
58         /* check dfframe */
59         /*
60         outmsg("\n");
61         for (i = 0; i < winlen; i++ ) 
62                 outmsg("%f,", dfframe->data[0][i]);
63         outmsg("\n");
64         */
65                         
66         aubio_beattracking_do(bt,dfframe,out);
67
68         /* rotate dfframe */
69         for (i = 0 ; i < winlen - step; i++ ) 
70                 dfframe->data[0][i] = dfframe->data[0][i+step];
71         for (i = winlen - step ; i < winlen; i++ ) 
72                 dfframe->data[0][i] = 0.;
73                 
74         pos2 = -1;
75       }
76       pos2++;
77       isonset = aubio_peakpick_pimrt_wt(onset,parms,&(dfframe->data[0][winlen - step + pos2]));
78       /* end of second level loop */
79       istactus = 0;
80       i=0;
81       for (i = 1; i < btoutput[0]; i++ ) { 
82               if (pos2 == btoutput[i]) {
83                       //printf("pos2: %d\n", pos2);
84                       //printf("tempo:\t%3.5f bpm \n", 
85                       //60.*44100./overlap_size/abs(btoutput[2]-btoutput[1]));
86                       /* test for silence */
87                       if (aubio_silence_detection(ibuf, threshold2)==1) {
88                               isonset  = 0;
89                               istactus = 0;
90                       } else {
91                               istactus = 1;
92                       }
93               }
94       }
95
96       if (istactus ==1) {
97               for (pos = 0; pos < overlap_size; pos++)
98                       obuf->data[0][pos] = woodblock->data[0][pos];
99       } else {
100               for (pos = 0; pos < overlap_size; pos++)
101                       obuf->data[0][pos] = 0.;
102       }
103       /* end of block loop */
104       pos = -1; /* so it will be zero next j loop */
105     }
106     pos++;
107   }
108   return 1;
109 }
110
111 void process_print (void);
112 void process_print (void) {
113         if (output_filename == NULL) {
114                 if (istactus)
115                         outmsg("%f\n",(frames)*overlap_size/(float)samplerate); 
116                 if (isonset && verbose)
117                         outmsg(" \t \t%f\n",(frames)*overlap_size/(float)samplerate);
118         }
119 }
120
121 int main(int argc, char **argv) {
122   
123   /* override default settings */
124   examples_common_init(argc,argv);
125   winlen = SQR(512)/overlap_size;
126
127   dfframe = new_fvec(winlen,channels);
128   step = winlen/4;
129   out = new_fvec(step,channels);
130
131   /* test input : impulses starting from 15, at intervals of 50 samples */
132   //for(i=0;i<16;i++){ 
133   //        dfframe->data[0][50*i+14] = 1.;
134   //}
135
136   bt = new_aubio_beattracking(winlen,channels);
137
138   examples_common_process(aubio_process,process_print);
139
140   examples_common_del();
141
142   debug("End of program.\n");
143
144   fflush(stderr);
145
146   return 0;
147 }
148