This commit is contained in:
Jade (Rose) Rowland 2024-03-03 21:22:17 -05:00
parent ee01b4a3a0
commit 823c208c71
2 changed files with 58 additions and 79 deletions

View File

@ -25,65 +25,59 @@ const waveforms = ['triangle', 'square', 'sawtooth', 'sine'];
const noises = ['pink', 'white', 'brown', 'crackle']; const noises = ['pink', 'white', 'brown', 'crackle'];
export function registerSynthSounds() { export function registerSynthSounds() {
[...waveforms].forEach((s, i) => { registerSound(
registerSound( 'supersaw',
s, (begin, value, onended) => {
(begin, value, onended) => { const ac = getAudioContext();
const ac = getAudioContext(); let { note, freq, duration } = value;
let { note, freq, duration } = value; note = note || 36;
note = note || 36; if (typeof note === 'string') {
if (typeof note === 'string') { note = noteToMidi(note); // e.g. c3 => 48
note = noteToMidi(note); // e.g. c3 => 48 }
} // get frequency
// get frequency if (!freq && typeof note === 'number') {
if (!freq && typeof note === 'number') { freq = midiToFreq(note); // + 48);
freq = midiToFreq(note); // + 48); }
}
// set frequency // set frequency
freq = Number(freq); freq = Number(freq);
const [attack, decay, sustain, release] = getADSRValues( const [attack, decay, sustain, release] = getADSRValues(
[value.attack, value.decay, value.sustain, value.release], [value.attack, value.decay, value.sustain, value.release],
'linear', 'linear',
[0.001, 0.05, 0.6, 0.01], [0.001, 0.05, 0.6, 0.01],
); );
const holdend = begin + duration; const holdend = begin + duration;
const end = holdend + release + 0.01; const end = holdend + release + 0.01;
let node = getWorklet( let node = getWorklet(
ac, ac,
'better-oscillator', 'supersaw-oscillator',
{ {
frequency: freq, frequency: freq,
begin, begin,
end, end,
}, },
{ {
outputChannelCount: [2], outputChannelCount: [2],
// numberOfOutputs: 1, },
}, );
);
const envGain = gainNode(1); const envGain = gainNode(1);
node = node.connect(envGain); node = node.connect(envGain);
getParamADSR(node.gain, attack, decay, sustain, release, 0, 0.3, begin, holdend, 'linear'); getParamADSR(node.gain, attack, decay, sustain, release, 0, 0.3, begin, holdend, 'linear');
return { return {
node, node,
stop: (time) => { stop: (time) => {
// o.stop(time); // o.stop(time);
}, },
triggerRelease: (time) => { };
// envGain?.stop(time); },
}, { prebake: true, type: 'synth' },
}; );
},
// { prebake: true },
);
});
[...noises].forEach((s) => { [...noises].forEach((s) => {
registerSound( registerSound(

View File

@ -132,24 +132,18 @@ const polyBlep = (t, dt) => {
} }
}; };
const polySaw = (t, dt) => { const saw = (t, dt) => {
// Correct phase, so it would be in line with sin(2.*M_PI * t) // Correct phase, so it would be in line with sin(2.*M_PI * t)
t += 0.5; t += 0.5;
if (t >= 1) t -= 1; if (t >= 1) t -= 1;
const osc = 2 * t - 1;
const naive_saw = 2 * t - 1; return osc - polyBlep(t, dt);
return naive_saw - polyBlep(t, dt);
// return naive_saw;
}; };
class BetterOscillatorProcessor extends AudioWorkletProcessor { class SuperSawOscillatorProcessor extends AudioWorkletProcessor {
constructor() { constructor() {
super(); super();
this.phase = []; this.phase = [];
this.sync_phase = 0;
this.prev_sync_phase = 0;
this.freq = [];
this.balance = [];
} }
static get parameterDescriptors() { static get parameterDescriptors() {
return [ return [
@ -194,22 +188,16 @@ class BetterOscillatorProcessor extends AudioWorkletProcessor {
if (n > 0) { if (n > 0) {
adj = isOdd ? n * detune : -((n - 1) * detune); adj = isOdd ? n * detune : -((n - 1) * detune);
} }
this.freq[n] = frequency + adj * 0.01 * frequency; const freq = frequency + adj * 0.01 * frequency;
this.balance[n] = isOdd ? 1 - spread : spread; const balance = isOdd ? 1 - spread : spread;
const dt = freq / sampleRate;
// for (let i = 0; i < output[0].length; i++) { for (let i = 0; i < output[0].length; i++) {
const osc = saw(this.phase[n], dt);
// } output[0][i] = output[0][i] + osc * (1 - balance);
} output[1][i] = output[1][i] + osc * balance;
for (let i = 0; i < output[0].length; i++) {
let outL = 0;
let outR = 0;
for (let n = 0; n < numSaws; n++) {
const dt = this.freq[n] / sampleRate;
const osc = polySaw(this.phase[n], this.freq[n] / sampleRate);
outL = outL + osc * (1 - this.balance[n]);
outR = outR + osc * this.balance[n];
this.phase[n] = this.phase[n] ?? Math.random(); this.phase[n] = this.phase[n] ?? Math.random();
this.phase[n] += dt; this.phase[n] += dt;
@ -217,12 +205,9 @@ class BetterOscillatorProcessor extends AudioWorkletProcessor {
this.phase[n] = this.phase[n] - 1; this.phase[n] = this.phase[n] - 1;
} }
} }
output[0][i] = outL;
output[1][i] = outR;
} }
return true; return true;
} }
} }
registerProcessor('better-oscillator', BetterOscillatorProcessor); registerProcessor('supersaw-oscillator', SuperSawOscillatorProcessor);