[96fb8ad] | 1 | /* |
---|
[466dff3] | 2 | Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org> |
---|
[96fb8ad] | 3 | |
---|
[6a2478c] | 4 | This file is part of aubio. |
---|
[96fb8ad] | 5 | |
---|
[6a2478c] | 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/>. |
---|
[96fb8ad] | 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
[4621cd6] | 21 | #define AUBIO_UNSTABLE 1 // for fvec_median |
---|
[96fb8ad] | 22 | #include "utils.h" |
---|
[1b25a70] | 23 | #define PROG_HAS_PITCH 1 |
---|
| 24 | #define PROG_HAS_ONSET 1 |
---|
[3da8187] | 25 | #define PROG_HAS_JACK 1 |
---|
| 26 | // TODO add PROG_HAS_OUTPUT |
---|
[1b25a70] | 27 | #include "parse_args.h" |
---|
[96fb8ad] | 28 | |
---|
[d4c5de7] | 29 | uint_t median = 6; |
---|
| 30 | |
---|
[466dff3] | 31 | fvec_t *note_buffer; |
---|
| 32 | fvec_t *note_buffer2; |
---|
[d4c5de7] | 33 | |
---|
| 34 | smpl_t curnote = 0.; |
---|
| 35 | smpl_t newnote = 0.; |
---|
| 36 | uint_t isready = 0; |
---|
[a0fd4e4] | 37 | |
---|
[466dff3] | 38 | aubio_pitch_t *pitch; |
---|
[d4c5de7] | 39 | aubio_onset_t *o; |
---|
| 40 | fvec_t *onset; |
---|
| 41 | fvec_t *pitch_obuf; |
---|
| 42 | |
---|
| 43 | /** append new note candidate to the note_buffer and return filtered value. we |
---|
| 44 | * need to copy the input array as fvec_median destroy its input data.*/ |
---|
| 45 | void note_append (fvec_t * note_buffer, smpl_t curnote); |
---|
| 46 | uint_t get_note (fvec_t * note_buffer, fvec_t * note_buffer2); |
---|
| 47 | |
---|
[466dff3] | 48 | static void |
---|
| 49 | process_block(fvec_t *ibuf, fvec_t *obuf) { |
---|
| 50 | fvec_zeros(obuf); |
---|
| 51 | aubio_onset_do(o, ibuf, onset); |
---|
| 52 | |
---|
| 53 | aubio_pitch_do (pitch, ibuf, pitch_obuf); |
---|
| 54 | smpl_t new_pitch = fvec_read_sample(pitch_obuf, 0); |
---|
| 55 | if(median){ |
---|
| 56 | note_append(note_buffer, new_pitch); |
---|
| 57 | } |
---|
[96fb8ad] | 58 | |
---|
[466dff3] | 59 | /* curlevel is negatif or 1 if silence */ |
---|
[ce6186a] | 60 | smpl_t curlevel = aubio_level_detection(ibuf, silence_threshold); |
---|
[466dff3] | 61 | if (fvec_read_sample(onset, 0)) { |
---|
| 62 | /* test for silence */ |
---|
| 63 | if (curlevel == 1.) { |
---|
| 64 | if (median) isready = 0; |
---|
| 65 | /* send note off */ |
---|
| 66 | send_noteon(curnote,0); |
---|
| 67 | } else { |
---|
| 68 | if (median) { |
---|
| 69 | isready = 1; |
---|
[96fb8ad] | 70 | } else { |
---|
[466dff3] | 71 | /* kill old note */ |
---|
| 72 | send_noteon(curnote,0); |
---|
| 73 | /* get and send new one */ |
---|
| 74 | send_noteon(new_pitch,127+(int)floor(curlevel)); |
---|
| 75 | curnote = new_pitch; |
---|
[96fb8ad] | 76 | } |
---|
| 77 | } |
---|
[466dff3] | 78 | } else { |
---|
| 79 | if (median) { |
---|
| 80 | if (isready > 0) |
---|
| 81 | isready++; |
---|
| 82 | if (isready == median) |
---|
| 83 | { |
---|
| 84 | /* kill old note */ |
---|
| 85 | send_noteon(curnote,0); |
---|
| 86 | newnote = get_note(note_buffer, note_buffer2); |
---|
| 87 | curnote = newnote; |
---|
| 88 | /* get and send new one */ |
---|
| 89 | if (curnote>45){ |
---|
| 90 | send_noteon(curnote,127+(int)floor(curlevel)); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | } // if median |
---|
[96fb8ad] | 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
[466dff3] | 97 | static void |
---|
| 98 | process_print (void) { |
---|
| 99 | //if (verbose) outmsg("%f\n",pitch_obuf->data[0]); |
---|
[a0fd4e4] | 100 | } |
---|
[96fb8ad] | 101 | |
---|
[d4c5de7] | 102 | void |
---|
| 103 | note_append (fvec_t * note_buffer, smpl_t curnote) |
---|
| 104 | { |
---|
| 105 | uint_t i = 0; |
---|
| 106 | for (i = 0; i < note_buffer->length - 1; i++) { |
---|
[4621cd6] | 107 | note_buffer->data[i] = note_buffer->data[i + 1]; |
---|
[d4c5de7] | 108 | } |
---|
[4621cd6] | 109 | note_buffer->data[note_buffer->length - 1] = curnote; |
---|
[d4c5de7] | 110 | return; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | uint_t |
---|
| 114 | get_note (fvec_t * note_buffer, fvec_t * note_buffer2) |
---|
| 115 | { |
---|
[247ce7d] | 116 | uint_t i; |
---|
[d4c5de7] | 117 | for (i = 0; i < note_buffer->length; i++) { |
---|
[4621cd6] | 118 | note_buffer2->data[i] = note_buffer->data[i]; |
---|
[d4c5de7] | 119 | } |
---|
[4621cd6] | 120 | return fvec_median (note_buffer2); |
---|
[d4c5de7] | 121 | } |
---|
| 122 | |
---|
[bd2f2ab] | 123 | int main(int argc, char **argv) { |
---|
| 124 | examples_common_init(argc,argv); |
---|
[d4c5de7] | 125 | |
---|
[466dff3] | 126 | verbmsg ("using source: %s at %dHz\n", source_uri, samplerate); |
---|
| 127 | |
---|
| 128 | verbmsg ("onset method: %s, ", onset_method); |
---|
| 129 | verbmsg ("buffer_size: %d, ", buffer_size); |
---|
| 130 | verbmsg ("hop_size: %d, ", hop_size); |
---|
| 131 | verbmsg ("threshold: %f\n", onset_threshold); |
---|
| 132 | |
---|
| 133 | verbmsg ("pitch method: %s, ", pitch_method); |
---|
| 134 | verbmsg ("buffer_size: %d, ", buffer_size * 4); |
---|
| 135 | verbmsg ("hop_size: %d, ", hop_size); |
---|
| 136 | verbmsg ("tolerance: %f\n", pitch_tolerance); |
---|
| 137 | |
---|
| 138 | o = new_aubio_onset (onset_method, buffer_size, hop_size, samplerate); |
---|
[1b25a70] | 139 | if (onset_threshold != 0.) aubio_onset_set_threshold (o, onset_threshold); |
---|
[4621cd6] | 140 | onset = new_fvec (1); |
---|
[d4c5de7] | 141 | |
---|
[466dff3] | 142 | pitch = new_aubio_pitch (pitch_method, buffer_size * 4, hop_size, samplerate); |
---|
| 143 | if (pitch_tolerance != 0.) aubio_pitch_set_tolerance (pitch, pitch_tolerance); |
---|
[4621cd6] | 144 | pitch_obuf = new_fvec (1); |
---|
[466dff3] | 145 | |
---|
[d4c5de7] | 146 | if (median) { |
---|
[4621cd6] | 147 | note_buffer = new_fvec (median); |
---|
| 148 | note_buffer2 = new_fvec (median); |
---|
[d4c5de7] | 149 | } |
---|
| 150 | |
---|
[466dff3] | 151 | examples_common_process((aubio_process_func_t)process_block, process_print); |
---|
[d4c5de7] | 152 | |
---|
[466dff3] | 153 | // send a last note off |
---|
[d4c5de7] | 154 | send_noteon (curnote, 0); |
---|
[466dff3] | 155 | |
---|
| 156 | del_aubio_pitch (pitch); |
---|
[d4c5de7] | 157 | if (median) { |
---|
| 158 | del_fvec (note_buffer); |
---|
| 159 | del_fvec (note_buffer2); |
---|
| 160 | } |
---|
| 161 | del_fvec (pitch_obuf); |
---|
| 162 | |
---|
[bd2f2ab] | 163 | examples_common_del(); |
---|
[96fb8ad] | 164 | return 0; |
---|
| 165 | } |
---|
| 166 | |
---|