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,9 +25,8 @@ 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(
s, 'supersaw',
(begin, value, onended) => { (begin, value, onended) => {
const ac = getAudioContext(); const ac = getAudioContext();
let { note, freq, duration } = value; let { note, freq, duration } = value;
@ -54,7 +53,7 @@ export function registerSynthSounds() {
let node = getWorklet( let node = getWorklet(
ac, ac,
'better-oscillator', 'supersaw-oscillator',
{ {
frequency: freq, frequency: freq,
begin, begin,
@ -62,7 +61,6 @@ export function registerSynthSounds() {
}, },
{ {
outputChannelCount: [2], outputChannelCount: [2],
// numberOfOutputs: 1,
}, },
); );
@ -76,14 +74,10 @@ export function registerSynthSounds() {
stop: (time) => { stop: (time) => {
// o.stop(time); // o.stop(time);
}, },
triggerRelease: (time) => {
// envGain?.stop(time);
},
}; };
}, },
// { prebake: true }, { prebake: true, type: 'synth' },
); );
});
[...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++) { for (let i = 0; i < output[0].length; i++) {
let outL = 0; const osc = saw(this.phase[n], dt);
let outR = 0;
for (let n = 0; n < numSaws; n++) { output[0][i] = output[0][i] + osc * (1 - balance);
const dt = this.freq[n] / sampleRate; output[1][i] = output[1][i] + osc * balance;
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);