ws-apifix: one bitrate unit: every encoder's bitrate option is bit/s (MP3 now too), values below 1000 are kbit/s (the old MP3 values keep working; Opus accepts them too); the MP3 Decoder's bitrate field is bit/s as the Decoder contract says (was kbit/s)

This commit is contained in:
4DBug committed 2026-10-31 14:06:51 -05:00
1 parent 3c2a646365
commit ac47545aa3
10 files changed
+35 -14

No files matched your search

+1 -1
View File
@@ -193,7 +193,7 @@ for _, c in Cases do
t:check(a.meta.source.format == "mp3" and a.meta.source.layer == dec.layer, name .. ": meta.source")
t:eq(Mp3.sniff(Bytes.reader(buf)) > 0.5, true, name .. ": sniff")
t:note("%-20s L%d MPEG-%s %5d Hz %d ch %6d frames %3d kbps SNR minimp3 %s, ffmpeg %.1f dB", name, dec.layer, tostring(dec.version), dec.sampleRate, ch, dec.frames, dec.bitrate, if ref then string.format("%.1f", worst) else "-", worstFF)
t:note("%-20s L%d MPEG-%s %5d Hz %d ch %6d frames %3d kbps SNR minimp3 %s, ffmpeg %.1f dB", name, dec.layer, tostring(dec.version), dec.sampleRate, ch, dec.frames, dec.bitrate // 1000, if ref then string.format("%.1f", worst) else "-", worstFF)
end
-- ------------------------------------------------------------------------------------------------ robustness
+8
View File
@@ -102,6 +102,13 @@ do
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")
@@ -169,6 +176,7 @@ do
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")
+3
View File
@@ -85,6 +85,9 @@ do
t:check(again, "encode from a decoder: " .. tostring(err))
local back = Opus.decode(Bytes.reader(again :: buffer)) :: any
t:eq(back.frames, math.floor(c.seconds * 48000 + 0.5), "re-encoded length")
-- values below 1000 are kbit/s: the same file as the bit/s value
local kb = Opus.encode(Opus.open(Bytes.reader(files[1] :: buffer)) :: any, { bitrate = 64 })
t:check(kb ~= nil and buffer.tostring(kb :: buffer) == buffer.tostring(again :: buffer), "bitrate 64 = 64000 bit/s")
end
-- errors