source: src/pitchdetection.c @ dcc8d90

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since dcc8d90 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
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"
[c078336]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;
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;
[96fb8ad]52};
53
[3ec9d9c]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
[96fb8ad]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;
[aa17581]81        p->mode = mode;
[96fb8ad]82        p->bufsize = bufsize;
83        switch(p->type) {
[5e9c68a]84                case aubio_pitch_yin:
[96fb8ad]85                        p->buf      = new_fvec(bufsize,channels);
86                        p->yin      = new_fvec(bufsize/2,channels);
[c078336]87                        p->callback = aubio_pitchdetection_yin;
[96fb8ad]88                        break;
[5e9c68a]89                case aubio_pitch_mcomb:
[96fb8ad]90                        p->pv       = new_aubio_pvoc(bufsize, hopsize, channels);
91                        p->fftgrain = new_cvec(bufsize, channels);
92                        p->mcomb    = new_aubio_pitchmcomb(bufsize,channels);
[c078336]93                        p->callback = aubio_pitchdetection_mcomb;
[96fb8ad]94                        break;
[5e9c68a]95                case aubio_pitch_fcomb:
[651b97e]96                        p->buf      = new_fvec(bufsize,channels);
[c078336]97                        p->fcomb    = new_aubio_pitchfcomb(bufsize,samplerate);
98                        p->callback = aubio_pitchdetection_fcomb;
99                        break;
[5e9c68a]100                case aubio_pitch_schmitt:
[651b97e]101                        p->buf      = new_fvec(bufsize,channels);
[c078336]102                        p->schmitt  = new_aubio_pitchschmitt(bufsize,samplerate);
[651b97e]103                        p->callback = aubio_pitchdetection_schmitt;
[c078336]104                        break;
105                default:
106                        break;
[96fb8ad]107        }
[aa17581]108        switch(p->mode) {
109                case aubio_pitchm_freq:
110                        p->freqconv = freqconvpass;
111                        break;
112                case aubio_pitchm_midi:
[3ec9d9c]113                        p->freqconv = freqconvmidi;
[aa17581]114                        break;
115                case aubio_pitchm_cent:
116                        /** bug: not implemented */
[3ec9d9c]117                        p->freqconv = freqconvmidi;
[aa17581]118                        break;
119                case aubio_pitchm_bin:
[3ec9d9c]120                        p->freqconv = freqconvbin;
[aa17581]121                        break;
122                default:
123                        break;
124        }
[96fb8ad]125        return p;
126}
127
128void del_aubio_pitchdetection(aubio_pitchdetection_t * p) {
129        switch(p->type) {
[5e9c68a]130                case aubio_pitch_yin:
[96fb8ad]131                        del_fvec(p->yin);
132                        del_fvec(p->buf);
133                        break;
[5e9c68a]134                case aubio_pitch_mcomb:
[96fb8ad]135                        del_aubio_pvoc(p->pv);
136                        del_cvec(p->fftgrain);
[c078336]137                        del_aubio_pitchmcomb(p->mcomb);
[96fb8ad]138                        break;
[5e9c68a]139                case aubio_pitch_schmitt:
[651b97e]140                        del_fvec(p->buf);
[c078336]141                        del_aubio_pitchschmitt(p->schmitt);
142                        break;
[5e9c68a]143                case aubio_pitch_fcomb:
[651b97e]144                        del_fvec(p->buf);
[c078336]145                        del_aubio_pitchfcomb(p->fcomb);
146                        break;
[96fb8ad]147                default:
148                        break;
149        }
150        AUBIO_FREE(p);
151}
152
[651b97e]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
[96fb8ad]170smpl_t aubio_pitchdetection(aubio_pitchdetection_t *p, fvec_t * ibuf) {
[3ec9d9c]171        return p->freqconv(p->callback(p,ibuf),p->srate,p->bufsize);
[c078336]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);
[28d8c4a]178        /** \bug should move the >0 check within aubio_bintofreq */
[c078336]179        if (pitch>0.) {
[28d8c4a]180                pitch = aubio_bintofreq(pitch,p->srate,p->bufsize);
[c078336]181        } else {
182                pitch = 0.;
183        }
184        return pitch;
185}
186
187smpl_t aubio_pitchdetection_yin(aubio_pitchdetection_t *p, fvec_t *ibuf) {
[96fb8ad]188        smpl_t pitch = 0.;
[651b97e]189        aubio_pitchdetection_slideblock(p,ibuf);
[c078336]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){
[651b97e]201        aubio_pitchdetection_slideblock(p,ibuf);
202        return aubio_pitchfcomb_detect(p->fcomb,p->buf);
[c078336]203}
204
205smpl_t aubio_pitchdetection_schmitt(aubio_pitchdetection_t *p, fvec_t *ibuf){
[651b97e]206        aubio_pitchdetection_slideblock(p,ibuf);
207        return aubio_pitchschmitt_detect(p->schmitt,p->buf);
[96fb8ad]208}
Note: See TracBrowser for help on using the repository browser.