source: src/pitch/pitchfcomb.c @ 554a9cb

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

remove src/sample.h

  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[7a04950]1/*
2   Copyright (C) 2004, 2005  Mario Lang <mlang@delysid.org>
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
20#include "aubio_priv.h"
[6c7d49b]21#include "fvec.h"
22#include "cvec.h"
[7a04950]23#include "mathutils.h"
[32d6958]24#include "spectral/fft.h"
[2d8cffa]25#include "pitch/pitchfcomb.h"
[7a04950]26
27#define MAX_PEAKS 8
28
29typedef struct {
30  smpl_t freq;
31  smpl_t db;
32} aubio_fpeak_t;
33
34struct _aubio_pitchfcomb_t {
[c8cbf3c]35  uint_t fftSize;
36  uint_t stepSize;
37  uint_t rate;
38  fvec_t * winput;
39  fvec_t * win;
40  cvec_t * fftOut;
41  fvec_t * fftLastPhase;
42  aubio_fft_t * fft;
43  //aubio_pvoc_t * pvoc;
[7a04950]44};
45
[fbd3de6]46aubio_pitchfcomb_t * new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize, uint_t samplerate)
[7a04950]47{
48  aubio_pitchfcomb_t * p = AUBIO_NEW(aubio_pitchfcomb_t);
49  p->rate         = samplerate;
[fbd3de6]50  p->fftSize      = bufsize;
[c8cbf3c]51  p->stepSize     = hopsize;
[fbd3de6]52  p->winput       = new_fvec(bufsize,1);
53  p->fftOut       = new_cvec(bufsize,1);
54  p->fftLastPhase = new_fvec(bufsize,1);
[8b2dc90]55  p->fft = new_aubio_fft(bufsize, 1);
[fbd3de6]56  p->win = new_fvec(bufsize,1);
57  aubio_window(p->win->data[0], bufsize, aubio_win_hanning);
[7a04950]58  return p;
59}
60
61/* input must be stepsize long */
62smpl_t aubio_pitchfcomb_detect (aubio_pitchfcomb_t * p, fvec_t * input)
63{
[fbd3de6]64  uint_t k, l, maxharm = 0;
[7a04950]65  smpl_t freqPerBin = p->rate/(smpl_t)p->fftSize,
[fbd3de6]66    phaseDifference = TWO_PI*(smpl_t)p->stepSize/(smpl_t)p->fftSize;
[7a04950]67  aubio_fpeak_t peaks[MAX_PEAKS];
68
69  for (k=0; k<MAX_PEAKS; k++) {
70    peaks[k].db = -200.;
71    peaks[k].freq = 0.;
72  }
73
[fbd3de6]74  for (k=0; k < input->length; k++){
[c8cbf3c]75    p->winput->data[0][k] = p->win->data[0][k] * input->data[0][k];
[fbd3de6]76  }
[8b2dc90]77  aubio_fft_do(p->fft,p->winput,p->fftOut);
[7a04950]78
[fbd3de6]79  for (k=0; k<=p->fftSize/2; k++) {
[7a04950]80    smpl_t
81      magnitude = 20.*LOG10(2.*p->fftOut->norm[0][k]/(smpl_t)p->fftSize),
82      phase     = p->fftOut->phas[0][k],
83      tmp, freq;
84
85    /* compute phase difference */
86    tmp = phase - p->fftLastPhase->data[0][k];
87    p->fftLastPhase->data[0][k] = phase;
88
89    /* subtract expected phase difference */
90    tmp -= (smpl_t)k*phaseDifference;
91
92    /* map delta phase into +/- Pi interval */
[1d4fc4a]93    tmp = aubio_unwrap2pi(tmp);
[7a04950]94
95    /* get deviation from bin frequency from the +/- Pi interval */
[1d4fc4a]96    tmp = p->fftSize/(smpl_t)p->stepSize*tmp/(TWO_PI);
[7a04950]97
98    /* compute the k-th partials' true frequency */
99    freq = (smpl_t)k*freqPerBin + tmp*freqPerBin;
100
[fbd3de6]101    if (freq > 0.0 && magnitude > peaks[0].db) { // && magnitude < 0) {
[7a04950]102      memmove(peaks+1, peaks, sizeof(aubio_fpeak_t)*(MAX_PEAKS-1));
103      peaks[0].freq = freq;
104      peaks[0].db = magnitude;
105    }
106  }
[c8cbf3c]107
[7a04950]108  k = 0;
109  for (l=1; l<MAX_PEAKS && peaks[l].freq > 0.0; l++) {
110    sint_t harmonic;
111    for (harmonic=5; harmonic>1; harmonic--) {
112      if (peaks[0].freq / peaks[l].freq < harmonic+.02 &&
[c8cbf3c]113          peaks[0].freq / peaks[l].freq > harmonic-.02) {
[b33e355]114        if (harmonic > (sint_t)maxharm &&
[c8cbf3c]115            peaks[0].db < peaks[l].db/2) {
[7a04950]116          maxharm = harmonic;
[c8cbf3c]117          k = l;
[7a04950]118        }
119      }
120    }
121  }
[8d84c0b]122  /* quick hack to clean output a bit */
[f97445c]123  if (peaks[k].freq > 5000.) return 0.;
[7a04950]124  return peaks[k].freq;
125}
126
127void del_aubio_pitchfcomb (aubio_pitchfcomb_t * p)
128{
129  del_cvec(p->fftOut);
130  del_fvec(p->fftLastPhase);
[028c8e9]131  del_fvec(p->win);
132  del_fvec(p->winput);
[8b2dc90]133  del_aubio_fft(p->fft);
[7a04950]134  AUBIO_FREE(p);
135}
136
Note: See TracBrowser for help on using the repository browser.