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'];
export function registerSynthSounds() {
[...waveforms].forEach((s, i) => {
registerSound(
s,
'supersaw',
(begin, value, onended) => {
const ac = getAudioContext();
let { note, freq, duration } = value;
@ -54,7 +53,7 @@ export function registerSynthSounds() {
let node = getWorklet(
ac,
'better-oscillator',
'supersaw-oscillator',
{
frequency: freq,
begin,
@ -62,7 +61,6 @@ export function registerSynthSounds() {
},
{
outputChannelCount: [2],
// numberOfOutputs: 1,
},
);
@ -76,14 +74,10 @@ export function registerSynthSounds() {
stop: (time) => {
// o.stop(time);
},
triggerRelease: (time) => {
// envGain?.stop(time);
},
};
},
// { prebake: true },
{ prebake: true, type: 'synth' },
);
});
[...noises].forEach((s) => {
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)
t += 0.5;
if (t >= 1) t -= 1;
const naive_saw = 2 * t - 1;
return naive_saw - polyBlep(t, dt);
// return naive_saw;
const osc = 2 * t - 1;
return osc - polyBlep(t, dt);
};
class BetterOscillatorProcessor extends AudioWorkletProcessor {
class SuperSawOscillatorProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.phase = [];
this.sync_phase = 0;
this.prev_sync_phase = 0;
this.freq = [];
this.balance = [];
}
static get parameterDescriptors() {
return [
@ -194,22 +188,16 @@ class BetterOscillatorProcessor extends AudioWorkletProcessor {
if (n > 0) {
adj = isOdd ? n * detune : -((n - 1) * detune);
}
this.freq[n] = frequency + adj * 0.01 * frequency;
this.balance[n] = isOdd ? 1 - spread : spread;
// for (let i = 0; i < output[0].length; i++) {
// }
}
const freq = frequency + adj * 0.01 * frequency;
const balance = isOdd ? 1 - spread : spread;
const dt = freq / sampleRate;
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];
const osc = saw(this.phase[n], dt);
output[0][i] = output[0][i] + osc * (1 - balance);
output[1][i] = output[1][i] + osc * balance;
this.phase[n] = this.phase[n] ?? Math.random();
this.phase[n] += dt;
@ -217,12 +205,9 @@ class BetterOscillatorProcessor extends AudioWorkletProcessor {
this.phase[n] = this.phase[n] - 1;
}
}
output[0][i] = outL;
output[1][i] = outR;
}
return true;
}
}
registerProcessor('better-oscillator', BetterOscillatorProcessor);
registerProcessor('supersaw-oscillator', SuperSawOscillatorProcessor);