Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/cljs_bach/synthesis.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,45 @@
(def ^:export square (partial oscillator "square"))
(def ^:export triangle (partial oscillator "triangle"))

; Inputs (constant source nodes)

(defn set-input! [input val]
"Set the value of an input (with immediate effect)."
(set! (-> input .-offset .-value) val))

(defn make-input! [context init-val]
"Create a mutable 'input' (constant source node)."
(let [input (.createConstantSource context)]
(doto input
(.start)
(set-input! init-val))))

(defn ramp-end-time [input target vel]
"Calculates the end time (according to the clock of the given
input's audio context) for ramping input to the target value at a
rate of vel (which is the value-change per second)."
(let [now (-> input .-context .-currentTime)
current-val (-> input .-offset .-value)
val-diff (.abs js/Math (- current-val target))
ramp-duration (/ val-diff vel)]
(+ now ramp-duration)))

(defn lin-ramp-input! [input target vel]
"Linearly ramp the given input's value toward the target value at
the given change/sec velociy."
(let [end-time (ramp-end-time input target vel)]
(-> input .-offset (.linearRampToValueAtTime target end-time))))

(defn exp-ramp-input! [input target vel]
"Exponentially ramp the given input's value toward the target value
at the given change/sec velociy."
(let [end-time (ramp-end-time input target vel)]
(-> input .-offset (.exponentialRampToValueAtTime target end-time))))

(defn jack [input]
"Wrap an input so that it can be used as a source node."
(fn [context at duration]
(subgraph input)))

; Filters

Expand Down