b3311cc24608f9d3ecef06c92bae15f400b661fc
[aubio.git] / examples / utils.c
1 /*
2   Copyright (C) 2003-2009 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 /**
22
23   This file includes some tools common to all examples. Code specific to the
24   algorithm performed by each program should go in the source file of that
25   program instead.
26
27 */
28
29 #include "utils.h"
30 #ifdef HAVE_JACK
31 #include "jackio.h"
32 #endif /* HAVE_JACK */
33
34 int verbose = 0;
35 // input / output
36 char_t *sink_uri = NULL;
37 char_t *source_uri = NULL;
38 // general stuff
39 uint_t samplerate = 0;
40 uint_t buffer_size = 512;
41 uint_t overlap_size = 256;
42 // onset stuff
43 char_t * onset_method = "default";
44 smpl_t onset_threshold = 0.0; // will be set if != 0.
45 // pitch stuff
46 char_t * pitch_unit = "default";
47 char_t * pitch_method = "default";
48 smpl_t pitch_tolerance = 0.0; // will be set if != 0.
49 // tempo stuff
50 char_t * tempo_method = "default";
51 // more general stuff
52 smpl_t silence = -90.;
53 uint_t mix_input = 0;
54
55 //
56 // internal memory stuff
57 aubio_source_t *this_source = NULL;
58 aubio_sink_t *this_sink = NULL;
59 fvec_t *ibuf;
60 fvec_t *obuf;
61
62
63 /* settings */
64 int frames = 0;
65 int usejack = 0;
66 int frames_delay = 0;
67
68 extern void usage (FILE * stream, int exit_code);
69 extern int parse_args (int argc, char **argv);
70
71 void
72 examples_common_init (int argc, char **argv)
73 {
74
75   /* parse command line arguments */
76   parse_args (argc, argv);
77
78   if (!usejack) {
79     debug ("Opening files ...\n");
80     this_source = new_aubio_source ((char_t*)source_uri, 0, overlap_size);
81     if (this_source == NULL) {
82       outmsg ("Could not open input file %s.\n", source_uri);
83       exit (1);
84     }
85     samplerate = aubio_source_get_samplerate(this_source);
86     if (sink_uri != NULL) {
87       this_sink = new_aubio_sink ((char_t*)sink_uri, samplerate);
88       if (this_sink == NULL) {
89         outmsg ("Could not open output file %s.\n", sink_uri);
90         exit (1);
91       }
92     }
93   }
94   ibuf = new_fvec (overlap_size);
95   obuf = new_fvec (overlap_size);
96
97 }
98
99 void
100 examples_common_del (void)
101 {
102   del_fvec (ibuf);
103   del_fvec (obuf);
104   aubio_cleanup ();
105 }
106
107 #if HAVE_JACK
108 aubio_jack_t *jack_setup;
109 #endif
110
111 void
112 examples_common_process (aubio_process_func_t process_func,
113     aubio_print_func_t print)
114 {
115
116   uint_t read = 0;
117   if (usejack) {
118
119 #if HAVE_JACK
120     debug ("Jack init ...\n");
121     jack_setup = new_aubio_jack (1, 1,
122         0, 1, (aubio_process_func_t) process_func);
123     debug ("Jack activation ...\n");
124     aubio_jack_activate (jack_setup);
125     debug ("Processing (Ctrl+C to quit) ...\n");
126     pause ();
127     aubio_jack_close (jack_setup);
128 #else
129     usage (stderr, 1);
130     outmsg ("Compiled without jack output, exiting.\n");
131 #endif
132
133   } else {
134     /* phasevoc */
135     debug ("Processing ...\n");
136
137     frames = 0;
138
139     do {
140       aubio_source_do (this_source, ibuf, &read);
141       process_func (&ibuf->data, &obuf->data, overlap_size);
142       print ();
143       if (this_sink) {
144         aubio_sink_do (this_sink, obuf, overlap_size);
145       }
146       frames++;
147     } while (read == overlap_size);
148
149     debug ("Processed %d frames of %d samples.\n", frames, buffer_size);
150
151     del_aubio_source (this_source);
152     del_aubio_sink   (this_sink);
153
154   }
155 }
156
157 void
158 send_noteon (int pitch, int velo)
159 {
160   smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5);
161 #if HAVE_JACK
162   jack_midi_event_t ev;
163   ev.size = 3;
164   ev.buffer = malloc (3 * sizeof (jack_midi_data_t)); // FIXME
165   ev.time = 0;
166   if (usejack) {
167     ev.buffer[2] = velo;
168     ev.buffer[1] = mpitch;
169     if (velo == 0) {
170       ev.buffer[0] = 0x80;      /* note off */
171     } else {
172       ev.buffer[0] = 0x90;      /* note on */
173     }
174     aubio_jack_midi_event_write (jack_setup, (jack_midi_event_t *) & ev);
175   } else
176 #endif
177   if (!verbose) {
178     if (velo == 0) {
179       outmsg ("%f\n", frames * overlap_size / (float) samplerate);
180     } else {
181       outmsg ("%f\t%f\t", mpitch, frames * overlap_size / (float) samplerate);
182     }
183   }
184 }
185