refactor scheduler constructor + add onEvent

This commit is contained in:
Felix Roos 2022-04-14 00:32:41 +02:00
parent 5629c0ee72
commit 325eec2c6f
2 changed files with 11 additions and 8 deletions

View File

@ -18,10 +18,13 @@ Loading...</textarea
const { cat, State, TimeSpan } = strudel; const { cat, State, TimeSpan } = strudel;
Object.assign(window, strudel); // add strudel to eval scope Object.assign(window, strudel); // add strudel to eval scope
const audioContext = getAudioContext(); const scheduler = new Scheduler({
const interval = 0.1; audioContext: getAudioContext(),
const lookahead = 0.5; interval: 0.1,
const scheduler = new Scheduler(audioContext, interval, lookahead); onEvent: (e) => {
e.context?.createAudioNode?.(e);
},
});
let initialCode = `sequence(1,2).mul(55/2) // frequencies let initialCode = `sequence(1,2).mul(55/2) // frequencies
.mul(slowcat(1,2)) .mul(slowcat(1,2))

View File

@ -3,7 +3,7 @@ import ClockWorker from './clockworker.mjs';
class Scheduler { class Scheduler {
worker; worker;
pattern; pattern;
constructor(audioContext, interval = 0.2) { constructor({ audioContext, interval = 0.2, onEvent }) {
this.worker = new ClockWorker( this.worker = new ClockWorker(
audioContext, audioContext,
(begin, end) => { (begin, end) => {
@ -11,10 +11,10 @@ class Scheduler {
if (!e.part.begin.equals(e.whole.begin)) { if (!e.part.begin.equals(e.whole.begin)) {
return; return;
} }
if (e.context.createAudioNode) { if (onEvent) {
e.context.createAudioNode(e); onEvent?.(e);
} else { } else {
console.warn('unplayable event: no audio node'); console.warn('unplayable event: no audio node nor onEvent callback', e);
} }
}); });
}, },