mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-23 19:48:31 +00:00
codeformat
This commit is contained in:
parent
83dfbb84c2
commit
1e352fdf80
@ -20,104 +20,110 @@ const fm = (osc, harmonicityRatio, modulationIndex, wave = 'sine') => {
|
|||||||
return mod(modfreq, modgain, wave);
|
return mod(modfreq, modgain, wave);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export function registerSynthSounds() {
|
export function registerSynthSounds() {
|
||||||
['sine', 'square', 'triangle',
|
['sine', 'square', 'triangle', 'sawtooth', 'pink', 'white', 'brown'].forEach((wave) => {
|
||||||
'sawtooth', 'pink', 'white',
|
registerSound(
|
||||||
'brown'].forEach((wave) => {
|
wave,
|
||||||
registerSound(
|
(t, value, onended) => {
|
||||||
wave,
|
// destructure adsr here, because the default should be different for synths and samples
|
||||||
(t, value, onended) => {
|
let {
|
||||||
// destructure adsr here, because the default should be different for synths and samples
|
attack = 0.001,
|
||||||
let {
|
decay = 0.05,
|
||||||
attack = 0.001,
|
sustain = 0.6,
|
||||||
decay = 0.05,
|
release = 0.01,
|
||||||
sustain = 0.6,
|
fmh: fmHarmonicity = 1,
|
||||||
release = 0.01,
|
fmi: fmModulationIndex,
|
||||||
fmh: fmHarmonicity = 1,
|
fmenv: fmEnvelopeType = 'lin',
|
||||||
fmi: fmModulationIndex,
|
fmattack: fmAttack,
|
||||||
fmenv: fmEnvelopeType = 'lin',
|
fmdecay: fmDecay,
|
||||||
fmattack: fmAttack,
|
fmsustain: fmSustain,
|
||||||
fmdecay: fmDecay,
|
fmrelease: fmRelease,
|
||||||
fmsustain: fmSustain,
|
fmvelocity: fmVelocity,
|
||||||
fmrelease: fmRelease,
|
fmwave: fmWaveform = 'sine',
|
||||||
fmvelocity: fmVelocity,
|
vib = 0,
|
||||||
fmwave: fmWaveform = 'sine',
|
vibmod = 0.5,
|
||||||
vib = 0,
|
noise = 0,
|
||||||
vibmod = 0.5,
|
} = value;
|
||||||
noise = 0,
|
let { n, note, freq } = value;
|
||||||
} = value;
|
// with synths, n and note are the same thing
|
||||||
let { n, note, freq } = value;
|
note = note || 36;
|
||||||
// with synths, n and note are the same thing
|
if (typeof note === 'string') {
|
||||||
note = note || 36;
|
note = noteToMidi(note); // e.g. c3 => 48
|
||||||
if (typeof note === 'string') {
|
}
|
||||||
note = noteToMidi(note); // e.g. c3 => 48
|
// get frequency
|
||||||
}
|
if (!freq && typeof note === 'number') {
|
||||||
// get frequency
|
freq = midiToFreq(note); // + 48);
|
||||||
if (!freq && typeof note === 'number') {
|
}
|
||||||
freq = midiToFreq(note); // + 48);
|
// maybe pull out the above frequency resolution?? (there is also getFrequency but it has no default)
|
||||||
}
|
// make oscillator
|
||||||
// maybe pull out the above frequency resolution?? (there is also getFrequency but it has no default)
|
const {
|
||||||
// make oscillator
|
node: o,
|
||||||
const { node: o, stop, dry_node = null } = getOscillator({
|
stop,
|
||||||
t,
|
dry_node = null,
|
||||||
s: wave,
|
} = getOscillator({
|
||||||
freq,
|
t,
|
||||||
vib,
|
s: wave,
|
||||||
vibmod,
|
freq,
|
||||||
partials: n,
|
vib,
|
||||||
noise: noise,
|
vibmod,
|
||||||
});
|
partials: n,
|
||||||
// FM + FM envelope
|
noise: noise,
|
||||||
let stopFm, fmEnvelope;
|
});
|
||||||
if (fmModulationIndex) {
|
// FM + FM envelope
|
||||||
const { node: modulator, stop } = fm(dry_node !== null ? dry_node : o, fmHarmonicity, fmModulationIndex, fmWaveform);
|
let stopFm, fmEnvelope;
|
||||||
if (![fmAttack, fmDecay, fmSustain, fmRelease, fmVelocity].find((v) => v !== undefined)) {
|
if (fmModulationIndex) {
|
||||||
// no envelope by default
|
const { node: modulator, stop } = fm(
|
||||||
modulator.connect(dry_node !== null ? dry_node.frequency : o.frequency);
|
dry_node !== null ? dry_node : o,
|
||||||
} else {
|
fmHarmonicity,
|
||||||
fmAttack = fmAttack ?? 0.001;
|
fmModulationIndex,
|
||||||
fmDecay = fmDecay ?? 0.001;
|
fmWaveform,
|
||||||
fmSustain = fmSustain ?? 1;
|
);
|
||||||
fmRelease = fmRelease ?? 0.001;
|
if (![fmAttack, fmDecay, fmSustain, fmRelease, fmVelocity].find((v) => v !== undefined)) {
|
||||||
fmVelocity = fmVelocity ?? 1;
|
// no envelope by default
|
||||||
fmEnvelope = getEnvelope(fmAttack, fmDecay, fmSustain, fmRelease, fmVelocity, t);
|
modulator.connect(dry_node !== null ? dry_node.frequency : o.frequency);
|
||||||
if (fmEnvelopeType === 'exp') {
|
} else {
|
||||||
fmEnvelope = getExpEnvelope(fmAttack, fmDecay, fmSustain, fmRelease, fmVelocity, t);
|
fmAttack = fmAttack ?? 0.001;
|
||||||
fmEnvelope.node.maxValue = fmModulationIndex * 2;
|
fmDecay = fmDecay ?? 0.001;
|
||||||
fmEnvelope.node.minValue = 0.00001;
|
fmSustain = fmSustain ?? 1;
|
||||||
}
|
fmRelease = fmRelease ?? 0.001;
|
||||||
modulator.connect(fmEnvelope.node);
|
fmVelocity = fmVelocity ?? 1;
|
||||||
fmEnvelope.node.connect(dry_node !== null ? dry_node.frequency : o.frequency);
|
fmEnvelope = getEnvelope(fmAttack, fmDecay, fmSustain, fmRelease, fmVelocity, t);
|
||||||
|
if (fmEnvelopeType === 'exp') {
|
||||||
|
fmEnvelope = getExpEnvelope(fmAttack, fmDecay, fmSustain, fmRelease, fmVelocity, t);
|
||||||
|
fmEnvelope.node.maxValue = fmModulationIndex * 2;
|
||||||
|
fmEnvelope.node.minValue = 0.00001;
|
||||||
}
|
}
|
||||||
stopFm = stop;
|
modulator.connect(fmEnvelope.node);
|
||||||
|
fmEnvelope.node.connect(dry_node !== null ? dry_node.frequency : o.frequency);
|
||||||
}
|
}
|
||||||
|
stopFm = stop;
|
||||||
|
}
|
||||||
|
|
||||||
// turn down
|
// turn down
|
||||||
const g = gainNode(0.3);
|
const g = gainNode(0.3);
|
||||||
|
|
||||||
// gain envelope
|
// gain envelope
|
||||||
const { node: envelope, stop: releaseEnvelope } = getEnvelope(attack, decay, sustain, release, 1, t);
|
const { node: envelope, stop: releaseEnvelope } = getEnvelope(attack, decay, sustain, release, 1, t);
|
||||||
|
|
||||||
o.onended = () => {
|
o.onended = () => {
|
||||||
o.disconnect();
|
o.disconnect();
|
||||||
g.disconnect();
|
g.disconnect();
|
||||||
onended();
|
onended();
|
||||||
};
|
};
|
||||||
return {
|
return {
|
||||||
node: o.connect(g).connect(envelope),
|
node: o.connect(g).connect(envelope),
|
||||||
stop: (releaseTime) => {
|
stop: (releaseTime) => {
|
||||||
releaseEnvelope(releaseTime);
|
releaseEnvelope(releaseTime);
|
||||||
fmEnvelope?.stop(releaseTime);
|
fmEnvelope?.stop(releaseTime);
|
||||||
let end = releaseTime + release;
|
let end = releaseTime + release;
|
||||||
stop(end);
|
stop(end);
|
||||||
stopFm?.(end);
|
stopFm?.(end);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
{ type: 'synth', prebake: true },
|
{ type: 'synth', prebake: true },
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function waveformN(partials, type) {
|
export function waveformN(partials, type) {
|
||||||
@ -163,16 +169,16 @@ export function getNoiseOscillator({ t, ac, type = 'white' }) {
|
|||||||
output[i] = Math.random() * 2 - 1;
|
output[i] = Math.random() * 2 - 1;
|
||||||
} else if (type === 'brown') {
|
} else if (type === 'brown') {
|
||||||
let white = Math.random() * 2 - 1;
|
let white = Math.random() * 2 - 1;
|
||||||
output[i] = (lastOut + (0.02 * white)) / 1.02;
|
output[i] = (lastOut + 0.02 * white) / 1.02;
|
||||||
lastOut = output[i];
|
lastOut = output[i];
|
||||||
} else if (type === 'pink') {
|
} else if (type === 'pink') {
|
||||||
let white = Math.random() * 2 - 1;
|
let white = Math.random() * 2 - 1;
|
||||||
b0 = 0.99886 * b0 + white * 0.0555179;
|
b0 = 0.99886 * b0 + white * 0.0555179;
|
||||||
b1 = 0.99332 * b1 + white * 0.0750759;
|
b1 = 0.99332 * b1 + white * 0.0750759;
|
||||||
b2 = 0.96900 * b2 + white * 0.1538520;
|
b2 = 0.969 * b2 + white * 0.153852;
|
||||||
b3 = 0.86650 * b3 + white * 0.3104856;
|
b3 = 0.8665 * b3 + white * 0.3104856;
|
||||||
b4 = 0.55000 * b4 + white * 0.5329522;
|
b4 = 0.55 * b4 + white * 0.5329522;
|
||||||
b5 = -0.7616 * b5 - white * 0.0168980;
|
b5 = -0.7616 * b5 - white * 0.016898;
|
||||||
output[i] = b0 + b1 + b2 + b3 + b4 + b5 + b6 + white * 0.5362;
|
output[i] = b0 + b1 + b2 + b3 + b4 + b5 + b6 + white * 0.5362;
|
||||||
output[i] *= 0.11;
|
output[i] *= 0.11;
|
||||||
b6 = white * 0.115926;
|
b6 = white * 0.115926;
|
||||||
@ -186,7 +192,7 @@ export function getNoiseOscillator({ t, ac, type = 'white' }) {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
node: o,
|
node: o,
|
||||||
stop: (time) => o.stop(time)
|
stop: (time) => o.stop(time),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,11 +202,11 @@ export function getOscillator({ s, freq, t, vib, vibmod, partials, noise }) {
|
|||||||
let o;
|
let o;
|
||||||
|
|
||||||
if (['pink', 'white', 'brown'].includes(s)) {
|
if (['pink', 'white', 'brown'].includes(s)) {
|
||||||
let noiseOscillator = getNoiseOscillator({ t: t, ac: getAudioContext(), type: s })
|
let noiseOscillator = getNoiseOscillator({ t: t, ac: getAudioContext(), type: s });
|
||||||
return {
|
return {
|
||||||
node: noiseOscillator.node,
|
node: noiseOscillator.node,
|
||||||
stop: noiseOscillator.stop
|
stop: noiseOscillator.stop,
|
||||||
}
|
};
|
||||||
} else {
|
} else {
|
||||||
if (!partials || s === 'sine') {
|
if (!partials || s === 'sine') {
|
||||||
o = getAudioContext().createOscillator();
|
o = getAudioContext().createOscillator();
|
||||||
@ -238,7 +244,7 @@ export function getOscillator({ s, freq, t, vib, vibmod, partials, noise }) {
|
|||||||
// Connecting the main oscillator to the gain node
|
// Connecting the main oscillator to the gain node
|
||||||
o.connect(o_gain).connect(mix_gain);
|
o.connect(o_gain).connect(mix_gain);
|
||||||
|
|
||||||
// Instanciating a noise oscillator and connecting
|
// Instanciating a noise oscillator and connecting
|
||||||
const noiseOscillator = getNoiseOscillator({ t: t, ac: ac, type: 'pink' });
|
const noiseOscillator = getNoiseOscillator({ t: t, ac: ac, type: 'pink' });
|
||||||
noiseOscillator.node.connect(n_gain).connect(mix_gain);
|
noiseOscillator.node.connect(n_gain).connect(mix_gain);
|
||||||
|
|
||||||
@ -249,8 +255,8 @@ export function getOscillator({ s, freq, t, vib, vibmod, partials, noise }) {
|
|||||||
vibrato_oscillator?.stop(time);
|
vibrato_oscillator?.stop(time);
|
||||||
o.stop(time);
|
o.stop(time);
|
||||||
noiseOscillator.stop(time);
|
noiseOscillator.stop(time);
|
||||||
}
|
},
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -262,4 +268,3 @@ export function getOscillator({ s, freq, t, vib, vibmod, partials, noise }) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -35,10 +35,7 @@ flavours of noise, here written from hard to soft.
|
|||||||
|
|
||||||
Some amount of pink noise can also be added to any oscillator by using the `noise` paremeter:
|
Some amount of pink noise can also be added to any oscillator by using the `noise` paremeter:
|
||||||
|
|
||||||
<MiniRepl
|
<MiniRepl client:idle tune={`note("c3").noise("<0.1 0.25 0.5>").scope()`} />
|
||||||
client:idle
|
|
||||||
tune={`note("c3").noise("<0.1 0.25 0.5>").scope()`}
|
|
||||||
/>
|
|
||||||
|
|
||||||
### Additive Synthesis
|
### Additive Synthesis
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user