source: src/pitchdetection.c @ 7e20db8b

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

update pitchdetection docs, make wrapper methods private
update pitchdetection docs, make wrapper methods private

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