source: src/onset/onset.c @ a243d10

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since a243d10 was 560fc50, checked in by Paul Brossier <piem@piem.org>, 7 years ago

src/onset/onset.c: add dummy default parameters for wphase (closes #150)

  • Property mode set to 100644
File size: 10.8 KB
RevLine 
[7524d0b]1/*
[df53936]2  Copyright (C) 2006-2013 Paul Brossier <piem@aubio.org>
[7524d0b]3
[e6a78ea]4  This file is part of aubio.
[7524d0b]5
[e6a78ea]6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
[7524d0b]10
[e6a78ea]11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
[7524d0b]18
19*/
20
21#include "aubio_priv.h"
[6c7d49b]22#include "fvec.h"
23#include "cvec.h"
[31907fd]24#include "spectral/specdesc.h"
[32d6958]25#include "spectral/phasevoc.h"
[9f060d1]26#include "spectral/awhitening.h"
[3e17aed]27#include "onset/peakpicker.h"
[7524d0b]28#include "mathutils.h"
[32d6958]29#include "onset/onset.h"
[7524d0b]30
[2410d3d2]31void aubio_onset_default_parameters (aubio_onset_t *o, const char_t * method);
[65c352e]32
[989bf7f]33/** structure to store object state */
[7524d0b]34struct _aubio_onset_t {
[989bf7f]35  aubio_pvoc_t * pv;            /**< phase vocoder */
[df53936]36  aubio_specdesc_t * od;        /**< spectral descriptor */
[8766cb6]37  aubio_peakpicker_t * pp;      /**< peak picker */
[989bf7f]38  cvec_t * fftgrain;            /**< phase vocoder output */
[df53936]39  fvec_t * desc;                /**< spectral description */
[989bf7f]40  smpl_t silence;               /**< silence threhsold */
41  uint_t minioi;                /**< minimum inter onset interval */
[f5e0a54]42  uint_t delay;                 /**< constant delay, in samples, removed from detected onset times */
[e4f142c]43  uint_t samplerate;            /**< sampling rate of the input signal */
[f5e0a54]44  uint_t hop_size;              /**< number of samples between two runs */
45
46  uint_t total_frames;          /**< total number of frames processed since the beginning */
47  uint_t last_onset;            /**< last detected onset location, in frames */
[9f060d1]48
[6352034]49  uint_t apply_compression;
50  smpl_t lambda_compression;
[40ed6a7]51  uint_t apply_awhitening;      /**< apply adaptive spectral whitening */
[9f060d1]52  aubio_spectral_whitening_t *spectral_whitening;
[7524d0b]53};
54
55/* execute onset detection function on iput buffer */
[00819aa]56void aubio_onset_do (aubio_onset_t *o, const fvec_t * input, fvec_t * onset)
[7524d0b]57{
[56ef7e1]58  smpl_t isonset = 0;
[7524d0b]59  aubio_pvoc_do (o->pv,input, o->fftgrain);
[9f060d1]60  /*
61  if (apply_filtering) {
62  }
63  */
[40ed6a7]64  if (o->apply_awhitening) {
[9f060d1]65    aubio_spectral_whitening_do(o->spectral_whitening, o->fftgrain);
66  }
[6352034]67  if (o->apply_compression) {
[40ed6a7]68    cvec_logmag(o->fftgrain, o->lambda_compression);
[6352034]69  }
[df53936]70  aubio_specdesc_do (o->od, o->fftgrain, o->desc);
71  aubio_peakpicker_do(o->pp, o->desc, onset);
[0b9a02a]72  isonset = onset->data[0];
[0f6f2e6]73  if (isonset > 0.) {
[7524d0b]74    if (aubio_silence_detection(input, o->silence)==1) {
[df53936]75      //AUBIO_DBG ("silent onset, not marking as onset\n");
[7524d0b]76      isonset  = 0;
77    } else {
[93579e5]78      // we have an onset
[8b884ef]79      uint_t new_onset = o->total_frames + (uint_t)ROUND(isonset * o->hop_size);
[93579e5]80      // check if last onset time was more than minioi ago
[35f73b8c]81      if (o->last_onset + o->minioi < new_onset) {
[93579e5]82        // start of file: make sure (new_onset - delay) >= 0
83        if (o->last_onset > 0 && o->delay > new_onset) {
84          isonset = 0;
85        } else {
86          //AUBIO_DBG ("accepted detection, marking as onset\n");
87          o->last_onset = MAX(o->delay, new_onset);
88        }
[7524d0b]89      } else {
[df53936]90        //AUBIO_DBG ("doubled onset, not marking as onset\n");
[7524d0b]91        isonset  = 0;
92      }
93    }
94  } else {
[763a7f1]95    // we are at the beginning of the file
96    if (o->total_frames <= o->delay) {
97      // and we don't find silence
98      if (aubio_silence_detection(input, o->silence) == 0) {
99        uint_t new_onset = o->total_frames;
100        if (o->total_frames == 0 || o->last_onset + o->minioi < new_onset) {
101          isonset = o->delay / o->hop_size;
102          o->last_onset = o->total_frames + o->delay;
103        }
104      }
[376946a]105    }
[7524d0b]106  }
[0b9a02a]107  onset->data[0] = isonset;
[f5e0a54]108  o->total_frames += o->hop_size;
[7524d0b]109  return;
110}
111
[00819aa]112uint_t aubio_onset_get_last (const aubio_onset_t *o)
[f5e0a54]113{
114  return o->last_onset - o->delay;
115}
116
[00819aa]117smpl_t aubio_onset_get_last_s (const aubio_onset_t *o)
[f5e0a54]118{
[8b884ef]119  return aubio_onset_get_last (o) / (smpl_t) (o->samplerate);
[f5e0a54]120}
121
[00819aa]122smpl_t aubio_onset_get_last_ms (const aubio_onset_t *o)
[f5e0a54]123{
[47e067b]124  return aubio_onset_get_last_s (o) * 1000.;
[f5e0a54]125}
126
[40ed6a7]127uint_t aubio_onset_set_awhitening (aubio_onset_t *o, uint_t enable)
[7ac374d]128{
[40ed6a7]129  o->apply_awhitening = enable == 1 ? 1 : 0;
[7ac374d]130  return AUBIO_OK;
131}
132
[40ed6a7]133smpl_t aubio_onset_get_awhitening (aubio_onset_t *o)
[7ac374d]134{
[40ed6a7]135  return o->apply_awhitening;
136}
137
[340f5c7]138uint_t aubio_onset_set_compression (aubio_onset_t *o, smpl_t lambda)
[40ed6a7]139{
140  if (lambda < 0.) {
141    return AUBIO_FAIL;
142  }
143  o->lambda_compression = lambda;
144  o->apply_compression = (o->lambda_compression > 0.) ? 1 : 0;
145  return AUBIO_OK;
146}
147
[340f5c7]148smpl_t aubio_onset_get_compression (aubio_onset_t *o)
[40ed6a7]149{
150  return o->apply_compression ? o->lambda_compression : 0;
[7ac374d]151}
152
[6338636]153uint_t aubio_onset_set_silence(aubio_onset_t * o, smpl_t silence) {
[7524d0b]154  o->silence = silence;
[6338636]155  return AUBIO_OK;
[7524d0b]156}
157
[00819aa]158smpl_t aubio_onset_get_silence(const aubio_onset_t * o) {
[96a96d7]159  return o->silence;
160}
161
[6338636]162uint_t aubio_onset_set_threshold(aubio_onset_t * o, smpl_t threshold) {
[8f14c6e]163  aubio_peakpicker_set_threshold(o->pp, threshold);
[6338636]164  return AUBIO_OK;
[7524d0b]165}
166
[00819aa]167smpl_t aubio_onset_get_threshold(const aubio_onset_t * o) {
[10b11d6]168  return aubio_peakpicker_get_threshold(o->pp);
169}
170
[6338636]171uint_t aubio_onset_set_minioi(aubio_onset_t * o, uint_t minioi) {
[35f73b8c]172  o->minioi = minioi;
[6338636]173  return AUBIO_OK;
[7524d0b]174}
175
[00819aa]176uint_t aubio_onset_get_minioi(const aubio_onset_t * o) {
[f5e0a54]177  return o->minioi;
178}
179
[35f73b8c]180uint_t aubio_onset_set_minioi_s(aubio_onset_t * o, smpl_t minioi) {
[44a3e5e]181  return aubio_onset_set_minioi (o, (uint_t)ROUND(minioi * o->samplerate));
[35f73b8c]182}
183
[00819aa]184smpl_t aubio_onset_get_minioi_s(const aubio_onset_t * o) {
[35f73b8c]185  return aubio_onset_get_minioi (o) / (smpl_t) o->samplerate;
186}
187
188uint_t aubio_onset_set_minioi_ms(aubio_onset_t * o, smpl_t minioi) {
189  return aubio_onset_set_minioi_s (o, minioi / 1000.);
190}
191
[00819aa]192smpl_t aubio_onset_get_minioi_ms(const aubio_onset_t * o) {
[35f73b8c]193  return aubio_onset_get_minioi_s (o) * 1000.;
194}
195
[f5e0a54]196uint_t aubio_onset_set_delay(aubio_onset_t * o, uint_t delay) {
197  o->delay = delay;
198  return AUBIO_OK;
199}
200
[00819aa]201uint_t aubio_onset_get_delay(const aubio_onset_t * o) {
[f5e0a54]202  return o->delay;
203}
204
205uint_t aubio_onset_set_delay_s(aubio_onset_t * o, smpl_t delay) {
206  return aubio_onset_set_delay (o, delay * o->samplerate);
207}
208
[00819aa]209smpl_t aubio_onset_get_delay_s(const aubio_onset_t * o) {
[f5e0a54]210  return aubio_onset_get_delay (o) / (smpl_t) o->samplerate;
211}
212
213uint_t aubio_onset_set_delay_ms(aubio_onset_t * o, smpl_t delay) {
214  return aubio_onset_set_delay_s (o, delay / 1000.);
215}
216
[00819aa]217smpl_t aubio_onset_get_delay_ms(const aubio_onset_t * o) {
[f5e0a54]218  return aubio_onset_get_delay_s (o) * 1000.;
219}
220
[00819aa]221smpl_t aubio_onset_get_descriptor(const aubio_onset_t * o) {
[df53936]222  return o->desc->data[0];
[35f73b8c]223}
224
[00819aa]225smpl_t aubio_onset_get_thresholded_descriptor(const aubio_onset_t * o) {
[35f73b8c]226  fvec_t * thresholded = aubio_peakpicker_get_thresholded_input(o->pp);
227  return thresholded->data[0];
228}
229
[7524d0b]230/* Allocate memory for an onset detection */
[00819aa]231aubio_onset_t * new_aubio_onset (const char_t * onset_mode,
[0b9a02a]232    uint_t buf_size, uint_t hop_size, uint_t samplerate)
[7524d0b]233{
234  aubio_onset_t * o = AUBIO_NEW(aubio_onset_t);
[892c369]235
236  /* check parameters are valid */
237  if ((sint_t)hop_size < 1) {
238    AUBIO_ERR("onset: got hop_size %d, but can not be < 1\n", hop_size);
239    goto beach;
[d897aae]240  } else if ((sint_t)buf_size < 2) {
241    AUBIO_ERR("onset: got buffer_size %d, but can not be < 2\n", buf_size);
[892c369]242    goto beach;
243  } else if (buf_size < hop_size) {
[0ff1b40]244    AUBIO_ERR("onset: hop size (%d) is larger than win size (%d)\n", hop_size, buf_size);
[892c369]245    goto beach;
246  } else if ((sint_t)samplerate < 1) {
247    AUBIO_ERR("onset: samplerate (%d) can not be < 1\n", samplerate);
248    goto beach;
249  }
250
[8f14c6e]251  /* store creation parameters */
[35f73b8c]252  o->samplerate = samplerate;
253  o->hop_size = hop_size;
[8f14c6e]254
255  /* allocate memory */
[35f73b8c]256  o->pv = new_aubio_pvoc(buf_size, o->hop_size);
[0b9a02a]257  o->pp = new_aubio_peakpicker();
258  o->od = new_aubio_specdesc(onset_mode,buf_size);
[078dad8]259  if (o->od == NULL) goto beach_specdesc;
[0b9a02a]260  o->fftgrain = new_cvec(buf_size);
[df53936]261  o->desc = new_fvec(1);
[9f060d1]262  o->spectral_whitening = new_aubio_spectral_whitening(buf_size, hop_size, samplerate);
[65c352e]263
[8f14c6e]264  /* initialize internal variables */
[fa3edc6]265  aubio_onset_set_default_parameters (o, onset_mode);
266
267  aubio_onset_reset(o);
[7524d0b]268  return o;
[892c369]269
[078dad8]270beach_specdesc:
271  del_aubio_peakpicker(o->pp);
272  del_aubio_pvoc(o->pv);
[892c369]273beach:
274  AUBIO_FREE(o);
275  return NULL;
[7524d0b]276}
277
[fa3edc6]278void aubio_onset_reset (aubio_onset_t *o) {
279  o->last_onset = 0;
280  o->total_frames = 0;
281}
282
283uint_t aubio_onset_set_default_parameters (aubio_onset_t * o, const char_t * onset_mode)
[65c352e]284{
[fa3edc6]285  uint_t ret = AUBIO_OK;
[65c352e]286  /* set some default parameter */
287  aubio_onset_set_threshold (o, 0.3);
288  aubio_onset_set_delay (o, 4.3 * o->hop_size);
289  aubio_onset_set_minioi_ms (o, 50.);
290  aubio_onset_set_silence (o, -70.);
[40ed6a7]291  // disable spectral whitening
292  aubio_onset_set_awhitening (o, 0);
293  // disable logarithmic magnitude
[340f5c7]294  aubio_onset_set_compression (o, 0.);
[65c352e]295
296  /* method specific optimisations */
297  if (strcmp (onset_mode, "energy") == 0) {
[00d0275]298  } else if (strcmp (onset_mode, "hfc") == 0 || strcmp (onset_mode, "default") == 0) {
[6352034]299    aubio_onset_set_threshold (o, 0.058);
[340f5c7]300    aubio_onset_set_compression (o, 1.);
[65c352e]301  } else if (strcmp (onset_mode, "complexdomain") == 0
302             || strcmp (onset_mode, "complex") == 0) {
303    aubio_onset_set_delay (o, 4.6 * o->hop_size);
304    aubio_onset_set_threshold (o, 0.15);
[bd973b0]305    aubio_onset_set_awhitening(o, 1);
306    aubio_onset_set_compression (o, 1.);
[65c352e]307  } else if (strcmp (onset_mode, "phase") == 0) {
[6352034]308    o->apply_compression = 0;
[bd973b0]309    aubio_onset_set_awhitening (o, 0);
[560fc50]310  } else if (strcmp (onset_mode, "wphase") == 0) {
311    // use defaults for now
[65c352e]312  } else if (strcmp (onset_mode, "mkl") == 0) {
313    aubio_onset_set_threshold (o, 0.05);
[bd973b0]314    aubio_onset_set_awhitening(o, 1);
315    aubio_onset_set_compression (o, 0.02);
[65c352e]316  } else if (strcmp (onset_mode, "kl") == 0) {
317    aubio_onset_set_threshold (o, 0.35);
[bd973b0]318    aubio_onset_set_awhitening(o, 1);
319    aubio_onset_set_compression (o, 0.02);
[65c352e]320  } else if (strcmp (onset_mode, "specflux") == 0) {
[dccfad2]321    aubio_onset_set_threshold (o, 0.18);
[bd973b0]322    aubio_onset_set_awhitening(o, 1);
[dccfad2]323    aubio_spectral_whitening_set_relax_time(o->spectral_whitening, 100);
324    aubio_spectral_whitening_set_floor(o->spectral_whitening, 1.);
325    aubio_onset_set_compression (o, 10.);
[65c352e]326  } else if (strcmp (onset_mode, "specdiff") == 0) {
[c82859f]327  } else if (strcmp (onset_mode, "old_default") == 0) {
328    // used to reproduce results obtained with the previous version
329    aubio_onset_set_threshold (o, 0.3);
330    aubio_onset_set_minioi_ms (o, 20.);
331    aubio_onset_set_compression (o, 0.);
[65c352e]332  } else {
[b75f890]333    AUBIO_WRN("onset: unknown spectral descriptor type %s, "
[65c352e]334               "using default parameters.\n", onset_mode);
[fa3edc6]335    ret = AUBIO_FAIL;
[65c352e]336  }
[fa3edc6]337  return ret;
[65c352e]338}
339
[7524d0b]340void del_aubio_onset (aubio_onset_t *o)
341{
[9f060d1]342  del_aubio_spectral_whitening(o->spectral_whitening);
[31907fd]343  del_aubio_specdesc(o->od);
[7524d0b]344  del_aubio_peakpicker(o->pp);
345  del_aubio_pvoc(o->pv);
[df53936]346  del_fvec(o->desc);
[7524d0b]347  del_cvec(o->fftgrain);
348  AUBIO_FREE(o);
349}
Note: See TracBrowser for help on using the repository browser.