From 6283273d811cbc3aa944eb17fd9d6c03b6560656 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Fri, 24 Feb 2023 10:15:06 +0100 Subject: [PATCH] add helper hap.ensureObjectValue --- packages/core/hap.mjs | 13 +++++++++++++ packages/csound/index.mjs | 4 +--- packages/osc/osc.mjs | 1 + packages/webaudio/webaudio.mjs | 18 +++--------------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/core/hap.mjs b/packages/core/hap.mjs index 30251959..c9c91c3b 100644 --- a/packages/core/hap.mjs +++ b/packages/core/hap.mjs @@ -126,6 +126,19 @@ export class Hap { setContext(context) { return new Hap(this.whole, this.part, this.value, context); } + + ensureObjectValue() { + /* if (isNote(hap.value)) { + // supports primitive hap values that look like notes + hap.value = { note: hap.value }; + } */ + if (typeof this.value !== 'object') { + throw new Error( + `expected hap.value to be an object, but got "${this.value}". Hint: append .note() or .s() to the end`, + 'error', + ); + } + } } export default Hap; diff --git a/packages/csound/index.mjs b/packages/csound/index.mjs index 3aabc22b..31ffa83a 100644 --- a/packages/csound/index.mjs +++ b/packages/csound/index.mjs @@ -28,9 +28,7 @@ export const csound = register('csound', (instrument, pat) => { logger('[csound] not loaded yet', 'warning'); return; } - if (typeof hap.value !== 'object') { - throw new Error('csound only support objects as hap values'); - } + hap.ensureObjectValue(); let { gain = 0.8 } = hap.value; gain *= 0.2; diff --git a/packages/osc/osc.mjs b/packages/osc/osc.mjs index 2176f61f..83e03dad 100644 --- a/packages/osc/osc.mjs +++ b/packages/osc/osc.mjs @@ -47,6 +47,7 @@ let startedAt = -1; */ Pattern.prototype.osc = function () { return this.onTrigger(async (time, hap, currentTime, cps = 1) => { + hap.ensureObjectValue(); const osc = await connect(); const cycle = hap.wholeOrPart().begin.valueOf(); const delta = hap.duration.valueOf(); diff --git a/packages/webaudio/webaudio.mjs b/packages/webaudio/webaudio.mjs index 968cb996..526c4912 100644 --- a/packages/webaudio/webaudio.mjs +++ b/packages/webaudio/webaudio.mjs @@ -193,21 +193,9 @@ function effectSend(input, effect, wet) { // export const webaudioOutput = async (t, hap, ct, cps) => { export const webaudioOutput = async (hap, deadline, hapDuration) => { const ac = getAudioContext(); - /* if (isNote(hap.value)) { - // supports primitive hap values that look like notes - hap.value = { note: hap.value }; - } */ - if (typeof hap.value !== 'object') { - logger( - `hap.value "${hap.value}" is not supported by webaudio output. Hint: append .note() or .s() to the end`, - 'error', - ); - /* throw new Error( - `hap.value "${hap.value}"" is not supported by webaudio output. Hint: append .note() or .s() to the end`, - ); */ - return; - } - // calculate correct time (tone.js workaround) + hap.ensureObjectValue(); + + // calculate absolute time let t = ac.currentTime + deadline; // destructure value let {