shithub: aubio

Download patch

ref: 6d41dac050d42aec8f08e27ef49804b85a03e3bf
parent: 0316feb35fa29adc9c1f035d56d5b500e1be3626
author: Paul Brossier <piem@piem.org>
date: Sat Nov 17 08:54:20 EST 2018

[filterbank] factorize input validation

--- a/src/spectral/filterbank_mel.c
+++ b/src/spectral/filterbank_mel.c
@@ -209,35 +209,43 @@
   return retval;
 }
 
-uint_t
-aubio_filterbank_set_mel_coeffs (aubio_filterbank_t * fb, smpl_t samplerate,
-    smpl_t freq_min, smpl_t freq_max)
+static uint_t aubio_filterbank_check_freqs (aubio_filterbank_t *fb UNUSED,
+    smpl_t samplerate, smpl_t *freq_min, smpl_t *freq_max)
 {
-  uint_t m, retval;
-  smpl_t start, end, step;
-  fvec_t *freqs;
-  fmat_t *coeffs = aubio_filterbank_get_coeffs(fb);
-  uint_t n_bands = coeffs->height;
-
   if (samplerate <= 0) {
     AUBIO_ERR("filterbank: set_mel_coeffs samplerate should be > 0\n");
     return AUBIO_FAIL;
   }
-  if (freq_max < 0) {
+  if (*freq_max < 0) {
     AUBIO_ERR("filterbank: set_mel_coeffs freq_max should be > 0\n");
     return AUBIO_FAIL;
-  } else if (freq_max == 0) {
-    end = aubio_hztomel(samplerate / 2.);
-  } else {
-    end = aubio_hztomel(freq_max);
+  } else if (*freq_max == 0) {
+    *freq_max = samplerate / 2.;
   }
-  if (freq_min < 0) {
+  if (*freq_min < 0) {
     AUBIO_ERR("filterbank: set_mel_coeffs freq_min should be > 0\n");
     return AUBIO_FAIL;
-  } else {
-    start = aubio_hztomel(freq_min);
   }
+  return AUBIO_OK;
+}
 
+uint_t
+aubio_filterbank_set_mel_coeffs (aubio_filterbank_t * fb, smpl_t samplerate,
+    smpl_t freq_min, smpl_t freq_max)
+{
+  uint_t m, retval;
+  smpl_t start = freq_min, end = freq_max, step;
+  fvec_t *freqs;
+  fmat_t *coeffs = aubio_filterbank_get_coeffs(fb);
+  uint_t n_bands = coeffs->height;
+
+  if (aubio_filterbank_check_freqs(fb, samplerate, &freq_min, &freq_max)) {
+    return AUBIO_FAIL;
+  }
+
+  start = aubio_hztomel(start);
+  end = aubio_hztomel(end);
+
   freqs = new_fvec(n_bands + 2);
   step = (end - start) / (n_bands + 1);
 
@@ -258,29 +266,17 @@
     smpl_t freq_min, smpl_t freq_max)
 {
   uint_t m, retval;
-  smpl_t start, end, step;
+  smpl_t start = freq_min, end = freq_max, step;
   fvec_t *freqs;
   fmat_t *coeffs = aubio_filterbank_get_coeffs(fb);
   uint_t n_bands = coeffs->height;
 
-  if (samplerate <= 0) {
-    AUBIO_ERR("filterbank: set_mel_coeffs samplerate should be > 0\n");
+  if (aubio_filterbank_check_freqs(fb, samplerate, &freq_min, &freq_max)) {
     return AUBIO_FAIL;
   }
-  if (freq_max < 0) {
-    AUBIO_ERR("filterbank: set_mel_coeffs freq_max should be > 0\n");
-    return AUBIO_FAIL;
-  } else if (freq_max == 0) {
-    end = aubio_hztomel_htk(samplerate / 2.);
-  } else {
-    end = aubio_hztomel_htk(freq_max);
-  }
-  if (freq_min < 0) {
-    AUBIO_ERR("filterbank: set_mel_coeffs freq_min should be > 0\n");
-    return AUBIO_FAIL;
-  } else {
-    start = aubio_hztomel_htk(freq_min);
-  }
+
+  start = aubio_hztomel_htk(start);
+  end = aubio_hztomel_htk(end);
 
   freqs = new_fvec (n_bands + 2);
   step = (end - start) / (n_bands + 1);