TypeScript & WebAudio
Encode PCM or WAV audio, inspect containers without WASM, and decode .qaa streams or QAAR pointers into WebAudio AudioBuffers.
@quajs/quaaudio embeds the cleanroom WebAssembly codec binary inline as Base64 (src/wasm-binary.generated.ts), requiring zero external .wasm asset hosting or fetch configuration. It runs identically in browsers, Node.js (>= 20.0.0), Cloudflare Workers, and constrained JavaScript realms.
Reference: Full signatures and field definitions are documented in the TypeScript API Reference.
Package installation & subpaths
Shellpnpm add @quajs/quaaudio
The package exports five runtime-focused ESM entry points:
| Entry | Target environment | Responsibility |
|---|---|---|
@quajs/quaaudio |
Browser / Node / Worker | PCM & WAV encode/decode, .qaab bank builder & mounted decode, header helpers |
@quajs/quaaudio/web-audio |
Browser WebAudio | Direct AudioBuffer decoding for standalone .qaa tracks and QAAR bank pointers |
@quajs/quaaudio/assets |
Generic JS / legacy hosts | Resolve .qaa or QAAR payloads into standard RIFF/WAVE bytes on demand |
@quajs/quaaudio/quack |
Node.js build pipelines | Quack bundler packAudioAssetsIntoSolidBank and QuaAudioPlugin |
@quajs/quaaudio/header |
Bare JS / QuickJS realms | Zero-WASM magic checks and pure DataView header/pointer parsing |
Encoding PCM and WAV
Encoding raw PCM arrays
encodePcmToQuaAudio accepts Int16Array, Int32Array, or Float32Array interleaved samples normalized to [-1, 1]. Lossless spectral compression with solid frame clusters (solid: true) is the default:
TypeScriptimport { decodeQuaAudio, encodePcmToQuaAudio } from '@quajs/quaaudio'// 1. Default: Deterministic Spectral Lossless (bit-exact integer recovery)const losslessQaa = await encodePcmToQuaAudio(pcmInt16, { sampleRate: 48000, channels: 2,})// 2. Perceptual Spatial VBR targeting a specific ratio (e.g. 12:1)const lossyQaa = await encodePcmToQuaAudio(pcmFloat32, { sampleRate: 48000, channels: 2, mode: 'lossy', targetRatio: 12, precision: 'int16',})const decoded = await decodeQuaAudio(losslessQaa)// Lossless decodes also expose exact integer PCM in decoded.exactPcmI32
Note: When encoding an
Int32Arrayin lossless mode without specifyingprecision,encodePcmToQuaAudiodefaults to'int24';Int16Arrayinputs automatically select'int16'.
Encoding WAV files and solid banks
When working with RIFF/WAVE files directly, use encodeWavToQuaAudio or pack multiple tracks into one cross-track .qaab solid bank with buildSolidBankFromWavs:
TypeScriptimport { buildSolidBankFromWavs, decodeSolidBankTrack, encodeWavToQuaAudio,} from '@quajs/quaaudio'const qaa = await encodeWavToQuaAudio(wavBytes, { mode: 'lossless' })const bankBytes = await buildSolidBankFromWavs([ { key: 'voice/ch01/001.wav', wavBytes: clip1Wav, mode: 'lossless' }, { key: 'voice/ch01/002.wav', wavBytes: clip2Wav, mode: 'lossless' },])const line1 = await decodeSolidBankTrack(bankBytes, 'voice/ch01/001.wav')
Mounted .qaab LRU handles in WASM
Repeatedly decoding clips from the same .qaab bank uses a WeakMap keyed by the Uint8Array instance and a 4-slot bounded LRU handle cache inside WASM:
- The first
decodeSolidBankTrack(bankBytes, trackKey)mountsbankBytes, verifies its CRC-32, and parses the directory once. - Subsequent calls with the same
bankBytesinstance pass only the track key string to WASM. - If a fifth distinct bank evicts an older handle (
-2), the JS wrapper automatically re-mounts and retries without error. - Call
await unmountSolidBank(bankBytes)to eagerly release WASM memory when unloading a scene or asset bundle.
Playing directly in WebAudio
@quajs/quaaudio/web-audio converts .qaa streams or QAAR solid-bank pointers straight into full-precision 32-bit float AudioBuffer objects, bypassing browser MP3/Ogg resamplers and lowpass filters:
TypeScriptimport { decodeQuaAudioToAudioBuffer } from '@quajs/quaaudio/web-audio'async function playAsset( audioCtx: AudioContext, assetBytes: Uint8Array, loadBankAsset: (bankAssetName: string) => Promise<Uint8Array>,) { const audioBuffer = await decodeQuaAudioToAudioBuffer( audioCtx, assetBytes, loadBankAsset, // Invoked only for QAAR pointers, with leading 'data/' stripped ) const source = audioCtx.createBufferSource() source.buffer = audioBuffer source.connect(audioCtx.destination) source.start() return source}
For legacy sinks that require standard WAV bytes, @quajs/quaaudio/assets exposes resolveQuaAudioToWavBytes, which resolves .qaa or QAAR payloads to RIFF/WAVE and passes non-QuaAudio buffers through unchanged:
TypeScriptimport { resolveQuaAudioToWavBytes } from '@quajs/quaaudio/assets'const wavBytes = await resolveQuaAudioToWavBytes(assetBytes, { loadDataAsset: name => qpkBundle.readData(name),})
Zero-WASM header inspection
@quajs/quaaudio/header (also re-exported from the root entry) parses headers and pointers in pure JavaScript without loading WASM. UTF-8 TextEncoder / TextDecoder instances are created lazily on first use so the module imports cleanly even inside minimal QuickJS realms:
TypeScriptimport { inspectQuaAudio, isQuaAudio, isQuaAudioBank, isQuaAudioPointer, parseQuaAudioPointer,} from '@quajs/quaaudio/header'if (isQuaAudio(bytes)) { const info = inspectQuaAudio(bytes) console.log(info.mode, info.sampleRate, info.leadingSilentSamples)} else if (isQuaAudioPointer(bytes)) { const ptr = parseQuaAudioPointer(bytes) console.log(ptr.bankPath, ptr.trackKey, ptr.durationSeconds)}