Changeset b130600 for src/pitch


Ignore:
Timestamp:
Mar 12, 2013, 4:44:33 AM (12 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
20b1aed
Parents:
426e6f7
Message:

src/pitch/pitch.c: clarify, use one void pointer for all algorithms

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/pitch/pitch.c

    r426e6f7 rb130600  
    5757} aubio_pitch_mode;
    5858
    59 typedef void (*aubio_pitch_func_t)
    60   (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
    61 typedef smpl_t (*aubio_pitch_conv_t)
    62   (smpl_t value, uint_t srate, uint_t bufsize);
    63 
    64 typedef smpl_t (*aubio_conf_cb_t) (void * p);
    65 
    66 void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf);
    67 
     59/** callback to get pitch candidate, defined below */
     60typedef void (*aubio_pitch_detect_t) (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
     61
     62/** callback to convert pitch from one unit to another, defined below */
     63typedef smpl_t(*aubio_pitch_convert_t) (smpl_t value, uint_t samplerate, uint_t bufsize);
     64
     65/** callback to fetch the confidence of the algorithm */
     66typedef smpl_t (*aubio_pitch_get_conf_t) (void * p);
     67
     68/** generic pitch detection structure */
     69struct _aubio_pitch_t
     70{
     71  aubio_pitch_type type;          /**< pitch detection mode */
     72  aubio_pitch_mode mode;          /**< pitch detection output mode */
     73  uint_t samplerate;              /**< samplerate */
     74  uint_t bufsize;                 /**< buffer size */
     75  void *p_object;                 /**< pointer to pitch object */
     76  aubio_filter_t *filter;         /**< filter */
     77  aubio_pvoc_t *pv;               /**< phase vocoder for mcomb */
     78  cvec_t *fftgrain;               /**< spectral frame for mcomb */
     79  fvec_t *buf;                    /**< temporary buffer for yin */
     80  aubio_pitch_detect_t detect_cb; /**< callback to get the pitch candidates */
     81  aubio_pitch_convert_t conv_cb;  /**< callback to convert it to the desired unit */
     82  aubio_pitch_get_conf_t conf_cb; /**< pointer to the current confidence callback */
     83};
     84
     85/* callback functions for pitch detection */
    6886static void aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
    6987static void aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
     
    7290static void aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
    7391
    74 /** generic pitch detection structure */
    75 struct _aubio_pitch_t
    76 {
    77   aubio_pitch_type type; /**< pitch detection mode */
    78   aubio_pitch_mode mode; /**< pitch detection output mode */
    79   uint_t srate;                   /**< samplerate */
    80   uint_t bufsize;                 /**< buffer size */
    81   aubio_pitchmcomb_t *mcomb;      /**< mcomb object */
    82   aubio_pitchfcomb_t *fcomb;      /**< fcomb object */
    83   aubio_pitchschmitt_t *schmitt;  /**< schmitt object */
    84   aubio_pitchyinfft_t *yinfft;    /**< yinfft object */
    85   aubio_pitchyin_t *yin;    /**< yinfft object */
    86   void *pitch;
    87   aubio_filter_t *filter;         /**< filter */
    88   aubio_pvoc_t *pv;               /**< phase vocoder for mcomb */
    89   cvec_t *fftgrain;               /**< spectral frame for mcomb */
    90   fvec_t *buf;                    /**< temporary buffer for yin */
    91   aubio_pitch_func_t callback; /**< pointer to current pitch detection method */
    92   aubio_pitch_conv_t freqconv; /**< pointer to current pitch conversion method */
    93   aubio_conf_cb_t confidence_callback; /**< pointer to the current confidence callback */
    94 };
    95 
    96 /* convenience wrapper function for frequency unit conversions
    97  * should probably be rewritten with #defines */
    98 smpl_t freqconvbin (smpl_t f, uint_t srate, uint_t bufsize);
    99 smpl_t
    100 freqconvbin (smpl_t f, uint_t srate, uint_t bufsize)
    101 {
    102   return aubio_freqtobin (f, srate, bufsize);
    103 }
    104 
    105 smpl_t freqconvmidi (smpl_t f, uint_t srate, uint_t bufsize);
    106 smpl_t
    107 freqconvmidi (smpl_t f, uint_t srate UNUSED, uint_t bufsize UNUSED)
    108 {
    109   return aubio_freqtomidi (f);
    110 }
    111 
    112 smpl_t freqconvpass (smpl_t f, uint_t srate, uint_t bufsize);
    113 smpl_t
    114 freqconvpass (smpl_t f, uint_t srate UNUSED, uint_t bufsize UNUSED)
    115 {
    116   return f;
    117 }
     92/* conversion functions for frequency conversions */
     93smpl_t freqconvbin (smpl_t f, uint_t samplerate, uint_t bufsize);
     94smpl_t freqconvmidi (smpl_t f, uint_t samplerate, uint_t bufsize);
     95smpl_t freqconvpass (smpl_t f, uint_t samplerate, uint_t bufsize);
     96
     97/* adapter to stack ibuf new samples at the end of buf, and trim `buf` to `bufsize` */
     98void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf);
     99
    118100
    119101aubio_pitch_t *
     
    140122    pitch_type = aubio_pitcht_default;
    141123  }
    142   p->srate = samplerate;
     124  p->samplerate = samplerate;
    143125  p->type = pitch_type;
    144126  aubio_pitch_set_unit (p, "default");
    145127  p->bufsize = bufsize;
    146   p->confidence_callback = NULL;
     128  p->conf_cb = NULL;
    147129  switch (p->type) {
    148130    case aubio_pitcht_yin:
    149131      p->buf = new_fvec (bufsize);
    150       p->yin = new_aubio_pitchyin (bufsize);
    151       p->callback = aubio_pitch_do_yin;
    152       p->confidence_callback = (aubio_conf_cb_t)aubio_pitchyin_get_confidence;
    153       p->pitch = (void*)p->yin;
    154       aubio_pitchyin_set_tolerance (p->yin, 0.15);
     132      p->p_object = new_aubio_pitchyin (bufsize);
     133      p->detect_cb = aubio_pitch_do_yin;
     134      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyin_get_confidence;
     135      aubio_pitchyin_set_tolerance (p->p_object, 0.15);
    155136      break;
    156137    case aubio_pitcht_mcomb:
    157138      p->pv = new_aubio_pvoc (bufsize, hopsize);
    158139      p->fftgrain = new_cvec (bufsize);
    159       p->mcomb = new_aubio_pitchmcomb (bufsize, hopsize);
     140      p->p_object = new_aubio_pitchmcomb (bufsize, hopsize);
    160141      p->filter = new_aubio_filter_c_weighting (samplerate);
    161       p->callback = aubio_pitch_do_mcomb;
     142      p->detect_cb = aubio_pitch_do_mcomb;
    162143      break;
    163144    case aubio_pitcht_fcomb:
    164145      p->buf = new_fvec (bufsize);
    165       p->fcomb = new_aubio_pitchfcomb (bufsize, hopsize);
    166       p->callback = aubio_pitch_do_fcomb;
     146      p->p_object = new_aubio_pitchfcomb (bufsize, hopsize);
     147      p->detect_cb = aubio_pitch_do_fcomb;
    167148      break;
    168149    case aubio_pitcht_schmitt:
    169150      p->buf = new_fvec (bufsize);
    170       p->schmitt = new_aubio_pitchschmitt (bufsize);
    171       p->callback = aubio_pitch_do_schmitt;
     151      p->p_object = new_aubio_pitchschmitt (bufsize);
     152      p->detect_cb = aubio_pitch_do_schmitt;
    172153      break;
    173154    case aubio_pitcht_yinfft:
    174155      p->buf = new_fvec (bufsize);
    175       p->yinfft = new_aubio_pitchyinfft (bufsize);
    176       p->callback = aubio_pitch_do_yinfft;
    177       p->confidence_callback = (aubio_conf_cb_t)aubio_pitchyinfft_get_confidence;
    178       p->pitch = (void*)p->yin;
    179       aubio_pitchyinfft_set_tolerance (p->yinfft, 0.85);
     156      p->p_object = new_aubio_pitchyinfft (bufsize);
     157      p->detect_cb = aubio_pitch_do_yinfft;
     158      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyinfft_get_confidence;
     159      aubio_pitchyinfft_set_tolerance (p->p_object, 0.85);
    180160      break;
    181161    default:
     
    191171    case aubio_pitcht_yin:
    192172      del_fvec (p->buf);
    193       del_aubio_pitchyin (p->yin);
     173      del_aubio_pitchyin (p->p_object);
    194174      break;
    195175    case aubio_pitcht_mcomb:
     
    197177      del_cvec (p->fftgrain);
    198178      del_aubio_filter (p->filter);
    199       del_aubio_pitchmcomb (p->mcomb);
     179      del_aubio_pitchmcomb (p->p_object);
    200180      break;
    201181    case aubio_pitcht_schmitt:
    202182      del_fvec (p->buf);
    203       del_aubio_pitchschmitt (p->schmitt);
     183      del_aubio_pitchschmitt (p->p_object);
    204184      break;
    205185    case aubio_pitcht_fcomb:
    206186      del_fvec (p->buf);
    207       del_aubio_pitchfcomb (p->fcomb);
     187      del_aubio_pitchfcomb (p->p_object);
    208188      break;
    209189    case aubio_pitcht_yinfft:
    210190      del_fvec (p->buf);
    211       del_aubio_pitchyinfft (p->yinfft);
     191      del_aubio_pitchyinfft (p->p_object);
    212192      break;
    213193    default:
     
    251231  switch (p->mode) {
    252232    case aubio_pitchm_freq:
    253       p->freqconv = freqconvpass;
     233      p->conv_cb = freqconvpass;
    254234      break;
    255235    case aubio_pitchm_midi:
    256       p->freqconv = freqconvmidi;
     236      p->conv_cb = freqconvmidi;
    257237      break;
    258238    case aubio_pitchm_cent:
    259239      /* bug: not implemented */
    260       p->freqconv = freqconvmidi;
     240      p->conv_cb = freqconvmidi;
    261241      break;
    262242    case aubio_pitchm_bin:
    263       p->freqconv = freqconvbin;
     243      p->conv_cb = freqconvbin;
    264244      break;
    265245    default:
     
    274254  switch (p->type) {
    275255    case aubio_pitcht_yin:
    276       aubio_pitchyin_set_tolerance (p->yin, tol);
     256      aubio_pitchyin_set_tolerance (p->p_object, tol);
    277257      break;
    278258    case aubio_pitcht_yinfft:
    279       aubio_pitchyinfft_set_tolerance (p->yinfft, tol);
     259      aubio_pitchyinfft_set_tolerance (p->p_object, tol);
    280260      break;
    281261    default:
     
    285265}
    286266
     267
     268/* do method, calling the detection callback, then the conversion callback */
    287269void
    288270aubio_pitch_do (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
    289271{
    290   p->callback (p, ibuf, obuf);
    291   obuf->data[0] = p->freqconv (obuf->data[0], p->srate, p->bufsize);
    292 }
    293 
     272  p->detect_cb (p, ibuf, obuf);
     273  obuf->data[0] = p->conv_cb (obuf->data[0], p->samplerate, p->bufsize);
     274}
     275
     276/* do method for each algorithm */
    294277void
    295278aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
     
    297280  aubio_filter_do (p->filter, ibuf);
    298281  aubio_pvoc_do (p->pv, ibuf, p->fftgrain);
    299   aubio_pitchmcomb_do (p->mcomb, p->fftgrain, obuf);
    300   obuf->data[0] = aubio_bintofreq (obuf->data[0], p->srate, p->bufsize);
     282  aubio_pitchmcomb_do (p->p_object, p->fftgrain, obuf);
     283  obuf->data[0] = aubio_bintofreq (obuf->data[0], p->samplerate, p->bufsize);
    301284}
    302285
     
    306289  smpl_t pitch = 0.;
    307290  aubio_pitch_slideblock (p, ibuf);
    308   aubio_pitchyin_do (p->yin, p->buf, obuf);
     291  aubio_pitchyin_do (p->p_object, p->buf, obuf);
    309292  pitch = obuf->data[0];
    310293  if (pitch > 0) {
    311     pitch = p->srate / (pitch + 0.);
     294    pitch = p->samplerate / (pitch + 0.);
    312295  } else {
    313296    pitch = 0.;
     
    322305  smpl_t pitch = 0.;
    323306  aubio_pitch_slideblock (p, ibuf);
    324   aubio_pitchyinfft_do (p->yinfft, p->buf, obuf);
     307  aubio_pitchyinfft_do (p->p_object, p->buf, obuf);
    325308  pitch = obuf->data[0];
    326309  if (pitch > 0) {
    327     pitch = p->srate / (pitch + 0.);
     310    pitch = p->samplerate / (pitch + 0.);
    328311  } else {
    329312    pitch = 0.;
     
    336319{
    337320  aubio_pitch_slideblock (p, ibuf);
    338   aubio_pitchfcomb_do (p->fcomb, p->buf, out);
    339   out->data[0] = aubio_bintofreq (out->data[0], p->srate, p->bufsize);
     321  aubio_pitchfcomb_do (p->p_object, p->buf, out);
     322  out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
    340323}
    341324
     
    345328  smpl_t period, pitch = 0.;
    346329  aubio_pitch_slideblock (p, ibuf);
    347   aubio_pitchschmitt_do (p->schmitt, p->buf, out);
     330  aubio_pitchschmitt_do (p->p_object, p->buf, out);
    348331  period = out->data[0];
    349332  if (period > 0) {
    350     pitch = p->srate / period;
     333    pitch = p->samplerate / period;
    351334  } else {
    352335    pitch = 0.;
    353336  }
    354337  out->data[0] = pitch;
     338}
     339
     340/* conversion callbacks */
     341smpl_t
     342freqconvbin(smpl_t f, uint_t samplerate, uint_t bufsize)
     343{
     344  return aubio_freqtobin(f, samplerate, bufsize);
     345}
     346
     347smpl_t
     348freqconvmidi (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
     349{
     350  return aubio_freqtomidi (f);
     351}
     352
     353smpl_t
     354freqconvpass (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
     355{
     356  return f;
    355357}
    356358
     
    359361aubio_pitch_get_confidence (aubio_pitch_t * p)
    360362{
    361   if (p->confidence_callback) {
    362     return p->confidence_callback ((void*)(p->pitch));
     363  if (p->conf_cb) {
     364    return p->conf_cb(p->p_object);
    363365  }
    364366  return 0.;
Note: See TracChangeset for help on using the changeset viewer.