Rust & cluster streaming
Integrate the pure-Rust quaaudio crate natively: full-track coding, O(1)-memory QaaStreamDecoder streaming, and SolidBankArchive LRU playback.
The quaaudio crate (located in native/ with zero external third-party crates) is the core implementation behind both native binaries and the embedded WASM engine. Native Rust applications also gain direct access to QaaStreamDecoder for bounded-memory music streaming and SolidBankArchive for repeated bank playback.
Reference: Full struct and function signatures live in the Rust API Reference.
Adding the crate
Depend on native/ locally or via Git in your Cargo.toml:
TOML[dependencies]# Local checkout pathquaaudio = { path = "../QuaAudio/native" }# Or Git dependency# quaaudio = { git = "https://github.com/QuaDevTeam/QuaAudio.git" }
Encoding and full-track decoding
Use encode_lossless_qaa, encode_lossy_vbr_qaa, and decode_qaa:
Rustuse quaaudio::{ decode_qaa, encode_lossless_qaa, encode_lossy_vbr_qaa, QuaAudioError, SamplePrecision,};pub fn roundtrip_demo(pcm_i32: &[i32], pcm_f32: &[f32]) -> Result<(), QuaAudioError> { // 1. Spectral lossless with solid clusters enabled let lossless = encode_lossless_qaa(pcm_i32, 2, 48_000, SamplePrecision::Int16, true)?; let dec = decode_qaa(&lossless)?; assert_eq!(dec.pcm_i32.as_deref(), Some(pcm_i32)); // 2. Perceptual spatial VBR targeting 12.0x compression let lossy = encode_lossy_vbr_qaa(pcm_f32, 2, 48_000, SamplePrecision::Int16, 12.0, true)?; let lossy_dec = decode_qaa(&lossy)?; assert_eq!(lossy_dec.pcm_f32.len(), pcm_f32.len()); Ok(())}
QaaStreamDecoder: bounded-memory streaming
Decoding an entire multi-minute BGM track into RAM at once allocates tens of megabytes of PCM. QaaStreamDecoder reads only the 48-byte QaaHeader and the 20-byte-per-cluster ClusterEntry table upfront, then decodes only the active 64-frame cluster (65,536 samples ≈ 1.36s at 48 kHz) as playback advances or seeks:
Rustuse std::sync::Arc;use quaaudio::{QaaStreamDecoder, QuaAudioError};pub fn stream_bgm(qaa_bytes: Arc<[u8]>) -> Result<Vec<f32>, QuaAudioError> { let mut decoder = QaaStreamDecoder::new(qaa_bytes)?; let channels = decoder.header().channels as usize; // Sample-accurate O(log C) seeking with seamless TDAC overlap continuity decoder.seek_to_sample(48_000 * 10)?; let mut ring_buf = vec![0.0f32; 512 * channels]; let mut collected = Vec::new(); loop { let written = decoder.read_interleaved_f32(&mut ring_buf)?; if written == 0 { break; } collected.extend_from_slice(&ring_buf[..written]); } Ok(collected)}
Key behavior:
- Zero-work silence regions: Seeking or reading inside
leading_silent_samplesor trailing silence fills zeros directly without touching cluster payloads. - Exact TDAC continuity across VBR clusters: In
LossyVbrmode,QaaStreamDecodercarrieslossy_tailoverlap buffers across clusters and restores overlap state accurately even on arbitrary mid-track seeks.
Building and serving .qaab solid banks
When playing many dialogue lines or sound effects from a bank, use SolidBankArchive::open rather than stateless decode_bank_track. SolidBankArchive validates the bank CRC-32 and decompresses the directory once, indexes tracks in a HashMap, and maintains a mutex-guarded 2-slot LRU cache of decompressed solid chunks:
Rustuse std::sync::Arc;use quaaudio::{ build_solid_bank, BankTrackInput, CodecMode, QuaAudioError, SamplePrecision, SolidBankArchive, DEFAULT_SOLID_CHUNK_BUDGET,};pub fn build_and_serve(clips: Vec<(String, Vec<i32>)>) -> Result<(), QuaAudioError> { let inputs: Vec<BankTrackInput> = clips .into_iter() .map(|(key, pcm_i32)| { let pcm_f32 = quaaudio::pcm_i32_to_f32(&pcm_i32, SamplePrecision::Int16); BankTrackInput { key, sample_rate: 48_000, channels: 1, precision: SamplePrecision::Int16, mode: CodecMode::Lossless, pcm_i32, pcm_f32, } }) .collect(); let (bank_bytes, _metas) = build_solid_bank(&inputs, DEFAULT_SOLID_CHUNK_BUDGET)?; let archive = SolidBankArchive::open(Arc::from(bank_bytes))?; let line = archive.decode_track("voice/ch01/001.ogg")?; println!("decoded {} samples at {} Hz", line.total_samples, line.sample_rate); Ok(())}
Native RIFF/WAVE helpers
quaaudio::wav provides zero-dependency WAV parsing and serialization for Int8, Int16, Int24, Int32 (scaled to 24-bit), and Float32, including WAVE_FORMAT_EXTENSIBLE (0xFFFE):
parse_wav(&bytes)populates both precision-scaledpcm_i32and normalizedpcm_f32.write_wav_exact(...)preserves integer precision or writes IEEE Float32 (fmt_tag = 3).write_wav_pcm16(...)emits standard 16-bit PCM WAV for legacy audio sinks.