Files
rosetta/tests/audio/_opus_cases.luau
T

43 lines
1.9 KiB
Luau

-- tests/audio/_opus_cases: the inputs of the CELT encoder reference check (tools/fixtures/opus_celt.py runs
-- tools/fixtures/_opus_encode.luau over them; tests/audio/opus_encode.luau re-encodes and compares): a deterministic
-- music-like signal (chord + vibrato + chirp + clicks + a silent stretch) at a given rate / channel count.
local Types = require("@formats/Types")
local Cases = {}
Cases.list = {
{ name = "celt_mono_64k", rate = 48000, channels = 1, seconds = 1.5, bitrate = 64000, minSnr = 15 },
{ name = "celt_stereo_128k", rate = 48000, channels = 2, seconds = 1.5, bitrate = 128000, minSnr = 15 },
{ name = "celt_stereo_256k", rate = 48000, channels = 2, seconds = 1, bitrate = 256000, minSnr = 22 },
{ name = "celt_mono_24k", rate = 48000, channels = 1, seconds = 1, bitrate = 24000, minSnr = 8 },
{ name = "celt_44k_stereo_96k", rate = 44100, channels = 2, seconds = 1, bitrate = 96000, minSnr = 0, tags = { title = "Celt \u{e9}", artist = "formats-luau", album = "Opus" } },
{ name = "celt_short", rate = 48000, channels = 1, seconds = 0.01, bitrate = 48000, minSnr = -100 },
}
function Cases.signal(c: any): Types.Audio
local n = math.floor(c.seconds * c.rate + 0.5)
local a = Types.Audio.new(c.rate, c.channels, n)
local s = 7
for i = 0, n - 1 do
local t = i / c.rate
for ch = 0, c.channels - 1 do
s = (s * 48271) % 2147483647
local v = 0.18 * math.sin(2 * math.pi * 220 * t + 2 * math.sin(2 * math.pi * 5 * t))
v += 0.12 * math.sin(2 * math.pi * (277.2 + ch * 2) * t) + 0.1 * math.sin(2 * math.pi * 329.6 * t)
v += 0.08 * math.sin(2 * math.pi * (1000 + 3000 * t) * t)
v += 0.01 * (s / 2147483647 - 0.5)
if i % (c.rate // 4) < 30 then
v += 0.4 * (if (i // 5) % 2 == 0 then 1 else -1)
end
if t > 0.6 and t < 0.75 then
v = 0 -- digital silence (the encoder's silence frames)
end
buffer.writef32(a.samples, (i * c.channels + ch) * 4, v)
end
end
a.tags = c.tags or {}
return a
end
return Cases