Working version for compressor, filter, supersaw, wavetable

This commit is contained in:
Aria 2025-12-04 13:04:33 -06:00
parent 6610713018
commit 4321814d36
4 changed files with 23 additions and 13 deletions

View file

@ -1,7 +1,8 @@
import { getAudioContext } from './audioContext.mjs'; import { getAudioContext } from './audioContext.mjs';
import { clamp, nanFallback, midiToFreq, noteToMidi } from './util.mjs';
import { getNoiseBuffer } from './noise.mjs';
import { logger } from './logger.mjs'; import { logger } from './logger.mjs';
import { getNoiseBuffer } from './noise.mjs';
import { getNodeFromPool } from './nodePools.mjs';
import { clamp, nanFallback, midiToFreq, noteToMidi } from './util.mjs';
export const noises = ['pink', 'white', 'brown', 'crackle']; export const noises = ['pink', 'white', 'brown', 'crackle'];
@ -128,6 +129,7 @@ export function getLfo(audioContext, begin, end, properties = {}) {
} }
export function getCompressor(ac, threshold, ratio, knee, attack, release) { export function getCompressor(ac, threshold, ratio, knee, attack, release) {
const node = getNodeFromPool('compressor', () => new DynamicsCompressorNode(ac, {}));
const options = { const options = {
threshold: threshold ?? -3, threshold: threshold ?? -3,
ratio: ratio ?? 10, ratio: ratio ?? 10,
@ -135,7 +137,12 @@ export function getCompressor(ac, threshold, ratio, knee, attack, release) {
attack: attack ?? 0.005, attack: attack ?? 0.005,
release: release ?? 0.05, release: release ?? 0.05,
}; };
return new DynamicsCompressorNode(ac, options); node.threshold.value = options.threshold;
node.ratio.value = options.ratio;
node.knee.value = options.knee;
node.attack.value = options.attack;
node.release.value = options.release;
return node;
} }
// changes the default values of the envelope based on what parameters the user has defined // changes the default values of the envelope based on what parameters the user has defined
@ -214,7 +221,8 @@ export function createFilter(context, start, end, params, cps, cycle) {
filter = getWorklet(context, 'ladder-processor', { frequency, q, drive }); filter = getWorklet(context, 'ladder-processor', { frequency, q, drive });
frequencyParam = filter.parameters.get('frequency'); frequencyParam = filter.parameters.get('frequency');
} else { } else {
filter = context.createBiquadFilter(); const factory = () => context.createBiquadFilter();
filter = getNodeFromPool('filter', factory);
filter.type = type; filter.type = type;
filter.Q.value = q; filter.Q.value = q;
filter.frequency.value = frequency; filter.frequency.value = frequency;

View file

@ -168,7 +168,7 @@ export function registerSynthSounds() {
const params = { const params = {
frequency, frequency,
begin, begin,
end: end + 0.5, // add a grace period for pooling end,
freqspread: detune, freqspread: detune,
voices, voices,
panspread, panspread,

View file

@ -226,7 +226,7 @@ export async function onTriggerSynth(t, value, onended, tables, cps, frameLen) {
const envEnd = endWithRelease + 0.01; const envEnd = endWithRelease + 0.01;
const params = { const params = {
begin: t, begin: t,
end: envEnd + 0.5, // add a grace period for pooling end: envEnd,
frequency, frequency,
freqspread: value.detune, freqspread: value.detune,
position: value.wt, position: value.wt,

View file

@ -518,16 +518,16 @@ class SuperSawOscillatorProcessor extends AudioWorkletProcessor {
]; ];
} }
process(_input, outputs, params) { process(_input, outputs, params) {
if (currentTime >= params.end[0]) { if (currentTime >= params.end[0] + 0.5) {
// should terminate // Outside of grace period - should terminate
if (this.isAlive) { if (this.isAlive) {
this.port.postMessage({ type: 'died' }); this.port.postMessage({ type: 'died' });
this.isAlive = false; this.isAlive = false;
} }
return false; return false;
} }
if (currentTime <= params.begin[0]) { if (currentTime >= params.end[0] || currentTime <= params.begin[0]) {
// keep alive // Inside of grace period or not yet started
return true; return true;
} }
const output = outputs[0]; const output = outputs[0];
@ -1163,7 +1163,7 @@ class WavetableOscillatorProcessor extends AudioWorkletProcessor {
this.frameLen = null; this.frameLen = null;
this.numFrames = null; this.numFrames = null;
this.phase = []; this.phase = [];
if (options?.frames) { if (options?.key) {
const key = options.key; const key = options.key;
this.frameLen = options.frameLen; this.frameLen = options.frameLen;
if (!tablesCache[key]) { if (!tablesCache[key]) {
@ -1317,14 +1317,16 @@ class WavetableOscillatorProcessor extends AudioWorkletProcessor {
} }
process(_inputs, outputs, parameters) { process(_inputs, outputs, parameters) {
if (currentTime >= parameters.end[0]) { if (currentTime >= parameters.end[0] + 0.5) {
// Outside of grace period - should terminate
if (this.isAlive) { if (this.isAlive) {
this.port.postMessage({ type: 'died' }); this.port.postMessage({ type: 'died' });
this.isAlive = false; this.isAlive = false;
} }
return false; return false;
} }
if (currentTime <= parameters.begin[0]) { if (currentTime >= parameters.end[0] || currentTime <= parameters.begin[0]) {
// Inside of grace period or not yet started
return true; return true;
} }
const outL = outputs[0][0]; const outL = outputs[0][0];