[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) { |
---|
| 55 | case yin: |
---|
| 56 | p->buf = new_fvec(bufsize,channels); |
---|
| 57 | p->yin = new_fvec(bufsize/2,channels); |
---|
| 58 | break; |
---|
| 59 | case mcomb: |
---|
| 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) { |
---|
| 73 | case yin: |
---|
| 74 | del_fvec(p->yin); |
---|
| 75 | del_fvec(p->buf); |
---|
| 76 | break; |
---|
| 77 | case mcomb: |
---|
| 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.; |
---|
| 92 | uint_t i,j = 0; |
---|
| 93 | switch(p->type) { |
---|
| 94 | case yin: |
---|
| 95 | /* do sliding window blocking */ |
---|
| 96 | for (i=0;i<p->buf->channels;i++){ |
---|
| 97 | for (j=0;j<p->buf->length-ibuf->length;j++){ |
---|
| 98 | p->buf->data[i][j] = p->buf->data[i][j+ibuf->length]; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | for (i=0;i<ibuf->channels;i++){ |
---|
| 102 | for (j=0;j<ibuf->length;j++){ |
---|
| 103 | p->buf->data[i][j] = ibuf->data[i][j]; |
---|
| 104 | } |
---|
| 105 | } |
---|
[cf7c76a] | 106 | pitch = aubio_pitchyin_getpitchfast(p->buf,p->yin, 0.5); |
---|
[96fb8ad] | 107 | if (pitch>0) { |
---|
| 108 | pitch = p->srate/(pitch+0.); |
---|
| 109 | } else { |
---|
| 110 | pitch = 0.; |
---|
| 111 | } |
---|
| 112 | break; |
---|
| 113 | case mcomb: |
---|
| 114 | aubio_filter_do(p->filter,ibuf); |
---|
| 115 | aubio_filter_do(p->filter,ibuf); |
---|
| 116 | aubio_pvoc_do(p->pv,ibuf,p->fftgrain); |
---|
| 117 | pitch = aubio_pitchmcomb_detect(p->mcomb,p->fftgrain); |
---|
| 118 | if (pitch>0.) { |
---|
| 119 | pitch = bintofreq(pitch,p->srate,p->bufsize); |
---|
| 120 | } else { |
---|
| 121 | pitch = 0.; |
---|
| 122 | } |
---|
| 123 | break; |
---|
| 124 | default: |
---|
| 125 | break; |
---|
| 126 | } |
---|
| 127 | return pitch; |
---|
| 128 | } |
---|