;; same as comb_c but includes APF
(bind-func static comb_apf_c
(lambda (maxdelay:i64)
(let ((line:SAMPLE* (zalloc maxdelay))
(in_head 0)
(out_head 0)
(out_head_alpha 0)
(olddelay:SAMPLE (convert maxdelay))
(alpha 0.0)
(tmp:SAMPLE 0.0)
(i:i64 0)
(idelay:i64 0)
(delayed:SAMPLE 0.0)
(delayedY:SAMPLE 0.0)
(delayedY1:SAMPLE 0.0)
(delayedLin:SAMPLE 0.0)) ;; linear interp
(dotimes (i maxdelay) (pset! line i 0.0))
(lambda (x:SAMPLE delay:SAMPLE apf_gain:SAMPLE)
(if (<> delay olddelay)
(begin (set! olddelay delay)
(set! alpha (- delay (floor delay)))
(set! idelay (convert (floor delay)))
(set! out_head (- (+ maxdelay in_head) idelay))
(set! out_head_alpha (- (+ maxdelay in_head) (+ 1 idelay)))))
(if (> out_head maxdelay) (set! out_head (- out_head maxdelay)))
(if (> out_head_alpha maxdelay) (set! out_head_alpha (- out_head_alpha maxdelay)))
(if (> in_head maxdelay) (set! in_head (- in_head maxdelay)))
(set! delayedY1 (pref line out_head))
(set! delayedY (pref line out_head_alpha))
(set! delayedLin (* (- delayedY delayedY1) alpha))
(set! delayed (+ delayedY1 delayedLin))
(set! tmp (+ x (* apf_gain delayed)))
(pset! line in_head tmp)
(set! in_head (+ in_head 1))
(set! out_head_alpha (+ out_head_alpha 1))
(set! out_head (+ out_head 1))
(+ (* -1.0:f apf_gain tmp) delayed)))))