Changeset 4b9443c4 for src/notes/notes.c


Ignore:
Timestamp:
Jul 1, 2016, 3:26:59 PM (8 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:
ba303e8
Parents:
f264b17
Message:

src/notes/notes.c: complete implementation, now equivalent to examples/aubionotes.c

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/notes/notes.c

    rf264b17 r4b9443c4  
    3838
    3939  aubio_pitch_t *pitch;
     40  fvec_t *pitch_output;
     41  smpl_t pitch_tolerance;
     42
    4043  aubio_onset_t *onset;
    4144  fvec_t *onset_output;
    42   fvec_t *pitch_output;
     45  smpl_t onset_threshold;
    4346
    4447  smpl_t curnote;
    4548  smpl_t newnote;
     49
     50  smpl_t silence_threshold;
     51
     52  uint_t isready;
    4653};
    4754
    48 aubio_notes_t * new_aubio_notes (char_t * notes_method,
     55aubio_notes_t * new_aubio_notes (const char_t * notes_method,
    4956    uint_t buf_size, uint_t hop_size, uint_t samplerate) {
    5057  aubio_notes_t *o = AUBIO_NEW(aubio_notes_t);
     58
     59  const char_t * onset_method = "default";
     60  const char_t * pitch_method = "default";
    5161
    5262  o->onset_buf_size = buf_size;
     
    5464  o->hop_size = hop_size;
    5565
     66  o->onset_threshold = 0.;
     67  o->pitch_tolerance = 0.;
     68
    5669  o->samplerate = samplerate;
    5770
    58   o->median = 9;
     71  o->median = 6;
     72
     73  o->isready = 0;
     74
     75  o->onset = new_aubio_onset (onset_method, o->onset_buf_size, o->hop_size, o->samplerate);
     76  if (o->onset_threshold != 0.) aubio_onset_set_threshold (o->onset, o->onset_threshold);
     77  o->onset_output = new_fvec (1);
     78
     79  o->pitch = new_aubio_pitch (pitch_method, o->pitch_buf_size, o->hop_size, o->samplerate);
     80  if (o->pitch_tolerance != 0.) aubio_pitch_set_tolerance (o->pitch, o->pitch_tolerance);
     81  o->pitch_output = new_fvec (1);
    5982
    6083  if (strcmp(notes_method, "default") != 0) {
     
    6790
    6891  o->curnote = -1.;
    69   o->newnote = -1.;
     92  o->newnote = 0.;
     93
     94  o->silence_threshold = -90.;
    7095
    7196  return o;
     
    76101}
    77102
     103/** append new note candidate to the note_buffer and return filtered value. we
     104 * need to copy the input array as fvec_median destroy its input data.*/
     105static void
     106note_append (fvec_t * note_buffer, smpl_t curnote)
     107{
     108  uint_t i = 0;
     109  for (i = 0; i < note_buffer->length - 1; i++) {
     110    note_buffer->data[i] = note_buffer->data[i + 1];
     111  }
     112  note_buffer->data[note_buffer->length - 1] = curnote;
     113  return;
     114}
     115
     116static uint_t
     117aubio_notes_get_latest_note (aubio_notes_t *o)
     118{
     119  uint_t i;
     120  for (i = 0; i < o->note_buffer->length; i++) {
     121    o->note_buffer2->data[i] = o->note_buffer->data[i];
     122  }
     123  return fvec_median (o->note_buffer2);
     124}
     125
     126
     127void aubio_notes_do (aubio_notes_t *o, const fvec_t * input, fvec_t * notes)
     128{
     129  smpl_t new_pitch, curlevel;
     130  fvec_zeros(notes);
     131  aubio_onset_do(o->onset, input, o->onset_output);
     132
     133  aubio_pitch_do (o->pitch, input, o->pitch_output);
     134  new_pitch = o->pitch_output->data[0];
     135  if(o->median){
     136    note_append(o->note_buffer, new_pitch);
     137  }
     138
     139  /* curlevel is negatif or 1 if silence */
     140  curlevel = aubio_level_detection(input, o->silence_threshold);
     141  if (o->onset_output->data[0] != 0) {
     142    /* test for silence */
     143    if (curlevel == 1.) {
     144      if (o->median) o->isready = 0;
     145      /* send note off */
     146      //send_noteon(o->curnote,0);
     147      //notes->data[0] = o->curnote;
     148      //notes->data[1] = 0.;
     149      notes->data[2] = o->curnote;
     150    } else {
     151      if (o->median) {
     152        o->isready = 1;
     153      } else {
     154        /* kill old note */
     155        //send_noteon(o->curnote,0, o->samplerate);
     156        notes->data[2] = o->curnote;
     157        /* get and send new one */
     158        //send_noteon(new_pitch,127+(int)floor(curlevel), o->samplerate);
     159        notes->data[0] = new_pitch;
     160        notes->data[1] = 127 + (int)floor(curlevel);
     161        o->curnote = new_pitch;
     162      }
     163    }
     164  } else {
     165    if (o->median) {
     166      if (o->isready > 0)
     167        o->isready++;
     168      if (o->isready == o->median)
     169      {
     170        /* kill old note */
     171        //send_noteon(curnote,0);
     172        notes->data[2] = o->curnote;
     173        notes->data[3] = 0;
     174        o->newnote = aubio_notes_get_latest_note(o);
     175        o->curnote = o->newnote;
     176        /* get and send new one */
     177        if (o->curnote>45){
     178          //send_noteon(curnote,127+(int)floor(curlevel));
     179          notes->data[0] = o->curnote;
     180          notes->data[1] = 127 + (int) floor(curlevel);
     181        }
     182      }
     183    } // if median
     184  }
     185}
     186
    78187void del_aubio_notes (aubio_notes_t *o) {
    79188  if (o->note_buffer) del_fvec(o->note_buffer);
    80189  if (o->note_buffer2) del_fvec(o->note_buffer2);
     190  if (o->pitch_output) del_fvec(o->pitch_output);
     191  if (o->pitch) del_aubio_pitch(o->pitch);
     192  if (o->onset_output) del_fvec(o->onset_output);
     193  if (o->onset) del_aubio_onset(o->onset);
    81194  AUBIO_FREE(o);
    82195}
Note: See TracChangeset for help on using the changeset viewer.