ws-audio: audio/aiff (AIFF/AIFC incl. sowt/fl32/fl64/ulaw/alaw), audio/au, audio/voc: decode/open/encode; codecs/Pcm folder (+ Convert: raw sample formats <-> f32, G.711, rawDecoder/audioDecoder/encode); fixtures vs ffmpeg/sox, encoder outputs decoded by ffmpeg in the fixture step

This commit is contained in:
4DBug committed 2026-10-31 08:38:56 -05:00
1 parent 66df4a2254
commit 315e819b01
14 files changed
+1940 -6

No files matched your search

+308
View File
@@ -0,0 +1,308 @@
--!strict
--!native
--!optimize 2
-- codecs/Pcm/Convert: raw sample formats <-> interleaved f32 (the hot loops of codecs/Pcm). Integers map to
-- v / 2^(bits-1) (as ffmpeg does); encoding rounds to nearest and clips. G.711 A-law / mu-law per ITU-T G.711 (the
-- classic Sun g711.c algorithm, written from the spec).
--
-- Formats: u8 s8 s16le s16be s24le s24be s32le s32be f32le f32be f64le f64be alaw ulaw (u8: offset binary).
-- Never throws: callers check ranges (Convert.SIZE[fmt] * count bytes) before calling.
local Convert = {}
local readu8, readi8, readi16, readi32, readu32 = buffer.readu8, buffer.readi8, buffer.readi16, buffer.readi32, buffer.readu32
local readf32, readf64, writef32, writef64 = buffer.readf32, buffer.readf64, buffer.writef32, buffer.writef64
local writeu8, writeu16, writeu32, writei16, writei32 = buffer.writeu8, buffer.writeu16, buffer.writeu32, buffer.writei16, buffer.writei32
local floor = math.floor
local byteswap = bit32.byteswap
local SIZE: { [string]: number } = {
u8 = 1,
s8 = 1,
s16le = 2,
s16be = 2,
s24le = 3,
s24be = 3,
s32le = 4,
s32be = 4,
f32le = 4,
f32be = 4,
f64le = 8,
f64be = 8,
alaw = 1,
ulaw = 1,
}
Convert.SIZE = SIZE
-- the integer bit depth of each format (float: 32 / 64; G.711: 16 after expansion)
Convert.BITS = {
u8 = 8,
s8 = 8,
s16le = 16,
s16be = 16,
s24le = 24,
s24be = 24,
s32le = 32,
s32be = 32,
f32le = 32,
f32be = 32,
f64le = 64,
f64be = 64,
alaw = 16,
ulaw = 16,
}
-- ------------------------------------------------------------------------------------------------ G.711
local ALAW = table.create(256, 0) -- code -> linear s16
local ULAW = table.create(256, 0)
for code = 0, 255 do
-- A-law
local a = bit32.bxor(code, 0x55)
local t = bit32.band(a, 0x0F) * 16
local seg = bit32.rshift(bit32.band(a, 0x70), 4)
if seg == 0 then
t += 8
elseif seg == 1 then
t += 0x108
else
t = (t + 0x108) * 2 ^ (seg - 1)
end
ALAW[code + 1] = if bit32.band(a, 0x80) ~= 0 then t else -t
-- mu-law
local u = bit32.bxor(code, 0xFF)
local m = (bit32.band(u, 0x0F) * 8 + 0x84) * 2 ^ bit32.rshift(bit32.band(u, 0x70), 4)
ULAW[code + 1] = if bit32.band(u, 0x80) ~= 0 then 0x84 - m else m - 0x84
end
local SEG_AEND = { 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF }
local SEG_UEND = { 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF }
local function segment(v: number, ends: { number }): number
for i = 1, 8 do
if v <= ends[i] then
return i - 1
end
end
return 8
end
-- linear s16 -> A-law code
local function alawEncode(pcm: number): number
local mask
local v = floor(pcm / 8) -- 13-bit magnitude domain
if v >= 0 then
mask = 0xD5
else
mask = 0x55
v = -v - 1
end
local seg = segment(v, SEG_AEND)
local aval
if seg >= 8 then
aval = 0x7F
else
aval = seg * 16
if seg < 2 then
aval += bit32.band(floor(v / 2), 0x0F)
else
aval += bit32.band(floor(v / 2 ^ seg), 0x0F)
end
end
return bit32.bxor(aval, mask)
end
-- linear s16 -> mu-law code
local function ulawEncode(pcm: number): number
local v = floor(pcm / 4) -- 14-bit
local mask
if v < 0 then
v = -v
mask = 0x7F
else
mask = 0xFF
end
if v > 8159 then
v = 8159
end
v += 0x21
local seg = segment(v, SEG_UEND)
local uval
if seg >= 8 then
uval = 0x7F
else
uval = seg * 16 + bit32.band(floor(v / 2 ^ (seg + 1)), 0x0F)
end
return bit32.bxor(uval, mask)
end
Convert.alawEncode = alawEncode
Convert.ulawEncode = ulawEncode
Convert.ALAW = ALAW
Convert.ULAW = ULAW
-- ------------------------------------------------------------------------------------------------ decode
-- decode(src, so, fmt, count, dst, do_) -> true | false (unknown fmt): count samples at src[so] -> f32 at dst[do_]
function Convert.decode(src: buffer, so: number, fmt: string, count: number, dst: buffer, d: number): boolean
if fmt == "s16le" then
for i = 0, count - 1 do
writef32(dst, d + i * 4, readi16(src, so + i * 2) / 32768)
end
elseif fmt == "s16be" then
for i = 0, count - 1 do
local p = so + i * 2
local v = readu8(src, p) * 256 + readu8(src, p + 1)
if v >= 32768 then
v -= 65536
end
writef32(dst, d + i * 4, v / 32768)
end
elseif fmt == "u8" then
for i = 0, count - 1 do
writef32(dst, d + i * 4, (readu8(src, so + i) - 128) / 128)
end
elseif fmt == "s8" then
for i = 0, count - 1 do
writef32(dst, d + i * 4, readi8(src, so + i) / 128)
end
elseif fmt == "s24le" then
for i = 0, count - 1 do
local p = so + i * 3
local v = readu8(src, p) + readu8(src, p + 1) * 256 + readi8(src, p + 2) * 65536
writef32(dst, d + i * 4, v / 8388608)
end
elseif fmt == "s24be" then
for i = 0, count - 1 do
local p = so + i * 3
local v = readi8(src, p) * 65536 + readu8(src, p + 1) * 256 + readu8(src, p + 2)
writef32(dst, d + i * 4, v / 8388608)
end
elseif fmt == "s32le" then
for i = 0, count - 1 do
writef32(dst, d + i * 4, readi32(src, so + i * 4) / 2147483648)
end
elseif fmt == "s32be" then
for i = 0, count - 1 do
local v = byteswap(readu32(src, so + i * 4))
if v >= 2147483648 then
v -= 4294967296
end
writef32(dst, d + i * 4, v / 2147483648)
end
elseif fmt == "f32le" then
buffer.copy(dst, d, src, so, count * 4)
elseif fmt == "f32be" then
for i = 0, count - 1 do
writeu32(dst, d + i * 4, byteswap(readu32(src, so + i * 4)))
end
elseif fmt == "f64le" then
for i = 0, count - 1 do
writef32(dst, d + i * 4, readf64(src, so + i * 8))
end
elseif fmt == "f64be" then
local tmp = buffer.create(8)
for i = 0, count - 1 do
local p = so + i * 8
writeu32(tmp, 0, byteswap(readu32(src, p + 4)))
writeu32(tmp, 4, byteswap(readu32(src, p)))
writef32(dst, d + i * 4, readf64(tmp, 0))
end
elseif fmt == "alaw" then
for i = 0, count - 1 do
writef32(dst, d + i * 4, ALAW[readu8(src, so + i) + 1] / 32768)
end
elseif fmt == "ulaw" then
for i = 0, count - 1 do
writef32(dst, d + i * 4, ULAW[readu8(src, so + i) + 1] / 32768)
end
else
return false
end
return true
end
-- ------------------------------------------------------------------------------------------------ encode
local function q(x: number, s: number): number
local v = floor(x * s + 0.5)
if v ~= v then
return 0
end
if v >= s then
return s - 1
elseif v < -s then
return -s
end
return v
end
-- encode(src, so, count, fmt, dst, do_) -> true | false: count f32 samples at src[so] -> fmt at dst[do_]
function Convert.encode(src: buffer, so: number, count: number, fmt: string, dst: buffer, d: number): boolean
if fmt == "s16le" then
for i = 0, count - 1 do
writei16(dst, d + i * 2, q(readf32(src, so + i * 4), 32768))
end
elseif fmt == "s16be" then
for i = 0, count - 1 do
local v = q(readf32(src, so + i * 4), 32768) % 65536
writeu16(dst, d + i * 2, (v % 256) * 256 + v // 256)
end
elseif fmt == "u8" then
for i = 0, count - 1 do
writeu8(dst, d + i, q(readf32(src, so + i * 4), 128) + 128)
end
elseif fmt == "s8" then
for i = 0, count - 1 do
writeu8(dst, d + i, q(readf32(src, so + i * 4), 128) % 256)
end
elseif fmt == "s24le" then
for i = 0, count - 1 do
local v = q(readf32(src, so + i * 4), 8388608) % 16777216
local p = d + i * 3
writeu8(dst, p, v % 256)
writeu8(dst, p + 1, (v // 256) % 256)
writeu8(dst, p + 2, v // 65536)
end
elseif fmt == "s24be" then
for i = 0, count - 1 do
local v = q(readf32(src, so + i * 4), 8388608) % 16777216
local p = d + i * 3
writeu8(dst, p, v // 65536)
writeu8(dst, p + 1, (v // 256) % 256)
writeu8(dst, p + 2, v % 256)
end
elseif fmt == "s32le" then
for i = 0, count - 1 do
writei32(dst, d + i * 4, q(readf32(src, so + i * 4), 2147483648))
end
elseif fmt == "s32be" then
for i = 0, count - 1 do
writeu32(dst, d + i * 4, byteswap(q(readf32(src, so + i * 4), 2147483648) % 4294967296))
end
elseif fmt == "f32le" then
buffer.copy(dst, d, src, so, count * 4)
elseif fmt == "f32be" then
for i = 0, count - 1 do
writeu32(dst, d + i * 4, byteswap(readu32(src, so + i * 4)))
end
elseif fmt == "f64le" then
for i = 0, count - 1 do
writef64(dst, d + i * 8, readf32(src, so + i * 4))
end
elseif fmt == "f64be" then
local tmp = buffer.create(8)
for i = 0, count - 1 do
writef64(tmp, 0, readf32(src, so + i * 4))
local p = d + i * 8
writeu32(dst, p, byteswap(readu32(tmp, 4)))
writeu32(dst, p + 4, byteswap(readu32(tmp, 0)))
end
elseif fmt == "alaw" then
for i = 0, count - 1 do
writeu8(dst, d + i, alawEncode(q(readf32(src, so + i * 4), 32768)))
end
elseif fmt == "ulaw" then
for i = 0, count - 1 do
writeu8(dst, d + i, ulawEncode(q(readf32(src, so + i * 4), 32768)))
end
else
return false
end
return true
end
return Convert
@@ -1,20 +1,28 @@
--!strict
-- codecs/Pcm: PCM plumbing for audio encoders: one pull interface over an Audio or a Decoder, int <-> float samples.
-- An encoder accepts a Types.Audio (fully decoded) or a
-- Types.Decoder (streamed); Pcm.source turns either into one pull interface, so encoders stream in blocks without
-- caring which they got. Also the float <-> integer sample conversions (the same rounding and clipping everywhere).
-- codecs/Pcm: PCM plumbing shared by audio formats: raw sample formats, streaming Decoders, encoder input sources.
-- An encoder accepts a Types.Audio (fully decoded) or a Types.Decoder (streamed); Pcm.source turns either into one
-- pull interface, so encoders stream in blocks without caring which they got. Uncompressed containers (aiff, au,
-- voc, ...) get their Decoder from Pcm.rawDecoder (over a sample region of the file) or Pcm.audioDecoder.
--
-- local src, err = Pcm.source(audioOrDecoder) -- nil, Err.ARG for anything else
-- local buf, n = src:read(4096) -- interleaved f32, n frames (0 at the end), like Decoder:read
-- local audio = Pcm.collect(src) -- read everything into a Types.Audio
-- local dec = Pcm.rawDecoder(buf, offset, frames, channels, rate, "s16be", tags?) -- a Types.Decoder
-- local dec = Pcm.audioDecoder(audio) -- a Types.Decoder over a decoded Audio
-- local raw = Pcm.encode(src, "s24le", writer) -- every frame of src appended to a core/Bytes Writer
-- Pcm.Convert.decode / encode / SIZE / BITS -- the raw sample loops (codecs/Pcm/Convert)
-- local s16 = Pcm.toInt(x, 16) -- round, clip to [-32768, 32767]
-- local x = Pcm.fromInt(s16, 16) -- s / 32768
-- local audio = Pcm.collect(src) -- read everything into a Types.Audio
--
-- Sample formats: u8 s8 s16le s16be s24le s24be s32le s32be f32le f32be f64le f64be alaw ulaw.
-- Never throws (a decoder's read that throws is caught and ends the stream).
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Convert = require("@formats/codecs/Pcm/Convert")
local Pcm = {}
Pcm.Convert = Convert
export type Source = {
sampleRate: number,
@@ -128,6 +136,81 @@ function Pcm.collect(src: Source, maxFrames: number?): (Types.Audio?, Err.Err?)
return a, nil
end
-- ------------------------------------------------------------------------------------------------ decoders
-- rawDecoder(buf, offset, frames, channels, sampleRate, fmt, tags?) -> Types.Decoder | nil, Err.ARG: a streaming
-- decoder over frames * channels samples of fmt at buf[offset] (the caller has checked the range is inside buf)
function Pcm.rawDecoder(buf: buffer, offset: number, frames: number, channels: number, sampleRate: number, fmt: string, tags: Types.Tags?): (Types.Decoder?, Err.Err?)
local size = Convert.SIZE[fmt]
if not size then
return nil, Err.new(Err.ARG, "unknown sample format " .. tostring(fmt))
end
if channels < 1 or sampleRate <= 0 or frames < 0 or offset < 0 or offset + frames * channels * size > buffer.len(buf) then
return nil, Err.new(Err.ARG, "bad raw PCM layout")
end
local pos = 0
local frameBytes = channels * size
local dec = {
sampleRate = sampleRate,
channels = channels,
frames = frames,
duration = frames / sampleRate,
bitrate = sampleRate * frameBytes * 8,
tags = tags or {},
}
function dec.read(_self: any, n: number, out: buffer?): (buffer, number)
n = math.clamp(math.floor(n), 0, frames - pos)
local bytes = n * channels * 4
local b = if out and buffer.len(out) >= bytes then out else buffer.create(bytes)
if n > 0 then
Convert.decode(buf, offset + pos * frameBytes, fmt, n * channels, b, 0)
pos += n
end
return b, n
end
function dec.seek(_self: any, seconds: number)
local f = math.floor(seconds * sampleRate + 0.5)
pos = if f ~= f then 0 else math.clamp(f, 0, frames)
end
function dec.position(_self: any): number
return pos / sampleRate
end
return dec :: any, nil
end
-- audioDecoder(audio) -> Types.Decoder over a decoded Audio (no copy of the samples)
function Pcm.audioDecoder(a: Types.Audio): Types.Decoder
local d = Pcm.rawDecoder(a.samples, 0, a.frames, a.channels, a.sampleRate, "f32le", a.tags) :: any
d.bitrate = a.sampleRate * a.channels * 32
return d
end
-- encode(src, fmt, w) -> frames written | nil, Err: drains src into the Writer w as fmt samples
function Pcm.encode(src: Source, fmt: string, w: Bytes.Writer): (number?, Err.Err?)
local size = Convert.SIZE[fmt]
if not size then
return nil, Err.new(Err.ARG, "unknown sample format " .. tostring(fmt))
end
local ch = src.channels
local total = 0
local tmp: buffer? = nil
while true do
local b, n = src:read(8192, tmp)
if n <= 0 then
break
end
tmp = b
local at = w:reserve(n * ch * size)
Convert.encode(b, 0, n * ch, fmt, w.buf, at)
w.pos = at + n * ch * size
total += n
end
if src.err then
return nil, src.err
end
return total, nil
end
-- ------------------------------------------------------------------------------------------------ conversions
-- toInt(x, bits) -> the nearest bits-wide signed integer to x * 2^(bits-1), clipped (NaN -> 0)
function Pcm.toInt(x: number, bits: number): number
local s = 2 ^ (bits - 1)