Merge branch 'master' into awhitening
[aubio.git] / src / onset / onset.c
index f416ef8..cdd539a 100644 (file)
@@ -51,7 +51,7 @@ struct _aubio_onset_t {
 };
 
 /* execute onset detection function on iput buffer */
 };
 
 /* execute onset detection function on iput buffer */
-void aubio_onset_do (aubio_onset_t *o, fvec_t * input, fvec_t * onset)
+void aubio_onset_do (aubio_onset_t *o, const fvec_t * input, fvec_t * onset)
 {
   smpl_t isonset = 0;
   aubio_pvoc_do (o->pv,input, o->fftgrain);
 {
   smpl_t isonset = 0;
   aubio_pvoc_do (o->pv,input, o->fftgrain);
@@ -99,17 +99,17 @@ void aubio_onset_do (aubio_onset_t *o, fvec_t * input, fvec_t * onset)
   return;
 }
 
   return;
 }
 
-uint_t aubio_onset_get_last (aubio_onset_t *o)
+uint_t aubio_onset_get_last (const aubio_onset_t *o)
 {
   return o->last_onset - o->delay;
 }
 
 {
   return o->last_onset - o->delay;
 }
 
-smpl_t aubio_onset_get_last_s (aubio_onset_t *o)
+smpl_t aubio_onset_get_last_s (const aubio_onset_t *o)
 {
   return aubio_onset_get_last (o) / (smpl_t) (o->samplerate);
 }
 
 {
   return aubio_onset_get_last (o) / (smpl_t) (o->samplerate);
 }
 
-smpl_t aubio_onset_get_last_ms (aubio_onset_t *o)
+smpl_t aubio_onset_get_last_ms (const aubio_onset_t *o)
 {
   return aubio_onset_get_last_s (o) * 1000.;
 }
 {
   return aubio_onset_get_last_s (o) * 1000.;
 }
@@ -130,7 +130,7 @@ uint_t aubio_onset_set_silence(aubio_onset_t * o, smpl_t silence) {
   return AUBIO_OK;
 }
 
   return AUBIO_OK;
 }
 
-smpl_t aubio_onset_get_silence(aubio_onset_t * o) {
+smpl_t aubio_onset_get_silence(const aubio_onset_t * o) {
   return o->silence;
 }
 
   return o->silence;
 }
 
@@ -139,7 +139,7 @@ uint_t aubio_onset_set_threshold(aubio_onset_t * o, smpl_t threshold) {
   return AUBIO_OK;
 }
 
   return AUBIO_OK;
 }
 
-smpl_t aubio_onset_get_threshold(aubio_onset_t * o) {
+smpl_t aubio_onset_get_threshold(const aubio_onset_t * o) {
   return aubio_peakpicker_get_threshold(o->pp);
 }
 
   return aubio_peakpicker_get_threshold(o->pp);
 }
 
@@ -148,15 +148,15 @@ uint_t aubio_onset_set_minioi(aubio_onset_t * o, uint_t minioi) {
   return AUBIO_OK;
 }
 
   return AUBIO_OK;
 }
 
-uint_t aubio_onset_get_minioi(aubio_onset_t * o) {
+uint_t aubio_onset_get_minioi(const aubio_onset_t * o) {
   return o->minioi;
 }
 
 uint_t aubio_onset_set_minioi_s(aubio_onset_t * o, smpl_t minioi) {
   return o->minioi;
 }
 
 uint_t aubio_onset_set_minioi_s(aubio_onset_t * o, smpl_t minioi) {
-  return aubio_onset_set_minioi (o, minioi * o->samplerate);
+  return aubio_onset_set_minioi (o, (uint_t)ROUND(minioi * o->samplerate));
 }
 
 }
 
-smpl_t aubio_onset_get_minioi_s(aubio_onset_t * o) {
+smpl_t aubio_onset_get_minioi_s(const aubio_onset_t * o) {
   return aubio_onset_get_minioi (o) / (smpl_t) o->samplerate;
 }
 
   return aubio_onset_get_minioi (o) / (smpl_t) o->samplerate;
 }
 
@@ -164,7 +164,7 @@ uint_t aubio_onset_set_minioi_ms(aubio_onset_t * o, smpl_t minioi) {
   return aubio_onset_set_minioi_s (o, minioi / 1000.);
 }
 
   return aubio_onset_set_minioi_s (o, minioi / 1000.);
 }
 
-smpl_t aubio_onset_get_minioi_ms(aubio_onset_t * o) {
+smpl_t aubio_onset_get_minioi_ms(const aubio_onset_t * o) {
   return aubio_onset_get_minioi_s (o) * 1000.;
 }
 
   return aubio_onset_get_minioi_s (o) * 1000.;
 }
 
@@ -173,7 +173,7 @@ uint_t aubio_onset_set_delay(aubio_onset_t * o, uint_t delay) {
   return AUBIO_OK;
 }
 
   return AUBIO_OK;
 }
 
-uint_t aubio_onset_get_delay(aubio_onset_t * o) {
+uint_t aubio_onset_get_delay(const aubio_onset_t * o) {
   return o->delay;
 }
 
   return o->delay;
 }
 
@@ -181,7 +181,7 @@ uint_t aubio_onset_set_delay_s(aubio_onset_t * o, smpl_t delay) {
   return aubio_onset_set_delay (o, delay * o->samplerate);
 }
 
   return aubio_onset_set_delay (o, delay * o->samplerate);
 }
 
-smpl_t aubio_onset_get_delay_s(aubio_onset_t * o) {
+smpl_t aubio_onset_get_delay_s(const aubio_onset_t * o) {
   return aubio_onset_get_delay (o) / (smpl_t) o->samplerate;
 }
 
   return aubio_onset_get_delay (o) / (smpl_t) o->samplerate;
 }
 
@@ -189,21 +189,21 @@ uint_t aubio_onset_set_delay_ms(aubio_onset_t * o, smpl_t delay) {
   return aubio_onset_set_delay_s (o, delay / 1000.);
 }
 
   return aubio_onset_set_delay_s (o, delay / 1000.);
 }
 
-smpl_t aubio_onset_get_delay_ms(aubio_onset_t * o) {
+smpl_t aubio_onset_get_delay_ms(const aubio_onset_t * o) {
   return aubio_onset_get_delay_s (o) * 1000.;
 }
 
   return aubio_onset_get_delay_s (o) * 1000.;
 }
 
-smpl_t aubio_onset_get_descriptor(aubio_onset_t * o) {
+smpl_t aubio_onset_get_descriptor(const aubio_onset_t * o) {
   return o->desc->data[0];
 }
 
   return o->desc->data[0];
 }
 
-smpl_t aubio_onset_get_thresholded_descriptor(aubio_onset_t * o) {
+smpl_t aubio_onset_get_thresholded_descriptor(const aubio_onset_t * o) {
   fvec_t * thresholded = aubio_peakpicker_get_thresholded_input(o->pp);
   return thresholded->data[0];
 }
 
 /* Allocate memory for an onset detection */
   fvec_t * thresholded = aubio_peakpicker_get_thresholded_input(o->pp);
   return thresholded->data[0];
 }
 
 /* Allocate memory for an onset detection */
-aubio_onset_t * new_aubio_onset (char_t * onset_mode, 
+aubio_onset_t * new_aubio_onset (const char_t * onset_mode,
     uint_t buf_size, uint_t hop_size, uint_t samplerate)
 {
   aubio_onset_t * o = AUBIO_NEW(aubio_onset_t);
     uint_t buf_size, uint_t hop_size, uint_t samplerate)
 {
   aubio_onset_t * o = AUBIO_NEW(aubio_onset_t);
@@ -212,11 +212,11 @@ aubio_onset_t * new_aubio_onset (char_t * onset_mode,
   if ((sint_t)hop_size < 1) {
     AUBIO_ERR("onset: got hop_size %d, but can not be < 1\n", hop_size);
     goto beach;
   if ((sint_t)hop_size < 1) {
     AUBIO_ERR("onset: got hop_size %d, but can not be < 1\n", hop_size);
     goto beach;
-  } else if ((sint_t)buf_size < 1) {
-    AUBIO_ERR("onset: got buffer_size %d, but can not be < 1\n", buf_size);
+  } else if ((sint_t)buf_size < 2) {
+    AUBIO_ERR("onset: got buffer_size %d, but can not be < 2\n", buf_size);
     goto beach;
   } else if (buf_size < hop_size) {
     goto beach;
   } else if (buf_size < hop_size) {
-    AUBIO_ERR("onset: hop size (%d) is larger than win size (%d)\n", buf_size, hop_size);
+    AUBIO_ERR("onset: hop size (%d) is larger than win size (%d)\n", hop_size, buf_size);
     goto beach;
   } else if ((sint_t)samplerate < 1) {
     AUBIO_ERR("onset: samplerate (%d) can not be < 1\n", samplerate);
     goto beach;
   } else if ((sint_t)samplerate < 1) {
     AUBIO_ERR("onset: samplerate (%d) can not be < 1\n", samplerate);
@@ -231,6 +231,7 @@ aubio_onset_t * new_aubio_onset (char_t * onset_mode,
   o->pv = new_aubio_pvoc(buf_size, o->hop_size);
   o->pp = new_aubio_peakpicker();
   o->od = new_aubio_specdesc(onset_mode,buf_size);
   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->fftgrain = new_cvec(buf_size);
   o->desc = new_fvec(1);
 
@@ -243,6 +244,9 @@ aubio_onset_t * new_aubio_onset (char_t * onset_mode,
   o->total_frames = 0;
   return o;
 
   o->total_frames = 0;
   return o;
 
+beach_specdesc:
+  del_aubio_peakpicker(o->pp);
+  del_aubio_pvoc(o->pv);
 beach:
   AUBIO_FREE(o);
   return NULL;
 beach:
   AUBIO_FREE(o);
   return NULL;