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