[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" |
---|
[c078336] | 23 | //#include "filter.h" |
---|
[96fb8ad] | 24 | #include "pitchmcomb.h" |
---|
[c078336] | 25 | #include "pitchyin.h" |
---|
| 26 | #include "pitchfcomb.h" |
---|
| 27 | #include "pitchschmitt.h" |
---|
[96fb8ad] | 28 | #include "pitchdetection.h" |
---|
| 29 | |
---|
[c078336] | 30 | typedef smpl_t (*aubio_pitchdetection_func_t)(aubio_pitchdetection_t *p, |
---|
| 31 | fvec_t * ibuf); |
---|
[651b97e] | 32 | void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf); |
---|
[c078336] | 33 | |
---|
[96fb8ad] | 34 | struct _aubio_pitchdetection_t { |
---|
| 35 | aubio_pitchdetection_type type; |
---|
| 36 | aubio_pitchdetection_mode mode; |
---|
| 37 | uint_t srate; |
---|
| 38 | uint_t bufsize; |
---|
| 39 | /* for mcomb */ |
---|
| 40 | aubio_pvoc_t * pv; |
---|
| 41 | cvec_t * fftgrain; |
---|
| 42 | aubio_pitchmcomb_t * mcomb; |
---|
[c078336] | 43 | aubio_pitchfcomb_t * fcomb; |
---|
| 44 | aubio_pitchschmitt_t * schmitt; |
---|
| 45 | //aubio_filter_t * filter; |
---|
[96fb8ad] | 46 | /* for yin */ |
---|
| 47 | fvec_t * buf; |
---|
| 48 | fvec_t * yin; |
---|
[c078336] | 49 | aubio_pitchdetection_func_t callback; |
---|
[96fb8ad] | 50 | }; |
---|
| 51 | |
---|
| 52 | aubio_pitchdetection_t * new_aubio_pitchdetection(uint_t bufsize, |
---|
| 53 | uint_t hopsize, |
---|
| 54 | uint_t channels, |
---|
| 55 | uint_t samplerate, |
---|
| 56 | aubio_pitchdetection_type type, |
---|
| 57 | aubio_pitchdetection_mode mode) |
---|
| 58 | { |
---|
| 59 | aubio_pitchdetection_t *p = AUBIO_NEW(aubio_pitchdetection_t); |
---|
| 60 | p->srate = samplerate; |
---|
| 61 | p->type = type; |
---|
| 62 | p->bufsize = bufsize; |
---|
| 63 | switch(p->type) { |
---|
[edca23f] | 64 | case aubio_yin: |
---|
[96fb8ad] | 65 | p->buf = new_fvec(bufsize,channels); |
---|
| 66 | p->yin = new_fvec(bufsize/2,channels); |
---|
[c078336] | 67 | p->callback = aubio_pitchdetection_yin; |
---|
[96fb8ad] | 68 | break; |
---|
[edca23f] | 69 | case aubio_mcomb: |
---|
[96fb8ad] | 70 | p->pv = new_aubio_pvoc(bufsize, hopsize, channels); |
---|
| 71 | p->fftgrain = new_cvec(bufsize, channels); |
---|
| 72 | p->mcomb = new_aubio_pitchmcomb(bufsize,channels); |
---|
[c078336] | 73 | p->callback = aubio_pitchdetection_mcomb; |
---|
[96fb8ad] | 74 | break; |
---|
[c078336] | 75 | case aubio_fcomb: |
---|
[651b97e] | 76 | p->buf = new_fvec(bufsize,channels); |
---|
[c078336] | 77 | p->fcomb = new_aubio_pitchfcomb(bufsize,samplerate); |
---|
| 78 | p->callback = aubio_pitchdetection_fcomb; |
---|
| 79 | break; |
---|
| 80 | case aubio_schmitt: |
---|
[651b97e] | 81 | p->buf = new_fvec(bufsize,channels); |
---|
[c078336] | 82 | p->schmitt = new_aubio_pitchschmitt(bufsize,samplerate); |
---|
[651b97e] | 83 | p->callback = aubio_pitchdetection_schmitt; |
---|
[c078336] | 84 | break; |
---|
| 85 | default: |
---|
| 86 | break; |
---|
[96fb8ad] | 87 | } |
---|
| 88 | return p; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void del_aubio_pitchdetection(aubio_pitchdetection_t * p) { |
---|
| 92 | switch(p->type) { |
---|
[edca23f] | 93 | case aubio_yin: |
---|
[96fb8ad] | 94 | del_fvec(p->yin); |
---|
| 95 | del_fvec(p->buf); |
---|
| 96 | break; |
---|
[edca23f] | 97 | case aubio_mcomb: |
---|
[96fb8ad] | 98 | del_aubio_pvoc(p->pv); |
---|
| 99 | del_cvec(p->fftgrain); |
---|
[c078336] | 100 | del_aubio_pitchmcomb(p->mcomb); |
---|
[96fb8ad] | 101 | break; |
---|
[c078336] | 102 | case aubio_schmitt: |
---|
[651b97e] | 103 | del_fvec(p->buf); |
---|
[c078336] | 104 | del_aubio_pitchschmitt(p->schmitt); |
---|
| 105 | break; |
---|
| 106 | case aubio_fcomb: |
---|
[651b97e] | 107 | del_fvec(p->buf); |
---|
[c078336] | 108 | del_aubio_pitchfcomb(p->fcomb); |
---|
| 109 | break; |
---|
[96fb8ad] | 110 | default: |
---|
| 111 | break; |
---|
| 112 | } |
---|
| 113 | AUBIO_FREE(p); |
---|
| 114 | } |
---|
| 115 | |
---|
[651b97e] | 116 | void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf){ |
---|
| 117 | uint_t i,j = 0, overlap_size = 0; |
---|
| 118 | overlap_size = p->buf->length-ibuf->length; |
---|
| 119 | for (i=0;i<p->buf->channels;i++){ |
---|
| 120 | for (j=0;j<overlap_size;j++){ |
---|
| 121 | p->buf->data[i][j] = |
---|
| 122 | p->buf->data[i][j+ibuf->length]; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | for (i=0;i<ibuf->channels;i++){ |
---|
| 126 | for (j=0;j<ibuf->length;j++){ |
---|
| 127 | p->buf->data[i][j+overlap_size] = |
---|
| 128 | ibuf->data[i][j]; |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
[96fb8ad] | 133 | smpl_t aubio_pitchdetection(aubio_pitchdetection_t *p, fvec_t * ibuf) { |
---|
[c078336] | 134 | return p->callback(p,ibuf); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | smpl_t aubio_pitchdetection_mcomb(aubio_pitchdetection_t *p, fvec_t *ibuf) { |
---|
| 138 | smpl_t pitch = 0.; |
---|
| 139 | aubio_pvoc_do(p->pv,ibuf,p->fftgrain); |
---|
| 140 | pitch = aubio_pitchmcomb_detect(p->mcomb,p->fftgrain); |
---|
| 141 | /** \bug should move the >0 check within bintofreq */ |
---|
| 142 | if (pitch>0.) { |
---|
| 143 | pitch = bintofreq(pitch,p->srate,p->bufsize); |
---|
| 144 | } else { |
---|
| 145 | pitch = 0.; |
---|
| 146 | } |
---|
| 147 | return pitch; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | smpl_t aubio_pitchdetection_yin(aubio_pitchdetection_t *p, fvec_t *ibuf) { |
---|
[96fb8ad] | 151 | smpl_t pitch = 0.; |
---|
[651b97e] | 152 | aubio_pitchdetection_slideblock(p,ibuf); |
---|
[c078336] | 153 | pitch = aubio_pitchyin_getpitchfast(p->buf,p->yin, 0.5); |
---|
| 154 | if (pitch>0) { |
---|
| 155 | pitch = p->srate/(pitch+0.); |
---|
| 156 | } else { |
---|
| 157 | pitch = 0.; |
---|
| 158 | } |
---|
| 159 | return pitch; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | smpl_t aubio_pitchdetection_fcomb(aubio_pitchdetection_t *p, fvec_t *ibuf){ |
---|
[651b97e] | 164 | aubio_pitchdetection_slideblock(p,ibuf); |
---|
| 165 | return aubio_pitchfcomb_detect(p->fcomb,p->buf); |
---|
[c078336] | 166 | } |
---|
| 167 | |
---|
| 168 | smpl_t aubio_pitchdetection_schmitt(aubio_pitchdetection_t *p, fvec_t *ibuf){ |
---|
[651b97e] | 169 | aubio_pitchdetection_slideblock(p,ibuf); |
---|
| 170 | return aubio_pitchschmitt_detect(p->schmitt,p->buf); |
---|
[96fb8ad] | 171 | } |
---|