143 lines
6.5 KiB
Luau
143 lines
6.5 KiB
Luau
-- ops/AudioOps tests on known signals.
|
|
local T = require("@tests/T")
|
|
local AudioOps = require("@rosetta/ops/AudioOps")
|
|
local Types = require("@rosetta/Types")
|
|
local Pcm = require("@rosetta/codecs/audio/Pcm")
|
|
local Err = require("@rosetta/core/Err")
|
|
local F = require("@rosetta")
|
|
|
|
local t = T.suite("audioops")
|
|
|
|
local function audio(rate: number, ch: number, frames: number, fn: (f: number, c: number) -> number): Types.Audio
|
|
local a = Types.Audio.new(rate, ch, frames)
|
|
for f = 0, frames - 1 do
|
|
for c = 0, ch - 1 do
|
|
buffer.writef32(a.samples, (f * ch + c) * 4, fn(f, c))
|
|
end
|
|
end
|
|
a.tags = { title = "x" }
|
|
return a
|
|
end
|
|
local function at(a: Types.Audio, f: number, c: number): number
|
|
return buffer.readf32(a.samples, (f * a.channels + c) * 4)
|
|
end
|
|
local function near(x: number, y: number, eps: number?): boolean
|
|
return math.abs(x - y) <= (eps or 1e-6)
|
|
end
|
|
|
|
local sine = audio(8000, 2, 8000, function(f, c)
|
|
return (if c == 0 then 0.5 else 0.25) * math.sin(2 * math.pi * 440 * f / 8000)
|
|
end)
|
|
|
|
do
|
|
local dec = Pcm.audioDecoder(sine)
|
|
local a = AudioOps.toAudio(dec) :: Types.Audio
|
|
t:check(a and a.frames == 8000 and a.channels == 2 and buffer.tostring(a.samples) == buffer.tostring(sine.samples), "toAudio(Decoder)")
|
|
t:check(AudioOps.toAudio(sine) == sine, "toAudio(Audio) is the Audio")
|
|
local _, e2 = AudioOps.toAudio(Pcm.audioDecoder(sine), { maxFrames = 10 })
|
|
t:check(e2 and e2.code == Err.LIMIT, "maxFrames -> LIMIT")
|
|
end
|
|
|
|
do
|
|
local p, per = AudioOps.peak(sine)
|
|
t:check(near(p :: number, 0.5, 1e-3) and near((per :: any)[2], 0.25, 1e-3), "peak")
|
|
local r, rper = AudioOps.rms(sine)
|
|
t:check(near((rper :: any)[1], 0.5 / math.sqrt(2), 1e-3) and near((rper :: any)[2], 0.25 / math.sqrt(2), 1e-3), "rms per channel")
|
|
t:check(near(r :: number, math.sqrt((0.125 + 0.03125) / 2), 1e-3), "rms")
|
|
local g = AudioOps.gainDb(sine, -6.0206) :: Types.Audio
|
|
t:check(near(at(g, 100, 0), at(sine, 100, 0) / 2, 1e-4), "gainDb -6 dB halves")
|
|
t:eq(g.tags.title, "x", "tags copied")
|
|
local n = AudioOps.normalize(sine) :: Types.Audio
|
|
t:check(near((AudioOps.peak(n)) :: number, 1, 1e-5), "normalize to 1")
|
|
local z = AudioOps.normalize(Types.Audio.new(8000, 1, 10)) :: Types.Audio
|
|
t:check(z and z.frames == 10, "normalize silence")
|
|
end
|
|
|
|
do
|
|
-- 5.1 (WAVE order FL FR C LFE BL BR) with distinct constants
|
|
local five = audio(8000, 6, 4, function(_, c)
|
|
return ({ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6 })[c + 1]
|
|
end)
|
|
local st = AudioOps.downmix(five) :: Types.Audio
|
|
t:check(st and st.channels == 2, "5.1 -> stereo")
|
|
local h = math.sqrt(0.5)
|
|
local norm = 1 / (1 + 2 * h)
|
|
t:check(near(at(st, 0, 0), (0.1 + h * 0.3 + h * 0.5) * norm, 1e-6), "stereo L = FL + C / sqrt2 + BL / sqrt2 (normalised)")
|
|
t:check(near(at(st, 0, 1), (0.2 + h * 0.3 + h * 0.6) * norm, 1e-6), "stereo R (LFE dropped)")
|
|
local mono = AudioOps.downmix(sine, 1) :: Types.Audio
|
|
t:check(near(at(mono, 100, 0), (at(sine, 100, 0) + at(sine, 100, 1)) / 2, 1e-6), "stereo -> mono averages")
|
|
local up = AudioOps.upmix(mono, 2) :: Types.Audio
|
|
t:check(up.channels == 2 and at(up, 100, 0) == at(mono, 100, 0) and at(up, 100, 1) == at(mono, 100, 0), "mono -> stereo copies")
|
|
local up6 = AudioOps.upmix(sine, 6) :: Types.Audio
|
|
t:check(up6.channels == 6 and at(up6, 7, 0) == at(sine, 7, 0) and at(up6, 7, 1) == at(sine, 7, 1) and at(up6, 7, 2) == 0, "stereo -> 5.1 keeps FL / FR")
|
|
local swapped = AudioOps.remix(sine, 2, { { 0, 1 }, { 1, 0 } }) :: Types.Audio
|
|
t:check(at(swapped, 9, 0) == at(sine, 9, 1), "explicit matrix")
|
|
local _, e = AudioOps.downmix(mono, 2)
|
|
t:check(e and e.code == Err.ARG, "downmix to more channels: ARG")
|
|
end
|
|
|
|
do
|
|
local r = AudioOps.resample(sine, 16000) :: Types.Audio
|
|
t:check(r.sampleRate == 16000 and r.frames == 16000, "resample 8 -> 16 kHz")
|
|
local err = 0
|
|
for f = 2000, 14000 do
|
|
err = math.max(err, math.abs(at(r, f, 0) - 0.5 * math.sin(2 * math.pi * 440 * f / 16000)))
|
|
end
|
|
t:check(err < 1e-3, string.format("resampled sine (max err %.1e)", err))
|
|
local c = AudioOps.trim(sine, 0.25, 0.5) :: Types.Audio
|
|
t:check(c.frames == 2000 and at(c, 0, 0) == at(sine, 2000, 0), "trim [0.25, 0.5) s")
|
|
t:eq((AudioOps.trim(sine, 0.9, 5) :: Types.Audio).frames, 800, "trim clamps")
|
|
local cat = AudioOps.concat({ sine, c }) :: Types.Audio
|
|
t:check(cat.frames == 10000 and at(cat, 8000, 1) == at(c, 0, 1), "concat")
|
|
local monoHi = audio(16000, 1, 1600, function()
|
|
return 0.1
|
|
end)
|
|
local cat2 = AudioOps.concat({ sine, monoHi }) :: Types.Audio
|
|
t:check(cat2.channels == 2 and cat2.sampleRate == 8000 and math.abs(cat2.frames - 8800) <= 1, "concat converts rate and channels")
|
|
local m = AudioOps.mix({ sine, sine }, { 0.5, 0.25 }) :: Types.Audio
|
|
t:check(near(at(m, 50, 0), at(sine, 50, 0) * 0.75, 1e-6), "mix with weights")
|
|
local m2 = AudioOps.mix({ c, sine }) :: Types.Audio
|
|
t:check(m2.frames == 8000, "mix is as long as the longest")
|
|
local fd = AudioOps.fade(sine, 0.5, 0.5) :: Types.Audio
|
|
t:check(at(fd, 0, 0) == 0 and near(at(fd, 2000, 0), at(sine, 2000, 0) * 0.5, 1e-6) and near(at(fd, 7999, 0), 0), "fades")
|
|
end
|
|
|
|
do
|
|
local junk: { any } = { nil, 5, "x", {}, { kind = "audio" }, Types.Image.new(2, 2) }
|
|
local calls = {
|
|
function(v: any) return AudioOps.gain(v, 2) end,
|
|
function(v: any) return AudioOps.remix(v, 2) end,
|
|
function(v: any) return AudioOps.resample(v, 44100) end,
|
|
function(v: any) return AudioOps.trim(v, 0) end,
|
|
function(v: any) return AudioOps.concat({ v }) end,
|
|
function(v: any) return AudioOps.mix(v) end,
|
|
function(v: any) return AudioOps.peak(v) end,
|
|
function(v: any) return AudioOps.fade(v, 1) end,
|
|
}
|
|
local ok = true
|
|
for _, fn in calls do
|
|
for i = 1, #junk + 1 do
|
|
local s, r, e = pcall(fn, junk[i])
|
|
ok = ok and s and r == nil and type(e) == "table" and e.code ~= nil
|
|
end
|
|
end
|
|
t:check(ok, "bad values return nil, Err")
|
|
t:check(AudioOps.gain(sine, 0 / 0) == nil, "NaN gain")
|
|
t:check(AudioOps.resample(sine, 0) == nil, "zero rate")
|
|
t:check(AudioOps.remix(sine, 0) == nil, "zero channels")
|
|
end
|
|
|
|
do
|
|
t:check(F.ops.audio == AudioOps, "F.ops.audio")
|
|
t:check(F.ops.image ~= nil and type(F.ops.image.resize) == "function", "F.ops.image")
|
|
t:check(type(F.ops.mesh.merge) == "function", "F.ops.mesh")
|
|
t:check(type(F.ops.layers.flatten) == "function", "F.ops.layers")
|
|
local v, err = F.load('<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8"><rect width="8" height="8" fill="red"/></svg>')
|
|
t:check(v, "svg loads: " .. tostring(err))
|
|
local img, rerr = F.ops.vector.rasterize(v, 8, 8)
|
|
t:check(img and img.width == 8 and buffer.readu8(img.pixels, 0) == 255 and buffer.readu8(img.pixels, 1) == 0, "F.ops.vector.rasterize: " .. tostring(rerr))
|
|
t:check(F.ops.nothing == nil, "unknown ops: nil")
|
|
end
|
|
|
|
t:done()
|