[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. |
---|
[340cb93] | 50 | // time stuff |
---|
[df7be43] | 51 | uint_t time_format = 0; // for "seconds", 1 for "ms", 2 for "samples" |
---|
[1b25a70] | 52 | // tempo stuff |
---|
[a559796] | 53 | char_t * tempo_method = "default"; |
---|
[1b25a70] | 54 | // more general stuff |
---|
[ce6186a] | 55 | smpl_t silence_threshold = -90.; |
---|
[1b25a70] | 56 | uint_t mix_input = 0; |
---|
[50bc5f2] | 57 | |
---|
[0a509c6] | 58 | uint_t force_overwrite = 0; |
---|
| 59 | |
---|
[1b25a70] | 60 | // |
---|
| 61 | // internal memory stuff |
---|
[5a66677] | 62 | aubio_source_t *this_source = NULL; |
---|
| 63 | aubio_sink_t *this_sink = NULL; |
---|
[50bc5f2] | 64 | fvec_t *ibuf; |
---|
| 65 | fvec_t *obuf; |
---|
[96fb8ad] | 66 | |
---|
[1b25a70] | 67 | /* settings */ |
---|
[466dff3] | 68 | int blocks = 0; |
---|
[50bc5f2] | 69 | |
---|
[1b25a70] | 70 | extern void usage (FILE * stream, int exit_code); |
---|
| 71 | extern int parse_args (int argc, char **argv); |
---|
[96fb8ad] | 72 | |
---|
[466dff3] | 73 | #if HAVE_JACK |
---|
| 74 | aubio_jack_t *jack_setup; |
---|
[7585822] | 75 | #endif /* HAVE_JACK */ |
---|
[466dff3] | 76 | |
---|
[d389e23] | 77 | void examples_common_init (int argc, char **argv); |
---|
| 78 | void examples_common_del (void); |
---|
| 79 | void examples_common_process (aubio_process_func_t process_func, |
---|
| 80 | aubio_print_func_t print); |
---|
| 81 | |
---|
| 82 | void examples_common_init (int argc, char **argv) |
---|
[50bc5f2] | 83 | { |
---|
[bd2f2ab] | 84 | |
---|
| 85 | /* parse command line arguments */ |
---|
[50bc5f2] | 86 | parse_args (argc, argv); |
---|
[bd2f2ab] | 87 | |
---|
[50bc5f2] | 88 | if (!usejack) { |
---|
| 89 | debug ("Opening files ...\n"); |
---|
[466dff3] | 90 | this_source = new_aubio_source ((char_t*)source_uri, samplerate, hop_size); |
---|
[5a66677] | 91 | if (this_source == NULL) { |
---|
[4d733b5] | 92 | errmsg ("Error: could not open input file %s\n", source_uri); |
---|
[50bc5f2] | 93 | exit (1); |
---|
[9af07aa] | 94 | } |
---|
[466dff3] | 95 | if (samplerate == 0) { |
---|
| 96 | samplerate = aubio_source_get_samplerate(this_source); |
---|
| 97 | } |
---|
[5a66677] | 98 | if (sink_uri != NULL) { |
---|
[0a509c6] | 99 | uint_t sink_exists = (access(sink_uri, F_OK) == 0 ); |
---|
| 100 | if (!force_overwrite && sink_exists) { |
---|
[4d733b5] | 101 | errmsg ("Error: output file %s already exists, use -f to overwrite.\n", |
---|
[0a509c6] | 102 | sink_uri); |
---|
| 103 | exit (1); |
---|
| 104 | } |
---|
[5a66677] | 105 | this_sink = new_aubio_sink ((char_t*)sink_uri, samplerate); |
---|
[88fc249] | 106 | if (this_sink == NULL) { |
---|
[4d733b5] | 107 | errmsg ("Error: could not create output file %s\n", sink_uri); |
---|
[88fc249] | 108 | exit (1); |
---|
| 109 | } |
---|
[5a66677] | 110 | } |
---|
[466dff3] | 111 | #ifdef HAVE_JACK |
---|
| 112 | } else { |
---|
| 113 | debug ("Jack init ...\n"); |
---|
| 114 | jack_setup = new_aubio_jack (hop_size, 1, 1, 0, 1); |
---|
| 115 | samplerate = aubio_jack_get_samplerate (jack_setup); |
---|
| 116 | source_uri = "jack"; |
---|
[7585822] | 117 | #endif /* HAVE_JACK */ |
---|
[bd2f2ab] | 118 | } |
---|
[466dff3] | 119 | ibuf = new_fvec (hop_size); |
---|
| 120 | obuf = new_fvec (hop_size); |
---|
[bd2f2ab] | 121 | |
---|
| 122 | } |
---|
| 123 | |
---|
[d389e23] | 124 | void examples_common_del (void) |
---|
[50bc5f2] | 125 | { |
---|
| 126 | del_fvec (ibuf); |
---|
[d4c5de7] | 127 | del_fvec (obuf); |
---|
[50bc5f2] | 128 | aubio_cleanup (); |
---|
[466dff3] | 129 | fflush(stderr); |
---|
| 130 | fflush(stdout); |
---|
[bd2f2ab] | 131 | } |
---|
| 132 | |
---|
[d389e23] | 133 | void examples_common_process (aubio_process_func_t process_func, |
---|
[50bc5f2] | 134 | aubio_print_func_t print) |
---|
| 135 | { |
---|
[5a66677] | 136 | |
---|
| 137 | uint_t read = 0; |
---|
[50bc5f2] | 138 | if (usejack) { |
---|
[ebbf5a0] | 139 | |
---|
[7585822] | 140 | #ifdef HAVE_JACK |
---|
[50bc5f2] | 141 | debug ("Jack activation ...\n"); |
---|
[466dff3] | 142 | aubio_jack_activate (jack_setup, process_func); |
---|
[50bc5f2] | 143 | debug ("Processing (Ctrl+C to quit) ...\n"); |
---|
| 144 | pause (); |
---|
| 145 | aubio_jack_close (jack_setup); |
---|
[7585822] | 146 | #else /* HAVE_JACK */ |
---|
[50bc5f2] | 147 | usage (stderr, 1); |
---|
| 148 | outmsg ("Compiled without jack output, exiting.\n"); |
---|
[7585822] | 149 | #endif /* HAVE_JACK */ |
---|
[bd2f2ab] | 150 | |
---|
| 151 | } else { |
---|
[5cc88f7] | 152 | |
---|
[466dff3] | 153 | uint_t total_read = 0; |
---|
[fe87823] | 154 | blocks = 0; |
---|
[bd2f2ab] | 155 | |
---|
[5a66677] | 156 | do { |
---|
| 157 | aubio_source_do (this_source, ibuf, &read); |
---|
[466dff3] | 158 | process_func (ibuf, obuf); |
---|
| 159 | // print to console if verbose or no output given |
---|
| 160 | if (verbose || sink_uri == NULL) { |
---|
| 161 | print(); |
---|
| 162 | } |
---|
[5a66677] | 163 | if (this_sink) { |
---|
[466dff3] | 164 | aubio_sink_do (this_sink, obuf, hop_size); |
---|
[bd2f2ab] | 165 | } |
---|
[466dff3] | 166 | blocks++; |
---|
| 167 | total_read += read; |
---|
| 168 | } while (read == hop_size); |
---|
[bd2f2ab] | 169 | |
---|
[2c31bb6] | 170 | verbmsg ("read %.2fs (%d samples in %d blocks of %d) from %s at %dHz\n", |
---|
| 171 | total_read * 1. / samplerate, |
---|
[466dff3] | 172 | total_read, blocks, hop_size, source_uri, samplerate); |
---|
[e24378a] | 173 | |
---|
[5a66677] | 174 | del_aubio_source (this_source); |
---|
| 175 | del_aubio_sink (this_sink); |
---|
[bd2f2ab] | 176 | |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
[50bc5f2] | 180 | void |
---|
| 181 | send_noteon (int pitch, int velo) |
---|
[bd2f2ab] | 182 | { |
---|
[50bc5f2] | 183 | smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5); |
---|
[7585822] | 184 | #ifdef HAVE_JACK |
---|
[ebbf5a0] | 185 | jack_midi_event_t ev; |
---|
| 186 | ev.size = 3; |
---|
| 187 | ev.buffer = malloc (3 * sizeof (jack_midi_data_t)); // FIXME |
---|
| 188 | ev.time = 0; |
---|
[50bc5f2] | 189 | if (usejack) { |
---|
[ebbf5a0] | 190 | ev.buffer[2] = velo; |
---|
| 191 | ev.buffer[1] = mpitch; |
---|
[50bc5f2] | 192 | if (velo == 0) { |
---|
[ebbf5a0] | 193 | ev.buffer[0] = 0x80; /* note off */ |
---|
[50bc5f2] | 194 | } else { |
---|
[ebbf5a0] | 195 | ev.buffer[0] = 0x90; /* note on */ |
---|
[50bc5f2] | 196 | } |
---|
[ebbf5a0] | 197 | aubio_jack_midi_event_write (jack_setup, (jack_midi_event_t *) & ev); |
---|
[50bc5f2] | 198 | } else |
---|
[bd2f2ab] | 199 | #endif |
---|
[466dff3] | 200 | if (velo == 0) { |
---|
[340cb93] | 201 | print_time (blocks * hop_size); |
---|
| 202 | outmsg ("\n"); |
---|
[466dff3] | 203 | } else { |
---|
[340cb93] | 204 | outmsg ("%f\t", mpitch); |
---|
| 205 | print_time (blocks * hop_size); |
---|
| 206 | outmsg ("\t"); |
---|
[50bc5f2] | 207 | } |
---|
[bd2f2ab] | 208 | } |
---|
| 209 | |
---|
[340cb93] | 210 | void print_time (uint_t time_in_samples) { |
---|
| 211 | /* output times in selected format */ |
---|
[df7be43] | 212 | if (time_format == 2) { |
---|
[340cb93] | 213 | outmsg ("%d", time_in_samples); |
---|
[df7be43] | 214 | } else if (time_format == 1) { |
---|
[340cb93] | 215 | outmsg ("%f", 1000. * time_in_samples / (float) samplerate); |
---|
| 216 | } else { |
---|
| 217 | outmsg ("%f", time_in_samples / (float) samplerate); |
---|
| 218 | } |
---|
| 219 | } |
---|