From 5216cb29a1563c37331c6c1599eb9df6609d7b5d Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 14:41:11 -0500 Subject: [PATCH] ws-audio2: bench lines (IMA / DSP decode, QOA encode / decode) and the guide's game audio section --- docs/guide/audio.md | 38 ++++++++++++++++++++++++++++++- tools/bench.luau | 55 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/docs/guide/audio.md b/docs/guide/audio.md index 94271f6..25f931d 100644 --- a/docs/guide/audio.md +++ b/docs/guide/audio.md @@ -179,6 +179,8 @@ Encoder options: | opus | `bitrate` (default 64000 per channel, 6000..510000); CELT only; resampled to 48 kHz | | aiff, au, voc | `sampleFormat` (see each format's `init.luau`) | | m4a, caf | ALAC (lossless); m4a and caf also keep tags | +| adx, vag | `loop = { start, stop }` (frames; default the source's `meta.source.loop`) | +| adpcm | `codec = "ima" \| "ima_wav" \| "ms"`, `blockAlign` (block codecs), `lowFirst` (ima nibble order) | `bitrate` is in **bits per second** for every encoder (`bitrate = 192000`), as the Decoder's `bitrate` field is. A value below 1000 is read as kbit/s (`bitrate = 192` is 192000), so older code that passed kbit/s to MP3 still works. @@ -189,7 +191,7 @@ Tags are written from `audio.tags`, or from the Decoder's `tags`, wherever the f | Read and write | Read only | |---|---| -| wav (+ RF64), flac (+ Ogg FLAC), mp3 / mp2, vorbis (.ogg), opus, aiff / aifc, au, voc, caf, m4a (ALAC) | aac (ADTS), m4a / caf AAC | +| wav (+ RF64), flac (+ Ogg FLAC), mp3 / mp2, vorbis (.ogg), opus, aiff / aifc, au, voc, caf, m4a (ALAC), qoa, adx, vag, adpcm (raw IMA / MS) | aac (ADTS), m4a / caf AAC, brstm / bcstm / bfstm, KVAG, wav / aifc / caf IMA and MS ADPCM | Each format's capabilities are listed in [FORMATS.md](../FORMATS.md). The audio tracks of video files open as Decoders too: see `v.audio` in [video.md](video.md). Music files (MOD, MIDI, ...) render to a Decoder: see @@ -243,6 +245,40 @@ end Roblox has no API to play raw samples, so on Roblox the decoded samples are data only, for example for analysis or visualisers. +### Game audio and ADPCM + +Console and game engines store sound as 4-bit ADPCM or QOA: small, and cheap to decode while the game runs. They load +and save like any other audio. Loop points (ADX, VAG, BRSTM) come out in `meta.source.loop` (and `dec.loop`), in +frames, `stop` exclusive; the encoders write them back. + +```luau +local tone = F.Types.Audio.new(22050, 1, 22050) -- 1 s of 440 Hz +for i = 0, tone.frames - 1 do + buffer.writef32(tone.samples, i * 4, 0.5 * math.sin(2 * math.pi * 440 * i / 22050)) +end +local qoa = assert(F.save(tone, nil, { format = "qoa" })) -- Quite OK Audio: 3.2 bits per sample, as qoa.h writes it +assert(F.detect(qoa) == "qoa" and F.audio.load(qoa).frames == tone.frames) +local adx = assert(F.save(tone, nil, { format = "adx", loop = { start = 0, stop = tone.frames } })) -- CRI ADX +local looped = assert(F.audio.load(adx)) +assert(looped.meta.source.loop.stop == tone.frames) +local vag = assert(F.save(tone, nil, { format = "vag" })) -- PlayStation VAG (mono VAGp; stereo is VAGi) +assert(F.audio.load(vag).frames >= tone.frames) -- + the blank frame VAGs start with, and frame padding +``` + +A raw ADPCM stream has no header, so the layout comes from the options, and only `opts.format` or the `.adpcm` / `.ima` +extension finds it: + + +```luau +local raw = assert(F.save(tone, nil, { format = "adpcm", codec = "ima" })) -- 4 bits per sample +local back = assert(F.audio.load(raw, { format = "adpcm", codec = "ima", sampleRate = 22050, channels = 1 })) +assert(back.frames == tone.frames) +``` + +Read only: Nintendo BRSTM / BCSTM / BFSTM (DSP ADPCM or PCM; `F.detect` names the flavour), KVAG, and the ADPCM +inside WAV (IMA, MS), AIFF-C and CAF (ima4). Decoding is bit-exact with vgmstream (ADX, VAG, BRSTM) and ffmpeg +(IMA, MS, ima4, QOA). Seeking is exact everywhere: the decoders keep the predictor state of every block they pass. + ## Gotchas - `F.ops.audio.downmix(a)` defaults to **2** channels, so a stereo input stays stereo. Use `downmix(a, 1)` for mono. diff --git a/tools/bench.luau b/tools/bench.luau index b32ee01..a3966dc 100644 --- a/tools/bench.luau +++ b/tools/bench.luau @@ -1078,3 +1078,58 @@ do jitTune(false) end end +-- ws-audio2: adpcm / qoa +do + -- codecs/audio/Adpcm (IMA nibble stream, Nintendo DSP frames) and QOA decode / encode, over generated signals; MB/s + -- of f32 output (MB/s of input for the encoder) + local Adpcm = require("@formats/codecs/audio/Adpcm") + local QoaCodec = require("@formats/codecs/audio/Qoa") + local Qoa = require("@formats/audio/qoa") + local n = 44100 * 5 + local sig = Types.Audio.new(44100, 2, n) + for i = 0, n - 1 do + local t = i / 44100 + buffer.writef32(sig.samples, i * 8, 0.5 * math.sin(2 * math.pi * 440 * t) + 0.1 * math.sin(2 * math.pi * 3000 * t)) + buffer.writef32(sig.samples, i * 8 + 4, 0.4 * math.sin(2 * math.pi * 660 * t + 1)) + end + -- IMA: encode the left channel as a nibble stream, then decode it + local ima = buffer.create(n // 2) + Adpcm.imaEncode(sig.samples, 0, 2, n, buffer.create(8), 0, ima, 0, 0, 1, false) + local out = buffer.create(n * 4) + bench("adpcm ima decode", 5, n * 4, function() + Adpcm.ima(ima, 0, 0, 1, n, false, buffer.create(8), 0, out, 0, 1) + end) + -- DSP: random frames with 8 coefficient pairs + local frames = n // 14 + local dsp = buffer.create(frames * 8) + local seed = 1 + for i = 0, frames * 8 - 1 do + seed = (seed * 1103515245 + 12345) % 2147483648 + local v = seed // 65536 % 256 + buffer.writeu8(dsp, i, if i % 8 == 0 then (v % 8) * 16 + v % 12 else v) + end + local coefs = buffer.create(64) + for k = 0, 7 do + buffer.writei32(coefs, k * 8, 1800 + k * 200) + buffer.writei32(coefs, k * 8 + 4, -900 - k * 100) + end + bench("adpcm dsp decode", 5, frames * 14 * 4, function() + Adpcm.dsp(dsp, 0, frames * 14, coefs, 0, buffer.create(8), 0, out, 0, 1) + end) + -- QOA (stereo 44.1 kHz) + local file = assert(Qoa.encode(sig)) + bench("qoa encode", 2, n * 2 * 4, function() + Qoa.encode(sig) + end) + local pcm = buffer.create(QoaCodec.FRAME_LEN * 2 * 4) + bench("qoa decode", 5, n * 2 * 4, function() + local p = 8 + while p < buffer.len(file) do + local _, size = QoaCodec.decodeFrame(file, p, buffer.len(file) - p, pcm, 0) + p += size :: number + end + end) + bench("qoa decode (F.load path)", 5, n * 2 * 4, function() + Qoa.decode(Bytes.reader(file)) + end) +end