Initial work on crackle
This commit is contained in:
parent
49aa9cf6da
commit
476bda812e
2 changed files with 15 additions and 6 deletions
|
|
@ -4,7 +4,7 @@ import { getAudioContext } from './superdough.mjs';
|
||||||
let noiseCache = {};
|
let noiseCache = {};
|
||||||
|
|
||||||
// lazy generates noise buffers and keeps them forever
|
// lazy generates noise buffers and keeps them forever
|
||||||
function getNoiseBuffer(type) {
|
function getNoiseBuffer(type, density) {
|
||||||
const ac = getAudioContext();
|
const ac = getAudioContext();
|
||||||
if (noiseCache[type]) {
|
if (noiseCache[type]) {
|
||||||
return noiseCache[type];
|
return noiseCache[type];
|
||||||
|
|
@ -34,17 +34,26 @@ function getNoiseBuffer(type) {
|
||||||
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;
|
||||||
|
} else if (type === 'crackle') {
|
||||||
|
if (Math.random() < (Math.random() * (density - 0.001) + density).toFixed(4)) {
|
||||||
|
output[i] = Math.random() * 2 - 1;
|
||||||
|
} else {
|
||||||
|
output[i] = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
noiseCache[type] = noiseBuffer;
|
|
||||||
|
// Prevent caching to randomize crackles
|
||||||
|
if (type !== "crackle")
|
||||||
|
noiseCache[type] = noiseBuffer;
|
||||||
return noiseBuffer;
|
return noiseBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// expects one of noises as type
|
// expects one of noises as type
|
||||||
export function getNoiseOscillator(type = 'white', t) {
|
export function getNoiseOscillator(type = 'white', t, density = 0.02) {
|
||||||
const ac = getAudioContext();
|
const ac = getAudioContext();
|
||||||
const o = ac.createBufferSource();
|
const o = ac.createBufferSource();
|
||||||
o.buffer = getNoiseBuffer(type);
|
o.buffer = getNoiseBuffer(type, density);
|
||||||
o.loop = true;
|
o.loop = true;
|
||||||
o.start(t);
|
o.start(t);
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ const fm = (osc, harmonicityRatio, modulationIndex, wave = 'sine') => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const waveforms = ['sine', 'square', 'triangle', 'sawtooth'];
|
const waveforms = ['sine', 'square', 'triangle', 'sawtooth'];
|
||||||
const noises = ['pink', 'white', 'brown'];
|
const noises = ['pink', 'white', 'brown', 'crackle'];
|
||||||
|
|
||||||
export function registerSynthSounds() {
|
export function registerSynthSounds() {
|
||||||
[...waveforms, ...noises].forEach((s) => {
|
[...waveforms, ...noises].forEach((s) => {
|
||||||
|
|
@ -36,7 +36,7 @@ export function registerSynthSounds() {
|
||||||
if (waveforms.includes(s)) {
|
if (waveforms.includes(s)) {
|
||||||
sound = getOscillator(s, t, value);
|
sound = getOscillator(s, t, value);
|
||||||
} else {
|
} else {
|
||||||
sound = getNoiseOscillator(s, t);
|
sound = getNoiseOscillator(s, t, 0.002);
|
||||||
}
|
}
|
||||||
|
|
||||||
let { node: o, stop, triggerRelease } = sound;
|
let { node: o, stop, triggerRelease } = sound;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue