myadhd/site/static/audio.js

127 lines
3.6 KiB
JavaScript

/* =========================================================================
BACKGROUND AUDIO (PROCEDURAL NOISE)
========================================================================= */
let audioCtx;
let noiseNode;
let gainNode;
let currentNoiseType = 'off';
/**
* Procedural noise generation (Brownian, Pink)
*/
function createNoiseBuffer(type) {
const bufferSize = audioCtx.sampleRate * 2; // 2 seconds of noise
const buffer = audioCtx.createBuffer(1, bufferSize, audioCtx.sampleRate);
const output = buffer.getChannelData(0);
if (type === 'brown') {
let lastOut = 0;
for (let i = 0; i < bufferSize; i++) {
let white = Math.random() * 2 - 1;
output[i] = (lastOut + (0.02 * white)) / 1.02;
lastOut = output[i];
output[i] *= 3.5; // Compensate for gain
}
} else if (type === 'pink') {
let b0 = 0, b1 = 0, b2 = 0, b3 = 0, b4 = 0, b5 = 0, b6 = 0;
for (let i = 0; i < bufferSize; i++) {
let white = Math.random() * 2 - 1;
b0 = 0.99886 * b0 + white * 0.0555179;
b1 = 0.99332 * b1 + white * 0.0750759;
b2 = 0.96900 * b2 + white * 0.1538520;
b3 = 0.86650 * b3 + white * 0.3104856;
b4 = 0.55000 * b4 + white * 0.5329522;
b5 = -0.7616 * b5 - white * 0.0168980;
output[i] = b0 + b1 + b2 + b3 + b4 + b5 + b6 + white * 0.5362;
output[i] *= 0.11; // Compensate for gain
b6 = white * 0.115926;
}
}
return buffer;
}
function initAudio() {
if (!audioCtx) {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
gainNode = audioCtx.createGain();
const volInput = document.getElementById('bg-volume');
gainNode.gain.value = volInput ? Number(volInput.value) : 0.2;
gainNode.connect(audioCtx.destination);
}
if (audioCtx.state === 'suspended') {
audioCtx.resume();
}
}
function stopNoise() {
if (noiseNode) {
noiseNode.stop();
noiseNode.disconnect();
noiseNode = null;
}
}
function playNoise(type) {
stopNoise();
initAudio();
if (type === 'off') return;
noiseNode = audioCtx.createBufferSource();
noiseNode.buffer = createNoiseBuffer(type);
noiseNode.loop = true;
noiseNode.connect(gainNode);
noiseNode.start();
}
function setupAudioUI() {
const soundBtns = document.querySelectorAll('.bg-sound');
const volControl = document.getElementById('bg-volume');
soundBtns.forEach(btn => {
btn.addEventListener('click', () => {
// Update UI
soundBtns.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
// Update audio
const soundType = btn.dataset.sound;
currentNoiseType = soundType;
playNoise(soundType);
});
});
if (volControl) {
volControl.addEventListener('input', (e) => {
if (gainNode) {
// Logarithmic volume scaling for smoother feel
const val = Number(e.target.value);
gainNode.gain.setTargetAtTime(val * val, audioCtx.currentTime, 0.05);
}
});
}
}
// Wire it up on app load
document.addEventListener('DOMContentLoaded', () => {
setupAudioUI();
});
window.playGroundingPing = function() {
if (!audioCtx) initAudio();
var osc = audioCtx.createOscillator();
var gain = audioCtx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(528, audioCtx.currentTime); // 528hz droplet
gain.gain.setValueAtTime(0, audioCtx.currentTime);
gain.gain.linearRampToValueAtTime(0.3, audioCtx.currentTime + 0.1);
gain.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 1.5);
osc.connect(gain);
gain.connect(gainNode);
osc.start();
osc.stop(audioCtx.currentTime + 1.5);
};