;; take the time_domain signal (time_buffer) and return the spectrum
(bind-func mag_spectrum
(let ((nbins 512) ; number of bins for spectrogram
(spectrum:double* (zalloc nbins))
(i:i64 0))
(lambda (audio:double* fft_window_size time_buffer freq_buffer)
(dotimes (i fft_window_size)
;; add sum of L and R channels into real component
;; of time_buffer (which is a buffer of complex values
(tset! (pref-ptr time_buffer i) 0
(+ (pref audio (* 2 i)) ; L channel
(pref audio (+ 1 (* 2 i)))))) ; R channel
;; take DFT, store results in freq_buffer
(fft_cpxd time_buffer freq_buffer fft_window_size)
(bin_spectrum spectrum freq_buffer fft_window_size nbins))))