examples/: add time format option
[aubio.git] / examples / utils.c
index 378cffa..2cb87d2 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
+char_t * time_format = "seconds";
 // tempo stuff
 char_t * tempo_method = "default";
 // more general stuff
@@ -196,9 +198,22 @@ send_noteon (int pitch, int velo)
   } else
 #endif
   if (velo == 0) {
-    outmsg ("%f\n", blocks * hop_size / (float) samplerate);
+    print_time (blocks * hop_size);
+    outmsg ("\n");
   } else {
-    outmsg ("%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 (strcmp (time_format, "samples") == 0) {
+    outmsg ("%d", time_in_samples);
+  } else if (strcmp (time_format, "ms") == 0) {
+    outmsg ("%f", 1000. * time_in_samples / (float) samplerate);
+  } else {
+    outmsg ("%f", time_in_samples / (float) samplerate);
+  }
+}