examples/utils.c: allocate note event once
[aubio.git] / examples / utils.c
index 96862af..39adef9 100644 (file)
@@ -43,10 +43,13 @@ uint_t hop_size = 256;
 // onset stuff
 char_t * onset_method = "default";
 smpl_t onset_threshold = 0.0; // will be set if != 0.
+smpl_t onset_minioi = 0.0; // will be set if != 0.
 // pitch stuff
 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 +65,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,10 +76,15 @@ extern int parse_args (int argc, char **argv);
 
 #if HAVE_JACK
 aubio_jack_t *jack_setup;
-#endif
+jack_midi_event_t ev;
+#endif /* HAVE_JACK */
 
-void
-examples_common_init (int argc, char **argv)
+void examples_common_init (int argc, char **argv);
+void examples_common_del (void);
+void examples_common_process (aubio_process_func_t process_func,
+    aubio_print_func_t print);
+
+void examples_common_init (int argc, char **argv)
 {
 
   /* parse command line arguments */
@@ -83,7 +94,7 @@ examples_common_init (int argc, char **argv)
     debug ("Opening files ...\n");
     this_source = new_aubio_source ((char_t*)source_uri, samplerate, hop_size);
     if (this_source == NULL) {
-      outmsg ("Could not open input file %s\n", source_uri);
+      errmsg ("Error: could not open input file %s\n", source_uri);
       exit (1);
     }
     if (samplerate == 0) {
@@ -92,13 +103,13 @@ examples_common_init (int argc, char **argv)
     if (sink_uri != NULL) {
       uint_t sink_exists = (access(sink_uri, F_OK) == 0 );
       if (!force_overwrite && sink_exists) {
-        outmsg ("Output file %s already exists, use -f to overwrite.\n",
+        errmsg ("Error: output file %s already exists, use -f to overwrite.\n",
             sink_uri);
         exit (1);
       }
       this_sink = new_aubio_sink ((char_t*)sink_uri, samplerate);
       if (this_sink == NULL) {
-        outmsg ("Could not open output file %s\n", sink_uri);
+        errmsg ("Error: could not create output file %s\n", sink_uri);
         exit (1);
       }
     }
@@ -108,16 +119,18 @@ 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);
 
 }
 
-void
-examples_common_del (void)
+void examples_common_del (void)
 {
+#ifdef HAVE_JACK
+  if (ev.buffer) free(ev.buffer);
+#endif
   del_fvec (ibuf);
   del_fvec (obuf);
   aubio_cleanup ();
@@ -125,29 +138,31 @@ examples_common_del (void)
   fflush(stdout);
 }
 
-void
-examples_common_process (aubio_process_func_t process_func,
+void examples_common_process (aubio_process_func_t process_func,
     aubio_print_func_t print)
 {
 
   uint_t read = 0;
   if (usejack) {
 
-#if HAVE_JACK
+#ifdef HAVE_JACK
+    ev.size = 3;
+    ev.buffer = malloc (3 * sizeof (jack_midi_data_t));
+    ev.time = 0; // send it now
     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 */
-    blocks = 0;
+
     uint_t total_read = 0;
+    blocks = 0;
 
     do {
       aubio_source_do (this_source, ibuf, &read);
@@ -174,17 +189,12 @@ examples_common_process (aubio_process_func_t process_func,
 }
 
 void
-send_noteon (int pitch, int velo)
+send_noteon (smpl_t pitch, smpl_t velo)
 {
-  smpl_t mpitch = floor (aubio_freqtomidi (pitch) + .5);
-#if HAVE_JACK
-  jack_midi_event_t ev;
-  ev.size = 3;
-  ev.buffer = malloc (3 * sizeof (jack_midi_data_t)); // FIXME
-  ev.time = 0;
+#ifdef HAVE_JACK
   if (usejack) {
     ev.buffer[2] = velo;
-    ev.buffer[1] = mpitch;
+    ev.buffer[1] = pitch;
     if (velo == 0) {
       ev.buffer[0] = 0x80;      /* note off */
     } else {
@@ -194,9 +204,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", pitch);
+    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);
+  }
+}