tests/src/{onset,tempo}/test-*.c: improve test programs
authorPaul Brossier <piem@piem.org>
Tue, 15 Oct 2013 20:46:18 +0000 (22:46 +0200)
committerPaul Brossier <piem@piem.org>
Tue, 15 Oct 2013 20:46:18 +0000 (22:46 +0200)
tests/src/onset/test-onset.c
tests/src/tempo/test-tempo.c

index a454fd7..4500b07 100644 (file)
@@ -1,34 +1,61 @@
 #include <aubio.h>
+#include "utils_tests.h"
 
-int main ()
+int main (int argc, char **argv)
 {
-  // 1. allocate some memory
-  uint_t n = 0; // frame counter
+  uint_t err = 0;
+  if (argc < 2) {
+    err = 2;
+    PRINT_ERR("not enough arguments\n");
+    PRINT_MSG("read a wave file as a mono vector\n");
+    PRINT_MSG("usage: %s <source_path> [samplerate] [hop_size]\n", argv[0]);
+    return err;
+  }
+  uint_t samplerate = 0;
   uint_t win_s = 1024; // window size
-  uint_t hop_s = win_s / 4; // hop size
-  uint_t samplerate = 44100; // samplerate
+  uint_t hop_size = win_s / 4;
+  uint_t n_frames = 0, read = 0;
+  if ( argc == 3 ) samplerate = atoi(argv[2]);
+  if ( argc == 4 ) hop_size = atoi(argv[3]);
+
+  char_t *source_path = argv[1];
+  aubio_source_t * source = new_aubio_source(source_path, samplerate, hop_size);
+  if (!source) { err = 1; goto beach; }
+
+  if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(source);
+
   // create some vectors
-  fvec_t * input = new_fvec (win_s/4); // input buffer
-  fvec_t * out = new_fvec (2); // input buffer
+  fvec_t * in = new_fvec (hop_size); // input audio buffer
+  fvec_t * out = new_fvec (2); // output position
+
   // create onset object
-  aubio_onset_t * onset = new_aubio_onset("complex", win_s, hop_s, samplerate);
-
-  // 2. do something with it
-  while (n < 10) {
-    // get `hop_s` new samples into `input`
-    // ...
-    // exectute onset detection
-    aubio_onset_do (onset, input, out);
-    // do something with output candidates
-    // ...
-    n++;
-  };
-
-  // 3. clean up memory
-  del_aubio_onset(onset);
-  del_fvec(input);
+  aubio_onset_t * o = new_aubio_onset("default", win_s, hop_size, samplerate);
+
+  do {
+    // put some fresh data in input vector
+    aubio_source_do(source, in, &read);
+    // execute onset
+    aubio_onset_do(o,in,out);
+    // do something with the onsets
+    if (out->data[0] != 0) {
+      PRINT_MSG("onset at %.3fms, %.3fs, frame %d\n",
+          aubio_onset_get_last_ms(o), aubio_onset_get_last_s(o),
+          aubio_onset_get_last(o));
+    }
+    n_frames += read;
+  } while ( read == hop_size );
+
+  PRINT_MSG("read %.2fs, %d frames at %dHz (%d blocks) from %s\n",
+      n_frames * 1. / samplerate,
+      n_frames, samplerate,
+      n_frames / hop_size, source_path);
+
+  // clean up memory
+  del_aubio_onset(o);
+  del_fvec(in);
   del_fvec(out);
+beach:
   aubio_cleanup();
 
-  return 0;
+  return err;
 }
index 73d4342..7e32f71 100644 (file)
@@ -1,37 +1,62 @@
 #include <aubio.h>
+#include "utils_tests.h"
 
-int main ()
+int main (int argc, char **argv)
 {
-  uint_t i = 0;
+  uint_t err = 0;
+  if (argc < 2) {
+    err = 2;
+    PRINT_ERR("not enough arguments\n");
+    PRINT_MSG("read a wave file as a mono vector\n");
+    PRINT_MSG("usage: %s <source_path> [samplerate] [hop_size]\n", argv[0]);
+    return err;
+  }
+  uint_t samplerate = 0;
   uint_t win_s = 1024; // window size
-  fvec_t * in = new_fvec (win_s); // input vector
-  fvec_t * out = new_fvec (2); // output beat position
+  uint_t hop_size = win_s / 4;
+  uint_t n_frames = 0, read = 0;
+  if ( argc == 3 ) samplerate = atoi(argv[2]);
+  if ( argc == 4 ) hop_size = atoi(argv[3]);
 
-  // create tempo object
-  aubio_tempo_t * o = new_aubio_tempo("complex", win_s, win_s/4, 44100.);
+  char_t *source_path = argv[1];
+  aubio_source_t * source = new_aubio_source(source_path, samplerate, hop_size);
+  if (!source) { err = 1; goto beach; }
 
-  smpl_t bpm, confidence;
+  if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(source);
 
-  while (i < 1000) {
-    // put some fresh data in input vector
-    // ...
+  // create some vectors
+  fvec_t * in = new_fvec (hop_size); // input audio buffer
+  fvec_t * out = new_fvec (2); // output position
+
+  // create tempo object
+  aubio_tempo_t * o = new_aubio_tempo("default", win_s, hop_size, samplerate);
 
+  do {
+    // put some fresh data in input vector
+    aubio_source_do(source, in, &read);
     // execute tempo
     aubio_tempo_do(o,in,out);
     // do something with the beats
-    // ...
-
-    // get bpm and confidence
-    bpm = aubio_tempo_get_bpm(o);
-    confidence = aubio_tempo_get_confidence(o);
-
-    i++;
-  };
-
+    if (out->data[0] != 0) {
+      PRINT_MSG("beat at %.3fms, %.3fs, frame %d, %.2fbpm with confidence %.2f\n",
+          aubio_tempo_get_last_ms(o), aubio_tempo_get_last_s(o),
+          aubio_tempo_get_last(o), aubio_tempo_get_bpm(o), aubio_tempo_get_confidence(o));
+    }
+    n_frames += read;
+  } while ( read == hop_size );
+
+  PRINT_MSG("read %.2fs, %d frames at %dHz (%d blocks) from %s\n",
+      n_frames * 1. / samplerate,
+      n_frames, samplerate,
+      n_frames / hop_size, source_path);
+
+  // clean up memory
   del_aubio_tempo(o);
   del_fvec(in);
   del_fvec(out);
+  del_aubio_source(source);
+beach:
   aubio_cleanup();
 
-  return 0;
+  return err;
 }