[source] simplify and avoid unrequired checks
[aubio.git] / examples / aubiopitch.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_PITCH 1
23 #define PROG_HAS_OUTPUT 1
24 #define PROG_HAS_SILENCE 1
25 #define PROG_HAS_JACK 1
26 #include "parse_args.h"
27
28 aubio_pitch_t *o;
29 aubio_wavetable_t *wavetable;
30 fvec_t *pitch;
31
32 void process_block(fvec_t * ibuf, fvec_t * obuf)
33 {
34   smpl_t freq;
35   aubio_pitch_do (o, ibuf, pitch);
36   if ( !usejack && ! sink_uri ) return;
37   fvec_zeros(obuf);
38   freq = fvec_get_sample(pitch, 0);
39   aubio_wavetable_set_amp ( wavetable, aubio_level_lin (ibuf) );
40   aubio_wavetable_set_freq ( wavetable, freq );
41   if (mix_input)
42     aubio_wavetable_do (wavetable, ibuf, obuf);
43   else
44     aubio_wavetable_do (wavetable, obuf, obuf);
45 }
46
47 void process_print (void)
48 {
49   smpl_t pitch_found = fvec_get_sample(pitch, 0);
50   print_time(blocks * hop_size);
51   outmsg(" %f\n", pitch_found);
52 }
53
54 int main(int argc, char **argv) {
55   int ret = 0;
56
57   buffer_size = 2048;
58
59   examples_common_init(argc,argv);
60
61   verbmsg ("using source: %s at %dHz\n", source_uri, samplerate);
62   verbmsg ("pitch method: %s, ", pitch_method);
63   verbmsg ("pitch unit: %s, ", pitch_unit);
64   verbmsg ("buffer_size: %d, ", buffer_size);
65   verbmsg ("hop_size: %d, ", hop_size);
66   verbmsg ("tolerance: %f\n", pitch_tolerance);
67
68   o = new_aubio_pitch (pitch_method, buffer_size, hop_size, samplerate);
69   if (o == NULL) { ret = 1; goto beach; }
70   if (pitch_tolerance != 0.)
71     aubio_pitch_set_tolerance (o, pitch_tolerance);
72   if (silence_threshold != -90.)
73     aubio_pitch_set_silence (o, silence_threshold);
74   if (pitch_unit != NULL)
75     aubio_pitch_set_unit (o, pitch_unit);
76
77   pitch = new_fvec (1);
78
79   wavetable = new_aubio_wavetable (samplerate, hop_size);
80   aubio_wavetable_play ( wavetable );
81
82   examples_common_process(process_block, process_print);
83
84   del_aubio_pitch (o);
85   del_aubio_wavetable (wavetable);
86   del_fvec (pitch);
87
88 beach:
89   examples_common_del();
90   return ret;
91 }