source: src/pitchdetection.c @ 3ec9d9c

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

wrapper functions to add pitchm_bin output

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