[4da806c] | 1 | /* |
---|
[6bba139] | 2 | Copyright (C) 2014-2018 Paul Brossier <piem@aubio.org> |
---|
[4da806c] | 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 | |
---|
[0b6a8a8] | 27 | #define AUBIO_DEFAULT_NOTES_SILENCE -70. |
---|
[6bba139] | 28 | #define AUBIO_DEFAULT_NOTES_RELEASE_DROP 10. |
---|
[0b6a8a8] | 29 | // increase to 10. for .1 cent precision |
---|
| 30 | // or to 100. for .01 cent precision |
---|
| 31 | #define AUBIO_DEFAULT_CENT_PRECISION 1. |
---|
| 32 | #define AUBIO_DEFAULT_NOTES_MINIOI_MS 30. |
---|
[ffd10fb] | 33 | |
---|
[4da806c] | 34 | struct _aubio_notes_t { |
---|
| 35 | |
---|
| 36 | uint_t onset_buf_size; |
---|
| 37 | uint_t pitch_buf_size; |
---|
| 38 | uint_t hop_size; |
---|
| 39 | |
---|
| 40 | uint_t samplerate; |
---|
| 41 | |
---|
| 42 | uint_t median; |
---|
| 43 | fvec_t *note_buffer; |
---|
| 44 | fvec_t *note_buffer2; |
---|
| 45 | |
---|
| 46 | aubio_pitch_t *pitch; |
---|
[4b9443c4] | 47 | fvec_t *pitch_output; |
---|
| 48 | smpl_t pitch_tolerance; |
---|
| 49 | |
---|
[4da806c] | 50 | aubio_onset_t *onset; |
---|
| 51 | fvec_t *onset_output; |
---|
[4b9443c4] | 52 | smpl_t onset_threshold; |
---|
[4da806c] | 53 | |
---|
| 54 | smpl_t curnote; |
---|
| 55 | smpl_t newnote; |
---|
[4b9443c4] | 56 | |
---|
| 57 | smpl_t silence_threshold; |
---|
| 58 | |
---|
| 59 | uint_t isready; |
---|
[f761f06] | 60 | |
---|
| 61 | smpl_t last_onset_level; |
---|
[6bba139] | 62 | smpl_t release_drop_level; |
---|
[4da806c] | 63 | }; |
---|
| 64 | |
---|
[dd18484] | 65 | aubio_notes_t * new_aubio_notes (const char_t * method, |
---|
[4da806c] | 66 | uint_t buf_size, uint_t hop_size, uint_t samplerate) { |
---|
| 67 | aubio_notes_t *o = AUBIO_NEW(aubio_notes_t); |
---|
| 68 | |
---|
[4b9443c4] | 69 | const char_t * onset_method = "default"; |
---|
| 70 | const char_t * pitch_method = "default"; |
---|
| 71 | |
---|
[4da806c] | 72 | o->onset_buf_size = buf_size; |
---|
| 73 | o->pitch_buf_size = buf_size * 4; |
---|
| 74 | o->hop_size = hop_size; |
---|
| 75 | |
---|
[4b9443c4] | 76 | o->onset_threshold = 0.; |
---|
| 77 | o->pitch_tolerance = 0.; |
---|
| 78 | |
---|
[4da806c] | 79 | o->samplerate = samplerate; |
---|
| 80 | |
---|
[4b9443c4] | 81 | o->median = 6; |
---|
| 82 | |
---|
| 83 | o->isready = 0; |
---|
| 84 | |
---|
| 85 | o->onset = new_aubio_onset (onset_method, o->onset_buf_size, o->hop_size, o->samplerate); |
---|
[c5ee130] | 86 | if (o->onset == NULL) goto fail; |
---|
[4b9443c4] | 87 | if (o->onset_threshold != 0.) aubio_onset_set_threshold (o->onset, o->onset_threshold); |
---|
| 88 | o->onset_output = new_fvec (1); |
---|
| 89 | |
---|
| 90 | o->pitch = new_aubio_pitch (pitch_method, o->pitch_buf_size, o->hop_size, o->samplerate); |
---|
[25f280f] | 91 | if (o->pitch == NULL) goto fail; |
---|
[4b9443c4] | 92 | if (o->pitch_tolerance != 0.) aubio_pitch_set_tolerance (o->pitch, o->pitch_tolerance); |
---|
[0b6a8a8] | 93 | aubio_pitch_set_unit (o->pitch, "midi"); |
---|
[4b9443c4] | 94 | o->pitch_output = new_fvec (1); |
---|
[4da806c] | 95 | |
---|
[dd18484] | 96 | if (strcmp(method, "default") != 0) { |
---|
[790b6d7] | 97 | AUBIO_ERR("notes: unknown notes detection method \"%s\"\n", method); |
---|
[4da806c] | 98 | goto fail; |
---|
| 99 | } |
---|
| 100 | o->note_buffer = new_fvec(o->median); |
---|
| 101 | o->note_buffer2 = new_fvec(o->median); |
---|
| 102 | |
---|
[c5ee130] | 103 | if (!o->onset_output || !o->pitch_output || |
---|
| 104 | !o->note_buffer || !o->note_buffer2) goto fail; |
---|
| 105 | |
---|
[4da806c] | 106 | o->curnote = -1.; |
---|
[4b9443c4] | 107 | o->newnote = 0.; |
---|
| 108 | |
---|
[0b6a8a8] | 109 | aubio_notes_set_silence(o, AUBIO_DEFAULT_NOTES_SILENCE); |
---|
| 110 | aubio_notes_set_minioi_ms (o, AUBIO_DEFAULT_NOTES_MINIOI_MS); |
---|
[4da806c] | 111 | |
---|
[f761f06] | 112 | o->last_onset_level = AUBIO_DEFAULT_NOTES_SILENCE; |
---|
[6bba139] | 113 | o->release_drop_level = AUBIO_DEFAULT_NOTES_RELEASE_DROP; |
---|
[f761f06] | 114 | |
---|
[4da806c] | 115 | return o; |
---|
| 116 | |
---|
| 117 | fail: |
---|
| 118 | del_aubio_notes(o); |
---|
| 119 | return NULL; |
---|
| 120 | } |
---|
| 121 | |
---|
[ffd10fb] | 122 | uint_t aubio_notes_set_silence(aubio_notes_t *o, smpl_t silence) |
---|
| 123 | { |
---|
| 124 | uint_t err = AUBIO_OK; |
---|
| 125 | if (aubio_pitch_set_silence(o->pitch, silence) != AUBIO_OK) { |
---|
| 126 | err = AUBIO_FAIL; |
---|
| 127 | } |
---|
| 128 | if (aubio_onset_set_silence(o->onset, silence) != AUBIO_OK) { |
---|
| 129 | err = AUBIO_FAIL; |
---|
| 130 | } |
---|
[0606188] | 131 | o->silence_threshold = silence; |
---|
[ffd10fb] | 132 | return err; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | smpl_t aubio_notes_get_silence(const aubio_notes_t *o) |
---|
| 136 | { |
---|
| 137 | return aubio_pitch_get_silence(o->pitch); |
---|
| 138 | } |
---|
| 139 | |
---|
[17b08e6] | 140 | uint_t aubio_notes_set_minioi_ms (aubio_notes_t *o, smpl_t minioi_ms) |
---|
| 141 | { |
---|
| 142 | uint_t err = AUBIO_OK; |
---|
| 143 | if (!o->onset || (aubio_onset_set_minioi_ms(o->onset, minioi_ms) != 0)) { |
---|
| 144 | err = AUBIO_FAIL; |
---|
| 145 | } |
---|
| 146 | return err; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | smpl_t aubio_notes_get_minioi_ms(const aubio_notes_t *o) |
---|
| 150 | { |
---|
[1f6a9f8] | 151 | return aubio_onset_get_minioi_ms(o->onset); |
---|
[17b08e6] | 152 | } |
---|
| 153 | |
---|
[6bba139] | 154 | uint_t aubio_notes_set_release_drop(aubio_notes_t *o, smpl_t release_drop_level) |
---|
| 155 | { |
---|
| 156 | uint_t err = AUBIO_OK; |
---|
[4724f60] | 157 | if (release_drop_level <= 0.) { |
---|
| 158 | AUBIO_ERR("notes: release_drop should be >= 0, got %f\n", release_drop_level); |
---|
[6bba139] | 159 | err = AUBIO_FAIL; |
---|
| 160 | } else { |
---|
| 161 | o->release_drop_level = release_drop_level; |
---|
| 162 | } |
---|
| 163 | return err; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | smpl_t aubio_notes_get_release_drop(const aubio_notes_t *o) |
---|
| 167 | { |
---|
| 168 | return o->release_drop_level; |
---|
| 169 | } |
---|
| 170 | |
---|
[4b9443c4] | 171 | /** append new note candidate to the note_buffer and return filtered value. we |
---|
| 172 | * need to copy the input array as fvec_median destroy its input data.*/ |
---|
| 173 | static void |
---|
| 174 | note_append (fvec_t * note_buffer, smpl_t curnote) |
---|
| 175 | { |
---|
| 176 | uint_t i = 0; |
---|
| 177 | for (i = 0; i < note_buffer->length - 1; i++) { |
---|
| 178 | note_buffer->data[i] = note_buffer->data[i + 1]; |
---|
| 179 | } |
---|
[0b6a8a8] | 180 | //note_buffer->data[note_buffer->length - 1] = ROUND(10.*curnote)/10.; |
---|
| 181 | note_buffer->data[note_buffer->length - 1] = ROUND(AUBIO_DEFAULT_CENT_PRECISION*curnote); |
---|
[4b9443c4] | 182 | return; |
---|
| 183 | } |
---|
| 184 | |
---|
[0b6a8a8] | 185 | static smpl_t |
---|
[4b9443c4] | 186 | aubio_notes_get_latest_note (aubio_notes_t *o) |
---|
| 187 | { |
---|
[0b6a8a8] | 188 | fvec_copy(o->note_buffer, o->note_buffer2); |
---|
| 189 | return fvec_median (o->note_buffer2) / AUBIO_DEFAULT_CENT_PRECISION; |
---|
[4b9443c4] | 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | void aubio_notes_do (aubio_notes_t *o, const fvec_t * input, fvec_t * notes) |
---|
| 194 | { |
---|
| 195 | smpl_t new_pitch, curlevel; |
---|
| 196 | fvec_zeros(notes); |
---|
| 197 | aubio_onset_do(o->onset, input, o->onset_output); |
---|
| 198 | |
---|
| 199 | aubio_pitch_do (o->pitch, input, o->pitch_output); |
---|
| 200 | new_pitch = o->pitch_output->data[0]; |
---|
| 201 | if(o->median){ |
---|
| 202 | note_append(o->note_buffer, new_pitch); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | /* curlevel is negatif or 1 if silence */ |
---|
| 206 | curlevel = aubio_level_detection(input, o->silence_threshold); |
---|
| 207 | if (o->onset_output->data[0] != 0) { |
---|
| 208 | /* test for silence */ |
---|
| 209 | if (curlevel == 1.) { |
---|
| 210 | if (o->median) o->isready = 0; |
---|
| 211 | /* send note off */ |
---|
| 212 | //send_noteon(o->curnote,0); |
---|
| 213 | //notes->data[0] = o->curnote; |
---|
| 214 | //notes->data[1] = 0.; |
---|
[f761f06] | 215 | //AUBIO_WRN("notes: sending note-off at onset, not enough level\n"); |
---|
[4b9443c4] | 216 | notes->data[2] = o->curnote; |
---|
| 217 | } else { |
---|
| 218 | if (o->median) { |
---|
| 219 | o->isready = 1; |
---|
| 220 | } else { |
---|
| 221 | /* kill old note */ |
---|
| 222 | //send_noteon(o->curnote,0, o->samplerate); |
---|
[f761f06] | 223 | //AUBIO_WRN("notes: sending note-off at onset, new onset detected\n"); |
---|
[4b9443c4] | 224 | notes->data[2] = o->curnote; |
---|
| 225 | /* get and send new one */ |
---|
| 226 | //send_noteon(new_pitch,127+(int)floor(curlevel), o->samplerate); |
---|
| 227 | notes->data[0] = new_pitch; |
---|
| 228 | notes->data[1] = 127 + (int)floor(curlevel); |
---|
| 229 | o->curnote = new_pitch; |
---|
| 230 | } |
---|
[f761f06] | 231 | o->last_onset_level = curlevel; |
---|
[4b9443c4] | 232 | } |
---|
| 233 | } else { |
---|
[6bba139] | 234 | if (curlevel < o->last_onset_level - o->release_drop_level) |
---|
[f761f06] | 235 | { |
---|
| 236 | // send note off |
---|
| 237 | //AUBIO_WRN("notes: sending note-off, release detected\n"); |
---|
| 238 | notes->data[0] = 0; |
---|
| 239 | notes->data[1] = 0; |
---|
| 240 | notes->data[2] = o->curnote; |
---|
| 241 | // reset last_onset_level to silence_threshold |
---|
| 242 | o->last_onset_level = o->silence_threshold; |
---|
| 243 | o->curnote = 0; |
---|
| 244 | } |
---|
| 245 | else if (o->median) |
---|
| 246 | { |
---|
[4b9443c4] | 247 | if (o->isready > 0) |
---|
| 248 | o->isready++; |
---|
| 249 | if (o->isready == o->median) |
---|
| 250 | { |
---|
| 251 | /* kill old note */ |
---|
| 252 | //send_noteon(curnote,0); |
---|
[f761f06] | 253 | if (o->curnote != 0) |
---|
| 254 | { |
---|
| 255 | //AUBIO_WRN("notes: sending note-off, new note detected\n"); |
---|
| 256 | notes->data[2] = o->curnote; |
---|
| 257 | } |
---|
[4b9443c4] | 258 | o->newnote = aubio_notes_get_latest_note(o); |
---|
| 259 | o->curnote = o->newnote; |
---|
| 260 | /* get and send new one */ |
---|
| 261 | if (o->curnote>45){ |
---|
| 262 | //send_noteon(curnote,127+(int)floor(curlevel)); |
---|
| 263 | notes->data[0] = o->curnote; |
---|
| 264 | notes->data[1] = 127 + (int) floor(curlevel); |
---|
| 265 | } |
---|
| 266 | } |
---|
| 267 | } // if median |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | |
---|
[4da806c] | 271 | void del_aubio_notes (aubio_notes_t *o) { |
---|
| 272 | if (o->note_buffer) del_fvec(o->note_buffer); |
---|
| 273 | if (o->note_buffer2) del_fvec(o->note_buffer2); |
---|
[4b9443c4] | 274 | if (o->pitch_output) del_fvec(o->pitch_output); |
---|
| 275 | if (o->pitch) del_aubio_pitch(o->pitch); |
---|
| 276 | if (o->onset_output) del_fvec(o->onset_output); |
---|
| 277 | if (o->onset) del_aubio_onset(o->onset); |
---|
[4da806c] | 278 | AUBIO_FREE(o); |
---|
| 279 | } |
---|