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

+16 -36
View File
@@ -1,44 +1,32 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Resample: audio sample-rate conversion by band-limited (Kaiser-windowed sinc) interpolation, streaming or
-- whole-buffer, any channel count, interleaved f32 (the Types.Audio / Decoder layout). Used by the Opus, MP3 and MP2
-- encoders (their input rates are fixed) and ops/AudioOps.resample. New in rosetta (the textbook polyphase
-- filter; the kernel is the one the Opus encoder carried).
-- codecs/audio/Resample: sample-rate conversion by band-limited (Kaiser-windowed sinc) polyphase interpolation.
-- New in rosetta.
--
-- The filter is a polyphase table: exact phases when to / gcd(from, to) <= 4096 (44100 <-> 48000: 160 phases),
-- else 4096 phases with linear interpolation between them. Each phase is normalised to unit DC gain. The cutoff is a
-- fraction (opts.cutoff, default 0.97) of the lower Nyquist frequency; quality picks the taps per side at the cutoff:
-- fraction (default 0.97) of the lower Nyquist frequency.
--
-- quality zero crossings per side Kaiser beta stop band
-- "low" 8 6 ~ -60 dB
-- "medium" 16 (default) 8 ~ -80 dB
-- "high" 32 10 ~ -100 dB
--
-- Output frame j sits at input time j * from / to (no delay: the filter is centred, the input is zero outside its
-- range); a stream of n input frames gives floor(n * to / from + 0.5) output frames.
--
-- local out, frames = Resample.buffer(samples, frames, channels, 44100, 48000, { quality = "high" })
-- local rs = Resample.new(44100, 48000, 2) -- nil, message for bad rates
-- local b, n = rs:process(chunk, chunkFrames) -- the output frames this input completes
-- local b2, n2 = rs:flush() -- the rest (the input end padded with zeros)
-- local src2 = Resample.source(pcmSource, 48000) -- a codecs/audio/Pcm Source at the new rate
--
-- Never throws: bad arguments return nil, message (the api turns them into Err.ARG).
-- Output frame j sits at input time j * from / to (centred filter, zero outside the input); n input frames give
-- floor(n * to / from + 0.5) output frames.
local Resample = {}
export type Options = {
quality: ("low" | "medium" | "high")?, -- default "medium"
cutoff: number?, -- pass band edge as a fraction of the lower Nyquist frequency, 0.5 .. 1 (default 0.97)
quality: ("low" | "medium" | "high")?,
cutoff: number?,
}
export type Resampler = {
from: number,
to: number,
channels: number,
-- process(src, frames, out?) -> interleaved f32 buffer, output frames (out is reused when big enough)
process: (self: Resampler, src: buffer, frames: number, out: buffer?) -> (buffer, number),
-- flush(out?) -> the remaining output frames; the resampler is finished afterwards
flush: (self: Resampler, out: buffer?) -> (buffer, number),
}
@@ -69,15 +57,14 @@ local function gcd(a: number, b: number): number
end
type Filter = {
taps: number, -- per phase
half: number, -- taps on the left of the centre (taps = 2 * half)
taps: number,
half: number,
phases: number,
exact: boolean,
h: { number }, -- phase p, tap k: h[p * taps + k + 1]
}
-- filter(ratio, phases, exact, opts) -> the polyphase table. Phase p is the kernel for an output at fraction
-- p / phases past an input sample; tap k multiplies input (floor(t) - half + 1 + k).
-- phase p: an output p / phases past an input sample; tap k multiplies input floor(t) - half + 1 + k
local function makeFilter(ratio: number, phases: number, exact: boolean, zc: number, beta: number, cut: number): Filter
local cutoff = cut * min(1, ratio)
local width = zc / cutoff -- in input samples, each side
@@ -111,7 +98,6 @@ local function makeFilter(ratio: number, phases: number, exact: boolean, zc: num
return { taps = taps, half = half, phases = phases, exact = exact, h = h }
end
-- the stream state: the input history as an interleaved Lua array; hist[1] is frame `base` (absolute)
type State = {
from: number,
to: number,
@@ -121,15 +107,14 @@ type State = {
L: number, -- to / g
hist: { number },
histFrames: number,
base: number, -- the absolute input frame of hist[1]
inFrames: number, -- absolute input frames received
outPos: number, -- next output frame (absolute)
base: number,
inFrames: number,
outPos: number,
done: boolean,
process: any,
flush: any,
}
-- run(st, lastInput) -> buffer, frames: every output frame whose taps end at or before lastInput (absolute, exclusive)
local function run(st: State, limitOut: number, out: buffer?): (buffer, number)
local f = st.f
local taps, half, h = f.taps, f.half, f.h
@@ -159,8 +144,8 @@ local function run(st: State, limitOut: number, out: buffer?): (buffer, number)
for j = j0, jmax - 1 do
local num = j * M
local ip = num // L -- floor(t)
local first = ip - half + 1 -- absolute input frame of tap 0
local rel = first - base -- frame index in hist
local first = ip - half + 1
local rel = first - base
local kLo = if rel < 0 then -rel else 0
local kHi = taps - 1
if rel + kHi >= histFrames then
@@ -199,7 +184,6 @@ local function run(st: State, limitOut: number, out: buffer?): (buffer, number)
end
end
st.outPos = jmax
-- drop history no future output needs (keep from the next output's first tap)
local keepFrom = (jmax * M) // L - half + 1
local drop = keepFrom - base
if drop > 0 then
@@ -243,7 +227,6 @@ local function flush(self: State, out: buffer?): (buffer, number)
return run(self, total, out)
end
-- new(from, to, channels, opts?) -> Resampler | nil, message
function Resample.new(from: number, to: number, channels: number, opts: Options?): (Resampler?, string?)
if type(from) ~= "number" or type(to) ~= "number" or from ~= from or to ~= to or from <= 0 or to <= 0 or from > 2 ^ 24 or to > 2 ^ 24 then
return nil, "bad sample rates"
@@ -287,7 +270,6 @@ function Resample.new(from: number, to: number, channels: number, opts: Options?
return st :: any, nil
end
-- buffer(src, frames, channels, from, to, opts?) -> interleaved f32 buffer, frames | nil, message
function Resample.buffer(src: buffer, frames: number, channels: number, from: number, to: number, opts: Options?): (buffer?, number | string)
local rs, err = Resample.new(from, to, channels, opts)
if not rs then
@@ -322,8 +304,6 @@ function Resample.buffer(src: buffer, frames: number, channels: number, from: nu
return out, got
end
-- source(src, to, opts?) -> a pull source ({ sampleRate, channels, frames, tags, read, err }, the codecs/audio/Pcm Source
-- shape) reading src (any such source: Pcm.source(audioOrDecoder)) at the rate `to` | nil, message
function Resample.source(src: any, to: number, opts: Options?): (any, string?)
if type(src) ~= "table" or type(src.read) ~= "function" then
return nil, "needs a source"
@@ -333,7 +313,7 @@ function Resample.source(src: any, to: number, opts: Options?): (any, string?)
return nil, err
end
local ch = src.channels
local pending = buffer.create(0) -- produced but not yet returned
local pending = buffer.create(0)
local pendingFrames, pendingPos = 0, 0
local ended = false
local out = {