Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Sep 21, 2022
1 parent 0707c73 commit f366326
Showing 2 changed files with 88 additions and 50 deletions.
69 changes: 44 additions & 25 deletions dist/1.x/ByteBeat.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ByteBeat@1.0.5, license MIT */
/* ByteBeat@1.0.6, license MIT */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
@@ -280,10 +280,32 @@
};
}

static addGlobals(object, name, filter = () => true) {
return `
var console = {
log() {},
info() {},
error() {},
warn() {},
};
var ${Object.getOwnPropertyNames(object).filter(filter).map(key => {
const value = object[key];
return (typeof value === 'function')
? `${key} = ${name}.${key}`
: `${key} = ${name}.${key}`;
}).join(',\n')};
`;
}

static s_fnHeader = (function() {
const keys = {};
const filter = () => true;
const windowKeep = new Set([
'parseInt',
'parseFloat',
'Array',
'isNaN',
]);
const filter = n => !windowKeep.has(n);
//const filter = n => n === 'scroll' || n === 'sin';
Object.getOwnPropertyNames(globalThis).filter(filter).forEach((key) => {
keys[key] = true;
@@ -292,28 +314,25 @@
delete keys['window'];
return `
var ${Object.keys(keys).sort().join(',\n')};
var ${Object.getOwnPropertyNames(Math).map(key => {
const value = Math[key];
return (typeof value === 'function')
? `${key} = Math.${key}.bind(Math)`
: `${key} = Math.${key}`;
}).join(',\n')};
${ByteBeatCompiler.addGlobals(Math, 'Math')}
`;
}());

// ${ByteBeatCompiler.addGlobals(globalThis, 'globalThis', n => n === 'parseInt' || n === 'parseFloat')}


static expressionStringToFn(evalExp, extra, test) {
// eslint-disable-next-line no-new-func
const fp = new Function('stack', 'window', 'extra', evalExp);
let f = fp(undefined, undefined, undefined);
const ctx = ByteBeatCompiler.makeContext();

const stack = new WrappingStack();
const tempExtra = Object.assign({}, extra);
// check function
let v = f(0, 0, stack, ctx, tempExtra);
let v = f.call(ctx, 0, 0, stack, ctx, tempExtra);
if (typeof v === 'function') {
f = f();
v = f(0, 0, stack, ctx, tempExtra);
v = f.call(ctx, 0, 0, stack, ctx, tempExtra);
}
const array = ByteBeatCompiler.is2NumberArray(v);

@@ -405,7 +424,7 @@
// case 0: // bytebeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
const s = fn0((time) / divisor, undefined, stack0, ctx0, extra);
const s = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
buffer0[time % buffer0.length] = (s[0] & 255) / 127 - 1;
buffer1[time % buffer1.length] = (s[1] & 255) / 127 - 1;
++time;
@@ -414,7 +433,7 @@
// case 1: // floatbeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
const s = fn0((time / divisor), undefined, stack0, ctx0, extra);
const s = fn0.call(ctx0, (time / divisor), undefined, stack0, ctx0, extra);
buffer0[time % buffer0.length] = s[0];
buffer1[time % buffer1.length] = s[1];
++time;
@@ -423,7 +442,7 @@
// case 2: // signed bytebeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
const s = fn0((time) / divisor, undefined, stack0, ctx0, extra);
const s = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
int8[0] = s[0];
buffer0[time % buffer0.length] = int8[0] / 128;
int8[0] = s[1];
@@ -436,24 +455,24 @@
// case 0: // bytebeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
buffer0[time % buffer0.length] = (fn0((time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
buffer1[time % buffer1.length] = (fn1((time) / divisor, undefined, stack1, ctx1, extra) & 255) / 127 - 1;
buffer0[time % buffer0.length] = (fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
buffer1[time % buffer1.length] = (fn1.call(ctx1, (time) / divisor, undefined, stack1, ctx1, extra) & 255) / 127 - 1;
++time;
}
},
// case 1: // floatbeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
buffer0[time % buffer0.length] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
buffer1[time % buffer1.length] = fn1((time) / divisor, undefined, stack1, ctx1, extra);
buffer0[time % buffer0.length] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
buffer1[time % buffer1.length] = fn1.call(ctx1, (time) / divisor, undefined, stack1, ctx1, extra);
}
},
// case 2: // signed bytebeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
int8[0] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
int8[0] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
buffer0[time % buffer0.length] = int8[0] / 128;
int8[0] = fn1((time) / divisor, undefined, stack1, ctx1, extra);
int8[0] = fn1.call(ctx1, (time) / divisor, undefined, stack1, ctx1, extra);
buffer1[time % buffer1.length] = int8[0] / 128;
++time;
}
@@ -463,21 +482,21 @@
// case 0: // bytebeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
buffer0[time % buffer0.length] = (fn0((time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
buffer0[time % buffer0.length] = (fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
++time;
}
},
// case 1: // floatbeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
buffer0[time % buffer0.length] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
buffer0[time % buffer0.length] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
++time;
}
},
// case 2: // signed bytebeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
int8[0] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
int8[0] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
buffer0[time % buffer0.length] = int8[0] / 128;
++time;
}
@@ -739,6 +758,7 @@ class BeatWorkletProcessor extends AudioWorkletProcessor {
}
setExpressionsAndResetToZero(data) {
this.byteBeat.reset();
this.byteBeat.setExpressions(data);
this.byteBeat.reset();
}
@@ -873,7 +893,6 @@ registerProcessor('bytebeat-processor', BeatWorkletProcessor);
reset() {
this.#callFunc('reset');
this.byteBeat.reset();
this.time = 0;
this.startTime = performance.now();
this.pauseTime = this.startTime;
}
@@ -955,7 +974,7 @@ registerProcessor('bytebeat-processor', BeatWorkletProcessor);
});
this.byteBeat.setExpressions(exp);
if (resetToZero) {
this.byteBeat.reset();
this.reset();
}
if (this.onCompileCallback) {
this.onCompileCallback(null);
69 changes: 44 additions & 25 deletions dist/1.x/ByteBeat.module.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ByteBeat@1.0.5, license MIT */
/* ByteBeat@1.0.6, license MIT */
class WrappingStack {
constructor(stackSize = 256) {
let sp = 0;
@@ -274,10 +274,32 @@ class ByteBeatCompiler {
};
}

static addGlobals(object, name, filter = () => true) {
return `
var console = {
log() {},
info() {},
error() {},
warn() {},
};
var ${Object.getOwnPropertyNames(object).filter(filter).map(key => {
const value = object[key];
return (typeof value === 'function')
? `${key} = ${name}.${key}`
: `${key} = ${name}.${key}`;
}).join(',\n')};
`;
}

static s_fnHeader = (function() {
const keys = {};
const filter = () => true;
const windowKeep = new Set([
'parseInt',
'parseFloat',
'Array',
'isNaN',
]);
const filter = n => !windowKeep.has(n);
//const filter = n => n === 'scroll' || n === 'sin';
Object.getOwnPropertyNames(globalThis).filter(filter).forEach((key) => {
keys[key] = true;
@@ -286,28 +308,25 @@ class ByteBeatCompiler {
delete keys['window'];
return `
var ${Object.keys(keys).sort().join(',\n')};
var ${Object.getOwnPropertyNames(Math).map(key => {
const value = Math[key];
return (typeof value === 'function')
? `${key} = Math.${key}.bind(Math)`
: `${key} = Math.${key}`;
}).join(',\n')};
${ByteBeatCompiler.addGlobals(Math, 'Math')}
`;
}());

// ${ByteBeatCompiler.addGlobals(globalThis, 'globalThis', n => n === 'parseInt' || n === 'parseFloat')}


static expressionStringToFn(evalExp, extra, test) {
// eslint-disable-next-line no-new-func
const fp = new Function('stack', 'window', 'extra', evalExp);
let f = fp(undefined, undefined, undefined);
const ctx = ByteBeatCompiler.makeContext();

const stack = new WrappingStack();
const tempExtra = Object.assign({}, extra);
// check function
let v = f(0, 0, stack, ctx, tempExtra);
let v = f.call(ctx, 0, 0, stack, ctx, tempExtra);
if (typeof v === 'function') {
f = f();
v = f(0, 0, stack, ctx, tempExtra);
v = f.call(ctx, 0, 0, stack, ctx, tempExtra);
}
const array = ByteBeatCompiler.is2NumberArray(v);

@@ -399,7 +418,7 @@ class ByteBeatProcessor {
// case 0: // bytebeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
const s = fn0((time) / divisor, undefined, stack0, ctx0, extra);
const s = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
buffer0[time % buffer0.length] = (s[0] & 255) / 127 - 1;
buffer1[time % buffer1.length] = (s[1] & 255) / 127 - 1;
++time;
@@ -408,7 +427,7 @@ class ByteBeatProcessor {
// case 1: // floatbeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
const s = fn0((time / divisor), undefined, stack0, ctx0, extra);
const s = fn0.call(ctx0, (time / divisor), undefined, stack0, ctx0, extra);
buffer0[time % buffer0.length] = s[0];
buffer1[time % buffer1.length] = s[1];
++time;
@@ -417,7 +436,7 @@ class ByteBeatProcessor {
// case 2: // signed bytebeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
const s = fn0((time) / divisor, undefined, stack0, ctx0, extra);
const s = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
int8[0] = s[0];
buffer0[time % buffer0.length] = int8[0] / 128;
int8[0] = s[1];
@@ -430,24 +449,24 @@ class ByteBeatProcessor {
// case 0: // bytebeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
buffer0[time % buffer0.length] = (fn0((time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
buffer1[time % buffer1.length] = (fn1((time) / divisor, undefined, stack1, ctx1, extra) & 255) / 127 - 1;
buffer0[time % buffer0.length] = (fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
buffer1[time % buffer1.length] = (fn1.call(ctx1, (time) / divisor, undefined, stack1, ctx1, extra) & 255) / 127 - 1;
++time;
}
},
// case 1: // floatbeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
buffer0[time % buffer0.length] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
buffer1[time % buffer1.length] = fn1((time) / divisor, undefined, stack1, ctx1, extra);
buffer0[time % buffer0.length] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
buffer1[time % buffer1.length] = fn1.call(ctx1, (time) / divisor, undefined, stack1, ctx1, extra);
}
},
// case 2: // signed bytebeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
int8[0] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
int8[0] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
buffer0[time % buffer0.length] = int8[0] / 128;
int8[0] = fn1((time) / divisor, undefined, stack1, ctx1, extra);
int8[0] = fn1.call(ctx1, (time) / divisor, undefined, stack1, ctx1, extra);
buffer1[time % buffer1.length] = int8[0] / 128;
++time;
}
@@ -457,21 +476,21 @@ class ByteBeatProcessor {
// case 0: // bytebeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
buffer0[time % buffer0.length] = (fn0((time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
buffer0[time % buffer0.length] = (fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
++time;
}
},
// case 1: // floatbeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
buffer0[time % buffer0.length] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
buffer0[time % buffer0.length] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
++time;
}
},
// case 2: // signed bytebeat
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
for (let i = 0; i < lastSample; ++i) {
int8[0] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
int8[0] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
buffer0[time % buffer0.length] = int8[0] / 128;
++time;
}
@@ -733,6 +752,7 @@ class BeatWorkletProcessor extends AudioWorkletProcessor {
}
setExpressionsAndResetToZero(data) {
this.byteBeat.reset();
this.byteBeat.setExpressions(data);
this.byteBeat.reset();
}
@@ -867,7 +887,6 @@ class ByteBeatNode extends AudioWorkletNode {
reset() {
this.#callFunc('reset');
this.byteBeat.reset();
this.time = 0;
this.startTime = performance.now();
this.pauseTime = this.startTime;
}
@@ -949,7 +968,7 @@ class ByteBeatNode extends AudioWorkletNode {
});
this.byteBeat.setExpressions(exp);
if (resetToZero) {
this.byteBeat.reset();
this.reset();
}
if (this.onCompileCallback) {
this.onCompileCallback(null);

0 comments on commit f366326

Please sign in to comment.