Skip to content

Commit f366326

Browse files
committed
build
1 parent 0707c73 commit f366326

File tree

2 files changed

+88
-50
lines changed

2 files changed

+88
-50
lines changed

dist/1.x/ByteBeat.js

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* ByteBeat@1.0.5, license MIT */
1+
/* ByteBeat@1.0.6, license MIT */
22
(function (global, factory) {
33
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
44
typeof define === 'function' && define.amd ? define(factory) :
@@ -280,10 +280,32 @@
280280
};
281281
}
282282

283+
static addGlobals(object, name, filter = () => true) {
284+
return `
285+
var console = {
286+
log() {},
287+
info() {},
288+
error() {},
289+
warn() {},
290+
};
291+
var ${Object.getOwnPropertyNames(object).filter(filter).map(key => {
292+
const value = object[key];
293+
return (typeof value === 'function')
294+
? `${key} = ${name}.${key}`
295+
: `${key} = ${name}.${key}`;
296+
}).join(',\n')};
297+
`;
298+
}
283299

284300
static s_fnHeader = (function() {
285301
const keys = {};
286-
const filter = () => true;
302+
const windowKeep = new Set([
303+
'parseInt',
304+
'parseFloat',
305+
'Array',
306+
'isNaN',
307+
]);
308+
const filter = n => !windowKeep.has(n);
287309
//const filter = n => n === 'scroll' || n === 'sin';
288310
Object.getOwnPropertyNames(globalThis).filter(filter).forEach((key) => {
289311
keys[key] = true;
@@ -292,28 +314,25 @@
292314
delete keys['window'];
293315
return `
294316
var ${Object.keys(keys).sort().join(',\n')};
295-
var ${Object.getOwnPropertyNames(Math).map(key => {
296-
const value = Math[key];
297-
return (typeof value === 'function')
298-
? `${key} = Math.${key}.bind(Math)`
299-
: `${key} = Math.${key}`;
300-
}).join(',\n')};
317+
${ByteBeatCompiler.addGlobals(Math, 'Math')}
301318
`;
302319
}());
303320

321+
// ${ByteBeatCompiler.addGlobals(globalThis, 'globalThis', n => n === 'parseInt' || n === 'parseFloat')}
322+
323+
304324
static expressionStringToFn(evalExp, extra, test) {
305325
// eslint-disable-next-line no-new-func
306326
const fp = new Function('stack', 'window', 'extra', evalExp);
307327
let f = fp(undefined, undefined, undefined);
308328
const ctx = ByteBeatCompiler.makeContext();
309-
310329
const stack = new WrappingStack();
311330
const tempExtra = Object.assign({}, extra);
312331
// check function
313-
let v = f(0, 0, stack, ctx, tempExtra);
332+
let v = f.call(ctx, 0, 0, stack, ctx, tempExtra);
314333
if (typeof v === 'function') {
315334
f = f();
316-
v = f(0, 0, stack, ctx, tempExtra);
335+
v = f.call(ctx, 0, 0, stack, ctx, tempExtra);
317336
}
318337
const array = ByteBeatCompiler.is2NumberArray(v);
319338

@@ -405,7 +424,7 @@
405424
// case 0: // bytebeat
406425
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
407426
for (let i = 0; i < lastSample; ++i) {
408-
const s = fn0((time) / divisor, undefined, stack0, ctx0, extra);
427+
const s = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
409428
buffer0[time % buffer0.length] = (s[0] & 255) / 127 - 1;
410429
buffer1[time % buffer1.length] = (s[1] & 255) / 127 - 1;
411430
++time;
@@ -414,7 +433,7 @@
414433
// case 1: // floatbeat
415434
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
416435
for (let i = 0; i < lastSample; ++i) {
417-
const s = fn0((time / divisor), undefined, stack0, ctx0, extra);
436+
const s = fn0.call(ctx0, (time / divisor), undefined, stack0, ctx0, extra);
418437
buffer0[time % buffer0.length] = s[0];
419438
buffer1[time % buffer1.length] = s[1];
420439
++time;
@@ -423,7 +442,7 @@
423442
// case 2: // signed bytebeat
424443
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
425444
for (let i = 0; i < lastSample; ++i) {
426-
const s = fn0((time) / divisor, undefined, stack0, ctx0, extra);
445+
const s = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
427446
int8[0] = s[0];
428447
buffer0[time % buffer0.length] = int8[0] / 128;
429448
int8[0] = s[1];
@@ -436,24 +455,24 @@
436455
// case 0: // bytebeat
437456
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
438457
for (let i = 0; i < lastSample; ++i) {
439-
buffer0[time % buffer0.length] = (fn0((time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
440-
buffer1[time % buffer1.length] = (fn1((time) / divisor, undefined, stack1, ctx1, extra) & 255) / 127 - 1;
458+
buffer0[time % buffer0.length] = (fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
459+
buffer1[time % buffer1.length] = (fn1.call(ctx1, (time) / divisor, undefined, stack1, ctx1, extra) & 255) / 127 - 1;
441460
++time;
442461
}
443462
},
444463
// case 1: // floatbeat
445464
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
446465
for (let i = 0; i < lastSample; ++i) {
447-
buffer0[time % buffer0.length] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
448-
buffer1[time % buffer1.length] = fn1((time) / divisor, undefined, stack1, ctx1, extra);
466+
buffer0[time % buffer0.length] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
467+
buffer1[time % buffer1.length] = fn1.call(ctx1, (time) / divisor, undefined, stack1, ctx1, extra);
449468
}
450469
},
451470
// case 2: // signed bytebeat
452471
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
453472
for (let i = 0; i < lastSample; ++i) {
454-
int8[0] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
473+
int8[0] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
455474
buffer0[time % buffer0.length] = int8[0] / 128;
456-
int8[0] = fn1((time) / divisor, undefined, stack1, ctx1, extra);
475+
int8[0] = fn1.call(ctx1, (time) / divisor, undefined, stack1, ctx1, extra);
457476
buffer1[time % buffer1.length] = int8[0] / 128;
458477
++time;
459478
}
@@ -463,21 +482,21 @@
463482
// case 0: // bytebeat
464483
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
465484
for (let i = 0; i < lastSample; ++i) {
466-
buffer0[time % buffer0.length] = (fn0((time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
485+
buffer0[time % buffer0.length] = (fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
467486
++time;
468487
}
469488
},
470489
// case 1: // floatbeat
471490
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
472491
for (let i = 0; i < lastSample; ++i) {
473-
buffer0[time % buffer0.length] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
492+
buffer0[time % buffer0.length] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
474493
++time;
475494
}
476495
},
477496
// case 2: // signed bytebeat
478497
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
479498
for (let i = 0; i < lastSample; ++i) {
480-
int8[0] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
499+
int8[0] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
481500
buffer0[time % buffer0.length] = int8[0] / 128;
482501
++time;
483502
}
@@ -739,6 +758,7 @@ class BeatWorkletProcessor extends AudioWorkletProcessor {
739758
}
740759
741760
setExpressionsAndResetToZero(data) {
761+
this.byteBeat.reset();
742762
this.byteBeat.setExpressions(data);
743763
this.byteBeat.reset();
744764
}
@@ -873,7 +893,6 @@ registerProcessor('bytebeat-processor', BeatWorkletProcessor);
873893
reset() {
874894
this.#callFunc('reset');
875895
this.byteBeat.reset();
876-
this.time = 0;
877896
this.startTime = performance.now();
878897
this.pauseTime = this.startTime;
879898
}
@@ -955,7 +974,7 @@ registerProcessor('bytebeat-processor', BeatWorkletProcessor);
955974
});
956975
this.byteBeat.setExpressions(exp);
957976
if (resetToZero) {
958-
this.byteBeat.reset();
977+
this.reset();
959978
}
960979
if (this.onCompileCallback) {
961980
this.onCompileCallback(null);

dist/1.x/ByteBeat.module.js

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* ByteBeat@1.0.5, license MIT */
1+
/* ByteBeat@1.0.6, license MIT */
22
class WrappingStack {
33
constructor(stackSize = 256) {
44
let sp = 0;
@@ -274,10 +274,32 @@ class ByteBeatCompiler {
274274
};
275275
}
276276

277+
static addGlobals(object, name, filter = () => true) {
278+
return `
279+
var console = {
280+
log() {},
281+
info() {},
282+
error() {},
283+
warn() {},
284+
};
285+
var ${Object.getOwnPropertyNames(object).filter(filter).map(key => {
286+
const value = object[key];
287+
return (typeof value === 'function')
288+
? `${key} = ${name}.${key}`
289+
: `${key} = ${name}.${key}`;
290+
}).join(',\n')};
291+
`;
292+
}
277293

278294
static s_fnHeader = (function() {
279295
const keys = {};
280-
const filter = () => true;
296+
const windowKeep = new Set([
297+
'parseInt',
298+
'parseFloat',
299+
'Array',
300+
'isNaN',
301+
]);
302+
const filter = n => !windowKeep.has(n);
281303
//const filter = n => n === 'scroll' || n === 'sin';
282304
Object.getOwnPropertyNames(globalThis).filter(filter).forEach((key) => {
283305
keys[key] = true;
@@ -286,28 +308,25 @@ class ByteBeatCompiler {
286308
delete keys['window'];
287309
return `
288310
var ${Object.keys(keys).sort().join(',\n')};
289-
var ${Object.getOwnPropertyNames(Math).map(key => {
290-
const value = Math[key];
291-
return (typeof value === 'function')
292-
? `${key} = Math.${key}.bind(Math)`
293-
: `${key} = Math.${key}`;
294-
}).join(',\n')};
311+
${ByteBeatCompiler.addGlobals(Math, 'Math')}
295312
`;
296313
}());
297314

315+
// ${ByteBeatCompiler.addGlobals(globalThis, 'globalThis', n => n === 'parseInt' || n === 'parseFloat')}
316+
317+
298318
static expressionStringToFn(evalExp, extra, test) {
299319
// eslint-disable-next-line no-new-func
300320
const fp = new Function('stack', 'window', 'extra', evalExp);
301321
let f = fp(undefined, undefined, undefined);
302322
const ctx = ByteBeatCompiler.makeContext();
303-
304323
const stack = new WrappingStack();
305324
const tempExtra = Object.assign({}, extra);
306325
// check function
307-
let v = f(0, 0, stack, ctx, tempExtra);
326+
let v = f.call(ctx, 0, 0, stack, ctx, tempExtra);
308327
if (typeof v === 'function') {
309328
f = f();
310-
v = f(0, 0, stack, ctx, tempExtra);
329+
v = f.call(ctx, 0, 0, stack, ctx, tempExtra);
311330
}
312331
const array = ByteBeatCompiler.is2NumberArray(v);
313332

@@ -399,7 +418,7 @@ class ByteBeatProcessor {
399418
// case 0: // bytebeat
400419
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
401420
for (let i = 0; i < lastSample; ++i) {
402-
const s = fn0((time) / divisor, undefined, stack0, ctx0, extra);
421+
const s = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
403422
buffer0[time % buffer0.length] = (s[0] & 255) / 127 - 1;
404423
buffer1[time % buffer1.length] = (s[1] & 255) / 127 - 1;
405424
++time;
@@ -408,7 +427,7 @@ class ByteBeatProcessor {
408427
// case 1: // floatbeat
409428
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
410429
for (let i = 0; i < lastSample; ++i) {
411-
const s = fn0((time / divisor), undefined, stack0, ctx0, extra);
430+
const s = fn0.call(ctx0, (time / divisor), undefined, stack0, ctx0, extra);
412431
buffer0[time % buffer0.length] = s[0];
413432
buffer1[time % buffer1.length] = s[1];
414433
++time;
@@ -417,7 +436,7 @@ class ByteBeatProcessor {
417436
// case 2: // signed bytebeat
418437
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
419438
for (let i = 0; i < lastSample; ++i) {
420-
const s = fn0((time) / divisor, undefined, stack0, ctx0, extra);
439+
const s = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
421440
int8[0] = s[0];
422441
buffer0[time % buffer0.length] = int8[0] / 128;
423442
int8[0] = s[1];
@@ -430,24 +449,24 @@ class ByteBeatProcessor {
430449
// case 0: // bytebeat
431450
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
432451
for (let i = 0; i < lastSample; ++i) {
433-
buffer0[time % buffer0.length] = (fn0((time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
434-
buffer1[time % buffer1.length] = (fn1((time) / divisor, undefined, stack1, ctx1, extra) & 255) / 127 - 1;
452+
buffer0[time % buffer0.length] = (fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
453+
buffer1[time % buffer1.length] = (fn1.call(ctx1, (time) / divisor, undefined, stack1, ctx1, extra) & 255) / 127 - 1;
435454
++time;
436455
}
437456
},
438457
// case 1: // floatbeat
439458
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
440459
for (let i = 0; i < lastSample; ++i) {
441-
buffer0[time % buffer0.length] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
442-
buffer1[time % buffer1.length] = fn1((time) / divisor, undefined, stack1, ctx1, extra);
460+
buffer0[time % buffer0.length] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
461+
buffer1[time % buffer1.length] = fn1.call(ctx1, (time) / divisor, undefined, stack1, ctx1, extra);
443462
}
444463
},
445464
// case 2: // signed bytebeat
446465
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
447466
for (let i = 0; i < lastSample; ++i) {
448-
int8[0] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
467+
int8[0] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
449468
buffer0[time % buffer0.length] = int8[0] / 128;
450-
int8[0] = fn1((time) / divisor, undefined, stack1, ctx1, extra);
469+
int8[0] = fn1.call(ctx1, (time) / divisor, undefined, stack1, ctx1, extra);
451470
buffer1[time % buffer1.length] = int8[0] / 128;
452471
++time;
453472
}
@@ -457,21 +476,21 @@ class ByteBeatProcessor {
457476
// case 0: // bytebeat
458477
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
459478
for (let i = 0; i < lastSample; ++i) {
460-
buffer0[time % buffer0.length] = (fn0((time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
479+
buffer0[time % buffer0.length] = (fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra) & 255) / 127 - 1;
461480
++time;
462481
}
463482
},
464483
// case 1: // floatbeat
465484
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
466485
for (let i = 0; i < lastSample; ++i) {
467-
buffer0[time % buffer0.length] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
486+
buffer0[time % buffer0.length] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
468487
++time;
469488
}
470489
},
471490
// case 2: // signed bytebeat
472491
function(buffer0, buffer1, fn0, fn1, time, divisor, stack0, stack1, ctx0, ctx1, extra, lastSample) {
473492
for (let i = 0; i < lastSample; ++i) {
474-
int8[0] = fn0((time) / divisor, undefined, stack0, ctx0, extra);
493+
int8[0] = fn0.call(ctx0, (time) / divisor, undefined, stack0, ctx0, extra);
475494
buffer0[time % buffer0.length] = int8[0] / 128;
476495
++time;
477496
}
@@ -733,6 +752,7 @@ class BeatWorkletProcessor extends AudioWorkletProcessor {
733752
}
734753
735754
setExpressionsAndResetToZero(data) {
755+
this.byteBeat.reset();
736756
this.byteBeat.setExpressions(data);
737757
this.byteBeat.reset();
738758
}
@@ -867,7 +887,6 @@ class ByteBeatNode extends AudioWorkletNode {
867887
reset() {
868888
this.#callFunc('reset');
869889
this.byteBeat.reset();
870-
this.time = 0;
871890
this.startTime = performance.now();
872891
this.pauseTime = this.startTime;
873892
}
@@ -949,7 +968,7 @@ class ByteBeatNode extends AudioWorkletNode {
949968
});
950969
this.byteBeat.setExpressions(exp);
951970
if (resetToZero) {
952-
this.byteBeat.reset();
971+
this.reset();
953972
}
954973
if (this.onCompileCallback) {
955974
this.onCompileCallback(null);

0 commit comments

Comments
 (0)