;; ==============================================================================
;; Amplifocus Art / Procedural Audio Library
;; ==============================================================================

(require "libs/core/src/core.coni" :all)
(require "libs/math/src/math.coni" :as math)
(require "libs/js-game/src/audio.coni" :as audio)

(defn mod-num [n m]
  (let [fn (float n) fm (float m)]
    (- fn (* (math/floor (/ fn fm)) fm))))

(defn speak-text [text]
  (let [win (js/global "window")
        synth (.-speechSynthesis win)]
    (when (not (nil? synth))
      (let [u (js/new (js/global "SpeechSynthesisUtterance") text)
            voices (.getVoices synth)
            num-voices (if (nil? voices) 0 (.-length voices))]
        (loop [i 0 found false]
          (if (and (< i num-voices) (not found))
            (let [v (.item voices i)
                  vname (.-name v)]
              (if (or (> (.indexOf vname "Samantha") -1)
                      (> (.indexOf vname "Victoria") -1)
                      (> (.indexOf vname "Karen") -1))
                (do (.-voice u v) (recur (+ i 1) true))
                (recur (+ i 1) false)))
            nil))
        (doto u
          (.-rate 0.35)
          (.-pitch 0.9)
          (.-volume 0.7))
        (.speak synth u)))))

(defn play-pad [freq time dur osc-type vol]
  (let [ctx (audio/audio-ctx)]
    (when (not (nil? ctx))
      (let [osc (.createOscillator ctx)
            g (.createGain ctx)]
        (.-type osc osc-type)
        (.setValueAtTime (.-frequency osc) freq time)
        
        (doto (.-gain g)
          (.setValueAtTime 0.0 time)
          (.linearRampToValueAtTime vol (+ time (* dur 0.3)))
          (.setValueAtTime vol (+ time (* dur 0.7)))
          (.linearRampToValueAtTime 0.001 (+ time dur)))
        
        (doto osc
          (.connect g)
          (.start time)
          (.stop (+ time dur 0.1)))
        (.connect g (audio/master))))))

(def *music-gain* (atom nil))

(defn get-music-gain []
  (let [ctx (audio/audio-ctx)]
    (if (nil? @*music-gain*)
      (let [g (.createGain ctx)]
        (.connect g (audio/master))
        (reset! *music-gain* g)
        g)
      @*music-gain*)))

(defn play-stereo-pad [freq time dur osc-type vol pan]
  (let [ctx (audio/audio-ctx)]
    (when (not (nil? ctx))
      (let [osc (.createOscillator ctx)
            g (.createGain ctx)
            panner (.createStereoPanner ctx)]
        (.-type osc osc-type)
        (.setValueAtTime (.-frequency osc) freq time)
        (.setValueAtTime (.-pan panner) pan time)
        
        (doto (.-gain g)
          (.setValueAtTime 0.0 time)
          (.linearRampToValueAtTime vol (+ time (* dur 0.3)))
          (.setValueAtTime vol (+ time (* dur 0.7)))
          (.linearRampToValueAtTime 0.001 (+ time dur)))
        
        (doto osc
          (.connect g)
          (.start time)
          (.stop (+ time dur 0.1)))
        (.connect g panner)
        (.connect panner (get-music-gain))))))

(defn play-bouncing-ball [time base-freq pan]
  (let [ctx (audio/audio-ctx)]
    (when (not (nil? ctx))
      (loop [i 0 
             curr-time time 
             curr-freq base-freq 
             curr-vol 0.4 
             curr-dur 0.7]
        (if (< i 10)
          (let [osc (.createOscillator ctx)
                g (.createGain ctx)
                panner (.createStereoPanner ctx)]
            (.-type osc "sine")
            (.setValueAtTime (.-frequency osc) curr-freq curr-time)
            (.setValueAtTime (.-pan panner) pan curr-time)
            
            (doto (.-gain g)
              (.setValueAtTime 0.0 curr-time)
              (.linearRampToValueAtTime curr-vol (+ curr-time 0.01))
              (.exponentialRampToValueAtTime 0.001 (+ curr-time curr-dur)))
            
            (doto osc
              (.connect g)
              (.start curr-time)
              (.stop (+ curr-time curr-dur 0.05)))
              
            (.connect g panner)
            (.connect panner (get-music-gain))
            
            (recur (+ i 1) 
                   (+ curr-time curr-dur)
                   (- curr-freq 15.0) 
                   (* curr-vol 0.75)  
                   (* curr-dur 0.82))) 
          nil)))))

(defn relaxing-music-fn [step t beat-len]
  (let [beat-in-bar (mod-num step 8)]
    (when (or (= beat-in-bar 0.0) (= beat-in-bar 3.0))
      (audio/play-sfx 60 30 (* beat-len 1.5) "sine" 0.6))
      
    (when (= (mod-num step 16) 0.0)
      (let [freq-base (+ 150.0 (* (math/random) 100.0))]
        (audio/play-note freq-base t (* beat-len 8.0) "sine" 0.1)
        (audio/play-note (* freq-base 1.25) t (* beat-len 8.0) "sine" 0.08)
        (audio/play-note (* freq-base 1.5) t (* beat-len 8.0) "triangle" 0.05)))
        
    (when (< (math/random) 0.3)
      (let [r (math/random)
            freq (if (< r 0.16) 261.63
                   (if (< r 0.33) 293.66
                     (if (< r 0.5) 329.63
                       (if (< r 0.66) 392.00
                         (if (< r 0.83) 440.00
                           523.25)))))]
        (audio/play-note freq t (* beat-len 2.0) "sine" 0.08)))))

(defn space-music-fn [step t beat-len]
  (let [beat8 (mod-num step 8)
        beat16 (mod-num step 16)
        beat32 (mod-num step 32)]
    
    (when (= (mod-num step 2) 0.0)
      (audio/play-sfx 90 30 0.15 "sine" 0.7))
      
    (when (= (mod-num step 2) 1.0)
      (audio/play-sfx 4000 1000 0.05 "square" 0.04))
      
    (let [bass-freq (if (< beat32 16.0) 55.0 43.65)]
      (audio/play-note bass-freq t (* beat-len 0.8) "triangle" 0.35)
      (when (= (mod-num step 4) 3.0)
        (audio/play-note (* bass-freq 2.0) t (* beat-len 0.5) "triangle" 0.3)))

    (let [note-idx (math/floor (mod-num step 8))
          freq (if (< beat32 16.0)
                 (if (= note-idx 0.0) 220.0
                  (if (= note-idx 1.0) 261.63
                   (if (= note-idx 2.0) 329.63
                    (if (= note-idx 3.0) 392.00
                     (if (= note-idx 4.0) 493.88
                      (if (= note-idx 5.0) 329.63
                       (if (= note-idx 6.0) 261.63
                        392.00)))))))
                 (if (= note-idx 0.0) 174.61
                  (if (= note-idx 1.0) 220.0
                   (if (= note-idx 2.0) 261.63
                    (if (= note-idx 3.0) 329.63
                     (if (= note-idx 4.0) 440.00
                      (if (= note-idx 5.0) 329.63
                       (if (= note-idx 6.0) 261.63
                        440.00))))))))]
       (audio/play-note freq t (* beat-len 0.5) "sine" 0.2))
       
    (when (= beat16 0.0)
       (let [pad-root (if (< beat32 16.0) 220.0 174.61)]
         (audio/play-note pad-root t (* beat-len 16.0) "sine" 0.12)
         (audio/play-note (* pad-root 1.5) t (* beat-len 16.0) "sine" 0.1)
         (audio/play-note (* pad-root 2.0) t (* beat-len 16.0) "triangle" 0.06)))))

(defn stereo-ambient-fn [step t beat-len]
  (let [beat16 (mod-num step 16)
        beat32 (mod-num step 32)
        beat64 (mod-num step 64)]
    
    (when (= beat16 0.0)
      (let [base-freq (if (< beat32 16.0) 87.31 110.0)] 
        (play-stereo-pad (/ base-freq 2.0) t (* beat-len 16.0) "sine" 0.8 0.0)
        (play-stereo-pad base-freq t (* beat-len 16.0) "triangle" 0.45 0.0)
        (play-stereo-pad (+ base-freq 0.5) t (* beat-len 16.0) "sine" 0.4 -1.0)
        (play-stereo-pad (- base-freq 0.4) t (* beat-len 16.0) "sine" 0.4 1.0)))
        
    (when (= (mod-num step 8) 0.0)
      (let [pad-root (if (< beat32 16.0) 174.61 220.0)]
        (play-stereo-pad pad-root t (* beat-len 8.0) "sine" 0.2 -1.0)
        (play-stereo-pad (+ pad-root 1.2) t (* beat-len 8.0) "triangle" 0.15 1.0)
        (play-stereo-pad (* pad-root 1.5) t (* beat-len 8.0) "sine" 0.15 1.0)
        (play-stereo-pad (* pad-root 2.0) t (* beat-len 8.0) "triangle" 0.1 -1.0)))
        
    (let [beat-in-bar (mod-num step 4)]
      (when (or (= beat-in-bar 0.0) (= beat-in-bar 2.5))
        (when (< (math/random) 0.6)
          (let [idx (math/floor (* (math/random) 4.0))
                freq (if (< beat32 16.0)
                       (if (= idx 0.0) 349.23
                        (if (= idx 1.0) 440.00
                         (if (= idx 2.0) 523.25
                          659.25)))
                       (if (= idx 0.0) 440.00
                        (if (= idx 1.0) 523.25
                         (if (= idx 2.0) 659.25
                          783.99))))
                pan-val (if (> (math/random) 0.5) 1.0 -1.0)] 
            (play-stereo-pad freq t (* beat-len 4.0) "sine" 0.2 pan-val)))))))

(defn mysterious-ambient-fn [step t beat-len]
  (let [beat16 (mod-num step 16)
        beat32 (mod-num step 32)
        beat64 (mod-num step 64)]
    
    (when (= beat16 0.0)
      (let [base-freq (if (< beat64 16.0) 73.42
                       (if (< beat64 32.0) 58.27
                        (if (< beat64 48.0) 55.00
                         77.78)))]
        (play-stereo-pad (/ base-freq 2.0) t (* beat-len 16.0) "sine" 0.8 0.0)
        (play-stereo-pad base-freq t (* beat-len 16.0) "triangle" 0.45 0.0)
        (play-stereo-pad (+ base-freq 0.5) t (* beat-len 16.0) "sine" 0.4 -1.0)
        (play-stereo-pad (- base-freq 0.4) t (* beat-len 16.0) "sine" 0.4 1.0)))
        
    (when (= (mod-num step 8) 0.0)
      (let [pad-root (if (< beat64 16.0) 146.83 
                      (if (< beat64 32.0) 116.54 
                       (if (< beat64 48.0) 110.00 
                        155.56)))] 
        (play-stereo-pad pad-root t (* beat-len 8.0) "sine" 0.2 -1.0)
        (play-stereo-pad (* pad-root 1.189) t (* beat-len 8.0) "triangle" 0.15 1.0) 
        (play-stereo-pad (* pad-root 1.498) t (* beat-len 8.0) "sine" 0.15 1.0)     
        (play-stereo-pad (* pad-root 2.0) t (* beat-len 8.0) "triangle" 0.1 -1.0)))
        
    (when (= (mod-num step 4) 2.0)
      (when (< (math/random) 0.4)
        (let [freq (+ 400.0 (* (math/random) 500.0))
              pan (if (> (math/random) 0.5) 1.0 -1.0)]
          (play-bouncing-ball t freq pan))))
          
    (when (and (= (mod-num step 8) 6.0) (< (math/random) 0.5))
       (let [pan (if (> (math/random) 0.5) 0.8 -0.8)]
         (play-stereo-pad (+ 1000.0 (* (math/random) 800.0)) t (* beat-len 6.0) "sine" 0.08 pan)))))

(defn dreamy-ambient-fn [step t beat-len]
  (let [beat16 (mod-num step 16)
        beat32 (mod-num step 32)
        beat64 (mod-num step 64)]
    
    ;; Deep continuous sustaining sub-bass (every 16 beats)
    (when (= beat16 0.0)
      (let [base-freq (if (< beat64 32.0) 65.41 ;; C2
                       87.31)] ;; F2
        (play-stereo-pad (/ base-freq 2.0) t (* beat-len 16.0) "sine" 0.7 0.0)
        (play-stereo-pad base-freq t (* beat-len 16.0) "triangle" 0.35 0.0)
        (play-stereo-pad (+ base-freq 0.3) t (* beat-len 16.0) "sine" 0.3 -0.8)
        (play-stereo-pad (- base-freq 0.2) t (* beat-len 16.0) "sine" 0.3 0.8)))
        
    ;; Lush dreamy maj7 / maj9 pads (every 8 beats)
    (when (= (mod-num step 8) 0.0)
      (let [pad-root (if (< beat64 32.0) 130.81 ;; C3
                      174.61)] ;; F3
        (play-stereo-pad pad-root t (* beat-len 8.0) "sine" 0.15 -1.0)
        ;; major third
        (play-stereo-pad (* pad-root 1.259) t (* beat-len 8.0) "triangle" 0.12 1.0) 
        ;; perfect fifth
        (play-stereo-pad (* pad-root 1.498) t (* beat-len 8.0) "sine" 0.1 0.5)     
        ;; major seventh for that dreamy floaty feel
        (play-stereo-pad (* pad-root 1.887) t (* beat-len 8.0) "triangle" 0.08 -0.5)
        ;; major ninth
        (play-stereo-pad (* pad-root 2.245) t (* beat-len 8.0) "sine" 0.06 1.0)))
        
    ;; Ethereal high notes (pentatonic) ping-ponging in stereo
    (let [beat-in-bar (mod-num step 4)]
      (when (or (= beat-in-bar 0.0) (= beat-in-bar 2.0) (= beat-in-bar 3.5))
        (when (< (math/random) 0.7)
          (let [idx (math/floor (* (math/random) 5.0))
                freq (if (< beat64 32.0)
                       ;; C major pentatonic
                       (if (= idx 0.0) 523.25
                        (if (= idx 1.0) 587.33
                         (if (= idx 2.0) 659.25
                          (if (= idx 3.0) 783.99
                           880.00))))
                       ;; F major pentatonic
                       (if (= idx 0.0) 698.46
                        (if (= idx 1.0) 783.99
                         (if (= idx 2.0) 880.00
                          (if (= idx 3.0) 1046.50
                           1174.66)))))
                pan-val (if (> (math/random) 0.5) 0.8 -0.8)] 
            (play-stereo-pad freq t (* beat-len 5.0) "sine" 0.15 pan-val)))))))

(defn stop-ambient-music! []
  (let [ctx (audio/audio-ctx)]
    (when (not (nil? ctx))
      (let [g (get-music-gain)
            t (.-currentTime ctx)]
        (.cancelScheduledValues (.-gain g) t)
        (.setValueAtTime (.-gain g) (.-value (.-gain g)) t)
        (.linearRampToValueAtTime (.-gain g) 0.0 (+ t 0.2))))))

(defn start-ambient-music! []
  (let [ctx (audio/audio-ctx)]
    (when (not (nil? ctx))
      (let [g (get-music-gain)
            t (.-currentTime ctx)]
        (.cancelScheduledValues (.-gain g) t)
        (.setValueAtTime (.-gain g) (.-value (.-gain g)) t)
        (.linearRampToValueAtTime (.-gain g) 1.0 (+ t 0.2))))))

(defn play-kick [time]
  (let [ctx (audio/audio-ctx)]
    (when (not (nil? ctx))
      (let [osc (.createOscillator ctx)
            g (.createGain ctx)]
        (.-type osc "sine")
        (.setValueAtTime (.-frequency osc) 150.0 time)
        (.exponentialRampToValueAtTime (.-frequency osc) 0.001 (+ time 0.5))
        
        (.setValueAtTime (.-gain g) 1.0 time)
        (.exponentialRampToValueAtTime (.-gain g) 0.001 (+ time 0.5))
        
        (doto osc
          (.connect g)
          (.start time)
          (.stop (+ time 0.5)))
        (.connect g (get-music-gain))))))

(defn play-hihat [time vol]
  (let [ctx (audio/audio-ctx)]
    (when (not (nil? ctx))
      ;; Simple hi-hat using high-passed square wave
      (let [osc (.createOscillator ctx)
            f (.createBiquadFilter ctx)
            g (.createGain ctx)]
        (.-type osc "square")
        (.-type f "highpass")
        (.setValueAtTime (.-frequency osc) 8000.0 time)
        (.setValueAtTime (.-frequency f) 7000.0 time)
        
        (.setValueAtTime (.-gain g) vol time)
        (.exponentialRampToValueAtTime (.-gain g) 0.001 (+ time 0.1))
        
        (doto osc
          (.connect f)
          (.start time)
          (.stop (+ time 0.1)))
        (.connect f g)
        (.connect g (get-music-gain))))))

(defn techno-beat-fn [step t beat-len]
  (let [beat16 (mod-num step 16)
        beat32 (mod-num step 32)
        beat4 (mod-num step 4)]
    
    ;; 4-on-the-floor Kick Drum
    (when (= beat4 0.0)
      (play-kick t))
      
    ;; Off-beat Hi-Hat
    (when (= beat4 2.0)
      (play-hihat t 0.4))
      
    ;; 16th note closed hi-hats
    (when (not (= beat4 2.0))
      (play-hihat t 0.1))
      
    ;; Driving Bassline
    (let [bass-freq (if (< beat32 16.0) 43.65 49.00)]
      (when (not (= beat4 0.0))
        (audio/play-note bass-freq t (* beat-len 0.5) "sawtooth" 0.3))
      (when (= beat4 3.0)
        (audio/play-note (* bass-freq 2.0) t (* beat-len 0.5) "square" 0.2)))
        
    ;; Synth Chords on the 1
    (when (= beat16 0.0)
      (let [root (if (< beat32 16.0) 174.61 196.00)] 
        (play-stereo-pad root t (* beat-len 2.0) "sawtooth" 0.1 -0.5)
        (play-stereo-pad (* root 1.189) t (* beat-len 2.0) "sawtooth" 0.1 0.5)
        (play-stereo-pad (* root 1.498) t (* beat-len 2.0) "sawtooth" 0.1 0.0)))))
