[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. |
---|
[6d4ec49] | 17 | */ |
---|
[96fb8ad] | 18 | |
---|
| 19 | #include "aubio_priv.h" |
---|
[6c7d49b] | 20 | #include "fvec.h" |
---|
| 21 | #include "cvec.h" |
---|
[a4364b8] | 22 | #include "lvec.h" |
---|
[32d6958] | 23 | #include "spectral/phasevoc.h" |
---|
[96fb8ad] | 24 | #include "mathutils.h" |
---|
[a695854] | 25 | #include "temporal/filter.h" |
---|
[c159aeb] | 26 | #include "temporal/c_weighting.h" |
---|
[2d8cffa] | 27 | #include "pitch/pitchmcomb.h" |
---|
| 28 | #include "pitch/pitchyin.h" |
---|
| 29 | #include "pitch/pitchfcomb.h" |
---|
| 30 | #include "pitch/pitchschmitt.h" |
---|
| 31 | #include "pitch/pitchyinfft.h" |
---|
[32d6958] | 32 | #include "pitch/pitchdetection.h" |
---|
[96fb8ad] | 33 | |
---|
[fe163ad] | 34 | /** pitch detection algorithm */ |
---|
| 35 | typedef enum { |
---|
| 36 | aubio_pitch_yin, /**< YIN algorithm */ |
---|
| 37 | aubio_pitch_mcomb, /**< Multi-comb filter */ |
---|
| 38 | aubio_pitch_schmitt, /**< Schmitt trigger */ |
---|
| 39 | aubio_pitch_fcomb, /**< Fast comb filter */ |
---|
| 40 | aubio_pitch_yinfft, /**< Spectral YIN */ |
---|
| 41 | aubio_pitch_default = aubio_pitch_yinfft, /**< the one used when "default" is asked */ |
---|
| 42 | } aubio_pitchdetection_type; |
---|
| 43 | |
---|
| 44 | /** pitch detection output mode */ |
---|
| 45 | typedef enum { |
---|
| 46 | aubio_pitchm_freq, /**< Frequency (Hz) */ |
---|
| 47 | aubio_pitchm_midi, /**< MIDI note (0.,127) */ |
---|
| 48 | aubio_pitchm_cent, /**< Cent */ |
---|
| 49 | aubio_pitchm_bin, /**< Frequency bin (0,bufsize) */ |
---|
| 50 | aubio_pitchm_default = aubio_pitchm_freq, /**< the one used when "default" is asked */ |
---|
| 51 | } aubio_pitchdetection_mode; |
---|
| 52 | |
---|
[7a6cbbe] | 53 | typedef void (*aubio_pitchdetection_func_t) |
---|
| 54 | (aubio_pitchdetection_t *p, fvec_t * ibuf, fvec_t *obuf); |
---|
[6d4ec49] | 55 | typedef smpl_t (*aubio_pitchdetection_conv_t) |
---|
| 56 | (smpl_t value, uint_t srate, uint_t bufsize); |
---|
| 57 | |
---|
[651b97e] | 58 | void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf); |
---|
[c078336] | 59 | |
---|
[7a6cbbe] | 60 | void aubio_pitchdetection_mcomb (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf); |
---|
| 61 | void aubio_pitchdetection_yin (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf); |
---|
| 62 | void aubio_pitchdetection_schmitt (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf); |
---|
| 63 | void aubio_pitchdetection_fcomb (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf); |
---|
| 64 | void aubio_pitchdetection_yinfft (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf); |
---|
[f44b111] | 65 | |
---|
[475da2f] | 66 | /** generic pitch detection structure */ |
---|
[96fb8ad] | 67 | struct _aubio_pitchdetection_t { |
---|
[475da2f] | 68 | aubio_pitchdetection_type type; /**< pitch detection mode */ |
---|
| 69 | aubio_pitchdetection_mode mode; /**< pitch detection output mode */ |
---|
| 70 | uint_t srate; /**< samplerate */ |
---|
| 71 | uint_t bufsize; /**< buffer size */ |
---|
| 72 | aubio_pitchmcomb_t * mcomb; /**< mcomb object */ |
---|
| 73 | aubio_pitchfcomb_t * fcomb; /**< fcomb object */ |
---|
| 74 | aubio_pitchschmitt_t * schmitt; /**< schmitt object */ |
---|
| 75 | aubio_pitchyinfft_t * yinfft; /**< yinfft object */ |
---|
[7a6cbbe] | 76 | aubio_pitchyin_t * yin; /**< yinfft object */ |
---|
[475da2f] | 77 | aubio_filter_t * filter; /**< filter */ |
---|
| 78 | aubio_pvoc_t * pv; /**< phase vocoder for mcomb */ |
---|
| 79 | cvec_t * fftgrain; /**< spectral frame for mcomb */ |
---|
| 80 | fvec_t * buf; /**< temporary buffer for yin */ |
---|
| 81 | aubio_pitchdetection_func_t callback; /**< pointer to current pitch detection method */ |
---|
| 82 | aubio_pitchdetection_conv_t freqconv; /**< pointer to current pitch conversion method */ |
---|
[96fb8ad] | 83 | }; |
---|
| 84 | |
---|
[3ec9d9c] | 85 | /* convenience wrapper function for frequency unit conversions |
---|
| 86 | * should probably be rewritten with #defines */ |
---|
| 87 | smpl_t freqconvbin(smpl_t f,uint_t srate,uint_t bufsize); |
---|
| 88 | smpl_t freqconvbin(smpl_t f,uint_t srate,uint_t bufsize){ |
---|
[6d4ec49] | 89 | return aubio_freqtobin(f,srate,bufsize); |
---|
[3ec9d9c] | 90 | } |
---|
| 91 | |
---|
| 92 | smpl_t freqconvmidi(smpl_t f,uint_t srate,uint_t bufsize); |
---|
[b377d04] | 93 | smpl_t freqconvmidi(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){ |
---|
[6d4ec49] | 94 | return aubio_freqtomidi(f); |
---|
[3ec9d9c] | 95 | } |
---|
| 96 | |
---|
| 97 | smpl_t freqconvpass(smpl_t f,uint_t srate,uint_t bufsize); |
---|
[b377d04] | 98 | smpl_t freqconvpass(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){ |
---|
[6d4ec49] | 99 | return f; |
---|
[3ec9d9c] | 100 | } |
---|
| 101 | |
---|
[fe163ad] | 102 | aubio_pitchdetection_t * |
---|
| 103 | new_aubio_pitchdetection (char_t * pitch_mode, |
---|
| 104 | uint_t bufsize, uint_t hopsize, uint_t channels, uint_t samplerate) |
---|
[96fb8ad] | 105 | { |
---|
[6d4ec49] | 106 | aubio_pitchdetection_t *p = AUBIO_NEW(aubio_pitchdetection_t); |
---|
[fe163ad] | 107 | aubio_pitchdetection_type pitch_type; |
---|
| 108 | if (strcmp (pitch_mode, "mcomb") == 0) |
---|
| 109 | pitch_type = aubio_pitch_mcomb; |
---|
| 110 | else if (strcmp (pitch_mode, "yinfft") == 0) |
---|
| 111 | pitch_type = aubio_pitch_yin; |
---|
| 112 | else if (strcmp (pitch_mode, "yin") == 0) |
---|
| 113 | pitch_type = aubio_pitch_yin; |
---|
| 114 | else if (strcmp (pitch_mode, "schmitt") == 0) |
---|
| 115 | pitch_type = aubio_pitch_schmitt; |
---|
| 116 | else if (strcmp (pitch_mode, "fcomb") == 0) |
---|
| 117 | pitch_type = aubio_pitch_fcomb; |
---|
| 118 | else if (strcmp (pitch_mode, "default") == 0) |
---|
| 119 | pitch_type = aubio_pitch_default; |
---|
| 120 | else { |
---|
| 121 | AUBIO_ERR ("unknown pitch detection method %s, using default.\n", pitch_mode); |
---|
| 122 | pitch_type = aubio_pitch_default; |
---|
| 123 | return NULL; |
---|
| 124 | } |
---|
[6d4ec49] | 125 | p->srate = samplerate; |
---|
[fe163ad] | 126 | p->type = pitch_type; |
---|
| 127 | aubio_pitchdetection_set_unit (p, "default"); |
---|
[6d4ec49] | 128 | p->bufsize = bufsize; |
---|
| 129 | switch(p->type) { |
---|
| 130 | case aubio_pitch_yin: |
---|
| 131 | p->buf = new_fvec(bufsize,channels); |
---|
[7a6cbbe] | 132 | p->yin = new_aubio_pitchyin(bufsize); |
---|
[6d4ec49] | 133 | p->callback = aubio_pitchdetection_yin; |
---|
[7a6cbbe] | 134 | aubio_pitchyin_set_tolerance (p->yin, 0.15); |
---|
[6d4ec49] | 135 | break; |
---|
| 136 | case aubio_pitch_mcomb: |
---|
| 137 | p->pv = new_aubio_pvoc(bufsize, hopsize, channels); |
---|
| 138 | p->fftgrain = new_cvec(bufsize, channels); |
---|
[7a6cbbe] | 139 | p->mcomb = new_aubio_pitchmcomb(bufsize,hopsize,channels); |
---|
[c159aeb] | 140 | p->filter = new_aubio_filter_c_weighting (samplerate, channels); |
---|
[6d4ec49] | 141 | p->callback = aubio_pitchdetection_mcomb; |
---|
| 142 | break; |
---|
| 143 | case aubio_pitch_fcomb: |
---|
| 144 | p->buf = new_fvec(bufsize,channels); |
---|
[7a6cbbe] | 145 | p->fcomb = new_aubio_pitchfcomb(bufsize,hopsize,channels); |
---|
[6d4ec49] | 146 | p->callback = aubio_pitchdetection_fcomb; |
---|
| 147 | break; |
---|
| 148 | case aubio_pitch_schmitt: |
---|
| 149 | p->buf = new_fvec(bufsize,channels); |
---|
[7a6cbbe] | 150 | p->schmitt = new_aubio_pitchschmitt(bufsize); |
---|
[6d4ec49] | 151 | p->callback = aubio_pitchdetection_schmitt; |
---|
| 152 | break; |
---|
| 153 | case aubio_pitch_yinfft: |
---|
| 154 | p->buf = new_fvec(bufsize,channels); |
---|
| 155 | p->yinfft = new_aubio_pitchyinfft(bufsize); |
---|
| 156 | p->callback = aubio_pitchdetection_yinfft; |
---|
[7a6cbbe] | 157 | aubio_pitchyinfft_set_tolerance (p->yinfft, 0.85); |
---|
[6d4ec49] | 158 | break; |
---|
| 159 | default: |
---|
| 160 | break; |
---|
| 161 | } |
---|
| 162 | return p; |
---|
[96fb8ad] | 163 | } |
---|
| 164 | |
---|
| 165 | void del_aubio_pitchdetection(aubio_pitchdetection_t * p) { |
---|
[6d4ec49] | 166 | switch(p->type) { |
---|
| 167 | case aubio_pitch_yin: |
---|
| 168 | del_fvec(p->buf); |
---|
[7a6cbbe] | 169 | del_aubio_pitchyin(p->yin); |
---|
[6d4ec49] | 170 | break; |
---|
| 171 | case aubio_pitch_mcomb: |
---|
| 172 | del_aubio_pvoc(p->pv); |
---|
| 173 | del_cvec(p->fftgrain); |
---|
[44d7cb7] | 174 | del_aubio_filter(p->filter); |
---|
[6d4ec49] | 175 | del_aubio_pitchmcomb(p->mcomb); |
---|
| 176 | break; |
---|
| 177 | case aubio_pitch_schmitt: |
---|
| 178 | del_fvec(p->buf); |
---|
| 179 | del_aubio_pitchschmitt(p->schmitt); |
---|
| 180 | break; |
---|
| 181 | case aubio_pitch_fcomb: |
---|
| 182 | del_fvec(p->buf); |
---|
| 183 | del_aubio_pitchfcomb(p->fcomb); |
---|
| 184 | break; |
---|
| 185 | case aubio_pitch_yinfft: |
---|
| 186 | del_fvec(p->buf); |
---|
| 187 | del_aubio_pitchyinfft(p->yinfft); |
---|
| 188 | break; |
---|
| 189 | default: |
---|
| 190 | break; |
---|
| 191 | } |
---|
| 192 | AUBIO_FREE(p); |
---|
[96fb8ad] | 193 | } |
---|
| 194 | |
---|
[651b97e] | 195 | void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf){ |
---|
[6d4ec49] | 196 | uint_t i,j = 0, overlap_size = 0; |
---|
| 197 | overlap_size = p->buf->length-ibuf->length; |
---|
| 198 | for (i=0;i<p->buf->channels;i++){ |
---|
| 199 | for (j=0;j<overlap_size;j++){ |
---|
| 200 | p->buf->data[i][j] = p->buf->data[i][j+ibuf->length]; |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | for (i=0;i<ibuf->channels;i++){ |
---|
| 204 | for (j=0;j<ibuf->length;j++){ |
---|
| 205 | p->buf->data[i][j+overlap_size] = ibuf->data[i][j]; |
---|
| 206 | } |
---|
| 207 | } |
---|
[651b97e] | 208 | } |
---|
| 209 | |
---|
[fe163ad] | 210 | uint_t aubio_pitchdetection_set_unit (aubio_pitchdetection_t *p, char_t * pitch_unit) { |
---|
| 211 | aubio_pitchdetection_mode pitch_mode; |
---|
| 212 | if (strcmp (pitch_unit, "freq") == 0) |
---|
| 213 | pitch_mode = aubio_pitchm_freq; |
---|
| 214 | else if (strcmp (pitch_unit, "midi") == 0) |
---|
| 215 | pitch_mode = aubio_pitchm_midi; |
---|
| 216 | else if (strcmp (pitch_unit, "cent") == 0) |
---|
| 217 | pitch_mode = aubio_pitchm_cent; |
---|
| 218 | else if (strcmp (pitch_unit, "bin") == 0) |
---|
| 219 | pitch_mode = aubio_pitchm_bin; |
---|
| 220 | else if (strcmp (pitch_unit, "default") == 0) |
---|
| 221 | pitch_mode = aubio_pitchm_default; |
---|
| 222 | else { |
---|
| 223 | AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit); |
---|
| 224 | pitch_mode = aubio_pitchm_default; |
---|
| 225 | } |
---|
| 226 | p->mode = pitch_mode; |
---|
| 227 | switch(p->mode) { |
---|
| 228 | case aubio_pitchm_freq: |
---|
| 229 | p->freqconv = freqconvpass; |
---|
| 230 | break; |
---|
| 231 | case aubio_pitchm_midi: |
---|
| 232 | p->freqconv = freqconvmidi; |
---|
| 233 | break; |
---|
| 234 | case aubio_pitchm_cent: |
---|
| 235 | /* bug: not implemented */ |
---|
| 236 | p->freqconv = freqconvmidi; |
---|
| 237 | break; |
---|
| 238 | case aubio_pitchm_bin: |
---|
| 239 | p->freqconv = freqconvbin; |
---|
| 240 | break; |
---|
| 241 | default: |
---|
| 242 | break; |
---|
| 243 | } |
---|
[93177fa] | 244 | return AUBIO_OK; |
---|
[fe163ad] | 245 | } |
---|
| 246 | |
---|
[93177fa] | 247 | uint_t aubio_pitchdetection_set_tolerance(aubio_pitchdetection_t *p, smpl_t tol) { |
---|
[7a6cbbe] | 248 | switch(p->type) { |
---|
| 249 | case aubio_pitch_yin: |
---|
| 250 | aubio_pitchyin_set_tolerance (p->yin, tol); |
---|
| 251 | break; |
---|
| 252 | case aubio_pitch_yinfft: |
---|
| 253 | aubio_pitchyinfft_set_tolerance (p->yinfft, tol); |
---|
| 254 | break; |
---|
| 255 | default: |
---|
| 256 | break; |
---|
| 257 | } |
---|
[93177fa] | 258 | return AUBIO_OK; |
---|
[f8a38c5] | 259 | } |
---|
| 260 | |
---|
[7a6cbbe] | 261 | void aubio_pitchdetection_do (aubio_pitchdetection_t *p, fvec_t * ibuf, fvec_t *obuf) { |
---|
| 262 | uint_t i; |
---|
| 263 | p->callback(p, ibuf, obuf); |
---|
| 264 | for (i = 0; i < obuf->channels; i++) { |
---|
| 265 | p->freqconv(obuf->data[i][0],p->srate,p->bufsize); |
---|
| 266 | } |
---|
[c078336] | 267 | } |
---|
| 268 | |
---|
[7a6cbbe] | 269 | void aubio_pitchdetection_mcomb(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t * obuf) { |
---|
| 270 | uint_t i; |
---|
[6d4ec49] | 271 | aubio_filter_do(p->filter,ibuf); |
---|
| 272 | aubio_pvoc_do(p->pv,ibuf,p->fftgrain); |
---|
[7a6cbbe] | 273 | aubio_pitchmcomb_do(p->mcomb,p->fftgrain, obuf); |
---|
| 274 | for (i = 0; i < obuf->channels; i++) { |
---|
| 275 | obuf->data[i][0] = aubio_bintofreq (obuf->data[i][0], p->srate, p->bufsize); |
---|
[6d4ec49] | 276 | } |
---|
[c078336] | 277 | } |
---|
| 278 | |
---|
[7a6cbbe] | 279 | void aubio_pitchdetection_yin(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t * obuf) { |
---|
[6d4ec49] | 280 | smpl_t pitch = 0.; |
---|
[7a6cbbe] | 281 | uint_t i; |
---|
[6d4ec49] | 282 | aubio_pitchdetection_slideblock(p,ibuf); |
---|
[7a6cbbe] | 283 | aubio_pitchyin_do(p->yin,p->buf, obuf); |
---|
| 284 | for (i = 0; i < obuf->channels; i++) { |
---|
| 285 | pitch = obuf->data[i][0]; |
---|
| 286 | if (pitch>0) { |
---|
| 287 | pitch = p->srate/(pitch+0.); |
---|
| 288 | } else { |
---|
| 289 | pitch = 0.; |
---|
| 290 | } |
---|
| 291 | obuf->data[i][0] = pitch; |
---|
[6d4ec49] | 292 | } |
---|
[c078336] | 293 | } |
---|
| 294 | |
---|
| 295 | |
---|
[7a6cbbe] | 296 | void aubio_pitchdetection_yinfft(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t * obuf){ |
---|
[6d4ec49] | 297 | smpl_t pitch = 0.; |
---|
[7a6cbbe] | 298 | uint_t i; |
---|
[6d4ec49] | 299 | aubio_pitchdetection_slideblock(p,ibuf); |
---|
[7a6cbbe] | 300 | aubio_pitchyinfft_do(p->yinfft,p->buf,obuf); |
---|
| 301 | for (i = 0; i < obuf->channels; i++) { |
---|
| 302 | pitch = obuf->data[i][0]; |
---|
| 303 | if (pitch>0) { |
---|
| 304 | pitch = p->srate/(pitch+0.); |
---|
| 305 | } else { |
---|
| 306 | pitch = 0.; |
---|
| 307 | } |
---|
| 308 | obuf->data[i][0] = pitch; |
---|
[6d4ec49] | 309 | } |
---|
[650e39b] | 310 | } |
---|
| 311 | |
---|
[7a6cbbe] | 312 | void aubio_pitchdetection_fcomb(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t * out){ |
---|
| 313 | uint_t i; |
---|
[6d4ec49] | 314 | aubio_pitchdetection_slideblock(p,ibuf); |
---|
[7a6cbbe] | 315 | aubio_pitchfcomb_do(p->fcomb,p->buf, out); |
---|
| 316 | for (i = 0; i < out->channels; i++) { |
---|
| 317 | out->data[i][0] = aubio_bintofreq (out->data[i][0], p->srate, p->bufsize); |
---|
| 318 | } |
---|
[c078336] | 319 | } |
---|
| 320 | |
---|
[7a6cbbe] | 321 | void aubio_pitchdetection_schmitt(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *out){ |
---|
| 322 | smpl_t period, pitch = 0.; |
---|
| 323 | uint_t i; |
---|
[6d4ec49] | 324 | aubio_pitchdetection_slideblock(p,ibuf); |
---|
[7a6cbbe] | 325 | aubio_pitchschmitt_do(p->schmitt,p->buf, out); |
---|
| 326 | for (i = 0; i < out->channels; i++) { |
---|
| 327 | period = out->data[i][0]; |
---|
| 328 | if (period>0) { |
---|
| 329 | pitch = p->srate/period; |
---|
| 330 | } else { |
---|
| 331 | pitch = 0.; |
---|
| 332 | } |
---|
| 333 | out->data[i][0] = pitch; |
---|
| 334 | } |
---|
[96fb8ad] | 335 | } |
---|