samples: support speed, begin and end

This commit is contained in:
Felix Roos 2022-06-18 13:56:41 +02:00
parent d532f8007e
commit 71b6b83ab3

View File

@ -61,6 +61,9 @@ Pattern.prototype.out = function () {
decay = 0, decay = 0,
sustain = 1, sustain = 1,
release = 0.001, release = 0.001,
speed = 1, // sample playback speed
begin = 0,
end = 1,
} = hap.value; } = hap.value;
// the chain will hold all audio nodes that connect to each other // the chain will hold all audio nodes that connect to each other
const chain = []; const chain = [];
@ -91,17 +94,30 @@ Pattern.prototype.out = function () {
console.warn('sample not found:', s, 'try one of ' + Object.keys(samples)); console.warn('sample not found:', s, 'try one of ' + Object.keys(samples));
return; return;
} else { } else {
if (speed === 0) {
// no playback
return;
}
if (!s) {
console.warn('no sample specified');
return;
}
const bank = samples[s]; const bank = samples[s];
const sampleUrl = bank[n % bank.length]; const sampleUrl = bank[n % bank.length];
let buffer = await loadBuffer(sampleUrl, ac); let buffer = await loadBuffer(sampleUrl, ac);
if (ac.currentTime > t) { if (ac.currentTime > t) {
console.warn('sample still loading:', s); console.warn('sample still loading:', s, n);
return; return;
} }
const src = ac.createBufferSource(); const src = ac.createBufferSource();
src.buffer = buffer; src.buffer = buffer;
src.start(t); let duration = src.buffer.duration;
src.stop(t + hap.duration + release); const offset = begin * duration;
const sus = ((end - begin) * duration) / Math.abs(speed);
src.playbackRate.value = Math.abs(speed);
// TODO: nudge, unit, cut, loop
src.start(t, offset, sus);
src.stop(t + duration);
chain.push(src); chain.push(src);
} }
} }