codecs/audio: comments to specs only

This commit is contained in:
4DBug committed 2026-11-01 01:55:39 -05:00
1 parent 0bd7b87d2b
commit 6dcce481bb
69 files changed
+523 -1604

No files matched your search

+12 -31
View File
@@ -1,18 +1,7 @@
--!strict
-- codecs/audio/Alac: Apple Lossless (ALAC) decoder and encoder, the codec inside m4a / mov / caf. A codec (layer 1).
-- Written from the ALAC format as Apple published it (ALACSpecificConfig, the element syntax, adaptive Golomb-Rice
-- residuals, the adaptive FIR predictor); no third-party code. 1 - 8 channels, 16 / 20 / 24 / 32-bit decode, 16 / 20 /
-- 24-bit encode. Channels come out (and go in) in WAV order (ALAC stores C L R ... for 3+ channels).
--
-- local Alac = require("@rosetta/codecs/audio/Alac")
-- local cfg, err = Alac.config(cookie) -- the 24-byte ALACSpecificConfig (frma / alac atoms skipped)
-- local dec = Alac.decoder(cfg)
-- local frames, err = dec:decode(buf, offset, length) -- one packet -> dec.pcm (interleaved integers, WAV order)
-- local enc = Alac.encoder({ sampleRate = 44100, channels = 2, bitDepth = 16 })
-- local packet = enc:encode(samples, first, frames) -- interleaved integers -> one packet (frames <= 4096)
-- local cookie = Alac.cookie(enc.config) -- after encoding: maxFrameBytes / avgBitRate filled
--
-- Never throws: bad data returns nil, Err (CORRUPT / TRUNCATED / UNSUPPORTED), bad arguments Err.ARG.
-- codecs/audio/Alac: Apple Lossless (ALAC) decoder and encoder.
-- Written from the ALAC format as Apple published it; no third-party code. ALAC stores C L R ... for 3+ channels; the
-- API uses WAV order.
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
local Decode = require("@rosetta/codecs/audio/Alac/Decode")
@@ -21,20 +10,19 @@ local Encode = require("@rosetta/codecs/audio/Alac/Encode")
local Alac = {}
export type Config = {
frameLength: number, -- samples per packet (4096)
frameLength: number,
compatibleVersion: number,
bitDepth: number,
pb: number, -- 40
mb: number, -- 10
kb: number, -- 14
pb: number,
mb: number,
kb: number,
channels: number,
maxRun: number, -- 255
maxRun: number,
maxFrameBytes: number,
avgBitRate: number,
sampleRate: number,
}
-- ALAC channel k (1-based) -> WAV channel (1-based), per channel count
local TO_WAV: { { number } } = {
{ 1 },
{ 1, 2 },
@@ -45,7 +33,6 @@ local TO_WAV: { { number } } = {
{ 3, 1, 2, 5, 6, 7, 4 },
{ 3, 7, 8, 1, 2, 5, 6, 4 },
}
-- the element layout (channels per element) the encoder writes
local ELEMENTS: { { number } } = {
{ 1 },
{ 2 },
@@ -58,7 +45,6 @@ local ELEMENTS: { { number } } = {
}
Alac.TO_WAV = TO_WAV
-- config(buf, offset?, length?) -> Config | nil, Err
function Alac.config(buf: buffer, offset: number?, length: number?): (Config?, Err.Err?)
local r = Bytes.reader(buf, offset, length)
-- a QuickTime 'wave' payload may hold frma + alac atoms ahead of the cookie
@@ -104,7 +90,6 @@ function Alac.config(buf: buffer, offset: number?, length: number?): (Config?, E
return c, nil
end
-- cookie(cfg) -> the 24-byte ALACSpecificConfig
function Alac.cookie(c: Config): buffer
local w = Bytes.writer(24)
w:u32be(c.frameLength)
@@ -121,10 +106,9 @@ function Alac.cookie(c: Config): buffer
return w:finish()
end
-- ------------------------------------------------------------------------------------------------ decoder
export type Decoder = {
config: Config,
pcm: { number }, -- the last packet's samples, interleaved, WAV order (integers of config.bitDepth bits)
pcm: { number },
decode: (self: Decoder, buf: buffer, offset: number?, length: number?) -> (number?, Err.Err?),
state: Decode.State,
}
@@ -157,7 +141,6 @@ local function decode(self: Decoder, buf: buffer, offset: number?, length: numbe
return frames, nil
end
-- decoder(cfg) -> Decoder
function Alac.decoder(cfg: Config): Decoder
return {
config = cfg,
@@ -167,7 +150,6 @@ function Alac.decoder(cfg: Config): Decoder
}
end
-- ------------------------------------------------------------------------------------------------ encoder
export type Encoder = {
config: Config,
encode: (self: Encoder, samples: { number }, first: number, frames: number) -> (buffer?, Err.Err?),
@@ -200,12 +182,11 @@ end
export type EncoderOptions = {
sampleRate: number,
channels: number, -- 1 .. 8
bitDepth: number?, -- 16 (default), 20 or 24
frameLength: number?, -- default 4096
channels: number,
bitDepth: number?,
frameLength: number?,
}
-- encoder(opts) -> Encoder | nil, Err
function Alac.encoder(opts: EncoderOptions): (Encoder?, Err.Err?)
if type(opts) ~= "table" then
return nil, Err.new(Err.ARG, "encoder: expected options")