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

+2 -2
View File
@@ -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")
```
+6 -4
View File
@@ -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.
+1 -1
View File
@@ -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)