Skip to content

Commit 8873116

Browse files
committed
working on making WebAudio Csound work as ES6 Modules
1 parent f39a125 commit 8873116

File tree

6 files changed

+71
-68
lines changed

6 files changed

+71
-68
lines changed

Emscripten/build.sh

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,32 @@ emcc -s LINKABLE=1 -s ASSERTIONS=0 ../src/CsoundObj.c -I../../include -Iinclude
2525
# 65536 * 1024 * 4 is 268435456
2626

2727
# Keep exports in alphabetical order please, to correlate with CsoundObj.js.
28+
## First build for WASM/ScriptProcessorNode (async compilation = 1, assertions = 0)
29+
emcc -v -O2 -g4 -DINIT_STATIC_MODULES=0 -s WASM=1 -s ASSERTIONS=0 -s "BINARYEN_METHOD='native-wasm'" -s LINKABLE=1 -s RESERVED_FUNCTION_POINTERS=1 -s TOTAL_MEMORY=268435456 -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=0 -s SINGLE_FILE=1 -s BINARYEN_ASYNC_COMPILATION=1 -s MODULARIZE=1 -s EXPORT_NAME=\"'libcsound'\" --pre-js ../src/FileList.js -s EXTRA_EXPORTED_RUNTIME_METHODS='["FS", "ccall", "cwrap"]' CsoundObj.bc FileList.bc libcsound.a ../deps/libsndfile-1.0.25/libsndfile-wasm.a -o libcsound.js
2830

2931

30-
## First build for WASM/ScriptProcessorNode (async compilation = 1, assertions = 1)
31-
emcc -v -O2 -g4 -DINIT_STATIC_MODULES=0 -s WASM=1 -s ASSERTIONS=1 -s "BINARYEN_METHOD='native-wasm'" -s LINKABLE=1 -s RESERVED_FUNCTION_POINTERS=1 -s TOTAL_MEMORY=268435456 -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=0 -s BINARYEN_ASYNC_COMPILATION=1 -s MODULARIZE=1 -s EXPORT_NAME=\"'libcsound'\" -s EXTRA_EXPORTED_RUNTIME_METHODS='["FS", "ccall", "cwrap"]' CsoundObj.bc FileList.bc libcsound.a ../deps/libsndfile-1.0.25/libsndfile-wasm.a -o libcsound.js
32-
3332
## Second build for WASM/AudioWorklet (async compilation = 0, assertions = 0)
34-
emcc -v -O2 -g4 -DINIT_STATIC_MODULES=0 -s WASM=1 -s ASSERTIONS=0 -s "BINARYEN_METHOD='native-wasm'" -s LINKABLE=1 -s RESERVED_FUNCTION_POINTERS=1 -s TOTAL_MEMORY=268435456 -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=0 -s BINARYEN_ASYNC_COMPILATION=0 -s MODULARIZE=1 -s EXPORT_NAME=\"'libcsound'\" -s EXTRA_EXPORTED_RUNTIME_METHODS='["FS", "ccall", "cwrap"]' CsoundObj.bc FileList.bc libcsound.a ../deps/libsndfile-1.0.25/libsndfile-wasm.a -o libcsound-worklet.js
33+
emcc -v -O2 -g4 -DINIT_STATIC_MODULES=0 -s WASM=1 -s ASSERTIONS=0 -s "BINARYEN_METHOD='native-wasm'" -s LINKABLE=1 -s RESERVED_FUNCTION_POINTERS=1 -s TOTAL_MEMORY=268435456 -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=0 -s SINGLE_FILE=1 -s BINARYEN_ASYNC_COMPILATION=0 -s MODULARIZE=1 -s EXPORT_NAME=\"'libcsound'\" --pre-js ../src/FileList.js -s EXTRA_EXPORTED_RUNTIME_METHODS='["FS", "ccall", "cwrap"]' CsoundObj.bc FileList.bc libcsound.a ../deps/libsndfile-1.0.25/libsndfile-wasm.a -o libcsound-worklet.js
3534

36-
node ../convert.js
35+
# node ../convert.js
3736

38-
echo "AudioWorkletGlobalScope.libcsound = libcsound" >> libcsound.js
39-
echo "AudioWorkletGlobalScope.libcsound = libcsound" >> libcsound-worklet.js
37+
## --post-js does not work with MODULARIZE...
38+
echo "export default libcsound;" >> libcsound.js
39+
echo "export default libcsound;" >> libcsound-worklet.js
4040

4141
cd ..
4242
rm -rf dist
4343
mkdir dist
44-
cp src/FileList.js dist/
44+
## cp src/FileList.js dist/
4545
cp src/CsoundProcessor.js dist/
4646
cp src/CsoundNode.js dist/
4747
cp src/CsoundScriptProcessorNode.js dist/
4848
cp src/CsoundObj.js dist/
4949
cp src/csound.js dist/
5050
cp build/libcsound.js dist/
51-
cp build/libcsound.wasm dist/
5251
cp build/libcsound-worklet.js dist/
53-
cp build/libcsound-worklet.wasm.js dist/
52+
#cp build/libcsound.wasm dist/
53+
#cp build/libcsound-worklet.js dist/
54+
#cp build/libcsound-worklet.wasm.js dist/
5455

5556

Emscripten/src/CsoundNode.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,9 @@ class CsoundNodeFactory {
289289
static importScripts(script_base='./') {
290290
let actx = CSOUND_AUDIO_CONTEXT;
291291
return new Promise( (resolve) => {
292-
actx.audioWorklet.addModule(script_base + 'libcsound-worklet.wasm.js').then(() => {
293-
actx.audioWorklet.addModule(script_base + 'libcsound-worklet.js').then(() => {
294-
actx.audioWorklet.addModule(script_base + 'CsoundProcessor.js').then(() => {
295-
resolve();
296-
}) }) })
292+
actx.audioWorklet.addModule(script_base + 'CsoundProcessor.js').then(() => {
293+
resolve();
294+
})
297295
})
298296
}
299297

@@ -313,5 +311,6 @@ class CsoundNodeFactory {
313311
}
314312

315313

314+
window.CsoundNodeFactory = CsoundNodeFactory;
316315

317-
316+
export default {CsoundNode, CsoundNodeFactory};

Emscripten/src/CsoundObj.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ if(typeof AudioWorkletNode !== 'undefined' &&
5959
const csound_load_script = function(src, callback) {
6060
var script = document.createElementNS("http://www.w3.org/1999/xhtml", "script");
6161
script.src = src;
62+
script.type = 'module';
6263
script.onload = callback;
6364
document.head.appendChild(script);
6465
}

Emscripten/src/CsoundProcessor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@
2121
02110-1301 USA
2222
*/
2323

24-
'use strict';
24+
import libcsound from './libcsound-worklet.js'
2525

26-
const WAM = AudioWorkletGlobalScope.WAM;
26+
const WAM = {};
2727

2828
WAM["ENVIRONMENT"] = "WEB";
2929
WAM["print"] = (t) => console.log(t);
3030
WAM["printErr"] = (t) => console.log(t);
3131

3232

3333
// INITIALIAZE WASM
34-
AudioWorkletGlobalScope.libcsound(WAM);
34+
libcsound(WAM);
3535

3636
// SETUP FS
3737

Emscripten/src/CsoundScriptProcessorNode.js

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
02110-1301 USA
2222
*/
2323

24+
import libcsound from './libcsound.js'
25+
2426
// Setup a single global AudioContext object
25-
var CSOUND_AUDIO_CONTEXT = CSOUND_AUDIO_CONTEXT ||
27+
window.CSOUND_AUDIO_CONTEXT = window.CSOUND_AUDIO_CONTEXT ||
2628
(function() {
2729
try {
2830
var AudioContext = window.AudioContext || window.webkitAudioContext;
@@ -35,9 +37,10 @@ var CSOUND_AUDIO_CONTEXT = CSOUND_AUDIO_CONTEXT ||
3537
return null;
3638
}());
3739

40+
let WAM = window.WAM = {}
41+
3842

3943
// Global singleton variables
40-
var AudioWorkletGlobalScope = AudioWorkletGlobalScope || {};
4144
var CSOUND;
4245

4346
/** This E6 class is used to setup scripts and
@@ -62,51 +65,46 @@ class CsoundScriptProcessorNodeFactory {
6265
*/
6366
static importScripts(script_base='./') {
6467
return new Promise((resolve) => {
65-
CsoundScriptProcessorNodeFactory.loadScript(script_base + 'libcsound.js', () => {
66-
AudioWorkletGlobalScope.WAM = {}
67-
let WAM = AudioWorkletGlobalScope.WAM;
68-
69-
WAM["ENVIRONMENT"] = "WEB";
70-
WAM["print"] = (t) => console.log(t);
71-
WAM["printErr"] = (t) => console.log(t);
72-
WAM["locateFile"] = (f) => script_base + f;
73-
74-
AudioWorkletGlobalScope.libcsound(WAM).then(() => {
75-
76-
// Cache cwrap functions into CSOUND global object
77-
CSOUND = {
78-
new: WAM.cwrap('CsoundObj_new', ['number'], null),
79-
compileCSD: WAM.cwrap('CsoundObj_compileCSD', ['number'], ['number', 'string']),
80-
evaluateCode: WAM.cwrap('CsoundObj_evaluateCode', ['number'], ['number', 'string']),
81-
readScore: WAM.cwrap('CsoundObj_readScore', ['number'], ['number', 'string']),
82-
reset: WAM.cwrap('CsoundObj_reset', null, ['number']),
83-
getOutputBuffer: WAM.cwrap('CsoundObj_getOutputBuffer', ['number'], ['number']),
84-
getInputBuffer: WAM.cwrap('CsoundObj_getInputBuffer', ['number'], ['number']),
85-
getControlChannel: WAM.cwrap('CsoundObj_getControlChannel', ['number'], ['number', 'string']),
86-
setControlChannel: WAM.cwrap('CsoundObj_setControlChannel', null, ['number', 'string', 'number']),
87-
setStringChannel: WAM.cwrap('CsoundObj_setStringChannel', null, ['number', 'string', 'string']),
88-
getKsmps: WAM.cwrap('CsoundObj_getKsmps', ['number'], ['number']),
89-
performKsmps: WAM.cwrap('CsoundObj_performKsmps', ['number'], ['number']),
90-
render: WAM.cwrap('CsoundObj_render', null, ['number']),
91-
getInputChannelCount: WAM.cwrap('CsoundObj_getInputChannelCount', ['number'], ['number']),
92-
getOutputChannelCount: WAM.cwrap('CsoundObj_getOutputChannelCount', ['number'], ['number']),
93-
getTableLength: WAM.cwrap('CsoundObj_getTableLength', ['number'], ['number', 'number']),
94-
getTable: WAM.cwrap('CsoundObj_getTable', ['number'], ['number', 'number']),
95-
getZerodBFS: WAM.cwrap('CsoundObj_getZerodBFS', ['number'], ['number']),
96-
setMidiCallbacks: WAM.cwrap('CsoundObj_setMidiCallbacks', null, ['number']),
97-
pushMidiMessage: WAM.cwrap('CsoundObj_pushMidiMessage', null, ['number', 'number', 'number', 'number']),
98-
setOutputChannelCallback: WAM.cwrap('CsoundObj_setOutputChannelCallback', null, ['number', 'number']),
99-
compileOrc: WAM.cwrap('CsoundObj_compileOrc', 'number', ['number', 'string']),
100-
setOption: WAM.cwrap('CsoundObj_setOption', null, ['number', 'string']),
101-
prepareRT: WAM.cwrap('CsoundObj_prepareRT', null, ['number']),
102-
getScoreTime: WAM.cwrap('CsoundObj_getScoreTime', null, ['number']),
103-
setTable: WAM.cwrap('CsoundObj_setTable', null, ['number', 'number', 'number', 'number']),
104-
destroy: WAM.cwrap('CsoundObj_destroy', null, ['number'])
105-
}
106-
107-
resolve();
108-
})
109-
})
68+
WAM["ENVIRONMENT"] = "WEB";
69+
WAM["print"] = (t) => console.log(t);
70+
WAM["printErr"] = (t) => console.log(t);
71+
WAM["locateFile"] = (f) => script_base + f;
72+
73+
libcsound(WAM).then(() => {
74+
75+
// Cache cwrap functions into CSOUND global object
76+
CSOUND = {
77+
new: WAM.cwrap('CsoundObj_new', ['number'], null),
78+
compileCSD: WAM.cwrap('CsoundObj_compileCSD', ['number'], ['number', 'string']),
79+
evaluateCode: WAM.cwrap('CsoundObj_evaluateCode', ['number'], ['number', 'string']),
80+
readScore: WAM.cwrap('CsoundObj_readScore', ['number'], ['number', 'string']),
81+
reset: WAM.cwrap('CsoundObj_reset', null, ['number']),
82+
getOutputBuffer: WAM.cwrap('CsoundObj_getOutputBuffer', ['number'], ['number']),
83+
getInputBuffer: WAM.cwrap('CsoundObj_getInputBuffer', ['number'], ['number']),
84+
getControlChannel: WAM.cwrap('CsoundObj_getControlChannel', ['number'], ['number', 'string']),
85+
setControlChannel: WAM.cwrap('CsoundObj_setControlChannel', null, ['number', 'string', 'number']),
86+
setStringChannel: WAM.cwrap('CsoundObj_setStringChannel', null, ['number', 'string', 'string']),
87+
getKsmps: WAM.cwrap('CsoundObj_getKsmps', ['number'], ['number']),
88+
performKsmps: WAM.cwrap('CsoundObj_performKsmps', ['number'], ['number']),
89+
render: WAM.cwrap('CsoundObj_render', null, ['number']),
90+
getInputChannelCount: WAM.cwrap('CsoundObj_getInputChannelCount', ['number'], ['number']),
91+
getOutputChannelCount: WAM.cwrap('CsoundObj_getOutputChannelCount', ['number'], ['number']),
92+
getTableLength: WAM.cwrap('CsoundObj_getTableLength', ['number'], ['number', 'number']),
93+
getTable: WAM.cwrap('CsoundObj_getTable', ['number'], ['number', 'number']),
94+
getZerodBFS: WAM.cwrap('CsoundObj_getZerodBFS', ['number'], ['number']),
95+
setMidiCallbacks: WAM.cwrap('CsoundObj_setMidiCallbacks', null, ['number']),
96+
pushMidiMessage: WAM.cwrap('CsoundObj_pushMidiMessage', null, ['number', 'number', 'number', 'number']),
97+
setOutputChannelCallback: WAM.cwrap('CsoundObj_setOutputChannelCallback', null, ['number', 'number']),
98+
compileOrc: WAM.cwrap('CsoundObj_compileOrc', 'number', ['number', 'string']),
99+
setOption: WAM.cwrap('CsoundObj_setOption', null, ['number', 'string']),
100+
prepareRT: WAM.cwrap('CsoundObj_prepareRT', null, ['number']),
101+
getScoreTime: WAM.cwrap('CsoundObj_getScoreTime', null, ['number']),
102+
setTable: WAM.cwrap('CsoundObj_setTable', null, ['number', 'number', 'number', 'number']),
103+
destroy: WAM.cwrap('CsoundObj_destroy', null, ['number'])
104+
}
105+
106+
resolve();
107+
})
110108
});
111109
}
112110

@@ -132,7 +130,7 @@ class CsoundScriptProcessorNodeFactory {
132130
* numberOfOutputs
133131
* @returns {object} A new CsoundScriptProcessorNode
134132
*/
135-
CsoundScriptProcessorNode = function(context, options) {
133+
let CsoundScriptProcessorNode = function(context, options) {
136134
var spn = context.createScriptProcessor(0, options.numberOfInputs, options.numberOfOutputs);
137135
spn.inputCount = options.numberOfInputs;
138136
spn.outputCount = options.numberOfOutputs;
@@ -481,10 +479,13 @@ CsoundScriptProcessorNode = function(context, options) {
481479
}
482480
}
483481

484-
let WAM = AudioWorkletGlobalScope.WAM;
485482
WAM["print"] = (t) => spn.msgCallback(t);
486483
WAM["printErr"] = (t) => spn.msgCallback(t);
487484

488485
return Object.assign(spn,CsoundMixin);
489486
}
490487

488+
window.CsoundScriptProcessorNodeFactory = CsoundScriptProcessorNodeFactory;
489+
window.CsoundScriptProcessorNode = CsoundScriptProcessorNode;
490+
491+
export default {CsoundScriptProcessorNode, CsoundScriptProcessorNodeFactory};

Emscripten/src/post.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default Module;

0 commit comments

Comments
 (0)