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

Commit 316f6c6

Browse files
committed
fix startup-fn timing bug
1 parent 2336a5b commit 316f6c6

File tree

8 files changed

+153
-107
lines changed

8 files changed

+153
-107
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,23 @@ require('csound-wasm/release/browser/csound-wasm-browser.js');
5858
<body>
5959
<h5>Click Start realtime once, gotta love chrome's new autoplay ban policy</h5>
6060
<button id="start">Start realtime</button>
61-
<button onclick="csound.inputMessage('i 1 0 1')">Make beep!</h1>
62-
<script src="https://github.com/hlolli/csound-wasm/releases/download/6.10.0-4/csound-wasm-browser.js"></script>
61+
<button id="beeper">Make beep!</button>
62+
<script src="https://github.com/hlolli/csound-wasm/releases/download/6.11.0-0/csound-wasm-browser.js" />
6363
<script>
6464
const beeper = `
6565
instr 1
6666
asig = poscil:a(0.3, 440)
6767
outc asig, asig
6868
endin
6969
`
70-
document.getElementById('start').onclick = ()=> {
71-
csound.startRealtime();
72-
csound.compileOrc(beeper);
70+
document.getElementById('start').onclick = () => {
71+
csound.startRealtime();
72+
csound.compileOrc(beeper);
7373
};
74+
75+
document.getElementById('beeper').onclick = () => {
76+
csound.inputMessage('i 1 0 1');
77+
}
7478
</script>
7579
</body>
7680
</html>

release/browser/csound-wasm-browser.js

Lines changed: 30 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

release/browser/csound-wasm-worklet-processor.js

Lines changed: 35 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

release/browser/example4.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
</head>
6+
<body>
7+
<h5>Click Start realtime once, gotta love chrome's new autoplay ban policy</h5>
8+
<button id="start">Start realtime</button>
9+
<button id="beeper">Make beep!</button>
10+
<script>window["csound_worklet_processor_url"] = "./csound-wasm-worklet-processor.js"</script>
11+
<script src="./csound-wasm-browser.js"></script>
12+
<!-- <script src="https://github.com/hlolli/csound-wasm/releases/download/6.11.0-0/csound-wasm-browser.js"></script> -->
13+
<script>
14+
const beeper = `
15+
instr 1
16+
asig = poscil:a(0.3, 440)
17+
outc asig, asig
18+
endin
19+
`
20+
document.getElementById('start').onclick = () => {
21+
csound.startRealtime();
22+
csound.compileOrc(beeper);
23+
};
24+
25+
document.getElementById('beeper').onclick = () => {
26+
csound.inputMessage('i 1 0 1');
27+
}
28+
</script>
29+
</body>
30+
</html>

release/node/index.js

Lines changed: 25 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/csound_wasm/browser.cljs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@
128128
@public/worklet-message-queue)
129129
(reset! public/worklet-message-queue [])
130130
(reset! public/audio-worklet-node
131-
{:object node
132-
:post (fn [msg]
133-
(.postMessage node.port msg))}))
131+
{:object node
132+
:post (fn [msg]
133+
(.postMessage node.port msg))
134+
:context audio-context}))
134135
(println "unhandled message: " event))))))))
135136
(.catch (fn [err]
136137
(.warn js/console

src/csound_wasm/public.cljs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
(def audio-worklet-processor
1818
(if (exists? js/AudioWorkletProcessor)
19-
(atom (fn [msg] (println "Missed a message: " msg)))
19+
(atom {:post (fn [msg] (println "Missed a message: " msg))})
2020
(atom nil)))
2121

2222
(def event-queue (volatile! []))
@@ -100,14 +100,14 @@
100100
(vswap! event-queue conj #(eval-code orc)))))
101101

102102
(defn input-message [sco]
103-
(if-let [awn @audio-worklet-node]
104-
((:post awn) #js ["inputMessage" sco])
105-
(if @wasm-loaded?
106-
(((.-cwrap @libcsound)
107-
"CsoundObj_inputMessage"
108-
"number" #js ["number" "string"])
109-
@csound-instance sco)
110-
(vswap! event-queue conj #(input-message sco)))))
103+
(when-let [awn @audio-worklet-node]
104+
((:post awn) #js ["inputMessage" sco]))
105+
(if @wasm-loaded?
106+
(((.-cwrap @libcsound)
107+
"CsoundObj_inputMessage"
108+
"number" #js ["number" "string"])
109+
@csound-instance sco)
110+
(vswap! event-queue conj #(input-message sco))))
111111

112112
#_(defn string-to-c [str]
113113
(let [len (inc (bit-shift-left (.-length str) 2))]
@@ -207,7 +207,8 @@
207207
(if-let [awn @audio-worklet-node]
208208
((:post awn) #js ["reset"])
209209
(if @wasm-loaded?
210-
(do (((.-cwrap @libcsound) "CsoundObj_reset" nil #js ["number"])
210+
(do (reset! csound-running? :reset)
211+
(((.-cwrap @libcsound) "CsoundObj_reset" nil #js ["number"])
211212
@csound-instance))
212213
(vswap! event-queue conj #(reset)))))
213214

@@ -330,6 +331,7 @@
330331

331332
;;;; Initializers
332333

334+
(declare activate-init-callback)
333335

334336
(defn csound-new-object []
335337
(if-let [awn @audio-worklet-node]

src/csound_wasm/worklet_processor.cljs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
(def worklet-audio-fn (atom (fn [& r] true)))
1313

1414
(defn start-audio-fn []
15-
(if @public/csound-running?
16-
(.err js/console "Csound already running, can't start audio again.")
15+
(if (and (not (= :reset @public/csound-running?)) @public/csound-running?)
16+
(.error js/console "Csound already running, can't start audio again.")
1717
(let [libcsound @public/libcsound
1818
csound-instance @public/csound-instance
1919
ksmps ((libcsound.cwrap
@@ -54,6 +54,8 @@
5454
(when (zero? res)
5555
(public/perform-ksmps-event))
5656
res))]
57+
(when (:reset @public/csound-running?)
58+
(reset! public/csound-running? false))
5759
(reset! worklet-audio-fn
5860
(fn [inputs outputs parameters]
5961
(let [output (aget outputs 0)
@@ -112,9 +114,10 @@
112114
"promise"
113115
(handle-promise data)
114116
"setStartupFn"
115-
(vreset! public/startup-fn
116-
(case (aget data 1)
117-
"startRealtime" #(public/start-realtime (aget data 2))))
117+
(do (vreset! public/startup-fn
118+
(case (aget data 1)
119+
"startRealtime" #(public/start-realtime (aget data 2))))
120+
(when @public/wasm-initialized? (@public/startup-fn)))
118121
(apply (get public-functions key) (rest data))
119122
;;(.error js/console "Error unhandled key in processor: " key)
120123
)))

0 commit comments

Comments
 (0)