shithub: aubio

Download patch

ref: 7e35b375ba2022da23a46afda00d575aaa5c9206
parent: abd326c73a577a832b2f51106e80098e4b11034b
author: Paul Brossier <piem@piem.org>
date: Sun Mar 3 09:09:48 EST 2013

tests/src/: improve examples

--- a/tests/src/test-cvec.c
+++ b/tests/src/test-cvec.c
@@ -1,11 +1,39 @@
 #include <aubio.h>
+#include "utils_tests.h"
 
-int main(){
-        /* allocate some memory */
-        uint_t win_s      = 1024;                       /* window size */
-        cvec_t * sp       = new_cvec (win_s); /* input buffer */
-        del_cvec(sp);
+int main ()
+{
+  uint_t i, window_size = 16; // window size
+  utils_init_random();
+  cvec_t * complex_vector = new_cvec (window_size); // input buffer
+  uint_t rand_times = 4;
 
-        return 0;
-}
+  while (rand_times -- ) {
+    // fill with random phas and norm
+    for ( i = 0; i < complex_vector->length; i++ ) {
+      complex_vector->norm[i] = ( 2. / RAND_MAX * random() - 1. );
+      complex_vector->phas[i] = ( 2. / RAND_MAX * random() - 1. ) * M_PI;
+    }
+    // print the vector
+    cvec_print(complex_vector);
+  }
 
+  // set all vector elements to `0`
+  cvec_zeros(complex_vector);
+  for ( i = 0; i < complex_vector->length; i++ ) {
+    assert( complex_vector->norm[i] == 0. );
+    // assert( complex_vector->phas[i] == 0 );
+  }
+  cvec_print(complex_vector);
+
+  // set all vector elements to `1`
+  cvec_ones(complex_vector);
+  for ( i = 0; i < complex_vector->length; i++ ) {
+    assert( complex_vector->norm[i] == 1. );
+    // assert( complex_vector->phas[i] == 0 );
+  }
+  cvec_print(complex_vector);
+  // destroy it
+  del_cvec(complex_vector);
+  return 0;
+}
--- /dev/null
+++ b/tests/src/test-delnull.c
@@ -1,0 +1,10 @@
+#include <stdlib.h>
+#include <aubio.h>
+
+int main ()
+{
+  del_fvec(NULL);
+  del_lvec(NULL);
+  del_cvec(NULL);
+  return 0;
+}
--- a/tests/src/test-fmat.c
+++ b/tests/src/test-fmat.c
@@ -1,11 +1,26 @@
 #include <aubio.h>
+#include <assert.h>
 
-int main(){
-        uint_t length = 1024;                     /* length */
-        uint_t height = 1024;                     /* height */
-        fmat_t * mat = new_fmat (length, height); /* input buffer */
-        fmat_print(mat);
-        del_fmat(mat);
-        return 0;
+// create a new matrix and fill it with i * 1. + j * .1, where i is the row,
+// and j the column.
+
+int main ()
+{
+  uint_t height = 3, length = 9, i, j;
+  // create fmat_t object
+  fmat_t * mat = new_fmat (length, height);
+  for ( i = 0; i < mat->height; i++ ) {
+    for ( j = 0; j < mat->length; j++ ) {
+      // all elements are already initialized to 0.
+      assert(mat->data[i][j] == 0);
+      // setting element of row i, column j
+      mat->data[i][j] = i * 1. + j *.1;
+    }
+  }
+  // print out matrix
+  fmat_print(mat);
+  // destroy it
+  del_fmat(mat);
+  return 0;
 }
 
--- a/tests/src/test-fvec.c
+++ b/tests/src/test-fvec.c
@@ -1,20 +1,42 @@
 #include <aubio.h>
 #include <assert.h>
 
-int main(){
-  uint_t buffer_size = 1024;
-  fvec_t * in = new_fvec (buffer_size);
+int main ()
+{
+  uint_t vec_size = 10, i;
+  fvec_t * vec = new_fvec (vec_size);
 
-  assert( in->length                == buffer_size);
+  // vec->length matches requested size
+  assert(vec->length == vec_size);
 
-  assert( in->data[0]               == 0);
-  assert( in->data[buffer_size / 2] == 0);
-  assert( in->data[buffer_size - 1] == 0);
+  // all elements are initialized to `0.`
+  for ( i = 0; i < vec->length; i++ ) {
+    assert(vec->data[i] == 0.);
+  }
 
-  in->data[buffer_size -1 ] = 1;
-  assert( in->data[buffer_size - 1] == 1);
+  // all elements can be set to `0.`
+  fvec_zeros(vec);
+  for ( i = 0; i < vec->length; i++ ) {
+    assert(vec->data[i] == 0.);
+  }
+  fvec_print(vec);
 
-  del_fvec(in);
+  // all elements can be set to `1.`
+  fvec_ones(vec);
+  for ( i = 0; i < vec->length; i++ ) {
+    assert(vec->data[i] == 1.);
+  }
+  fvec_print(vec);
+
+  // each element can be accessed directly
+  for ( i = 0; i < vec->length; i++ ) {
+    vec->data[i] = i;
+    assert(vec->data[i] == i);
+  }
+  fvec_print(vec);
+
+  // now destroys the vector
+  del_fvec(vec);
 
   return 0;
 }
--- a/tests/src/test-lvec.c
+++ b/tests/src/test-lvec.c
@@ -1,11 +1,10 @@
 #include <aubio.h>
 
-int main(){
-        /* allocate some memory */
-        uint_t win_s      = 1024;                       /* window size */
-        lvec_t * sp       = new_lvec (win_s); /* input buffer */
-        del_lvec(sp);
-
-        return 0;
+int main()
+{
+  uint_t win_s = 1024; // window size
+  lvec_t * sp = new_lvec (win_s); // input buffer
+  del_lvec(sp);
+  return 0;
 }
 
--- a/tests/src/test-mathutils-window.c
+++ b/tests/src/test-mathutils-window.c
@@ -1,40 +1,31 @@
 #include <aubio.h>
-#include <stdlib.h>
+#include <math.h>
+#include <stdio.h>
 
-int main( )
+int main ()
 {
-  uint_t length;
-  for (length = 2; length <= 5; length++)
-  {
-    fvec_t *t = new_aubio_window("rectangle", length);
-    del_fvec(t);
-    t = new_aubio_window("hamming", length);
-    fvec_print(t);
-    del_fvec(t);
-    t = new_aubio_window("hanning", length);
-    fvec_print(t);
-    del_fvec(t);
-    t = new_aubio_window("hanningz", length);
-    fvec_print(t);
-    del_fvec(t);
-    t = new_aubio_window("blackman", length);
-    fvec_print(t);
-    del_fvec(t);
-    t = new_aubio_window("blackman_harris", length);
-    fvec_print(t);
-    del_fvec(t);
-    t = new_aubio_window("gaussian", length);
-    fvec_print(t);
-    del_fvec(t);
-    t = new_aubio_window("welch", length);
-    fvec_print(t);
-    del_fvec(t);
-    t = new_aubio_window("parzen", length);
-    fvec_print(t);
-    del_fvec(t);
-    t = new_aubio_window("default", length);
-    fvec_print(t);
-    del_fvec(t);
+  uint_t length = 0;
+  uint_t n_length = 4, n_types = 10, i, t;
+  uint_t lengths[4] = { 8, 10, 15, 16 };
+  char *method = "default";
+  char *window_types[10] = { "default",
+    "rectangle", "hamming", "hanning", "hanningz",
+    "blackman", "blackman_harris", "gaussian", "welch", "parzen"};
+
+  for ( t = 0; t < n_types; t ++ ) {
+    for ( i = 0; i < n_length; i++)
+    {
+      length = lengths[i];
+      method = window_types[t];
+
+      fvec_t * window = new_aubio_window(method, length);
+
+      fvec_set_window(window, method);
+      fprintf(stdout, "length: %d, method: %s, window:, ", length, method);
+      fvec_print(window);
+
+      del_fvec(window);
+    }
   }
   return 0;
 }
--- a/tests/src/test-mathutils.c
+++ b/tests/src/test-mathutils.c
@@ -3,21 +3,113 @@
 #define AUBIO_UNSTABLE 1
 #include <aubio.h>
 
-int main(){
+int test_next_power_of_two()
+{
   uint_t a, b;
+  a = 15; b = aubio_next_power_of_two(a); assert(b == 16);
+  fprintf(stdout, "aubio_next_power_of_two(%d) = %d\n", a, b);
 
-  a = 31; b = aubio_next_power_of_two(a);
-  fprintf(stdout, "next_power_of_two of %d is %d\n", a, b);
-  assert(b == 32);
+  a = 17; b = aubio_next_power_of_two(a); assert(b == 32);
+  fprintf(stdout, "aubio_next_power_of_two(%d) = %d\n", a, b);
 
-  a = 32; b = aubio_next_power_of_two(a);
-  fprintf(stdout, "next_power_of_two of %d is %d\n", a, b);
-  assert(b == 32);
+  a = 31; b = aubio_next_power_of_two(a); assert(b == 32);
+  fprintf(stdout, "aubio_next_power_of_two(%d) = %d\n", a, b);
 
-  a = 33; b = aubio_next_power_of_two(a);
-  fprintf(stdout, "next_power_of_two of %d is %d\n", a, b);
-  assert(b == 64);
+  a = 32; b = aubio_next_power_of_two(a); assert(b == 32);
+  fprintf(stdout, "aubio_next_power_of_two(%d) = %d\n", a, b);
 
+  a = 33; b = aubio_next_power_of_two(a); assert(b == 64);
+  fprintf(stdout, "aubio_next_power_of_two(%d) = %d\n", a, b);
+
   return 0;
 }
 
+int test_miditofreq()
+{
+  smpl_t midi, freq;
+  for ( midi = 0; midi < 128; midi += 3 ) {
+    freq = aubio_miditofreq(midi);
+    fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
+  }
+  midi = 69.5;
+  freq = aubio_miditofreq(midi);
+  fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
+  midi = -69.5;
+  freq = aubio_miditofreq(midi);
+  fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
+  midi = -169.5;
+  freq = aubio_miditofreq(midi);
+  fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
+  midi = 140.;
+  freq = aubio_miditofreq(midi);
+  fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
+  midi = 0;
+  freq = aubio_miditofreq(midi);
+  fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
+  midi = 8.2e10;
+  freq = aubio_miditofreq(midi);
+  fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
+  midi = -5.e10;
+  freq = aubio_miditofreq(midi);
+  fprintf(stdout, "aubio_miditofreq(%.2f) = %.2f\n", midi, freq);
+  return 0;
+}
+
+int test_freqtomidi()
+{
+  smpl_t midi, freq;
+  for ( freq = 0.; freq < 30000.; freq += 440. ) {
+    midi = aubio_freqtomidi(freq);
+    fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
+  }
+  freq = 69.5;
+  midi = aubio_freqtomidi(freq);
+  fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
+  freq = -69.5;
+  midi = aubio_freqtomidi(freq);
+  fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
+  freq = -169.5;
+  midi = aubio_freqtomidi(freq);
+  fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
+  freq = 140.;
+  midi = aubio_freqtomidi(freq);
+  fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
+  freq = 0;
+  midi = aubio_freqtomidi(freq);
+  fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
+  freq = 8.2e10;
+  midi = aubio_freqtomidi(freq);
+  fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
+  freq = -5.;
+  midi = aubio_freqtomidi(freq);
+  fprintf(stdout, "aubio_freqtomidi(%.2f) = %.2f\n", freq, midi);
+  return 0;
+}
+
+int test_aubio_window()
+{
+  uint_t window_size = 16;
+  fvec_t * window = new_aubio_window("default", window_size);
+  del_fvec(window);
+
+  window = new_fvec(window_size);
+  fvec_set_window(window, "rectangle");
+  fvec_print(window);
+
+  window_size /= 2.;
+  window = new_aubio_window("triangle", window_size);
+  fvec_print(window);
+  del_fvec(window);
+
+  window = new_aubio_window("rectangle", 16);
+  del_fvec (window);
+  return 0;
+}
+
+int main ()
+{
+  test_next_power_of_two();
+  test_miditofreq();
+  test_freqtomidi();
+  return 0;
+}