[tests] run some tests in tempo if no arguments passed
[aubio.git] / tests / src / tempo / test-tempo.c
index 7e32f71..87da22d 100644 (file)
@@ -1,22 +1,30 @@
 #include <aubio.h>
 #include "utils_tests.h"
 
+int test_wrong_params(void);
+
 int main (int argc, char **argv)
 {
   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]);
+    PRINT_WRN("no arguments, running tests\n");
+    if (test_wrong_params() != 0) {
+      PRINT_ERR("tests failed!\n");
+      err = 1;
+    } else {
+      err = 0;
+    }
+    PRINT_MSG("usage: %s <source_path> [samplerate] [win_size] [hop_size]\n", argv[0]);
     return err;
   }
   uint_t samplerate = 0;
-  uint_t win_s = 1024; // window size
-  uint_t hop_size = win_s / 4;
+  if ( argc >= 3 ) samplerate = atoi(argv[2]);
+  uint_t win_size = 1024; // window size
+  if ( argc >= 4 ) win_size = atoi(argv[3]);
+  uint_t hop_size = win_size / 4;
+  if ( argc >= 5 ) hop_size = atoi(argv[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);
@@ -26,10 +34,12 @@ int main (int argc, char **argv)
 
   // create some vectors
   fvec_t * in = new_fvec (hop_size); // input audio buffer
-  fvec_t * out = new_fvec (2); // output position
+  fvec_t * out = new_fvec (1); // output position
 
   // create tempo object
-  aubio_tempo_t * o = new_aubio_tempo("default", win_s, hop_size, samplerate);
+  aubio_tempo_t * o = new_aubio_tempo("default", win_size, hop_size, samplerate);
+
+  if (!o) { err = 1; goto beach_tempo; }
 
   do {
     // put some fresh data in input vector
@@ -52,6 +62,7 @@ int main (int argc, char **argv)
 
   // clean up memory
   del_aubio_tempo(o);
+beach_tempo:
   del_fvec(in);
   del_fvec(out);
   del_aubio_source(source);
@@ -60,3 +71,63 @@ beach:
 
   return err;
 }
+
+int test_wrong_params(void)
+{
+  uint_t win_size = 1024;
+  uint_t hop_size = 256;
+  uint_t samplerate = 44100;
+  aubio_tempo_t *t;
+  fvec_t* in, *out;
+  uint_t i;
+
+  // test wrong method fails
+  if (new_aubio_tempo("unexisting_method", win_size, hop_size, samplerate))
+    return 1;
+
+  // test hop > win fails
+  if (new_aubio_tempo("default", hop_size, win_size, samplerate))
+    return 1;
+
+  // test null hop_size fails
+  if (new_aubio_tempo("default", win_size, 0, samplerate))
+    return 1;
+
+  // test 1 buf_size fails
+  if (new_aubio_tempo("default", 1, 1, samplerate))
+    return 1;
+
+  // test null samplerate fails
+  if (new_aubio_tempo("default", win_size, hop_size, 0))
+    return 1;
+
+  // test short sizes workaround
+  t = new_aubio_tempo("default", 2048, 2048, 500);
+  if (!t)
+    return 1;
+
+  del_aubio_tempo(t);
+
+  t = new_aubio_tempo("default", win_size, hop_size, samplerate);
+  if (!t)
+    return 1;
+
+  in = new_fvec(hop_size);
+  out = new_fvec(1);
+
+  for (i = 0; i < 4; i++)
+  {
+    aubio_tempo_do(t,in,out);
+    PRINT_MSG("beat at %.3fms, %.3fs, frame %d, %.2f bpm "
+        "with confidence %.2f, was tatum %d\n",
+        aubio_tempo_get_last_ms(t), aubio_tempo_get_last_s(t),
+        aubio_tempo_get_last(t), aubio_tempo_get_bpm(t),
+        aubio_tempo_get_confidence(t), aubio_tempo_was_tatum(t));
+  }
+
+  del_aubio_tempo(t);
+  del_fvec(in);
+  del_fvec(out);
+
+  return 0;
+}