2 Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
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
32 #endif /* HAVE_JACK */
37 char_t *sink_uri = NULL;
38 char_t *source_uri = NULL;
40 uint_t samplerate = 0;
41 uint_t buffer_size = 512;
42 uint_t hop_size = 256;
44 char_t * onset_method = "default";
45 smpl_t onset_threshold = 0.0; // will be set if != 0.
47 char_t * pitch_unit = "default";
48 char_t * pitch_method = "default";
49 smpl_t pitch_tolerance = 0.0; // will be set if != 0.
51 char_t * tempo_method = "specflux";
53 smpl_t silence_threshold = -90.;
56 uint_t force_overwrite = 0;
59 // internal memory stuff
60 aubio_source_t *this_source = NULL;
61 aubio_sink_t *this_sink = NULL;
68 extern void usage (FILE * stream, int exit_code);
69 extern int parse_args (int argc, char **argv);
72 aubio_jack_t *jack_setup;
75 void examples_common_init (int argc, char **argv);
76 void examples_common_del (void);
77 void examples_common_process (aubio_process_func_t process_func,
78 aubio_print_func_t print);
80 void examples_common_init (int argc, char **argv)
83 /* parse command line arguments */
84 parse_args (argc, argv);
87 debug ("Opening files ...\n");
88 this_source = new_aubio_source ((char_t*)source_uri, samplerate, hop_size);
89 if (this_source == NULL) {
90 errmsg ("Error: could not open input file %s\n", source_uri);
93 if (samplerate == 0) {
94 samplerate = aubio_source_get_samplerate(this_source);
96 if (sink_uri != NULL) {
97 uint_t sink_exists = (access(sink_uri, F_OK) == 0 );
98 if (!force_overwrite && sink_exists) {
99 errmsg ("Error: output file %s already exists, use -f to overwrite.\n",
103 this_sink = new_aubio_sink ((char_t*)sink_uri, samplerate);
104 if (this_sink == NULL) {
105 errmsg ("Error: could not create output file %s\n", sink_uri);
111 debug ("Jack init ...\n");
112 jack_setup = new_aubio_jack (hop_size, 1, 1, 0, 1);
113 samplerate = aubio_jack_get_samplerate (jack_setup);
117 ibuf = new_fvec (hop_size);
118 obuf = new_fvec (hop_size);
122 void examples_common_del (void)
131 void examples_common_process (aubio_process_func_t process_func,
132 aubio_print_func_t print)
139 debug ("Jack activation ...\n");
140 aubio_jack_activate (jack_setup, process_func);
141 debug ("Processing (Ctrl+C to quit) ...\n");
143 aubio_jack_close (jack_setup);
146 outmsg ("Compiled without jack output, exiting.\n");
151 uint_t total_read = 0;
155 aubio_source_do (this_source, ibuf, &read);
156 process_func (ibuf, obuf);
157 // print to console if verbose or no output given
158 if (verbose || sink_uri == NULL) {
162 aubio_sink_do (this_sink, obuf, hop_size);
166 } while (read == hop_size);
168 verbmsg ("read %.2fs (%d samples in %d blocks of %d) from %s at %dHz\n",
169 total_read * 1. / samplerate,
170 total_read, blocks, hop_size, source_uri, samplerate);
172 del_aubio_source (this_source);
173 del_aubio_sink (this_sink);
179 send_noteon (int pitch, int velo)
181 smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5);
183 jack_midi_event_t ev;
185 ev.buffer = malloc (3 * sizeof (jack_midi_data_t)); // FIXME
189 ev.buffer[1] = mpitch;
191 ev.buffer[0] = 0x80; /* note off */
193 ev.buffer[0] = 0x90; /* note on */
195 aubio_jack_midi_event_write (jack_setup, (jack_midi_event_t *) & ev);
199 verbmsg ("%f\n", blocks * hop_size / (float) samplerate);
201 verbmsg ("%f\t%f\t", mpitch, blocks * hop_size / (float) samplerate);