encapsulate out

+ encapsulate destination
+ error handling
to simplify merge scheduler branch
This commit is contained in:
Felix Roos 2022-09-22 19:50:07 +02:00
parent 82cb6c93cf
commit 6dbb2cb98a

View File

@ -22,6 +22,21 @@ export const getAudioContext = () => {
return audioContext;
};
let destination;
export const getDestination = () => {
const ctx = getAudioContext();
if (!destination) {
destination = ctx.createGain();
destination.connect(ctx.destination);
}
return destination;
};
export const panic = () => {
getDestination().gain.linearRampToValueAtTime(0, getAudioContext().currentTime + 0.01);
destination = null;
};
const getFilter = (type, frequency, Q) => {
const filter = getAudioContext().createBiquadFilter();
filter.type = type;
@ -87,7 +102,7 @@ const getSampleBufferSource = async (s, n, note) => {
}
const bank = samples?.[s];
if (!bank) {
throw new Error('sample not found:', s, 'try one of ' + Object.keys(samples));
throw new Error(`sample not found: "${s}", try one of ${Object.keys(samples).join(', ')}`);
}
if (typeof bank !== 'object') {
throw new Error('wrong format for sample bank:', s);
@ -155,13 +170,17 @@ try {
const cutGroups = [];
Pattern.prototype.out = function () {
return this.onTrigger(async (t, hap, ct, cps) => {
const hapDuration = hap.duration / cps;
// export const webaudioOutput = async (t, hap, ct, cps) => {
export const webaudioOutput = async (hap, deadline, hapDuration) => {
try {
const ac = getAudioContext();
if (typeof hap.value !== 'object') {
throw new Error(
`hap.value ${hap.value} is not supported by webaudio output. Hint: append .note() or .s() to the end`,
);
}
// calculate correct time (tone.js workaround)
t = ac.currentTime + t - ct;
let t = ac.currentTime + deadline;
// destructure value
let {
freq,
@ -308,17 +327,17 @@ Pattern.prototype.out = function () {
panner.pan.value = 2 * pan - 1;
chain.push(panner);
}
// master out
/* const master = ac.createGain();
master.gain.value = 0.8 * gain;
chain.push(master); */
chain.push(ac.destination);
// connect chain elements together
chain.slice(1).reduce((last, current) => last.connect(current), chain[0]);
chain[chain.length - 1].connect(getDestination());
// disconnect all nodes when source node has ended:
chain[0].onended = () => chain.forEach((n) => n.disconnect());
} catch (e) {
console.warn('.out error:', e);
}
});
};
Pattern.prototype.out = function () {
// TODO: refactor (t, hap, ct, cps) to (hap, deadline, duration) ?
return this.onTrigger((t, hap, ct, cps) => webaudioOutput(hap, t - ct, hap.duration / cps));
};