Tiny beatbyte player for js13k
Click a song to play. Click again to stop.
Bytebeat music (or one-liner music) is a piece of rhythmic and somewhat melodic music with no score, no instruments, and no real oscillators. It's simply a single-line formula that defines a waveform as a function of time, processed (usually) 8000 times per second, resulting in an audible waveform with a 256-step resolution from silence (0) to full amplitude (256). If you put that formula into a program with a loop that increments time variable (t), you can generate the headerless unsigned 8 bit mono 8kHz audio stream on output, like in this application. Since these directly output a waveform, they have great performance in compiled languages and can often be ran on even the weakest embedded devices.
// Setup code
const audioCtx = new AudioContext();
const masterVolume = 0.5;
let scriptProcessor = null;
// Player
function playBytebeat(f, sampleRate, mapFunc) {
let t = 0;
scriptProcessor = audioCtx.createScriptProcessor(4096, 1, 1);
scriptProcessor.onaudioprocess = (e) => {
const buf = e.outputBuffer.getChannelData(0);
for (let i = 0; i < buf.length; i++) {
buf[i] = masterVolume * mapFunc(f(int(t)));
t += sampleRate / audioCtx.sampleRate;
}
};
scriptProcessor.connect(audioCtx.destination);
}
// Example
playBytebeat((t) => t*(1+'4451'[t>>13&3]/10)&t>>9+(.003*t&3), 11025, unsignedByteToFloat);