Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek committed Sep 23, 2021
1 parent 21de7ef commit 1faa271
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 39 deletions.
68 changes: 30 additions & 38 deletions src/example/cljs_audio/app.cljs
Expand Up @@ -5,19 +5,19 @@
[cljs-audio.scheduling :as s]
[cljs-audio.time :as t]
[cljs-audio.modules :as m]
[cljs.core.async :refer [go <!]]
[cljs.core.async :refer [go <! take!]]
[cljs.core.async.interop :refer-macros [<p!]]
["standardized-audio-context" :refer (AudioContext)]))
["standardized-audio-context" :as sac]))

(defn graph [{:keys [ir kick time bpm freq]}]
(let [{:keys [frequency gain]} (s/schedule-parameters {:frequency at-time :gain (partial adsr 0.2 0.15 0.6 0.5 0.2)}
[{:frequency freq :gain 0.3 ::s/time (+ time 0)}
{:frequency (* 2 freq) :gain 0.3 ::s/time (+ time (t/seconds 120 "1/4"))}
{:frequency (* 4 freq) :gain 0.3 ::s/time (+ time (t/seconds 120 "1/4")
(t/seconds 120 "1/4"))}])]
[{:voice (m/simple-voice {:frequency frequency
:gain gain
:type "sawtooth"})
(t/seconds 120 "1/4"))}])]
[{:voice (m/simple-voice {:frequency frequency
:gain gain
:type "sawtooth"})
:sampled-kick (m/poly m/one-shot-sample
[{:buffer kick :start-time time :gain gain :rate 0.7}
{:buffer kick :start-time (+ time (t/seconds bpm "1/4")) :gain gain :rate 0.7}
Expand All @@ -28,9 +28,9 @@
; :gain gain
; :type "sine"})
;:reverb [:convolver {:buffer ir}]
:fx (m/multi-tap-delay {:times (mapv m/at-start [(t/seconds bpm "1/2")
(t/seconds bpm "1")
(t/seconds bpm "2")]) :gains (mapv m/at-start [0.5 0.25 0.125 0.05])})
:fx (m/multi-tap-delay {:times (mapv m/at-start [(t/seconds bpm "1/2")
(t/seconds bpm "1")
(t/seconds bpm "2")]) :gains (mapv m/at-start [0.5 0.25 0.125 0.05])})
;:vca [:gain {:gain 0.3}]
;:comp [:dynamics-compressor {:threshold -50 :knee 0 :ratio 20 :attack 0.005 :release 0.050}]
}
Expand All @@ -52,36 +52,28 @@

(def events ["touchstart" "touchend" "mousedown" "keydown"])

(defn fetch-sample [ctx path] (go (->> (js/fetch path)
(<p!)
(.arrayBuffer)
(<p!)
(.decodeAudioData ctx)
(<p!))))

(defn resume-audio-context []
(let [ctx (new AudioContext)]
(.then (.resume ctx)
(fn []
(go
(let [ir-audio-data (<! (fetch-sample ctx "resources/ir.wav"))
kick-audio-data (<! (fetch-sample ctx "resources/kick.wav"))
bpm 120]
(try
(doseq [ev events] (.removeEventListener js/document.body ev resume-audio-context))
(js/document.body.addEventListener "mousedown"
(fn [e]
(.preventDefault e)
(swap! audio wa/update-audio (graph {:kick kick-audio-data :freq (/ (.-offsetY e) 10) :ir ir-audio-data :time (.-currentTime ctx) :bpm bpm}))))
(js/document.body.addEventListener "touchstart"
(fn [e]
(.preventDefault e)
(let [touch (.item (.-changedTouches e) 0)]
(when (not (nil? touch))
(swap! audio wa/update-audio (graph {:kick kick-audio-data :freq (/ (.-pageY touch) 5) :ir ir-audio-data :time (.-currentTime ctx) :bpm bpm}))))
))
(reset! audio (wa/make-audio ctx nil))
(catch js/Error err (js/console.log (ex-cause err))))))))))
(let [audio (atom (wa/make-audio {:polyfill sac}))]
(take! (wa/resume @audio)
(fn []
(go
(let [ir-audio-data (<! (wa/fetch-sample @audio "resources/ir.wav"))
kick-audio-data (<! (wa/fetch-sample @audio "resources/kick.wav"))
bpm 120]
(try
(doseq [ev events] (.removeEventListener js/document.body ev resume-audio-context))
(js/document.body.addEventListener "mousedown"
(fn [e]
(.preventDefault e)
(swap! audio wa/update-audio (graph {:kick kick-audio-data :freq (/ (.-offsetY e) 10) :ir ir-audio-data :time (wa/current-time @audio) :bpm bpm}))))
(js/document.body.addEventListener "touchstart"
(fn [e]
(.preventDefault e)
(let [touch (.item (.-changedTouches e) 0)]
(when (not (nil? touch))
(swap! audio wa/update-audio (graph {:kick kick-audio-data :freq (/ (.-pageY touch) 5) :ir ir-audio-data :time (wa/current-time @audio) :bpm bpm}))))
))
(catch js/Error err (js/console.log (ex-cause err))))))))))

(defn main []
(doseq [ev events] (js/document.body.addEventListener ev resume-audio-context false)))
2 changes: 1 addition & 1 deletion src/main/cljs_audio/webaudio.cljs
Expand Up @@ -9,7 +9,7 @@
(let [polyfill (js->clj polyfill)
clazz (get polyfill "AudioContext" js/AudioContext)
ctx (new clazz)]
{:ctx ctx :patch [{} #{}] :env {} :stream stream :polyfill polyfill}))
{:ctx ctx :patch [{} #{}] :env {} :polyfill polyfill :stream stream}))

(defn update-audio [{:keys [ctx patch env polyfill stream]} new-patch]
(println :polyfill polyfill)
Expand Down

0 comments on commit 1faa271

Please sign in to comment.