Changeset fddfa64 for src


Ignore:
Timestamp:
Nov 3, 2009, 4:14:03 PM (15 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:
bafe71d
Parents:
63f3c70
Message:

src/pitch/: indent

Location:
src/pitch
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • src/pitch/pitch.c

    r63f3c70 rfddfa64  
    3636
    3737/** pitch detection algorithm */
    38 typedef enum {
     38typedef enum
     39{
    3940  aubio_pitcht_yin,     /**< YIN algorithm */
    4041  aubio_pitcht_mcomb,   /**< Multi-comb filter */
     
    4647
    4748/** pitch detection output mode */
    48 typedef enum {
     49typedef enum
     50{
    4951  aubio_pitchm_freq,   /**< Frequency (Hz) */
    5052  aubio_pitchm_midi,   /**< MIDI note (0.,127) */
     
    5557
    5658typedef void (*aubio_pitch_func_t)
    57   (aubio_pitch_t *p, fvec_t * ibuf, fvec_t *obuf);
     59  (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
    5860typedef smpl_t (*aubio_pitch_conv_t)
    5961  (smpl_t value, uint_t srate, uint_t bufsize);
    6062
    61 void aubio_pitch_slideblock(aubio_pitch_t *p, fvec_t *ibuf);
    62 
    63 void aubio_pitch_do_mcomb   (aubio_pitch_t *p, fvec_t *ibuf, fvec_t *obuf);
    64 void aubio_pitch_do_yin     (aubio_pitch_t *p, fvec_t *ibuf, fvec_t *obuf);
    65 void aubio_pitch_do_schmitt (aubio_pitch_t *p, fvec_t *ibuf, fvec_t *obuf);
    66 void aubio_pitch_do_fcomb   (aubio_pitch_t *p, fvec_t *ibuf, fvec_t *obuf);
    67 void aubio_pitch_do_yinfft  (aubio_pitch_t *p, fvec_t *ibuf, fvec_t *obuf);
     63void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf);
     64
     65void aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
     66void aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
     67void aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
     68void aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
     69void aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
    6870
    6971/** generic pitch detection structure */
    70 struct _aubio_pitch_t {
     72struct _aubio_pitch_t
     73{
    7174  aubio_pitch_type type; /**< pitch detection mode */
    7275  aubio_pitch_mode mode; /**< pitch detection output mode */
    7376  uint_t srate;                   /**< samplerate */
    7477  uint_t bufsize;                 /**< buffer size */
    75   aubio_pitchmcomb_t * mcomb;     /**< mcomb object */
    76   aubio_pitchfcomb_t * fcomb;     /**< fcomb object */
    77   aubio_pitchschmitt_t * schmitt; /**< schmitt object */
    78   aubio_pitchyinfft_t * yinfft;   /**< yinfft object */
    79   aubio_pitchyin_t * yin;   /**< yinfft object */
    80   aubio_filter_t * filter;        /**< filter */
    81   aubio_pvoc_t * pv;              /**< phase vocoder for mcomb */
    82   cvec_t * fftgrain;              /**< spectral frame for mcomb */
    83   fvec_t * buf;                   /**< temporary buffer for yin */
     78  aubio_pitchmcomb_t *mcomb;      /**< mcomb object */
     79  aubio_pitchfcomb_t *fcomb;      /**< fcomb object */
     80  aubio_pitchschmitt_t *schmitt; /**< schmitt object */
     81  aubio_pitchyinfft_t *yinfft;    /**< yinfft object */
     82  aubio_pitchyin_t *yin;    /**< yinfft object */
     83  aubio_filter_t *filter;         /**< filter */
     84  aubio_pvoc_t *pv;               /**< phase vocoder for mcomb */
     85  cvec_t *fftgrain;               /**< spectral frame for mcomb */
     86  fvec_t *buf;                    /**< temporary buffer for yin */
    8487  aubio_pitch_func_t callback; /**< pointer to current pitch detection method */
    85   aubio_pitch_conv_t freqconv; /**< pointer to current pitch conversion method */ 
     88  aubio_pitch_conv_t freqconv; /**< pointer to current pitch conversion method */
    8689};
    8790
    8891/* convenience wrapper function for frequency unit conversions
    8992 * should probably be rewritten with #defines */
    90 smpl_t freqconvbin(smpl_t f,uint_t srate,uint_t bufsize);
    91 smpl_t freqconvbin(smpl_t f,uint_t srate,uint_t bufsize){
    92   return aubio_freqtobin(f,srate,bufsize);
    93 }
    94 
    95 smpl_t freqconvmidi(smpl_t f,uint_t srate,uint_t bufsize);
    96 smpl_t freqconvmidi(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){
    97   return aubio_freqtomidi(f);
    98 }
    99 
    100 smpl_t freqconvpass(smpl_t f,uint_t srate,uint_t bufsize);
    101 smpl_t freqconvpass(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){
     93smpl_t freqconvbin (smpl_t f, uint_t srate, uint_t bufsize);
     94smpl_t
     95freqconvbin (smpl_t f, uint_t srate, uint_t bufsize)
     96{
     97  return aubio_freqtobin (f, srate, bufsize);
     98}
     99
     100smpl_t freqconvmidi (smpl_t f, uint_t srate, uint_t bufsize);
     101smpl_t
     102freqconvmidi (smpl_t f, uint_t srate UNUSED, uint_t bufsize UNUSED)
     103{
     104  return aubio_freqtomidi (f);
     105}
     106
     107smpl_t freqconvpass (smpl_t f, uint_t srate, uint_t bufsize);
     108smpl_t
     109freqconvpass (smpl_t f, uint_t srate UNUSED, uint_t bufsize UNUSED)
     110{
    102111  return f;
    103112}
     
    107116    uint_t bufsize, uint_t hopsize, uint_t channels, uint_t samplerate)
    108117{
    109   aubio_pitch_t *p = AUBIO_NEW(aubio_pitch_t);
     118  aubio_pitch_t *p = AUBIO_NEW (aubio_pitch_t);
    110119  aubio_pitch_type pitch_type;
    111120  if (strcmp (pitch_mode, "mcomb") == 0)
    112       pitch_type = aubio_pitcht_mcomb;
     121    pitch_type = aubio_pitcht_mcomb;
    113122  else if (strcmp (pitch_mode, "yinfft") == 0)
    114       pitch_type = aubio_pitcht_yin;
     123    pitch_type = aubio_pitcht_yin;
    115124  else if (strcmp (pitch_mode, "yin") == 0)
    116       pitch_type = aubio_pitcht_yin;
     125    pitch_type = aubio_pitcht_yin;
    117126  else if (strcmp (pitch_mode, "schmitt") == 0)
    118       pitch_type = aubio_pitcht_schmitt;
     127    pitch_type = aubio_pitcht_schmitt;
    119128  else if (strcmp (pitch_mode, "fcomb") == 0)
    120       pitch_type = aubio_pitcht_fcomb;
     129    pitch_type = aubio_pitcht_fcomb;
    121130  else if (strcmp (pitch_mode, "default") == 0)
    122       pitch_type = aubio_pitcht_default;
     131    pitch_type = aubio_pitcht_default;
    123132  else {
    124       AUBIO_ERR ("unknown pitch detection method %s, using default.\n", pitch_mode);
    125       pitch_type = aubio_pitcht_default;
    126       return NULL;
     133    AUBIO_ERR ("unknown pitch detection method %s, using default.\n",
     134        pitch_mode);
     135    pitch_type = aubio_pitcht_default;
     136    return NULL;
    127137  }
    128138  p->srate = samplerate;
     
    130140  aubio_pitch_set_unit (p, "default");
    131141  p->bufsize = bufsize;
    132   switch(p->type) {
     142  switch (p->type) {
    133143    case aubio_pitcht_yin:
    134       p->buf      = new_fvec(bufsize,channels);
    135       p->yin      = new_aubio_pitchyin(bufsize);
     144      p->buf = new_fvec (bufsize, channels);
     145      p->yin = new_aubio_pitchyin (bufsize);
    136146      p->callback = aubio_pitch_do_yin;
    137147      aubio_pitchyin_set_tolerance (p->yin, 0.15);
    138148      break;
    139149    case aubio_pitcht_mcomb:
    140       p->pv       = new_aubio_pvoc(bufsize, hopsize, channels);
    141       p->fftgrain = new_cvec(bufsize, channels);
    142       p->mcomb    = new_aubio_pitchmcomb(bufsize,hopsize,channels);
    143       p->filter   = new_aubio_filter_c_weighting (samplerate, channels);
     150      p->pv = new_aubio_pvoc (bufsize, hopsize, channels);
     151      p->fftgrain = new_cvec (bufsize, channels);
     152      p->mcomb = new_aubio_pitchmcomb (bufsize, hopsize, channels);
     153      p->filter = new_aubio_filter_c_weighting (samplerate, channels);
    144154      p->callback = aubio_pitch_do_mcomb;
    145155      break;
    146156    case aubio_pitcht_fcomb:
    147       p->buf      = new_fvec(bufsize,channels);
    148       p->fcomb    = new_aubio_pitchfcomb(bufsize,hopsize,channels);
     157      p->buf = new_fvec (bufsize, channels);
     158      p->fcomb = new_aubio_pitchfcomb (bufsize, hopsize, channels);
    149159      p->callback = aubio_pitch_do_fcomb;
    150160      break;
    151161    case aubio_pitcht_schmitt:
    152       p->buf      = new_fvec(bufsize,channels);
    153       p->schmitt  = new_aubio_pitchschmitt(bufsize);
     162      p->buf = new_fvec (bufsize, channels);
     163      p->schmitt = new_aubio_pitchschmitt (bufsize);
    154164      p->callback = aubio_pitch_do_schmitt;
    155165      break;
    156166    case aubio_pitcht_yinfft:
    157       p->buf      = new_fvec(bufsize,channels);
    158       p->yinfft   = new_aubio_pitchyinfft(bufsize);
     167      p->buf = new_fvec (bufsize, channels);
     168      p->yinfft = new_aubio_pitchyinfft (bufsize);
    159169      p->callback = aubio_pitch_do_yinfft;
    160170      aubio_pitchyinfft_set_tolerance (p->yinfft, 0.85);
     
    166176}
    167177
    168 void del_aubio_pitch(aubio_pitch_t * p) {
    169   switch(p->type) {
     178void
     179del_aubio_pitch (aubio_pitch_t * p)
     180{
     181  switch (p->type) {
    170182    case aubio_pitcht_yin:
    171       del_fvec(p->buf);
    172       del_aubio_pitchyin(p->yin);
     183      del_fvec (p->buf);
     184      del_aubio_pitchyin (p->yin);
    173185      break;
    174186    case aubio_pitcht_mcomb:
    175       del_aubio_pvoc(p->pv);
    176       del_cvec(p->fftgrain);
    177       del_aubio_filter(p->filter);
    178       del_aubio_pitchmcomb(p->mcomb);
     187      del_aubio_pvoc (p->pv);
     188      del_cvec (p->fftgrain);
     189      del_aubio_filter (p->filter);
     190      del_aubio_pitchmcomb (p->mcomb);
    179191      break;
    180192    case aubio_pitcht_schmitt:
    181       del_fvec(p->buf);
    182       del_aubio_pitchschmitt(p->schmitt);
     193      del_fvec (p->buf);
     194      del_aubio_pitchschmitt (p->schmitt);
    183195      break;
    184196    case aubio_pitcht_fcomb:
    185       del_fvec(p->buf);
    186       del_aubio_pitchfcomb(p->fcomb);
     197      del_fvec (p->buf);
     198      del_aubio_pitchfcomb (p->fcomb);
    187199      break;
    188200    case aubio_pitcht_yinfft:
    189       del_fvec(p->buf);
    190       del_aubio_pitchyinfft(p->yinfft);
     201      del_fvec (p->buf);
     202      del_aubio_pitchyinfft (p->yinfft);
    191203      break;
    192204    default:
    193205      break;
    194206  }
    195   AUBIO_FREE(p);
    196 }
    197 
    198 void aubio_pitch_slideblock(aubio_pitch_t *p, fvec_t *ibuf){
    199   uint_t i,j = 0, overlap_size = 0;
    200   overlap_size = p->buf->length-ibuf->length;
    201   for (i=0;i<p->buf->channels;i++){
    202     for (j=0;j<overlap_size;j++){
    203       p->buf->data[i][j] = p->buf->data[i][j+ibuf->length];
    204     }
    205   }
    206   for (i=0;i<ibuf->channels;i++){
    207     for (j=0;j<ibuf->length;j++){
    208       p->buf->data[i][j+overlap_size] = ibuf->data[i][j];
    209     }
    210   }
    211 }
    212 
    213 uint_t aubio_pitch_set_unit (aubio_pitch_t *p, char_t * pitch_unit) {
     207  AUBIO_FREE (p);
     208}
     209
     210void
     211aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf)
     212{
     213  uint_t i, j = 0, overlap_size = 0;
     214  overlap_size = p->buf->length - ibuf->length;
     215  for (i = 0; i < p->buf->channels; i++) {
     216    for (j = 0; j < overlap_size; j++) {
     217      p->buf->data[i][j] = p->buf->data[i][j + ibuf->length];
     218    }
     219  }
     220  for (i = 0; i < ibuf->channels; i++) {
     221    for (j = 0; j < ibuf->length; j++) {
     222      p->buf->data[i][j + overlap_size] = ibuf->data[i][j];
     223    }
     224  }
     225}
     226
     227uint_t
     228aubio_pitch_set_unit (aubio_pitch_t * p, char_t * pitch_unit)
     229{
    214230  aubio_pitch_mode pitch_mode;
    215231  if (strcmp (pitch_unit, "freq") == 0)
    216       pitch_mode = aubio_pitchm_freq;
     232    pitch_mode = aubio_pitchm_freq;
    217233  else if (strcmp (pitch_unit, "midi") == 0)
    218       pitch_mode = aubio_pitchm_midi;
     234    pitch_mode = aubio_pitchm_midi;
    219235  else if (strcmp (pitch_unit, "cent") == 0)
    220       pitch_mode = aubio_pitchm_cent;
     236    pitch_mode = aubio_pitchm_cent;
    221237  else if (strcmp (pitch_unit, "bin") == 0)
    222       pitch_mode = aubio_pitchm_bin;
     238    pitch_mode = aubio_pitchm_bin;
    223239  else if (strcmp (pitch_unit, "default") == 0)
    224       pitch_mode = aubio_pitchm_default;
     240    pitch_mode = aubio_pitchm_default;
    225241  else {
    226       AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit);
    227       pitch_mode = aubio_pitchm_default;
     242    AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit);
     243    pitch_mode = aubio_pitchm_default;
    228244  }
    229245  p->mode = pitch_mode;
    230   switch(p->mode) {
     246  switch (p->mode) {
    231247    case aubio_pitchm_freq:
    232248      p->freqconv = freqconvpass;
     
    248264}
    249265
    250 uint_t aubio_pitch_set_tolerance(aubio_pitch_t *p, smpl_t tol) {
    251   switch(p->type) {
     266uint_t
     267aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol)
     268{
     269  switch (p->type) {
    252270    case aubio_pitcht_yin:
    253271      aubio_pitchyin_set_tolerance (p->yin, tol);
     
    262280}
    263281
    264 void aubio_pitch_do (aubio_pitch_t *p, fvec_t * ibuf, fvec_t *obuf) {
    265   uint_t i;
    266   p->callback(p, ibuf, obuf);
     282void
     283aubio_pitch_do (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
     284{
     285  uint_t i;
     286  p->callback (p, ibuf, obuf);
    267287  for (i = 0; i < obuf->channels; i++) {
    268     p->freqconv(obuf->data[i][0],p->srate,p->bufsize);
    269   }
    270 }
    271 
    272 void aubio_pitch_do_mcomb(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * obuf) {
    273   uint_t i;
    274   aubio_filter_do(p->filter,ibuf);
    275   aubio_pvoc_do(p->pv,ibuf,p->fftgrain);
    276   aubio_pitchmcomb_do(p->mcomb,p->fftgrain, obuf);
     288    p->freqconv (obuf->data[i][0], p->srate, p->bufsize);
     289  }
     290}
     291
     292void
     293aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
     294{
     295  uint_t i;
     296  aubio_filter_do (p->filter, ibuf);
     297  aubio_pvoc_do (p->pv, ibuf, p->fftgrain);
     298  aubio_pitchmcomb_do (p->mcomb, p->fftgrain, obuf);
    277299  for (i = 0; i < obuf->channels; i++) {
    278300    obuf->data[i][0] = aubio_bintofreq (obuf->data[i][0], p->srate, p->bufsize);
     
    280302}
    281303
    282 void aubio_pitch_do_yin(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * obuf) {
     304void
     305aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
     306{
    283307  smpl_t pitch = 0.;
    284308  uint_t i;
    285   aubio_pitch_slideblock(p,ibuf);
    286   aubio_pitchyin_do(p->yin,p->buf, obuf);
     309  aubio_pitch_slideblock (p, ibuf);
     310  aubio_pitchyin_do (p->yin, p->buf, obuf);
    287311  for (i = 0; i < obuf->channels; i++) {
    288312    pitch = obuf->data[i][0];
    289     if (pitch>0) {
    290       pitch = p->srate/(pitch+0.);
     313    if (pitch > 0) {
     314      pitch = p->srate / (pitch + 0.);
    291315    } else {
    292316      pitch = 0.;
     
    297321
    298322
    299 void aubio_pitch_do_yinfft(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * obuf){
     323void
     324aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
     325{
    300326  smpl_t pitch = 0.;
    301327  uint_t i;
    302   aubio_pitch_slideblock(p,ibuf);
    303   aubio_pitchyinfft_do(p->yinfft,p->buf,obuf);
     328  aubio_pitch_slideblock (p, ibuf);
     329  aubio_pitchyinfft_do (p->yinfft, p->buf, obuf);
    304330  for (i = 0; i < obuf->channels; i++) {
    305331    pitch = obuf->data[i][0];
    306     if (pitch>0) {
    307       pitch = p->srate/(pitch+0.);
     332    if (pitch > 0) {
     333      pitch = p->srate / (pitch + 0.);
    308334    } else {
    309335      pitch = 0.;
     
    313339}
    314340
    315 void aubio_pitch_do_fcomb(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * out){
    316   uint_t i;
    317   aubio_pitch_slideblock(p,ibuf);
    318   aubio_pitchfcomb_do(p->fcomb,p->buf, out);
     341void
     342aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
     343{
     344  uint_t i;
     345  aubio_pitch_slideblock (p, ibuf);
     346  aubio_pitchfcomb_do (p->fcomb, p->buf, out);
    319347  for (i = 0; i < out->channels; i++) {
    320348    out->data[i][0] = aubio_bintofreq (out->data[i][0], p->srate, p->bufsize);
     
    322350}
    323351
    324 void aubio_pitch_do_schmitt(aubio_pitch_t *p, fvec_t *ibuf, fvec_t *out){
     352void
     353aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
     354{
    325355  smpl_t period, pitch = 0.;
    326356  uint_t i;
    327   aubio_pitch_slideblock(p,ibuf);
    328   aubio_pitchschmitt_do(p->schmitt,p->buf, out);
     357  aubio_pitch_slideblock (p, ibuf);
     358  aubio_pitchschmitt_do (p->schmitt, p->buf, out);
    329359  for (i = 0; i < out->channels; i++) {
    330360    period = out->data[i][0];
    331     if (period>0) {
    332       pitch = p->srate/period;
     361    if (period > 0) {
     362      pitch = p->srate / period;
    333363    } else {
    334364      pitch = 0.;
  • src/pitch/pitch.h

    r63f3c70 rfddfa64  
    4545
    4646*/
    47 void aubio_pitch_do (aubio_pitch_t * o, fvec_t * in,
    48     fvec_t * out);
     47void aubio_pitch_do (aubio_pitch_t * o, fvec_t * in, fvec_t * out);
    4948
    5049/** change yin or yinfft tolerance threshold
     
    5453
    5554*/
    56 uint_t aubio_pitch_set_tolerance (aubio_pitch_t * o,
    57     smpl_t tol);
     55uint_t aubio_pitch_set_tolerance (aubio_pitch_t * o, smpl_t tol);
    5856
    5957/** deletion of the pitch detection object
     
    7371
    7472*/
    75 aubio_pitch_t * new_aubio_pitch (char_t * mode,
     73aubio_pitch_t *new_aubio_pitch (char_t * mode,
    7674    uint_t bufsize, uint_t hopsize, uint_t channels, uint_t samplerate);
    7775
     
    8280
    8381*/
    84 uint_t aubio_pitch_set_unit (aubio_pitch_t * o,
    85     char_t * mode);
     82uint_t aubio_pitch_set_unit (aubio_pitch_t * o, char_t * mode);
    8683
    8784#ifdef __cplusplus
  • src/pitch/pitchfcomb.c

    r63f3c70 rfddfa64  
    3030#define MAX_PEAKS 8
    3131
    32 typedef struct {
     32typedef struct
     33{
    3334  smpl_t bin;
    3435  smpl_t db;
    3536} aubio_fpeak_t;
    3637
    37 struct _aubio_pitchfcomb_t {
     38struct _aubio_pitchfcomb_t
     39{
    3840  uint_t fftSize;
    3941  uint_t stepSize;
    4042  uint_t rate;
    41   fvec_t * winput;
    42   fvec_t * win;
    43   cvec_t * fftOut;
    44   fvec_t * fftLastPhase;
    45   aubio_fft_t * fft;
     43  fvec_t *winput;
     44  fvec_t *win;
     45  cvec_t *fftOut;
     46  fvec_t *fftLastPhase;
     47  aubio_fft_t *fft;
    4648};
    4749
    48 aubio_pitchfcomb_t * new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize, uint_t channels)
     50aubio_pitchfcomb_t *
     51new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize, uint_t channels)
    4952{
    50   aubio_pitchfcomb_t * p = AUBIO_NEW(aubio_pitchfcomb_t);
    51   p->fftSize      = bufsize;
    52   p->stepSize     = hopsize;
    53   p->winput       = new_fvec(bufsize,1);
    54   p->fftOut       = new_cvec(bufsize,1);
    55   p->fftLastPhase = new_fvec(bufsize, channels);
    56   p->fft = new_aubio_fft(bufsize, 1);
    57   p->win = new_aubio_window("hanning", bufsize);
     53  aubio_pitchfcomb_t *p = AUBIO_NEW (aubio_pitchfcomb_t);
     54  p->fftSize = bufsize;
     55  p->stepSize = hopsize;
     56  p->winput = new_fvec (bufsize, 1);
     57  p->fftOut = new_cvec (bufsize, 1);
     58  p->fftLastPhase = new_fvec (bufsize, channels);
     59  p->fft = new_aubio_fft (bufsize, 1);
     60  p->win = new_aubio_window ("hanning", bufsize);
    5861  return p;
    5962}
    6063
    6164/* input must be stepsize long */
    62 void aubio_pitchfcomb_do (aubio_pitchfcomb_t * p, fvec_t * input, fvec_t * output)
     65void
     66aubio_pitchfcomb_do (aubio_pitchfcomb_t * p, fvec_t * input, fvec_t * output)
    6367{
    6468  uint_t i, k, l, maxharm = 0;
    65   smpl_t phaseDifference = TWO_PI*(smpl_t)p->stepSize/(smpl_t)p->fftSize;
     69  smpl_t phaseDifference = TWO_PI * (smpl_t) p->stepSize / (smpl_t) p->fftSize;
    6670  aubio_fpeak_t peaks[MAX_PEAKS];
    6771
    6872  for (i = 0; i < input->channels; i++) {
    6973
    70   for (k=0; k<MAX_PEAKS; k++) {
    71     peaks[k].db = -200.;
    72     peaks[k].bin = 0.;
    73   }
     74    for (k = 0; k < MAX_PEAKS; k++) {
     75      peaks[k].db = -200.;
     76      peaks[k].bin = 0.;
     77    }
    7478
    75   for (k=0; k < input->length; k++){
    76     p->winput->data[0][k] = p->win->data[0][k] * input->data[i][k];
    77   }
    78   aubio_fft_do(p->fft,p->winput,p->fftOut);
     79    for (k = 0; k < input->length; k++) {
     80      p->winput->data[0][k] = p->win->data[0][k] * input->data[i][k];
     81    }
     82    aubio_fft_do (p->fft, p->winput, p->fftOut);
    7983
    80   for (k=0; k<=p->fftSize/2; k++) {
    81     smpl_t
    82       magnitude = 20.*LOG10(2.*p->fftOut->norm[0][k]/(smpl_t)p->fftSize),
    83       phase     = p->fftOut->phas[0][k],
    84       tmp, bin;
     84    for (k = 0; k <= p->fftSize / 2; k++) {
     85      smpl_t
     86          magnitude =
     87          20. * LOG10 (2. * p->fftOut->norm[0][k] / (smpl_t) p->fftSize),
     88          phase = p->fftOut->phas[0][k], tmp, bin;
    8589
    86     /* compute phase difference */
    87     tmp = phase - p->fftLastPhase->data[i][k];
    88     p->fftLastPhase->data[i][k] = phase;
     90      /* compute phase difference */
     91      tmp = phase - p->fftLastPhase->data[i][k];
     92      p->fftLastPhase->data[i][k] = phase;
    8993
    90     /* subtract expected phase difference */
    91     tmp -= (smpl_t)k*phaseDifference;
     94      /* subtract expected phase difference */
     95      tmp -= (smpl_t) k *phaseDifference;
    9296
    93     /* map delta phase into +/- Pi interval */
    94     tmp = aubio_unwrap2pi(tmp);
     97      /* map delta phase into +/- Pi interval */
     98      tmp = aubio_unwrap2pi (tmp);
    9599
    96     /* get deviation from bin frequency from the +/- Pi interval */
    97     tmp = p->fftSize/(smpl_t)p->stepSize*tmp/(TWO_PI);
     100      /* get deviation from bin frequency from the +/- Pi interval */
     101      tmp = p->fftSize / (smpl_t) p->stepSize * tmp / (TWO_PI);
    98102
    99     /* compute the k-th partials' true bin */
    100     bin = (smpl_t)k + tmp;
     103      /* compute the k-th partials' true bin */
     104      bin = (smpl_t) k + tmp;
    101105
    102     if (bin > 0.0 && magnitude > peaks[0].db) { // && magnitude < 0) {
    103       memmove(peaks+1, peaks, sizeof(aubio_fpeak_t)*(MAX_PEAKS-1));
    104       peaks[0].bin = bin;
    105       peaks[0].db = magnitude;
     106      if (bin > 0.0 && magnitude > peaks[0].db) {       // && magnitude < 0) {
     107        memmove (peaks + 1, peaks, sizeof (aubio_fpeak_t) * (MAX_PEAKS - 1));
     108        peaks[0].bin = bin;
     109        peaks[0].db = magnitude;
     110      }
    106111    }
    107   }
    108112
    109   k = 0;
    110   for (l=1; l<MAX_PEAKS && peaks[l].bin > 0.0; l++) {
    111     sint_t harmonic;
    112     for (harmonic=5; harmonic>1; harmonic--) {
    113       if (peaks[0].bin / peaks[l].bin < harmonic+.02 &&
    114           peaks[0].bin / peaks[l].bin > harmonic-.02) {
    115         if (harmonic > (sint_t)maxharm &&
    116             peaks[0].db < peaks[l].db/2) {
    117           maxharm = harmonic;
    118           k = l;
     113    k = 0;
     114    for (l = 1; l < MAX_PEAKS && peaks[l].bin > 0.0; l++) {
     115      sint_t harmonic;
     116      for (harmonic = 5; harmonic > 1; harmonic--) {
     117        if (peaks[0].bin / peaks[l].bin < harmonic + .02 &&
     118            peaks[0].bin / peaks[l].bin > harmonic - .02) {
     119          if (harmonic > (sint_t) maxharm && peaks[0].db < peaks[l].db / 2) {
     120            maxharm = harmonic;
     121            k = l;
     122          }
    119123        }
    120124      }
    121125    }
    122   }
    123   output->data[i][0] = peaks[k].bin;
    124   /* quick hack to clean output a bit */
    125   if (peaks[k].bin > 5000.) output->data[i][0] = 0.;
     126    output->data[i][0] = peaks[k].bin;
     127    /* quick hack to clean output a bit */
     128    if (peaks[k].bin > 5000.)
     129      output->data[i][0] = 0.;
    126130  }
    127131}
    128132
    129 void del_aubio_pitchfcomb (aubio_pitchfcomb_t * p)
     133void
     134del_aubio_pitchfcomb (aubio_pitchfcomb_t * p)
    130135{
    131   del_cvec(p->fftOut);
    132   del_fvec(p->fftLastPhase);
    133   del_fvec(p->win);
    134   del_fvec(p->winput);
    135   del_aubio_fft(p->fft);
    136   AUBIO_FREE(p);
     136  del_cvec (p->fftOut);
     137  del_fvec (p->fftLastPhase);
     138  del_fvec (p->win);
     139  del_fvec (p->winput);
     140  del_aubio_fft (p->fft);
     141  AUBIO_FREE (p);
    137142}
    138 
  • src/pitch/pitchfcomb.h

    r63f3c70 rfddfa64  
    5050 
    5151*/
    52 void aubio_pitchfcomb_do (aubio_pitchfcomb_t *p, fvec_t * input, fvec_t * output);
     52void aubio_pitchfcomb_do (aubio_pitchfcomb_t * p, fvec_t * input,
     53    fvec_t * output);
     54
    5355/** creation of the pitch detection object
    5456 
     
    5860 
    5961*/
    60 aubio_pitchfcomb_t * new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize, uint_t channels);
     62aubio_pitchfcomb_t *new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize,
     63    uint_t channels);
     64
    6165/** deletion of the pitch detection object
    6266 
     
    6468 
    6569*/
    66 void del_aubio_pitchfcomb (aubio_pitchfcomb_t *p);
    67 
     70void del_aubio_pitchfcomb (aubio_pitchfcomb_t * p);
    6871
    6972#ifdef __cplusplus
  • src/pitch/pitchmcomb.c

    r63f3c70 rfddfa64  
    2929typedef struct _aubio_spectralpeak_t aubio_spectralpeak_t;
    3030typedef struct _aubio_spectralcandidate_t aubio_spectralcandidate_t;
    31 uint_t aubio_pitchmcomb_get_root_peak(aubio_spectralpeak_t * peaks, uint_t length);
    32 uint_t aubio_pitchmcomb_quadpick(aubio_spectralpeak_t * spectral_peaks, fvec_t * X);
    33 void aubio_pitchmcomb_spectral_pp(aubio_pitchmcomb_t * p, fvec_t * oldmag);
    34 void aubio_pitchmcomb_combdet(aubio_pitchmcomb_t * p, fvec_t * newmag);
     31uint_t aubio_pitchmcomb_get_root_peak (aubio_spectralpeak_t * peaks,
     32    uint_t length);
     33uint_t aubio_pitchmcomb_quadpick (aubio_spectralpeak_t * spectral_peaks,
     34    fvec_t * X);
     35void aubio_pitchmcomb_spectral_pp (aubio_pitchmcomb_t * p, fvec_t * oldmag);
     36void aubio_pitchmcomb_combdet (aubio_pitchmcomb_t * p, fvec_t * newmag);
    3537/* not used but useful : sort by amplitudes (or anything else)
    3638 * sort_pitchpeak(peaks, length);
    3739 */
    3840/** spectral_peak comparison function (must return signed int) */
    39 static sint_t aubio_pitchmcomb_sort_peak_comp(const void *x, const void *y);
     41static sint_t aubio_pitchmcomb_sort_peak_comp (const void *x, const void *y);
    4042/** sort spectral_peak against their mag */
    41 void aubio_pitchmcomb_sort_peak(aubio_spectralpeak_t * peaks, uint_t nbins);
     43void aubio_pitchmcomb_sort_peak (aubio_spectralpeak_t * peaks, uint_t nbins);
    4244/** select the best candidates */
    43 uint_t aubio_pitch_cands(aubio_pitchmcomb_t * p, cvec_t * fftgrain, smpl_t * cands);
     45uint_t aubio_pitch_cands (aubio_pitchmcomb_t * p, cvec_t * fftgrain,
     46    smpl_t * cands);
    4447
    4548/** sort spectral_candidate against their comb ene */
    46 void aubio_pitchmcomb_sort_cand_ene(aubio_spectralcandidate_t ** candidates, uint_t nbins);
     49void aubio_pitchmcomb_sort_cand_ene (aubio_spectralcandidate_t ** candidates,
     50    uint_t nbins);
    4751/** sort spectral_candidate against their frequency */
    48 void aubio_pitchmcomb_sort_cand_freq(aubio_spectralcandidate_t ** candidates, uint_t nbins);
    49 
    50 struct _aubio_pitchmcomb_t {
     52void aubio_pitchmcomb_sort_cand_freq (aubio_spectralcandidate_t ** candidates,
     53    uint_t nbins);
     54
     55struct _aubio_pitchmcomb_t
     56{
    5157  smpl_t threshold;                        /**< offset threshold [0.033 or 0.01]     */
    5258  smpl_t alpha;                            /**< normalisation exponent [9]           */
     
    6167  uint_t goodcandidate;                    /**< best candidate                       */
    6268  uint_t spec_partition;                   /**< spectrum partition to consider       */
    63   aubio_spectralpeak_t * peaks;            /**< up to length win/spec_partition      */
    64   aubio_spectralcandidate_t ** candidates; /** up to five candidates                 */
     69  aubio_spectralpeak_t *peaks;             /**< up to length win/spec_partition      */
     70  aubio_spectralcandidate_t **candidates; /** up to five candidates                 */
    6571  /* some scratch pads */
    6672  /** \bug  (unnecessary copied from fftgrain?) */
    67   fvec_t * newmag;                         /**< vec to store mag                     */
    68   fvec_t * scratch;                        /**< vec to store modified mag            */
    69   fvec_t * scratch2;                       /**< vec to compute moving median         */
    70   fvec_t * theta;                         /**< vec to store phase                     */
     73  fvec_t *newmag;                          /**< vec to store mag                     */
     74  fvec_t *scratch;                         /**< vec to store modified mag            */
     75  fvec_t *scratch2;                        /**< vec to compute moving median         */
     76  fvec_t *theta;                          /**< vec to store phase                     */
    7177  smpl_t phasediff;
    7278  smpl_t phasefreq;
     
    7884
    7985/** spectral peak object */
    80 struct _aubio_spectralpeak_t {
     86struct _aubio_spectralpeak_t
     87{
    8188  uint_t bin;     /**< bin [0-(length-1)] */
    8289  smpl_t ebin;    /**< estimated bin */
     
    8592
    8693/** spectral candidates array object */
    87 struct _aubio_spectralcandidate_t {
     94struct _aubio_spectralcandidate_t
     95{
    8896  smpl_t ebin;    /**< interpolated bin */
    89   smpl_t * ecomb; /**< comb */
     97  smpl_t *ecomb; /**< comb */
    9098  smpl_t ene;     /**< candidate energy */
    9199  smpl_t len;     /**< length */
     
    93101
    94102
    95 void aubio_pitchmcomb_do (aubio_pitchmcomb_t * p, cvec_t * fftgrain, fvec_t * output) {
    96   uint_t i,j;
     103void
     104aubio_pitchmcomb_do (aubio_pitchmcomb_t * p, cvec_t * fftgrain, fvec_t * output)
     105{
     106  uint_t i, j;
    97107  smpl_t instfreq;
    98   fvec_t * newmag = (fvec_t *)p->newmag;
     108  fvec_t *newmag = (fvec_t *) p->newmag;
    99109  //smpl_t hfc; //fe=instfreq(theta1,theta,ops); //theta1=theta;
    100110  /* copy incoming grain to newmag */
    101   for (i=0; i< fftgrain->channels; i++) {
    102   for (j=0; j< newmag->length; j++)
    103     newmag->data[0][j]=fftgrain->norm[i][j];
    104   /* detect only if local energy > 10. */
    105   //if (fvec_local_energy(newmag)>10.) {
     111  for (i = 0; i < fftgrain->channels; i++) {
     112    for (j = 0; j < newmag->length; j++)
     113      newmag->data[0][j] = fftgrain->norm[i][j];
     114    /* detect only if local energy > 10. */
     115    //if (fvec_local_energy(newmag)>10.) {
    106116    //hfc = fvec_local_hfc(newmag); //not used
    107     aubio_pitchmcomb_spectral_pp(p, newmag);
    108     aubio_pitchmcomb_combdet(p,newmag);
     117    aubio_pitchmcomb_spectral_pp (p, newmag);
     118    aubio_pitchmcomb_combdet (p, newmag);
    109119    //aubio_pitchmcomb_sort_cand_freq(p->candidates,p->ncand);
    110120    //return p->candidates[p->goodcandidate]->ebin;
    111   j = (uint_t)FLOOR(p->candidates[p->goodcandidate]->ebin+.5);
    112   instfreq  = aubio_unwrap2pi(fftgrain->phas[i][j]
    113       - p->theta->data[i][j] - j*p->phasediff);
    114   instfreq *= p->phasefreq;
    115   /* store phase for next run */
    116   for (j=0; j< p->theta->length; j++) {
    117     p->theta->data[i][j]=fftgrain->phas[i][j];
    118   }
    119   //return p->candidates[p->goodcandidate]->ebin;
    120   output->data[i][0] = FLOOR(p->candidates[p->goodcandidate]->ebin+.5) + instfreq;
    121   /*} else {
    122     return -1.;
    123   }*/
    124   }
    125 }
    126 
    127 uint_t aubio_pitch_cands(aubio_pitchmcomb_t * p, cvec_t * fftgrain,
    128     smpl_t * cands) {
    129   uint_t i=0,j;
     121    j = (uint_t) FLOOR (p->candidates[p->goodcandidate]->ebin + .5);
     122    instfreq = aubio_unwrap2pi (fftgrain->phas[i][j]
     123        - p->theta->data[i][j] - j * p->phasediff);
     124    instfreq *= p->phasefreq;
     125    /* store phase for next run */
     126    for (j = 0; j < p->theta->length; j++) {
     127      p->theta->data[i][j] = fftgrain->phas[i][j];
     128    }
     129    //return p->candidates[p->goodcandidate]->ebin;
     130    output->data[i][0] =
     131        FLOOR (p->candidates[p->goodcandidate]->ebin + .5) + instfreq;
     132    /*} else {
     133       return -1.;
     134       } */
     135  }
     136}
     137
     138uint_t
     139aubio_pitch_cands (aubio_pitchmcomb_t * p, cvec_t * fftgrain, smpl_t * cands)
     140{
     141  uint_t i = 0, j;
    130142  uint_t k;
    131   fvec_t * newmag = (fvec_t *)p->newmag;
    132   aubio_spectralcandidate_t ** scands =
    133     (aubio_spectralcandidate_t **)(p->candidates);
     143  fvec_t *newmag = (fvec_t *) p->newmag;
     144  aubio_spectralcandidate_t **scands =
     145      (aubio_spectralcandidate_t **) (p->candidates);
    134146  //smpl_t hfc; //fe=instfreq(theta1,theta,ops); //theta1=theta;
    135147  /* copy incoming grain to newmag */
    136   for (j=0; j< newmag->length; j++)
    137     newmag->data[i][j]=fftgrain->norm[i][j];
     148  for (j = 0; j < newmag->length; j++)
     149    newmag->data[i][j] = fftgrain->norm[i][j];
    138150  /* detect only if local energy > 10. */
    139   if (fvec_local_energy(newmag)>10.) {
     151  if (fvec_local_energy (newmag) > 10.) {
    140152    /* hfc = fvec_local_hfc(newmag); do not use */
    141     aubio_pitchmcomb_spectral_pp(p, newmag);
    142     aubio_pitchmcomb_combdet(p,newmag);
    143     aubio_pitchmcomb_sort_cand_freq(scands,p->ncand);
     153    aubio_pitchmcomb_spectral_pp (p, newmag);
     154    aubio_pitchmcomb_combdet (p, newmag);
     155    aubio_pitchmcomb_sort_cand_freq (scands, p->ncand);
    144156    /* store ncand comb energies in cands[1:ncand] */
    145     for (k = 0; k<p->ncand; k++)
     157    for (k = 0; k < p->ncand; k++)
    146158      cands[k] = p->candidates[k]->ene;
    147159    /* store ncand[end] freq in cands[end] */
    148     cands[p->ncand] = p->candidates[p->ncand-1]->ebin;
     160    cands[p->ncand] = p->candidates[p->ncand - 1]->ebin;
    149161    return 1;
    150162  } else {
    151     for (k = 0; k<p->ncand; k++)
     163    for (k = 0; k < p->ncand; k++)
    152164      cands[k] = 0;
    153165    return 0;
     
    155167}
    156168
    157 void aubio_pitchmcomb_spectral_pp(aubio_pitchmcomb_t * p, fvec_t * newmag) {
    158   fvec_t * mag = (fvec_t *)p->scratch;
    159   fvec_t * tmp = (fvec_t *)p->scratch2;
    160   uint_t i=0,j;
     169void
     170aubio_pitchmcomb_spectral_pp (aubio_pitchmcomb_t * p, fvec_t * newmag)
     171{
     172  fvec_t *mag = (fvec_t *) p->scratch;
     173  fvec_t *tmp = (fvec_t *) p->scratch2;
     174  uint_t i = 0, j;
    161175  uint_t length = mag->length;
    162176  /* copy newmag to mag (scracth) */
    163   for (j=0;j<length;j++) {
     177  for (j = 0; j < length; j++) {
    164178    mag->data[i][j] = newmag->data[i][j];
    165179  }
    166   fvec_min_removal(mag);              /* min removal          */
    167   fvec_alpha_normalise(mag,p->alpha); /* alpha normalisation  */
    168   /* skipped */                       /* low pass filtering   */
     180  fvec_min_removal (mag);       /* min removal          */
     181  fvec_alpha_normalise (mag, p->alpha); /* alpha normalisation  */
     182  /* skipped *//* low pass filtering   */
    169183  /** \bug fvec_moving_thres may write out of bounds */
    170   fvec_adapt_thres(mag,tmp,p->win_post,p->win_pre,i); /* adaptative threshold */
    171   fvec_add(mag,-p->threshold);        /* fixed threshold      */
     184  fvec_adapt_thres (mag, tmp, p->win_post, p->win_pre, i);      /* adaptative threshold */
     185  fvec_add (mag, -p->threshold);        /* fixed threshold      */
    172186  {
    173     aubio_spectralpeak_t * peaks = (aubio_spectralpeak_t *)p->peaks;
     187    aubio_spectralpeak_t *peaks = (aubio_spectralpeak_t *) p->peaks;
    174188    uint_t count;
    175189    /*  return bin and ebin */
    176     count = aubio_pitchmcomb_quadpick(peaks,mag);
    177     for (j=0;j<count;j++)
     190    count = aubio_pitchmcomb_quadpick (peaks, mag);
     191    for (j = 0; j < count; j++)
    178192      peaks[j].mag = newmag->data[i][peaks[j].bin];
    179193    /* reset non peaks */
    180     for (j=count;j<length;j++)
     194    for (j = count; j < length; j++)
    181195      peaks[j].mag = 0.;
    182196    p->peaks = peaks;
     
    185199}
    186200
    187 void aubio_pitchmcomb_combdet(aubio_pitchmcomb_t * p, fvec_t * newmag) {
    188   aubio_spectralpeak_t * peaks = (aubio_spectralpeak_t *)p->peaks;
    189   aubio_spectralcandidate_t ** candidate =
    190     (aubio_spectralcandidate_t **)p->candidates;
     201void
     202aubio_pitchmcomb_combdet (aubio_pitchmcomb_t * p, fvec_t * newmag)
     203{
     204  aubio_spectralpeak_t *peaks = (aubio_spectralpeak_t *) p->peaks;
     205  aubio_spectralcandidate_t **candidate =
     206      (aubio_spectralcandidate_t **) p->candidates;
    191207
    192208  /* parms */
    193   uint_t N = p->npartials; /* maximum number of partials to be considered 10 */
    194   uint_t M = p->ncand;  /* maximum number of combs to be considered 5 */
     209  uint_t N = p->npartials;      /* maximum number of partials to be considered 10 */
     210  uint_t M = p->ncand;          /* maximum number of combs to be considered 5 */
    195211  uint_t length = newmag->length;
    196212  uint_t count = p->count;
     
    209225
    210226  /* get the biggest peak in the spectrum */
    211   root_peak = aubio_pitchmcomb_get_root_peak(peaks,count);
     227  root_peak = aubio_pitchmcomb_get_root_peak (peaks, count);
    212228  /* not enough partials in highest notes, could be forced */
    213229  //if (peaks[root_peak].ebin >= aubio_miditofreq(85.)/p->tau) N=2;
    214230  //if (peaks[root_peak].ebin >= aubio_miditofreq(90.)/p->tau) N=1;
    215231  /* now calculate the energy of each of the 5 combs */
    216   for (l=0;l<M;l++) {
    217     smpl_t scaler = (1./(l+1.));
    218     candidate[l]->ene = 0.; /* reset ene and len sums */
     232  for (l = 0; l < M; l++) {
     233    smpl_t scaler = (1. / (l + 1.));
     234    candidate[l]->ene = 0.;     /* reset ene and len sums */
    219235    candidate[l]->len = 0.;
    220     candidate[l]->ebin=scaler*peaks[root_peak].ebin;
     236    candidate[l]->ebin = scaler * peaks[root_peak].ebin;
    221237    /* if less than N peaks available, curlen < N */
    222238    if (candidate[l]->ebin != 0.)
    223       curlen = (uint_t)FLOOR(length/(candidate[l]->ebin));
    224     curlen = (N < curlen )? N : curlen;
     239      curlen = (uint_t) FLOOR (length / (candidate[l]->ebin));
     240    curlen = (N < curlen) ? N : curlen;
    225241    /* fill candidate[l]->ecomb[k] with (k+1)*candidate[l]->ebin */
    226     for (k=0;k<curlen;k++)
    227       candidate[l]->ecomb[k]=(candidate[l]->ebin)*(k+1.);
    228     for (k=curlen;k<length;k++)
    229       candidate[l]->ecomb[k]=0.;
     242    for (k = 0; k < curlen; k++)
     243      candidate[l]->ecomb[k] = (candidate[l]->ebin) * (k + 1.);
     244    for (k = curlen; k < length; k++)
     245      candidate[l]->ecomb[k] = 0.;
    230246    /* for each in candidate[l]->ecomb[k] */
    231     for (k=0;k<curlen;k++) {
     247    for (k = 0; k < curlen; k++) {
    232248      xx = 100000.;
    233249      /** get the candidate->ecomb the closer to peaks.ebin
    234250       * (to cope with the inharmonicity)*/
    235       for (d=0;d<count;d++) {
    236         delta2 = ABS(candidate[l]->ecomb[k]-peaks[d].ebin);
     251      for (d = 0; d < count; d++) {
     252        delta2 = ABS (candidate[l]->ecomb[k] - peaks[d].ebin);
    237253        if (delta2 <= xx) {
    238254          position = d;
     
    242258      /* for a Q factor of 17, maintaining "constant Q filtering",
    243259       * and sum energy and length over non null combs */
    244       if ( 17. * xx < candidate[l]->ecomb[k] ) {
    245         candidate[l]->ecomb[k]=peaks[position].ebin;
    246         candidate[l]->ene += /* ecomb rounded to nearest int */
    247           POW(newmag->data[0][(uint_t)FLOOR(candidate[l]->ecomb[k]+.5)],0.25);
    248         candidate[l]->len += 1./curlen;
     260      if (17. * xx < candidate[l]->ecomb[k]) {
     261        candidate[l]->ecomb[k] = peaks[position].ebin;
     262        candidate[l]->ene +=    /* ecomb rounded to nearest int */
     263            POW (newmag->data[0][(uint_t) FLOOR (candidate[l]->ecomb[k] + .5)],
     264            0.25);
     265        candidate[l]->len += 1. / curlen;
    249266      } else
    250         candidate[l]->ecomb[k]=0.;
     267        candidate[l]->ecomb[k] = 0.;
    251268    }
    252269    /* punishment */
    253270    /*if (candidate[l]->len<0.6)
    254       candidate[l]->ene=0.; */
     271       candidate[l]->ene=0.; */
    255272    /* remember best candidate energy (in polyphonic, could check for
    256273     * tmpene*1.1 < candidate->ene to reduce jumps towards low frequencies) */
     
    270287 * \bug peak-picking too picky, sometimes counts too many peaks ?
    271288 */
    272 uint_t aubio_pitchmcomb_quadpick(aubio_spectralpeak_t * spectral_peaks, fvec_t * X){
     289uint_t
     290aubio_pitchmcomb_quadpick (aubio_spectralpeak_t * spectral_peaks, fvec_t * X)
     291{
    273292  uint_t i, j, ispeak, count = 0;
    274   for (i=0;i<X->channels;i++)
    275     for (j=1;j<X->length-1;j++) {
    276       ispeak = fvec_peakpick(X,j);
     293  for (i = 0; i < X->channels; i++)
     294    for (j = 1; j < X->length - 1; j++) {
     295      ispeak = fvec_peakpick (X, j);
    277296      if (ispeak) {
    278297        count += ispeak;
    279         spectral_peaks[count-1].bin = j;
    280         spectral_peaks[count-1].ebin = fvec_quadint(X, j, i) - 1.;
     298        spectral_peaks[count - 1].bin = j;
     299        spectral_peaks[count - 1].ebin = fvec_quadint (X, j, i) - 1.;
    281300      }
    282301    }
     
    285304
    286305/* get predominant partial */
    287 uint_t aubio_pitchmcomb_get_root_peak(aubio_spectralpeak_t * peaks, uint_t length) {
    288   uint_t i,pos=0;
     306uint_t
     307aubio_pitchmcomb_get_root_peak (aubio_spectralpeak_t * peaks, uint_t length)
     308{
     309  uint_t i, pos = 0;
    289310  smpl_t tmp = 0.;
    290   for (i=0;i<length;i++)
     311  for (i = 0; i < length; i++)
    291312    if (tmp <= peaks[i].mag) {
    292313      pos = i;
     
    296317}
    297318
    298 void aubio_pitchmcomb_sort_peak(aubio_spectralpeak_t * peaks, uint_t nbins) {
    299   qsort(peaks, nbins, sizeof(aubio_spectralpeak_t),
     319void
     320aubio_pitchmcomb_sort_peak (aubio_spectralpeak_t * peaks, uint_t nbins)
     321{
     322  qsort (peaks, nbins, sizeof (aubio_spectralpeak_t),
    300323      aubio_pitchmcomb_sort_peak_comp);
    301324}
    302 static sint_t aubio_pitchmcomb_sort_peak_comp(const void *x, const void *y) {
    303   return (((aubio_spectralpeak_t *)y)->mag - ((aubio_spectralpeak_t *)x)->mag);
    304 }
    305 
    306 
    307 void aubio_pitchmcomb_sort_cand_ene(aubio_spectralcandidate_t ** candidates, uint_t nbins) {
     325
     326static sint_t
     327aubio_pitchmcomb_sort_peak_comp (const void *x, const void *y)
     328{
     329  return (((aubio_spectralpeak_t *) y)->mag -
     330      ((aubio_spectralpeak_t *) x)->mag);
     331}
     332
     333
     334void
     335aubio_pitchmcomb_sort_cand_ene (aubio_spectralcandidate_t ** candidates,
     336    uint_t nbins)
     337{
    308338  uint_t cur = 0;
    309339  uint_t run = 0;
    310   for (cur=0;cur<nbins;cur++) {
     340  for (cur = 0; cur < nbins; cur++) {
    311341    run = cur + 1;
    312     for (run=cur;run<nbins;run++) {
    313       if(candidates[run]->ene > candidates[cur]->ene)
    314         CAND_SWAP(candidates[run], candidates[cur]);
    315     }
    316   }
    317 }
    318 
    319 
    320 void aubio_pitchmcomb_sort_cand_freq(aubio_spectralcandidate_t ** candidates, uint_t nbins) {
     342    for (run = cur; run < nbins; run++) {
     343      if (candidates[run]->ene > candidates[cur]->ene)
     344        CAND_SWAP (candidates[run], candidates[cur]);
     345    }
     346  }
     347}
     348
     349
     350void
     351aubio_pitchmcomb_sort_cand_freq (aubio_spectralcandidate_t ** candidates,
     352    uint_t nbins)
     353{
    321354  uint_t cur = 0;
    322355  uint_t run = 0;
    323   for (cur=0;cur<nbins;cur++) {
     356  for (cur = 0; cur < nbins; cur++) {
    324357    run = cur + 1;
    325     for (run=cur;run<nbins;run++) {
    326       if(candidates[run]->ebin < candidates[cur]->ebin)
    327         CAND_SWAP(candidates[run], candidates[cur]);
    328     }
    329   }
    330 }
    331 
    332 aubio_pitchmcomb_t * new_aubio_pitchmcomb(uint_t bufsize, uint_t hopsize, uint_t channels) {
    333   aubio_pitchmcomb_t * p = AUBIO_NEW(aubio_pitchmcomb_t);
     358    for (run = cur; run < nbins; run++) {
     359      if (candidates[run]->ebin < candidates[cur]->ebin)
     360        CAND_SWAP (candidates[run], candidates[cur]);
     361    }
     362  }
     363}
     364
     365aubio_pitchmcomb_t *
     366new_aubio_pitchmcomb (uint_t bufsize, uint_t hopsize, uint_t channels)
     367{
     368  aubio_pitchmcomb_t *p = AUBIO_NEW (aubio_pitchmcomb_t);
    334369  /* bug: should check if size / 8 > post+pre+1 */
    335370  uint_t i, j;
    336371  uint_t spec_size;
    337   p->spec_partition   = 4;
    338   p->ncand            = 5;
    339   p->npartials        = 5;
    340   p->cutoff           = 1.;
    341   p->threshold        = 0.01;
    342   p->win_post         = 8;
    343   p->win_pre          = 7;
     372  p->spec_partition = 4;
     373  p->ncand = 5;
     374  p->npartials = 5;
     375  p->cutoff = 1.;
     376  p->threshold = 0.01;
     377  p->win_post = 8;
     378  p->win_pre = 7;
    344379  // p->tau              = samplerate/bufsize;
    345   p->alpha            = 9.;
    346   p->goodcandidate    = 0;
    347   p->phasefreq        = bufsize/hopsize/TWO_PI;
    348   p->phasediff        = TWO_PI*hopsize/bufsize;
    349   spec_size = bufsize/p->spec_partition;
     380  p->alpha = 9.;
     381  p->goodcandidate = 0;
     382  p->phasefreq = bufsize / hopsize / TWO_PI;
     383  p->phasediff = TWO_PI * hopsize / bufsize;
     384  spec_size = bufsize / p->spec_partition;
    350385  //p->pickerfn = quadpick;
    351386  //p->biquad = new_biquad(0.1600,0.3200,0.1600, -0.5949, 0.2348);
    352387  /* allocate temp memory */
    353   p->newmag     = new_fvec(spec_size,1);
     388  p->newmag = new_fvec (spec_size, 1);
    354389  /* array for median */
    355   p->scratch    = new_fvec(spec_size,1);
     390  p->scratch = new_fvec (spec_size, 1);
    356391  /* array for phase */
    357   p->theta      = new_fvec(spec_size,channels);
     392  p->theta = new_fvec (spec_size, channels);
    358393  /* array for adaptative threshold */
    359   p->scratch2   = new_fvec(p->win_post+p->win_pre+1,1);
     394  p->scratch2 = new_fvec (p->win_post + p->win_pre + 1, 1);
    360395  /* array of spectral peaks */
    361   p->peaks      = AUBIO_ARRAY(aubio_spectralpeak_t,spec_size);
     396  p->peaks = AUBIO_ARRAY (aubio_spectralpeak_t, spec_size);
    362397  for (i = 0; i < spec_size; i++) {
    363398    p->peaks[i].bin = 0.;
     
    366401  }
    367402  /* array of pointers to spectral candidates */
    368   p->candidates = AUBIO_ARRAY(aubio_spectralcandidate_t *,p->ncand);
    369   for (i=0;i<p->ncand;i++) {
    370     p->candidates[i] = AUBIO_NEW(aubio_spectralcandidate_t);
    371     p->candidates[i]->ecomb = AUBIO_ARRAY(smpl_t, spec_size);
    372     for (j=0; j < spec_size; j++) {
     403  p->candidates = AUBIO_ARRAY (aubio_spectralcandidate_t *, p->ncand);
     404  for (i = 0; i < p->ncand; i++) {
     405    p->candidates[i] = AUBIO_NEW (aubio_spectralcandidate_t);
     406    p->candidates[i]->ecomb = AUBIO_ARRAY (smpl_t, spec_size);
     407    for (j = 0; j < spec_size; j++) {
    373408      p->candidates[i]->ecomb[j] = 0.;
    374409    }
     
    381416
    382417
    383 void del_aubio_pitchmcomb (aubio_pitchmcomb_t *p) {
     418void
     419del_aubio_pitchmcomb (aubio_pitchmcomb_t * p)
     420{
    384421  uint_t i;
    385   del_fvec(p->newmag);
    386   del_fvec(p->scratch);
    387   del_fvec(p->theta);
    388   del_fvec(p->scratch2);
    389   AUBIO_FREE(p->peaks);
    390   for (i=0;i<p->ncand;i++) {
    391     AUBIO_FREE(p->candidates[i]->ecomb);
    392     AUBIO_FREE(p->candidates[i]);
    393   }
    394   AUBIO_FREE(p->candidates);
    395   AUBIO_FREE(p);
    396 }
     422  del_fvec (p->newmag);
     423  del_fvec (p->scratch);
     424  del_fvec (p->theta);
     425  del_fvec (p->scratch2);
     426  AUBIO_FREE (p->peaks);
     427  for (i = 0; i < p->ncand; i++) {
     428    AUBIO_FREE (p->candidates[i]->ecomb);
     429    AUBIO_FREE (p->candidates[i]);
     430  }
     431  AUBIO_FREE (p->candidates);
     432  AUBIO_FREE (p);
     433}
  • src/pitch/pitchmcomb.h

    r63f3c70 rfddfa64  
    5050 
    5151*/
    52 void aubio_pitchmcomb_do (aubio_pitchmcomb_t * p, cvec_t * fftgrain, fvec_t * output);
     52void aubio_pitchmcomb_do (aubio_pitchmcomb_t * p, cvec_t * fftgrain,
     53    fvec_t * output);
     54
    5355/** creation of the pitch detection object
    5456 
     
    5961 
    6062*/
    61 aubio_pitchmcomb_t * new_aubio_pitchmcomb(uint_t bufsize, uint_t hopsize, uint_t channels);
     63aubio_pitchmcomb_t *new_aubio_pitchmcomb (uint_t bufsize, uint_t hopsize,
     64    uint_t channels);
     65
    6266/** deletion of the pitch detection object
    6367 
     
    6569 
    6670*/
    67 void del_aubio_pitchmcomb(aubio_pitchmcomb_t *p);
     71void del_aubio_pitchmcomb (aubio_pitchmcomb_t * p);
    6872
    6973#ifdef __cplusplus
  • src/pitch/pitchyin.c

    r63f3c70 rfddfa64  
    3333#include "pitch/pitchyin.h"
    3434
    35 struct _aubio_pitchyin_t {
    36   fvec_t * yin;
     35struct _aubio_pitchyin_t
     36{
     37  fvec_t *yin;
    3738  smpl_t tol;
    3839};
     
    4445
    4546*/
    46 void aubio_pitchyin_diff(fvec_t * input, fvec_t * yinbuf);
     47void aubio_pitchyin_diff (fvec_t * input, fvec_t * yinbuf);
    4748
    4849/** in place computation of the YIN cumulative normalised function
     
    5152
    5253*/
    53 void aubio_pitchyin_getcum(fvec_t * yinbuf);
     54void aubio_pitchyin_getcum (fvec_t * yinbuf);
    5455
    5556/** detect pitch in a YIN function
     
    5859
    5960*/
    60 uint_t aubio_pitchyin_getpitch(fvec_t *yinbuf);
     61uint_t aubio_pitchyin_getpitch (fvec_t * yinbuf);
    6162
    62 aubio_pitchyin_t * new_aubio_pitchyin (uint_t bufsize) {
    63   aubio_pitchyin_t * o = AUBIO_NEW(aubio_pitchyin_t);
    64   o->yin = new_fvec (bufsize/2, 1);
     63aubio_pitchyin_t *
     64new_aubio_pitchyin (uint_t bufsize)
     65{
     66  aubio_pitchyin_t *o = AUBIO_NEW (aubio_pitchyin_t);
     67  o->yin = new_fvec (bufsize / 2, 1);
    6568  o->tol = 0.15;
    6669  return o;
    6770}
    6871
    69 void del_aubio_pitchyin (aubio_pitchyin_t *o) {
    70   del_fvec(o->yin);
    71   AUBIO_FREE(o);
     72void
     73del_aubio_pitchyin (aubio_pitchyin_t * o)
     74{
     75  del_fvec (o->yin);
     76  AUBIO_FREE (o);
    7277}
    7378
    7479/* outputs the difference function */
    75 void aubio_pitchyin_diff(fvec_t * input, fvec_t * yin){
    76   uint_t c,j,tau;
     80void
     81aubio_pitchyin_diff (fvec_t * input, fvec_t * yin)
     82{
     83  uint_t c, j, tau;
    7784  smpl_t tmp;
    78   for (c=0;c<input->channels;c++)
    79   {
    80     for (tau=0;tau<yin->length;tau++)
    81     {
     85  for (c = 0; c < input->channels; c++) {
     86    for (tau = 0; tau < yin->length; tau++) {
    8287      yin->data[c][tau] = 0.;
    8388    }
    84     for (tau=1;tau<yin->length;tau++)
    85     {
    86       for (j=0;j<yin->length;j++)
    87       {
    88         tmp = input->data[c][j] - input->data[c][j+tau];
    89         yin->data[c][tau] += SQR(tmp);
     89    for (tau = 1; tau < yin->length; tau++) {
     90      for (j = 0; j < yin->length; j++) {
     91        tmp = input->data[c][j] - input->data[c][j + tau];
     92        yin->data[c][tau] += SQR (tmp);
    9093      }
    9194    }
     
    9497
    9598/* cumulative mean normalized difference function */
    96 void aubio_pitchyin_getcum(fvec_t * yin) {
    97   uint_t c,tau;
     99void
     100aubio_pitchyin_getcum (fvec_t * yin)
     101{
     102  uint_t c, tau;
    98103  smpl_t tmp;
    99   for (c=0;c<yin->channels;c++)
    100   {
     104  for (c = 0; c < yin->channels; c++) {
    101105    tmp = 0.;
    102106    yin->data[c][0] = 1.;
    103107    //AUBIO_DBG("%f\t",yin->data[c][0]);
    104     for (tau=1;tau<yin->length;tau++)
    105     {
     108    for (tau = 1; tau < yin->length; tau++) {
    106109      tmp += yin->data[c][tau];
    107       yin->data[c][tau] *= tau/tmp;
     110      yin->data[c][tau] *= tau / tmp;
    108111      //AUBIO_DBG("%f\t",yin->data[c][tau]);
    109112    }
     
    112115}
    113116
    114 uint_t aubio_pitchyin_getpitch(fvec_t * yin) {
    115   uint_t c=0,tau=1;
    116   do
    117   {
    118     if(yin->data[c][tau] < 0.1) {
    119       while (yin->data[c][tau+1] < yin->data[c][tau]) {
     117uint_t
     118aubio_pitchyin_getpitch (fvec_t * yin)
     119{
     120  uint_t c = 0, tau = 1;
     121  do {
     122    if (yin->data[c][tau] < 0.1) {
     123      while (yin->data[c][tau + 1] < yin->data[c][tau]) {
    120124        tau++;
    121125      }
     
    123127    }
    124128    tau++;
    125   } while (tau<yin->length);
     129  } while (tau < yin->length);
    126130  //AUBIO_DBG("No pitch found");
    127131  return 0;
     
    130134
    131135/* all the above in one */
    132 void aubio_pitchyin_do(aubio_pitchyin_t *o, fvec_t * input, fvec_t * out){
     136void
     137aubio_pitchyin_do (aubio_pitchyin_t * o, fvec_t * input, fvec_t * out)
     138{
    133139  smpl_t tol = o->tol;
    134   fvec_t * yin = o->yin;
    135   uint_t c , j,tau = 0;
     140  fvec_t *yin = o->yin;
     141  uint_t c, j, tau = 0;
    136142  sint_t period;
    137143  smpl_t tmp = 0., tmp2 = 0.;
    138144  for (c = 0; c < input->channels; c++) {
    139145    yin->data[c][0] = 1.;
    140     for (tau=1;tau<yin->length;tau++)
    141     {
     146    for (tau = 1; tau < yin->length; tau++) {
    142147      yin->data[c][tau] = 0.;
    143       for (j=0;j<yin->length;j++)
    144       {
    145         tmp = input->data[c][j] - input->data[c][j+tau];
    146         yin->data[c][tau] += SQR(tmp);
     148      for (j = 0; j < yin->length; j++) {
     149        tmp = input->data[c][j] - input->data[c][j + tau];
     150        yin->data[c][tau] += SQR (tmp);
    147151      }
    148152      tmp2 += yin->data[c][tau];
    149       yin->data[c][tau] *= tau/tmp2;
    150       period = tau-3;
    151       if(tau > 4 && (yin->data[c][period] < tol) &&
    152           (yin->data[c][period] < yin->data[c][period+1])) {
    153         out->data[c][0] = fvec_quadint(yin,period,c);
     153      yin->data[c][tau] *= tau / tmp2;
     154      period = tau - 3;
     155      if (tau > 4 && (yin->data[c][period] < tol) &&
     156          (yin->data[c][period] < yin->data[c][period + 1])) {
     157        out->data[c][0] = fvec_quadint (yin, period, c);
    154158        goto beach;
    155159      }
    156160    }
    157     out->data[c][0] = fvec_quadint(yin,fvec_min_elem(yin),c);
    158 beach:
     161    out->data[c][0] = fvec_quadint (yin, fvec_min_elem (yin), c);
     162  beach:
    159163    continue;
    160164  }
     
    162166}
    163167
    164 uint_t aubio_pitchyin_set_tolerance (aubio_pitchyin_t *o, smpl_t tol) {
     168uint_t
     169aubio_pitchyin_set_tolerance (aubio_pitchyin_t * o, smpl_t tol)
     170{
    165171  o->tol = tol;
    166172  return 0;
    167173}
    168174
    169 smpl_t aubio_pitchyin_get_tolerance (aubio_pitchyin_t *o) {
     175smpl_t
     176aubio_pitchyin_get_tolerance (aubio_pitchyin_t * o)
     177{
    170178  return o->tol;
    171179}
  • src/pitch/pitchyin.h

    r63f3c70 rfddfa64  
    4848 
    4949*/
    50 aubio_pitchyin_t * new_aubio_pitchyin (uint_t bufsize);
     50aubio_pitchyin_t *new_aubio_pitchyin (uint_t bufsize);
    5151
    5252/** deletion of the pitch detection object
     
    7373
    7474*/
    75 uint_t aubio_pitchyin_set_tolerance (aubio_pitchyin_t *o, smpl_t tol);
     75uint_t aubio_pitchyin_set_tolerance (aubio_pitchyin_t * o, smpl_t tol);
    7676
    7777/** get tolerance parameter for YIN algorithm
  • src/pitch/pitchyinfft.c

    r63f3c70 rfddfa64  
    2727
    2828/** pitch yinfft structure */
    29 struct _aubio_pitchyinfft_t {
    30   fvec_t * win;       /**< temporal weighting window */
    31   fvec_t * winput;    /**< windowed spectrum */
    32   cvec_t * res;       /**< complex vector to compute square difference function */
    33   fvec_t * sqrmag;    /**< square difference function */
    34   fvec_t * weight;    /**< spectral weighting window (psychoacoustic model) */
    35   cvec_t * fftout;    /**< Fourier transform output */
    36   aubio_fft_t * fft;  /**< fft object to compute square difference function */
    37   fvec_t * yinfft;    /**< Yin function */
     29struct _aubio_pitchyinfft_t
     30{
     31  fvec_t *win;        /**< temporal weighting window */
     32  fvec_t *winput;     /**< windowed spectrum */
     33  cvec_t *res;        /**< complex vector to compute square difference function */
     34  fvec_t *sqrmag;     /**< square difference function */
     35  fvec_t *weight;     /**< spectral weighting window (psychoacoustic model) */
     36  cvec_t *fftout;     /**< Fourier transform output */
     37  aubio_fft_t *fft;   /**< fft object to compute square difference function */
     38  fvec_t *yinfft;     /**< Yin function */
    3839  smpl_t tol;         /**< Yin tolerance */
    3940};
    4041
    41 static const smpl_t freqs[] = {0., 20., 25., 31.5, 40., 50., 63., 80., 100.,
     42static const smpl_t freqs[] = { 0., 20., 25., 31.5, 40., 50., 63., 80., 100.,
    4243  125., 160., 200., 250., 315., 400., 500., 630., 800., 1000., 1250.,
    4344  1600., 2000., 2500., 3150., 4000., 5000., 6300., 8000., 9000., 10000.,
    44   12500., 15000., 20000.,  25100};
     45  12500., 15000., 20000., 25100
     46};
    4547
    46 static const smpl_t weight[] = {-75.8, -70.1, -60.8, -52.1, -44.2, -37.5,
     48static const smpl_t weight[] = { -75.8, -70.1, -60.8, -52.1, -44.2, -37.5,
    4749  -31.3, -25.6, -20.9, -16.5, -12.6, -9.6, -7.0, -4.7, -3.0, -1.8, -0.8,
    4850  -0.2, -0.0, 0.5, 1.6, 3.2, 5.4, 7.8, 8.1, 5.3, -2.4, -11.1, -12.8,
    49   -12.2, -7.4, -17.8, -17.8, -17.8};
     51  -12.2, -7.4, -17.8, -17.8, -17.8
     52};
    5053
    51 aubio_pitchyinfft_t * new_aubio_pitchyinfft (uint_t bufsize)
     54aubio_pitchyinfft_t *
     55new_aubio_pitchyinfft (uint_t bufsize)
    5256{
    53   aubio_pitchyinfft_t * p = AUBIO_NEW(aubio_pitchyinfft_t);
    54   p->winput = new_fvec(bufsize,1);
    55   p->fft    = new_aubio_fft(bufsize, 1);
    56   p->fftout = new_cvec(bufsize,1);
    57   p->sqrmag = new_fvec(bufsize,1);
    58   p->res    = new_cvec(bufsize,1);
    59   p->yinfft = new_fvec(bufsize/2+1,1);
    60   p->tol    = 0.85;
    61   p->win    = new_aubio_window("hanningz", bufsize);
    62   p->weight      = new_fvec(bufsize/2+1,1);
     57  aubio_pitchyinfft_t *p = AUBIO_NEW (aubio_pitchyinfft_t);
     58  p->winput = new_fvec (bufsize, 1);
     59  p->fft = new_aubio_fft (bufsize, 1);
     60  p->fftout = new_cvec (bufsize, 1);
     61  p->sqrmag = new_fvec (bufsize, 1);
     62  p->res = new_cvec (bufsize, 1);
     63  p->yinfft = new_fvec (bufsize / 2 + 1, 1);
     64  p->tol = 0.85;
     65  p->win = new_aubio_window ("hanningz", bufsize);
     66  p->weight = new_fvec (bufsize / 2 + 1, 1);
    6367  {
    6468    uint_t i = 0, j = 1;
    6569    smpl_t freq = 0, a0 = 0, a1 = 0, f0 = 0, f1 = 0;
    66     for (i=0; i<p->weight->length; i++) {
    67       freq = (smpl_t)i/(smpl_t)bufsize*(smpl_t)44100.;
     70    for (i = 0; i < p->weight->length; i++) {
     71      freq = (smpl_t) i / (smpl_t) bufsize *(smpl_t) 44100.;
    6872      while (freq > freqs[j]) {
    69         j +=1;
    70             }
    71       a0 = weight[j-1];
    72       f0 = freqs[j-1];
    73             a1 = weight[j];
     73        j += 1;
     74      }
     75      a0 = weight[j - 1];
     76      f0 = freqs[j - 1];
     77      a1 = weight[j];
    7478      f1 = freqs[j];
    75       if (f0 == f1) { // just in case
     79      if (f0 == f1) {           // just in case
    7680        p->weight->data[0][i] = a0;
    77       } else if (f0 == 0) { // y = ax+b
    78         p->weight->data[0][i] = (a1-a0)/f1*freq + a0;
     81      } else if (f0 == 0) {     // y = ax+b
     82        p->weight->data[0][i] = (a1 - a0) / f1 * freq + a0;
    7983      } else {
    80         p->weight->data[0][i] = (a1-a0)/(f1-f0)*freq +
    81           (a0 - (a1 - a0)/(f1/f0 - 1.));
     84        p->weight->data[0][i] = (a1 - a0) / (f1 - f0) * freq +
     85            (a0 - (a1 - a0) / (f1 / f0 - 1.));
    8286      }
    8387      while (freq > freqs[j]) {
    84         j +=1;
     88        j += 1;
    8589      }
    8690      //AUBIO_DBG("%f\n",p->weight->data[0][i]);
    87       p->weight->data[0][i] = DB2LIN(p->weight->data[0][i]);
     91      p->weight->data[0][i] = DB2LIN (p->weight->data[0][i]);
    8892      //p->weight->data[0][i] = SQRT(DB2LIN(p->weight->data[0][i]));
    8993    }
     
    9296}
    9397
    94 void aubio_pitchyinfft_do (aubio_pitchyinfft_t * p, fvec_t * input, fvec_t * output) {
     98void
     99aubio_pitchyinfft_do (aubio_pitchyinfft_t * p, fvec_t * input, fvec_t * output)
     100{
    95101  uint_t i, tau, l;
    96102  uint_t halfperiod;
    97103  smpl_t tmp, sum;
    98   cvec_t * res = (cvec_t *)p->res;
    99   fvec_t * yin = (fvec_t *)p->yinfft;
    100   for (i=0; i < input->channels; i++){
    101   l = 0; tmp = 0.; sum = 0.;
    102   for (l=0; l < input->length; l++){
    103     p->winput->data[0][l] = p->win->data[0][l] * input->data[i][l];
    104   }
    105   aubio_fft_do(p->fft,p->winput,p->fftout);
    106   for (l=0; l < p->fftout->length; l++){
    107     p->sqrmag->data[0][l] = SQR(p->fftout->norm[0][l]);
    108     p->sqrmag->data[0][l] *= p->weight->data[0][l];
    109   }
    110   for (l=1; l < p->fftout->length; l++){
    111     p->sqrmag->data[0][(p->fftout->length-1)*2-l] =
    112      SQR(p->fftout->norm[0][l]);
    113     p->sqrmag->data[0][(p->fftout->length-1)*2-l] *=
    114      p->weight->data[0][l];
    115   }
    116   for (l=0; l < p->sqrmag->length/2+1; l++) {
    117     sum += p->sqrmag->data[0][l];
    118   }
    119   sum *= 2.;
    120   aubio_fft_do(p->fft,p->sqrmag,res);
    121   yin->data[0][0] = 1.;
    122   for (tau=1; tau < yin->length; tau++) {
    123     yin->data[0][tau] = sum -
    124       res->norm[0][tau]*COS(res->phas[0][tau]);
    125     tmp += yin->data[0][tau];
    126     yin->data[0][tau] *= tau/tmp;
    127   }
    128   tau = fvec_min_elem(yin);
    129   if (yin->data[0][tau] < p->tol) {
    130     /* no interpolation */
    131     //return tau;
    132     /* 3 point quadratic interpolation */
    133     //return fvec_quadint_min(yin,tau,1);
    134     /* additional check for (unlikely) octave doubling in higher frequencies */
    135     if (tau>35) {
    136       output->data[i][0] = fvec_quadint(yin,tau,i);
     104  cvec_t *res = (cvec_t *) p->res;
     105  fvec_t *yin = (fvec_t *) p->yinfft;
     106  for (i = 0; i < input->channels; i++) {
     107    l = 0;
     108    tmp = 0.;
     109    sum = 0.;
     110    for (l = 0; l < input->length; l++) {
     111      p->winput->data[0][l] = p->win->data[0][l] * input->data[i][l];
     112    }
     113    aubio_fft_do (p->fft, p->winput, p->fftout);
     114    for (l = 0; l < p->fftout->length; l++) {
     115      p->sqrmag->data[0][l] = SQR (p->fftout->norm[0][l]);
     116      p->sqrmag->data[0][l] *= p->weight->data[0][l];
     117    }
     118    for (l = 1; l < p->fftout->length; l++) {
     119      p->sqrmag->data[0][(p->fftout->length - 1) * 2 - l] =
     120          SQR (p->fftout->norm[0][l]);
     121      p->sqrmag->data[0][(p->fftout->length - 1) * 2 - l] *=
     122          p->weight->data[0][l];
     123    }
     124    for (l = 0; l < p->sqrmag->length / 2 + 1; l++) {
     125      sum += p->sqrmag->data[0][l];
     126    }
     127    sum *= 2.;
     128    aubio_fft_do (p->fft, p->sqrmag, res);
     129    yin->data[0][0] = 1.;
     130    for (tau = 1; tau < yin->length; tau++) {
     131      yin->data[0][tau] = sum - res->norm[0][tau] * COS (res->phas[0][tau]);
     132      tmp += yin->data[0][tau];
     133      yin->data[0][tau] *= tau / tmp;
     134    }
     135    tau = fvec_min_elem (yin);
     136    if (yin->data[0][tau] < p->tol) {
     137      /* no interpolation */
     138      //return tau;
     139      /* 3 point quadratic interpolation */
     140      //return fvec_quadint_min(yin,tau,1);
     141      /* additional check for (unlikely) octave doubling in higher frequencies */
     142      if (tau > 35) {
     143        output->data[i][0] = fvec_quadint (yin, tau, i);
     144      } else {
     145        /* should compare the minimum value of each interpolated peaks */
     146        halfperiod = FLOOR (tau / 2 + .5);
     147        if (yin->data[0][halfperiod] < p->tol)
     148          output->data[i][0] = fvec_quadint (yin, halfperiod, i);
     149        else
     150          output->data[i][0] = fvec_quadint (yin, tau, i);
     151      }
    137152    } else {
    138       /* should compare the minimum value of each interpolated peaks */
    139       halfperiod = FLOOR(tau/2+.5);
    140       if (yin->data[0][halfperiod] < p->tol)
    141         output->data[i][0] = fvec_quadint(yin,halfperiod,i);
    142       else
    143         output->data[i][0] = fvec_quadint(yin,tau,i);
     153      output->data[i][0] = 0.;
    144154    }
    145   } else {
    146     output->data[i][0] = 0.;
    147   }
    148155  }
    149156}
    150157
    151 void del_aubio_pitchyinfft(aubio_pitchyinfft_t *p){
    152   del_fvec(p->win);
    153   del_aubio_fft(p->fft);
    154   del_fvec(p->yinfft);
    155   del_fvec(p->sqrmag);
    156   del_cvec(p->res);
    157   del_cvec(p->fftout);
    158   del_fvec(p->winput);
    159   del_fvec(p->weight);
    160   AUBIO_FREE(p);
     158void
     159del_aubio_pitchyinfft (aubio_pitchyinfft_t * p)
     160{
     161  del_fvec (p->win);
     162  del_aubio_fft (p->fft);
     163  del_fvec (p->yinfft);
     164  del_fvec (p->sqrmag);
     165  del_cvec (p->res);
     166  del_cvec (p->fftout);
     167  del_fvec (p->winput);
     168  del_fvec (p->weight);
     169  AUBIO_FREE (p);
    161170}
    162171
    163 uint_t aubio_pitchyinfft_set_tolerance (aubio_pitchyinfft_t * p, smpl_t tol) {
     172uint_t
     173aubio_pitchyinfft_set_tolerance (aubio_pitchyinfft_t * p, smpl_t tol)
     174{
    164175  p->tol = tol;
    165176  return 0;
    166177}
    167178
    168 smpl_t aubio_pitchyinfft_get_tolerance (aubio_pitchyinfft_t * p) {
     179smpl_t
     180aubio_pitchyinfft_get_tolerance (aubio_pitchyinfft_t * p)
     181{
    169182  return p->tol;
    170183}
  • src/pitch/pitchyinfft.h

    r63f3c70 rfddfa64  
    5151 
    5252*/
    53 void aubio_pitchyinfft_do (aubio_pitchyinfft_t *p, fvec_t * in, fvec_t * out);
     53void aubio_pitchyinfft_do (aubio_pitchyinfft_t * p, fvec_t * in, fvec_t * out);
    5454/** creation of the pitch detection object
    5555 
     
    5757 
    5858*/
    59 aubio_pitchyinfft_t * new_aubio_pitchyinfft (uint_t bufsize);
     59aubio_pitchyinfft_t *new_aubio_pitchyinfft (uint_t bufsize);
    6060/** deletion of the pitch detection object
    6161 
     
    6363 
    6464*/
    65 void del_aubio_pitchyinfft (aubio_pitchyinfft_t *p);
     65void del_aubio_pitchyinfft (aubio_pitchyinfft_t * p);
    6666
    6767/** get tolerance parameter for YIN algorithm
Note: See TracChangeset for help on using the changeset viewer.