 
 (defn extract-fundemental-frequency [wav-data block-size sample-rate]
   (let [r (double-array 1)]
-    (xtract/xtract_wavelet_f0 wav-data block-size (xtract-args sample-rate) r)
-
-;;    (when (> (first r) 0.0) (println :---------------------> (first r)))
-    (first r)
-    )
-)
+    (raise-errors
+     (xtract/xtract_wavelet_f0 wav-data block-size (xtract-args sample-rate) r))
+    (first r)))
 
 (defn extract-spectrum
   "Extract frequency domain spectrum from time domain signal"

  ([windowed-data block-size sample-ratio spectrum-type db-component normalised]

   (let [spectrum (c-double block-size)]
     (xtract/xtract_spectrum windowed-data
                             block-size (xtract-args sample-ratio
                                                     spectrum-type
                                                     db-component
                                                     normalised)
                             spectrum)
     spectrum)))

(defn extract-fft-spectrum [wave-data block-size sample-rate full-window]
  (let [windowed (c-double block-size)]
    (xtract/xtract_windowed wave-data block-size (xtract/doublea_to_voidp full-window) windowed)
    (xtract/xtract_init_fft block-size xtract-spectrum)
    (let [spectrum (extract-spectrum windowed block-size (/ sample-rate block-size) xtract-spectrum-magnitude)]
      (xtract/xtract_free_fft)
      (xtract/delete_double_array windowed)
      spectrum)))

(defn extract-peaks
  "Extract the amplitude and frequency of spectral peaks from a magnitude spectrum"
  [coefficients block-size sample-ratio peak-threshold]
  (let [peaks (c-double block-size)]
    (xtract/xtract_peak_spectrum coefficients
                                 (/ block-size 2)
                                 (xtract-args sample-ratio peak-threshold)
                                 peaks)
    peaks))

(defn extract-rms-amp
  "Extract the RMS amplitude of an input vector"
  [wav-data block-size sample-ratio]
  (let [rms (double-array 1)]
    (xtract/xtract_rms_amplitude wav-data block-size (xtract-args sample-ratio) rms)
    rms))

(defn raise-errors [result]
    (cond
      (= result xtract-success)        true
      (= result xtract-bad-argv)       (throw (Exception. "Bad argv error"))
      (= result xtract-argument-error) (throw (Exception. "Bad arguments error"))
      true (throw (Exception. "Unknown result type"))))

(defn frequency->cents [f0]
  (if (= 0.0 f0)

     (let [full-window (xtract/xtract_init_window block-size xtract-hann)
           half-window (xtract/xtract_init_window half-block-size xtract-hann)]

       (raise-errors
        (xtract/xtract_init_wavelet_f0_state))

       (let [stats
             (map-indexed
              (fn [idx v]
                (let [wav-data (clojure->c-double v)
                      f0       (extract-fundemental-frequency wav-data block-size SAMPLE_RATE)
                      cents    (frequency->cents f0)
