-- Audio throughput (luau -O2 tools/bench_audio.luau [-a filter]): decode / encode speed of every audio codec as x real -- time and MB/s of f32 output, best of n runs. Fixtures: tests/gen (tools/fixtures.py). local Bytes = require("@rosetta/core/Bytes") local Types = require("@rosetta/Types") local filter = ... local runtime = if (_G :: any).jit ~= nil then "luajit" else "luau" local jit = (_G :: any).jit if jit and type(jit.opt) == "table" then jit.opt.start("maxtrace=8000", "maxmcode=65536") -- as host/init.luau sets them for the api end local function want(label: string): boolean return filter == nil or string.find(label, filter, 1, true) ~= nil end local function best(n: number, f: () -> ()): number f() local dt = math.huge for _ = 1, n do local t0 = os.clock() f() dt = math.min(dt, os.clock() - t0) end return dt end local function report(label: string, dt: number, secs: number, bytes: number) print(string.format("%-8s %-34s %9.2f ms %8.1f x RT %8.2f MB/s", runtime, label, dt * 1000, secs / dt, bytes / dt / 1e6)) end local function case(list: any, name: string): any for _, c in list do if c.name == name then return c end end error("no fixture " .. name) end -- decodeBench(label, module path, fixture, case name, runs): Module.decode(reader) on the case's bytes local function decodeBench(label: string, mod: any, data: () -> string, n: number) if not want(label) then return end local b = buffer.fromstring(data()) local a = assert(mod.decode(Bytes.reader(b))) local dt = best(n, function() mod.decode(Bytes.reader(b)) end) report(label, dt, a.frames / a.sampleRate, a.frames * a.channels * 4) end local Mp3 = require("@rosetta/audio/mp3") local function mp3Case(name: string): () -> string return function() return case(require("@tests/gen/mp3_cases"), name).data end end for _, name in { "l3_cbr128_js", "l3_vbr_v2_48k", "l2_192_st" } do decodeBench("mp3 " .. name, Mp3, mp3Case(name), 5) end if want("mp3 stream") then local b = buffer.fromstring(mp3Case("l3_cbr128_js")()) local out = buffer.create(1152 * 2 * 4) local frames, rate = 0, 44100 local dt = best(5, function() local d = assert(Mp3.open(Bytes.reader(b))) rate = d.sampleRate frames = 0 while true do local _, n = d:read(1152, out) if n == 0 then break end frames += n end end) report("mp3 stream read(1152) cbr128", dt, frames / rate, frames * 8) end local Opus = require("@rosetta/audio/opus") for _, c in { { "opus celt 128k", function() return require("@tests/gen/opus_celt_st_128").data end }, { "opus silk wb", function() return require("@tests/gen/opus_silk_wb_st").data end }, { "opus hybrid", function() return require("@tests/gen/opus_hybrid_st").data end }, { "opus 48k music (ogg)", function() return case(require("@tests/gen/ogg_cases"), "opus_stereo").data end }, } do decodeBench(c[1], Opus, c[2], 3) end local Vorbis = require("@rosetta/audio/vorbis") decodeBench("vorbis q5 (10 s st)", Vorbis, function() return require("@tests/gen/vorbis_bench").data end, 3) local M4a = require("@rosetta/audio/m4a") decodeBench("aac-lc m4a stereo 128k", M4a, function() return case(require("@tests/gen/m4a_aac"), "aac_stereo_128k.m4a").data end, 3) decodeBench("aac fdk m4a stereo", M4a, function() return case(require("@tests/gen/m4a_aac"), "fdk_stereo.m4a").data end, 3) -- HE-AAC: the 1 s fdkaac fixtures, ADTS frames repeated 4x (4.4 s) local AacF = require("@rosetta/audio/aac") for _, name in { "v1_stereo_48k", "v2_48k" } do local label = if name == "v2_48k" then "he-aac v2 adts 48k" else "he-aac v1 adts stereo 48k" decodeBench(label, AacF, function() return string.rep(case(require("@tests/gen/heaac"), name .. ".aac").data, 4) end, 5) end decodeBench("alac m4a stereo 16", M4a, function() return require("@tests/gen/m4a_alac")[1].data end, 3) local Flac = require("@rosetta/audio/flac") decodeBench("flac s16_long", Flac, function() return case(require("@tests/gen/flac_cases"), "s16_long").data end, 5) decodeBench("flac s24_96k", Flac, function() return case(require("@tests/gen/flac_cases"), "s24_96k").data end, 5) local Wav = require("@rosetta/audio/wav") do local a = Types.Audio.new(44100, 2, 441000) for i = 0, 882000 - 1 do buffer.writef32(a.samples, i * 4, math.sin(i * 0.01) * 0.5) end if want("wav") then for _, fmt in { "s16", "s24", "f32" } do local file = assert(Wav.encode(a, { sampleFormat = fmt })) local dt = best(5, function() Wav.decode(Bytes.reader(file)) end) report("wav decode " .. fmt .. " (10 s st)", dt, 10, buffer.len(a.samples)) dt = best(5, function() Wav.encode(a, { sampleFormat = fmt }) end) report("wav encode " .. fmt .. " (10 s st)", dt, 10, buffer.len(a.samples)) end end local Resample = require("@rosetta/codecs/audio/Resample") for _, r in { { 44100, 48000 }, { 48000, 44100 }, { 32000, 48000 } } do local label = string.format("resample %d->%d (10 s st)", r[1], r[2]) if want(label) then local dt = best(3, function() Resample.buffer(a.samples, 441000, 2, r[1], r[2]) end) report(label, dt, 441000 / r[1], buffer.len(a.samples)) end end local AudioOps = require("@rosetta/ops/AudioOps") if want("audioops") then report("audioops gain (10 s st)", best(5, function() AudioOps.gain(a, 0.5) end), 10, buffer.len(a.samples)) report("audioops peak+rms (10 s st)", best(5, function() AudioOps.peak(a) AudioOps.rms(a) end), 10, buffer.len(a.samples)) report("audioops downmix 2->1 (10 s)", best(5, function() AudioOps.downmix(a, 1) end), 10, buffer.len(a.samples)) report("audioops fade (10 s st)", best(5, function() AudioOps.fade(a, 2, 2) end), 10, buffer.len(a.samples)) end end local fixtures: { [string]: () -> any } = { wavpack = function() return require("@tests/gen/wavpack_cases") end, ape = function() return require("@tests/gen/ape_cases") end, tta = function() return require("@tests/gen/tta_cases") end, mpc = function() return require("@tests/gen/mpc_cases") end, ac3 = function() return require("@tests/gen/ac3_cases") end, } local function lossless(label: string, mod: any, fx: string, name: string, n: number) decodeBench(label, mod, function() return case(fixtures[fx](), name).data end, n) end lossless("wavpack s16_long", require("@rosetta/audio/wavpack"), "wavpack", "s16_long", 3) lossless("wavpack s16_hh", require("@rosetta/audio/wavpack"), "wavpack", "s16_hh", 3) lossless("ape s16_long", require("@rosetta/audio/ape"), "ape", "s16_long", 3) lossless("ape s16_long_c4000", require("@rosetta/audio/ape"), "ape", "s16_long_c4000", 3) lossless("tta s16_long", require("@rosetta/audio/tta"), "tta", "s16_long", 3) lossless("mpc sv8 q5 stereo", require("@rosetta/audio/mpc"), "mpc", "sv8_q5_stereo", 3) lossless("mpc sv7 q5 stereo", require("@rosetta/audio/mpc"), "mpc", "sv7_q5_stereo", 3) lossless("ac3 5.1_448", require("@rosetta/audio/ac3"), "ac3", "5.1_448", 3) lossless("ac3 stereo_44k", require("@rosetta/audio/ac3"), "ac3", "stereo_44k", 3) -- renders: x RT of stereo 44.1 kHz output (MB/s of f32 stereo) local function render(label: string, n: number, mk: () -> any, maxFrames: number?) if not want(label) then return end local out = buffer.create(4096 * 8) local frames = 0 local dt = best(n, function() local dec = mk() frames = 0 while true do local _, k = dec:read(4096, out) if k == 0 or (maxFrames and frames >= maxFrames) then break end frames += k end end) report(label, dt, frames / 44100, frames * 8) end if want("midi render sf2 (test song)") or want("midi render fallback") or want("synth 32 voices x 2 s") then local Midi = require("@rosetta/music/midi") local Sf2 = require("@rosetta/music/sf2") local SF = require("@tests/gen/sf2_cases") local sf = assert(Sf2.decode(Bytes.reader(buffer.fromstring(SF.sf2)))) local song = assert(Midi.decode(Bytes.reader(buffer.fromstring(SF.song)))) render("midi render sf2 (test song)", 3, function() return assert(Midi.render(song, { soundfont = sf, tail = 0 })) end) local midiCases: any = require("@tests/gen/midi_cases") local fbData: string = case(midiCases, "song").data local fb = assert(Midi.decode(Bytes.reader(buffer.fromstring(fbData)))) render("midi render fallback", 3, function() return assert(Midi.render(fb, { tail = 0 })) end) if want("synth 32 voices") then local SoundFont = require("@rosetta/codecs/audio/SoundFont") local frames = 88200 local o = buffer.create(frames * 8) local dt = best(3, function() local s = SoundFont.synth(sf, 44100) SoundFont.Synth.midi(s, 0xC0, 1, 0) for k = 0, 31 do SoundFont.Synth.midi(s, 0x90, 40 + k, 100) end for f = 0, frames - 1, 1024 do SoundFont.Synth.render(s, o, f, 1024) end end) report("synth 32 voices x 2 s", dt, 2, frames * 8) end end if want("tracker 32 ch (mod)") or want("tracker 32 ch + NNA, filters (it)") then local Tracker = require("@rosetta/codecs/audio/Tracker") local function song(format: string, filter: boolean): any local m = Tracker.newModule(format, 32) m.order = { 0 } m.patterns[1] = Tracker.newPattern(64, 32) local s = Tracker.newSample("saw") s.length = 1000 s.data = buffer.create(4000) for i = 0, 999 do buffer.writef32(s.data, i * 4, (i % 100) / 50 - 1) end s.loopStart, s.loopEnd, s.loopType = 0, 1000, 1 m.samples[1] = s if format == "it" then local ins = Tracker.newInstrument("nna") for n = 1, 120 do ins.sampleMap[n] = 1 end ins.nna = 1 ins.fadeout = 64 if filter then ins.cutoff, ins.resonance = 60, 40 end m.instruments[1] = ins end for ch = 1, 32 do for row = 0, 63, 16 do Tracker.setCell(m, 0, row, ch, 37 + ch + row // 16, 1, 0, 0, 0) end end return m end local mod, it = song("mod", false), song("it", true) render("tracker 32 ch (mod)", 3, function() return Tracker.render(mod) end) render("tracker 32 ch + NNA, filters (it)", 3, function() return Tracker.render(it) end) end if want("ahx render karma (4 ch)") or want("hvl render demo (6 ch)") then local Ahx, Hvl = require("@rosetta/music/ahx"), require("@rosetta/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 local c = case(spec[2], spec[3]) local s = assert(spec[1].decode(Bytes.reader(buffer.fromstring(c.data)))) render(spec[4], 3, function() return s:render({ maxDuration = c.maxDuration }) end) end end