[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" |
---|
| 23 | #include "filter.h" |
---|
| 24 | #include "pitchyin.h" |
---|
| 25 | #include "pitchmcomb.h" |
---|
| 26 | #include "pitchdetection.h" |
---|
| 27 | |
---|
| 28 | struct _aubio_pitchdetection_t { |
---|
| 29 | aubio_pitchdetection_type type; |
---|
| 30 | aubio_pitchdetection_mode mode; |
---|
| 31 | uint_t srate; |
---|
| 32 | uint_t bufsize; |
---|
| 33 | /* for mcomb */ |
---|
| 34 | aubio_pvoc_t * pv; |
---|
| 35 | cvec_t * fftgrain; |
---|
| 36 | aubio_pitchmcomb_t * mcomb; |
---|
| 37 | aubio_filter_t * filter; |
---|
| 38 | /* for yin */ |
---|
| 39 | fvec_t * buf; |
---|
| 40 | fvec_t * yin; |
---|
| 41 | }; |
---|
| 42 | |
---|
| 43 | aubio_pitchdetection_t * new_aubio_pitchdetection(uint_t bufsize, |
---|
| 44 | uint_t hopsize, |
---|
| 45 | uint_t channels, |
---|
| 46 | uint_t samplerate, |
---|
| 47 | aubio_pitchdetection_type type, |
---|
| 48 | aubio_pitchdetection_mode mode) |
---|
| 49 | { |
---|
| 50 | aubio_pitchdetection_t *p = AUBIO_NEW(aubio_pitchdetection_t); |
---|
| 51 | p->srate = samplerate; |
---|
| 52 | p->type = type; |
---|
| 53 | p->bufsize = bufsize; |
---|
| 54 | switch(p->type) { |
---|
[edca23f] | 55 | case aubio_yin: |
---|
[96fb8ad] | 56 | p->buf = new_fvec(bufsize,channels); |
---|
| 57 | p->yin = new_fvec(bufsize/2,channels); |
---|
| 58 | break; |
---|
[edca23f] | 59 | case aubio_mcomb: |
---|
[96fb8ad] | 60 | p->pv = new_aubio_pvoc(bufsize, hopsize, channels); |
---|
| 61 | p->fftgrain = new_cvec(bufsize, channels); |
---|
| 62 | p->filter = new_aubio_adsgn_filter((smpl_t)samplerate); |
---|
| 63 | p->mcomb = new_aubio_pitchmcomb(bufsize,channels); |
---|
| 64 | break; |
---|
| 65 | default: |
---|
| 66 | break; |
---|
| 67 | } |
---|
| 68 | return p; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void del_aubio_pitchdetection(aubio_pitchdetection_t * p) { |
---|
| 72 | switch(p->type) { |
---|
[edca23f] | 73 | case aubio_yin: |
---|
[96fb8ad] | 74 | del_fvec(p->yin); |
---|
| 75 | del_fvec(p->buf); |
---|
| 76 | break; |
---|
[edca23f] | 77 | case aubio_mcomb: |
---|
[96fb8ad] | 78 | del_aubio_pvoc(p->pv); |
---|
| 79 | del_cvec(p->fftgrain); |
---|
| 80 | //del_aubio_adsgn_filter(p->filter); |
---|
| 81 | //del_aubio_pitchmcomb(p->mcomb); |
---|
| 82 | break; |
---|
| 83 | default: |
---|
| 84 | break; |
---|
| 85 | } |
---|
| 86 | AUBIO_FREE(p); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** \bug ugly, should replace with function pointers or so */ |
---|
| 90 | smpl_t aubio_pitchdetection(aubio_pitchdetection_t *p, fvec_t * ibuf) { |
---|
| 91 | smpl_t pitch = 0.; |
---|
[f8714ee] | 92 | uint_t i,j = 0, overlap_size = 0; |
---|
[96fb8ad] | 93 | switch(p->type) { |
---|
[edca23f] | 94 | case aubio_yin: |
---|
[f8714ee] | 95 | overlap_size = p->buf->length-ibuf->length; |
---|
[96fb8ad] | 96 | /* do sliding window blocking */ |
---|
| 97 | for (i=0;i<p->buf->channels;i++){ |
---|
[f8714ee] | 98 | for (j=0;j<overlap_size;j++){ |
---|
| 99 | p->buf->data[i][j] = |
---|
| 100 | p->buf->data[i][j+ibuf->length]; |
---|
[96fb8ad] | 101 | } |
---|
| 102 | } |
---|
| 103 | for (i=0;i<ibuf->channels;i++){ |
---|
| 104 | for (j=0;j<ibuf->length;j++){ |
---|
[f8714ee] | 105 | p->buf->data[i][j+overlap_size] = |
---|
| 106 | ibuf->data[i][j]; |
---|
[96fb8ad] | 107 | } |
---|
| 108 | } |
---|
[cf7c76a] | 109 | pitch = aubio_pitchyin_getpitchfast(p->buf,p->yin, 0.5); |
---|
[96fb8ad] | 110 | if (pitch>0) { |
---|
| 111 | pitch = p->srate/(pitch+0.); |
---|
| 112 | } else { |
---|
| 113 | pitch = 0.; |
---|
| 114 | } |
---|
| 115 | break; |
---|
[edca23f] | 116 | case aubio_mcomb: |
---|
[96fb8ad] | 117 | aubio_filter_do(p->filter,ibuf); |
---|
| 118 | aubio_filter_do(p->filter,ibuf); |
---|
| 119 | aubio_pvoc_do(p->pv,ibuf,p->fftgrain); |
---|
| 120 | pitch = aubio_pitchmcomb_detect(p->mcomb,p->fftgrain); |
---|
| 121 | if (pitch>0.) { |
---|
| 122 | pitch = bintofreq(pitch,p->srate,p->bufsize); |
---|
| 123 | } else { |
---|
| 124 | pitch = 0.; |
---|
| 125 | } |
---|
| 126 | break; |
---|
| 127 | default: |
---|
| 128 | break; |
---|
| 129 | } |
---|
| 130 | return pitch; |
---|
| 131 | } |
---|