;; --------------------------------------------------------------------------
;; Coni Visual Sound Generator
;; --------------------------------------------------------------------------
;; Node-based modular synthesizer powered by Web Audio API and Re-frame WASM
;; --------------------------------------------------------------------------

(defn safe-float [v]
  (let [num (.parseFloat (js/global "window") (if (nil? v) "0" v))]
    (if (js/call (js/global "window") "isNaN" num) 0.0 num)))

(require "libs/reframe/src/reframe_wasm.coni")
(require "libs/dom/src/dom.coni")
(require "libs/str/src/str.coni" :as str)
(require "libs/math/src/math.coni" :as math)

(def window (js/global "window"))
(def document (js/global "document"))
(def Math (js/global "Math"))


;; --------------------------------------------------------------------------
;; Web Audio API Interop Engine
;; --------------------------------------------------------------------------

;; The global audio context. Must be initialized after first user interaction (click).
(def *audio-ctx* (atom nil))

(defn init-audio! []
  (if (nil? @*audio-ctx*)
    (let [AudioContext (or (js/global "AudioContext") (js/global "webkitAudioContext"))
          ctx (js/new AudioContext)]
      (js/log "Web Audio API Initialized.")
      (js/set (js/global "window") "audioCtx" ctx)
      (reset! *audio-ctx* ctx)
      ctx)
    @*audio-ctx*))

(defn create-oscillator [ctx type freq]
  (let [osc (js/call ctx "createOscillator")
        freq-param (js/get osc "frequency")]
    (js/set osc "type" type)
    (js/set freq-param "value" (safe-float freq))
    (js/call osc "start")
    osc))

(defn create-gain [ctx vol]
  (let [gain (js/call ctx "createGain")
        gain-param (js/get gain "gain")]
    (js/set gain-param "value" (safe-float vol))
    gain))

(defn create-filter [ctx type freq q]
  (let [filt (js/call ctx "createBiquadFilter")
        freq-param (js/get filt "frequency")
        q-param (js/get filt "Q")]
    (js/set filt "type" type)
    (js/set freq-param "value" (safe-float freq))
    (js/set q-param "value" (safe-float q))
    filt))

(defn create-delay [ctx time fbk]
  (let [delay (js/call ctx "createDelay")
        feedback (js/call ctx "createGain")
        out-gain (js/call ctx "createGain")
        time-param (js/get delay "delayTime")
        fbk-param (js/get feedback "gain")]
    
    (js/set time-param "value" time)
    (js/set fbk-param "value" fbk)
    
    (js/call delay "connect" feedback)
    (js/call feedback "connect" delay)
    (js/call delay "connect" out-gain)
    
    {:in delay :out out-gain :fb feedback :delay delay}))

(defn create-compressor [ctx threshold knee ratio attack release]
  (let [comp (js/call ctx "createDynamicsCompressor")]
    (js/set (js/get comp "threshold") "value" (safe-float threshold))
    (js/set (js/get comp "knee") "value" (safe-float knee))
    (js/set (js/get comp "ratio") "value" (safe-float ratio))
    (js/set (js/get comp "attack") "value" (safe-float attack))
    (js/set (js/get comp "release") "value" (safe-float release))
    {:in comp :out comp :comp comp}))

(defn create-tremolo [ctx rate depth]
  (let [sine (js/call ctx "createOscillator")
        lfo-gain (js/call ctx "createGain")
        trem-gain (js/call ctx "createGain")]
    (js/set sine "type" "sine")
    (js/set (js/get sine "frequency") "value" (safe-float rate))
    (js/set (js/get lfo-gain "gain") "value" (safe-float depth))
    (js/set (js/get trem-gain "gain") "value" (- 1.0 (safe-float depth))) ;; base volume to prevent clipping
    (js/call sine "connect" lfo-gain)
    (js/call lfo-gain "connect" (js/get trem-gain "gain"))
    (js/call sine "start")
    {:in trem-gain :out trem-gain :osc sine :lfo lfo-gain}))

(defn create-chorus [ctx rate depth delay]
  (let [in-gain (js/call ctx "createGain")
        dry-gain (js/call ctx "createGain")
        wet-gain (js/call ctx "createGain")
        del (js/call ctx "createDelay")
        lfo (js/call ctx "createOscillator")
        lfo-gain (js/call ctx "createGain")
        out-gain (js/call ctx "createGain")]
    
    (js/set (js/get del "delayTime") "value" (safe-float delay))
    (js/set (js/get lfo "frequency") "value" (safe-float rate))
    (js/set (js/get lfo-gain "gain") "value" (safe-float depth))
    (js/set (js/get dry-gain "gain") "value" 0.7)
    (js/set (js/get wet-gain "gain") "value" 0.7)
    
    ;; Split physical input
    (js/call in-gain "connect" dry-gain)
    (js/call in-gain "connect" wet-gain)
    
    ;; Dry path
    (js/call dry-gain "connect" out-gain)
    
    ;; Modulated Delay path
    (js/call lfo "connect" lfo-gain)
    (js/call lfo-gain "connect" (js/get del "delayTime"))
    (js/call lfo "start")
    (js/call wet-gain "connect" del)
    (js/call del "connect" out-gain)
    
    {:in in-gain
     :out out-gain
     :dry dry-gain :wet wet-gain :delay del :osc lfo :lfo lfo-gain}))

(defn create-panner [ctx pan]
  (let [panner (js/call ctx "createStereoPanner")
        pan-param (js/get panner "pan")]
    (js/set pan-param "value" (safe-float pan))
    panner))

(defn make-distortion-async [ws amount]
  (let [wid @*reverb-worker-id*
        window (js/global "window")]
    (reset! *reverb-worker-id* (+ wid 1))
    (js/set (js/get window "pendingReverbs") (str wid) ws)
    (js/call (js/get window "dspWorker") "postMessage"
      [:calc-distortion {:id (str wid) :amount amount}])))

(defn create-distortion [ctx amount]
  (let [drive-gain (js/call ctx "createGain")
        ws (js/call ctx "createWaveShaper")]
    (make-distortion-async ws amount)
    (js/set ws "oversample" "4x")
    (js/set (js/get drive-gain "gain") "value" (safe-float amount))
    (js/call drive-gain "connect" ws)
    {:in drive-gain :out ws :drive drive-gain}))

(defn create-bitcrusher [ctx bits]
  (let [ws (js/call ctx "createWaveShaper")
        curve (js/new (js/global "Float32Array") 4096)
        step (math/pow 0.5 (safe-float bits))]
    (loop [i 0]
      (if (< i 4096)
        (let [x (- (* (/ (float i) 4096.0) 2.0) 1.0)
              val (* (math/round (/ x step)) step)]
          (js/set curve (str i) val)
          (recur (+ i 1)))
        nil))
    (js/set ws "curve" curve)
    {:in ws :out ws :ws ws}))

(def *reverb-worker-id* (atom 0))

(defn make-reverb-async [ctx rev duration decay]
  (let [wid @*reverb-worker-id*
        window (js/global "window")]
    (reset! *reverb-worker-id* (+ wid 1))
    (js/set (js/get window "pendingReverbs") (str wid) rev)
    (js/call (js/get window "dspWorker") "postMessage"
      [:calc-reverb {:id (str wid) 
                     :sampleRate (js/get ctx "sampleRate") 
                     :duration duration 
                     :decay decay}])))

(defn create-reverb [ctx duration decay amount]
  (let [rev (js/call ctx "createConvolver")
        in-gain (js/call ctx "createGain")
        out-gain (js/call ctx "createGain")
        dry-gain (js/call ctx "createGain")
        wet-gain (js/call ctx "createGain")]
    
    (make-reverb-async ctx rev (safe-float duration) (safe-float decay))
    
    (js/set (js/get dry-gain "gain") "value" (- 1.0 (safe-float amount)))
    (js/set (js/get wet-gain "gain") "value" (safe-float amount))
    
    (js/call in-gain "connect" dry-gain)
    (js/call in-gain "connect" wet-gain)
    (js/call wet-gain "connect" rev)
    (js/call rev "connect" out-gain)
    (js/call dry-gain "connect" out-gain)
    
    {:in in-gain :out out-gain :rev rev :wet wet-gain :dry dry-gain}))

(defn create-media-player [ctx url loops?]
  (let [source (js/call ctx "createBufferSource")
        gain (js/call ctx "createGain")
        out-gain (js/get gain "gain")]
    (js/set out-gain "value" 0.0) ; Start muted until loaded
    
    (js/set source "loop" loops?)
    (js/call source "connect" gain)
    (js/call source "start")
    
    (let [window (js/global "window")]
      (fetch-media-buffer ctx url (fn [audio-buf]
        (js/set source "buffer" audio-buf)
        (js/call out-gain "setTargetAtTime" 1.0 (js/get ctx "currentTime") 0.05)
        (js/log (str "Loaded media buffer: " url)))))
        
    {:in nil :out gain :source source}))

(defn create-sampler [ctx loops?]
  (let [gain (js/call ctx "createGain")
        out-gain (js/get gain "gain")]
    (js/set out-gain "value" 0.0)
    {:in nil :out gain :source nil :buffer nil :loop loops? :start 0.0 :end 10.0}))

(defn create-lfo [ctx freq depth]
  (let [osc (js/call ctx "createOscillator")
        gain (js/call ctx "createGain")]
    (js/set (js/get osc "frequency") "value" (safe-float freq))
    (js/set (js/get gain "gain") "value" (safe-float depth))
    (js/call osc "connect" gain)
    (js/call osc "start")
    {:osc osc :gain gain :out gain}))

(defn create-sequencer [ctx bpm]
  (let [osc (js/call ctx "createOscillator")
        ws (js/call ctx "createWaveShaper")
        gate (js/call ctx "createGain")
        curve (js/new (js/global "Float32Array") 100)]
    (loop [i 0]
      (if (< i 100)
        (do
          (js/set curve (str i) (if (> i 85) 1.0 0.0))
          (recur (+ i 1)))
        nil))
    (js/set ws "curve" curve)
    (js/set osc "type" "sawtooth")
    (js/set (js/get osc "frequency") "value" (/ bpm 60.0))
    (js/set (js/get gate "gain") "value" 0.0) ;; Gate is closed by default
    (js/call osc "connect" ws)
    (js/call ws "connect" (js/get gate "gain")) ;; Modulate gate gain
    (js/call osc "start")
    {:osc osc :in gate :out gate}))

(defn create-bouncer [ctx gravity height]
  (let [window (js/global "window")
        gate (js/call ctx "createGain")
        gain-param (js/get gate "gain")
        state-ref (atom {:timeout-id nil :current-delay height :bounces 0})]
    
    (js/set gain-param "value" 0.0)
    
    (let [trigger-bounce 
          (fn [self state]
            (let [now (js/get ctx "currentTime")]
              ;; Trigger a fast, staccato envelope
              (js/call gain-param "setValueAtTime" 0.0 now)
              (js/call gain-param "linearRampToValueAtTime" 1.0 (+ now 0.01))
              (js/call gain-param "exponentialRampToValueAtTime" 0.001 (+ now 0.08))
              (js/call gain-param "setValueAtTime" 0.0 (+ now 0.081))
              
              ;; Calculate next bounce
              (let [next-delay (* (:current-delay state) gravity)
                    next-bounces (+ (:bounces state) 1)]
                (if (< next-delay 40)
                  ;; Reset drop after a random pause
                  (let [pause (+ 500 (* (math/random) 2000))
                        tid (js/call window "setTimeout" 
                              (fn [] (self self (assoc (assoc state :current-delay (+ height (* (math/random) 100))) :bounces 0))) 
                              pause)]
                    (swap! state-ref (fn [s] (assoc s :timeout-id tid))))
                  ;; Continue bouncing
                  (let [tid (js/call window "setTimeout" 
                              (fn [] (self self (assoc (assoc state :current-delay next-delay) :bounces next-bounces))) 
                              (:current-delay state))]
                    (swap! state-ref (fn [s] (assoc s :timeout-id tid))))))))]
      
      ;; Start the first drop
      (trigger-bounce trigger-bounce @state-ref)
      
      {:in gate :out gate 
       :cleanup (fn [] 
                  (let [tid (:timeout-id @state-ref)]
                    (if tid (js/call window "clearTimeout" tid) nil)))})))

(defn create-random [ctx rate-hz]
  (let [window (js/global "window")
        source (js/call ctx "createConstantSource")
        safe-rate (if (or (nil? rate-hz) (= (safe-float rate-hz) 0.0)) 0.1 (safe-float rate-hz))
        interval-ms (/ 1000.0 safe-rate)]
    (js/call source "start")
    (let [int-id (js/call window "setInterval" 
                   (fn []
                     (let [now (js/get ctx "currentTime")
                           rn (- (* (math/random) 2.0) 1.0)
                           offset (js/get source "offset")]
                       (js/call offset "setTargetAtTime" rn now 0.01)))
                   interval-ms)]
      (js/set source "_pulseIntervalId" int-id)
      (let [gain (js/call ctx "createGain")]
        (js/call source "connect" gain)
        (js/set (js/get gain "gain") "value" 0.5)
        {:osc source :gain gain :out gain 
         :cleanup (fn [] (js/call window "clearInterval" int-id))}))))

(defn create-noise [ctx vol]
  (let [sr (js/get ctx "sampleRate")
        buf-size (* 2 sr)
        noise-buf (js/call ctx "createBuffer" 1 buf-size sr)
        output (js/call noise-buf "getChannelData" 0)]
    (loop [i 0]
      (if (< i buf-size)
        (do
          (js/set output (str i) (float (- (* (math/random) 2.0) 1.0)))
          (recur (+ i 1)))
        nil))
    (let [noise-source (js/call ctx "createBufferSource")
          gain (js/call ctx "createGain")]
      (js/set noise-source "buffer" noise-buf)
      (js/set noise-source "loop" true)
      (js/call noise-source "start" 0)
      (js/set (js/get gain "gain") "value" (safe-float vol))
      (js/call noise-source "connect" gain)
      {:source noise-source :gain gain :out gain})))

(defn create-kick [ctx bpm decay pitch-drop]
  (let [window (js/global "window")
        out-gain (js/call ctx "createGain")
        state-ref (atom {:timeout-id nil :bpm (safe-float bpm) :decay (safe-float decay) :pitch (safe-float pitch-drop)})]
    (let [trigger-kick 
          (fn [self]
            (let [now (js/get ctx "currentTime")
                  osc (js/call ctx "createOscillator")
                  gain (js/call ctx "createGain")
                  p-freq (js/get osc "frequency")
                  p-gain (js/get gain "gain")
                  s @state-ref
                  t-bpm (if (= (:bpm s) 0.0) 120.0 (:bpm s))
                  interval-ms (/ 60000.0 t-bpm)]
              
              (js/set osc "type" "sine")
              (js/call p-freq "setValueAtTime" 150.0 now)
              (js/call p-freq "exponentialRampToValueAtTime" 40.0 (+ now (:pitch s)))
              
              (js/call p-gain "setValueAtTime" 0.001 now)
              (js/call p-gain "linearRampToValueAtTime" 1.0 (+ now 0.005))
              (js/call p-gain "exponentialRampToValueAtTime" 0.001 (+ now (:decay s)))
              
              (js/call osc "connect" gain)
              (js/call gain "connect" out-gain)
              (js/call osc "start" now)
              (js/call osc "stop" (+ now (:decay s) 0.1))
              
              (let [tid (js/call window "setTimeout" (fn [] (self self)) interval-ms)]
                (swap! state-ref (fn [st] (assoc st :timeout-id tid))))))]
      (trigger-kick trigger-kick)
      {:out out-gain :state state-ref :cleanup (fn [] (let [tid (:timeout-id @state-ref)] (if tid (js/call window "clearTimeout" tid) nil)))})))

(defn create-hat [ctx bpm decay]
  (let [window (js/global "window")
        out-gain (js/call ctx "createGain")
        sr (js/get ctx "sampleRate")
        buf-size (* 2 sr)
        buffer (js/call ctx "createBuffer" 1 buf-size sr)
        data (js/call buffer "getChannelData" 0)
        state-ref (atom {:timeout-id nil :bpm (safe-float bpm) :decay (safe-float decay)})]
    
    (loop [i 0]
      (if (< i buf-size)
        (do (js/set data (str i) (- (* (math/random) 2.0) 1.0)) (recur (+ i 1))) nil))

    (let [trigger-hat 
          (fn [self]
            (let [now (js/get ctx "currentTime")
                  source (js/call ctx "createBufferSource")
                  filter (js/call ctx "createBiquadFilter")
                  gain (js/call ctx "createGain")
                  p-gain (js/get gain "gain")
                  s @state-ref
                  t-bpm (if (= (:bpm s) 0.0) 120.0 (:bpm s))
                  interval-ms (/ 60000.0 t-bpm)]
              
              (js/set source "buffer" buffer)
              (js/set filter "type" "highpass")
              (js/set (js/get filter "frequency") "value" 7000.0)
              
              (js/call p-gain "setValueAtTime" 0.001 now)
              (js/call p-gain "linearRampToValueAtTime" 1.0 (+ now 0.005))
              (js/call p-gain "exponentialRampToValueAtTime" 0.001 (+ now (:decay s)))
              
              (js/call source "connect" filter)
              (js/call filter "connect" gain)
              (js/call gain "connect" out-gain)
              
              (js/call source "start" now)
              (js/call source "stop" (+ now (:decay s) 0.1))
              
              (let [tid (js/call window "setTimeout" (fn [] (self self)) interval-ms)]
                (swap! state-ref (fn [st] (assoc st :timeout-id tid))))))]
      (trigger-hat trigger-hat)
      {:out out-gain :state state-ref :cleanup (fn [] (let [tid (:timeout-id @state-ref)] (if tid (js/call window "clearTimeout" tid) nil)))})))

;; --------------------------------------------------------------------------
;; Node Registry & Factory
;; --------------------------------------------------------------------------

(def *next-node-id* (atom 0))
(defn next-id []
  (let [id @*next-node-id*]
    (reset! *next-node-id* (+ id 1))
    (str "node_" id)))

(def node-registry
  {:oscillator {:category :source 
                :label "Oscillator"
                :inputs [:frequency :detune] 
                :outputs [:out]
                 :params [{:id :frequency :label "Frequency" :min 20.0 :max 2000.0 :step 1.0 :default 440.0}
                         {:id :type :label "Wave" :options ["sine" "square" "sawtooth" "triangle"] :default "sine"}]
                :create (fn [ctx params] (create-oscillator ctx (:type params) (:frequency params)))
                :update (fn [an param val] 
                          (if (= param "type")
                            (do (js/set an "type" val) nil)
                            (let [p-obj (js/get an param)]
                              (if p-obj 
                                (let [ctx (js/get an "context")
                                      now (js/get ctx "currentTime")
                                      num-val (safe-float val)]
                                  (do (js/call p-obj "setTargetAtTime" num-val now 0.05) nil)) nil))))}
                
   :gain       {:category :util 
                :label "Gain/Volume"
                :inputs [:in :gain] 
                :outputs [:out]
                :params [{:id :gain :label "Volume" :min 0.0 :max 2.0 :step 0.01 :default 0.8}]
                :create (fn [ctx params] (create-gain ctx (:gain params)))
                :update (fn [an param val]
                          (let [p-obj (js/get an param)]
                            (if p-obj 
                              (let [ctx (js/get an "context")
                                    now (js/get ctx "currentTime")
                                    num-val (safe-float val)]
                                (do (js/call p-obj "setTargetAtTime" num-val now 0.05) nil)) nil)))}

   :compressor {:category :util 
                :label "Compressor"
                :inputs [:in] 
                :outputs [:out]
                :params [{:id :threshold :label "Threshold (dB)" :min -100.0 :max 0.0 :step 1.0 :default -24.0}
                         {:id :knee :label "Knee" :min 0.0 :max 40.0 :step 1.0 :default 30.0}
                         {:id :ratio :label "Ratio" :min 1.0 :max 20.0 :step 0.1 :default 12.0}
                         {:id :attack :label "Attack (s)" :min 0.0 :max 1.0 :step 0.001 :default 0.003}
                         {:id :release :label "Release (s)" :min 0.0 :max 1.0 :step 0.01 :default 0.25}]
                :create (fn [ctx params] (create-compressor ctx (:threshold params) (:knee params) (:ratio params) (:attack params) (:release params)))
                :update (fn [an param val]
                          (let [comp (:comp an)
                                p-obj (js/get comp param)]
                            (if p-obj 
                              (let [ctx (js/get comp "context")
                                    now (js/get ctx "currentTime")
                                    num-val (safe-float val)]
                                (do (js/call p-obj "setTargetAtTime" num-val now 0.05) nil)) nil)))}

   :filter     {:category :tone 
                :label "Biquad Filter"
                :inputs [:in :frequency :Q] 
                :outputs [:out]
                :params [{:id :type :label "Type" :options ["lowpass" "highpass" "bandpass"] :default "lowpass"}
                         {:id :frequency :label "Cutoff" :min 20.0 :max 10000.0 :step 1.0 :default 1000.0}
                         {:id :Q :label "Resonance (Q)" :min 0.1 :max 20.0 :step 0.1 :default 1.0}]
                :create (fn [ctx params] (create-filter ctx (:type params) (:frequency params) (:Q params)))
                :update (fn [an param val]
                          (if (= param "type")
                            (do (js/set an "type" val) nil)
                            (let [p-obj (js/get an param)]
                              (if p-obj 
                                (let [ctx (js/get an "context")
                                      now (js/get ctx "currentTime")
                                      num-val (safe-float val)]
                                  (do (js/call p-obj "setTargetAtTime" num-val now 0.05) nil)) nil))))}
                
   :delay      {:category :effect 
                :label "Analog Delay"
                :inputs [:in :delayTime :feedback] 
                :outputs [:out]
                :params [{:id :delayTime :label "Time (s)" :min 0.01 :max 2.0 :step 0.01 :default 0.3}
                         {:id :feedback :label "Feedback" :min 0.0 :max 0.95 :step 0.01 :default 0.4}]
                :create (fn [ctx params] (create-delay ctx (:delayTime params) (:feedback params)))
                :update (fn [an param val]
                          (let [delay-node (:delay an)
                                fbk-node (:fb an)
                                p-obj (if (= param "delayTime") (js/get delay-node "delayTime")
                                        (if (= param "feedback") (js/get fbk-node "gain") nil))]
                            (if p-obj
                              (let [ctx (js/get delay-node "context")
                                    now (js/get ctx "currentTime")
                                    num-val (safe-float val)]
                                (do (js/call p-obj "setTargetAtTime" num-val now 0.05) nil)) nil)))}

   :distortion {:category :effect
                :label "Distortion"
                :inputs [:in :amount]
                :outputs [:out]
                :params [{:id :amount :label "Drive" :min 0.0 :max 10.0 :step 0.1 :default 1.0}]
                :create (fn [ctx params] (create-distortion ctx (:amount params)))
                :update (fn [an param val]
                          (if (= param "amount")
                            (let [p-obj (js/get (:drive an) "gain")
                                  ctx (js/get (:out an) "context")
                                  now (js/get ctx "currentTime")
                                  num-val (safe-float val)]
                              (make-distortion-async (:out an) num-val)
                              (do (js/call p-obj "setTargetAtTime" num-val now 0.05) nil)) nil))}

   :bitcrusher {:category :effect 
                :label "Bitcrusher"
                :inputs [:in] 
                :outputs [:out]
                :params [{:id :bits :label "Fidelity (Bits)" :min 1.0 :max 16.0 :step 1.0 :default 4.0}]
                :create (fn [ctx params] (create-bitcrusher ctx (:bits params)))
                :update (fn [an param val]
                          (if (= param "bits")
                            (let [bits (safe-float val)
                                  step (math/pow 0.5 bits)
                                  curve (js/new (js/global "Float32Array") 4096)]
                              (loop [i 0]
                                (if (< i 4096)
                                  (let [x (- (* (/ (float i) 4096.0) 2.0) 1.0)
                                        v (* (math/round (/ x step)) step)]
                                    (js/set curve (str i) v)
                                    (recur (+ i 1)))
                                  nil))
                              (js/set (:ws an) "curve" curve) nil) nil))}

   :eq         {:category :tone 
                :label "Multi-Band EQ"
                :inputs [:in :low :mid :high] 
                :outputs [:out]
                :params [{:id :low :label "Low (dB)" :min -40.0 :max 10.0 :step 0.1 :default 0.0}
                         {:id :mid :label "Mid (dB)" :min -40.0 :max 10.0 :step 0.1 :default 0.0}
                         {:id :high :label "High (dB)" :min -40.0 :max 10.0 :step 0.1 :default 0.0}]
                :create (fn [ctx params] (create-eq ctx (:low params) (:mid params) (:high params)))
                :update (fn [an param val]
                          (let [p-obj (if (= param "low") (js/get (:low an) "gain")
                                        (if (= param "mid") (js/get (:mid an) "gain")
                                          (js/get (:high an) "gain")))]
                            (if p-obj 
                              (let [ctx (js/get (:out an) "context")
                                    now (js/get ctx "currentTime")
                                    num-val (safe-float val)]
                                (do (js/call p-obj "setTargetAtTime" num-val now 0.05) nil)) nil)))}

   :analyser   {:category :util 
                :label "Analyser"
                :inputs [:in] 
                :outputs [:out]
                :params []
                :create (fn [ctx params] (create-analyser ctx))
                :update (fn [an param val] nil)}

   :tremolo    {:category :effect 
                :label "Tremolo"
                :inputs [:in] 
                :outputs [:out]
                :params [{:id :rate :label "Rate (Hz)" :min 0.1 :max 20.0 :step 0.1 :default 4.0}
                         {:id :depth :label "Depth" :min 0.0 :max 1.0 :step 0.01 :default 0.5}]
                :create (fn [ctx params] (create-tremolo ctx (:rate params) (:depth params)))
                :update (fn [an param val]
                          (let [p-obj (if (= param "rate") (js/get (:osc an) "frequency") (js/get (:lfo an) "gain"))]
                            (if p-obj
                              (let [ctx (js/get (:osc an) "context")
                                    now (js/get ctx "currentTime")
                                    num-val (safe-float val)]
                                (do (js/call p-obj "setTargetAtTime" num-val now 0.05) nil)) nil)))}

   :chorus     {:category :effect 
                :label "Chorus"
                :inputs [:in] 
                :outputs [:out]
                :params [{:id :rate :label "Rate (Hz)" :min 0.1 :max 10.0 :step 0.1 :default 1.5}
                         {:id :depth :label "Depth (s)" :min 0.0 :max 0.05 :step 0.001 :default 0.01}
                         {:id :delay :label "Delay (s)" :min 0.0 :max 0.1 :step 0.001 :default 0.03}]
                :create (fn [ctx params] (create-chorus ctx (:rate params) (:depth params) (:delay params)))
                :update (fn [an param val]
                          (let [p-obj (if (= param "rate") (js/get (:osc an) "frequency")
                                        (if (= param "depth") (js/get (:lfo an) "gain")
                                          (js/get (:delay an) "delayTime")))]
                            (if p-obj
                              (let [ctx (js/get (:osc an) "context")
                                    now (js/get ctx "currentTime")
                                    num-val (safe-float val)]
                                (do (js/call p-obj "setTargetAtTime" num-val now 0.05) nil)) nil)))}

   :panner     {:category :util
                :label "Stereo Panner"
                :inputs [:in :pan]
                :outputs [:out]
                :params [{:id :pan :label "Pan (L/R)" :min -1.0 :max 1.0 :step 0.05 :default 0.0}]
                :create (fn [ctx params] (create-panner ctx (:pan params)))
                :update (fn [an param val]
                          (let [p-obj (js/get an "pan")]
                            (if p-obj 
                              (let [ctx (js/get an "context")
                                    now (js/get ctx "currentTime")
                                    num-val (safe-float val)]
                                (do (js/call p-obj "setTargetAtTime" num-val now 0.05) nil)) nil)))}

   :lfo        {:category :source 
                :label "LFO (Sweeper)"
                :inputs []
                :outputs [:out]
                :params [{:id :frequency :label "Rate (Hz)" :min 0.01 :max 20.0 :step 0.01 :default 0.2}
                         {:id :depth :label "Depth / Amount" :min 0.0 :max 1000.0 :step 1.0 :default 100.0}]
                :create (fn [ctx params] (create-lfo ctx (:frequency params) (:depth params)))
                :update (fn [an param val]
                          (let [p-obj (if (= param "frequency") (js/get (:osc an) "frequency")
                                          (js/get (:gain an) "gain"))]
                            (if p-obj 
                              (let [ctx (js/get (:osc an) "context")
                                    now (js/get ctx "currentTime")
                                    num-val (safe-float val)]
                                (do (js/call p-obj "setTargetAtTime" num-val now 0.05) nil)) nil)))}

   :sequencer  {:category :effect
                :label "Clock / Sequencer"
                :inputs [:in]
                :outputs [:out]
                :params [{:id :bpm :label "BPM" :min 20.0 :max 300.0 :step 1.0 :default 120.0}]
                :create (fn [ctx params] (create-sequencer ctx (:bpm params)))
                :update (fn [an param val]
                          (if (= param "bpm")
                            (let [ctx (js/get (:osc an) "context")
                                  now (js/get ctx "currentTime")
                                  num-val (safe-float val)
                                  freq (/ num-val 60.0)]
                              (do (js/call (js/get (:osc an) "frequency") "setTargetAtTime" freq now 0.05) nil)) nil))}

   :bouncer    {:category :util
                :label "Bouncing Envelope"
                :inputs [:in]
                :outputs [:out]
                :params [{:id :gravity :label "Gravity Decay" :min 0.5 :max 0.99 :step 0.01 :default 0.75}
                         {:id :height :label "Drop Height" :min 200.0 :max 1000.0 :step 10.0 :default 600.0}]
                :create (fn [ctx params] (create-bouncer ctx (:gravity params) (:height params)))
                :update (fn [an param val] nil)}

   :kick       {:category :source 
                :label "Kick Drum"
                :inputs []
                :outputs [:out]
                :params [{:id :bpm :label "BPM" :min 20.0 :max 300.0 :step 1.0 :default 140.0}
                         {:id :decay :label "Decay" :min 0.05 :max 1.0 :step 0.01 :default 0.3}
                         {:id :pitch :label "Punch" :min 0.01 :max 0.2 :step 0.01 :default 0.05}]
                :create (fn [ctx params] (create-kick ctx (:bpm params) (:decay params) (:pitch params)))
                :update (fn [an param val]
                          (let [s-ref (:state an)]
                            (if s-ref 
                              (swap! s-ref (fn [s] (assoc s (keyword param) (safe-float val)))) nil)))}

   :hat        {:category :source 
                :label "Hi-Hat"
                :inputs []
                :outputs [:out]
                :params [{:id :bpm :label "BPM" :min 20.0 :max 600.0 :step 1.0 :default 280.0}
                         {:id :decay :label "Decay" :min 0.01 :max 0.5 :step 0.01 :default 0.1}]
                :create (fn [ctx params] (create-hat ctx (:bpm params) (:decay params)))
                :update (fn [an param val]
                          (let [s-ref (:state an)]
                            (if s-ref 
                              (swap! s-ref (fn [s] (assoc s (keyword param) (safe-float val)))) nil)))}

   :random     {:category :source
                :label "Random Pulse"
                :inputs []
                :outputs [:out]
                :params [{:id :rate :label "Rate (Hz)" :min 0.1 :max 20.0 :step 0.1 :default 5.0}
                         {:id :volume :label "Amount" :min 0.0 :max 1000.0 :step 1.0 :default 100.0}]
                :create (fn [ctx params] (create-random ctx (:rate params)))
                :update (fn [an param val]
                          (if (= param "volume")
                            (let [ctx (js/get (:gain an) "context")
                                  now (js/get ctx "currentTime")
                                  num-val (safe-float val)]
                              (do (js/call (js/get (:gain an) "gain") "setTargetAtTime" num-val now 0.05) nil))
                            (if (= param "rate")
                              (let [window (js/global "window")
                                    source (:osc an)
                                    rate-val (js/call window "parseFloat" val)
                                    safe-rate (if (or (nil? rate-val) (= (float rate-val) 0.0)) 0.1 (float rate-val))
                                    interval-ms (/ 1000.0 safe-rate)]
                                (js/call window "clearInterval" (js/get source "_pulseIntervalId"))
                                (let [int-id (js/call window "setInterval"
                                               (fn []
                                                 (let [now (.-currentTime (js/get source "context"))
                                                       rn (- (* (math/random) 2.0) 1.0)
                                                       offset (js/get source "offset")]
                                                   (js/call offset "setTargetAtTime" rn now 0.01)))
                                               interval-ms)]
                                  (js/set source "_pulseIntervalId" int-id) nil))

                              nil)))}

   :reverb     {:category :effect
                :label "Reverb"
                :inputs [:in :amount]
                :outputs [:out]
                :params [{:id :amount :label "Wet Mix" :min 0.0 :max 1.0 :step 0.01 :default 0.5}
                         {:id :duration :label "Duration (s)" :min 0.1 :max 10.0 :step 0.1 :default 2.0}
                         {:id :decay :label "Decay" :min 0.1 :max 10.0 :step 0.1 :default 2.0}]
                :create (fn [ctx params] (create-reverb ctx (:duration params) (:decay params) (or (:amount params) 0.5)))
                :update (fn [an param val]
                          (let [num-val (safe-float val)
                                ctx (js/get (:out an) "context")
                                now (js/get ctx "currentTime")]
                            (if (= param "amount")
                              (do 
                                (js/call (js/get (:wet an) "gain") "setTargetAtTime" num-val now 0.05)
                                (js/call (js/get (:dry an) "gain") "setTargetAtTime" (- 1.0 num-val) now 0.05) 
                                nil)
                              (let [dur (if (= param "duration") num-val 2.0)
                                    dec (if (= param "decay") num-val 2.0)]
                                (make-reverb-async ctx (:rev an) dur dec))) 
                            nil))}

   :sampler    {:category :source 
                :label "Local Sampler"
                :inputs []
                :outputs [:out]
                :params [{:id :path :label "File URL / Local Path" :type "text" :default ""}
                          {:id :file :label "Load OS File" :type "button"}
                         {:id :start-time :label "Start (s)" :min 0.0 :max 120.0 :step 0.01 :default 0.0}
                         {:id :end-time :label "End (s)" :min 0.0 :max 120.0 :step 0.01 :default 10.0}
                         {:id :looping :label "Loop?" :options ["true" "false"] :default "false"}]
                :create (fn [ctx params] 
                           (let [an (create-sampler ctx (= (:looping params) "true"))
                                 path (:path params)]
                             an))
                :update (fn [an param val]
                          (let [num-val (if (not= param "looping") (safe-float val) val)
                                new-an (if (= param "start-time") (assoc an :start num-val) 
                                         (if (= param "end-time") (assoc an :end num-val)
                                           (if (= param "looping") (assoc an :loop (= val "true")) an)))
                                src (:source new-an)
                                buf (:buffer new-an)]
                            
                            (if (= param "looping")
                              (if src (js/set src "loop" (= val "true")) nil) nil)
                              
                            (if (and buf (or (= param "start-time") (= param "end-time") (= param "looping")))
                              (let [ctx (js/get (:out new-an) "context")
                                    new-src (js/call ctx "createBufferSource")
                                    s-time (or (:start new-an) 0.0)
                                    e-time (or (:end new-an) 10.0)]
                                (js/set new-src "buffer" buf)
                                (js/set new-src "loop" (:loop new-an))
                                (js/set new-src "loopStart" s-time)
                                (js/set new-src "loopEnd" e-time)
                                (js/call new-src "connect" (:out new-an))
                                (if (:source new-an) (do (.stop (:source new-an)) (.disconnect (:source new-an))) nil)
                                
                                (if (:loop new-an)
                                  (js/call new-src "start" 0 s-time)
                                  (js/call new-src "start" 0 s-time (math/abs (- e-time s-time))))
                                
                                (assoc new-an :source new-src))
                              new-an)))
                :on-load (fn [an buf name]
                           (let [ctx (js/get (:out an) "context")
                                 new-src (js/call ctx "createBufferSource")
                                 gain (:out an)
                                 s-time (or (:start an) 0.0)
                                 e-time (or (:end an) 10.0)]
                             (js/set new-src "buffer" buf)
                             (js/set new-src "loop" (:loop an))
                             (js/set new-src "loopStart" s-time)
                             (js/set new-src "loopEnd" e-time)
                             (js/call new-src "connect" gain)
                             
                             (if (:source an) (do (.stop (:source an)) (.disconnect (:source an))) nil)
                             
                             (if (:loop an)
                               (js/call new-src "start" 0 s-time)
                               (js/call new-src "start" 0 s-time (math/abs (- e-time s-time))))
                               
                             (js/call (js/get gain "gain") "setTargetAtTime" 1.0 (js/get ctx "currentTime") 0.05)
                             (assoc (assoc (assoc an :source new-src) :buffer buf) :loaded-name name)))}

   :media      {:category :source
                :label "Media Player"
                :inputs []
                :outputs [:out]
                :params [{:id :url :label "File URL" :options ["https://actions.google.com/sounds/v1/alarms/spaceship_alarm.ogg" "https://actions.google.com/sounds/v1/ambiences/coffee_shop.ogg"] :default "https://actions.google.com/sounds/v1/alarms/spaceship_alarm.ogg"}
                         {:id :looping :label "Loop?" :options ["true" "false"] :default "true"}]
                :create (fn [ctx params] (create-media-player ctx (:url params) (= (:looping params) "true")))
                :update (fn [an param val]
                          (let [source (:source an)]
                            (if (= param "looping")
                              (js/set source "loop" (= val "true"))
                              nil)))}

   :noise      {:category :source
                :label "White Noise"
                :inputs []
                :outputs [:out]
                :params [{:id :volume :label "Volume" :min 0.0 :max 1.0 :step 0.01 :default 0.2}]
                :create (fn [ctx params] (create-noise ctx (:volume params)))
                :update (fn [an param val]
                          (let [ctx (js/get (:gain an) "context")
                                now (js/get ctx "currentTime")
                                num-val (safe-float val)]
                            (do (js/call (js/get (:gain an) "gain") "setTargetAtTime" num-val now 0.05) nil)))}
                
   :destination {:category :output 
                 :label "Audio Output"
                 :inputs [:in] 
                 :outputs []
                 :params []
                 :create (fn [ctx params] 
                           (let [gain (js/call ctx "createGain")
                                 dest (js/get ctx "destination")
                                 stream-dest (js/call ctx "createMediaStreamDestination")]
                             (js/call gain "connect" dest)
                             (js/call gain "connect" stream-dest)
                             (js/set (js/global "window") "audioRecorderDest" stream-dest)
                             gain))
                 :update (fn [an param val] nil)} })

;; --------------------------------------------------------------------------
;; Application State (Re-frame DB)
;; --------------------------------------------------------------------------


;; --------------------------------------------------------------------------
;; Audio Processing Utilities (Ported from JS)
;; --------------------------------------------------------------------------

(defn make-distortion-curve [amount]
  (let [k (if amount amount 50)
        n-samples 44100
        curve (make-float32-array (int n-samples))
        deg (/ math/PI 180)]
    (loop [i 0]
      (if (< i n-samples)
        (let [x (- (* (/ (* i 2.0) n-samples)) 1.0)]
          (f32-set! curve i (/ (* (* (* (+ 3.0 k) x) 20.0) deg) (+ math/PI (* k (math/abs x)))))
          (recur (+ i 1)))
        (js/float32-buffer curve)))))

(defn make-impulse-response [ctx duration decay]
  (let [sr (js/get ctx "sampleRate")
        len (int (* sr duration))
        impulse (js/call ctx "createBuffer" 2 len sr)]
    (loop [i 0]
      (if (< i 2)
        (let [channel-arr (make-float32-array len)]
          (loop [j 0]
            (if (< j len)
              (do
                (f32-set! channel-arr j (* (- (* (math/random) 2.0) 1.0) (math/pow (- 1.0 (/ j len)) decay)))
                (recur (+ j 1)))
              nil))
          (js/call impulse "copyToChannel" (js/float32-buffer channel-arr) i)
          (recur (+ i 1)))
        impulse))))

(defn create-white-noise [ctx]
  (let [sr (js/get ctx "sampleRate")
        buf-size (int (* 2 sr))
        noise-buf (js/call ctx "createBuffer" 1 buf-size sr)
        noise-arr (make-float32-array buf-size)]
    (loop [i 0]
      (if (< i buf-size)
        (do
          (f32-set! noise-arr i (- (* (math/random) 2.0) 1.0))
          (recur (+ i 1)))
        nil))
    (js/call noise-buf "copyToChannel" (js/float32-buffer noise-arr) 0)
    (let [white-noise (js/call ctx "createBufferSource")]
      (js/set white-noise "buffer" noise-buf)
      (js/set white-noise "loop" true)
      (js/call white-noise "start" 0)
      white-noise)))

(defn create-eq [ctx low-gain mid-gain high-gain]
  (let [low (js/call ctx "createBiquadFilter")
        mid (js/call ctx "createBiquadFilter")
        high (js/call ctx "createBiquadFilter")]
    (js/set low "type" "lowshelf")
    (js/set (js/get low "frequency") "value" 250.0)
    (js/set (js/get low "gain") "value" (safe-float low-gain))
    
    (js/set mid "type" "peaking")
    (js/set (js/get mid "frequency") "value" 1000.0)
    (js/set (js/get mid "Q") "value" 1.0)
    (js/set (js/get mid "gain") "value" (safe-float mid-gain))
    
    (js/set high "type" "highshelf")
    (js/set (js/get high "frequency") "value" 4000.0)
    (js/set (js/get high "gain") "value" (safe-float high-gain))
    
    (js/call low "connect" mid)
    (js/call mid "connect" high)
    {:in low :low low :mid mid :high high :out high}))
    
(defn create-analyser [ctx]
  (let [analyser (js/call ctx "createAnalyser")
        window (js/global "window")]
    (js/set analyser "fftSize" 2048)
    (let [buffer-len (js/get analyser "frequencyBinCount")
          data-array (js/new (js/global "Uint8Array") buffer-len)]
      {:in analyser :out analyser :analyser analyser :data data-array})))
      
