audio: drop our codecs/Mdct: ws-m4a landed theirs first (960107b), merging it instead
This commit is contained in:
1 parent
b94692d4c0
commit
aea16eeec7
3 files changed
-420
No files matched your search
@@ -1,131 +0,0 @@
|
||||
-- codecs/Mdct tests: the FFT against a direct DFT, the DCT-IV against its definition, the MDCT / IMDCT against their
|
||||
-- O(N^2) definitions at offsets, perfect reconstruction (TDAC) with the sine and Vorbis windows at scale 4/N, and bad
|
||||
-- sizes. No fixtures (the definitions are the reference).
|
||||
local T = require("@tests/T")
|
||||
local Mdct = require("@formats/codecs/Mdct")
|
||||
|
||||
local t = T.suite("mdct")
|
||||
local rand = T.rng(7)
|
||||
local function rnd(): number
|
||||
return rand(20001) / 10000 - 1
|
||||
end
|
||||
local cos, sin, pi = math.cos, math.sin, math.pi
|
||||
|
||||
-- FFT vs DFT
|
||||
for _, n in { 2, 4, 8, 64, 256 } do
|
||||
local f = Mdct.fft(n) :: Mdct.Fft
|
||||
local re, im, r0, i0 = {}, {}, {}, {}
|
||||
for i = 1, n do
|
||||
re[i], im[i] = rnd(), rnd()
|
||||
r0[i], i0[i] = re[i], im[i]
|
||||
end
|
||||
f:forward(re, im)
|
||||
local worst = 0
|
||||
for k = 0, n - 1 do
|
||||
local sr, si = 0, 0
|
||||
for j = 0, n - 1 do
|
||||
local a = -2 * pi * j * k / n
|
||||
sr += r0[j + 1] * cos(a) - i0[j + 1] * sin(a)
|
||||
si += r0[j + 1] * sin(a) + i0[j + 1] * cos(a)
|
||||
end
|
||||
worst = math.max(worst, math.abs(sr - re[k + 1]), math.abs(si - im[k + 1]))
|
||||
end
|
||||
t:check(worst < 1e-9, "fft " .. n .. ": error " .. worst)
|
||||
f:inverse(re, im)
|
||||
local back = 0
|
||||
for i = 1, n do
|
||||
back = math.max(back, math.abs(re[i] / n - r0[i]), math.abs(im[i] / n - i0[i]))
|
||||
end
|
||||
t:check(back < 1e-12, "fft " .. n .. ": inverse")
|
||||
end
|
||||
|
||||
-- DCT-IV
|
||||
for _, m in { 4, 8, 32, 128 } do
|
||||
local u, X = {}, {}
|
||||
for i = 1, m do
|
||||
u[i] = rnd()
|
||||
end
|
||||
t:check(Mdct.dct4(u, 0, X, 0, m), "dct4 plan " .. m)
|
||||
local worst = 0
|
||||
for k = 0, m - 1 do
|
||||
local s = 0
|
||||
for n = 0, m - 1 do
|
||||
s += u[n + 1] * cos(pi / m * (n + 0.5) * (k + 0.5))
|
||||
end
|
||||
worst = math.max(worst, math.abs(s - X[k + 1]))
|
||||
end
|
||||
t:check(worst < 1e-9, "dct4 " .. m .. ": error " .. worst)
|
||||
end
|
||||
|
||||
-- MDCT / IMDCT vs the definitions, with offsets
|
||||
for _, n in { 16, 32, 64, 256, 512 } do
|
||||
local p = Mdct.new(n) :: Mdct.Plan
|
||||
local m = n // 2
|
||||
local x = {}
|
||||
for i = 1, n + 3 do
|
||||
x[i] = rnd()
|
||||
end
|
||||
local X = table.create(m + 5, 0)
|
||||
p:forward(x, 3, X, 5)
|
||||
local worst = 0
|
||||
for k = 0, m - 1 do
|
||||
local s = 0
|
||||
for i = 0, n - 1 do
|
||||
s += x[3 + i + 1] * cos(pi / m * (i + 0.5 + m / 2) * (k + 0.5))
|
||||
end
|
||||
worst = math.max(worst, math.abs(s - X[5 + k + 1]))
|
||||
end
|
||||
t:check(worst < 1e-8, "mdct " .. n .. ": error " .. worst)
|
||||
local y = table.create(n + 2, 0)
|
||||
p:inverse(X, 5, y, 2)
|
||||
worst = 0
|
||||
for i = 0, n - 1 do
|
||||
local s = 0
|
||||
for k = 0, m - 1 do
|
||||
s += X[5 + k + 1] * cos(pi / m * (i + 0.5 + m / 2) * (k + 0.5))
|
||||
end
|
||||
worst = math.max(worst, math.abs(s - y[2 + i + 1]))
|
||||
end
|
||||
t:check(worst < 1e-8, "imdct " .. n .. ": error " .. worst)
|
||||
end
|
||||
|
||||
-- TDAC: window, forward (scale 4/N), inverse, window, overlap-add reconstructs the middle blocks
|
||||
for _, wname in { "sine", "vorbis" } do
|
||||
local n = 256
|
||||
local m = n // 2
|
||||
local p = Mdct.new(n) :: Mdct.Plan
|
||||
local w = if wname == "sine" then Mdct.sineWindow(n) else Mdct.vorbisWindow(m)
|
||||
local function win(i: number): number -- i = 0 .. n-1
|
||||
if wname == "sine" then
|
||||
return w[i + 1]
|
||||
end
|
||||
return if i < m then w[i + 1] else w[n - i]
|
||||
end
|
||||
local len = m * 6
|
||||
local sig = {}
|
||||
for i = 1, len do
|
||||
sig[i] = rnd()
|
||||
end
|
||||
local out = table.create(len, 0)
|
||||
local blk, X, y = table.create(n, 0), table.create(m, 0), table.create(n, 0)
|
||||
for start = 0, len - n, m do
|
||||
for i = 0, n - 1 do
|
||||
blk[i + 1] = sig[start + i + 1] * win(i)
|
||||
end
|
||||
p:forward(blk, 0, X, 0, 4 / n)
|
||||
p:inverse(X, 0, y, 0)
|
||||
for i = 0, n - 1 do
|
||||
out[start + i + 1] += y[i + 1] * win(i)
|
||||
end
|
||||
end
|
||||
local worst = 0
|
||||
for i = m + 1, len - m do
|
||||
worst = math.max(worst, math.abs(out[i] - sig[i]))
|
||||
end
|
||||
t:check(worst < 1e-9, wname .. " window TDAC: error " .. worst)
|
||||
end
|
||||
|
||||
t:check(Mdct.new(100) == nil and Mdct.new(8) == nil and Mdct.fft(3) == nil and Mdct.new(0.5) == nil, "bad sizes")
|
||||
t:check(Mdct.new(64) == Mdct.new(64), "plans are cached")
|
||||
-- (Mdct is a codec without a decoder of untrusted bytes: there is nothing to fuzz.)
|
||||
t:done()
|
||||
Reference in new issue
Block a user