[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. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #include "aubio_priv.h" |
---|
| 20 | #include "sample.h" |
---|
| 21 | #include "phasevoc.h" |
---|
| 22 | #include "mathutils.h" |
---|
[f8a38c5] | 23 | #include "filter.h" |
---|
[96fb8ad] | 24 | #include "pitchmcomb.h" |
---|
[c078336] | 25 | #include "pitchyin.h" |
---|
| 26 | #include "pitchfcomb.h" |
---|
| 27 | #include "pitchschmitt.h" |
---|
[650e39b] | 28 | #include "pitchyinfft.h" |
---|
[96fb8ad] | 29 | #include "pitchdetection.h" |
---|
| 30 | |
---|
[c078336] | 31 | typedef smpl_t (*aubio_pitchdetection_func_t)(aubio_pitchdetection_t *p, |
---|
| 32 | fvec_t * ibuf); |
---|
[3ec9d9c] | 33 | typedef smpl_t (*aubio_pitchdetection_conv_t)(smpl_t value,uint_t srate,uint_t bufsize); |
---|
[651b97e] | 34 | void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf); |
---|
[c078336] | 35 | |
---|
[96fb8ad] | 36 | struct _aubio_pitchdetection_t { |
---|
| 37 | aubio_pitchdetection_type type; |
---|
| 38 | aubio_pitchdetection_mode mode; |
---|
| 39 | uint_t srate; |
---|
| 40 | uint_t bufsize; |
---|
| 41 | /* for mcomb */ |
---|
| 42 | aubio_pvoc_t * pv; |
---|
| 43 | cvec_t * fftgrain; |
---|
| 44 | aubio_pitchmcomb_t * mcomb; |
---|
[c078336] | 45 | aubio_pitchfcomb_t * fcomb; |
---|
| 46 | aubio_pitchschmitt_t * schmitt; |
---|
[650e39b] | 47 | aubio_pitchyinfft_t * yinfft; |
---|
[f8a38c5] | 48 | aubio_filter_t * filter; |
---|
[96fb8ad] | 49 | /* for yin */ |
---|
| 50 | fvec_t * buf; |
---|
| 51 | fvec_t * yin; |
---|
[c078336] | 52 | aubio_pitchdetection_func_t callback; |
---|
[aa17581] | 53 | aubio_pitchdetection_conv_t freqconv; |
---|
[f8a38c5] | 54 | smpl_t yinthres; |
---|
[96fb8ad] | 55 | }; |
---|
| 56 | |
---|
[3ec9d9c] | 57 | /* convenience wrapper function for frequency unit conversions |
---|
| 58 | * should probably be rewritten with #defines */ |
---|
| 59 | smpl_t freqconvbin(smpl_t f,uint_t srate,uint_t bufsize); |
---|
| 60 | smpl_t freqconvbin(smpl_t f,uint_t srate,uint_t bufsize){ |
---|
| 61 | return aubio_freqtobin(f,srate,bufsize); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | smpl_t freqconvmidi(smpl_t f,uint_t srate,uint_t bufsize); |
---|
| 65 | smpl_t freqconvmidi(smpl_t f,uint_t srate,uint_t bufsize){ |
---|
| 66 | return aubio_freqtomidi(f); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | smpl_t freqconvpass(smpl_t f,uint_t srate,uint_t bufsize); |
---|
| 70 | smpl_t freqconvpass(smpl_t f,uint_t srate,uint_t bufsize){ |
---|
| 71 | return f; |
---|
| 72 | } |
---|
| 73 | |
---|
[96fb8ad] | 74 | aubio_pitchdetection_t * new_aubio_pitchdetection(uint_t bufsize, |
---|
| 75 | uint_t hopsize, |
---|
| 76 | uint_t channels, |
---|
| 77 | uint_t samplerate, |
---|
| 78 | aubio_pitchdetection_type type, |
---|
| 79 | aubio_pitchdetection_mode mode) |
---|
| 80 | { |
---|
| 81 | aubio_pitchdetection_t *p = AUBIO_NEW(aubio_pitchdetection_t); |
---|
| 82 | p->srate = samplerate; |
---|
| 83 | p->type = type; |
---|
[aa17581] | 84 | p->mode = mode; |
---|
[96fb8ad] | 85 | p->bufsize = bufsize; |
---|
| 86 | switch(p->type) { |
---|
[5e9c68a] | 87 | case aubio_pitch_yin: |
---|
[96fb8ad] | 88 | p->buf = new_fvec(bufsize,channels); |
---|
| 89 | p->yin = new_fvec(bufsize/2,channels); |
---|
[c078336] | 90 | p->callback = aubio_pitchdetection_yin; |
---|
[f8a38c5] | 91 | p->yinthres = 0.2; |
---|
[96fb8ad] | 92 | break; |
---|
[5e9c68a] | 93 | case aubio_pitch_mcomb: |
---|
[96fb8ad] | 94 | p->pv = new_aubio_pvoc(bufsize, hopsize, channels); |
---|
| 95 | p->fftgrain = new_cvec(bufsize, channels); |
---|
[650e39b] | 96 | p->mcomb = new_aubio_pitchmcomb(bufsize,hopsize,channels,samplerate); |
---|
[f8a38c5] | 97 | p->filter = new_aubio_cdsgn_filter(samplerate); |
---|
[c078336] | 98 | p->callback = aubio_pitchdetection_mcomb; |
---|
[96fb8ad] | 99 | break; |
---|
[5e9c68a] | 100 | case aubio_pitch_fcomb: |
---|
[651b97e] | 101 | p->buf = new_fvec(bufsize,channels); |
---|
[f8a38c5] | 102 | p->fcomb = new_aubio_pitchfcomb(bufsize,hopsize,samplerate); |
---|
[c078336] | 103 | p->callback = aubio_pitchdetection_fcomb; |
---|
| 104 | break; |
---|
[5e9c68a] | 105 | case aubio_pitch_schmitt: |
---|
[651b97e] | 106 | p->buf = new_fvec(bufsize,channels); |
---|
[c078336] | 107 | p->schmitt = new_aubio_pitchschmitt(bufsize,samplerate); |
---|
[651b97e] | 108 | p->callback = aubio_pitchdetection_schmitt; |
---|
[c078336] | 109 | break; |
---|
[650e39b] | 110 | case aubio_pitch_yinfft: |
---|
| 111 | p->buf = new_fvec(bufsize,channels); |
---|
| 112 | p->yinfft = new_aubio_pitchyinfft(bufsize); |
---|
| 113 | p->callback = aubio_pitchdetection_yinfft; |
---|
| 114 | p->yinthres = 0.2; |
---|
| 115 | break; |
---|
[c078336] | 116 | default: |
---|
| 117 | break; |
---|
[96fb8ad] | 118 | } |
---|
[aa17581] | 119 | switch(p->mode) { |
---|
| 120 | case aubio_pitchm_freq: |
---|
| 121 | p->freqconv = freqconvpass; |
---|
| 122 | break; |
---|
| 123 | case aubio_pitchm_midi: |
---|
[3ec9d9c] | 124 | p->freqconv = freqconvmidi; |
---|
[aa17581] | 125 | break; |
---|
| 126 | case aubio_pitchm_cent: |
---|
| 127 | /** bug: not implemented */ |
---|
[3ec9d9c] | 128 | p->freqconv = freqconvmidi; |
---|
[aa17581] | 129 | break; |
---|
| 130 | case aubio_pitchm_bin: |
---|
[3ec9d9c] | 131 | p->freqconv = freqconvbin; |
---|
[aa17581] | 132 | break; |
---|
| 133 | default: |
---|
| 134 | break; |
---|
| 135 | } |
---|
[96fb8ad] | 136 | return p; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | void del_aubio_pitchdetection(aubio_pitchdetection_t * p) { |
---|
| 140 | switch(p->type) { |
---|
[5e9c68a] | 141 | case aubio_pitch_yin: |
---|
[96fb8ad] | 142 | del_fvec(p->yin); |
---|
| 143 | del_fvec(p->buf); |
---|
| 144 | break; |
---|
[5e9c68a] | 145 | case aubio_pitch_mcomb: |
---|
[96fb8ad] | 146 | del_aubio_pvoc(p->pv); |
---|
| 147 | del_cvec(p->fftgrain); |
---|
[c078336] | 148 | del_aubio_pitchmcomb(p->mcomb); |
---|
[96fb8ad] | 149 | break; |
---|
[5e9c68a] | 150 | case aubio_pitch_schmitt: |
---|
[651b97e] | 151 | del_fvec(p->buf); |
---|
[c078336] | 152 | del_aubio_pitchschmitt(p->schmitt); |
---|
| 153 | break; |
---|
[5e9c68a] | 154 | case aubio_pitch_fcomb: |
---|
[651b97e] | 155 | del_fvec(p->buf); |
---|
[c078336] | 156 | del_aubio_pitchfcomb(p->fcomb); |
---|
| 157 | break; |
---|
[650e39b] | 158 | case aubio_pitch_yinfft: |
---|
| 159 | del_fvec(p->buf); |
---|
| 160 | del_aubio_pitchyinfft(p->yinfft); |
---|
| 161 | break; |
---|
[96fb8ad] | 162 | default: |
---|
| 163 | break; |
---|
| 164 | } |
---|
| 165 | AUBIO_FREE(p); |
---|
| 166 | } |
---|
| 167 | |
---|
[651b97e] | 168 | void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf){ |
---|
| 169 | uint_t i,j = 0, overlap_size = 0; |
---|
| 170 | overlap_size = p->buf->length-ibuf->length; |
---|
| 171 | for (i=0;i<p->buf->channels;i++){ |
---|
| 172 | for (j=0;j<overlap_size;j++){ |
---|
| 173 | p->buf->data[i][j] = |
---|
| 174 | p->buf->data[i][j+ibuf->length]; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | for (i=0;i<ibuf->channels;i++){ |
---|
| 178 | for (j=0;j<ibuf->length;j++){ |
---|
| 179 | p->buf->data[i][j+overlap_size] = |
---|
| 180 | ibuf->data[i][j]; |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
[f8a38c5] | 185 | void aubio_pitchdetection_set_yinthresh(aubio_pitchdetection_t *p, smpl_t thres) { |
---|
| 186 | p->yinthres = thres; |
---|
| 187 | } |
---|
| 188 | |
---|
[96fb8ad] | 189 | smpl_t aubio_pitchdetection(aubio_pitchdetection_t *p, fvec_t * ibuf) { |
---|
[3ec9d9c] | 190 | return p->freqconv(p->callback(p,ibuf),p->srate,p->bufsize); |
---|
[c078336] | 191 | } |
---|
| 192 | |
---|
| 193 | smpl_t aubio_pitchdetection_mcomb(aubio_pitchdetection_t *p, fvec_t *ibuf) { |
---|
| 194 | smpl_t pitch = 0.; |
---|
[f8a38c5] | 195 | aubio_filter_do(p->filter,ibuf); |
---|
[c078336] | 196 | aubio_pvoc_do(p->pv,ibuf,p->fftgrain); |
---|
| 197 | pitch = aubio_pitchmcomb_detect(p->mcomb,p->fftgrain); |
---|
[28d8c4a] | 198 | /** \bug should move the >0 check within aubio_bintofreq */ |
---|
[c078336] | 199 | if (pitch>0.) { |
---|
[28d8c4a] | 200 | pitch = aubio_bintofreq(pitch,p->srate,p->bufsize); |
---|
[c078336] | 201 | } else { |
---|
| 202 | pitch = 0.; |
---|
| 203 | } |
---|
| 204 | return pitch; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | smpl_t aubio_pitchdetection_yin(aubio_pitchdetection_t *p, fvec_t *ibuf) { |
---|
[96fb8ad] | 208 | smpl_t pitch = 0.; |
---|
[651b97e] | 209 | aubio_pitchdetection_slideblock(p,ibuf); |
---|
[f8a38c5] | 210 | pitch = aubio_pitchyin_getpitchfast(p->buf,p->yin, p->yinthres); |
---|
[c078336] | 211 | if (pitch>0) { |
---|
| 212 | pitch = p->srate/(pitch+0.); |
---|
| 213 | } else { |
---|
| 214 | pitch = 0.; |
---|
| 215 | } |
---|
| 216 | return pitch; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | |
---|
[650e39b] | 220 | smpl_t aubio_pitchdetection_yinfft(aubio_pitchdetection_t *p, fvec_t *ibuf){ |
---|
| 221 | smpl_t pitch = 0.; |
---|
| 222 | aubio_pitchdetection_slideblock(p,ibuf); |
---|
| 223 | pitch = aubio_pitchyinfft_detect(p->yinfft,p->buf,p->yinthres); |
---|
| 224 | if (pitch>0) { |
---|
| 225 | pitch = p->srate/(pitch+0.); |
---|
| 226 | } else { |
---|
| 227 | pitch = 0.; |
---|
| 228 | } |
---|
| 229 | return pitch; |
---|
| 230 | } |
---|
| 231 | |
---|
[c078336] | 232 | smpl_t aubio_pitchdetection_fcomb(aubio_pitchdetection_t *p, fvec_t *ibuf){ |
---|
[651b97e] | 233 | aubio_pitchdetection_slideblock(p,ibuf); |
---|
| 234 | return aubio_pitchfcomb_detect(p->fcomb,p->buf); |
---|
[c078336] | 235 | } |
---|
| 236 | |
---|
| 237 | smpl_t aubio_pitchdetection_schmitt(aubio_pitchdetection_t *p, fvec_t *ibuf){ |
---|
[651b97e] | 238 | aubio_pitchdetection_slideblock(p,ibuf); |
---|
| 239 | return aubio_pitchschmitt_detect(p->schmitt,p->buf); |
---|
[96fb8ad] | 240 | } |
---|