[96fb8ad] | 1 | /* |
---|
| 2 | Copyright (C) 2003 Paul Brossier |
---|
| 3 | |
---|
| 4 | This program is free software; you can redistribute it and/or modify |
---|
| 5 | it under the terms of the GNU General Public License as published by |
---|
| 6 | the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | (at your option) any later version. |
---|
| 8 | |
---|
| 9 | This program is distributed in the hope that it will be useful, |
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | GNU General Public License for more details. |
---|
| 13 | |
---|
| 14 | You should have received a copy of the GNU General Public License |
---|
| 15 | along with this program; if not, write to the Free Software |
---|
| 16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 17 | */ |
---|
| 18 | |
---|
[299ad52] | 19 | #define AUBIO_UNSTABLE 1 // for fvec_median_channel |
---|
| 20 | |
---|
[96fb8ad] | 21 | #include "utils.h" |
---|
| 22 | |
---|
[d4c5de7] | 23 | /* pitch objects */ |
---|
| 24 | smpl_t pitch = 0.; |
---|
| 25 | |
---|
| 26 | uint_t median = 6; |
---|
| 27 | smpl_t curlevel = 0.; |
---|
| 28 | |
---|
[b14107f] | 29 | aubio_pitch_t *pitchdet; |
---|
[d4c5de7] | 30 | |
---|
| 31 | fvec_t *note_buffer = NULL; |
---|
| 32 | fvec_t *note_buffer2 = NULL; |
---|
| 33 | |
---|
| 34 | smpl_t curnote = 0.; |
---|
| 35 | smpl_t newnote = 0.; |
---|
| 36 | uint_t isready = 0; |
---|
[1f40359] | 37 | unsigned int pos = 0; /*frames%dspblocksize*/ |
---|
[a0fd4e4] | 38 | |
---|
[b14107f] | 39 | aubio_pitch_t *pitchdet; |
---|
[d4c5de7] | 40 | aubio_onset_t *o; |
---|
| 41 | fvec_t *onset; |
---|
| 42 | fvec_t *pitch_obuf; |
---|
| 43 | |
---|
| 44 | /** append new note candidate to the note_buffer and return filtered value. we |
---|
| 45 | * need to copy the input array as fvec_median destroy its input data.*/ |
---|
| 46 | void note_append (fvec_t * note_buffer, smpl_t curnote); |
---|
| 47 | uint_t get_note (fvec_t * note_buffer, fvec_t * note_buffer2); |
---|
| 48 | |
---|
| 49 | static int aubio_process(smpl_t **input, smpl_t **output, int nframes) { |
---|
[96fb8ad] | 50 | unsigned int i; /*channels*/ |
---|
| 51 | unsigned int j; /*frames*/ |
---|
[af35ed0] | 52 | for (j=0;j<(unsigned)nframes;j++) { |
---|
[96fb8ad] | 53 | if(usejack) { |
---|
| 54 | for (i=0;i<channels;i++) { |
---|
| 55 | /* write input to datanew */ |
---|
| 56 | fvec_write_sample(ibuf, input[i][j], i, pos); |
---|
| 57 | /* put synthnew in output */ |
---|
| 58 | output[i][j] = fvec_read_sample(obuf, i, pos); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | /*time for fft*/ |
---|
| 62 | if (pos == overlap_size-1) { |
---|
| 63 | /* block loop */ |
---|
[d4c5de7] | 64 | aubio_onset_do(o, ibuf, onset); |
---|
[96fb8ad] | 65 | |
---|
[b14107f] | 66 | aubio_pitch_do (pitchdet, ibuf, pitch_obuf); |
---|
[61316a6] | 67 | pitch = fvec_read_sample(pitch_obuf, 0, 0); |
---|
[a0fd4e4] | 68 | if(median){ |
---|
[bd2f2ab] | 69 | note_append(note_buffer, pitch); |
---|
[a0fd4e4] | 70 | } |
---|
[96fb8ad] | 71 | |
---|
| 72 | /* curlevel is negatif or 1 if silence */ |
---|
[660cad22] | 73 | curlevel = aubio_level_detection(ibuf, silence); |
---|
[d4c5de7] | 74 | if (fvec_read_sample(onset, 0, 0)) { |
---|
[bd2f2ab] | 75 | /* test for silence */ |
---|
| 76 | if (curlevel == 1.) { |
---|
| 77 | if (median) isready = 0; |
---|
| 78 | /* send note off */ |
---|
| 79 | send_noteon(curnote,0); |
---|
| 80 | } else { |
---|
| 81 | if (median) { |
---|
| 82 | isready = 1; |
---|
| 83 | } else { |
---|
| 84 | /* kill old note */ |
---|
| 85 | send_noteon(curnote,0); |
---|
| 86 | /* get and send new one */ |
---|
[144aff7] | 87 | send_noteon(pitch,127+(int)floor(curlevel)); |
---|
[bd2f2ab] | 88 | curnote = pitch; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | for (pos = 0; pos < overlap_size; pos++){ |
---|
| 92 | obuf->data[0][pos] = woodblock->data[0][pos]; |
---|
| 93 | } |
---|
| 94 | } |
---|
[96fb8ad] | 95 | } else { |
---|
[a0fd4e4] | 96 | if (median) { |
---|
[bd2f2ab] | 97 | if (isready > 0) |
---|
| 98 | isready++; |
---|
| 99 | if (isready == median) |
---|
| 100 | { |
---|
| 101 | /* kill old note */ |
---|
| 102 | send_noteon(curnote,0); |
---|
| 103 | newnote = get_note(note_buffer, note_buffer2); |
---|
| 104 | curnote = newnote; |
---|
| 105 | /* get and send new one */ |
---|
| 106 | if (curnote>45){ |
---|
[144aff7] | 107 | send_noteon(curnote,127+(int)floor(curlevel)); |
---|
[bd2f2ab] | 108 | } |
---|
| 109 | } |
---|
| 110 | } // if median |
---|
[96fb8ad] | 111 | for (pos = 0; pos < overlap_size; pos++) |
---|
| 112 | obuf->data[0][pos] = 0.; |
---|
| 113 | } |
---|
| 114 | /* end of block loop */ |
---|
| 115 | pos = -1; /* so it will be zero next j loop */ |
---|
| 116 | } |
---|
| 117 | pos++; |
---|
| 118 | } |
---|
| 119 | return 1; |
---|
| 120 | } |
---|
| 121 | |
---|
[d4c5de7] | 122 | static void process_print (void) { |
---|
[bd2f2ab] | 123 | if (verbose) outmsg("%f\n",pitch); |
---|
[a0fd4e4] | 124 | } |
---|
[96fb8ad] | 125 | |
---|
[d4c5de7] | 126 | void |
---|
| 127 | note_append (fvec_t * note_buffer, smpl_t curnote) |
---|
| 128 | { |
---|
| 129 | uint_t i = 0; |
---|
| 130 | for (i = 0; i < note_buffer->length - 1; i++) { |
---|
| 131 | note_buffer->data[0][i] = note_buffer->data[0][i + 1]; |
---|
| 132 | } |
---|
| 133 | note_buffer->data[0][note_buffer->length - 1] = curnote; |
---|
| 134 | return; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | uint_t |
---|
| 138 | get_note (fvec_t * note_buffer, fvec_t * note_buffer2) |
---|
| 139 | { |
---|
[247ce7d] | 140 | uint_t i; |
---|
[d4c5de7] | 141 | for (i = 0; i < note_buffer->length; i++) { |
---|
| 142 | note_buffer2->data[0][i] = note_buffer->data[0][i]; |
---|
| 143 | } |
---|
[247ce7d] | 144 | return fvec_median_channel (note_buffer2, 0); |
---|
[d4c5de7] | 145 | } |
---|
| 146 | |
---|
[bd2f2ab] | 147 | int main(int argc, char **argv) { |
---|
| 148 | examples_common_init(argc,argv); |
---|
[d4c5de7] | 149 | |
---|
| 150 | o = new_aubio_onset (onset_mode, buffer_size, overlap_size, channels, |
---|
| 151 | samplerate); |
---|
[40acfbc] | 152 | if (threshold != 0.) aubio_onset_set_threshold (o, threshold); |
---|
[d4c5de7] | 153 | onset = new_fvec (1, channels); |
---|
| 154 | |
---|
[b14107f] | 155 | pitchdet = new_aubio_pitch (pitch_mode, buffer_size * 4, |
---|
[d4c5de7] | 156 | overlap_size, channels, samplerate); |
---|
[b14107f] | 157 | aubio_pitch_set_tolerance (pitchdet, 0.7); |
---|
[d4c5de7] | 158 | pitch_obuf = new_fvec (1, channels); |
---|
| 159 | if (median) { |
---|
| 160 | note_buffer = new_fvec (median, 1); |
---|
| 161 | note_buffer2 = new_fvec (median, 1); |
---|
| 162 | } |
---|
| 163 | |
---|
[bd2f2ab] | 164 | examples_common_process(aubio_process, process_print); |
---|
[d4c5de7] | 165 | |
---|
| 166 | send_noteon (curnote, 0); |
---|
[b14107f] | 167 | del_aubio_pitch (pitchdet); |
---|
[d4c5de7] | 168 | if (median) { |
---|
| 169 | del_fvec (note_buffer); |
---|
| 170 | del_fvec (note_buffer2); |
---|
| 171 | } |
---|
| 172 | del_fvec (pitch_obuf); |
---|
| 173 | |
---|
[bd2f2ab] | 174 | examples_common_del(); |
---|
[96fb8ad] | 175 | debug("End of program.\n"); |
---|
| 176 | fflush(stderr); |
---|
| 177 | return 0; |
---|
| 178 | } |
---|
| 179 | |
---|