shithub: aubio

Download patch

ref: 248da647c4011d8770733f1a2a3bea8b0752aa75
parent: c71aa4417bf3e9bd52acd75c51c7b43193ad285a
author: Paul Brossier <piem@piem.org>
date: Sun Mar 3 08:30:40 EST 2013

tests/src/io: improve examples

--- a/tests/src/io/test-sink.c
+++ b/tests/src/io/test-sink.c
@@ -1,26 +1,41 @@
-#include <stdio.h>
 #include <aubio.h>
-#include "config.h"
+#include "utils_tests.h"
 
-char_t *path = "/home/piem/archives/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav";
-char_t *outpath = "/var/tmp/test.wav";
+int main (int argc, char **argv)
+{
+  sint_t err = 0;
 
-int main(){
-  int err = 0;
+  if (argc < 3) {
+    err = 2;
+    PRINT_ERR("not enough arguments\n");
+    PRINT_MSG("usage: %s <input_path> <output_path> [samplerate]\n", argv[0]);
+    return err;
+  }
+
   uint_t samplerate = 44100;
   uint_t hop_size = 512;
-  uint_t read = hop_size;
+  uint_t n_frames = 0, read = 0;
+
+  char_t *source_path = argv[1];
+  char_t *sink_path = argv[2];
+  if ( argc == 4 ) samplerate = atoi(argv[3]);
+
   fvec_t *vec = new_fvec(hop_size);
-  aubio_source_t * i = new_aubio_source(path, samplerate, hop_size);
-  aubio_sink_t *   o = new_aubio_sink(outpath, samplerate);
+  aubio_source_t *i = new_aubio_source(source_path, samplerate, hop_size);
+  if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(i);
+  aubio_sink_t *o = new_aubio_sink(sink_path, samplerate);
 
-  if (!i || !o) { err = -1; goto beach; }
+  if (!i || !o) { err = 1; goto beach; }
 
-  while ( read == hop_size ) {
+  do {
     aubio_source_do(i, vec, &read);
     aubio_sink_do(o, vec, read);
-  }
+    n_frames += read;
+  } while ( read == hop_size );
 
+  PRINT_MSG("%d frames read from %s\n written to %s at %dHz\n",
+      n_frames, source_path, sink_path, samplerate);
+
 beach:
   del_aubio_source(i);
   del_aubio_sink(o);
@@ -27,4 +42,3 @@
   del_fvec(vec);
   return err;
 }
-
--- a/tests/src/io/test-sink_apple_audio_file.c
+++ b/tests/src/io/test-sink_apple_audio_file.c
@@ -1,34 +1,49 @@
-#include <stdio.h>
 #include <aubio.h>
-#include "config.h"
+#include "utils_tests.h"
 
-char_t *path = "/Users/piem/archives/sounds/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav";
-char_t *outpath = "/var/tmp/test.wav";
+int main (int argc, char **argv)
+{
+  sint_t err = 0;
 
-int main(){
-  int err = 0;
+  if (argc < 3) {
+    err = 2;
+    PRINT_ERR("not enough arguments\n");
+    PRINT_MSG("usage: %s <input_path> <output_path> [samplerate]\n", argv[0]);
+    return err;
+  }
+
 #ifdef __APPLE__
   uint_t samplerate = 44100;
   uint_t hop_size = 512;
-  uint_t read = hop_size;
+  uint_t n_frames = 0, read = 0;
+
+  char_t *source_path = argv[1];
+  char_t *sink_path = argv[2];
+  if ( argc == 4 ) samplerate = atoi(argv[3]);
+
   fvec_t *vec = new_fvec(hop_size);
-  aubio_source_apple_audio_t * i = new_aubio_source_apple_audio(path, samplerate, hop_size);
-  aubio_sink_apple_audio_t *   o = new_aubio_sink_apple_audio(outpath, samplerate);
+  aubio_source_apple_audio_t *i = new_aubio_source_apple_audio(source_path, samplerate, hop_size);
+  if (samplerate == 0 ) samplerate = aubio_source_apple_audio_get_samplerate(i);
+  aubio_sink_apple_audio_t *o = new_aubio_sink_apple_audio(sink_path, samplerate);
 
-  if (!i || !o) { err = -1; goto beach; }
+  if (!i || !o) { err = 1; goto beach; }
 
-  while ( read == hop_size ) {
+  do {
     aubio_source_apple_audio_do(i, vec, &read);
     aubio_sink_apple_audio_do(o, vec, read);
-  }
+    n_frames += read;
+  } while ( read == hop_size );
 
+  PRINT_MSG("%d frames read from %s\n written to %s at %dHz\n",
+      n_frames, source_path, sink_path, samplerate);
+
 beach:
   del_aubio_source_apple_audio(i);
   del_aubio_sink_apple_audio(o);
   del_fvec(vec);
 #else
-  fprintf(stderr, "ERR: aubio was not compiled with aubio_source_apple_audio\n");
+  PRINT_ERR("aubio was not compiled with aubio_source_apple_audio\n");
+  err = 3;
 #endif /* __APPLE__ */
   return err;
 }
-
--- a/tests/src/io/test-sink_sndfile.c
+++ b/tests/src/io/test-sink_sndfile.c
@@ -1,34 +1,49 @@
-#include <stdio.h>
 #include <aubio.h>
-#include "config.h"
+#include "utils_tests.h"
 
-char_t *path = "/home/piem/archives/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav";
-char_t *outpath = "/var/tmp/test.wav";
+int main (int argc, char **argv)
+{
+  sint_t err = 0;
 
-int main(){
-  int err = 0;
+  if (argc < 3) {
+    err = 2;
+    PRINT_ERR("not enough arguments\n");
+    PRINT_MSG("usage: %s <input_path> <output_path> [samplerate]\n", argv[0]);
+    return err;
+  }
+
 #ifdef HAVE_SNDFILE
   uint_t samplerate = 44100;
   uint_t hop_size = 512;
-  uint_t read = hop_size;
+  uint_t n_frames = 0, read = 0;
+
+  char_t *source_path = argv[1];
+  char_t *sink_path = argv[2];
+  if ( argc == 4 ) samplerate = atoi(argv[3]);
+
   fvec_t *vec = new_fvec(hop_size);
   aubio_source_sndfile_t * i = new_aubio_source_sndfile(path, samplerate, hop_size);
+  if (samplerate == 0 ) samplerate = aubio_source_sndfile_get_samplerate(i);
   aubio_sink_sndfile_t *   o = new_aubio_sink_sndfile(outpath, samplerate);
 
-  if (!i || !o) { err = -1; goto beach; }
+  if (!i || !o) { err = 1; goto beach; }
 
-  while ( read == hop_size ) {
+  do {
     aubio_source_sndfile_do(i, vec, &read);
     aubio_sink_sndfile_do(o, vec, read);
-  }
+    n_frames += read;
+  } while ( read == hop_size );
 
+  PRINT_MSG("%d frames read from %s\n written to %s at %dHz\n",
+      n_frames, source_path, sink_path, samplerate);
+
 beach:
   del_aubio_source_sndfile(i);
   del_aubio_sink_sndfile(o);
   del_fvec(vec);
 #else
-  fprintf(stderr, "ERR: aubio was not compiled with aubio_source_sndfile\n");
+  PRINT_ERR("aubio was not compiled with aubio_source_sndfile\n");
+  err = 3;
 #endif /* HAVE_SNDFILE */
   return err;
 }
-
--- a/tests/src/io/test-source.c
+++ b/tests/src/io/test-source.c
@@ -1,25 +1,38 @@
-#include <stdio.h>
 #include <aubio.h>
+#include "utils_tests.h"
 
-char_t *path = "/Users/piem/archives/sounds/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav";
-//char_t *path = "/Users/piem/Downloads/Keziah Jones - Where's Life.mp3";
+int main (int argc, char **argv)
+{
+  uint_t err = 0;
+  if (argc < 2) {
+    err = 2;
+    PRINT_ERR("not enough arguments\n");
+    PRINT_MSG("usage: %s <source_path> [samplerate]\n", argv[0]);
+    return err;
+  }
 
-int main(){
   uint_t samplerate = 32000;
-  uint_t hop_size = 1024;
-  uint_t read = hop_size;
+  uint_t hop_size = 256;
+  uint_t n_frames = 0, read = 0;
+  if ( argc == 3 ) samplerate = atoi(argv[2]);
+
+  char_t *source_path = argv[1];
+
   fvec_t *vec = new_fvec(hop_size);
-  aubio_source_t* s = new_aubio_source(path, samplerate, hop_size);
+  aubio_source_t* s = new_aubio_source(source_path, samplerate, hop_size);
+  if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(s);
 
-  if (!s) return -1;
+  if (!s) { err = 1; goto beach; }
 
-  while ( read == hop_size ) {
+  do {
     aubio_source_do(s, vec, &read);
-    fprintf(stdout, "%d [%f, %f, ..., %f]\n", read, vec->data[0], vec->data[1], vec->data[read - 1]);
-  }
+    // fvec_print (vec);
+    n_frames += read;
+  } while ( read == hop_size );
 
-  del_aubio_source(s);
+beach:
+  del_aubio_source (s);
+  del_fvec (vec);
 
-  return 0;
+  return err;
 }
-
--- a/tests/src/io/test-source_apple_audio_file.c
+++ b/tests/src/io/test-source_apple_audio_file.c
@@ -1,28 +1,42 @@
-#include <stdio.h>
 #include <aubio.h>
+#include "utils_tests.h"
 
-char_t *path = "/Users/piem/archives/sounds/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav";
-//char_t *path = "/Volumes/moving/moving/photos/gopro2/100GOPRO/GOPR4515.MP4";
+int main (int argc, char **argv)
+{
+  uint_t err = 0;
+  if (argc < 2) {
+    err = 2;
+    PRINT_ERR("not enough arguments\n");
+    PRINT_MSG("usage: %s <source_path> [samplerate]\n", argv[0]);
+    return err;
+  }
 
-int main(){
-#ifdef __APPLE__
+#if __APPLE__
   uint_t samplerate = 32000;
-  uint_t hop_size = 1024;
-  uint_t read = hop_size;
+  uint_t hop_size = 256;
+  uint_t n_frames = 0, read = 0;
+  if ( argc == 3 ) samplerate = atoi(argv[2]);
+
+  char_t *source_path = argv[1];
+
   fvec_t *vec = new_fvec(hop_size);
-  aubio_source_apple_audio_t * s = new_aubio_source_apple_audio(path, samplerate, hop_size);
+  aubio_source_apple_audio_t * s = new_aubio_source_apple_audio(source_path, samplerate, hop_size);
+  if (samplerate == 0 ) samplerate = aubio_source_apple_audio_get_samplerate(s);
 
-  if (!s) return -1;
+  if (!s) { err = 1; goto beach; }
 
-  while ( read == hop_size ) {
+  do {
     aubio_source_apple_audio_do(s, vec, &read);
-    fprintf(stdout, "%d [%f, %f, ..., %f]\n", read, vec->data[0], vec->data[1], vec->data[read - 1]);
-  }
+    // fvec_print (vec);
+    n_frames += read;
+  } while ( read == hop_size );
 
-  del_aubio_source_apple_audio(s);
+beach:
+  del_aubio_source_apple_audio (s);
+  del_fvec (vec);
 #else
-  fprintf(stderr, "ERR: aubio was not compiled with aubio_source_apple_audio\n");
+  err = 3;
+  PRINT_ERR("aubio was not compiled with aubio_source_apple_audio\n");
 #endif /* __APPLE__ */
   return 0;
 }
-
--- a/tests/src/io/test-source_sndfile.c
+++ b/tests/src/io/test-source_sndfile.c
@@ -1,33 +1,42 @@
-#include <stdio.h>
 #include <aubio.h>
-#include "config.h"
+#include "utils_tests.h"
 
-char_t *path = "/home/piem/archives/samples/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav";
+int main (int argc, char **argv)
+{
+  uint_t err = 0;
+  if (argc < 2) {
+    err = 2;
+    PRINT_ERR("not enough arguments\n");
+    PRINT_MSG("usage: %s <source_path> [samplerate]\n", argv[0]);
+    return err;
+  }
 
-int main(){
-  int err = 0;
 #ifdef HAVE_SNDFILE
-  uint_t samplerate = 8000;
-  uint_t hop_size = 512;
-  uint_t read = hop_size;
+  uint_t samplerate = 32000;
+  uint_t hop_size = 256;
+  uint_t n_frames = 0, read = 0;
+  if ( argc == 3 ) samplerate = atoi(argv[2]);
+
+  char_t *source_path = argv[1];
+
   fvec_t *vec = new_fvec(hop_size);
-  aubio_source_sndfile_t * s = new_aubio_source_sndfile(path, samplerate, hop_size);
+  aubio_source_sndfile_t * s = new_aubio_source_sndfile(source_path, samplerate, hop_size);
+  if (samplerate == 0 ) samplerate = aubio_source_sndfile_get_samplerate(s);
 
   if (!s) { err = 1; goto beach; }
 
-  while ( read == hop_size ) {
+  do {
     aubio_source_sndfile_do(s, vec, &read);
-    if (read == 0) break;
-    fprintf(stdout, "%d [%f, %f, ..., %f]\n", read, vec->data[0], vec->data[1], vec->data[read - 1]);
-  }
+    // fvec_print (vec);
+    n_frames += read;
+  } while ( read == hop_size );
 
 beach:
-  del_aubio_source_sndfile(s);
-  del_fvec(vec);
+  del_aubio_source_sndfile (s);
+  del_fvec (vec);
 #else
-  fprintf(stderr, "ERR: aubio was not compiled with aubio_source_sndfile\n");
+  PRINT_ERR("aubio was not compiled with aubio_source_sndfile\n");
   err = 2;
 #endif /* HAVE_SNDFILE */
   return err;
 }
-
--- /dev/null
+++ b/tests/utils_tests.h
@@ -1,0 +1,7 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#define PRINT_ERR(format, args...)   fprintf(stderr, "AUBIO ERROR: " format , ##args)
+#define PRINT_MSG(format, args...)   fprintf(stdout, format , ##args)
+#define PRINT_DBG(format, args...)   fprintf(stderr, format , ##args)
+#define PRINT_WRN(...)               fprintf(stderr, "AUBIO WARNING: " format, ##args)