[4da806c] | 1 | /* |
---|
| 2 | Copyright (C) 2014 Paul Brossier <piem@aubio.org> |
---|
| 3 | |
---|
| 4 | This file is part of aubio. |
---|
| 5 | |
---|
| 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 | */ |
---|
| 20 | |
---|
| 21 | #include "aubio_priv.h" |
---|
| 22 | #include "fvec.h" |
---|
| 23 | #include "pitch/pitch.h" |
---|
| 24 | #include "onset/onset.h" |
---|
| 25 | #include "notes/notes.h" |
---|
| 26 | |
---|
| 27 | struct _aubio_notes_t { |
---|
| 28 | |
---|
| 29 | uint_t onset_buf_size; |
---|
| 30 | uint_t pitch_buf_size; |
---|
| 31 | uint_t hop_size; |
---|
| 32 | |
---|
| 33 | uint_t samplerate; |
---|
| 34 | |
---|
| 35 | uint_t median; |
---|
| 36 | fvec_t *note_buffer; |
---|
| 37 | fvec_t *note_buffer2; |
---|
| 38 | |
---|
| 39 | aubio_pitch_t *pitch; |
---|
[4b9443c4] | 40 | fvec_t *pitch_output; |
---|
| 41 | smpl_t pitch_tolerance; |
---|
| 42 | |
---|
[4da806c] | 43 | aubio_onset_t *onset; |
---|
| 44 | fvec_t *onset_output; |
---|
[4b9443c4] | 45 | smpl_t onset_threshold; |
---|
[4da806c] | 46 | |
---|
| 47 | smpl_t curnote; |
---|
| 48 | smpl_t newnote; |
---|
[4b9443c4] | 49 | |
---|
| 50 | smpl_t silence_threshold; |
---|
| 51 | |
---|
| 52 | uint_t isready; |
---|
[4da806c] | 53 | }; |
---|
| 54 | |
---|
[dd18484] | 55 | aubio_notes_t * new_aubio_notes (const char_t * method, |
---|
[4da806c] | 56 | uint_t buf_size, uint_t hop_size, uint_t samplerate) { |
---|
| 57 | aubio_notes_t *o = AUBIO_NEW(aubio_notes_t); |
---|
| 58 | |
---|
[4b9443c4] | 59 | const char_t * onset_method = "default"; |
---|
| 60 | const char_t * pitch_method = "default"; |
---|
| 61 | |
---|
[4da806c] | 62 | o->onset_buf_size = buf_size; |
---|
| 63 | o->pitch_buf_size = buf_size * 4; |
---|
| 64 | o->hop_size = hop_size; |
---|
| 65 | |
---|
[4b9443c4] | 66 | o->onset_threshold = 0.; |
---|
| 67 | o->pitch_tolerance = 0.; |
---|
| 68 | |
---|
[4da806c] | 69 | o->samplerate = samplerate; |
---|
| 70 | |
---|
[4b9443c4] | 71 | o->median = 6; |
---|
| 72 | |
---|
| 73 | o->isready = 0; |
---|
| 74 | |
---|
| 75 | o->onset = new_aubio_onset (onset_method, o->onset_buf_size, o->hop_size, o->samplerate); |
---|
| 76 | if (o->onset_threshold != 0.) aubio_onset_set_threshold (o->onset, o->onset_threshold); |
---|
| 77 | o->onset_output = new_fvec (1); |
---|
| 78 | |
---|
| 79 | o->pitch = new_aubio_pitch (pitch_method, o->pitch_buf_size, o->hop_size, o->samplerate); |
---|
| 80 | if (o->pitch_tolerance != 0.) aubio_pitch_set_tolerance (o->pitch, o->pitch_tolerance); |
---|
| 81 | o->pitch_output = new_fvec (1); |
---|
[4da806c] | 82 | |
---|
[dd18484] | 83 | if (strcmp(method, "default") != 0) { |
---|
[790b6d7] | 84 | AUBIO_ERR("notes: unknown notes detection method \"%s\"\n", method); |
---|
[4da806c] | 85 | goto fail; |
---|
| 86 | } |
---|
| 87 | o->note_buffer = new_fvec(o->median); |
---|
| 88 | o->note_buffer2 = new_fvec(o->median); |
---|
| 89 | |
---|
| 90 | o->curnote = -1.; |
---|
[4b9443c4] | 91 | o->newnote = 0.; |
---|
| 92 | |
---|
| 93 | o->silence_threshold = -90.; |
---|
[4da806c] | 94 | |
---|
| 95 | return o; |
---|
| 96 | |
---|
| 97 | fail: |
---|
| 98 | del_aubio_notes(o); |
---|
| 99 | return NULL; |
---|
| 100 | } |
---|
| 101 | |
---|
[4b9443c4] | 102 | /** append new note candidate to the note_buffer and return filtered value. we |
---|
| 103 | * need to copy the input array as fvec_median destroy its input data.*/ |
---|
| 104 | static void |
---|
| 105 | note_append (fvec_t * note_buffer, smpl_t curnote) |
---|
| 106 | { |
---|
| 107 | uint_t i = 0; |
---|
| 108 | for (i = 0; i < note_buffer->length - 1; i++) { |
---|
| 109 | note_buffer->data[i] = note_buffer->data[i + 1]; |
---|
| 110 | } |
---|
| 111 | note_buffer->data[note_buffer->length - 1] = curnote; |
---|
| 112 | return; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | static uint_t |
---|
| 116 | aubio_notes_get_latest_note (aubio_notes_t *o) |
---|
| 117 | { |
---|
| 118 | uint_t i; |
---|
| 119 | for (i = 0; i < o->note_buffer->length; i++) { |
---|
| 120 | o->note_buffer2->data[i] = o->note_buffer->data[i]; |
---|
| 121 | } |
---|
| 122 | return fvec_median (o->note_buffer2); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | void aubio_notes_do (aubio_notes_t *o, const fvec_t * input, fvec_t * notes) |
---|
| 127 | { |
---|
| 128 | smpl_t new_pitch, curlevel; |
---|
| 129 | fvec_zeros(notes); |
---|
| 130 | aubio_onset_do(o->onset, input, o->onset_output); |
---|
| 131 | |
---|
| 132 | aubio_pitch_do (o->pitch, input, o->pitch_output); |
---|
| 133 | new_pitch = o->pitch_output->data[0]; |
---|
| 134 | if(o->median){ |
---|
| 135 | note_append(o->note_buffer, new_pitch); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /* curlevel is negatif or 1 if silence */ |
---|
| 139 | curlevel = aubio_level_detection(input, o->silence_threshold); |
---|
| 140 | if (o->onset_output->data[0] != 0) { |
---|
| 141 | /* test for silence */ |
---|
| 142 | if (curlevel == 1.) { |
---|
| 143 | if (o->median) o->isready = 0; |
---|
| 144 | /* send note off */ |
---|
| 145 | //send_noteon(o->curnote,0); |
---|
| 146 | //notes->data[0] = o->curnote; |
---|
| 147 | //notes->data[1] = 0.; |
---|
| 148 | notes->data[2] = o->curnote; |
---|
| 149 | } else { |
---|
| 150 | if (o->median) { |
---|
| 151 | o->isready = 1; |
---|
| 152 | } else { |
---|
| 153 | /* kill old note */ |
---|
| 154 | //send_noteon(o->curnote,0, o->samplerate); |
---|
| 155 | notes->data[2] = o->curnote; |
---|
| 156 | /* get and send new one */ |
---|
| 157 | //send_noteon(new_pitch,127+(int)floor(curlevel), o->samplerate); |
---|
| 158 | notes->data[0] = new_pitch; |
---|
| 159 | notes->data[1] = 127 + (int)floor(curlevel); |
---|
| 160 | o->curnote = new_pitch; |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | } else { |
---|
| 164 | if (o->median) { |
---|
| 165 | if (o->isready > 0) |
---|
| 166 | o->isready++; |
---|
| 167 | if (o->isready == o->median) |
---|
| 168 | { |
---|
| 169 | /* kill old note */ |
---|
| 170 | //send_noteon(curnote,0); |
---|
| 171 | notes->data[2] = o->curnote; |
---|
| 172 | o->newnote = aubio_notes_get_latest_note(o); |
---|
| 173 | o->curnote = o->newnote; |
---|
| 174 | /* get and send new one */ |
---|
| 175 | if (o->curnote>45){ |
---|
| 176 | //send_noteon(curnote,127+(int)floor(curlevel)); |
---|
| 177 | notes->data[0] = o->curnote; |
---|
| 178 | notes->data[1] = 127 + (int) floor(curlevel); |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | } // if median |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
[4da806c] | 185 | void del_aubio_notes (aubio_notes_t *o) { |
---|
| 186 | if (o->note_buffer) del_fvec(o->note_buffer); |
---|
| 187 | if (o->note_buffer2) del_fvec(o->note_buffer2); |
---|
[4b9443c4] | 188 | if (o->pitch_output) del_fvec(o->pitch_output); |
---|
| 189 | if (o->pitch) del_aubio_pitch(o->pitch); |
---|
| 190 | if (o->onset_output) del_fvec(o->onset_output); |
---|
| 191 | if (o->onset) del_aubio_onset(o->onset); |
---|
[4da806c] | 192 | AUBIO_FREE(o); |
---|
| 193 | } |
---|