shithub: aubio

Download patch

ref: ce323e4845b423ddbc4a73229eaa7abcf73e39d6
parent: 1cf031a3a5b869368562b1251419fd45191eaa53
parent: 0f5f40bbd31550e8b1b8b3019c94fdf5c2a3dd20
author: Paul Brossier <piem@piem.org>
date: Mon Nov 26 06:35:06 EST 2018

Merge branch 'fix/onset_nullptr' (thanks to @niugx)

--- a/src/onset/onset.c
+++ b/src/onset/onset.c
@@ -256,11 +256,14 @@
   o->pv = new_aubio_pvoc(buf_size, o->hop_size);
   o->pp = new_aubio_peakpicker();
   o->od = new_aubio_specdesc(onset_mode,buf_size);
-  if (o->od == NULL) goto beach_specdesc;
   o->fftgrain = new_cvec(buf_size);
   o->desc = new_fvec(1);
   o->spectral_whitening = new_aubio_spectral_whitening(buf_size, hop_size, samplerate);
 
+  if (!o->pv || !o->pp || !o->od || !o->fftgrain
+      || !o->desc || !o->spectral_whitening)
+    goto beach;
+
   /* initialize internal variables */
   aubio_onset_set_default_parameters (o, onset_mode);
 
@@ -267,11 +270,8 @@
   aubio_onset_reset(o);
   return o;
 
-beach_specdesc:
-  del_aubio_peakpicker(o->pp);
-  del_aubio_pvoc(o->pv);
 beach:
-  AUBIO_FREE(o);
+  del_aubio_onset(o);
   return NULL;
 }
 
@@ -339,11 +339,17 @@
 
 void del_aubio_onset (aubio_onset_t *o)
 {
-  del_aubio_spectral_whitening(o->spectral_whitening);
-  del_aubio_specdesc(o->od);
-  del_aubio_peakpicker(o->pp);
-  del_aubio_pvoc(o->pv);
-  del_fvec(o->desc);
-  del_cvec(o->fftgrain);
+  if (o->spectral_whitening)
+    del_aubio_spectral_whitening(o->spectral_whitening);
+  if (o->od)
+    del_aubio_specdesc(o->od);
+  if (o->pp)
+    del_aubio_peakpicker(o->pp);
+  if (o->pv)
+    del_aubio_pvoc(o->pv);
+  if (o->desc)
+    del_fvec(o->desc);
+  if (o->fftgrain)
+    del_cvec(o->fftgrain);
   AUBIO_FREE(o);
 }
--- a/tests/src/onset/test-onset.c
+++ b/tests/src/onset/test-onset.c
@@ -1,13 +1,20 @@
 #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_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] [hop_size]\n", argv[0]);
     return err;
   }
@@ -59,4 +66,38 @@
   aubio_cleanup();
 
   return err;
+}
+
+int test_wrong_params(void)
+{
+  uint_t win_size = 1024;
+  uint_t hop_size = win_size / 2;
+  uint_t samplerate = 44100;
+  // hop_size < 1
+  if (new_aubio_onset("default", 5, 0, samplerate))
+    return 1;
+  // buf_size < 2
+  if (new_aubio_onset("default", 1, 1, samplerate))
+    return 1;
+  // buf_size < hop_size
+  if (new_aubio_onset("default", hop_size, win_size, samplerate))
+    return 1;
+  // samplerate < 1
+  if (new_aubio_onset("default", 1024, 512, 0))
+    return 1;
+
+  // specdesc creation failed
+  if (new_aubio_onset("abcd", win_size, win_size/2, samplerate))
+    return 1;
+  // pv creation failed
+  if (new_aubio_onset("default", 5, 2, samplerate))
+    return 1;
+
+  aubio_onset_t *o;
+  o = new_aubio_onset("default", win_size, hop_size, samplerate);
+  if (!aubio_onset_set_default_parameters(o, "wrong_type"))
+    return 1;
+  del_aubio_onset(o);
+
+  return 0;
 }