Files
rosetta/src/Formats/codecs/audio/Tracker/Pcm.luau
T
4DBugandClaude Opus 5.5 434a304315 ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
C2, the hierarchy (tools/move.py, every reference rewritten mechanically):
- codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder;
  codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample
  VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url;
  codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err
  Crc32 Adler32 Base64 Md5 Input Sniff.
- LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md).
- Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes.

C3, generic code out of format folders:
- containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first,
  trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64
  through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it.
- codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position,
  priming and end trimming) that aac, caf and m4a each carried (~70 lines each).
- codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of
  its own (the AND mask / alpha rule stay ICO's).
- codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model),
  Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is
  Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the
  fixtures' bind shapes).
- README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes:
  FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the
  shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks
  decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type
  baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-31 12:25:03 -05:00

133 lines
3.8 KiB
Luau

--!strict
--!native
-- codecs/audio/Tracker/Pcm: sample data conversions shared by the tracker formats. Raw 8 / 16-bit PCM (signed or unsigned,
-- plain or delta coded as in XM / IT 2.15, interleaved or planar stereo) to the engine's f32 samples, and back.
--
-- Never throws: reads past the source are refused up front (decode returns nil).
local readu8, readi8, readu16, readi16 = buffer.readu8, buffer.readi8, buffer.readu16, buffer.readi16
local writef32, readf32, writeu8, writei16 = buffer.writef32, buffer.readf32, buffer.writeu8, buffer.writei16
local floor = math.floor
local Pcm = {}
export type Spec = {
bits: number, -- 8 | 16
signed: boolean,
delta: boolean?, -- each value is the difference to the previous one (XM, IT 2.15 "delta" flag)
channels: number?, -- 1 (default) | 2
planar: boolean?, -- stereo stored as all left then all right (XM, IT); default interleaved
bigEndian: boolean?,
}
-- bytes(frames, spec) -> the size of the raw data
function Pcm.bytes(frames: number, spec: Spec): number
return frames * (spec.channels or 1) * (spec.bits // 8)
end
-- decode(buf, off, frames, spec) -> f32 buffer (frames * channels, interleaved) | nil when the source is too short
function Pcm.decode(buf: buffer, off: number, frames: number, spec: Spec): buffer?
local nch = spec.channels or 1
local bps = spec.bits // 8
local total = frames * nch
if frames < 0 or off < 0 or off + total * bps > buffer.len(buf) then
return nil
end
local out = buffer.create(total * 4)
local planar = spec.planar and nch == 2
for c = 0, nch - 1 do
local acc = 0
for i = 0, frames - 1 do
local k = if planar then c * frames + i else i * nch + c
local v
if bps == 1 then
if spec.signed then
v = readi8(buf, off + k)
else
v = readu8(buf, off + k) - 128
end
if spec.delta then
acc = (acc + v + 128) % 256 - 128
v = acc
end
writef32(out, (i * nch + c) * 4, v / 128)
else
local p = off + k * 2
if spec.bigEndian then
v = readu8(buf, p) * 256 + readu8(buf, p + 1)
if spec.signed and v >= 32768 then
v -= 65536
end
elseif spec.signed then
v = readi16(buf, p)
else
v = readu16(buf, p)
end
if not spec.signed then
v -= 32768
end
if spec.delta then
acc = (acc + v + 32768) % 65536 - 32768
v = acc
end
writef32(out, (i * nch + c) * 4, v / 32768)
end
end
end
return out
end
-- encode(samples, frames, spec) -> raw PCM (signed or unsigned, optional delta, interleaved or planar)
function Pcm.encode(samples: buffer, frames: number, spec: Spec): buffer
local nch = spec.channels or 1
local bps = spec.bits // 8
local out = buffer.create(frames * nch * bps)
local planar = spec.planar and nch == 2
local avail = buffer.len(samples) // 4
for c = 0, nch - 1 do
local prev = 0
for i = 0, frames - 1 do
local si = i * nch + c
local x = if si < avail then readf32(samples, si * 4) else 0
local k = if planar then c * frames + i else i * nch + c
if bps == 1 then
local v = math.clamp(floor(x * 128 + 0.5), -128, 127)
local w = v
if spec.delta then
w = (v - prev) % 256
prev = v
else
w = if spec.signed then v % 256 else v + 128
end
writeu8(out, k, w)
else
local v = math.clamp(floor(x * 32768 + 0.5), -32768, 32767)
local w = v
if spec.delta then
w = (v - prev + 32768) % 65536 - 32768
prev = v
elseif not spec.signed then
w = v + 32768
if w >= 32768 then
w -= 65536
end
end
writei16(out, k * 2, w)
end
end
end
return out
end
-- bits(samples) -> 8 when every value is exactly representable in 8 bits (data loaded from 8-bit), else 16
function Pcm.bitsNeeded(samples: buffer): number
for i = 0, buffer.len(samples) // 4 - 1 do
local x = readf32(samples, i * 4) * 128
if x ~= floor(x) then
return 16
end
end
return 8
end
return Pcm