From 819abbfa7a10abf43757365db7c2f52f0b58535f Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Wed, 21 Sep 2022 22:25:35 +0200 Subject: [PATCH 1/3] fix sample slicing + unit + nudge --- packages/webaudio/webaudio.mjs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/webaudio/webaudio.mjs b/packages/webaudio/webaudio.mjs index b1568ea7..4e597c2a 100644 --- a/packages/webaudio/webaudio.mjs +++ b/packages/webaudio/webaudio.mjs @@ -187,6 +187,8 @@ Pattern.prototype.out = function () { begin = 0, end = 1, vowel, + unit, + nudge = 0, // TODO: is this in seconds? } = hap.value; const { velocity = 1 } = hap.context; gain *= velocity; // legacy fix for velocity @@ -253,17 +255,22 @@ Pattern.prototype.out = function () { console.warn('no buffer source'); return; } + // TODO: cut, loop bufferSource.playbackRate.value = Math.abs(speed) * bufferSource.playbackRate.value; - // TODO: nudge, unit, cut, loop - let duration = soundfont || clip ? hapDuration : bufferSource.buffer.duration; - // let duration = bufferSource.buffer.duration; - const offset = begin * duration; - duration = ((end - begin) * duration) / Math.abs(speed); - if (soundfont || clip) { - bufferSource.start(t, offset); // duration does not work here for some reason - } else { - bufferSource.start(t, offset, duration); + if (unit === 'c') { + // are there other units? + bufferSource.playbackRate.value = bufferSource.playbackRate.value * bufferSource.buffer.duration; } + let duration = soundfont || clip ? hapDuration : bufferSource.buffer.duration / bufferSource.playbackRate.value; + // "The computation of the offset into the sound is performed using the sound buffer's natural sample rate, + // rather than the current playback rate, so even if the sound is playing at twice its normal speed, + // the midway point through a 10-second audio buffer is still 5." + const offset = begin * duration * bufferSource.playbackRate.value; + duration = (end - begin) * duration; + t += nudge; + + bufferSource.start(t, offset); + chain.push(bufferSource); if (soundfont || clip) { const env = ac.createGain(); From 8d5682deb0bb824b4943732e74537e42c4aef3a3 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Wed, 21 Sep 2022 22:44:56 +0200 Subject: [PATCH 2/3] cut + loop --- packages/webaudio/webaudio.mjs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/webaudio/webaudio.mjs b/packages/webaudio/webaudio.mjs index 4e597c2a..c316f3c1 100644 --- a/packages/webaudio/webaudio.mjs +++ b/packages/webaudio/webaudio.mjs @@ -153,6 +153,8 @@ try { console.warn('could not load AudioWorklet effects coarse, crush and shape', err); } +const cutGroups = []; + Pattern.prototype.out = function () { return this.onTrigger(async (t, hap, ct, cps) => { const hapDuration = hap.duration / cps; @@ -189,6 +191,8 @@ Pattern.prototype.out = function () { vowel, unit, nudge = 0, // TODO: is this in seconds? + cut, + loop, } = hap.value; const { velocity = 1 } = hap.context; gain *= velocity; // legacy fix for velocity @@ -255,7 +259,6 @@ Pattern.prototype.out = function () { console.warn('no buffer source'); return; } - // TODO: cut, loop bufferSource.playbackRate.value = Math.abs(speed) * bufferSource.playbackRate.value; if (unit === 'c') { // are there other units? @@ -267,10 +270,19 @@ Pattern.prototype.out = function () { // the midway point through a 10-second audio buffer is still 5." const offset = begin * duration * bufferSource.playbackRate.value; duration = (end - begin) * duration; + if (loop) { + bufferSource.loop = true; + bufferSource.loopStart = offset; + bufferSource.loopEnd = offset + duration; + duration = loop * duration; + } t += nudge; bufferSource.start(t, offset); - + if (cut !== undefined) { + cutGroups[cut]?.stop(); // fade out? + cutGroups[cut] = bufferSource; + } chain.push(bufferSource); if (soundfont || clip) { const env = ac.createGain(); From 7f6940f3a5d80539dfa92c6b12e939f228f666ab Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Wed, 21 Sep 2022 22:47:16 +0200 Subject: [PATCH 3/3] cut precision --- packages/webaudio/webaudio.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webaudio/webaudio.mjs b/packages/webaudio/webaudio.mjs index c316f3c1..17563bba 100644 --- a/packages/webaudio/webaudio.mjs +++ b/packages/webaudio/webaudio.mjs @@ -280,7 +280,7 @@ Pattern.prototype.out = function () { bufferSource.start(t, offset); if (cut !== undefined) { - cutGroups[cut]?.stop(); // fade out? + cutGroups[cut]?.stop(t); // fade out? cutGroups[cut] = bufferSource; } chain.push(bufferSource);