[96fb8ad] | 1 | /* |
---|
[6a2478c] | 2 | Copyright (C) 2003-2009 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 |
---|
[299ad52] | 22 | |
---|
[96fb8ad] | 23 | #include "utils.h" |
---|
| 24 | |
---|
[d4c5de7] | 25 | /* pitch objects */ |
---|
| 26 | smpl_t pitch = 0.; |
---|
| 27 | |
---|
| 28 | uint_t median = 6; |
---|
| 29 | smpl_t curlevel = 0.; |
---|
| 30 | |
---|
[b14107f] | 31 | aubio_pitch_t *pitchdet; |
---|
[d4c5de7] | 32 | |
---|
| 33 | fvec_t *note_buffer = NULL; |
---|
| 34 | fvec_t *note_buffer2 = NULL; |
---|
| 35 | |
---|
| 36 | smpl_t curnote = 0.; |
---|
| 37 | smpl_t newnote = 0.; |
---|
| 38 | uint_t isready = 0; |
---|
[1f40359] | 39 | unsigned int pos = 0; /*frames%dspblocksize*/ |
---|
[a0fd4e4] | 40 | |
---|
[b14107f] | 41 | aubio_pitch_t *pitchdet; |
---|
[d4c5de7] | 42 | aubio_onset_t *o; |
---|
| 43 | fvec_t *onset; |
---|
| 44 | fvec_t *pitch_obuf; |
---|
| 45 | |
---|
| 46 | /** append new note candidate to the note_buffer and return filtered value. we |
---|
| 47 | * need to copy the input array as fvec_median destroy its input data.*/ |
---|
| 48 | void note_append (fvec_t * note_buffer, smpl_t curnote); |
---|
| 49 | uint_t get_note (fvec_t * note_buffer, fvec_t * note_buffer2); |
---|
| 50 | |
---|
| 51 | static int aubio_process(smpl_t **input, smpl_t **output, int nframes) { |
---|
[96fb8ad] | 52 | unsigned int j; /*frames*/ |
---|
[af35ed0] | 53 | for (j=0;j<(unsigned)nframes;j++) { |
---|
[96fb8ad] | 54 | if(usejack) { |
---|
[4621cd6] | 55 | /* write input to datanew */ |
---|
| 56 | fvec_write_sample(ibuf, input[0][j], pos); |
---|
| 57 | /* put synthnew in output */ |
---|
| 58 | output[0][j] = fvec_read_sample(obuf, pos); |
---|
[96fb8ad] | 59 | } |
---|
| 60 | /*time for fft*/ |
---|
| 61 | if (pos == overlap_size-1) { |
---|
| 62 | /* block loop */ |
---|
[d4c5de7] | 63 | aubio_onset_do(o, ibuf, onset); |
---|
[96fb8ad] | 64 | |
---|
[b14107f] | 65 | aubio_pitch_do (pitchdet, ibuf, pitch_obuf); |
---|
[4621cd6] | 66 | pitch = fvec_read_sample(pitch_obuf, 0); |
---|
[a0fd4e4] | 67 | if(median){ |
---|
[bd2f2ab] | 68 | note_append(note_buffer, pitch); |
---|
[a0fd4e4] | 69 | } |
---|
[96fb8ad] | 70 | |
---|
| 71 | /* curlevel is negatif or 1 if silence */ |
---|
[660cad22] | 72 | curlevel = aubio_level_detection(ibuf, silence); |
---|
[4621cd6] | 73 | if (fvec_read_sample(onset, 0)) { |
---|
[bd2f2ab] | 74 | /* test for silence */ |
---|
| 75 | if (curlevel == 1.) { |
---|
| 76 | if (median) isready = 0; |
---|
| 77 | /* send note off */ |
---|
| 78 | send_noteon(curnote,0); |
---|
| 79 | } else { |
---|
| 80 | if (median) { |
---|
| 81 | isready = 1; |
---|
| 82 | } else { |
---|
| 83 | /* kill old note */ |
---|
| 84 | send_noteon(curnote,0); |
---|
| 85 | /* get and send new one */ |
---|
[144aff7] | 86 | send_noteon(pitch,127+(int)floor(curlevel)); |
---|
[bd2f2ab] | 87 | curnote = pitch; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | for (pos = 0; pos < overlap_size; pos++){ |
---|
[4621cd6] | 91 | obuf->data[pos] = woodblock->data[pos]; |
---|
[bd2f2ab] | 92 | } |
---|
| 93 | } |
---|
[96fb8ad] | 94 | } else { |
---|
[a0fd4e4] | 95 | if (median) { |
---|
[bd2f2ab] | 96 | if (isready > 0) |
---|
| 97 | isready++; |
---|
| 98 | if (isready == median) |
---|
| 99 | { |
---|
| 100 | /* kill old note */ |
---|
| 101 | send_noteon(curnote,0); |
---|
| 102 | newnote = get_note(note_buffer, note_buffer2); |
---|
| 103 | curnote = newnote; |
---|
| 104 | /* get and send new one */ |
---|
| 105 | if (curnote>45){ |
---|
[144aff7] | 106 | send_noteon(curnote,127+(int)floor(curlevel)); |
---|
[bd2f2ab] | 107 | } |
---|
| 108 | } |
---|
| 109 | } // if median |
---|
[96fb8ad] | 110 | for (pos = 0; pos < overlap_size; pos++) |
---|
[4621cd6] | 111 | obuf->data[pos] = 0.; |
---|
[96fb8ad] | 112 | } |
---|
| 113 | /* end of block loop */ |
---|
| 114 | pos = -1; /* so it will be zero next j loop */ |
---|
| 115 | } |
---|
| 116 | pos++; |
---|
| 117 | } |
---|
| 118 | return 1; |
---|
| 119 | } |
---|
| 120 | |
---|
[d4c5de7] | 121 | static void process_print (void) { |
---|
[bd2f2ab] | 122 | if (verbose) outmsg("%f\n",pitch); |
---|
[a0fd4e4] | 123 | } |
---|
[96fb8ad] | 124 | |
---|
[d4c5de7] | 125 | void |
---|
| 126 | note_append (fvec_t * note_buffer, smpl_t curnote) |
---|
| 127 | { |
---|
| 128 | uint_t i = 0; |
---|
| 129 | for (i = 0; i < note_buffer->length - 1; i++) { |
---|
[4621cd6] | 130 | note_buffer->data[i] = note_buffer->data[i + 1]; |
---|
[d4c5de7] | 131 | } |
---|
[4621cd6] | 132 | note_buffer->data[note_buffer->length - 1] = curnote; |
---|
[d4c5de7] | 133 | return; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | uint_t |
---|
| 137 | get_note (fvec_t * note_buffer, fvec_t * note_buffer2) |
---|
| 138 | { |
---|
[247ce7d] | 139 | uint_t i; |
---|
[d4c5de7] | 140 | for (i = 0; i < note_buffer->length; i++) { |
---|
[4621cd6] | 141 | note_buffer2->data[i] = note_buffer->data[i]; |
---|
[d4c5de7] | 142 | } |
---|
[4621cd6] | 143 | return fvec_median (note_buffer2); |
---|
[d4c5de7] | 144 | } |
---|
| 145 | |
---|
[bd2f2ab] | 146 | int main(int argc, char **argv) { |
---|
| 147 | examples_common_init(argc,argv); |
---|
[d4c5de7] | 148 | |
---|
[4621cd6] | 149 | o = new_aubio_onset (onset_mode, buffer_size, overlap_size, samplerate); |
---|
[40acfbc] | 150 | if (threshold != 0.) aubio_onset_set_threshold (o, threshold); |
---|
[4621cd6] | 151 | onset = new_fvec (1); |
---|
[d4c5de7] | 152 | |
---|
[b14107f] | 153 | pitchdet = new_aubio_pitch (pitch_mode, buffer_size * 4, |
---|
[4621cd6] | 154 | overlap_size, samplerate); |
---|
[b14107f] | 155 | aubio_pitch_set_tolerance (pitchdet, 0.7); |
---|
[4621cd6] | 156 | pitch_obuf = new_fvec (1); |
---|
[d4c5de7] | 157 | if (median) { |
---|
[4621cd6] | 158 | note_buffer = new_fvec (median); |
---|
| 159 | note_buffer2 = new_fvec (median); |
---|
[d4c5de7] | 160 | } |
---|
| 161 | |
---|
[bd2f2ab] | 162 | examples_common_process(aubio_process, process_print); |
---|
[d4c5de7] | 163 | |
---|
| 164 | send_noteon (curnote, 0); |
---|
[b14107f] | 165 | del_aubio_pitch (pitchdet); |
---|
[d4c5de7] | 166 | if (median) { |
---|
| 167 | del_fvec (note_buffer); |
---|
| 168 | del_fvec (note_buffer2); |
---|
| 169 | } |
---|
| 170 | del_fvec (pitch_obuf); |
---|
| 171 | |
---|
[bd2f2ab] | 172 | examples_common_del(); |
---|
[96fb8ad] | 173 | debug("End of program.\n"); |
---|
| 174 | fflush(stderr); |
---|
| 175 | return 0; |
---|
| 176 | } |
---|
| 177 | |
---|