Skip to content

Commit

Permalink
working on making WebAudio Csound work as ES6 Modules
Browse files Browse the repository at this point in the history
  • Loading branch information
kunstmusik committed Sep 23, 2018
1 parent f39a125 commit 8873116
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 68 deletions.
21 changes: 11 additions & 10 deletions Emscripten/build.sh
Expand Up @@ -25,31 +25,32 @@ emcc -s LINKABLE=1 -s ASSERTIONS=0 ../src/CsoundObj.c -I../../include -Iinclude
# 65536 * 1024 * 4 is 268435456

# Keep exports in alphabetical order please, to correlate with CsoundObj.js.
## First build for WASM/ScriptProcessorNode (async compilation = 1, assertions = 0)
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


## First build for WASM/ScriptProcessorNode (async compilation = 1, assertions = 1)
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

## Second build for WASM/AudioWorklet (async compilation = 0, assertions = 0)
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
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

node ../convert.js
# node ../convert.js

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

cd ..
rm -rf dist
mkdir dist
cp src/FileList.js dist/
## cp src/FileList.js dist/
cp src/CsoundProcessor.js dist/
cp src/CsoundNode.js dist/
cp src/CsoundScriptProcessorNode.js dist/
cp src/CsoundObj.js dist/
cp src/csound.js dist/
cp build/libcsound.js dist/
cp build/libcsound.wasm dist/
cp build/libcsound-worklet.js dist/
cp build/libcsound-worklet.wasm.js dist/
#cp build/libcsound.wasm dist/
#cp build/libcsound-worklet.js dist/
#cp build/libcsound-worklet.wasm.js dist/


11 changes: 5 additions & 6 deletions Emscripten/src/CsoundNode.js
Expand Up @@ -289,11 +289,9 @@ class CsoundNodeFactory {
static importScripts(script_base='./') {
let actx = CSOUND_AUDIO_CONTEXT;
return new Promise( (resolve) => {
actx.audioWorklet.addModule(script_base + 'libcsound-worklet.wasm.js').then(() => {
actx.audioWorklet.addModule(script_base + 'libcsound-worklet.js').then(() => {
actx.audioWorklet.addModule(script_base + 'CsoundProcessor.js').then(() => {
resolve();
}) }) })
actx.audioWorklet.addModule(script_base + 'CsoundProcessor.js').then(() => {
resolve();
})
})
}

Expand All @@ -313,5 +311,6 @@ class CsoundNodeFactory {
}


window.CsoundNodeFactory = CsoundNodeFactory;


export default {CsoundNode, CsoundNodeFactory};
1 change: 1 addition & 0 deletions Emscripten/src/CsoundObj.js
Expand Up @@ -59,6 +59,7 @@ if(typeof AudioWorkletNode !== 'undefined' &&
const csound_load_script = function(src, callback) {
var script = document.createElementNS("http://www.w3.org/1999/xhtml", "script");
script.src = src;
script.type = 'module';
script.onload = callback;
document.head.appendChild(script);
}
Expand Down
6 changes: 3 additions & 3 deletions Emscripten/src/CsoundProcessor.js
Expand Up @@ -21,17 +21,17 @@
02110-1301 USA
*/

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

const WAM = AudioWorkletGlobalScope.WAM;
const WAM = {};

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


// INITIALIAZE WASM
AudioWorkletGlobalScope.libcsound(WAM);
libcsound(WAM);

// SETUP FS

Expand Down
99 changes: 50 additions & 49 deletions Emscripten/src/CsoundScriptProcessorNode.js
Expand Up @@ -21,8 +21,10 @@
02110-1301 USA
*/

import libcsound from './libcsound.js'

// Setup a single global AudioContext object
var CSOUND_AUDIO_CONTEXT = CSOUND_AUDIO_CONTEXT ||
window.CSOUND_AUDIO_CONTEXT = window.CSOUND_AUDIO_CONTEXT ||
(function() {
try {
var AudioContext = window.AudioContext || window.webkitAudioContext;
Expand All @@ -35,9 +37,10 @@ var CSOUND_AUDIO_CONTEXT = CSOUND_AUDIO_CONTEXT ||
return null;
}());

let WAM = window.WAM = {}


// Global singleton variables
var AudioWorkletGlobalScope = AudioWorkletGlobalScope || {};
var CSOUND;

/** This E6 class is used to setup scripts and
Expand All @@ -62,51 +65,46 @@ class CsoundScriptProcessorNodeFactory {
*/
static importScripts(script_base='./') {
return new Promise((resolve) => {
CsoundScriptProcessorNodeFactory.loadScript(script_base + 'libcsound.js', () => {
AudioWorkletGlobalScope.WAM = {}
let WAM = AudioWorkletGlobalScope.WAM;

WAM["ENVIRONMENT"] = "WEB";
WAM["print"] = (t) => console.log(t);
WAM["printErr"] = (t) => console.log(t);
WAM["locateFile"] = (f) => script_base + f;

AudioWorkletGlobalScope.libcsound(WAM).then(() => {

// Cache cwrap functions into CSOUND global object
CSOUND = {
new: WAM.cwrap('CsoundObj_new', ['number'], null),
compileCSD: WAM.cwrap('CsoundObj_compileCSD', ['number'], ['number', 'string']),
evaluateCode: WAM.cwrap('CsoundObj_evaluateCode', ['number'], ['number', 'string']),
readScore: WAM.cwrap('CsoundObj_readScore', ['number'], ['number', 'string']),
reset: WAM.cwrap('CsoundObj_reset', null, ['number']),
getOutputBuffer: WAM.cwrap('CsoundObj_getOutputBuffer', ['number'], ['number']),
getInputBuffer: WAM.cwrap('CsoundObj_getInputBuffer', ['number'], ['number']),
getControlChannel: WAM.cwrap('CsoundObj_getControlChannel', ['number'], ['number', 'string']),
setControlChannel: WAM.cwrap('CsoundObj_setControlChannel', null, ['number', 'string', 'number']),
setStringChannel: WAM.cwrap('CsoundObj_setStringChannel', null, ['number', 'string', 'string']),
getKsmps: WAM.cwrap('CsoundObj_getKsmps', ['number'], ['number']),
performKsmps: WAM.cwrap('CsoundObj_performKsmps', ['number'], ['number']),
render: WAM.cwrap('CsoundObj_render', null, ['number']),
getInputChannelCount: WAM.cwrap('CsoundObj_getInputChannelCount', ['number'], ['number']),
getOutputChannelCount: WAM.cwrap('CsoundObj_getOutputChannelCount', ['number'], ['number']),
getTableLength: WAM.cwrap('CsoundObj_getTableLength', ['number'], ['number', 'number']),
getTable: WAM.cwrap('CsoundObj_getTable', ['number'], ['number', 'number']),
getZerodBFS: WAM.cwrap('CsoundObj_getZerodBFS', ['number'], ['number']),
setMidiCallbacks: WAM.cwrap('CsoundObj_setMidiCallbacks', null, ['number']),
pushMidiMessage: WAM.cwrap('CsoundObj_pushMidiMessage', null, ['number', 'number', 'number', 'number']),
setOutputChannelCallback: WAM.cwrap('CsoundObj_setOutputChannelCallback', null, ['number', 'number']),
compileOrc: WAM.cwrap('CsoundObj_compileOrc', 'number', ['number', 'string']),
setOption: WAM.cwrap('CsoundObj_setOption', null, ['number', 'string']),
prepareRT: WAM.cwrap('CsoundObj_prepareRT', null, ['number']),
getScoreTime: WAM.cwrap('CsoundObj_getScoreTime', null, ['number']),
setTable: WAM.cwrap('CsoundObj_setTable', null, ['number', 'number', 'number', 'number']),
destroy: WAM.cwrap('CsoundObj_destroy', null, ['number'])
}

resolve();
})
})
WAM["ENVIRONMENT"] = "WEB";
WAM["print"] = (t) => console.log(t);
WAM["printErr"] = (t) => console.log(t);
WAM["locateFile"] = (f) => script_base + f;

libcsound(WAM).then(() => {

// Cache cwrap functions into CSOUND global object
CSOUND = {
new: WAM.cwrap('CsoundObj_new', ['number'], null),
compileCSD: WAM.cwrap('CsoundObj_compileCSD', ['number'], ['number', 'string']),
evaluateCode: WAM.cwrap('CsoundObj_evaluateCode', ['number'], ['number', 'string']),
readScore: WAM.cwrap('CsoundObj_readScore', ['number'], ['number', 'string']),
reset: WAM.cwrap('CsoundObj_reset', null, ['number']),
getOutputBuffer: WAM.cwrap('CsoundObj_getOutputBuffer', ['number'], ['number']),
getInputBuffer: WAM.cwrap('CsoundObj_getInputBuffer', ['number'], ['number']),
getControlChannel: WAM.cwrap('CsoundObj_getControlChannel', ['number'], ['number', 'string']),
setControlChannel: WAM.cwrap('CsoundObj_setControlChannel', null, ['number', 'string', 'number']),
setStringChannel: WAM.cwrap('CsoundObj_setStringChannel', null, ['number', 'string', 'string']),
getKsmps: WAM.cwrap('CsoundObj_getKsmps', ['number'], ['number']),
performKsmps: WAM.cwrap('CsoundObj_performKsmps', ['number'], ['number']),
render: WAM.cwrap('CsoundObj_render', null, ['number']),
getInputChannelCount: WAM.cwrap('CsoundObj_getInputChannelCount', ['number'], ['number']),
getOutputChannelCount: WAM.cwrap('CsoundObj_getOutputChannelCount', ['number'], ['number']),
getTableLength: WAM.cwrap('CsoundObj_getTableLength', ['number'], ['number', 'number']),
getTable: WAM.cwrap('CsoundObj_getTable', ['number'], ['number', 'number']),
getZerodBFS: WAM.cwrap('CsoundObj_getZerodBFS', ['number'], ['number']),
setMidiCallbacks: WAM.cwrap('CsoundObj_setMidiCallbacks', null, ['number']),
pushMidiMessage: WAM.cwrap('CsoundObj_pushMidiMessage', null, ['number', 'number', 'number', 'number']),
setOutputChannelCallback: WAM.cwrap('CsoundObj_setOutputChannelCallback', null, ['number', 'number']),
compileOrc: WAM.cwrap('CsoundObj_compileOrc', 'number', ['number', 'string']),
setOption: WAM.cwrap('CsoundObj_setOption', null, ['number', 'string']),
prepareRT: WAM.cwrap('CsoundObj_prepareRT', null, ['number']),
getScoreTime: WAM.cwrap('CsoundObj_getScoreTime', null, ['number']),
setTable: WAM.cwrap('CsoundObj_setTable', null, ['number', 'number', 'number', 'number']),
destroy: WAM.cwrap('CsoundObj_destroy', null, ['number'])
}

resolve();
})
});
}

Expand All @@ -132,7 +130,7 @@ class CsoundScriptProcessorNodeFactory {
* numberOfOutputs
* @returns {object} A new CsoundScriptProcessorNode
*/
CsoundScriptProcessorNode = function(context, options) {
let CsoundScriptProcessorNode = function(context, options) {
var spn = context.createScriptProcessor(0, options.numberOfInputs, options.numberOfOutputs);
spn.inputCount = options.numberOfInputs;
spn.outputCount = options.numberOfOutputs;
Expand Down Expand Up @@ -481,10 +479,13 @@ CsoundScriptProcessorNode = function(context, options) {
}
}

let WAM = AudioWorkletGlobalScope.WAM;
WAM["print"] = (t) => spn.msgCallback(t);
WAM["printErr"] = (t) => spn.msgCallback(t);

return Object.assign(spn,CsoundMixin);
}

window.CsoundScriptProcessorNodeFactory = CsoundScriptProcessorNodeFactory;
window.CsoundScriptProcessorNode = CsoundScriptProcessorNode;

export default {CsoundScriptProcessorNode, CsoundScriptProcessorNodeFactory};
1 change: 1 addition & 0 deletions Emscripten/src/post.js
@@ -0,0 +1 @@
export default Module;

0 comments on commit 8873116

Please sign in to comment.