source: src/pitchdetection.c @ 6610143

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 6610143 was f8a38c5, checked in by Paul Brossier <piem@altern.org>, 18 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
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 "pitchdetection.h"
29
30typedef smpl_t (*aubio_pitchdetection_func_t)(aubio_pitchdetection_t *p, 
31                fvec_t * ibuf);
32typedef smpl_t (*aubio_pitchdetection_conv_t)(smpl_t value,uint_t srate,uint_t bufsize);
33void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf);
34
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;
44        aubio_pitchfcomb_t * fcomb;
45        aubio_pitchschmitt_t * schmitt;
46        aubio_filter_t * filter;
47        /* for yin */
48        fvec_t * buf;
49        fvec_t * yin;
50        aubio_pitchdetection_func_t callback;
51        aubio_pitchdetection_conv_t freqconv;
52        smpl_t yinthres;
53};
54
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
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;
82        p->mode = mode;
83        p->bufsize = bufsize;
84        switch(p->type) {
85                case aubio_pitch_yin:
86                        p->buf      = new_fvec(bufsize,channels);
87                        p->yin      = new_fvec(bufsize/2,channels);
88                        p->callback = aubio_pitchdetection_yin;
89                        p->yinthres = 0.2;
90                        break;
91                case aubio_pitch_mcomb:
92                        p->pv       = new_aubio_pvoc(bufsize, hopsize, channels);
93                        p->fftgrain = new_cvec(bufsize, channels);
94                        p->mcomb    = new_aubio_pitchmcomb(bufsize,channels,samplerate);
95                        p->filter   = new_aubio_cdsgn_filter(samplerate);
96                        p->callback = aubio_pitchdetection_mcomb;
97                        break;
98                case aubio_pitch_fcomb:
99                        p->buf      = new_fvec(bufsize,channels);
100                        p->fcomb    = new_aubio_pitchfcomb(bufsize,hopsize,samplerate);
101                        p->callback = aubio_pitchdetection_fcomb;
102                        break;
103                case aubio_pitch_schmitt:
104                        p->buf      = new_fvec(bufsize,channels);
105                        p->schmitt  = new_aubio_pitchschmitt(bufsize,samplerate);
106                        p->callback = aubio_pitchdetection_schmitt;
107                        break;
108                default:
109                        break;
110        }
111        switch(p->mode) {
112                case aubio_pitchm_freq:
113                        p->freqconv = freqconvpass;
114                        break;
115                case aubio_pitchm_midi:
116                        p->freqconv = freqconvmidi;
117                        break;
118                case aubio_pitchm_cent:
119                        /** bug: not implemented */
120                        p->freqconv = freqconvmidi;
121                        break;
122                case aubio_pitchm_bin:
123                        p->freqconv = freqconvbin;
124                        break;
125                default:
126                        break;
127        }
128        return p;
129}
130
131void del_aubio_pitchdetection(aubio_pitchdetection_t * p) {
132        switch(p->type) {
133                case aubio_pitch_yin:
134                        del_fvec(p->yin);
135                        del_fvec(p->buf);
136                        break;
137                case aubio_pitch_mcomb:
138                        del_aubio_pvoc(p->pv);
139                        del_cvec(p->fftgrain);
140                        del_aubio_pitchmcomb(p->mcomb);
141                        break;
142                case aubio_pitch_schmitt:
143                        del_fvec(p->buf);
144                        del_aubio_pitchschmitt(p->schmitt);
145                        break;
146                case aubio_pitch_fcomb:
147                        del_fvec(p->buf);
148                        del_aubio_pitchfcomb(p->fcomb);
149                        break;
150                default:
151                        break;
152        }
153        AUBIO_FREE(p);
154}
155
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
173void aubio_pitchdetection_set_yinthresh(aubio_pitchdetection_t *p, smpl_t thres) {
174        p->yinthres = thres;
175}
176
177smpl_t aubio_pitchdetection(aubio_pitchdetection_t *p, fvec_t * ibuf) {
178        return p->freqconv(p->callback(p,ibuf),p->srate,p->bufsize);
179}
180
181smpl_t aubio_pitchdetection_mcomb(aubio_pitchdetection_t *p, fvec_t *ibuf) {
182        smpl_t pitch = 0.;
183        aubio_filter_do(p->filter,ibuf);
184        aubio_pvoc_do(p->pv,ibuf,p->fftgrain);
185        pitch = aubio_pitchmcomb_detect(p->mcomb,p->fftgrain);
186        /** \bug should move the >0 check within aubio_bintofreq */
187        if (pitch>0.) {
188                pitch = aubio_bintofreq(pitch,p->srate,p->bufsize);
189        } else {
190                pitch = 0.;
191        }
192        return pitch;
193}
194
195smpl_t aubio_pitchdetection_yin(aubio_pitchdetection_t *p, fvec_t *ibuf) {
196        smpl_t pitch = 0.;
197        aubio_pitchdetection_slideblock(p,ibuf);
198        pitch = aubio_pitchyin_getpitchfast(p->buf,p->yin, p->yinthres);
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){
209        aubio_pitchdetection_slideblock(p,ibuf);
210        return aubio_pitchfcomb_detect(p->fcomb,p->buf);
211}
212
213smpl_t aubio_pitchdetection_schmitt(aubio_pitchdetection_t *p, fvec_t *ibuf){
214        aubio_pitchdetection_slideblock(p,ibuf);
215        return aubio_pitchschmitt_detect(p->schmitt,p->buf);
216}
Note: See TracBrowser for help on using the repository browser.