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

Commit

Permalink
AudioWorklet first impl
Browse files Browse the repository at this point in the history
  • Loading branch information
hlolli committed Aug 18, 2018
1 parent cab22eb commit 19063a4
Show file tree
Hide file tree
Showing 19 changed files with 5,480 additions and 2,072 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ release/browser/manifest.edn
yarn.lock
release/browser/main.js
release/browser/manifest.edn
release/browser/processor.js.map
libcsound/libcsound_browser.js
2 changes: 2 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Publishing to clojars:
- Remember to export libcsound resources which are otherwise handled by shadow-cljs hooks
1 change: 0 additions & 1 deletion examples/example1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const beeper = `
instr 1
asig = poscil:a(0.3, 440)
outc asig, asig
prints "HÆÆ"
endin`

const makeBeep = `i 1 0 1`
Expand Down
Binary file modified libcsound.wasm
Binary file not shown.
118 changes: 85 additions & 33 deletions libcsound/datauri.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,98 @@
const DataURI = require('datauri').promise;
const fs = require('fs');
const replace = require('replace-in-file');
// const stdin = process.argv[2];

// DataURI('libcsound/libcsound.wasm')
// .then(content =>
// {
// var replace_data = "'" + content + "';";
// // var append_data = "\nmodule.exports = Module;\n";

// const replace_options = {
// files: 'libcsound/libcsound.js',
// from: /'libcsound.wasm'/g,
// to: replace_data
// };

// try {
// replace.sync(replace_options);
// }
// catch (error) {
// console.error('Error occurred:', error);
// }

// // fs.appendFileSync('./libcsound.js', append_data)
// })
// .catch(err => { throw err; });

// DataURI('libcsound/libcsound.wasm')
// .then(content =>
// {
// console.log(content);
// })
// .catch(err => { throw err; });

DataURI('libcsound.wasm')
.then(content =>
{
// Old WebAudio
var file1 = fs.readFileSync('libcsound/libcsound.js').toString();
var replc = file1.replace('libcsound.wasm', content);
var replc1 = file1.replace('libcsound.wasm', content);

// AudioWorklet
var file2 = fs.readFileSync('libcsound/libcsound.js').toString();
var replc2 = file2.replace('libcsound.wasm', content);
replc2 = replc2.replace(`ENVIRONMENT_IS_WEB = typeof window === 'object';
ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
ENVIRONMENT_IS_NODE = typeof process === 'object' && typeof require === 'function' && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER;
ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;`,
`ENVIRONMENT_IS_WEB = true;\n`)

replc2 = replc2.replace(` if (document.currentScript) {
scriptDirectory = document.currentScript.src;
}`, '');
// replc2 = replc2.replace(`!Module['wasmBinary'] && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) && typeof fetch === 'function'`, 'false');
replc2 = replc2.replace(`typeof fetch === 'function'`, `false`);
replc2 = replc2.replace(`typeof fetch === 'function'`, `false`);
replc2 = replc2.replace(` function getBinary() {
try {
if (Module['wasmBinary']) {
return new Uint8Array(Module['wasmBinary']);
}
if (Module['readBinary']) {
return Module['readBinary'](wasmBinaryFile);
} else {
throw "both async and sync fetching of the wasm failed";
}
}
catch (err) {
abort(err);
}
}`, `
var base64_decode = function (base64) {
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var lookup = new Uint8Array(256);
for (var i = 0; i < chars.length; i++) {
lookup[chars.charCodeAt(i)] = i;
}
var bufferLength = base64.length * 0.75,
len = base64.length, i, p = 0,
encoded1, encoded2, encoded3, encoded4;
if (base64[base64.length - 1] === "=") {
bufferLength--;
if (base64[base64.length - 2] === "=") {
bufferLength--;
}
}
var arraybuffer = new ArrayBuffer(bufferLength),
bytes = new Uint8Array(arraybuffer);
for (i = 0; i < len; i += 4) {
encoded1 = lookup[base64.charCodeAt(i)];
encoded2 = lookup[base64.charCodeAt(i + 1)];
encoded3 = lookup[base64.charCodeAt(i + 2)];
encoded4 = lookup[base64.charCodeAt(i + 3)];
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
}
return arraybuffer;
}
var BASE64_MARKER = ';base64,';
function convertDataURIToBinary(dataURI) {
var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = dataURI.substring(base64Index);
var raw = base64_decode(base64);
return raw;
}
function getBinary() {
return convertDataURIToBinary(wasmBinaryFile);
}
`)

try {
fs.writeFileSync('libcsound/libcsound_browser.js', replc);
fs.writeFileSync('libcsound/libcsound_browser.js', replc1);
fs.writeFileSync('libcsound/libcsound_browser_worklet.js', replc2);
}
catch (error) {
console.error('Error occurred:', error);
Expand Down
Loading

0 comments on commit 19063a4

Please sign in to comment.