fix(audio): implement true 40hz binaural stereo oscillators

This commit is contained in:
djg 2026-09-23 19:06:24 +00:00
parent 0e7d1e796d
commit ad413165ee

View file

@ -7,16 +7,12 @@ let noiseNode;
let gainNode; let gainNode;
let currentNoiseType = 'off'; let currentNoiseType = 'off';
/**
* Procedural noise generation (Brownian, Pink)
*/
function createNoiseBuffer(type) { function createNoiseBuffer(type) {
const bufferSize = audioCtx.sampleRate * 2; // 2 seconds of noise const bufferSize = audioCtx.sampleRate * 2;
const buffer = audioCtx.createBuffer(1, bufferSize, audioCtx.sampleRate); const buffer = audioCtx.createBuffer(1, bufferSize, audioCtx.sampleRate);
const output = buffer.getChannelData(0); const output = buffer.getChannelData(0);
if (type === 'rain') { if (type === 'rain') {
// Procedural rain using a low-pass filter over white noise
let lastOut = 0; let lastOut = 0;
for (let i = 0; i < bufferSize; i++) { for (let i = 0; i < bufferSize; i++) {
let white = Math.random() * 2 - 1; let white = Math.random() * 2 - 1;
@ -24,18 +20,13 @@ function createNoiseBuffer(type) {
lastOut = output[i]; lastOut = output[i];
output[i] *= 2.0; output[i] *= 2.0;
} }
} else if (type === 'gamma') {
// 40Hz Gamma binaural - just placeholder noise here, handled in real oscillators playing
for (let i = 0; i < bufferSize; i++) {
output[i] = (Math.random() * 2 - 1) * 0.1;
}
} else if (type === 'brown') { } else if (type === 'brown') {
let lastOut = 0; let lastOut = 0;
for (let i = 0; i < bufferSize; i++) { for (let i = 0; i < bufferSize; i++) {
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];
output[i] *= 3.5; // Compensate for gain output[i] *= 3.5;
} }
} else if (type === 'pink') { } else if (type === 'pink') {
let b0 = 0, b1 = 0, b2 = 0, b3 = 0, b4 = 0, b5 = 0, b6 = 0; let b0 = 0, b1 = 0, b2 = 0, b3 = 0, b4 = 0, b5 = 0, b6 = 0;
@ -48,7 +39,7 @@ function createNoiseBuffer(type) {
b4 = 0.55000 * b4 + white * 0.5329522; b4 = 0.55000 * b4 + white * 0.5329522;
b5 = -0.7616 * b5 - white * 0.0168980; b5 = -0.7616 * b5 - white * 0.0168980;
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; // Compensate for gain output[i] *= 0.11;
b6 = white * 0.115926; b6 = white * 0.115926;
} }
} }
@ -83,6 +74,40 @@ function playNoise(type) {
if (type === 'off') return; if (type === 'off') return;
if (type === 'gamma') {
// 40Hz Gamma true binaural setup
const oscLeft = audioCtx.createOscillator();
const oscRight = audioCtx.createOscillator();
const pannerLeft = audioCtx.createStereoPanner();
const pannerRight = audioCtx.createStereoPanner();
oscLeft.frequency.value = 200; // Base carrier frequency
oscRight.frequency.value = 240; // 40Hz beat frequency difference
pannerLeft.pan.value = -1; // Hard left
pannerRight.pan.value = 1; // Hard right
oscLeft.connect(pannerLeft);
oscRight.connect(pannerRight);
pannerLeft.connect(gainNode);
pannerRight.connect(gainNode);
oscLeft.start();
oscRight.start();
noiseNode = {
stop: () => {
oscLeft.stop(); oscRight.stop();
},
disconnect: () => {
oscLeft.disconnect(); oscRight.disconnect();
pannerLeft.disconnect(); pannerRight.disconnect();
}
};
return;
}
noiseNode = audioCtx.createBufferSource(); noiseNode = audioCtx.createBufferSource();
noiseNode.buffer = createNoiseBuffer(type); noiseNode.buffer = createNoiseBuffer(type);
noiseNode.loop = true; noiseNode.loop = true;
@ -97,11 +122,9 @@ function setupAudioUI() {
soundBtns.forEach(btn => { soundBtns.forEach(btn => {
btn.addEventListener('click', () => { btn.addEventListener('click', () => {
// Update UI
soundBtns.forEach(b => b.classList.remove('active')); soundBtns.forEach(b => b.classList.remove('active'));
btn.classList.add('active'); btn.classList.add('active');
// Update audio
const soundType = btn.dataset.sound; const soundType = btn.dataset.sound;
currentNoiseType = soundType; currentNoiseType = soundType;
playNoise(soundType); playNoise(soundType);
@ -111,7 +134,6 @@ function setupAudioUI() {
if (volControl) { if (volControl) {
volControl.addEventListener('input', (e) => { volControl.addEventListener('input', (e) => {
if (gainNode) { if (gainNode) {
// Logarithmic volume scaling for smoother feel
const val = Number(e.target.value); const val = Number(e.target.value);
gainNode.gain.setTargetAtTime(val * val, audioCtx.currentTime, 0.05); gainNode.gain.setTargetAtTime(val * val, audioCtx.currentTime, 0.05);
} }
@ -119,18 +141,16 @@ function setupAudioUI() {
} }
} }
// Wire it up on app load
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
setupAudioUI(); setupAudioUI();
}); });
window.playGroundingPing = function() { window.playGroundingPing = function() {
if (!audioCtx) initAudio(); if (!audioCtx) initAudio();
var osc = audioCtx.createOscillator(); var osc = audioCtx.createOscillator();
var gain = audioCtx.createGain(); var gain = audioCtx.createGain();
osc.type = 'sine'; osc.type = 'sine';
osc.frequency.setValueAtTime(528, audioCtx.currentTime); // 528hz droplet osc.frequency.setValueAtTime(528, audioCtx.currentTime);
gain.gain.setValueAtTime(0, audioCtx.currentTime); gain.gain.setValueAtTime(0, audioCtx.currentTime);
gain.gain.linearRampToValueAtTime(0.3, audioCtx.currentTime + 0.1); gain.gain.linearRampToValueAtTime(0.3, audioCtx.currentTime + 0.1);
gain.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 1.5); gain.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 1.5);