Finding, converting, and uploading `.mp3` or `.wav` assets for your browser games is slow. Using the Web Audio API, your game can generate authentic 8-bit sound effects directly via browser code. Here are the core formulas to feed your AI builder to generate classic synth audio effects.
1. The Retro Coin Collect Sound
Classic retro coins use two quick frequencies: a base note followed instantly by a higher fifth. Prompt the AI to use this double-tone ramp oscillator setup:
function playCoinSound() {
const ctx = new (window.AudioContext || window.webkitAudioContext)();
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.connect(gain);
gain.connect(ctx.destination);
osc.type = "sine";
osc.frequency.setValueAtTime(523.25, ctx.currentTime); // C5
osc.frequency.setValueAtTime(659.25, ctx.currentTime + 0.08); // E5
gain.gain.setValueAtTime(0.08, ctx.currentTime);
gain.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + 0.35);
osc.start();
osc.stop(ctx.currentTime + 0.35);
}2. The Retro Explosion / Noise Sound
Explosions require noise. Instead of clean sine waves, generate a buffer filled with random values (white noise), feed it through a low-pass band filter, and ramp down the volume:
function playExplosionSound() {
const ctx = new (window.AudioContext || window.webkitAudioContext)();
const bufferSize = ctx.sampleRate * 0.4; // 0.4 seconds
const buffer = ctx.createBuffer(1, bufferSize, ctx.sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < bufferSize; i++) {
data[i] = Math.random() * 2 - 1; // White noise
}
const noise = ctx.createBufferSource();
noise.buffer = buffer;
const filter = ctx.createBiquadFilter();
filter.type = "lowpass";
filter.frequency.setValueAtTime(800, ctx.currentTime);
filter.frequency.exponentialRampToValueAtTime(10, ctx.currentTime + 0.4);
const gain = ctx.createGain();
gain.gain.setValueAtTime(0.15, ctx.currentTime);
gain.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + 0.4);
noise.connect(filter);
filter.connect(gain);
gain.connect(ctx.destination);
noise.start();
noise.stop(ctx.currentTime + 0.4);
}

