From 51630729f6fb2e94c0894a5105f42a82b5989679 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 14:06:51 -0500 Subject: [PATCH] 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) Co-Authored-By: Claude Opus 5.5 --- docs/guide/README.md | 4 ++-- docs/guide/audio.md | 10 ++++++---- docs/guide/recipes.md | 2 +- examples/audio.luau | 3 +-- src/Formats/audio/mp3/Stream.luau | 2 +- src/Formats/audio/mp3/init.luau | 9 +++++++-- src/Formats/audio/opus/Encode.luau | 6 +++++- tests/audio/mp3.luau | 2 +- tests/audio/mp3_encode.luau | 8 ++++++++ tests/audio/opus_encode.luau | 3 +++ 10 files changed, 35 insertions(+), 14 deletions(-) diff --git a/docs/guide/README.md b/docs/guide/README.md index 233334d..d1fff6c 100644 --- a/docs/guide/README.md +++ b/docs/guide/README.md @@ -193,7 +193,7 @@ Every call takes an optional `opts` table. The options every format understands: | `maxPixels`, `maxBytes`, `maxFrames`, ... | size limits; going over returns `LIMIT` | Each format adds its own options, such as `quality` for JPEG, `lossless` for WebP, `level` for PNG and zip, or -`bitrate` for MP3. The type pages list them. +`bitrate` (bits per second) for MP3 and Opus. The type pages list them. ```luau local img = assert(F.load(pngBytes)) @@ -252,7 +252,7 @@ file instead of returning a buffer. ```luau local webp = assert(F.convert(pngBytes, "webp", { lossless = true })) -local mp3 = assert(F.convert(wavBytes, "mp3", { bitrate = 128 })) +local mp3 = assert(F.convert(wavBytes, "mp3", { bitrate = 128000 })) assert(F.detect(webp) == "webp" and F.detect(mp3) == "mp3") ``` diff --git a/docs/guide/audio.md b/docs/guide/audio.md index 8d602b4..94271f6 100644 --- a/docs/guide/audio.md +++ b/docs/guide/audio.md @@ -10,7 +10,7 @@ local song = assert(F.audio.load(flacBytes)) -- Audio: the whole file print(song.sampleRate, song.channels, song.frames) -- 44100 2 44100 print(song.tags.title, song.tags.artist) -- Docs Tone formats-luau local quieter = F.ops.audio.gainDb(song, -6) -local mp3 = assert(F.save(quieter, nil, { format = "mp3", bitrate = 192 })) -- a buffer +local mp3 = assert(F.save(quieter, nil, { format = "mp3", bitrate = 192000 })) -- a buffer assert(F.detect(mp3) == "mp3") ``` @@ -174,12 +174,15 @@ Encoder options: |---|---| | wav | `sampleFormat = "u8" \| "s16" \| "s24" \| "s32" \| "f32"` (default `"s16"`), `rf64`, `extensible`, `channelMask`, `tags` | | flac | `level` 0..8 (default 5), `bits` 4..24 (default: the source depth, else 16), `blockSize`, `seekInterval`, `padding` | -| mp3 | `bitrate` **in kbps** (default 128), `layer` 3 or 2 (MP2), `mode = "stereo" \| "joint" \| "mono"`, `sampleRate`, `id3`, `resampleQuality` | +| mp3 | `bitrate` (default 128000; one of the layer's rates), `layer` 3 or 2 (MP2), `mode = "stereo" \| "joint" \| "mono"`, `sampleRate`, `id3`, `resampleQuality` | | vorbis | `quality` -1..10 (default 3) | -| opus | `bitrate` **in bits per second** (default 64000 per channel); CELT only; resampled to 48 kHz | +| opus | `bitrate` (default 64000 per channel, 6000..510000); CELT only; resampled to 48 kHz | | aiff, au, voc | `sampleFormat` (see each format's `init.luau`) | | m4a, caf | ALAC (lossless); m4a and caf also keep tags | +`bitrate` is in **bits per second** for every encoder (`bitrate = 192000`), as the Decoder's `bitrate` field is. A +value below 1000 is read as kbit/s (`bitrate = 192` is 192000), so older code that passed kbit/s to MP3 still works. + Tags are written from `audio.tags`, or from the Decoder's `tags`, wherever the format has tags. ## Formats @@ -242,7 +245,6 @@ visualisers. ## Gotchas -- **Bitrate units differ.** MP3 takes kbps (`bitrate = 192`); Opus takes bits per second (`bitrate = 96000`). - `F.ops.audio.downmix(a)` defaults to **2** channels, so a stereo input stays stereo. Use `downmix(a, 1)` for mono. - `read(n, out)` returns a new buffer when `out` is too small. Always use the buffer it returns. - Opus always decodes at 48 kHz. Resample with `F.ops.audio.resample` if you need another rate. diff --git a/docs/guide/recipes.md b/docs/guide/recipes.md index aab1bee..d632022 100644 --- a/docs/guide/recipes.md +++ b/docs/guide/recipes.md @@ -112,7 +112,7 @@ end `F.convert` streams audio: the encoder reads the decoder as it goes. ```luau -local mp3 = assert(F.convert(flacBytes, "mp3", { bitrate = 192 })) -- MP3 bit rates are in kbit/s +local mp3 = assert(F.convert(flacBytes, "mp3", { bitrate = 192000 })) -- bit rates are in bit/s local dec = assert(F.audio.open(mp3)) assert(dec.sampleRate == 44100 and dec.channels == 2) assert(math.abs(dec.duration - 1) < 0.1) diff --git a/examples/audio.luau b/examples/audio.luau index 173b7bf..99a5120 100644 --- a/examples/audio.luau +++ b/examples/audio.luau @@ -22,8 +22,7 @@ print(string.format("edited: %d frames, %.2f s, peak %.2f", both.frames, both.fr -- 3. encode: lossless and lossy for _, fmt in { "wav", "flac", "mp3", "vorbis", "opus" } do local opts = { format = fmt } - if fmt == "mp3" then opts.bitrate = 128 end -- kbit/s - if fmt == "opus" then opts.bitrate = 64000 end -- bit/s + if fmt == "mp3" or fmt == "opus" then opts.bitrate = 128000 end -- bit/s (values below 1000: kbit/s) local bytes = assert(F.save(both, nil, opts)) local dec = assert(F.audio.open(bytes)) print(string.format("%-6s %6d bytes %5d Hz %d ch %.2f s title=%s", fmt, buffer.len(bytes), dec.sampleRate, dec.channels, diff --git a/src/Formats/audio/mp3/Stream.luau b/src/Formats/audio/mp3/Stream.luau index b080873..d720d99 100644 --- a/src/Formats/audio/mp3/Stream.luau +++ b/src/Formats/audio/mp3/Stream.luau @@ -68,7 +68,7 @@ function Stream.new(b: buffer, idx: any, tags: { [string]: string }, id3: any): channels = nch, frames = total, duration = total / hz, - bitrate = if idx.n > 0 then floor(idx.bytes * 8 * hz / (idx.n * spf) / 1000 + 0.5) else 0, + bitrate = if idx.n > 0 then floor(idx.bytes * 8 * hz / (idx.n * spf) + 0.5) else 0, -- bit/s tags = tags, id3 = id3, layer = idx.layer, diff --git a/src/Formats/audio/mp3/init.luau b/src/Formats/audio/mp3/init.luau index 5bcaffa..8875cbc 100644 --- a/src/Formats/audio/mp3/init.luau +++ b/src/Formats/audio/mp3/init.luau @@ -44,7 +44,8 @@ export type OpenOptions = { export type Decoder = Stream.Decoder export type EncodeOptions = { layer: number?, -- 3 (MP3, default) or 2 (MP2) - bitrate: number?, -- kbps, constant (default 128 for layer III, 192 for layer II; MPEG-2 / 2.5 rates: 64 / 96) + bitrate: number?, -- bits per second, constant: one of the layer's rates (default 128000 for layer III, 192000 for + -- layer II; MPEG-2 / 2.5 rates: 64000 / 96000); values below 1000 are kbit/s (128 = 128000, the old unit) mode: ("stereo" | "joint" | "mono")?, -- default "joint" (mid / side) for stereo layer III, "stereo" for layer II, -- "mono" for mono input; layer II has no joint stereo (intensity) and writes "joint" as "stereo" id3: boolean?, -- write an ID3v2.4 tag from the tags (default true when there are tags) @@ -208,9 +209,13 @@ function Mp3.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?) if layer == 2 and mode == "joint" then mode = "stereo" end + -- bitrate: bit/s, or kbit/s below 1000; the encoder works in kbit/s local bitrate = o.bitrate or (if mpeg1 then (if layer == 3 then 128 else 192) else (if layer == 3 then 64 else 96)) + if type(bitrate) == "number" and bitrate >= 1000 then + bitrate /= 1000 + end if not table.find(Enc.BITRATES[if mpeg1 then 1 else 2], bitrate) then - return nil, Err.new(Err.ARG, string.format("%s kbps is not a layer %d bit rate at %d Hz", tostring(bitrate), layer, rate)) + return nil, Err.new(Err.ARG, string.format("%s bit/s is not a layer %d bit rate at %d Hz", tostring(if type(o.bitrate) == "number" and o.bitrate < 1000 then o.bitrate * 1000 else o.bitrate), layer, rate)) end if layer == 2 and mpeg1 then -- the standard's allowed bit rate / mode combinations diff --git a/src/Formats/audio/opus/Encode.luau b/src/Formats/audio/opus/Encode.luau index 3195ab7..36406b7 100644 --- a/src/Formats/audio/opus/Encode.luau +++ b/src/Formats/audio/opus/Encode.luau @@ -15,7 +15,8 @@ local CeltEncoder = require("@formats/codecs/audio/Opus/CeltEncoder") local Encode = {} export type Options = { - bitrate: number?, -- bits per second (default 64000 per channel; 6000 .. 510000, constant) + bitrate: number?, -- bits per second (default 64000 per channel; 6000 .. 510000, constant); values below 1000 + -- are kbit/s (96 = 96000), as for every encoder's bitrate option tags: { [string]: string }?, -- default: the audio's tags vendor: string?, -- OpusTags vendor (default "formats-luau") serial: number?, -- Ogg stream serial (default 0x4f707573) @@ -39,6 +40,9 @@ function Encode.encode(value: any, opts: Options?): (buffer?, Err.Err?) return nil, Err.new(Err.ARG, "bad sample rate " .. tostring(rate)) end local bitrate = o.bitrate or 64000 * ch + if type(bitrate) == "number" and bitrate > 0 and bitrate < 1000 then + bitrate *= 1000 -- kbit/s + end if type(bitrate) ~= "number" or bitrate < 6000 or bitrate > 510000 then return nil, Err.new(Err.ARG, "bitrate 6000 .. 510000") end diff --git a/tests/audio/mp3.luau b/tests/audio/mp3.luau index b03c97a..c2d711c 100644 --- a/tests/audio/mp3.luau +++ b/tests/audio/mp3.luau @@ -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 diff --git a/tests/audio/mp3_encode.luau b/tests/audio/mp3_encode.luau index 2403a4e..7eb71cf 100644 --- a/tests/audio/mp3_encode.luau +++ b/tests/audio/mp3_encode.luau @@ -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") diff --git a/tests/audio/opus_encode.luau b/tests/audio/opus_encode.luau index c3aeff4..f1c6507 100644 --- a/tests/audio/opus_encode.luau +++ b/tests/audio/opus_encode.luau @@ -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