[96fb8ad] | 1 | /* |
---|
[e6a78ea] | 2 | Copyright (C) 2003-2009 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 | */ |
---|
[96fb8ad] | 20 | |
---|
| 21 | #include "aubio_priv.h" |
---|
[6c7d49b] | 22 | #include "fvec.h" |
---|
| 23 | #include "cvec.h" |
---|
[a4364b8] | 24 | #include "lvec.h" |
---|
[96fb8ad] | 25 | #include "mathutils.h" |
---|
[83963b3] | 26 | #include "musicutils.h" |
---|
| 27 | #include "spectral/phasevoc.h" |
---|
[a695854] | 28 | #include "temporal/filter.h" |
---|
[c159aeb] | 29 | #include "temporal/c_weighting.h" |
---|
[2d8cffa] | 30 | #include "pitch/pitchmcomb.h" |
---|
| 31 | #include "pitch/pitchyin.h" |
---|
| 32 | #include "pitch/pitchfcomb.h" |
---|
| 33 | #include "pitch/pitchschmitt.h" |
---|
| 34 | #include "pitch/pitchyinfft.h" |
---|
[c268bf2] | 35 | #include "pitch/pitchyinfast.h" |
---|
[95dc7f2] | 36 | #include "pitch/pitchspecacf.h" |
---|
[ca1abdd] | 37 | #include "pitch/pitch.h" |
---|
[96fb8ad] | 38 | |
---|
[8c3f717] | 39 | #define DEFAULT_PITCH_SILENCE -50. |
---|
| 40 | |
---|
[a64ef1d] | 41 | /** pitch detection algorithms */ |
---|
[fddfa64] | 42 | typedef enum |
---|
| 43 | { |
---|
[a64ef1d] | 44 | aubio_pitcht_yin, /**< `yin`, YIN algorithm */ |
---|
| 45 | aubio_pitcht_mcomb, /**< `mcomb`, Multi-comb filter */ |
---|
| 46 | aubio_pitcht_schmitt, /**< `schmitt`, Schmitt trigger */ |
---|
| 47 | aubio_pitcht_fcomb, /**< `fcomb`, Fast comb filter */ |
---|
| 48 | aubio_pitcht_yinfft, /**< `yinfft`, Spectral YIN */ |
---|
[c268bf2] | 49 | aubio_pitcht_yinfast, /**< `yinfast`, YIN fast */ |
---|
[95dc7f2] | 50 | aubio_pitcht_specacf, /**< `specacf`, Spectral autocorrelation */ |
---|
[f5699b9] | 51 | aubio_pitcht_crepe, /**< `crepe`, convolutional neural network */ |
---|
[a64ef1d] | 52 | aubio_pitcht_default |
---|
| 53 | = aubio_pitcht_yinfft, /**< `default` */ |
---|
[ca1abdd] | 54 | } aubio_pitch_type; |
---|
[fe163ad] | 55 | |
---|
[a64ef1d] | 56 | /** pitch detection output modes */ |
---|
[fddfa64] | 57 | typedef enum |
---|
| 58 | { |
---|
[fe163ad] | 59 | aubio_pitchm_freq, /**< Frequency (Hz) */ |
---|
| 60 | aubio_pitchm_midi, /**< MIDI note (0.,127) */ |
---|
| 61 | aubio_pitchm_cent, /**< Cent */ |
---|
| 62 | aubio_pitchm_bin, /**< Frequency bin (0,bufsize) */ |
---|
| 63 | aubio_pitchm_default = aubio_pitchm_freq, /**< the one used when "default" is asked */ |
---|
[ca1abdd] | 64 | } aubio_pitch_mode; |
---|
[fe163ad] | 65 | |
---|
[b130600] | 66 | /** callback to get pitch candidate, defined below */ |
---|
[ce3ff2b] | 67 | typedef void (*aubio_pitch_detect_t) (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf); |
---|
[6d4ec49] | 68 | |
---|
[b130600] | 69 | /** callback to convert pitch from one unit to another, defined below */ |
---|
| 70 | typedef smpl_t(*aubio_pitch_convert_t) (smpl_t value, uint_t samplerate, uint_t bufsize); |
---|
[5284e0d] | 71 | |
---|
[b130600] | 72 | /** callback to fetch the confidence of the algorithm */ |
---|
| 73 | typedef smpl_t (*aubio_pitch_get_conf_t) (void * p); |
---|
[f44b111] | 74 | |
---|
[475da2f] | 75 | /** generic pitch detection structure */ |
---|
[fddfa64] | 76 | struct _aubio_pitch_t |
---|
| 77 | { |
---|
[b130600] | 78 | aubio_pitch_type type; /**< pitch detection mode */ |
---|
| 79 | aubio_pitch_mode mode; /**< pitch detection output mode */ |
---|
| 80 | uint_t samplerate; /**< samplerate */ |
---|
[475da2f] | 81 | uint_t bufsize; /**< buffer size */ |
---|
[b130600] | 82 | void *p_object; /**< pointer to pitch object */ |
---|
[fddfa64] | 83 | aubio_filter_t *filter; /**< filter */ |
---|
[ce3ff2b] | 84 | fvec_t *filtered; /**< filtered input */ |
---|
[fddfa64] | 85 | aubio_pvoc_t *pv; /**< phase vocoder for mcomb */ |
---|
| 86 | cvec_t *fftgrain; /**< spectral frame for mcomb */ |
---|
| 87 | fvec_t *buf; /**< temporary buffer for yin */ |
---|
[b130600] | 88 | aubio_pitch_detect_t detect_cb; /**< callback to get the pitch candidates */ |
---|
| 89 | aubio_pitch_convert_t conv_cb; /**< callback to convert it to the desired unit */ |
---|
| 90 | aubio_pitch_get_conf_t conf_cb; /**< pointer to the current confidence callback */ |
---|
[8c3f717] | 91 | smpl_t silence; /**< silence threshold */ |
---|
[96fb8ad] | 92 | }; |
---|
| 93 | |
---|
[b130600] | 94 | /* callback functions for pitch detection */ |
---|
[ce3ff2b] | 95 | static void aubio_pitch_do_mcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf); |
---|
| 96 | static void aubio_pitch_do_yin (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf); |
---|
| 97 | static void aubio_pitch_do_schmitt (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf); |
---|
| 98 | static void aubio_pitch_do_fcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf); |
---|
| 99 | static void aubio_pitch_do_yinfft (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf); |
---|
[c268bf2] | 100 | static void aubio_pitch_do_yinfast (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf); |
---|
[ce3ff2b] | 101 | static void aubio_pitch_do_specacf (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf); |
---|
[f5699b9] | 102 | static void aubio_pitch_do_crepe (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf); |
---|
[3ec9d9c] | 103 | |
---|
[6388c37] | 104 | /* internal functions for frequency conversion */ |
---|
| 105 | static smpl_t freqconvbin (smpl_t f, uint_t samplerate, uint_t bufsize); |
---|
| 106 | static smpl_t freqconvmidi (smpl_t f, uint_t samplerate, uint_t bufsize); |
---|
| 107 | static smpl_t freqconvpass (smpl_t f, uint_t samplerate, uint_t bufsize); |
---|
[b130600] | 108 | |
---|
[f5699b9] | 109 | typedef struct _aubio_pitch_crepe_t aubio_pitch_crepe_t; |
---|
| 110 | extern aubio_pitch_crepe_t *new_aubio_pitch_crepe(void); |
---|
| 111 | extern void aubio_pitch_crepe_do(aubio_pitch_crepe_t *t, fvec_t *input, fvec_t *out); |
---|
| 112 | extern void del_aubio_pitch_crepe(aubio_pitch_crepe_t *t); |
---|
| 113 | extern smpl_t aubio_pitch_crepe_get_confidence (aubio_pitch_crepe_t * o); |
---|
| 114 | uint_t aubio_pitch_crepe_set_tolerance(aubio_pitch_crepe_t * o, smpl_t |
---|
| 115 | tolerance); |
---|
| 116 | smpl_t aubio_pitch_crepe_get_tolerance (aubio_pitch_crepe_t * o); |
---|
| 117 | |
---|
[b130600] | 118 | /* adapter to stack ibuf new samples at the end of buf, and trim `buf` to `bufsize` */ |
---|
[ce3ff2b] | 119 | void aubio_pitch_slideblock (aubio_pitch_t * p, const fvec_t * ibuf); |
---|
[3ec9d9c] | 120 | |
---|
| 121 | |
---|
[ca1abdd] | 122 | aubio_pitch_t * |
---|
[ce3ff2b] | 123 | new_aubio_pitch (const char_t * pitch_mode, |
---|
[168337e] | 124 | uint_t bufsize, uint_t hopsize, uint_t samplerate) |
---|
[96fb8ad] | 125 | { |
---|
[fddfa64] | 126 | aubio_pitch_t *p = AUBIO_NEW (aubio_pitch_t); |
---|
[ca1abdd] | 127 | aubio_pitch_type pitch_type; |
---|
[3f512b8] | 128 | if (pitch_mode == NULL) { |
---|
| 129 | AUBIO_ERR ("pitch: can not use ‘NULL‘ for pitch detection method\n"); |
---|
| 130 | goto beach; |
---|
| 131 | } |
---|
[fe163ad] | 132 | if (strcmp (pitch_mode, "mcomb") == 0) |
---|
[fddfa64] | 133 | pitch_type = aubio_pitcht_mcomb; |
---|
[c268bf2] | 134 | else if (strcmp (pitch_mode, "yinfast") == 0) |
---|
| 135 | pitch_type = aubio_pitcht_yinfast; |
---|
[fe163ad] | 136 | else if (strcmp (pitch_mode, "yinfft") == 0) |
---|
[97a5878b] | 137 | pitch_type = aubio_pitcht_yinfft; |
---|
[fe163ad] | 138 | else if (strcmp (pitch_mode, "yin") == 0) |
---|
[fddfa64] | 139 | pitch_type = aubio_pitcht_yin; |
---|
[fe163ad] | 140 | else if (strcmp (pitch_mode, "schmitt") == 0) |
---|
[fddfa64] | 141 | pitch_type = aubio_pitcht_schmitt; |
---|
[fe163ad] | 142 | else if (strcmp (pitch_mode, "fcomb") == 0) |
---|
[fddfa64] | 143 | pitch_type = aubio_pitcht_fcomb; |
---|
[95dc7f2] | 144 | else if (strcmp (pitch_mode, "specacf") == 0) |
---|
| 145 | pitch_type = aubio_pitcht_specacf; |
---|
[f5699b9] | 146 | else if (strcmp (pitch_mode, "crepe") == 0) |
---|
| 147 | pitch_type = aubio_pitcht_crepe; |
---|
[fe163ad] | 148 | else if (strcmp (pitch_mode, "default") == 0) |
---|
[fddfa64] | 149 | pitch_type = aubio_pitcht_default; |
---|
[fe163ad] | 150 | else { |
---|
[afa21cdc] | 151 | AUBIO_ERR ("pitch: unknown pitch detection method ‘%s’\n", pitch_mode); |
---|
| 152 | goto beach; |
---|
[fe163ad] | 153 | } |
---|
[2abe563] | 154 | |
---|
| 155 | // check parameters are valid |
---|
| 156 | if ((sint_t)hopsize < 1) { |
---|
[d04875c] | 157 | AUBIO_ERR("pitch: got hopsize %d, but can not be < 1\n", hopsize); |
---|
[2abe563] | 158 | goto beach; |
---|
| 159 | } else if ((sint_t)bufsize < 1) { |
---|
[d04875c] | 160 | AUBIO_ERR("pitch: got buffer_size %d, but can not be < 1\n", bufsize); |
---|
[2abe563] | 161 | goto beach; |
---|
| 162 | } else if (bufsize < hopsize) { |
---|
[724922c] | 163 | AUBIO_ERR("pitch: hop size (%d) is larger than win size (%d)\n", hopsize, bufsize); |
---|
[2abe563] | 164 | goto beach; |
---|
| 165 | } else if ((sint_t)samplerate < 1) { |
---|
[d04875c] | 166 | AUBIO_ERR("pitch: samplerate (%d) can not be < 1\n", samplerate); |
---|
[2abe563] | 167 | goto beach; |
---|
| 168 | } |
---|
| 169 | |
---|
[b130600] | 170 | p->samplerate = samplerate; |
---|
[fe163ad] | 171 | p->type = pitch_type; |
---|
[ca1abdd] | 172 | aubio_pitch_set_unit (p, "default"); |
---|
[6d4ec49] | 173 | p->bufsize = bufsize; |
---|
[8c3f717] | 174 | p->silence = DEFAULT_PITCH_SILENCE; |
---|
[b130600] | 175 | p->conf_cb = NULL; |
---|
[fddfa64] | 176 | switch (p->type) { |
---|
[ca1abdd] | 177 | case aubio_pitcht_yin: |
---|
[168337e] | 178 | p->buf = new_fvec (bufsize); |
---|
[b130600] | 179 | p->p_object = new_aubio_pitchyin (bufsize); |
---|
[4cb7a0a] | 180 | if (!p->p_object) goto beach; |
---|
[b130600] | 181 | p->detect_cb = aubio_pitch_do_yin; |
---|
| 182 | p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyin_get_confidence; |
---|
| 183 | aubio_pitchyin_set_tolerance (p->p_object, 0.15); |
---|
[6d4ec49] | 184 | break; |
---|
[ca1abdd] | 185 | case aubio_pitcht_mcomb: |
---|
[ce3ff2b] | 186 | p->filtered = new_fvec (hopsize); |
---|
[168337e] | 187 | p->pv = new_aubio_pvoc (bufsize, hopsize); |
---|
[4cb7a0a] | 188 | if (!p->pv) goto beach; |
---|
[168337e] | 189 | p->fftgrain = new_cvec (bufsize); |
---|
[b130600] | 190 | p->p_object = new_aubio_pitchmcomb (bufsize, hopsize); |
---|
[168337e] | 191 | p->filter = new_aubio_filter_c_weighting (samplerate); |
---|
[b130600] | 192 | p->detect_cb = aubio_pitch_do_mcomb; |
---|
[6d4ec49] | 193 | break; |
---|
[ca1abdd] | 194 | case aubio_pitcht_fcomb: |
---|
[168337e] | 195 | p->buf = new_fvec (bufsize); |
---|
[b130600] | 196 | p->p_object = new_aubio_pitchfcomb (bufsize, hopsize); |
---|
[4cb7a0a] | 197 | if (!p->p_object) goto beach; |
---|
[b130600] | 198 | p->detect_cb = aubio_pitch_do_fcomb; |
---|
[6d4ec49] | 199 | break; |
---|
[ca1abdd] | 200 | case aubio_pitcht_schmitt: |
---|
[168337e] | 201 | p->buf = new_fvec (bufsize); |
---|
[b130600] | 202 | p->p_object = new_aubio_pitchschmitt (bufsize); |
---|
| 203 | p->detect_cb = aubio_pitch_do_schmitt; |
---|
[6d4ec49] | 204 | break; |
---|
[ca1abdd] | 205 | case aubio_pitcht_yinfft: |
---|
[168337e] | 206 | p->buf = new_fvec (bufsize); |
---|
[9c9202f] | 207 | p->p_object = new_aubio_pitchyinfft (samplerate, bufsize); |
---|
[4cb7a0a] | 208 | if (!p->p_object) goto beach; |
---|
[b130600] | 209 | p->detect_cb = aubio_pitch_do_yinfft; |
---|
| 210 | p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyinfft_get_confidence; |
---|
| 211 | aubio_pitchyinfft_set_tolerance (p->p_object, 0.85); |
---|
[6d4ec49] | 212 | break; |
---|
[c268bf2] | 213 | case aubio_pitcht_yinfast: |
---|
| 214 | p->buf = new_fvec (bufsize); |
---|
| 215 | p->p_object = new_aubio_pitchyinfast (bufsize); |
---|
| 216 | if (!p->p_object) goto beach; |
---|
| 217 | p->detect_cb = aubio_pitch_do_yinfast; |
---|
| 218 | p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyinfast_get_confidence; |
---|
| 219 | aubio_pitchyinfast_set_tolerance (p->p_object, 0.15); |
---|
| 220 | break; |
---|
[95dc7f2] | 221 | case aubio_pitcht_specacf: |
---|
| 222 | p->buf = new_fvec (bufsize); |
---|
| 223 | p->p_object = new_aubio_pitchspecacf (bufsize); |
---|
[4cb7a0a] | 224 | if (!p->p_object) goto beach; |
---|
[95dc7f2] | 225 | p->detect_cb = aubio_pitch_do_specacf; |
---|
| 226 | p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchspecacf_get_tolerance; |
---|
| 227 | aubio_pitchspecacf_set_tolerance (p->p_object, 0.85); |
---|
| 228 | break; |
---|
[f5699b9] | 229 | case aubio_pitcht_crepe: |
---|
[259c33b] | 230 | // TODO add resampling and blocking |
---|
| 231 | if (samplerate != 16000) { |
---|
| 232 | AUBIO_ERROR("pitch: crepe samplerate must be 16000Hz, got %d\n", |
---|
| 233 | samplerate); |
---|
| 234 | goto beach; |
---|
| 235 | } |
---|
| 236 | if (bufsize != 1024) { |
---|
| 237 | AUBIO_ERROR("pitch: crepe buffer size must be 1024, got %d\n", |
---|
| 238 | bufsize); |
---|
| 239 | goto beach; |
---|
| 240 | } |
---|
[f5699b9] | 241 | p->buf = new_fvec (bufsize); |
---|
| 242 | p->p_object = new_aubio_pitch_crepe(); |
---|
| 243 | if (!p->p_object) goto beach; |
---|
| 244 | p->detect_cb = aubio_pitch_do_crepe; |
---|
| 245 | p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitch_crepe_get_confidence; |
---|
| 246 | //aubio_pitch_crepe_set_tolerance (p->p_object, 0.85); |
---|
| 247 | break; |
---|
[6d4ec49] | 248 | default: |
---|
| 249 | break; |
---|
| 250 | } |
---|
| 251 | return p; |
---|
[2abe563] | 252 | |
---|
| 253 | beach: |
---|
[4cb7a0a] | 254 | if (p->filtered) del_fvec(p->filtered); |
---|
| 255 | if (p->buf) del_fvec(p->buf); |
---|
[2abe563] | 256 | AUBIO_FREE(p); |
---|
| 257 | return NULL; |
---|
[96fb8ad] | 258 | } |
---|
| 259 | |
---|
[fddfa64] | 260 | void |
---|
| 261 | del_aubio_pitch (aubio_pitch_t * p) |
---|
| 262 | { |
---|
| 263 | switch (p->type) { |
---|
[ca1abdd] | 264 | case aubio_pitcht_yin: |
---|
[fddfa64] | 265 | del_fvec (p->buf); |
---|
[b130600] | 266 | del_aubio_pitchyin (p->p_object); |
---|
[6d4ec49] | 267 | break; |
---|
[ca1abdd] | 268 | case aubio_pitcht_mcomb: |
---|
[ce3ff2b] | 269 | del_fvec (p->filtered); |
---|
[fddfa64] | 270 | del_aubio_pvoc (p->pv); |
---|
| 271 | del_cvec (p->fftgrain); |
---|
| 272 | del_aubio_filter (p->filter); |
---|
[b130600] | 273 | del_aubio_pitchmcomb (p->p_object); |
---|
[6d4ec49] | 274 | break; |
---|
[ca1abdd] | 275 | case aubio_pitcht_schmitt: |
---|
[fddfa64] | 276 | del_fvec (p->buf); |
---|
[b130600] | 277 | del_aubio_pitchschmitt (p->p_object); |
---|
[6d4ec49] | 278 | break; |
---|
[ca1abdd] | 279 | case aubio_pitcht_fcomb: |
---|
[fddfa64] | 280 | del_fvec (p->buf); |
---|
[b130600] | 281 | del_aubio_pitchfcomb (p->p_object); |
---|
[6d4ec49] | 282 | break; |
---|
[ca1abdd] | 283 | case aubio_pitcht_yinfft: |
---|
[fddfa64] | 284 | del_fvec (p->buf); |
---|
[b130600] | 285 | del_aubio_pitchyinfft (p->p_object); |
---|
[6d4ec49] | 286 | break; |
---|
[c268bf2] | 287 | case aubio_pitcht_yinfast: |
---|
| 288 | del_fvec (p->buf); |
---|
| 289 | del_aubio_pitchyinfast (p->p_object); |
---|
| 290 | break; |
---|
[95dc7f2] | 291 | case aubio_pitcht_specacf: |
---|
| 292 | del_fvec (p->buf); |
---|
| 293 | del_aubio_pitchspecacf (p->p_object); |
---|
| 294 | break; |
---|
[f5699b9] | 295 | case aubio_pitcht_crepe: |
---|
| 296 | del_fvec (p->buf); |
---|
| 297 | del_aubio_pitch_crepe (p->p_object); |
---|
| 298 | break; |
---|
[6d4ec49] | 299 | default: |
---|
| 300 | break; |
---|
| 301 | } |
---|
[fddfa64] | 302 | AUBIO_FREE (p); |
---|
[96fb8ad] | 303 | } |
---|
| 304 | |
---|
[fddfa64] | 305 | void |
---|
[ce3ff2b] | 306 | aubio_pitch_slideblock (aubio_pitch_t * p, const fvec_t * ibuf) |
---|
[fddfa64] | 307 | { |
---|
[6c8ef58] | 308 | uint_t overlap_size = p->buf->length - ibuf->length; |
---|
[094a8be] | 309 | #if 1 //!HAVE_MEMCPY_HACKS |
---|
[6c8ef58] | 310 | uint_t j; |
---|
[168337e] | 311 | for (j = 0; j < overlap_size; j++) { |
---|
| 312 | p->buf->data[j] = p->buf->data[j + ibuf->length]; |
---|
[6d4ec49] | 313 | } |
---|
[168337e] | 314 | for (j = 0; j < ibuf->length; j++) { |
---|
| 315 | p->buf->data[j + overlap_size] = ibuf->data[j]; |
---|
[6d4ec49] | 316 | } |
---|
[6c8ef58] | 317 | #else |
---|
| 318 | smpl_t *data = p->buf->data; |
---|
| 319 | smpl_t *newdata = ibuf->data; |
---|
| 320 | memmove(data, data + ibuf->length, overlap_size); |
---|
| 321 | memcpy(data + overlap_size, newdata, ibuf->length); |
---|
| 322 | #endif |
---|
[651b97e] | 323 | } |
---|
| 324 | |
---|
[fddfa64] | 325 | uint_t |
---|
[ce3ff2b] | 326 | aubio_pitch_set_unit (aubio_pitch_t * p, const char_t * pitch_unit) |
---|
[fddfa64] | 327 | { |
---|
[21e2e6db] | 328 | uint_t err = AUBIO_OK; |
---|
[ca1abdd] | 329 | aubio_pitch_mode pitch_mode; |
---|
[fe163ad] | 330 | if (strcmp (pitch_unit, "freq") == 0) |
---|
[fddfa64] | 331 | pitch_mode = aubio_pitchm_freq; |
---|
[53d1497] | 332 | else if (strcmp (pitch_unit, "hertz") == 0) |
---|
| 333 | pitch_mode = aubio_pitchm_freq; |
---|
[5a2a6c6] | 334 | else if (strcmp (pitch_unit, "Hertz") == 0) |
---|
| 335 | pitch_mode = aubio_pitchm_freq; |
---|
[53d1497] | 336 | else if (strcmp (pitch_unit, "Hz") == 0) |
---|
| 337 | pitch_mode = aubio_pitchm_freq; |
---|
| 338 | else if (strcmp (pitch_unit, "f0") == 0) |
---|
| 339 | pitch_mode = aubio_pitchm_freq; |
---|
[fe163ad] | 340 | else if (strcmp (pitch_unit, "midi") == 0) |
---|
[fddfa64] | 341 | pitch_mode = aubio_pitchm_midi; |
---|
[fe163ad] | 342 | else if (strcmp (pitch_unit, "cent") == 0) |
---|
[fddfa64] | 343 | pitch_mode = aubio_pitchm_cent; |
---|
[fe163ad] | 344 | else if (strcmp (pitch_unit, "bin") == 0) |
---|
[fddfa64] | 345 | pitch_mode = aubio_pitchm_bin; |
---|
[fe163ad] | 346 | else if (strcmp (pitch_unit, "default") == 0) |
---|
[fddfa64] | 347 | pitch_mode = aubio_pitchm_default; |
---|
[fe163ad] | 348 | else { |
---|
[afa21cdc] | 349 | AUBIO_WRN("pitch: unknown pitch detection unit ‘%s’, using default\n", |
---|
| 350 | pitch_unit); |
---|
[fddfa64] | 351 | pitch_mode = aubio_pitchm_default; |
---|
[21e2e6db] | 352 | err = AUBIO_FAIL; |
---|
[fe163ad] | 353 | } |
---|
| 354 | p->mode = pitch_mode; |
---|
[fddfa64] | 355 | switch (p->mode) { |
---|
[fe163ad] | 356 | case aubio_pitchm_freq: |
---|
[b130600] | 357 | p->conv_cb = freqconvpass; |
---|
[fe163ad] | 358 | break; |
---|
| 359 | case aubio_pitchm_midi: |
---|
[b130600] | 360 | p->conv_cb = freqconvmidi; |
---|
[fe163ad] | 361 | break; |
---|
| 362 | case aubio_pitchm_cent: |
---|
| 363 | /* bug: not implemented */ |
---|
[b130600] | 364 | p->conv_cb = freqconvmidi; |
---|
[fe163ad] | 365 | break; |
---|
| 366 | case aubio_pitchm_bin: |
---|
[b130600] | 367 | p->conv_cb = freqconvbin; |
---|
[fe163ad] | 368 | break; |
---|
| 369 | default: |
---|
| 370 | break; |
---|
| 371 | } |
---|
[21e2e6db] | 372 | return err; |
---|
[fe163ad] | 373 | } |
---|
| 374 | |
---|
[fddfa64] | 375 | uint_t |
---|
| 376 | aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol) |
---|
| 377 | { |
---|
| 378 | switch (p->type) { |
---|
[ca1abdd] | 379 | case aubio_pitcht_yin: |
---|
[b130600] | 380 | aubio_pitchyin_set_tolerance (p->p_object, tol); |
---|
[7a6cbbe] | 381 | break; |
---|
[ca1abdd] | 382 | case aubio_pitcht_yinfft: |
---|
[b130600] | 383 | aubio_pitchyinfft_set_tolerance (p->p_object, tol); |
---|
[7a6cbbe] | 384 | break; |
---|
[c268bf2] | 385 | case aubio_pitcht_yinfast: |
---|
| 386 | aubio_pitchyinfast_set_tolerance (p->p_object, tol); |
---|
| 387 | break; |
---|
[f5699b9] | 388 | case aubio_pitcht_crepe: |
---|
| 389 | aubio_pitch_crepe_set_tolerance (p->p_object, tol); |
---|
| 390 | break; |
---|
[7a6cbbe] | 391 | default: |
---|
| 392 | break; |
---|
| 393 | } |
---|
[93177fa] | 394 | return AUBIO_OK; |
---|
[f8a38c5] | 395 | } |
---|
| 396 | |
---|
[0cf2b44] | 397 | smpl_t |
---|
| 398 | aubio_pitch_get_tolerance (aubio_pitch_t * p) |
---|
| 399 | { |
---|
| 400 | smpl_t tolerance = 1.; |
---|
| 401 | switch (p->type) { |
---|
| 402 | case aubio_pitcht_yin: |
---|
| 403 | tolerance = aubio_pitchyin_get_tolerance (p->p_object); |
---|
| 404 | break; |
---|
| 405 | case aubio_pitcht_yinfft: |
---|
| 406 | tolerance = aubio_pitchyinfft_get_tolerance (p->p_object); |
---|
| 407 | break; |
---|
[c268bf2] | 408 | case aubio_pitcht_yinfast: |
---|
| 409 | tolerance = aubio_pitchyinfast_get_tolerance (p->p_object); |
---|
| 410 | break; |
---|
[f5699b9] | 411 | case aubio_pitcht_crepe: |
---|
| 412 | tolerance = aubio_pitch_crepe_get_tolerance (p->p_object); |
---|
| 413 | break; |
---|
[0cf2b44] | 414 | default: |
---|
| 415 | break; |
---|
| 416 | } |
---|
| 417 | return tolerance; |
---|
| 418 | } |
---|
| 419 | |
---|
[8c3f717] | 420 | uint_t |
---|
| 421 | aubio_pitch_set_silence (aubio_pitch_t * p, smpl_t silence) |
---|
| 422 | { |
---|
[973eb75] | 423 | if (silence <= 0 && silence >= -200) { |
---|
[8c3f717] | 424 | p->silence = silence; |
---|
| 425 | return AUBIO_OK; |
---|
| 426 | } else { |
---|
[afa21cdc] | 427 | AUBIO_WRN("pitch: could not set silence to %.2f\n", silence); |
---|
[8c3f717] | 428 | return AUBIO_FAIL; |
---|
| 429 | } |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | smpl_t |
---|
| 433 | aubio_pitch_get_silence (aubio_pitch_t * p) |
---|
| 434 | { |
---|
| 435 | return p->silence; |
---|
| 436 | } |
---|
| 437 | |
---|
[b130600] | 438 | |
---|
| 439 | /* do method, calling the detection callback, then the conversion callback */ |
---|
[fddfa64] | 440 | void |
---|
[ce3ff2b] | 441 | aubio_pitch_do (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf) |
---|
[fddfa64] | 442 | { |
---|
[b130600] | 443 | p->detect_cb (p, ibuf, obuf); |
---|
[8c3f717] | 444 | if (aubio_silence_detection(ibuf, p->silence) == 1) { |
---|
| 445 | obuf->data[0] = 0.; |
---|
| 446 | } |
---|
[b130600] | 447 | obuf->data[0] = p->conv_cb (obuf->data[0], p->samplerate, p->bufsize); |
---|
[c078336] | 448 | } |
---|
| 449 | |
---|
[b130600] | 450 | /* do method for each algorithm */ |
---|
[fddfa64] | 451 | void |
---|
[ce3ff2b] | 452 | aubio_pitch_do_mcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf) |
---|
[fddfa64] | 453 | { |
---|
[ce3ff2b] | 454 | aubio_filter_do_outplace (p->filter, ibuf, p->filtered); |
---|
[fddfa64] | 455 | aubio_pvoc_do (p->pv, ibuf, p->fftgrain); |
---|
[b130600] | 456 | aubio_pitchmcomb_do (p->p_object, p->fftgrain, obuf); |
---|
| 457 | obuf->data[0] = aubio_bintofreq (obuf->data[0], p->samplerate, p->bufsize); |
---|
[c078336] | 458 | } |
---|
| 459 | |
---|
[fddfa64] | 460 | void |
---|
[ce3ff2b] | 461 | aubio_pitch_do_yin (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf) |
---|
[fddfa64] | 462 | { |
---|
[6d4ec49] | 463 | smpl_t pitch = 0.; |
---|
[fddfa64] | 464 | aubio_pitch_slideblock (p, ibuf); |
---|
[b130600] | 465 | aubio_pitchyin_do (p->p_object, p->buf, obuf); |
---|
[168337e] | 466 | pitch = obuf->data[0]; |
---|
| 467 | if (pitch > 0) { |
---|
[b130600] | 468 | pitch = p->samplerate / (pitch + 0.); |
---|
[168337e] | 469 | } else { |
---|
| 470 | pitch = 0.; |
---|
[6d4ec49] | 471 | } |
---|
[168337e] | 472 | obuf->data[0] = pitch; |
---|
[c078336] | 473 | } |
---|
| 474 | |
---|
| 475 | |
---|
[fddfa64] | 476 | void |
---|
[ce3ff2b] | 477 | aubio_pitch_do_yinfft (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf) |
---|
[fddfa64] | 478 | { |
---|
[6d4ec49] | 479 | smpl_t pitch = 0.; |
---|
[fddfa64] | 480 | aubio_pitch_slideblock (p, ibuf); |
---|
[b130600] | 481 | aubio_pitchyinfft_do (p->p_object, p->buf, obuf); |
---|
[168337e] | 482 | pitch = obuf->data[0]; |
---|
| 483 | if (pitch > 0) { |
---|
[b130600] | 484 | pitch = p->samplerate / (pitch + 0.); |
---|
[168337e] | 485 | } else { |
---|
| 486 | pitch = 0.; |
---|
[6d4ec49] | 487 | } |
---|
[168337e] | 488 | obuf->data[0] = pitch; |
---|
[650e39b] | 489 | } |
---|
| 490 | |
---|
[fddfa64] | 491 | void |
---|
[c268bf2] | 492 | aubio_pitch_do_yinfast (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf) |
---|
| 493 | { |
---|
| 494 | smpl_t pitch = 0.; |
---|
| 495 | aubio_pitch_slideblock (p, ibuf); |
---|
| 496 | aubio_pitchyinfast_do (p->p_object, p->buf, obuf); |
---|
| 497 | pitch = obuf->data[0]; |
---|
| 498 | if (pitch > 0) { |
---|
| 499 | pitch = p->samplerate / (pitch + 0.); |
---|
| 500 | } else { |
---|
| 501 | pitch = 0.; |
---|
| 502 | } |
---|
| 503 | obuf->data[0] = pitch; |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | void |
---|
[ce3ff2b] | 507 | aubio_pitch_do_specacf (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * out) |
---|
[95dc7f2] | 508 | { |
---|
[c21acb9] | 509 | smpl_t pitch = 0., period; |
---|
[95dc7f2] | 510 | aubio_pitch_slideblock (p, ibuf); |
---|
| 511 | aubio_pitchspecacf_do (p->p_object, p->buf, out); |
---|
| 512 | //out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize); |
---|
[c21acb9] | 513 | period = out->data[0]; |
---|
[95dc7f2] | 514 | if (period > 0) { |
---|
| 515 | pitch = p->samplerate / period; |
---|
| 516 | } else { |
---|
| 517 | pitch = 0.; |
---|
| 518 | } |
---|
| 519 | out->data[0] = pitch; |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | void |
---|
[f5699b9] | 523 | aubio_pitch_do_crepe (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * out) |
---|
| 524 | { |
---|
| 525 | //smpl_t pitch = 0., period; |
---|
| 526 | aubio_pitch_slideblock (p, ibuf); |
---|
| 527 | aubio_pitch_crepe_do(p->p_object, p->buf, out); |
---|
| 528 | out->data[0] = aubio_miditofreq(out->data[0]); |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | void |
---|
[ce3ff2b] | 532 | aubio_pitch_do_fcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * out) |
---|
[fddfa64] | 533 | { |
---|
| 534 | aubio_pitch_slideblock (p, ibuf); |
---|
[b130600] | 535 | aubio_pitchfcomb_do (p->p_object, p->buf, out); |
---|
| 536 | out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize); |
---|
[c078336] | 537 | } |
---|
| 538 | |
---|
[fddfa64] | 539 | void |
---|
[ce3ff2b] | 540 | aubio_pitch_do_schmitt (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * out) |
---|
[fddfa64] | 541 | { |
---|
[7a6cbbe] | 542 | smpl_t period, pitch = 0.; |
---|
[fddfa64] | 543 | aubio_pitch_slideblock (p, ibuf); |
---|
[b130600] | 544 | aubio_pitchschmitt_do (p->p_object, p->buf, out); |
---|
[168337e] | 545 | period = out->data[0]; |
---|
| 546 | if (period > 0) { |
---|
[b130600] | 547 | pitch = p->samplerate / period; |
---|
[168337e] | 548 | } else { |
---|
| 549 | pitch = 0.; |
---|
[7a6cbbe] | 550 | } |
---|
[168337e] | 551 | out->data[0] = pitch; |
---|
[96fb8ad] | 552 | } |
---|
[5284e0d] | 553 | |
---|
[b130600] | 554 | /* conversion callbacks */ |
---|
| 555 | smpl_t |
---|
| 556 | freqconvbin(smpl_t f, uint_t samplerate, uint_t bufsize) |
---|
| 557 | { |
---|
| 558 | return aubio_freqtobin(f, samplerate, bufsize); |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | smpl_t |
---|
| 562 | freqconvmidi (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED) |
---|
| 563 | { |
---|
| 564 | return aubio_freqtomidi (f); |
---|
| 565 | } |
---|
| 566 | |
---|
| 567 | smpl_t |
---|
| 568 | freqconvpass (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED) |
---|
| 569 | { |
---|
| 570 | return f; |
---|
| 571 | } |
---|
| 572 | |
---|
[5284e0d] | 573 | /* confidence callbacks */ |
---|
| 574 | smpl_t |
---|
| 575 | aubio_pitch_get_confidence (aubio_pitch_t * p) |
---|
| 576 | { |
---|
[b130600] | 577 | if (p->conf_cb) { |
---|
| 578 | return p->conf_cb(p->p_object); |
---|
[5284e0d] | 579 | } |
---|
| 580 | return 0.; |
---|
| 581 | } |
---|