merge ws/audio2

This commit is contained in:
4DBug committed 2026-10-31 15:59:38 -05:00
commit f0a23eb676
149 files changed
+23004 -312

No files matched your search

+198
View File
@@ -1538,3 +1538,201 @@ do
Exr.encode(img)
end)
end
-- ------------------------------------------------------------------------------------------- ws-video2-legacy: fps
do
-- the legacy codecs (codecs/video/Legacy) through F.video.open, rgba8 frames out: ffmpeg's encoders at 320x240
-- (tools/fixtures/legacy.py), a 320x200 FLC (fli.py) and a 320x200 SMK4 (smk.py, textured: mostly FULL blocks);
-- frames per second of the whole decode (and the Smacker audio track, seconds of audio per second)
local F = require("@formats")
local LB = require("@tests/gen/legacy_bench")
local cases = table.clone(LB)
table.insert(cases, { name = "fli.flc", data = require("@tests/gen/fli_bench").data, width = 320, height = 200 })
table.insert(cases, { name = "smk4.smk", data = require("@tests/gen/smk_bench").data, width = 320, height = 200 })
for _, c in cases do
local data = buffer.fromstring(c.data)
local frames = 0
local dt = math.huge
for _ = 1, 3 do
local t0 = os.clock()
local v = assert(F.video.open(data, { audio = false }))
frames = 0
while v:nextFrame() do
frames += 1
end
dt = math.min(dt, os.clock() - t0)
end
print(string.format("%-8s %-30s %8.2f ms %7.1f fps", runtime, string.format("legacy %s %dx%d", c.name, c.width, c.height), dt * 1000 / frames, frames / dt))
end
local v = assert(F.video.open(buffer.fromstring(require("@tests/gen/smk_bench").data)))
local a = assert(v.audio)
local t0 = os.clock()
local n = 0
while true do
local _, got = a:read(4096)
if got == 0 then
break
end
n += got
end
local dt = os.clock() - t0
print(string.format("%-8s %-30s %8.2f ms %7.1fx real time", runtime, "smacker audio 22 kHz stereo", dt * 1000, n / a.sampleRate / dt))
end
-- ws-audio2: chip/midi
do
-- AHX / HVL (codecs/audio/Hively): a whole song rendered at 44.1 kHz (tools/fixtures/ahx.py karma.ahx, 3 s; hvl.py
-- the 6-channel demo with ring modulation); MB/s of stereo f32 output, and how many times faster than real time
local Ahx, Hvl = require("@formats/music/ahx"), require("@formats/music/hvl")
local AC, HC = require("@tests/gen/ahx_cases"), require("@tests/gen/hvl_cases")
for _, spec in { { Ahx, AC, "karma.ahx", "ahx render karma (4 ch)" }, { Hvl, HC, "demo", "hvl render demo (6 ch)" } } do
for _, c in spec[2] do
if c.name == spec[3] then
local song = assert(spec[1].decode(Bytes.reader(buffer.fromstring(c.data))))
local frames = 0
local dt = bench(spec[4], 3, c.frames * 8, function()
local dec = song:render({ maxDuration = c.maxDuration })
frames = dec.frames
dec:read(frames)
end)
print(string.format("%-8s %-30s %8.1f x real time", runtime, "", frames / 44100 / dt))
end
end
end
end
-- ws-audio2: lossy
do
-- Musepack (codecs/audio/Musepack) SV8 / SV7 and AC-3 5.1 (codecs/audio/Ac3): whole-file decodes, MB/s of f32 output
-- (tools/fixtures/mpc.py: 1.2 s of 44.1 kHz stereo at quality 5; tools/fixtures/ac3.py: 0.16 s of 48 kHz 5.1 at
-- 448 kbps, decoded 4 times per run)
local Mpc = require("@formats/audio/mpc")
local MpcFx = require("@tests/gen/mpc_cases")
local Ac3F = require("@formats/audio/ac3")
local Ac3Fx = require("@tests/gen/ac3_cases")
for _, c in MpcFx do
if c.name == "sv8_q5_stereo" or c.name == "sv7_q5_stereo" then
local b = buffer.fromstring(c.data)
bench("mpc " .. c.name, 5, c.frames * c.channels * 4, function()
assert(Mpc.decode(Bytes.reader(b)))
end)
end
end
for _, c in Ac3Fx do
if c.name == "5.1_448" then
local b = buffer.fromstring(c.data)
bench("ac3 5.1 448 kbps", 5, c.frames * c.channels * 4 * 4, function()
for _ = 1, 4 do
assert(Ac3F.decode(Bytes.reader(b)))
end
end)
end
end
end
-- ws-audio2: adpcm / qoa
do
-- codecs/audio/Adpcm (IMA nibble stream, Nintendo DSP frames) and QOA decode / encode, over generated signals; MB/s
-- of f32 output (MB/s of input for the encoder)
local Adpcm = require("@formats/codecs/audio/Adpcm")
local QoaCodec = require("@formats/codecs/audio/Qoa")
local Qoa = require("@formats/audio/qoa")
local n = 44100 * 5
local sig = Types.Audio.new(44100, 2, n)
for i = 0, n - 1 do
local t = i / 44100
buffer.writef32(sig.samples, i * 8, 0.5 * math.sin(2 * math.pi * 440 * t) + 0.1 * math.sin(2 * math.pi * 3000 * t))
buffer.writef32(sig.samples, i * 8 + 4, 0.4 * math.sin(2 * math.pi * 660 * t + 1))
end
-- IMA: encode the left channel as a nibble stream, then decode it
local ima = buffer.create(n // 2)
Adpcm.imaEncode(sig.samples, 0, 2, n, buffer.create(8), 0, ima, 0, 0, 1, false)
local out = buffer.create(n * 4)
bench("adpcm ima decode", 5, n * 4, function()
Adpcm.ima(ima, 0, 0, 1, n, false, buffer.create(8), 0, out, 0, 1)
end)
-- DSP: random frames with 8 coefficient pairs
local frames = n // 14
local dsp = buffer.create(frames * 8)
local seed = 1
for i = 0, frames * 8 - 1 do
seed = (seed * 1103515245 + 12345) % 2147483648
local v = seed // 65536 % 256
buffer.writeu8(dsp, i, if i % 8 == 0 then (v % 8) * 16 + v % 12 else v)
end
local coefs = buffer.create(64)
for k = 0, 7 do
buffer.writei32(coefs, k * 8, 1800 + k * 200)
buffer.writei32(coefs, k * 8 + 4, -900 - k * 100)
end
bench("adpcm dsp decode", 5, frames * 14 * 4, function()
Adpcm.dsp(dsp, 0, frames * 14, coefs, 0, buffer.create(8), 0, out, 0, 1)
end)
-- QOA (stereo 44.1 kHz)
local file = assert(Qoa.encode(sig))
bench("qoa encode", 2, n * 2 * 4, function()
Qoa.encode(sig)
end)
local pcm = buffer.create(QoaCodec.FRAME_LEN * 2 * 4)
bench("qoa decode", 5, n * 2 * 4, function()
local p = 8
while p < buffer.len(file) do
local _, size = QoaCodec.decodeFrame(file, p, buffer.len(file) - p, pcm, 0)
p += size :: number
end
end)
bench("qoa decode (F.load path)", 5, n * 2 * 4, function()
Qoa.decode(Bytes.reader(file))
end)
end
-- ws-audio2: lossless
do
-- WavPack / Monkey's Audio / TTA decoders (tools/fixtures/wavpack.py, ape.py, tta.py): MB/s of f32 output
local function lossless(label: string, mod: any, data: string)
local b = buffer.fromstring(data)
local dec = assert(mod.open(Bytes.reader(b)))
bench(label, 5, dec.frames * dec.channels * 4, function()
mod.decode(Bytes.reader(b))
end)
end
local Wv = require("@formats/audio/wavpack")
for _, c in require("@tests/gen/wavpack_cases") do
if c.name == "s16_long" or c.name == "s16_hh" or c.name == "s24_hh" or c.name == "f32" or c.name == "hyb_b3" then
lossless("wavpack " .. c.name, Wv, c.data)
end
end
local ApeF = require("@formats/audio/ape")
for _, c in require("@tests/gen/ape_cases") do
if c.name == "s16_long" or c.name == "s16_long_c4000" or c.name == "s16_c5000" or c.name == "s24" or c.name == "s16_c1000" then
lossless("ape " .. c.name, ApeF, c.data)
end
end
local TtaF = require("@formats/audio/tta")
for _, c in require("@tests/gen/tta_cases") do
if c.name == "s16_long" or c.name == "s24" or c.name == "ch6" then
lossless("tta " .. c.name, TtaF, c.data)
end
end
end
-- ws-audio2: trackers
do
-- the other tracker loaders on the shared engine: a 669 (Hz periods, auto slides) and an MMD1 (hold / decay
-- envelopes, instruments) from tools/fixtures/{669,med}.py, decoded and rendered at 44.1 kHz
local Six69 = require("@formats/music/669")
local Med = require("@formats/music/med")
local songs = { { Six69, require("@tests/gen/669_cases")[1].data }, { Med, require("@tests/gen/med_cases")[2].data } }
local best, frames = math.huge, 0
local out = buffer.create(4096 * 8)
for _ = 1, 3 do
frames = 0
local t0 = os.clock()
for _, s in songs do
local dec = (s[1].decode(Bytes.reader(buffer.fromstring(s[2]))) :: any):render()
while true do
local _, n = dec:read(4096, out)
if n == 0 then
break
end
frames += n
end
end
best = math.min(best, os.clock() - t0)
end
print(string.format("%-8s %-30s %8.2f ms %7.1f x realtime", runtime, "render 669 / med", best * 1000, frames / 44100 / best))
end