 (ns klangmeister.instruments)
+
+(defn run [ugen at context]
+  (ugen at context))
+
+(defn plug [param input at context]
+  "Plug an input into an audio parameter,
+  accepting both numbers and ugens."
+  (if (fn? input)
+    (.connect (input at context) param)
+    (.setValueAtTime param input at)))
 
 (defn gain [level]
   (fn [at context]
     (doto (.createGain context)
-      (-> .-gain (.setValueAtTime level at)))))
-
+      (-> .-gain (plug level at context)))))
 
 (defn line
-  "Build a line out of [x y] coordinates, starting at [0 0]."
+  "Build a line out of [dx y] coordinates, starting at [0 0]."
   [& corners]
   (fn [at context]
     (let [node (.createGain context)


(defn blend [ugen1 ugen2]
  (fn [at context]
    (let [one (ugen1 at context)
          two (ugen2 at context)
          sink ((gain 1.0) at context)]
      (.connect one sink)
      (.connect two sink)
      sink)))

(defn >< [& nodes]
  (reduce blend nodes))

(def saw (partial oscillator "sawtooth"))
(def square (partial oscillator "square"))

(defn biquad-filter [type freq]
  (fn [at context]
    (doto (.createBiquadFilter context)
      (-> .-frequency (plug freq at context))
      (-> .-type (set! type)))))

(def lpf (partial biquad-filter "lowpass"))
