Last active
August 2, 2025 15:04
-
-
Save reececomo/b015e6e2aac0ac45c09d2d6cb7a25dd5 to your computer and use it in GitHub Desktop.
Get the uncompressed size of WebAudio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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