Files

183 lines
7.6 KiB
Luau

-- audio/mp3 encoder tests (layer III and II): fixture-step encodes decode in ffmpeg and LAME, SNR over a per-bit-rate
-- floor after aligning the decoder delay.
-- Fixtures: tools/fixtures/mp3_enc.py.
local T = require("@tests/T")
local Mp3 = require("@rosetta/audio/mp3")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Pcm = require("@rosetta/codecs/audio/Pcm")
local Enc = require("@tests/gen/mp3_enc")
local Sigs = require("@tests/gen/mp3_enc_sigs")
local t = T.suite("mp3_encode")
local function audio(name: string): Types.Audio
local s = Sigs[name]
local a = Types.Audio.new(s.rate, s.channels, s.frames)
buffer.writestring(a.samples, 0, s.data)
return a
end
local function snr(x: Types.Audio, y: Types.Audio, lag: number, from: number, to: number): number
local ch = x.channels
local sig, er = 0, 0
for i = from, to - 1 do
local j = i + lag
if j >= 0 and j < y.frames then
for c = 0, ch - 1 do
local a = buffer.readf32(x.samples, (i * ch + c) * 4)
local b = buffer.readf32(y.samples, (j * y.channels + math.min(c, y.channels - 1)) * 4)
sig += a * a
er += (a - b) * (a - b)
end
end
end
if er == 0 then
return 200
end
return if sig > 0 then 10 * math.log10(sig / er) else -200
end
-- the SNR floors (dB) on the music / sine / sweep signals, by layer and bit rate per channel
local function floorFor(e: any): number
if string.find(e.name, "silence", 1, true) then
return -math.huge
elseif string.find(e.name, "noise", 1, true) then
return 8 -- white noise has no spectral structure to exploit
end
local perCh = e.bitrate / (if e.mode == "mono" then 1 else 2)
local nyquistBand = e.rate <= 16000 -- low rates lose little to the encoder's band limit
if e.layer == 3 then
if perCh >= 64 then
return 14
elseif perCh >= 32 or nyquistBand then
return 8
end
return 4
end
if perCh >= 96 then
return 14
elseif perCh >= 48 then
return 8
end
return 4
end
t:check(Mp3.caps.encode and Mp3.encode ~= nil, "caps.encode")
local minByLayer = { [2] = math.huge, [3] = math.huge }
for _, e in Enc do
t:eq(e.ffErrors, "", e.name .. ": ffmpeg decodes without errors")
t:check(e.ffRate == e.rate, e.name .. ": rate")
t:check(e.ffChannels == (if e.mode == "mono" then 1 else 2), e.name .. ": channels")
-- the stream covers the input: the encoder adds one frame of flush and the filterbank delay
local spf = if e.layer == 3 and e.rate < 32000 then 576 else 1152
t:check(e.ffFrames >= e.frames and e.ffFrames <= e.frames + 3 * spf, string.format("%s: %d decoded frames for %d", e.name, e.ffFrames, e.frames))
local f = floorFor(e)
t:check(e.ffSnr >= f, string.format("%s: ffmpeg SNR %.1f dB (floor %g)", e.name, e.ffSnr, f))
if e.lameSnr then
t:check(e.lameSnr >= f, string.format("%s: LAME SNR %.1f dB (floor %g)", e.name, e.lameSnr, f))
end
if f > -math.huge then
minByLayer[e.layer] = math.min(minByLayer[e.layer], e.ffSnr)
end
local x = audio(e.sig)
local y, err = Mp3.decode(Bytes.reader(buffer.fromstring(e.data)))
t:check(y, e.name .. ": our decoder: " .. tostring(err))
if e.channels == y.channels then
local s = snr(x, y, e.ffLag, 200, x.frames)
t:check(s >= f and math.abs(s - e.ffSnr) < 3, string.format("%s: our decoder %.1f dB, ffmpeg %.1f dB", e.name, s, e.ffSnr))
end
end
t:note("%d encodes: ffmpeg + LAME decode them; lowest SNR layer III %.1f dB, layer II %.1f dB", #Enc, minByLayer[3], minByLayer[2])
do
local a = audio("music_2_44100")
local f1 = Mp3.encode(a, { bitrate = 128 }) :: buffer
local f2 = Mp3.encode(Pcm.audioDecoder(a), { bitrate = 128 }) :: buffer
t:check(buffer.tostring(f1) == buffer.tostring(f2), "Decoder source = Audio source")
-- bitrate in bit/s (the documented unit), and the old kbit/s values below 1000: the same file
local f4 = Mp3.encode(a, { bitrate = 128000 }) :: buffer
t:check(f4 ~= nil and buffer.tostring(f1) == buffer.tostring(f4), "bitrate 128000 bit/s = 128 kbit/s")
local d4 = Mp3.open(Bytes.reader(f4)) :: any
t:check(math.abs(d4.bitrate - 128000) < 1000, "the decoder reports bit/s: " .. tostring(d4.bitrate))
local _, e5 = Mp3.encode(a, { bitrate = 127000 })
t:check(e5 and e5.code == Err.ARG and string.find(e5.message, "127000 bit/s", 1, true) ~= nil, "127000 bit/s refused: " .. tostring(e5))
a.tags = { title = "Ünïcode title", artist = "An Artist", album = "Album", track = "4", comment = "made here" }
local f3 = Mp3.encode(a, { layer = 2 }) :: buffer
t:eq(buffer.readstring(f3, 0, 3), "ID3", "an ID3v2 tag in front")
local back = Mp3.open(Bytes.reader(f3)) :: any
t:eq(back.tags.title, "Ünïcode title", "title")
t:eq(back.tags.artist, "An Artist", "artist")
t:eq(back.tags.album, "Album", "album")
t:eq(back.tags.track, "4", "track")
t:eq(back.layer, 2, "layer II by default bit rate 192")
local f4 = Mp3.encode(a, { id3 = false }) :: buffer
t:check(buffer.readu8(f4, 0) == 0xFF, "id3 = false")
local m = Types.Audio.new(22050, 1, 3000)
local fm = Mp3.encode(m) :: buffer
local dm = Mp3.open(Bytes.reader(fm)) :: any
t:check(dm.channels == 1 and dm.sampleRate == 22050, "mono MPEG-2 default")
local empty = Mp3.encode(Types.Audio.new(44100, 2, 0)) :: buffer
t:check(empty and buffer.len(empty) > 0, "empty input: one flush frame")
end
-- other input rates are resampled to the next rate up (codecs/audio/Resample)
do
local function tone(rate: number, f: number): any
local n = rate
local a = Types.Audio.new(rate, 1, n)
for i = 0, n - 1 do
buffer.writef32(a.samples, i * 4, 0.5 * math.sin(2 * math.pi * f * i / rate))
end
return a
end
-- the share of the decoded energy at f (a DFT bin over the middle half)
local function purity(d: any, f: number): number
local pcm, n = d:read(d.frames)
local re, im, e = 0, 0, 0
local lo, hi = n // 4, 3 * n // 4
for i = lo, hi - 1 do
local x = buffer.readf32(pcm, i * d.channels * 4)
re += x * math.cos(2 * math.pi * f * i / d.sampleRate)
im += x * math.sin(2 * math.pi * f * i / d.sampleRate)
e += x * x
end
return 2 * (re * re + im * im) / (hi - lo) / math.max(e, 1e-30)
end
for _, c in { { 44000, nil, nil, 44100 }, { 46000, nil, nil, 48000 }, { 8000, 2, nil, 16000 }, { 44100, 3, 22050, 22050 }, { 11000, 3, nil, 11025 } } do
local f, err = Mp3.encode(tone(c[1], 600), { layer = c[2], sampleRate = c[3], bitrate = if c[4] < 32000 then 64 else 128 })
t:check(f, c[1] .. " Hz input: " .. tostring(err))
if f then
local d = Mp3.open(Bytes.reader(f)) :: any
t:eq(d.sampleRate, c[4], c[1] .. " Hz input: written at " .. c[4])
-- (no LAME gapless header: the encoder delay and the last frame's padding stay)
t:check(d.frames >= c[4] and d.frames <= c[4] + 3 * 1152, c[1] .. " Hz input: length " .. d.frames)
local p = purity(d, 600)
t:check(p > 0.98, string.format("%d Hz input: tone purity %.4f", c[1], p))
end
end
end
do
local function code(v: any, o: any): string?
local _, e = Mp3.encode(v, o)
return e and e.code
end
local a = Types.Audio.new(44100, 2, 100)
t:eq(code({}, nil), Err.ARG, "junk")
t:eq(code(a, { layer = 1 }), Err.ARG, "layer I")
t:eq(code(a, { bitrate = 100 }), Err.ARG, "100 kbps")
t:eq(code(a, { bitrate = 100000 }), Err.ARG, "100000 bit/s")
t:eq(code(a, { mode = "dual" }), Err.ARG, "dual mode")
t:eq(code(a, { sampleRate = 44000 }), Err.ARG, "opts.sampleRate 44 kHz")
t:eq(code(a, { layer = 2, sampleRate = 8000 }), Err.ARG, "layer II at 8 kHz")
t:eq(code(Types.Audio.new(44100, 3, 10), nil), Err.ARG, "3 channels")
t:eq(code(a, { layer = 2, bitrate = 32 }), Err.ARG, "layer II stereo at 32 kbps")
t:eq(code(Types.Audio.new(44100, 1, 10), { layer = 2, bitrate = 320 }), Err.ARG, "layer II mono at 320 kbps")
end
-- (the decoder's fuzz pass is in tests/audio/mp3.luau; the encoders take no untrusted bytes)
t:done()