Merge branch 'master' into awhitening
[aubio.git] / examples / aubiotrack.c
1 /*
2   Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org>
3
4   This file is part of aubio.
5
6   aubio is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   aubio is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "utils.h"
22 #define PROG_HAS_TEMPO 1
23 #define PROG_HAS_ONSET 1
24 #define PROG_HAS_SILENCE 1
25 #define PROG_HAS_OUTPUT 1
26 #define PROG_HAS_JACK 1
27 #include "parse_args.h"
28
29 aubio_tempo_t * tempo;
30 aubio_wavetable_t *wavetable;
31 fvec_t * tempo_out;
32 smpl_t is_beat = 0.;
33 uint_t is_silence = 0;
34
35 void process_block(fvec_t * ibuf, fvec_t *obuf) {
36   aubio_tempo_do (tempo, ibuf, tempo_out);
37   is_beat = fvec_get_sample (tempo_out, 0);
38   if (silence_threshold != -90.)
39     is_silence = aubio_silence_detection(ibuf, silence_threshold);
40   if ( !usejack && ! sink_uri ) return;
41   fvec_zeros (obuf);
42   if ( is_beat && !is_silence ) {
43     aubio_wavetable_play ( wavetable );
44     /* send a midi tap (default to C0) out to the midi output */
45     if (usejack) send_noteon(miditap_note, miditap_velo);
46   } else {
47     aubio_wavetable_stop ( wavetable );
48   }
49   if (mix_input) {
50     aubio_wavetable_do (wavetable, ibuf, obuf);
51     fvec_clamp(obuf, 1.);
52   } else {
53     aubio_wavetable_do (wavetable, obuf, obuf);
54   }
55 }
56
57 void process_print (void) {
58   if ( is_beat && !is_silence ) {
59     print_time (aubio_tempo_get_last (tempo));
60     outmsg ("\n");
61   }
62 }
63
64 int main(int argc, char **argv) {
65   int ret = 0;
66   // override general settings from utils.c
67   buffer_size = 1024;
68   hop_size = 512;
69
70   examples_common_init(argc,argv);
71
72   verbmsg ("using source: %s at %dHz\n", source_uri, samplerate);
73
74   verbmsg ("tempo method: %s, ", tempo_method);
75   verbmsg ("buffer_size: %d, ", buffer_size);
76   verbmsg ("hop_size: %d, ", hop_size);
77   verbmsg ("threshold: %f\n", onset_threshold);
78
79   tempo_out = new_fvec(2);
80   tempo = new_aubio_tempo(tempo_method, buffer_size, hop_size, samplerate);
81   if (tempo == NULL) { ret = 1; goto beach; }
82   // set silence threshold very low to output beats even during silence
83   // aubio_tempo_set_silence(tempo, -1000.);
84   if (onset_threshold != 0.) aubio_tempo_set_threshold (tempo, onset_threshold);
85   if (onset_minioi != 0.) errmsg ("warning: minioio not supported yet\n");
86
87   wavetable = new_aubio_wavetable (samplerate, hop_size);
88   aubio_wavetable_set_freq ( wavetable, 2450.);
89   //aubio_sampler_load (sampler, "/archives/sounds/woodblock.aiff");
90
91   examples_common_process((aubio_process_func_t)process_block,process_print);
92
93   // send a last note off
94   if (usejack) {
95     send_noteon (miditap_note, 0);
96   }
97
98   del_aubio_tempo(tempo);
99   del_aubio_wavetable (wavetable);
100   del_fvec(tempo_out);
101
102 beach:
103   examples_common_del();
104   return ret;
105 }