;; simple sawtooth
;;
;; mod is phase between 0.0-1.0 (i.e. not 2pi)
;; ramp is rising by default (invert for falling)
(bind-func saw_c
(lambda (mod inverted:i1)
(let ((inc:float 0.0) (absinc 0.0:f) (out 0.0)
(rising (if inverted 1 0))
(inv (if inverted -1.0 1.0)))
(lambda (amp:float frq:float)
(set! inc (/ frq SRs))
;; positive frequencies
(if (and (> inc 0.0) (>= mod 1.0))
(set! mod (- mod 1.0)))
;; negative frequencies
(if (and (< inc 0.0) (<= mod 0.0))
(set! mod (+ mod 1.0)))
(set! out (* inv (- (* mod 2.0) 1.0))) ;; bipolar
(set! mod (+ mod inc)) ;; update mod
(* amp -1.0 out)))))