mirror of
https://github.com/eliasstepanik/strudel.git
synced 2026-01-11 21:58:37 +00:00
working
This commit is contained in:
parent
8a7d72bf24
commit
7383dca9ee
@ -26,47 +26,51 @@ const noises = ['pink', 'white', 'brown', 'crackle'];
|
||||
|
||||
export function registerSynthSounds() {
|
||||
[...waveforms].forEach((s, i) => {
|
||||
registerSound(s, (begin, value, onended) => {
|
||||
const ac = getAudioContext();
|
||||
let { note, freq, duration } = value;
|
||||
note = note || 36;
|
||||
if (typeof note === 'string') {
|
||||
note = noteToMidi(note); // e.g. c3 => 48
|
||||
}
|
||||
// get frequency
|
||||
if (!freq && typeof note === 'number') {
|
||||
freq = midiToFreq(note); // + 48);
|
||||
}
|
||||
registerSound(
|
||||
s,
|
||||
(begin, value, onended) => {
|
||||
const ac = getAudioContext();
|
||||
let { note, freq, duration } = value;
|
||||
note = note || 36;
|
||||
if (typeof note === 'string') {
|
||||
note = noteToMidi(note); // e.g. c3 => 48
|
||||
}
|
||||
// get frequency
|
||||
if (!freq && typeof note === 'number') {
|
||||
freq = midiToFreq(note); // + 48);
|
||||
}
|
||||
|
||||
// set frequency
|
||||
freq = Number(freq);
|
||||
// set frequency
|
||||
freq = Number(freq);
|
||||
|
||||
const [attack, decay, sustain, release] = getADSRValues(
|
||||
[value.attack, value.decay, value.sustain, value.release],
|
||||
'linear',
|
||||
[0.001, 0.05, 0.6, 0.01],
|
||||
);
|
||||
const [attack, decay, sustain, release] = getADSRValues(
|
||||
[value.attack, value.decay, value.sustain, value.release],
|
||||
'linear',
|
||||
[0.001, 0.05, 0.6, 0.01],
|
||||
);
|
||||
|
||||
const holdend = begin + duration;
|
||||
const end = holdend + release + 0.01;
|
||||
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 });
|
||||
|
||||
const envGain = gainNode(1);
|
||||
node = node.connect(envGain);
|
||||
const envGain = gainNode(1);
|
||||
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 {
|
||||
node,
|
||||
stop: (time) => {
|
||||
// o.stop(time);
|
||||
},
|
||||
triggerRelease: (time) => {
|
||||
// envGain?.stop(time);
|
||||
},
|
||||
};
|
||||
});
|
||||
return {
|
||||
node,
|
||||
stop: (time) => {
|
||||
// o.stop(time);
|
||||
},
|
||||
triggerRelease: (time) => {
|
||||
// envGain?.stop(time);
|
||||
},
|
||||
};
|
||||
},
|
||||
{ type: 'synth', prebake: true },
|
||||
);
|
||||
});
|
||||
|
||||
[...noises].forEach((s) => {
|
||||
|
||||
@ -111,29 +111,6 @@ class DistortProcessor extends AudioWorkletProcessor {
|
||||
|
||||
registerProcessor('distort-processor', DistortProcessor);
|
||||
|
||||
// class SupersawProcessor extends AudioWorkletProcessor {
|
||||
// static get parameterDescriptors() {
|
||||
// return [
|
||||
// { name: 'distort', defaultValue: 0 },
|
||||
// { name: 'postgain', defaultValue: 1 },
|
||||
// ];
|
||||
// }
|
||||
|
||||
// constructor() {
|
||||
// super();
|
||||
// }
|
||||
|
||||
// process(inputs, outputs, parameters) {
|
||||
// const output = outputs[0];
|
||||
// let saw = (x, t) => ((x * t % 1) - 0.5) * 2
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
// registerProcessor('supersaw-processor', SupersawProcessor);
|
||||
|
||||
const saw = (v) => v - Math.floor(v);
|
||||
const twoPI = Math.PI * 2;
|
||||
const polyBlep = (t, dt) => {
|
||||
// 0 <= t < 1
|
||||
if (t < dt) {
|
||||
@ -155,25 +132,6 @@ const polyBlep = (t, dt) => {
|
||||
}
|
||||
};
|
||||
|
||||
const inc = () => {
|
||||
t += freqInSecondsPerSample;
|
||||
t -= bitwiseOrZero(t);
|
||||
};
|
||||
|
||||
const square_number = (x) => {
|
||||
return x * x;
|
||||
};
|
||||
|
||||
const blep = (t, dt) => {
|
||||
if (t < dt) {
|
||||
return -square_number(t / dt - 1);
|
||||
} else if (t > 1 - dt) {
|
||||
return square_number((t - 1) / dt + 1);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
const polySaw = (t, dt) => {
|
||||
// Correct phase, so it would be in line with sin(2.*M_PI * t)
|
||||
t += 0.5;
|
||||
@ -184,8 +142,6 @@ const polySaw = (t, dt) => {
|
||||
// return naive_saw;
|
||||
};
|
||||
|
||||
const saw2 = (x, t) => (((x * t) % 1) - 0.5) * 2;
|
||||
|
||||
class BetterOscillatorProcessor extends AudioWorkletProcessor {
|
||||
constructor() {
|
||||
super();
|
||||
@ -252,9 +208,6 @@ class BetterOscillatorProcessor extends AudioWorkletProcessor {
|
||||
const output = outputs[0];
|
||||
|
||||
for (let i = 0; i < output[0].length; i++) {
|
||||
// const blep = polyBlep(this.phase, frequency / sampleRate);
|
||||
// console.log(blep);
|
||||
// const out = saw2(frequency, this.phase / sampleRate);
|
||||
const out = polySaw(this.phase, frequency / sampleRate);
|
||||
output[0][i] = out;
|
||||
|
||||
@ -264,16 +217,6 @@ class BetterOscillatorProcessor extends AudioWorkletProcessor {
|
||||
this.phase = this.phase - 1;
|
||||
}
|
||||
}
|
||||
|
||||
// for (let i = 0; i < outlen; x++) {
|
||||
// const val = saw2(frequency, this.phase)
|
||||
// // out[x] = 2 * saw(dt + this.phase) - 1;
|
||||
// // out[x] = polySaw(this.phase, dt);
|
||||
// }
|
||||
// this.phase = this.phase + dt * outlen;
|
||||
|
||||
// this.phase %= sampleRate;
|
||||
|
||||
return true;
|
||||
|
||||
// for (let z = 0; z < outputs.length; z++) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user