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]>
162 lines
5.7 KiB
Luau
162 lines
5.7 KiB
Luau
--!strict
|
|
-- audio/au: Sun / NeXT audio (.au / .snd), big endian. New code from the Sun audio file format description.
|
|
-- Supports: decode / open of encodings 1 (G.711 mu-law), 2 (8-bit), 3 (16-bit), 4 (24-bit), 5 (32-bit linear),
|
|
-- 6 (32-bit float), 7 (64-bit float), 27 (G.711 A-law); an unknown data size (0xFFFFFFFF: to the end of the file);
|
|
-- the annotation as tags.comment. Encode (Audio or Decoder, streamed) to any of those encodings (default 16-bit).
|
|
-- Not supported: G.721 / G.722 / G.723 ADPCM and the other NeXT encodings (Err.UNSUPPORTED).
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Types = require("@formats/Types")
|
|
local Pcm = require("@formats/codecs/audio/Pcm")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Au = {
|
|
name = "au",
|
|
category = "audio",
|
|
extensions = { "au", "snd" },
|
|
mime = { "audio/basic", "audio/x-au" },
|
|
deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Pcm" },
|
|
caps = { decode = true, encode = true, streaming = true, depths = { 8, 16, 24, 32 }, float = true, lossy = false },
|
|
}
|
|
|
|
export type DecodeOptions = {
|
|
maxFrames: number?, -- refuse more sample frames (default 2^28)
|
|
}
|
|
export type EncodeOptions = {
|
|
sampleFormat: ("s8" | "s16" | "s24" | "s32" | "f32" | "f64" | "ulaw" | "alaw")?, -- default "s16"
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ constants
|
|
local ENCODINGS: { [number]: string } = {
|
|
[1] = "ulaw",
|
|
[2] = "s8",
|
|
[3] = "s16be",
|
|
[4] = "s24be",
|
|
[5] = "s32be",
|
|
[6] = "f32be",
|
|
[7] = "f64be",
|
|
[27] = "alaw",
|
|
}
|
|
local WRITE = {
|
|
ulaw = { 1, "ulaw" },
|
|
s8 = { 2, "s8" },
|
|
s16 = { 3, "s16be" },
|
|
s24 = { 4, "s24be" },
|
|
s32 = { 5, "s32be" },
|
|
f32 = { 6, "f32be" },
|
|
f64 = { 7, "f64be" },
|
|
alaw = { 27, "alaw" },
|
|
}
|
|
local BITS = { ulaw = 8, alaw = 8, s8 = 8, s16be = 16, s24be = 24, s32be = 32, f32be = 32, f64be = 64 }
|
|
|
|
local function be32(b: buffer, o: number): number
|
|
return bit32.byteswap(buffer.readu32(b, o))
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Au.sniff(r: Bytes.Reader): number
|
|
return if r:peek(4) == ".snd" then 1 else 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
function Au.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Audio?, Err.Err?)
|
|
local dec, err = Au.open(r, opts)
|
|
if not dec then
|
|
return nil, err
|
|
end
|
|
return Pcm.drain(dec)
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
function Au.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
|
local src, serr = Pcm.source(value)
|
|
if not src then
|
|
return nil, serr
|
|
end
|
|
local o: EncodeOptions = opts or {}
|
|
local spec = WRITE[o.sampleFormat or "s16"]
|
|
if not spec then
|
|
return nil, Err.new(Err.ARG, "unknown sampleFormat " .. tostring(o.sampleFormat))
|
|
end
|
|
if src.sampleRate ~= math.floor(src.sampleRate) or src.sampleRate >= 2 ^ 32 then
|
|
return nil, Err.new(Err.ARG, "the sample rate must be an integer")
|
|
end
|
|
-- annotation: the comment tag, NUL-terminated, padded to a multiple of 8 (at least 4 bytes, as Sun requires)
|
|
local note = if type(src.tags.comment) == "string" then src.tags.comment else ""
|
|
local alen = #note + 1
|
|
alen = math.max(8, alen + (8 - alen % 8) % 8)
|
|
local w = Bytes.writer(65536)
|
|
w:string(".snd")
|
|
w:u32be(24 + alen)
|
|
w:u32be(0xFFFFFFFF) -- patched
|
|
w:u32be(spec[1])
|
|
w:u32be(src.sampleRate)
|
|
w:u32be(src.channels)
|
|
w:string(note)
|
|
w:fill(0, alen - #note)
|
|
local frames, perr = Pcm.encode(src, spec[2], w)
|
|
if not frames then
|
|
return nil, perr
|
|
end
|
|
w:setU32be(8, w:tell() - 24 - alen)
|
|
return w:finish(), nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ open
|
|
function Au.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.Err?)
|
|
local b, base, limit = r.buf, r.base, r.limit
|
|
if limit - base < 24 or buffer.readstring(b, base, 4) ~= ".snd" then
|
|
return nil, Err.new(Err.CORRUPT, "bad magic", base)
|
|
end
|
|
local off, size, enc, rate, ch = be32(b, base + 4), be32(b, base + 8), be32(b, base + 12), be32(b, base + 16), be32(b, base + 20)
|
|
local fmt = ENCODINGS[enc]
|
|
if not fmt then
|
|
return nil, Err.new(Err.UNSUPPORTED, "encoding " .. enc, base + 12)
|
|
end
|
|
if ch < 1 or ch > 65535 then
|
|
return nil, Err.new(Err.CORRUPT, "bad channel count " .. ch, base + 20)
|
|
end
|
|
if rate < 1 then
|
|
return nil, Err.new(Err.CORRUPT, "bad sample rate", base + 16)
|
|
end
|
|
if off < 24 or off > limit - base then
|
|
return nil, Err.new(Err.CORRUPT, "bad data offset", base + 4)
|
|
end
|
|
local tags: Types.Tags = {}
|
|
if off > 24 then
|
|
local s = buffer.readstring(b, base + 24, off - 24)
|
|
local z = string.find(s, "\0", 1, true)
|
|
if z then
|
|
s = string.sub(s, 1, z - 1)
|
|
end
|
|
if #s > 0 then
|
|
tags.comment = s
|
|
end
|
|
end
|
|
local avail = limit - base - off
|
|
local truncated = false
|
|
if size == 0xFFFFFFFF then
|
|
size = avail
|
|
elseif size > avail then
|
|
size = avail
|
|
truncated = true
|
|
end
|
|
local frameBytes = ch * Pcm.Convert.SIZE[fmt]
|
|
local frames = size // frameBytes
|
|
local maxFrames = opts and opts.maxFrames or 2 ^ 28
|
|
if frames > maxFrames then
|
|
return nil, Err.new(Err.LIMIT, string.format("%d frames is over the %d frame limit", frames, maxFrames))
|
|
end
|
|
local dec, derr = Pcm.rawDecoder(b, base + off, frames, ch, rate, fmt, tags)
|
|
if not dec then
|
|
return nil, derr
|
|
end
|
|
local d = dec :: any
|
|
d.source = { format = "au", encoding = enc, sampleFormat = fmt, bitDepth = BITS[fmt], truncated = truncated or nil }
|
|
return dec, nil
|
|
end
|
|
|
|
return Au
|