source: src/pitchdetection.c @ 7cded32

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

fix default yin thresholds
fix default yin thresholds

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