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