source: src/pitch/pitchfcomb.c @ f5ea4fb

feature/cnn_orgfeature/crepe_org
Last change on this file since f5ea4fb was e9a2af1, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/pitch/pitchfcomb.c: return NULL if fft creation failed

  • Property mode set to 100644
File size: 3.7 KB
RevLine 
[7a04950]1/*
[e6a78ea]2  Copyright (C) 2004, 2005  Mario Lang <mlang@delysid.org>
3  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
[7a04950]4
[e6a78ea]5  This file is part of aubio.
[7a04950]6
[e6a78ea]7  aubio is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
[7a04950]11
[e6a78ea]12  aubio is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  GNU General Public License for more details.
16
17  You should have received a copy of the GNU General Public License
18  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
[7a04950]19
20*/
21
22#include "aubio_priv.h"
[6c7d49b]23#include "fvec.h"
24#include "cvec.h"
[7a04950]25#include "mathutils.h"
[83963b3]26#include "musicutils.h"
[32d6958]27#include "spectral/fft.h"
[2d8cffa]28#include "pitch/pitchfcomb.h"
[7a04950]29
30#define MAX_PEAKS 8
31
[fddfa64]32typedef struct
33{
[8040cca]34  smpl_t bin;
[7a04950]35  smpl_t db;
36} aubio_fpeak_t;
37
[fddfa64]38struct _aubio_pitchfcomb_t
39{
[c8cbf3c]40  uint_t fftSize;
41  uint_t stepSize;
42  uint_t rate;
[fddfa64]43  fvec_t *winput;
44  fvec_t *win;
45  cvec_t *fftOut;
46  fvec_t *fftLastPhase;
47  aubio_fft_t *fft;
[7a04950]48};
49
[fddfa64]50aubio_pitchfcomb_t *
[168337e]51new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize)
[7a04950]52{
[fddfa64]53  aubio_pitchfcomb_t *p = AUBIO_NEW (aubio_pitchfcomb_t);
54  p->fftSize = bufsize;
55  p->stepSize = hopsize;
[e9a2af1]56  p->fft = new_aubio_fft (bufsize);
57  if (!p->fft) goto beach;
[168337e]58  p->winput = new_fvec (bufsize);
59  p->fftOut = new_cvec (bufsize);
60  p->fftLastPhase = new_fvec (bufsize);
[fddfa64]61  p->win = new_aubio_window ("hanning", bufsize);
[7a04950]62  return p;
[e9a2af1]63
64beach:
65  AUBIO_FREE(p);
66  return NULL;
[7a04950]67}
68
69/* input must be stepsize long */
[fddfa64]70void
[ce3ff2b]71aubio_pitchfcomb_do (aubio_pitchfcomb_t * p, const fvec_t * input, fvec_t * output)
[7a04950]72{
[168337e]73  uint_t k, l, maxharm = 0;
[fddfa64]74  smpl_t phaseDifference = TWO_PI * (smpl_t) p->stepSize / (smpl_t) p->fftSize;
[7a04950]75  aubio_fpeak_t peaks[MAX_PEAKS];
76
[168337e]77  for (k = 0; k < MAX_PEAKS; k++) {
78    peaks[k].db = -200.;
79    peaks[k].bin = 0.;
80  }
[7a04950]81
[168337e]82  for (k = 0; k < input->length; k++) {
83    p->winput->data[k] = p->win->data[k] * input->data[k];
84  }
85  aubio_fft_do (p->fft, p->winput, p->fftOut);
[7a04950]86
[168337e]87  for (k = 0; k <= p->fftSize / 2; k++) {
88    smpl_t
89        magnitude =
90        20. * LOG10 (2. * p->fftOut->norm[k] / (smpl_t) p->fftSize),
91        phase = p->fftOut->phas[k], tmp, bin;
[7a04950]92
[168337e]93    /* compute phase difference */
94    tmp = phase - p->fftLastPhase->data[k];
95    p->fftLastPhase->data[k] = phase;
[7a04950]96
[168337e]97    /* subtract expected phase difference */
98    tmp -= (smpl_t) k *phaseDifference;
[7a04950]99
[168337e]100    /* map delta phase into +/- Pi interval */
101    tmp = aubio_unwrap2pi (tmp);
[7a04950]102
[168337e]103    /* get deviation from bin frequency from the +/- Pi interval */
104    tmp = p->fftSize / (smpl_t) p->stepSize * tmp / (TWO_PI);
[7a04950]105
[168337e]106    /* compute the k-th partials' true bin */
107    bin = (smpl_t) k + tmp;
[7a04950]108
[168337e]109    if (bin > 0.0 && magnitude > peaks[0].db) {       // && magnitude < 0) {
110      memmove (peaks + 1, peaks, sizeof (aubio_fpeak_t) * (MAX_PEAKS - 1));
111      peaks[0].bin = bin;
112      peaks[0].db = magnitude;
[7a04950]113    }
[168337e]114  }
[c8cbf3c]115
[168337e]116  k = 0;
117  for (l = 1; l < MAX_PEAKS && peaks[l].bin > 0.0; l++) {
118    sint_t harmonic;
119    for (harmonic = 5; harmonic > 1; harmonic--) {
120      if (peaks[0].bin / peaks[l].bin < harmonic + .02 &&
121          peaks[0].bin / peaks[l].bin > harmonic - .02) {
122        if (harmonic > (sint_t) maxharm && peaks[0].db < peaks[l].db / 2) {
123          maxharm = harmonic;
124          k = l;
[7a04950]125        }
126      }
127    }
[8040cca]128  }
[168337e]129  output->data[0] = peaks[k].bin;
130  /* quick hack to clean output a bit */
131  if (peaks[k].bin > 5000.)
132    output->data[0] = 0.;
[7a04950]133}
134
[fddfa64]135void
136del_aubio_pitchfcomb (aubio_pitchfcomb_t * p)
[7a04950]137{
[fddfa64]138  del_cvec (p->fftOut);
139  del_fvec (p->fftLastPhase);
140  del_fvec (p->win);
141  del_fvec (p->winput);
142  del_aubio_fft (p->fft);
143  AUBIO_FREE (p);
[7a04950]144}
Note: See TracBrowser for help on using the repository browser.