[50bc5f2] | 1 | /* |
---|
[466dff3] | 2 | Copyright (C) 2003-2013 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; |
---|
[466dff3] | 35 | int usejack = 0; |
---|
[1b25a70] | 36 | // input / output |
---|
| 37 | char_t *sink_uri = NULL; |
---|
| 38 | char_t *source_uri = NULL; |
---|
| 39 | // general stuff |
---|
| 40 | uint_t samplerate = 0; |
---|
| 41 | uint_t buffer_size = 512; |
---|
[466dff3] | 42 | uint_t hop_size = 256; |
---|
[1b25a70] | 43 | // onset stuff |
---|
| 44 | char_t * onset_method = "default"; |
---|
| 45 | smpl_t onset_threshold = 0.0; // will be set if != 0. |
---|
| 46 | // pitch stuff |
---|
[d4c5de7] | 47 | char_t * pitch_unit = "default"; |
---|
[1b25a70] | 48 | char_t * pitch_method = "default"; |
---|
| 49 | smpl_t pitch_tolerance = 0.0; // will be set if != 0. |
---|
| 50 | // tempo stuff |
---|
| 51 | char_t * tempo_method = "default"; |
---|
| 52 | // more general stuff |
---|
[ce6186a] | 53 | smpl_t silence_threshold = -90.; |
---|
[1b25a70] | 54 | uint_t mix_input = 0; |
---|
[50bc5f2] | 55 | |
---|
[1b25a70] | 56 | // |
---|
| 57 | // internal memory stuff |
---|
[5a66677] | 58 | aubio_source_t *this_source = NULL; |
---|
| 59 | aubio_sink_t *this_sink = NULL; |
---|
[50bc5f2] | 60 | fvec_t *ibuf; |
---|
| 61 | fvec_t *obuf; |
---|
[96fb8ad] | 62 | |
---|
[1b25a70] | 63 | /* settings */ |
---|
[466dff3] | 64 | int blocks = 0; |
---|
[50bc5f2] | 65 | |
---|
[1b25a70] | 66 | extern void usage (FILE * stream, int exit_code); |
---|
| 67 | extern int parse_args (int argc, char **argv); |
---|
[96fb8ad] | 68 | |
---|
[466dff3] | 69 | #if HAVE_JACK |
---|
| 70 | aubio_jack_t *jack_setup; |
---|
| 71 | #endif |
---|
| 72 | |
---|
[50bc5f2] | 73 | void |
---|
| 74 | examples_common_init (int argc, char **argv) |
---|
| 75 | { |
---|
[bd2f2ab] | 76 | |
---|
| 77 | /* parse command line arguments */ |
---|
[50bc5f2] | 78 | parse_args (argc, argv); |
---|
[bd2f2ab] | 79 | |
---|
[50bc5f2] | 80 | if (!usejack) { |
---|
| 81 | debug ("Opening files ...\n"); |
---|
[466dff3] | 82 | this_source = new_aubio_source ((char_t*)source_uri, samplerate, hop_size); |
---|
[5a66677] | 83 | if (this_source == NULL) { |
---|
[912f343] | 84 | outmsg ("Could not open input file %s\n", source_uri); |
---|
[50bc5f2] | 85 | exit (1); |
---|
[9af07aa] | 86 | } |
---|
[466dff3] | 87 | if (samplerate == 0) { |
---|
| 88 | samplerate = aubio_source_get_samplerate(this_source); |
---|
| 89 | } |
---|
[5a66677] | 90 | if (sink_uri != NULL) { |
---|
| 91 | this_sink = new_aubio_sink ((char_t*)sink_uri, samplerate); |
---|
[88fc249] | 92 | if (this_sink == NULL) { |
---|
[912f343] | 93 | outmsg ("Could not open output file %s\n", sink_uri); |
---|
[88fc249] | 94 | exit (1); |
---|
| 95 | } |
---|
[5a66677] | 96 | } |
---|
[466dff3] | 97 | #ifdef HAVE_JACK |
---|
| 98 | } else { |
---|
| 99 | debug ("Jack init ...\n"); |
---|
| 100 | jack_setup = new_aubio_jack (hop_size, 1, 1, 0, 1); |
---|
| 101 | samplerate = aubio_jack_get_samplerate (jack_setup); |
---|
| 102 | source_uri = "jack"; |
---|
| 103 | #endif |
---|
[bd2f2ab] | 104 | } |
---|
[466dff3] | 105 | ibuf = new_fvec (hop_size); |
---|
| 106 | obuf = new_fvec (hop_size); |
---|
[bd2f2ab] | 107 | |
---|
| 108 | } |
---|
| 109 | |
---|
[50bc5f2] | 110 | void |
---|
| 111 | examples_common_del (void) |
---|
| 112 | { |
---|
| 113 | del_fvec (ibuf); |
---|
[d4c5de7] | 114 | del_fvec (obuf); |
---|
[50bc5f2] | 115 | aubio_cleanup (); |
---|
[466dff3] | 116 | fflush(stderr); |
---|
| 117 | fflush(stdout); |
---|
[bd2f2ab] | 118 | } |
---|
| 119 | |
---|
[50bc5f2] | 120 | void |
---|
| 121 | examples_common_process (aubio_process_func_t process_func, |
---|
| 122 | aubio_print_func_t print) |
---|
| 123 | { |
---|
[5a66677] | 124 | |
---|
| 125 | uint_t read = 0; |
---|
[50bc5f2] | 126 | if (usejack) { |
---|
[ebbf5a0] | 127 | |
---|
[b511fa9] | 128 | #if HAVE_JACK |
---|
[50bc5f2] | 129 | debug ("Jack activation ...\n"); |
---|
[466dff3] | 130 | aubio_jack_activate (jack_setup, process_func); |
---|
[50bc5f2] | 131 | debug ("Processing (Ctrl+C to quit) ...\n"); |
---|
| 132 | pause (); |
---|
| 133 | aubio_jack_close (jack_setup); |
---|
[bd2f2ab] | 134 | #else |
---|
[50bc5f2] | 135 | usage (stderr, 1); |
---|
| 136 | outmsg ("Compiled without jack output, exiting.\n"); |
---|
[bd2f2ab] | 137 | #endif |
---|
| 138 | |
---|
| 139 | } else { |
---|
| 140 | /* phasevoc */ |
---|
[466dff3] | 141 | blocks = 0; |
---|
| 142 | uint_t total_read = 0; |
---|
[bd2f2ab] | 143 | |
---|
[5a66677] | 144 | do { |
---|
| 145 | aubio_source_do (this_source, ibuf, &read); |
---|
[466dff3] | 146 | process_func (ibuf, obuf); |
---|
| 147 | // print to console if verbose or no output given |
---|
| 148 | if (verbose || sink_uri == NULL) { |
---|
| 149 | print(); |
---|
| 150 | } |
---|
[5a66677] | 151 | if (this_sink) { |
---|
[466dff3] | 152 | aubio_sink_do (this_sink, obuf, hop_size); |
---|
[bd2f2ab] | 153 | } |
---|
[466dff3] | 154 | blocks++; |
---|
| 155 | total_read += read; |
---|
| 156 | } while (read == hop_size); |
---|
[bd2f2ab] | 157 | |
---|
[466dff3] | 158 | verbmsg ("read %d samples (%d blocks of %d) from %s at %dHz\n", |
---|
| 159 | total_read, blocks, hop_size, source_uri, samplerate); |
---|
[e24378a] | 160 | |
---|
[5a66677] | 161 | del_aubio_source (this_source); |
---|
| 162 | del_aubio_sink (this_sink); |
---|
[bd2f2ab] | 163 | |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
[50bc5f2] | 167 | void |
---|
| 168 | send_noteon (int pitch, int velo) |
---|
[bd2f2ab] | 169 | { |
---|
[50bc5f2] | 170 | smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5); |
---|
[ebbf5a0] | 171 | #if HAVE_JACK |
---|
| 172 | jack_midi_event_t ev; |
---|
| 173 | ev.size = 3; |
---|
| 174 | ev.buffer = malloc (3 * sizeof (jack_midi_data_t)); // FIXME |
---|
| 175 | ev.time = 0; |
---|
[50bc5f2] | 176 | if (usejack) { |
---|
[ebbf5a0] | 177 | ev.buffer[2] = velo; |
---|
| 178 | ev.buffer[1] = mpitch; |
---|
[50bc5f2] | 179 | if (velo == 0) { |
---|
[ebbf5a0] | 180 | ev.buffer[0] = 0x80; /* note off */ |
---|
[50bc5f2] | 181 | } else { |
---|
[ebbf5a0] | 182 | ev.buffer[0] = 0x90; /* note on */ |
---|
[50bc5f2] | 183 | } |
---|
[ebbf5a0] | 184 | aubio_jack_midi_event_write (jack_setup, (jack_midi_event_t *) & ev); |
---|
[50bc5f2] | 185 | } else |
---|
[bd2f2ab] | 186 | #endif |
---|
[466dff3] | 187 | if (velo == 0) { |
---|
[3da8187] | 188 | verbmsg ("%f\n", blocks * hop_size / (float) samplerate); |
---|
[466dff3] | 189 | } else { |
---|
[3da8187] | 190 | verbmsg ("%f\t%f\t", mpitch, blocks * hop_size / (float) samplerate); |
---|
[50bc5f2] | 191 | } |
---|
[bd2f2ab] | 192 | } |
---|
| 193 | |
---|