examples/: also emit midi note from aubioonset, thanks to topas-rec (closes #62)
[aubio.git] / examples / utils.c
index 186dc80..39d46bb 100644 (file)
@@ -47,6 +47,8 @@ smpl_t onset_threshold = 0.0; // will be set if != 0.
 char_t * pitch_unit = "default";
 char_t * pitch_method = "default";
 smpl_t pitch_tolerance = 0.0; // will be set if != 0.
+// time stuff
+uint_t time_format = 0; // for "seconds", 1 for "ms", 2 for "samples"
 // tempo stuff
 char_t * tempo_method = "default";
 // more general stuff
@@ -62,6 +64,9 @@ aubio_sink_t *this_sink = NULL;
 fvec_t *ibuf;
 fvec_t *obuf;
 
+smpl_t miditap_note = 69.;
+smpl_t miditap_velo = 65.;
+
 /* settings */
 int blocks = 0;
 
@@ -70,7 +75,7 @@ extern int parse_args (int argc, char **argv);
 
 #if HAVE_JACK
 aubio_jack_t *jack_setup;
-#endif
+#endif /* HAVE_JACK */
 
 void examples_common_init (int argc, char **argv);
 void examples_common_del (void);
@@ -112,7 +117,7 @@ void examples_common_init (int argc, char **argv)
     jack_setup = new_aubio_jack (hop_size, 1, 1, 0, 1);
     samplerate = aubio_jack_get_samplerate (jack_setup);
     source_uri = "jack";
-#endif
+#endif /* HAVE_JACK */
   }
   ibuf = new_fvec (hop_size);
   obuf = new_fvec (hop_size);
@@ -135,19 +140,19 @@ void examples_common_process (aubio_process_func_t process_func,
   uint_t read = 0;
   if (usejack) {
 
-#if HAVE_JACK
+#ifdef HAVE_JACK
     debug ("Jack activation ...\n");
     aubio_jack_activate (jack_setup, process_func);
     debug ("Processing (Ctrl+C to quit) ...\n");
     pause ();
     aubio_jack_close (jack_setup);
-#else
+#else /* HAVE_JACK */
     usage (stderr, 1);
     outmsg ("Compiled without jack output, exiting.\n");
-#endif
+#endif /* HAVE_JACK */
 
   } else {
-    /* phasevoc */
+
     uint_t total_read = 0;
     blocks = 0;
 
@@ -179,7 +184,7 @@ void
 send_noteon (int pitch, int velo)
 {
   smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5);
-#if HAVE_JACK
+#ifdef HAVE_JACK
   jack_midi_event_t ev;
   ev.size = 3;
   ev.buffer = malloc (3 * sizeof (jack_midi_data_t)); // FIXME
@@ -196,9 +201,22 @@ send_noteon (int pitch, int velo)
   } else
 #endif
   if (velo == 0) {
-    verbmsg ("%f\n", blocks * hop_size / (float) samplerate);
+    print_time (blocks * hop_size);
+    outmsg ("\n");
   } else {
-    verbmsg ("%f\t%f\t", mpitch, blocks * hop_size / (float) samplerate);
+    outmsg ("%f\t", mpitch);
+    print_time (blocks * hop_size);
+    outmsg ("\t");
   }
 }
 
+void print_time (uint_t time_in_samples) {
+  /* output times in selected format */
+  if (time_format == 2) {
+    outmsg ("%d", time_in_samples);
+  } else if (time_format == 1) {
+    outmsg ("%f", 1000. * time_in_samples / (float) samplerate);
+  } else {
+    outmsg ("%f", time_in_samples / (float) samplerate);
+  }
+}