Skip to content

Instantly share code, notes, and snippets.

@reececomo
Last active August 2, 2025 15:04
Show Gist options
  • Select an option

  • Save reececomo/b015e6e2aac0ac45c09d2d6cb7a25dd5 to your computer and use it in GitHub Desktop.

Select an option

Save reececomo/b015e6e2aac0ac45c09d2d6cb7a25dd5 to your computer and use it in GitHub Desktop.
Get the uncompressed size of WebAudio
async function logWebAudioStats( url )
{
const response = await fetch(url);
const responseBytes = await response.arrayBuffer();
// decode audio
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const audioBuffer = await audioContext.decodeAudioData(responseBytes);
// get statistics
const { duration, sampleRate, numberOfChannels } = audioBuffer;
const sizeKB = getWebAudioBufferSizeKB(duration, numberOfChannels, sampleRate);
console.info("Decoded WebAudio stats:", {
url,
sizeKB: sizeKB.toFixed(2) + " kilobytes",
sizeMB: (sizeKB/1024).toFixed(2) + " megabytes",
duration,
sampleRate,
numberOfChannels,
});
}
function getWebAudioBufferSizeKB(durationSeconds, numberOfChannels = 1, sampleRate = 44100)
{
// WebAudio buffers use 4 bytes per sample
// ttps://github.com/WebAudio/web-audio-api/issues/2396
const bytesPerSample = 4;
const sizeBytes = numberOfChannels * durationSeconds * sampleRate * bytesPerSample;
return sizeBytes / 1024;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment