[50bc5f2] | 1 | /* |
---|
| 2 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
[96fb8ad] | 3 | |
---|
[50bc5f2] | 4 | This file is part of aubio. |
---|
[96fb8ad] | 5 | |
---|
[50bc5f2] | 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 | */ |
---|
[96fb8ad] | 20 | |
---|
[9430dfd] | 21 | /** |
---|
[d4c5de7] | 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 | |
---|
[96fb8ad] | 29 | #include "utils.h" |
---|
[1b25a70] | 30 | #ifdef HAVE_JACK |
---|
| 31 | #include "jackio.h" |
---|
| 32 | #endif /* HAVE_JACK */ |
---|
[96fb8ad] | 33 | |
---|
[bd2f2ab] | 34 | int verbose = 0; |
---|
[1b25a70] | 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 |
---|
[d4c5de7] | 46 | char_t * pitch_unit = "default"; |
---|
[1b25a70] | 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 |
---|
[50bc5f2] | 52 | smpl_t silence = -90.; |
---|
[1b25a70] | 53 | uint_t mix_input = 0; |
---|
[50bc5f2] | 54 | |
---|
[1b25a70] | 55 | // |
---|
| 56 | // internal memory stuff |
---|
[5a66677] | 57 | aubio_source_t *this_source = NULL; |
---|
| 58 | aubio_sink_t *this_sink = NULL; |
---|
[50bc5f2] | 59 | fvec_t *ibuf; |
---|
| 60 | fvec_t *obuf; |
---|
[96fb8ad] | 61 | |
---|
[d4c5de7] | 62 | |
---|
[1b25a70] | 63 | /* settings */ |
---|
| 64 | int frames = 0; |
---|
| 65 | int usejack = 0; |
---|
| 66 | int frames_delay = 0; |
---|
[50bc5f2] | 67 | |
---|
[1b25a70] | 68 | extern void usage (FILE * stream, int exit_code); |
---|
| 69 | extern int parse_args (int argc, char **argv); |
---|
[96fb8ad] | 70 | |
---|
[50bc5f2] | 71 | void |
---|
| 72 | examples_common_init (int argc, char **argv) |
---|
| 73 | { |
---|
[bd2f2ab] | 74 | |
---|
| 75 | /* parse command line arguments */ |
---|
[50bc5f2] | 76 | parse_args (argc, argv); |
---|
[bd2f2ab] | 77 | |
---|
[50bc5f2] | 78 | if (!usejack) { |
---|
| 79 | debug ("Opening files ...\n"); |
---|
[98874a6] | 80 | this_source = new_aubio_source ((char_t*)source_uri, 0, overlap_size); |
---|
[5a66677] | 81 | if (this_source == NULL) { |
---|
| 82 | outmsg ("Could not open input file %s.\n", source_uri); |
---|
[50bc5f2] | 83 | exit (1); |
---|
[9af07aa] | 84 | } |
---|
[98874a6] | 85 | samplerate = aubio_source_get_samplerate(this_source); |
---|
[5a66677] | 86 | if (sink_uri != NULL) { |
---|
| 87 | this_sink = new_aubio_sink ((char_t*)sink_uri, samplerate); |
---|
[88fc249] | 88 | if (this_sink == NULL) { |
---|
| 89 | outmsg ("Could not open output file %s.\n", sink_uri); |
---|
| 90 | exit (1); |
---|
| 91 | } |
---|
[5a66677] | 92 | } |
---|
[bd2f2ab] | 93 | } |
---|
[4621cd6] | 94 | ibuf = new_fvec (overlap_size); |
---|
| 95 | obuf = new_fvec (overlap_size); |
---|
[bd2f2ab] | 96 | |
---|
| 97 | } |
---|
| 98 | |
---|
[50bc5f2] | 99 | void |
---|
| 100 | examples_common_del (void) |
---|
| 101 | { |
---|
| 102 | del_fvec (ibuf); |
---|
[d4c5de7] | 103 | del_fvec (obuf); |
---|
[50bc5f2] | 104 | aubio_cleanup (); |
---|
[bd2f2ab] | 105 | } |
---|
| 106 | |
---|
[ebbf5a0] | 107 | #if HAVE_JACK |
---|
| 108 | aubio_jack_t *jack_setup; |
---|
| 109 | #endif |
---|
| 110 | |
---|
[50bc5f2] | 111 | void |
---|
| 112 | examples_common_process (aubio_process_func_t process_func, |
---|
| 113 | aubio_print_func_t print) |
---|
| 114 | { |
---|
[5a66677] | 115 | |
---|
| 116 | uint_t read = 0; |
---|
[50bc5f2] | 117 | if (usejack) { |
---|
[ebbf5a0] | 118 | |
---|
[b511fa9] | 119 | #if HAVE_JACK |
---|
[50bc5f2] | 120 | debug ("Jack init ...\n"); |
---|
[4621cd6] | 121 | jack_setup = new_aubio_jack (1, 1, |
---|
[ebbf5a0] | 122 | 0, 1, (aubio_process_func_t) process_func); |
---|
[50bc5f2] | 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); |
---|
[bd2f2ab] | 128 | #else |
---|
[50bc5f2] | 129 | usage (stderr, 1); |
---|
| 130 | outmsg ("Compiled without jack output, exiting.\n"); |
---|
[bd2f2ab] | 131 | #endif |
---|
| 132 | |
---|
| 133 | } else { |
---|
| 134 | /* phasevoc */ |
---|
[50bc5f2] | 135 | debug ("Processing ...\n"); |
---|
[bd2f2ab] | 136 | |
---|
| 137 | frames = 0; |
---|
| 138 | |
---|
[5a66677] | 139 | do { |
---|
| 140 | aubio_source_do (this_source, ibuf, &read); |
---|
[4621cd6] | 141 | process_func (&ibuf->data, &obuf->data, overlap_size); |
---|
[50bc5f2] | 142 | print (); |
---|
[5a66677] | 143 | if (this_sink) { |
---|
| 144 | aubio_sink_do (this_sink, obuf, overlap_size); |
---|
[bd2f2ab] | 145 | } |
---|
| 146 | frames++; |
---|
[5a66677] | 147 | } while (read == overlap_size); |
---|
[bd2f2ab] | 148 | |
---|
[50bc5f2] | 149 | debug ("Processed %d frames of %d samples.\n", frames, buffer_size); |
---|
[e24378a] | 150 | |
---|
[5a66677] | 151 | del_aubio_source (this_source); |
---|
| 152 | del_aubio_sink (this_sink); |
---|
[bd2f2ab] | 153 | |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
[50bc5f2] | 157 | void |
---|
| 158 | send_noteon (int pitch, int velo) |
---|
[bd2f2ab] | 159 | { |
---|
[50bc5f2] | 160 | smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5); |
---|
[ebbf5a0] | 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; |
---|
[50bc5f2] | 166 | if (usejack) { |
---|
[ebbf5a0] | 167 | ev.buffer[2] = velo; |
---|
| 168 | ev.buffer[1] = mpitch; |
---|
[50bc5f2] | 169 | if (velo == 0) { |
---|
[ebbf5a0] | 170 | ev.buffer[0] = 0x80; /* note off */ |
---|
[50bc5f2] | 171 | } else { |
---|
[ebbf5a0] | 172 | ev.buffer[0] = 0x90; /* note on */ |
---|
[50bc5f2] | 173 | } |
---|
[ebbf5a0] | 174 | aubio_jack_midi_event_write (jack_setup, (jack_midi_event_t *) & ev); |
---|
[50bc5f2] | 175 | } else |
---|
[bd2f2ab] | 176 | #endif |
---|
[50bc5f2] | 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); |
---|
[bd2f2ab] | 182 | } |
---|
[50bc5f2] | 183 | } |
---|
[bd2f2ab] | 184 | } |
---|
| 185 | |
---|