source: src/pitchdetection.c @ 4994ebb

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 4994ebb was f8a38c5, checked in by Paul Brossier <piem@altern.org>, 19 years ago

samplerate support, add filter for mcomb, yinthresh
samplerate support, add filter for mcomb, yinthresh

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