[tests] add run_on_default_source_and_sink helper
[aubio.git] / examples / aubionotes.c
index 9b09d0b..339fdaf 100644 (file)
 
 */
 
-#define AUBIO_UNSTABLE 1 // for fvec_median
 #include "utils.h"
 #define PROG_HAS_PITCH 1
 #define PROG_HAS_ONSET 1
+#define PROG_HAS_NOTES 1
+#define PROG_HAS_SILENCE 1
 #define PROG_HAS_JACK 1
 // TODO add PROG_HAS_OUTPUT
 #include "parse_args.h"
 
-uint_t median = 6;
+aubio_notes_t *notes;
+smpl_t lastmidi = 0.;
 
-fvec_t *note_buffer;
-fvec_t *note_buffer2;
-
-smpl_t curnote = 0.;
-smpl_t newnote = 0.;
-uint_t isready = 0;
-
-aubio_pitch_t *pitch;
-aubio_onset_t *o;
-fvec_t *onset;
-fvec_t *pitch_obuf;
-
-/** append new note candidate to the note_buffer and return filtered value. we
- * need to copy the input array as fvec_median destroy its input data.*/
-void note_append (fvec_t * note_buffer, smpl_t curnote);
-uint_t get_note (fvec_t * note_buffer, fvec_t * note_buffer2);
-
-static void
-process_block(fvec_t *ibuf, fvec_t *obuf) {
-  fvec_zeros(obuf);
-  aubio_onset_do(o, ibuf, onset);
-
-  aubio_pitch_do (pitch, ibuf, pitch_obuf);
-  smpl_t new_pitch = fvec_get_sample(pitch_obuf, 0);
-  if(median){
-    note_append(note_buffer, new_pitch);
-  }
-
-  /* curlevel is negatif or 1 if silence */
-  smpl_t curlevel = aubio_level_detection(ibuf, silence_threshold);
-  if (fvec_get_sample(onset, 0)) {
-    /* test for silence */
-    if (curlevel == 1.) {
-      if (median) isready = 0;
-      /* send note off */
-      send_noteon(curnote,0);
-    } else {
-      if (median) {
-        isready = 1;
-      } else {
-        /* kill old note */
-        send_noteon(curnote,0);
-        /* get and send new one */
-        send_noteon(new_pitch,127+(int)floor(curlevel));
-        curnote = new_pitch;
-      }
-    }
-  } else {
-    if (median) {
-      if (isready > 0)
-        isready++;
-      if (isready == median)
-      {
-        /* kill old note */
-        send_noteon(curnote,0);
-        newnote = get_note(note_buffer, note_buffer2);
-        curnote = newnote;
-        /* get and send new one */
-        if (curnote>45){
-          send_noteon(curnote,127+(int)floor(curlevel));
-        }
-      }
-    } // if median
-  }
-}
-
-static void
-process_print (void) {
-  //if (verbose) outmsg("%f\n",pitch_obuf->data[0]);
-}
-
-void
-note_append (fvec_t * note_buffer, smpl_t curnote)
+void process_block (fvec_t *ibuf, fvec_t *obuf)
 {
-  uint_t i = 0;
-  for (i = 0; i < note_buffer->length - 1; i++) {
-    note_buffer->data[i] = note_buffer->data[i + 1];
+  aubio_notes_do (notes, ibuf, obuf);
+  // did we get a note off?
+  if (obuf->data[2] != 0) {
+    lastmidi = obuf->data[2];
+    send_noteon(lastmidi, 0);
+  }
+  // did we get a note on?
+  if (obuf->data[0] != 0) {
+    lastmidi = obuf->data[0];
+    send_noteon(lastmidi, obuf->data[1]);
   }
-  note_buffer->data[note_buffer->length - 1] = curnote;
-  return;
 }
 
-uint_t
-get_note (fvec_t * note_buffer, fvec_t * note_buffer2)
+void process_print (void)
 {
-  uint_t i;
-  for (i = 0; i < note_buffer->length; i++) {
-    note_buffer2->data[i] = note_buffer->data[i];
-  }
-  return fvec_median (note_buffer2);
+  //if (verbose) outmsg("%f\n",pitch_obuf->data[0]);
 }
 
 int main(int argc, char **argv) {
+  int ret = 0;
+
   examples_common_init(argc,argv);
 
   verbmsg ("using source: %s at %dHz\n", source_uri, samplerate);
@@ -135,32 +67,39 @@ int main(int argc, char **argv) {
   verbmsg ("hop_size: %d, ", hop_size);
   verbmsg ("tolerance: %f\n", pitch_tolerance);
 
-  o = new_aubio_onset (onset_method, buffer_size, hop_size, samplerate);
-  if (onset_threshold != 0.) aubio_onset_set_threshold (o, onset_threshold);
-  onset = new_fvec (1);
+  notes = new_aubio_notes ("default", buffer_size, hop_size, samplerate);
+  if (notes == NULL) { ret = 1; goto beach; }
 
-  pitch = new_aubio_pitch (pitch_method, buffer_size * 4, hop_size, samplerate);
-  if (pitch_tolerance != 0.) aubio_pitch_set_tolerance (pitch, pitch_tolerance);
-  pitch_obuf = new_fvec (1);
-
-  if (median) {
-      note_buffer = new_fvec (median);
-      note_buffer2 = new_fvec (median);
+  if (onset_minioi != 0.) {
+    aubio_notes_set_minioi_ms(notes, onset_minioi);
+  }
+  if (onset_threshold != 0.) {
+    errmsg ("warning: onset threshold not supported yet\n");
+    //aubio_onset_set_threshold(aubio_notes_get_aubio_onset(o), onset_threshold);
+  }
+  if (silence_threshold != -90.) {
+    if (aubio_notes_set_silence (notes, silence_threshold) != 0) {
+      errmsg ("failed setting notes silence threshold to %.2f\n",
+          silence_threshold);
+    }
+  }
+  if (release_drop != 10.) {
+    if (aubio_notes_set_release_drop (notes, release_drop) != 0) {
+      errmsg ("failed setting notes release drop to %.2f\n",
+          release_drop);
+    }
   }
 
   examples_common_process((aubio_process_func_t)process_block, process_print);
 
-  // send a last note off
-  send_noteon (curnote, 0);
-
-  del_aubio_pitch (pitch);
-  if (median) {
-      del_fvec (note_buffer);
-      del_fvec (note_buffer2);
+  // send a last note off if required
+  if (lastmidi) {
+    send_noteon (lastmidi, 0);
   }
-  del_fvec (pitch_obuf);
 
+  del_aubio_notes (notes);
+
+beach:
   examples_common_del();
-  return 0;
+  return ret;
 }
-