refactor event.duration references

This commit is contained in:
Felix Roos 2022-04-23 23:36:26 +02:00
parent 9e2e5ce581
commit 766cccf335
7 changed files with 13 additions and 13 deletions

View file

@ -27,7 +27,7 @@
const events = pattern.firstCycle(); // query first cycle const events = pattern.firstCycle(); // query first cycle
events.forEach((event) => { events.forEach((event) => {
ctx.fillStyle = event.value; ctx.fillStyle = event.value;
ctx.fillRect(event.whole.begin * canvas.width, 0, event.duration * canvas.width, canvas.height); ctx.fillRect(event.whole.begin * canvas.width, 0, event.duration.valueOf() * canvas.width, canvas.height);
}); });
} }
</script> </script>

View file

@ -69,7 +69,7 @@ Pattern.prototype.midi = function (output, channel = 1) {
// await enableWebMidi() // await enableWebMidi()
device.playNote(note, channel, { device.playNote(note, channel, {
time, time,
duration: event.duration * 1000 - 5, duration: event.duration.valueOf() * 1000 - 5,
velocity, velocity,
}); });
}; };

View file

@ -54,14 +54,14 @@ Pattern.prototype.tone = function (instrument) {
note = getPlayableNoteValue(event); note = getPlayableNoteValue(event);
instrument.triggerAttack(note, time); instrument.triggerAttack(note, time);
} else if (instrument instanceof NoiseSynth) { } else if (instrument instanceof NoiseSynth) {
instrument.triggerAttackRelease(event.duration, time); // noise has no value instrument.triggerAttackRelease(event.duration.valueOf(), time); // noise has no value
} else if (instrument instanceof Piano) { } else if (instrument instanceof Piano) {
note = getPlayableNoteValue(event); note = getPlayableNoteValue(event);
instrument.keyDown({ note, time, velocity }); instrument.keyDown({ note, time, velocity });
instrument.keyUp({ note, time: time + event.duration, velocity }); instrument.keyUp({ note, time: time + event.duration.valueOf(), velocity });
} else if (instrument instanceof Sampler) { } else if (instrument instanceof Sampler) {
note = getPlayableNoteValue(event); note = getPlayableNoteValue(event);
instrument.triggerAttackRelease(note, event.duration, time, velocity); instrument.triggerAttackRelease(note, event.duration.valueOf(), time, velocity);
} else if (instrument instanceof Players) { } else if (instrument instanceof Players) {
if (!instrument.has(event.value)) { if (!instrument.has(event.value)) {
throw new Error(`name "${event.value}" not defined for players`); throw new Error(`name "${event.value}" not defined for players`);
@ -69,10 +69,10 @@ Pattern.prototype.tone = function (instrument) {
const player = instrument.player(event.value); const player = instrument.player(event.value);
// velocity ? // velocity ?
player.start(time); player.start(time);
player.stop(time + event.duration); player.stop(time + event.duration.valueOf());
} else { } else {
note = getPlayableNoteValue(event); note = getPlayableNoteValue(event);
instrument.triggerAttackRelease(note, event.duration, time, velocity); instrument.triggerAttackRelease(note, event.duration.valueOf(), time, velocity);
} }
}; };
return event.setContext({ ...event.context, instrument, onTrigger }); return event.setContext({ ...event.context, instrument, onTrigger });

View file

@ -39,7 +39,7 @@ Pattern.prototype._wave = function (type) {
const f = getFrequency(e); const f = getFrequency(e);
osc.frequency.value = f; // expects frequency.. osc.frequency.value = f; // expects frequency..
const begin = t ?? e.whole.begin.valueOf() + lookahead; const begin = t ?? e.whole.begin.valueOf() + lookahead;
const end = begin + e.duration; const end = begin + e.valueOf();
osc.start(begin); osc.start(begin);
osc.stop(end); // release? osc.stop(end); // release?
return osc; return osc;
@ -49,7 +49,7 @@ Pattern.prototype.adsr = function (a = 0.01, d = 0.05, s = 1, r = 0.01) {
return this.withAudioNode((t, e, node) => { return this.withAudioNode((t, e, node) => {
const velocity = e.context?.velocity || 1; const velocity = e.context?.velocity || 1;
const begin = t ?? e.whole.begin.valueOf() + lookahead; const begin = t ?? e.whole.begin.valueOf() + lookahead;
const end = begin + e.duration + lookahead; const end = begin + e.duration.valueOf() + lookahead;
const envelope = adsr(a, d, s, r, velocity, begin, end); const envelope = adsr(a, d, s, r, velocity, begin, end);
node?.connect(envelope); node?.connect(envelope);
return envelope; return envelope;

View file

@ -44,8 +44,8 @@ export const markEvent = (editor) => (time, event) => {
//Tone.Transport.schedule(() => { // problem: this can be cleared by scheduler... //Tone.Transport.schedule(() => { // problem: this can be cleared by scheduler...
setTimeout(() => { setTimeout(() => {
marks.forEach((mark) => mark.clear()); marks.forEach((mark) => mark.clear());
// }, '+' + event.duration * 0.5); // }, '+' + event.duration.valueOf() * 0.5);
}, event.duration /* * 0.9 */ * 1000); }, event.duration.valueOf() /* * 0.9 */ * 1000);
}; };
let parenMark; let parenMark;

View file

@ -45,7 +45,7 @@ async function playStatic(code) {
if (!onTrigger) { if (!onTrigger) {
if (defaultSynth) { if (defaultSynth) {
const note = getPlayableNoteValue(event); const note = getPlayableNoteValue(event);
defaultSynth.triggerAttackRelease(note, event.duration, time, velocity); defaultSynth.triggerAttackRelease(note, event.duration.valueOf(), time, velocity);
} else { } else {
throw new Error('no defaultSynth passed to useRepl.'); throw new Error('no defaultSynth passed to useRepl.');
} }

View file

@ -44,7 +44,7 @@ function useRepl({ tune, defaultSynth, autolink = true, onEvent, onDraw }) {
if (!onTrigger) { if (!onTrigger) {
if (defaultSynth) { if (defaultSynth) {
const note = getPlayableNoteValue(event); const note = getPlayableNoteValue(event);
defaultSynth.triggerAttackRelease(note, event.duration, time, velocity); defaultSynth.triggerAttackRelease(note, event.duration.valueOf(), time, velocity);
} else { } else {
throw new Error('no defaultSynth passed to useRepl.'); throw new Error('no defaultSynth passed to useRepl.');
} }