This commit is contained in:
Jade (Rose) Rowland 2024-03-03 13:16:32 -05:00
parent 7f60a9e52b
commit 78a6ec2f98
3 changed files with 45 additions and 14 deletions

View File

@ -50,8 +50,8 @@ function loadWorklets() {
return workletsLoading;
}
export function getWorklet(ac, processor, params) {
const node = new AudioWorkletNode(ac, processor);
export function getWorklet(ac, processor, params, config) {
const node = new AudioWorkletNode(ac, processor, config);
Object.entries(params).forEach(([key, value]) => {
node.parameters.get(key).value = value;
});

View File

@ -52,7 +52,20 @@ export function registerSynthSounds() {
const holdend = begin + duration;
const end = holdend + release + 0.01;
let node = getWorklet(ac, 'better-oscillator', { frequency: freq, wave: i, begin, end });
let node = getWorklet(
ac,
'better-oscillator',
{
frequency: freq,
wave: i,
begin,
end,
},
{
outputChannelCount: [2],
// numberOfOutputs: 1,
},
);
const envGain = gainNode(1);
node = node.connect(envGain);
@ -69,7 +82,7 @@ export function registerSynthSounds() {
},
};
},
{ type: 'synth', prebake: true },
// { prebake: true },
);
});

View File

@ -148,7 +148,8 @@ class BetterOscillatorProcessor extends AudioWorkletProcessor {
this.phase = [];
this.sync_phase = 0;
this.prev_sync_phase = 0;
this.adj = [];
this.freq = [];
this.balance = [];
}
static get parameterDescriptors() {
return [
@ -209,23 +210,31 @@ class BetterOscillatorProcessor extends AudioWorkletProcessor {
const output = outputs[0];
const numSaws = 7;
const detune = 0.5;
const spread = 0.5;
const spread = 0.6;
for (let n = 0; n < numSaws; n++) {
this.adj[n] = 0;
let adj = 0;
const isOdd = n % 2 === 1;
if (n > 0) {
this.adj[n] = n % 2 === 1 ? n * detune : -((n - 1) * detune);
adj = isOdd ? n * detune : -((n - 1) * detune);
}
this.freq[n] = frequency + adj * 0.01 * frequency;
// if (isOdd) {
// this.balance[n][0] =
// }
this.balance[n] = isOdd ? 1 - spread : spread;
}
for (let i = 0; i < output[0].length; i++) {
let out = 0;
let outL = 0;
let outR = 0;
for (let n = 0; n < numSaws; n++) {
const freq = frequency + this.adj[n];
const dt = freq / sampleRate;
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];
out = out + polySaw(this.phase[n], freq / sampleRate);
// out = out + polySaw(this.phase[n], this.freq[n] / sampleRate);
this.phase[n] = this.phase[n] ?? Math.random();
@ -235,7 +244,16 @@ class BetterOscillatorProcessor extends AudioWorkletProcessor {
this.phase[n] = this.phase[n] - 1;
}
}
output[0][i] = out;
output[0][i] = outL;
output[1][i] = outR;
// for (let c = 0; c < output.length; c++) {
// let modifier = c === 0 ? 1 - spread : spread;
// output[c][i] = out * modifier;
// }
// for (let c = 0; c < outputs.length; c++) {
// outputs[c][0][i] = out;
// }
}
return true;