Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
fix async problems, release 5th minorversion
Browse files Browse the repository at this point in the history
  • Loading branch information
hlolli committed Nov 12, 2018
1 parent 8cc1887 commit 2850c9b
Show file tree
Hide file tree
Showing 11 changed files with 430 additions and 341 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "csound-wasm",
"version": "6.12.0-4",
"version": "6.12.0-5",
"author": "Hlödver Sigurdsson <[email protected]>",
"license": "GPL-3.0",
"keywords": [
Expand Down Expand Up @@ -29,6 +29,7 @@
"libcsound": "^6.12.0-0",
"midi": "^0.9.5",
"speaker": "^0.4.1",
"strip-ansi": "^5.0.0",
"utf-8-validate": "^5.0.1"
},
"devDependencies": {
Expand Down
116 changes: 61 additions & 55 deletions release/browser/csound-wasm-browser.js

Large diffs are not rendered by default.

119 changes: 62 additions & 57 deletions release/browser/csound-wasm-worklet-processor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion release/browser/main.js.map

Large diffs are not rendered by default.

141 changes: 81 additions & 60 deletions release/node/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion release/node/index.js.map

Large diffs are not rendered by default.

185 changes: 108 additions & 77 deletions src/csound_wasm/browser.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

(def audio-process-node-atom (atom nil))


(defn get-audio-input [config body]
(if (:microphone config)
(-> (.getUserMedia (.-mediaDevices js/navigator) #js {:video false :audio true})
(.then (fn [stream] stream))
(.catch (fn [err]
(println (str "Error opening microphone: " err))
(body nil))))
(body nil)))

(defn start-audio [config]
(when (= :reset @public/csound-running?)
(.disconnect @audio-process-node-atom)
Expand All @@ -16,81 +26,99 @@
(reset! public/csound-running? false))
(if (and (not (= :reset @public/csound-running?)) @public/csound-running?)
(.error js/console "Csound already running, can't start audio again.")
(let [csound-instance @public/csound-instance
libcsound @public/libcsound
buffer-size (:buffer config)
ksmps ((libcsound.cwrap
"CsoundObj_getKsmps"
#js ["number"] #js ["number"])
csound-instance)
input-count ((libcsound.cwrap
"CsoundObj_getInputChannelCount"
#js ["number"] #js ["number"])
csound-instance)
output-count ((libcsound.cwrap
"CsoundObj_getOutputChannelCount"
#js ["number"] #js ["number"])
csound-instance)
actx (or js/window.AudioContext js/window.webkitAudioContext)
audio-context (new actx)
audio-process-node (.createScriptProcessor
audio-context
buffer-size input-count output-count)
_ (do (set! (.-inputCount ^js audio-process-node) input-count)
(set! (.-outputCount ^js audio-process-node) output-count))
buffer-size (.-bufferSize audio-process-node)
frame-len (* ksmps output-count)
output-pointer ((libcsound.cwrap
"CsoundObj_getOutputBuffer"
#js ["number"] #js ["number"])
csound-instance)
output-buffer (new js/Float64Array (.-buffer (.-HEAP8 ^js libcsound))
^js output-pointer frame-len)
;; TODO add microphone input buffer
zerodbfs ((libcsound.cwrap
"CsoundObj_getZerodBFS"
#js ["number"] #js ["number"])
csound-instance)
range-output-cnt (range output-count)
perform-ksmps-fn (fn []
(let [res ((libcsound.cwrap
"CsoundObj_performKsmps"
#js ["number"] #js ["number"])
csound-instance)]
(when-not @public/csound-running?
(public/dispatch-event "csoundStarted" nil)
(reset! public/csound-running? true))
(when (zero? res)
(public/perform-ksmps-event))
res))
debug @public/csound-running?]
(set! (.-onaudioprocess audio-process-node)
(fn [e]
(let [output (.-outputBuffer e)
len (.-length (.getChannelData output 0))]
(loop [res (perform-ksmps-fn)
i 0
cnt 0]
(if (not= 0 res)
(do (.disconnect audio-process-node)
(set! (.-onaudioprocess audio-process-node) nil)
(reset! public/csound-running? false))
(when (< i len)
(if (< cnt ksmps)
(do
(dotimes [chn output-count]
(aset (.getChannelData output chn)
i
(/ (aget output-buffer (+ chn (* cnt output-count))) zerodbfs)))
(recur res (inc i) (inc cnt)))
(let [res (perform-ksmps-fn)]
(recur res
i
0))))))
true )))
(reset! audio-process-node-atom audio-process-node)
(.connect audio-process-node (.-destination audio-context))
nil)))
(get-audio-input
config
(fn [input-stream]
(let [csound-instance @public/csound-instance
libcsound @public/libcsound
buffer-size (:buffer config)
ksmps ((libcsound.cwrap
"CsoundObj_getKsmps"
#js ["number"] #js ["number"])
csound-instance)
input-count ((libcsound.cwrap
"CsoundObj_getInputChannelCount"
#js ["number"] #js ["number"])
csound-instance)
output-count ((libcsound.cwrap
"CsoundObj_getOutputChannelCount"
#js ["number"] #js ["number"])
csound-instance)
actx (or js/window.AudioContext js/window.webkitAudioContext)
audio-context (new actx)
audio-process-node (.createScriptProcessor
audio-context
buffer-size input-count output-count)
_ (do (set! (.-inputCount ^js audio-process-node) input-count)
(set! (.-outputCount ^js audio-process-node) output-count))
input-process-node (when input-stream
(.createMediaStreamSource audio-context input-stream))
buffer-size (.-bufferSize audio-process-node)
frame-len (* ksmps output-count)
input-pointer ((libcsound.cwrap
"CsoundObj_getInputBuffer"
#js ["number"] #js ["number"])
csound-instance)
input-buffer (when input-stream
(new js/Float64Array (.-buffer (.-HEAP8 ^js libcsound))
^js input-pointer (* ksmps input-count)))
output-pointer ((libcsound.cwrap
"CsoundObj_getOutputBuffer"
#js ["number"] #js ["number"])
csound-instance)
output-buffer (new js/Float64Array (.-buffer (.-HEAP8 ^js libcsound))
^js output-pointer frame-len)
;; TODO add microphone input buffer
zerodbfs ((libcsound.cwrap
"CsoundObj_getZerodBFS"
#js ["number"] #js ["number"])
csound-instance)
range-output-cnt (range output-count)
perform-ksmps-fn (fn []
(let [res ((libcsound.cwrap
"CsoundObj_performKsmps"
#js ["number"] #js ["number"])
csound-instance)]
(when-not @public/csound-running?
(public/dispatch-event "csoundStarted" nil)
(reset! public/csound-running? true))
(when (zero? res)
(public/perform-ksmps-event))
res))
debug @public/csound-running?]
(set! (.-onaudioprocess audio-process-node)
(fn [e]
(let [output (.-outputBuffer e)
len (.-length (.getChannelData output 0))]
(loop [res (perform-ksmps-fn)
i 0
cnt 0]
(if (not= 0 res)
(do (.disconnect audio-process-node)
(set! (.-onaudioprocess audio-process-node) nil)
(reset! public/csound-running? false))
(when (< i len)
(if (< cnt ksmps)
(do
(dotimes [chn output-count]
(aset (.getChannelData output chn)
i
(/ (aget output-buffer (+ chn (* cnt output-count))) zerodbfs))
;; (input-buffer)
)
(recur res (inc i) (inc cnt)))
(let [res (perform-ksmps-fn)]
(recur res
i
0))))))
true )))
;; (set! (.-onaudioprocess input-process-node)
;; (fn [e] (prn e)))
(reset! audio-process-node-atom audio-process-node)
(.connect audio-process-node (.-destination audio-context))
(when input-stream
(.connect audio-process-node (.-destination audio-context)))
nil)))))

(if (exists? js/AudioWorklet)
(do
Expand All @@ -114,7 +142,7 @@
(if (exists? ^js js/window.csound_worklet_processor_url)
^js js/window.csound_worklet_processor_url
(str "https://s3.amazonaws.com/hlolli/csound-wasm/"
"6.12.0-4"
"6.12.0-5"
"/csound-wasm-worklet-processor.js")))
(.then
(fn []
Expand All @@ -132,14 +160,17 @@
(apply public/dispatch-event (rest (.-data event)))
"workletProcessorReady"
(do (public/activate-init-callback nil)
(run! #(.postMessage node.port %)
(run! #(when (= "instanciateLibcsound" (aget % 0))
(.postMessage node.port %))
@public/worklet-message-queue)
(reset! public/worklet-message-queue [])
(reset! public/audio-worklet-node
{:object node
:post (fn [msg]
(.postMessage node.port msg))
:context audio-context}))
"workletProcessorWasmReady"
(public/run-event-queue)
(println "unhandled message: " event))))))))
(.catch (fn [err]
(js-delete component "prototype")
Expand Down
5 changes: 3 additions & 2 deletions src/csound_wasm/browser_shared.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
(-> (public/write-to-fs blobs root-dir)
(.then (fn [filenames]
(@resolver filenames)))))))))]
(-> (wrap-promise callback)
(.then (fn [res] promise-return)))))
(wrap-promise callback)
;; (-> (.then (fn [res] promise-return)))
promise-return))


(defn enable-midi []
Expand Down
Loading

0 comments on commit 2850c9b

Please sign in to comment.