Changeset 633400d for src/onset/onset.c


Ignore:
Timestamp:
Dec 5, 2018, 10:34:39 PM (5 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/cnn, feature/crepe, feature/pitchshift, feature/timestretch, fix/ffmpeg5, master
Children:
283a619a
Parents:
5b46bc3 (diff), f19db54 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into feature/pitchshift

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/onset/onset.c

    r5b46bc3 r633400d  
    2424#include "spectral/specdesc.h"
    2525#include "spectral/phasevoc.h"
     26#include "spectral/awhitening.h"
    2627#include "onset/peakpicker.h"
    2728#include "mathutils.h"
    2829#include "onset/onset.h"
     30
     31void aubio_onset_default_parameters (aubio_onset_t *o, const char_t * method);
    2932
    3033/** structure to store object state */
     
    4346  uint_t total_frames;          /**< total number of frames processed since the beginning */
    4447  uint_t last_onset;            /**< last detected onset location, in frames */
     48
     49  uint_t apply_compression;
     50  smpl_t lambda_compression;
     51  uint_t apply_awhitening;      /**< apply adaptive spectral whitening */
     52  aubio_spectral_whitening_t *spectral_whitening;
    4553};
    4654
     
    5058  smpl_t isonset = 0;
    5159  aubio_pvoc_do (o->pv,input, o->fftgrain);
     60  /*
     61  if (apply_filtering) {
     62  }
     63  */
     64  if (o->apply_awhitening) {
     65    aubio_spectral_whitening_do(o->spectral_whitening, o->fftgrain);
     66  }
     67  if (o->apply_compression) {
     68    cvec_logmag(o->fftgrain, o->lambda_compression);
     69  }
    5270  aubio_specdesc_do (o->od, o->fftgrain, o->desc);
    5371  aubio_peakpicker_do(o->pp, o->desc, onset);
     
    5876      isonset  = 0;
    5977    } else {
     78      // we have an onset
    6079      uint_t new_onset = o->total_frames + (uint_t)ROUND(isonset * o->hop_size);
     80      // check if last onset time was more than minioi ago
    6181      if (o->last_onset + o->minioi < new_onset) {
    62         //AUBIO_DBG ("accepted detection, marking as onset\n");
    63         o->last_onset = new_onset;
     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        }
    6489      } else {
    6590        //AUBIO_DBG ("doubled onset, not marking as onset\n");
     
    100125}
    101126
     127uint_t aubio_onset_set_awhitening (aubio_onset_t *o, uint_t enable)
     128{
     129  o->apply_awhitening = enable == 1 ? 1 : 0;
     130  return AUBIO_OK;
     131}
     132
     133smpl_t aubio_onset_get_awhitening (aubio_onset_t *o)
     134{
     135  return o->apply_awhitening;
     136}
     137
     138uint_t aubio_onset_set_compression (aubio_onset_t *o, smpl_t lambda)
     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
     148smpl_t aubio_onset_get_compression (aubio_onset_t *o)
     149{
     150  return o->apply_compression ? o->lambda_compression : 0;
     151}
     152
    102153uint_t aubio_onset_set_silence(aubio_onset_t * o, smpl_t silence) {
    103154  o->silence = silence;
     
    206257  o->pp = new_aubio_peakpicker();
    207258  o->od = new_aubio_specdesc(onset_mode,buf_size);
    208   if (o->od == NULL) goto beach_specdesc;
    209259  o->fftgrain = new_cvec(buf_size);
    210260  o->desc = new_fvec(1);
    211 
     261  o->spectral_whitening = new_aubio_spectral_whitening(buf_size, hop_size, samplerate);
     262
     263  if (!o->pv || !o->pp || !o->od || !o->fftgrain
     264      || !o->desc || !o->spectral_whitening)
     265    goto beach;
     266
     267  /* initialize internal variables */
     268  aubio_onset_set_default_parameters (o, onset_mode);
     269
     270  aubio_onset_reset(o);
     271  return o;
     272
     273beach:
     274  del_aubio_onset(o);
     275  return NULL;
     276}
     277
     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)
     284{
     285  uint_t ret = AUBIO_OK;
    212286  /* set some default parameter */
    213287  aubio_onset_set_threshold (o, 0.3);
    214   aubio_onset_set_delay(o, 4.3 * hop_size);
    215   aubio_onset_set_minioi_ms(o, 20.);
    216   aubio_onset_set_silence(o, -70.);
    217 
    218   /* initialize internal variables */
    219   o->last_onset = 0;
    220   o->total_frames = 0;
    221   return o;
    222 
    223 beach_specdesc:
    224   del_aubio_peakpicker(o->pp);
    225   del_aubio_pvoc(o->pv);
    226 beach:
     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.);
     291  // disable spectral whitening
     292  aubio_onset_set_awhitening (o, 0);
     293  // disable logarithmic magnitude
     294  aubio_onset_set_compression (o, 0.);
     295
     296  /* method specific optimisations */
     297  if (strcmp (onset_mode, "energy") == 0) {
     298  } else if (strcmp (onset_mode, "hfc") == 0 || strcmp (onset_mode, "default") == 0) {
     299    aubio_onset_set_threshold (o, 0.058);
     300    aubio_onset_set_compression (o, 1.);
     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);
     305    aubio_onset_set_awhitening(o, 1);
     306    aubio_onset_set_compression (o, 1.);
     307  } else if (strcmp (onset_mode, "phase") == 0) {
     308    o->apply_compression = 0;
     309    aubio_onset_set_awhitening (o, 0);
     310  } else if (strcmp (onset_mode, "wphase") == 0) {
     311    // use defaults for now
     312  } else if (strcmp (onset_mode, "mkl") == 0) {
     313    aubio_onset_set_threshold (o, 0.05);
     314    aubio_onset_set_awhitening(o, 1);
     315    aubio_onset_set_compression (o, 0.02);
     316  } else if (strcmp (onset_mode, "kl") == 0) {
     317    aubio_onset_set_threshold (o, 0.35);
     318    aubio_onset_set_awhitening(o, 1);
     319    aubio_onset_set_compression (o, 0.02);
     320  } else if (strcmp (onset_mode, "specflux") == 0) {
     321    aubio_onset_set_threshold (o, 0.18);
     322    aubio_onset_set_awhitening(o, 1);
     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.);
     326  } else if (strcmp (onset_mode, "specdiff") == 0) {
     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.);
     332  } else {
     333    AUBIO_WRN("onset: unknown spectral descriptor type %s, "
     334               "using default parameters.\n", onset_mode);
     335    ret = AUBIO_FAIL;
     336  }
     337  return ret;
     338}
     339
     340void del_aubio_onset (aubio_onset_t *o)
     341{
     342  if (o->spectral_whitening)
     343    del_aubio_spectral_whitening(o->spectral_whitening);
     344  if (o->od)
     345    del_aubio_specdesc(o->od);
     346  if (o->pp)
     347    del_aubio_peakpicker(o->pp);
     348  if (o->pv)
     349    del_aubio_pvoc(o->pv);
     350  if (o->desc)
     351    del_fvec(o->desc);
     352  if (o->fftgrain)
     353    del_cvec(o->fftgrain);
    227354  AUBIO_FREE(o);
    228   return NULL;
    229 }
    230 
    231 void del_aubio_onset (aubio_onset_t *o)
    232 {
    233   del_aubio_specdesc(o->od);
    234   del_aubio_peakpicker(o->pp);
    235   del_aubio_pvoc(o->pv);
    236   del_fvec(o->desc);
    237   del_cvec(o->fftgrain);
    238   AUBIO_FREE(o);
    239 }
     355}
Note: See TracChangeset for help on using the changeset viewer.