source: src/pitchdetection.c @ aa17581

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

added midi and hertz output modes

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