From 5e6102d4b2638168e342712005d155a7fb63c2b4 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sun, 1 Nov 2026 01:53:22 -0500 Subject: [PATCH] comments: ops AudioOps / ImageOps Kernels --- src/Rosetta/ops/AudioOps.luau | 39 ++------------------------- src/Rosetta/ops/ImageOps/Kernels.luau | 22 ++++----------- 2 files changed, 7 insertions(+), 54 deletions(-) diff --git a/src/Rosetta/ops/AudioOps.luau b/src/Rosetta/ops/AudioOps.luau index 8d10b59..35f5682 100644 --- a/src/Rosetta/ops/AudioOps.luau +++ b/src/Rosetta/ops/AudioOps.luau @@ -1,18 +1,6 @@ --!strict --- ops/AudioOps: editing on the standard Audio (layer 5.5: core, Types and codecs only): a Decoder collected into an --- Audio, gain (linear or dB), normalize, mix, channel remixing (downmix / upmix with the usual WAVE-layout matrices), --- resampling (codecs/audio/Resample), trim, concat, fades, peak / RMS. New code. --- --- local AudioOps = require("@rosetta/ops/AudioOps") -- F.ops.audio in the api --- local a = AudioOps.toAudio(F.audio.open(src)) -- an Audio from an Audio or a streaming Decoder --- local louder = AudioOps.gainDb(a, 6) -- every call returns a new Audio | nil, Err (never throws) --- local mono = AudioOps.downmix(a) -- or AudioOps.remix(a, 1); upmix(a, 6) --- local a48 = AudioOps.resample(a, 48000, { quality = "high" }) --- local mixed = AudioOps.mix({ a, b }, { 0.5, 0.5 }) -- other rates / channel counts are converted to a's --- local clip = AudioOps.trim(a, 1.5, 4) -- seconds [1.5, 4) --- local both = AudioOps.concat({ a, b }) --- local peak, perChannel = AudioOps.peak(a) -- max |sample|; AudioOps.rms(a) the same shape --- +-- ops/AudioOps: editing on the standard Audio: collect, gain, normalize, mix, remix, resample (codecs/audio/Resample), +-- trim, concat, fades, peak / RMS. New code. -- Conventions: samples are the Audio's interleaved f32 (no clipping is applied: values past +-1 are kept, encoders -- clip). Channels are in WAVE order (FL FR C LFE BL BR SL SR, as every decoder returns them). Downmixes follow the -- ITU-R BS.775 coefficients (centre and surrounds at -3 dB, LFE dropped), scaled so a full-scale input cannot @@ -28,7 +16,6 @@ local AudioOps = {} local readf32, writef32 = buffer.readf32, buffer.writef32 local floor, max, min, abs, sqrt = math.floor, math.max, math.min, math.abs, math.sqrt --- ------------------------------------------------------------------------------------------------ validation local function isAudio(a: any): boolean return type(a) == "table" and a.kind == "audio" @@ -45,7 +32,6 @@ local function bad(what: string): (nil, Err.Err) return nil, Err.new(Err.ARG, what) end --- a new Audio with a's rate / tags / meta and the given layout local function like(a: Types.Audio, channels: number, frames: number, rate: number?): Types.Audio local out = Types.Audio.new(rate or a.sampleRate, channels, frames) out.tags = table.clone(a.tags or {}) @@ -53,9 +39,6 @@ local function like(a: Types.Audio, channels: number, frames: number, rate: numb return out end --- ------------------------------------------------------------------------------------------------ conversion --- toAudio(audioOrDecoder, opts?) -> Audio: an Audio as it is, a Decoder read to its end (opts.maxFrames: Err.LIMIT --- past it; default 2^31) function AudioOps.toAudio(v: any, opts: { maxFrames: number? }?): (Types.Audio?, Err.Err?) if isAudio(v) then return v, nil @@ -68,8 +51,6 @@ function AudioOps.toAudio(v: any, opts: { maxFrames: number? }?): (Types.Audio?, end AudioOps.fromDecoder = AudioOps.toAudio --- ------------------------------------------------------------------------------------------------ levels --- gain(a, factor) -> Audio with every sample times factor function AudioOps.gain(a: Types.Audio, factor: number): (Types.Audio?, Err.Err?) if not isAudio(a) then return bad("gain: expected an Audio") @@ -86,7 +67,6 @@ function AudioOps.gain(a: Types.Audio, factor: number): (Types.Audio?, Err.Err?) return out, nil end --- gainDb(a, dB) -> Audio scaled by 10^(dB / 20) function AudioOps.gainDb(a: Types.Audio, db: number): (Types.Audio?, Err.Err?) if type(db) ~= "number" or db ~= db then return bad("gainDb: dB must be a number") @@ -94,7 +74,6 @@ function AudioOps.gainDb(a: Types.Audio, db: number): (Types.Audio?, Err.Err?) return AudioOps.gain(a, 10 ^ (db / 20)) end --- peak(a) -> the largest |sample|, { per channel } function AudioOps.peak(a: Types.Audio): (number?, { number } | Err.Err) if not isAudio(a) then return nil, Err.new(Err.ARG, "peak: expected an Audio") @@ -118,7 +97,6 @@ function AudioOps.peak(a: Types.Audio): (number?, { number } | Err.Err) return m, per end --- rms(a) -> the root mean square over every sample, { per channel } (0 for no frames) function AudioOps.rms(a: Types.Audio): (number?, { number } | Err.Err) if not isAudio(a) then return nil, Err.new(Err.ARG, "rms: expected an Audio") @@ -142,7 +120,6 @@ function AudioOps.rms(a: Types.Audio): (number?, { number } | Err.Err) return sqrt(total / (n * ch)), per end --- normalize(a, target?) -> Audio scaled so its peak is target (default 1; silence is returned unchanged) function AudioOps.normalize(a: Types.Audio, target: number?): (Types.Audio?, Err.Err?) local p, perr = AudioOps.peak(a) if not p then @@ -155,7 +132,6 @@ function AudioOps.normalize(a: Types.Audio, target: number?): (Types.Audio?, Err return AudioOps.gain(a, if p > 0 then t / p else 1) end --- fade(a, inSeconds?, outSeconds?) -> Audio with linear fade-in / fade-out ramps function AudioOps.fade(a: Types.Audio, inSec: number?, outSec: number?): (Types.Audio?, Err.Err?) if not isAudio(a) then return bad("fade: expected an Audio") @@ -186,7 +162,6 @@ function AudioOps.fade(a: Types.Audio, inSec: number?, outSec: number?): (Types. return out, nil end --- ------------------------------------------------------------------------------------------------ channels -- the WAVE-order speakers of 1 .. 8 channel layouts (as FFmpeg's default layouts) local LAYOUT: { { string } } = { { "C" }, @@ -296,7 +271,6 @@ local function matrix(from: number, to: number): { { number } } end AudioOps.matrix = matrix --- remix(a, channels, m?) -> Audio with that many channels (m: an explicit m[out][in] matrix; default matrix(...)) function AudioOps.remix(a: Types.Audio, channels: number, m: { { number } }?): (Types.Audio?, Err.Err?) if not isAudio(a) then return bad("remix: expected an Audio") @@ -338,7 +312,6 @@ function AudioOps.remix(a: Types.Audio, channels: number, m: { { number } }?): ( return out, nil end --- downmix(a, channels?) -> Audio with fewer channels (default 2; mono input stays mono) function AudioOps.downmix(a: Types.Audio, channels: number?): (Types.Audio?, Err.Err?) if not isAudio(a) then return bad("downmix: expected an Audio") @@ -350,7 +323,6 @@ function AudioOps.downmix(a: Types.Audio, channels: number?): (Types.Audio?, Err return AudioOps.remix(a, n) end --- upmix(a, channels) -> Audio with more channels function AudioOps.upmix(a: Types.Audio, channels: number): (Types.Audio?, Err.Err?) if not isAudio(a) then return bad("upmix: expected an Audio") @@ -361,8 +333,6 @@ function AudioOps.upmix(a: Types.Audio, channels: number): (Types.Audio?, Err.Er return AudioOps.remix(a, channels) end --- ------------------------------------------------------------------------------------------------ time --- resample(a, rate, opts?) -> Audio at the new rate (codecs/audio/Resample: opts.quality "low" | "medium" | "high") function AudioOps.resample(a: Types.Audio, rate: number, opts: Resample.Options?): (Types.Audio?, Err.Err?) if not isAudio(a) then return bad("resample: expected an Audio") @@ -377,7 +347,6 @@ function AudioOps.resample(a: Types.Audio, rate: number, opts: Resample.Options? return out, nil end --- trim(a, start, stop?) -> Audio of the seconds [start, stop) (clamped to the audio; stop default the end) function AudioOps.trim(a: Types.Audio, start: number, stop: number?): (Types.Audio?, Err.Err?) if not isAudio(a) then return bad("trim: expected an Audio") @@ -393,7 +362,6 @@ function AudioOps.trim(a: Types.Audio, start: number, stop: number?): (Types.Aud return out, nil end --- conform(list, what) -> the audios converted to the first one's rate and channel count | nil, Err local function conform(list: { Types.Audio }, what: string): ({ Types.Audio }?, Err.Err?) if type(list) ~= "table" or #list == 0 then return nil, Err.new(Err.ARG, what .. ": expected a non-empty list of Audio") @@ -427,7 +395,6 @@ local function conform(list: { Types.Audio }, what: string): ({ Types.Audio }?, return out, nil end --- concat(list) -> Audio: the audios one after the other (at the first one's rate and channel count) function AudioOps.concat(list: { Types.Audio }): (Types.Audio?, Err.Err?) local items, err = conform(list, "concat") if not items then @@ -448,8 +415,6 @@ function AudioOps.concat(list: { Types.Audio }): (Types.Audio?, Err.Err?) return out, nil end --- mix(list, weights?) -> Audio: the sum of the audios (times weights[i], default 1 each), as long as the longest, at --- the first one's rate and channel count function AudioOps.mix(list: { Types.Audio }, weights: { number }?): (Types.Audio?, Err.Err?) local items, err = conform(list, "mix") if not items then diff --git a/src/Rosetta/ops/ImageOps/Kernels.luau b/src/Rosetta/ops/ImageOps/Kernels.luau index c55ae9e..175da10 100644 --- a/src/Rosetta/ops/ImageOps/Kernels.luau +++ b/src/Rosetta/ops/ImageOps/Kernels.luau @@ -1,12 +1,9 @@ --!strict --!native --!optimize 2 --- ops/ImageOps/Kernels: the hot loops behind ops/ImageOps (native; never throws). Every function trusts its caller --- (ops/ImageOps validates sizes, formats and buffer lengths first), so all buffer indexes are in range by --- construction. Samples are addressed by kind: 1 = u8, 2 = u16, 4 = f32 (little endian, as Types.Image buffers). --- The resampling follows Pillow's Resample.c (separable convolution, horizontal pass first, filter support scaled by --- the downscale ratio, 22-bit fixed-point coefficients for 8-bit samples) and Geometry.c (affine transforms through --- pixel centres, bilinear with clamped neighbours); the alpha compositing is Pillow's AlphaComposite.c. +-- ops/ImageOps/Kernels: the hot loops behind ops/ImageOps; callers validate, so indexes are in range by construction. +-- Sample kinds: 1 = u8, 2 = u16, 4 = f32 (little endian). Resampling follows Pillow's Resample.c (separable, 22-bit +-- fixed point for 8-bit) and Geometry.c (affine through pixel centres); compositing is Pillow's AlphaComposite.c. local readu8, writeu8, readu16, writeu16, readf32, writef32 = buffer.readu8, buffer.writeu8, buffer.readu16, buffer.writeu16, buffer.readf32, buffer.writef32 local floor, clamp = math.floor, math.clamp @@ -36,7 +33,6 @@ local function wr(b: buffer, o: number, kind: number, v: number) end end --- ------------------------------------------------------------------------------------------------ convert -- depth: 8 -> 0..255, 16 -> 0..65535, 0 -> float 0..1 local DEPTH = { 8, 16, 0, 8, 8, 16, 8, 8 } @@ -55,7 +51,6 @@ local function depthConv(v: number, from: number, to: number): number end return v / 65535 end - -- float if v ~= v then v = 0 end @@ -201,8 +196,6 @@ function K.yuvToRgba(src: buffer, w: number, h: number, dst: buffer) local _ = ch end --- ------------------------------------------------------------------------------------------------ geometry --- flip(src, dst, w, h, bpp, horizontal) function K.flip(src: buffer, dst: buffer, w: number, h: number, bpp: number, horizontal: boolean) local stride = w * bpp if not horizontal then @@ -219,7 +212,7 @@ function K.flip(src: buffer, dst: buffer, w: number, h: number, bpp: number, hor end end --- rotate90(src, dst, w, h, bpp, turns): counter-clockwise quarter turns 1..3 +-- counter-clockwise quarter turns 1..3 function K.rotate90(src: buffer, dst: buffer, w: number, h: number, bpp: number, turns: number) for y = 0, h - 1 do for x = 0, w - 1 do @@ -241,7 +234,6 @@ function K.rotate90(src: buffer, dst: buffer, w: number, h: number, bpp: number, end end --- ------------------------------------------------------------------------------------------------ resample local function bilinearF(x: number): number if x < 0 then x = -x @@ -303,7 +295,6 @@ function K.coeffs(inSize: number, outSize: number, filter: string): Coeffs return { start = start, count = count, weights = weights } end --- one separable pass. horizontal: src w x h -> dst outN x h; vertical: src w x h -> dst w x outN. ch channels of kind. function K.resamplePass(src: buffer, w: number, h: number, ch: number, kind: number, dst: buffer, outN: number, c: Coeffs, horizontal: boolean) local start, count, weights = c.start, c.count, c.weights local size = kind @@ -421,8 +412,7 @@ function K.affine(src: buffer, w: number, h: number, ch: number, kind: number, d end end --- ------------------------------------------------------------------------------------------------ compositing --- overRgba8(dst, dw, src, sw, sx, sy, dx, dy, w, h, opacity255): Pillow's alpha_composite of src onto dst, in place +-- Pillow's alpha_composite of src onto dst, in place function K.overRgba8(dst: buffer, dw: number, src: buffer, sw: number, sx: number, sy: number, dx: number, dy: number, w: number, h: number, opacity: number) for y = 0, h - 1 do for x = 0, w - 1 do @@ -475,14 +465,12 @@ function K.overFloat(dst: buffer, dw: number, src: buffer, sw: number, sx: numbe end end --- copyRect: rows of a rectangle (bpp bytes per pixel) function K.copyRect(dst: buffer, dw: number, src: buffer, sw: number, sx: number, sy: number, dx: number, dy: number, w: number, h: number, bpp: number) for y = 0, h - 1 do buffer.copy(dst, ((dy + y) * dw + dx) * bpp, src, ((sy + y) * sw + sx) * bpp, w * bpp) end end --- premultiply / unpremultiply in place: ch channels, alpha last, kind function K.premultiply(b: buffer, n: number, ch: number, kind: number, un: boolean) local max = if kind == 1 then 255 elseif kind == 2 then 65535 else 1 local px = ch * kind