ws-cleanup: codecs/Resample, codecs/RangeCoder; Opus / MP3 / MP2 encoders resample through it
- codecs/Resample (new): Kaiser-windowed sinc resampler, polyphase (exact phases when to / gcd <= 4096, else 4096
interpolated phases), quality low / medium / high (8 / 16 / 32 zero crossings), streaming (new / process / flush),
whole buffer (Resample.buffer) and a codecs/Pcm source wrapper (Resample.source). tests/codecs/resample: SNR
87 dB (44.1 -> 48 kHz, medium) .. 108 dB (high), alias rejection, streamed = whole, identity.
- audio/opus/Encode: input through codecs/Pcm (Pcm.source + collect) and codecs/Resample instead of its private
resampler and Decoder loop; tags through codecs/VorbisComment.
- audio/mp3 encode: rates the layer cannot hold are resampled to the next rate up (opts.sampleRate picks one,
opts.resampleQuality); was Err.ARG "no resampling".
- codecs/RangeCoder/{Decoder,Encoder}: the RFC 6716 range coder moved out of audio/opus (same code, no speed change).
- audio/opus/Mdct stays private: routing CELT through codecs/Mdct measured 2-3x slower CELT decoding and different
output bits (documented in its header).
- (this commit also carries the Opus side of the tags / channel order consistency: codecs/VorbisComment for OpusTags,
meta.source.comments, mapping family 1 decoded to WAVE channel order with Pcm.reorder / Pcm.WAVE_FROM_VORBIS.)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
a25b7c0caa
commit
6b68c2ffab
17 files changed
+777
-150
No files matched your search
@@ -2,10 +2,14 @@
|
||||
-- audio/opus/Encode: Ogg Opus writing (RFC 7845) around audio/opus/CeltEncoder: CELT-only packets (TOC config 31:
|
||||
-- full band, 20 ms, code 0), OpusHead (mapping family 0, the pre-skip = the encoder's delay), OpusTags (vendor +
|
||||
-- the tags as Vorbis comments), granule positions with end trimming, pages through containers/Ogg's muxer. Input at
|
||||
-- other rates is resampled to 48 kHz (a Kaiser-windowed sinc; quality enough for a codec input). New in formats-luau.
|
||||
-- other rates is resampled to 48 kHz (codecs/Resample); the input is a Types.Audio or a Decoder (codecs/Pcm). New in
|
||||
-- formats-luau.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Ogg = require("@formats/containers/Ogg")
|
||||
local Pcm = require("@formats/codecs/Pcm")
|
||||
local Resample = require("@formats/codecs/Resample")
|
||||
local VorbisComment = require("@formats/codecs/VorbisComment")
|
||||
local CeltEncoder = require("@formats/audio/opus/CeltEncoder")
|
||||
|
||||
local Encode = {}
|
||||
@@ -17,69 +21,20 @@ export type Options = {
|
||||
serial: number?, -- Ogg stream serial (default 0x4f707573)
|
||||
}
|
||||
|
||||
local floor, sin, pi, sqrt = math.floor, math.sin, math.pi, math.sqrt
|
||||
|
||||
local function besselI0(x: number): number
|
||||
local sum, term = 1, 1
|
||||
for k = 1, 60 do
|
||||
term *= (x / (2 * k)) ^ 2
|
||||
sum += term
|
||||
if term < sum * 1e-15 then
|
||||
break
|
||||
end
|
||||
end
|
||||
return sum
|
||||
end
|
||||
|
||||
-- resample(src (interleaved f32 buffer), frames, channels, from, to) -> { number } interleaved, frames out
|
||||
local function resample(src: buffer, frames: number, channels: number, from: number, to: number): ({ number }, number)
|
||||
local outFrames = floor(frames * to / from + 0.5)
|
||||
local out = table.create(outFrames * channels, 0)
|
||||
local ratio = to / from
|
||||
local cutoff = 0.97 * math.min(1, ratio) -- of the input Nyquist
|
||||
local half = 16 -- zero crossings each side (at the cutoff)
|
||||
local width = half / cutoff
|
||||
local beta = 8
|
||||
local i0b = besselI0(beta)
|
||||
local readf32 = buffer.readf32
|
||||
for j = 0, outFrames - 1 do
|
||||
local t = j / ratio -- the input position
|
||||
local first = math.max(0, floor(t - width) + 1)
|
||||
local last = math.min(frames - 1, floor(t + width))
|
||||
for c = 0, channels - 1 do
|
||||
local acc, wsum = 0, 0
|
||||
for i = first, last do
|
||||
local d = i - t
|
||||
local x = d * cutoff
|
||||
local s = if x == 0 then 1 else sin(pi * x) / (pi * x)
|
||||
local r = d / width
|
||||
local w = besselI0(beta * sqrt(math.max(0, 1 - r * r))) / i0b
|
||||
local h = s * w
|
||||
acc += readf32(src, (i * channels + c) * 4) * h
|
||||
wsum += h
|
||||
end
|
||||
out[j * channels + c + 1] = if wsum ~= 0 then acc / wsum else 0
|
||||
end
|
||||
end
|
||||
return out, outFrames
|
||||
end
|
||||
|
||||
local TAG_NAMES: { [string]: string } = {
|
||||
title = "TITLE", artist = "ARTIST", album = "ALBUM", albumartist = "ALBUMARTIST", date = "DATE", genre = "GENRE",
|
||||
track = "TRACKNUMBER", tracktotal = "TRACKTOTAL", disc = "DISCNUMBER", comment = "COMMENT", composer = "COMPOSER",
|
||||
}
|
||||
local floor = math.floor
|
||||
|
||||
-- encode(audio | decoder, opts) -> buffer | nil, Err
|
||||
function Encode.encode(value: any, opts: Options?): (buffer?, Err.Err?)
|
||||
local o = opts or {}
|
||||
if type(value) ~= "table" or type(value.sampleRate) ~= "number" or type(value.channels) ~= "number" then
|
||||
return nil, Err.new(Err.ARG, "expected an Audio or a Decoder")
|
||||
local src, serr = Pcm.source(value)
|
||||
if not src then
|
||||
return nil, serr
|
||||
end
|
||||
local ch = value.channels
|
||||
local ch = src.channels
|
||||
if ch ~= 1 and ch ~= 2 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "the CELT encoder writes mono or stereo (got " .. ch .. " channels)")
|
||||
end
|
||||
local rate = value.sampleRate
|
||||
local rate = src.sampleRate
|
||||
if rate < 1000 or rate > 768000 then
|
||||
return nil, Err.new(Err.ARG, "bad sample rate " .. tostring(rate))
|
||||
end
|
||||
@@ -87,40 +42,24 @@ function Encode.encode(value: any, opts: Options?): (buffer?, Err.Err?)
|
||||
if type(bitrate) ~= "number" or bitrate < 6000 or bitrate > 510000 then
|
||||
return nil, Err.new(Err.ARG, "bitrate 6000 .. 510000")
|
||||
end
|
||||
-- the whole input as an interleaved f32 buffer
|
||||
local samples: buffer, frames: number
|
||||
if value.kind == "audio" and type(value.samples) == "buffer" then
|
||||
samples, frames = value.samples, value.frames
|
||||
if buffer.len(samples) < frames * ch * 4 then
|
||||
return nil, Err.new(Err.ARG, "audio.samples is shorter than frames * channels")
|
||||
-- the whole input at 48 kHz (codecs/Resample for other rates), then as floats
|
||||
if rate ~= 48000 then
|
||||
local rs, rerr = Resample.source(src, 48000)
|
||||
if not rs then
|
||||
return nil, Err.new(Err.ARG, tostring(rerr))
|
||||
end
|
||||
elseif type(value.read) == "function" then
|
||||
local w = Bytes.writer(65536)
|
||||
frames = 0
|
||||
local chunk: buffer? = nil
|
||||
while true do
|
||||
local b, n = value:read(16384, chunk)
|
||||
chunk = b
|
||||
if n <= 0 then
|
||||
break
|
||||
end
|
||||
w:bytes(b, 0, n * ch * 4)
|
||||
frames += n
|
||||
end
|
||||
samples = w:finish()
|
||||
else
|
||||
return nil, Err.new(Err.ARG, "expected an Audio or a Decoder")
|
||||
src = rs
|
||||
end
|
||||
-- 48 kHz floats
|
||||
local pcm: { number }, n48: number
|
||||
if rate == 48000 then
|
||||
n48 = frames
|
||||
pcm = table.create(frames * ch, 0)
|
||||
for i = 1, frames * ch do
|
||||
pcm[i] = buffer.readf32(samples, (i - 1) * 4)
|
||||
end
|
||||
else
|
||||
pcm, n48 = resample(samples, frames, ch, rate, 48000)
|
||||
local all, aerr = Pcm.collect(src :: Pcm.Source)
|
||||
if not all then
|
||||
return nil, aerr
|
||||
end
|
||||
local n48 = all.frames
|
||||
local samples = all.samples
|
||||
local pcm: { number } = table.create(n48 * ch, 0)
|
||||
local readf32 = buffer.readf32
|
||||
for i = 1, n48 * ch do
|
||||
pcm[i] = readf32(samples, (i - 1) * 4)
|
||||
end
|
||||
local FRAME = CeltEncoder.FRAME
|
||||
local preSkip = CeltEncoder.DELAY
|
||||
@@ -144,22 +83,15 @@ function Encode.encode(value: any, opts: Options?): (buffer?, Err.Err?)
|
||||
head:u8(0)
|
||||
s:packet(head:finish(), 0, { flush = true })
|
||||
-- OpusTags
|
||||
local tags = o.tags or value.tags or {}
|
||||
local tags = o.tags or src.tags or {}
|
||||
local tw = Bytes.writer(256)
|
||||
tw:string("OpusTags")
|
||||
local vendor = o.vendor or "formats-luau"
|
||||
tw:u32le(#vendor)
|
||||
tw:string(vendor)
|
||||
local keys = {}
|
||||
for k, v in tags do
|
||||
if type(k) == "string" and type(v) == "string" then
|
||||
table.insert(keys, k)
|
||||
end
|
||||
end
|
||||
table.sort(keys)
|
||||
tw:u32le(#keys)
|
||||
for _, k in keys do
|
||||
local field = (TAG_NAMES[k] or string.upper(k)) .. "=" .. tags[k]
|
||||
local fields = VorbisComment.fields(tags)
|
||||
tw:u32le(#fields)
|
||||
for _, field in fields do
|
||||
tw:u32le(#field)
|
||||
tw:string(field)
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user