audio perf: resample planar history + shared dot loop, mdct inverse fused unfold + fft index hoisting, AudioOps strided loops / remix term lists, wav sample loops, SILK LPC in locals, upvalue hoisting (Celt, Mpa), --!optimize 2 on every audio module; tools/bench_audio.luau

This commit is contained in:
4DBug committed 2026-11-01 19:00:38 -06:00
1 parent 01c044805f
commit 051aca5b74
98 files changed
+707 -171

No files matched your search

+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/aac: raw AAC in ADTS framing (ISO/IEC 13818-7 / 14496-3 1.A.3). Supports: AAC-LC via codecs/audio/Aac (1-8
-- channels, every rate), CRC-protected headers, a leading ID3v2 tag, resync, seeking by frame; HE-AAC plays its
-- AAC-LC core. Not supported: writing, ADIF, LATM.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/ac3: raw AC-3 (Dolby Digital, ATSC A/52) via codecs/audio/Ac3. Supports: every channel layout (WAVE order, no
-- downmix), 32 / 44.1 / 48 kHz, every bit rate, the CRC check, resync, exact seeking; drc 0 .. 1, off by default.
-- Not supported: writing, E-AC-3 (UNSUPPORTED), bsid 9 / 10 reduced-rate streams.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/adpcm: headerless ADPCM (.adpcm / .ima / .vox) via codecs/audio/Adpcm; sniff is 0, the layout comes from opts.
-- "ima": DVI nibbles, high first; "ima_wav": MS IMA blocks; "ms": MS ADPCM, 7 standard coefficient pairs; "ima_qt":
-- Apple ima4 (34-byte packets, 64 frames); "psx": PS-ADPCM (16-byte frames, 28 samples). Encode: ima, ima_wav, ms.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/adx: CRI ADX via codecs/audio/Adpcm; header and decoder follow vgmstream r2117 (ISC,
-- licenses/vgmstream-LICENSE.txt), bit-exact with it. Supports: encoding type 3, versions 3-5, 1-8 channels, loop
-- points; encode as version 4. Not supported: encrypted ADX (0x0408 / 0x0409), encoding types 2 and 4, AHX.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/aiff: AIFF / AIFF-C (Apple AIFF 1.3, AIFF-C draft 1991). Supports: PCM 1-32 bits; AIFF-C NONE / twos /
-- sowt / raw / in24 / in32 / fl32 / fl64 / ulaw / alaw / ima4; text chunks as tags; truncated SSND; encode 8-32 bit,
-- sowt, fl32, fl64, ulaw, alaw. Not supported: other compressed AIFF-C codecs, MARK / INST, the ID3 chunk.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/ape: Monkey's Audio (.ape) via codecs/audio/Ape and ApeTag, from the Monkey's Audio SDK 13.28 (BSD 3-clause,
-- licenses/monkeys-audio-LICENSE.txt). Supports: versions 3.95+, every level, 8-32-bit integer and 32-bit float, 1-32
-- channels, an ID3v2 prefix, seeking by the seek table. Not supported: files before 3.95, encoding.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/au: Sun / NeXT audio (.au / .snd), big endian. Supports: encodings 1 (mu-law), 2-5 (8-32-bit linear), 6 / 7
-- (float), 27 (A-law), data size 0xFFFFFFFF (to the end), the annotation as tags.comment; encode to any of them.
-- Not supported: G.721 / G.722 / G.723 ADPCM and the other NeXT encodings.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/brstm: Nintendo BRSTM (Wii, "RSTM", BE), BCSTM (3DS, "CSTM", LE), BFSTM (Wii U BE / Switch LE, "FSTM"); layouts
-- follow vgmstream r2117 (ISC, licenses/vgmstream-LICENSE.txt), bit-exact. Supports: DSP ADPCM, PCM16 / PCM8, 1-255
-- channels, loop points. Read only. Not supported: BCSTM IMA ADPCM, BRSTM 0.1, BFSTM regions, the track table.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/caf: Apple Core Audio Format (CAF 1.0 spec). Supports: linear PCM (8-32-bit integer, 32 / 64-bit float, either
-- endian), ALAC, AAC-LC (kuki esds), mu-law, A-law, IMA4; pakt trimming, data size -1, info tags, seeking; encode PCM
-- (16 / 24-bit BE, 32-bit float) and ALAC (16 / 20 / 24-bit). Not supported: other compressed formats.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/flac: FLAC (RFC 9639), native and Ogg FLAC; absorbed from flac-luau 7083d0d (same author). Supports: every
-- subframe type, Rice / Rice2, 4-32 bits, 1-8 channels, CRC-16 resync, seeking, an ID3v2 prefix, the main metadata
-- blocks; encode levels 0-8 (libFLAC presets), 4-24 bits. Not supported: MD5 check on decode, chained Ogg, Ogg output.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/m4a: MPEG-4 audio (M4A / M4B / MP4 / MOV audio tracks) via containers/IsoBmff. Supports: AAC-LC (HE-AAC plays
-- its core), ALAC, edit-list gapless trimming, seeking, iTunes tags and cover art; encode ALAC (16 / 20 / 24-bit, 1-8
-- channels). Not supported: writing AAC, other codecs in MP4 (UNSUPPORTED), encrypted files.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/mp3: MPEG audio layers I-III (ISO/IEC 11172-3, 13818-3, MPEG-2.5); absorbed from mp3-luau c755bbf, a port of
-- minimp3 by lieff (CC0: licenses/minimp3-CC0.txt). Supports: every mode, CBR / VBR / free format, Xing / LAME gapless,
-- exact seeking, ID3v2 / ID3v1 tags; encode layers III / II (CBR). Not supported: MPEG-2 multichannel, CRC, VBR output.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/mpc: Musepack (.mpc / .mp+ / .mpp) SV7 and SV8 via codecs/audio/Musepack (a libmpcdec port). Supports: mono /
-- stereo, 32-48 kHz, gapless length, seeking (SV8 exact; SV7 with 32 frames of pre-roll, as libmpcdec), APEv2 tags,
-- an ID3v2 prefix, ReplayGain in meta. Not supported: writing, SV4-SV6 (UNSUPPORTED), chapters.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/opus/Encode: Ogg Opus writing (RFC 7845), CELT only (TOC config 31: full band, 20 ms, code 0), family 0.
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/opus: Ogg Opus (RFC 6716, RFC 7845); absorbed from opus-luau dd2b4e5, a port of the libopus 1.6 decoder (BSD:
-- licenses/libopus-BSD.txt). Supports: every mode / bandwidth / frame size, mapping families 0 / 1 / 255, seeking, PLC;
-- encode CELT only (20 ms, full band, CBR). Not supported: SILK / hybrid encoding, mapping family 3, chained streams.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/qoa: QOA "Quite OK Audio" via codecs/audio/Qoa (a port of qoa.h, phoboslab, MIT). Supports: static and
-- streaming files, 1-255 channels, rates up to 16777215 Hz, frame-accurate seeking; encode byte-for-byte as qoa_encode,
-- 1-8 channels. Not supported: channel count or rate changes mid-stream.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/tta: True Audio (.tta, TTA1) via codecs/audio/Tta, from the TTA1 format description. Supports: 8 / 16 / 24
-- bits, any channel count, seeking by the seek table, APEv2 tags, an ID3v2 prefix. Not supported: password-protected
-- files, TTA2, ID3v2 contents, encoding.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/vag: Sony VAG (PS-ADPCM: VAGp mono, VAGi stereo) and KVAG (IMA); layouts follow vgmstream r2117 (ISC,
-- licenses/vgmstream-LICENSE.txt), bit-exact, Sony's leading blank frame included. Supports: loop points; encode VAGp /
-- VAGi. Not supported: HEVAG (0x00020001 / 0x00030000), game-specific variants (pGAV, VAG1 / VAG2, Jak, ...).
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/voc: Creative Voice File (.voc), from Creative's format description. Supports: block types 1, 2, 3, 5, 8, 9
-- (8 / 16-bit, A-law, mu-law); markers and repeats played once, as ffmpeg; truncated files; encode version 1.20 type 9
-- blocks. Not supported: Creative ADPCM, channel count changes mid-file (the first block's rate is used).
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/vorbis: Ogg Vorbis (Vorbis I spec; RFC 5215); the decoder is a port of stb_vorbis 1.22 (public domain / MIT,
-- licenses/stb_vorbis-LICENSE.txt) with libvorbis' sample-exact trimming and seeking. Supports: 1-255 channels,
-- floor 1, residues 0-2, packetDecoder for Matroska / MP4; an encoder. Not supported: floor 0, chained streams.
+56 -43
View File
@@ -7,54 +7,60 @@ local readu8, readi8, readu16, readi16, readu32, readi32 =
local readf32, readf64, writef32, writeu32 = buffer.readf32, buffer.readf64, buffer.writef32, buffer.writeu32
local band, rshift, byteswap = bit32.band, bit32.rshift, bit32.byteswap
local writeu8, writeu16, writei16, writei32 = buffer.writeu8, buffer.writeu16, buffer.writei16, buffer.writei32
local floor = math.floor
local Codecs = {}
-- integer PCM: 1 byte = unsigned, 2 / 3 / 4 bytes = signed
function Codecs.int(src: buffer, off: number, dst: buffer, d: number, count: number, bytes: number, be: boolean)
local o = d * 4
local e = off + (count - 1) * bytes -- the last sample's offset
if bytes == 1 then
for i = 0, count - 1 do
writef32(dst, o + i * 4, (readu8(src, off + i) - 128) / 128)
for s = off, e do
writef32(dst, o, (readu8(src, s) - 128) / 128)
o += 4
end
elseif bytes == 2 then
if be then
for i = 0, count - 1 do
local s = off + i * 2
for s = off, e, 2 do
local v = readi8(src, s) * 256 + readu8(src, s + 1)
writef32(dst, o + i * 4, v / 32768)
writef32(dst, o, v / 32768)
o += 4
end
else
for i = 0, count - 1 do
writef32(dst, o + i * 4, readi16(src, off + i * 2) / 32768)
for s = off, e, 2 do
writef32(dst, o, readi16(src, s) / 32768)
o += 4
end
end
elseif bytes == 3 then
if be then
for i = 0, count - 1 do
local s = off + i * 3
for s = off, e, 3 do
local v = readi8(src, s) * 65536 + readu8(src, s + 1) * 256 + readu8(src, s + 2)
writef32(dst, o + i * 4, v / 8388608)
writef32(dst, o, v / 8388608)
o += 4
end
else
for i = 0, count - 1 do
local s = off + i * 3
for s = off, e, 3 do
local v = readi8(src, s + 2) * 65536 + readu16(src, s)
writef32(dst, o + i * 4, v / 8388608)
writef32(dst, o, v / 8388608)
o += 4
end
end
else
if be then
for i = 0, count - 1 do
local v = byteswap(readu32(src, off + i * 4))
for s = off, e, 4 do
local v = byteswap(readu32(src, s))
if v >= 0x80000000 then
v -= 0x100000000
end
writef32(dst, o + i * 4, v / 2147483648)
writef32(dst, o, v / 2147483648)
o += 4
end
else
for i = 0, count - 1 do
writef32(dst, o + i * 4, readi32(src, off + i * 4) / 2147483648)
for s = off, e, 4 do
writef32(dst, o, readi32(src, s) / 2147483648)
o += 4
end
end
end
@@ -63,28 +69,29 @@ end
function Codecs.float(src: buffer, off: number, dst: buffer, d: number, count: number, bytes: number, be: boolean,
tmp: buffer)
local o = d * 4
local e = off + (count - 1) * bytes
if bytes == 4 then
if be then
for i = 0, count - 1 do
writeu32(tmp, 0, byteswap(readu32(src, off + i * 4)))
writef32(dst, o + i * 4, readf32(tmp, 0))
for s = off, e, 4 do
writeu32(tmp, 0, byteswap(readu32(src, s)))
writef32(dst, o, readf32(tmp, 0))
o += 4
end
else
for i = 0, count - 1 do
writef32(dst, o + i * 4, readf32(src, off + i * 4))
end
buffer.copy(dst, o, src, off, count * 4)
end
else
if be then
for i = 0, count - 1 do
local s = off + i * 8
for s = off, e, 8 do
writeu32(tmp, 0, byteswap(readu32(src, s + 4)))
writeu32(tmp, 4, byteswap(readu32(src, s)))
writef32(dst, o + i * 4, readf64(tmp, 0))
writef32(dst, o, readf64(tmp, 0))
o += 4
end
else
for i = 0, count - 1 do
writef32(dst, o + i * 4, readf64(src, off + i * 8))
for s = off, e, 8 do
writef32(dst, o, readf64(src, s))
o += 4
end
end
end
@@ -92,8 +99,9 @@ end
function Codecs.law(src: buffer, off: number, dst: buffer, d: number, count: number, tbl: buffer)
local o = d * 4
for i = 0, count - 1 do
writef32(dst, o + i * 4, readf32(tbl, readu8(src, off + i) * 4))
for s = off, off + count - 1 do
writef32(dst, o, readf32(tbl, readu8(src, s) * 4))
o += 4
end
end
@@ -103,11 +111,13 @@ end
function Codecs.quantize(src: buffer, s: number, dst: buffer, off: number, count: number, kind: string)
local so = s * 4
local se = so + count * 4 - 4
local o = off
if kind == "f32" then
buffer.copy(dst, off, src, so, count * 4)
elseif kind == "s16" then
for i = 0, count - 1 do
local v = math.floor(readf32(src, so + i * 4) * 32768 + 0.5)
for i = so, se, 4 do
local v = floor(readf32(src, i) * 32768 + 0.5)
if v > 32767 then
v = 32767
elseif v < -32768 then
@@ -115,11 +125,12 @@ function Codecs.quantize(src: buffer, s: number, dst: buffer, off: number, count
elseif v ~= v then
v = 0
end
writei16(dst, off + i * 2, v)
writei16(dst, o, v)
o += 2
end
elseif kind == "s24" then
for i = 0, count - 1 do
local v = math.floor(readf32(src, so + i * 4) * 8388608 + 0.5)
for i = so, se, 4 do
local v = floor(readf32(src, i) * 8388608 + 0.5)
if v > 8388607 then
v = 8388607
elseif v < -8388608 then
@@ -130,13 +141,13 @@ function Codecs.quantize(src: buffer, s: number, dst: buffer, off: number, count
if v < 0 then
v += 16777216
end
local o = off + i * 3
writeu16(dst, o, v % 65536)
writeu8(dst, o + 2, v // 65536)
o += 3
end
elseif kind == "s32" then
for i = 0, count - 1 do
local v = math.floor(readf32(src, so + i * 4) * 2147483648 + 0.5)
for i = so, se, 4 do
local v = floor(readf32(src, i) * 2147483648 + 0.5)
if v > 2147483647 then
v = 2147483647
elseif v < -2147483648 then
@@ -144,11 +155,12 @@ function Codecs.quantize(src: buffer, s: number, dst: buffer, off: number, count
elseif v ~= v then
v = 0
end
writei32(dst, off + i * 4, v)
writei32(dst, o, v)
o += 4
end
else -- u8
for i = 0, count - 1 do
local v = math.floor(readf32(src, so + i * 4) * 128 + 0.5)
for i = so, se, 4 do
local v = floor(readf32(src, i) * 128 + 0.5)
if v > 127 then
v = 127
elseif v < -128 then
@@ -156,7 +168,8 @@ function Codecs.quantize(src: buffer, s: number, dst: buffer, off: number, count
elseif v ~= v then
v = 0
end
writeu8(dst, off + i, v + 128)
writeu8(dst, o, v + 128)
o += 1
end
end
end
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- audio/wavpack: WavPack (.wv) via codecs/audio/WavPack, from the WavPack 5 file format document and WavPack 5.9.0's
-- open_utils.c (BSD 3-clause, licenses/wavpack-LICENSE.txt). Supports: lossless and hybrid (lossy without the .wvc, as
-- wvunpack), 8-32-bit integer and float, 1-4096 channels, seeking, APEv2. Not supported: DSD, WavPack 1-3, encoding.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Aac/Tables: the AAC-LC tables of ISO/IEC 14496-3 (subpart 4, Annex 4.A): Huffman codebooks, scalefactor
-- band offsets, TNS band limits. Transcribed from FFmpeg libavcodec/aactab.c (tables of the standard, no code; see
-- licenses/NOTICE.md). Generated; do not edit.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Aac: AAC-LC decoder (ISO/IEC 14496-3 subpart 4, ISO/IEC 13818-7): AudioSpecificConfig, ADTS, raw_data_block.
-- Written from the standard; no third-party code. HE-AAC decodes its AAC-LC core at the core rate. Not supported: Main
-- prediction, LTP, SSR, coupling channels, 960-sample frames, ER / USAC (UNSUPPORTED).
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Ac3/Tables: transcribed from ATSC A/52:2018 section 7.2.3 (bit allocation tables) and section 5.4 (frame
-- sizes); masktab is derived from bndtab / bndsz. No third-party code.
local T = {}
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Ac3: AC-3 (Dolby Digital, ATSC A/52) syncframes: syncinfo / BSI, CRC check and decoder.
-- Written from ATSC A/52:2018; no third-party code. E-AC-3 (bsid 11 .. 16) and bsid 9 / 10 are UNSUPPORTED.
local Err = require("@rosetta/core/Err")
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Adpcm/Decoder: the streaming Types.Decoder over packetised ADPCM (ADX, VAG, BRSTM, raw IMA) with exact seeking.
-- New in rosetta.
local Err = require("@rosetta/core/Err")
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Alac: Apple Lossless (ALAC) decoder and encoder.
-- Written from the ALAC format as Apple published it; no third-party code. ALAC stores C L R ... for 3+ channels; the
-- API uses WAV order.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Ape: the Monkey's Audio codec (.ape), file versions 3.95 and later.
-- Ported from the Monkey's Audio SDK 13.28 (Matthew T. Ashland, BSD 3-clause since v10, licenses/monkeys-audio-LICENSE.txt):
-- APEHeader.cpp, APEInfo / APEDecompress.cpp. Not supported: pre-3.95 (UNSUPPORTED), the pre-8.50 "interim" 24-bit re-decode.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/ApeTag: APEv1 / APEv2 tags -> Types.Tags.
-- From the APEv2 specification (wiki.hydrogenaud.io); no third-party code. Items + a 32-byte "APETAGEX" footer, optional
-- 32-byte header (APEv2), before an optional ID3v1 tag.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Flac: FLAC frames without the container (frame header, subframes, CRC-8 / CRC-16) and the frame encoder.
-- Absorbed from flac-luau 7083d0d; the encoder is new.
local Flac = {
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Hively/Load: AHX ("THX\0".."THX\2") and HivelyTracker ("HVL\0", "HVL\1") module files -> Tune.
-- Ported from hvl2wav replay.c 1.9 (hvl_load_ahx / hvl_reset; BSD-3-Clause, (c) 2006-2018 Pete Gordon;
-- licenses/hivelytracker-LICENSE.txt), with bounds checks the C code does not have.
@@ -1,5 +1,6 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Hively/Player: the AHX / HivelyTracker replayer and integer mixer, as hvl2wav's replay.c 1.9 computes them.
-- Ported from HivelyTracker (BSD-3-Clause, (c) 2006-2018 Pete Gordon; licenses/hivelytracker-LICENSE.txt). Where the C
-- code reads outside its arrays on bad data this port clamps instead.
@@ -1,5 +1,6 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Hively/Tables: the AHX / HivelyTracker replayer's waveform, panning, vibrato and period tables.
-- Ported from hvl2wav replay.c 1.9 (BSD-3-Clause, (c) 2006-2018 Pete Gordon; licenses/hivelytracker-LICENSE.txt) as
-- built on 64-bit Linux (`long` is 64-bit: the noise generator's shifts do not wrap at 32 bits).
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Hively: the AHX / HivelyTracker engine (loader, replayer, waveform tables, render glue).
-- A port of hvl2wav replay.c 1.9 (BSD-3-Clause, (c) 2006-2018 Pete Gordon; licenses/hivelytracker-LICENSE.txt); at
-- hvl2wav's settings the output equals its 16-bit samples / 32768. hvl2wav pads a 44.1 kHz speed-multiplier-4 frame to
+53 -35
View File
@@ -83,76 +83,94 @@ end
local function pass(p: number, ido: number, l1: number, ar: { number }, ai: { number }, br: { number }, bi: { number }, tr: { number }, ti: { number }, cr: { number }, ci: { number })
local lp = ido * l1
if p == 4 then
local ido2, ido3, lp2, lp3 = 2 * ido, 3 * ido, 2 * lp, 3 * lp
for k = 0, l1 - 1 do
for i = 0, ido - 1 do
local s = 1 + i + ido * 4 * k
local s = 1 + ido * 4 * k
local d = 1 + ido * k
do -- i = 0: no twiddle
local a0r, a0i = ar[s], ai[s]
local a1r, a1i = ar[s + ido], ai[s + ido]
local a2r, a2i = ar[s + 2 * ido], ai[s + 2 * ido]
local a3r, a3i = ar[s + 3 * ido], ai[s + 3 * ido]
local a2r, a2i = ar[s + ido2], ai[s + ido2]
local a3r, a3i = ar[s + ido3], ai[s + ido3]
local t0r, t0i = a0r + a2r, a0i + a2i
local t1r, t1i = a0r - a2r, a0i - a2i
local t2r, t2i = a1r + a3r, a1i + a3i
local t3r, t3i = a1r - a3r, a1i - a3i
br[d], bi[d] = t0r + t2r, t0i + t2i
br[d + lp], bi[d + lp] = t1r + t3i, t1i - t3r
br[d + lp2], bi[d + lp2] = t0r - t2r, t0i - t2i
br[d + lp3], bi[d + lp3] = t1r - t3i, t1i + t3r
end
for i1 = 2, ido do -- i = i1 - 1
s += 1
d += 1
local a0r, a0i = ar[s], ai[s]
local a1r, a1i = ar[s + ido], ai[s + ido]
local a2r, a2i = ar[s + ido2], ai[s + ido2]
local a3r, a3i = ar[s + ido3], ai[s + ido3]
local t0r, t0i = a0r + a2r, a0i + a2i
local t1r, t1i = a0r - a2r, a0i - a2i
local t2r, t2i = a1r + a3r, a1i + a3i
local t3r, t3i = a1r - a3r, a1i - a3i
local d = 1 + i + ido * k
br[d], bi[d] = t0r + t2r, t0i + t2i
local y1r, y1i = t1r + t3i, t1i - t3r
local y2r, y2i = t0r - t2r, t0i - t2i
local y3r, y3i = t1r - t3i, t1i + t3r
if i == 0 then
br[d + lp], bi[d + lp] = y1r, y1i
br[d + 2 * lp], bi[d + 2 * lp] = y2r, y2i
br[d + 3 * lp], bi[d + 3 * lp] = y3r, y3i
else
local w1r, w1i = tr[i + 1], ti[i + 1]
local w2r, w2i = tr[ido + i + 1], ti[ido + i + 1]
local w3r, w3i = tr[2 * ido + i + 1], ti[2 * ido + i + 1]
br[d + lp], bi[d + lp] = y1r * w1r - y1i * w1i, y1r * w1i + y1i * w1r
br[d + 2 * lp], bi[d + 2 * lp] = y2r * w2r - y2i * w2i, y2r * w2i + y2i * w2r
br[d + 3 * lp], bi[d + 3 * lp] = y3r * w3r - y3i * w3i, y3r * w3i + y3i * w3r
end
local w1r, w1i = tr[i1], ti[i1]
local w2r, w2i = tr[ido + i1], ti[ido + i1]
local w3r, w3i = tr[ido2 + i1], ti[ido2 + i1]
br[d + lp], bi[d + lp] = y1r * w1r - y1i * w1i, y1r * w1i + y1i * w1r
br[d + lp2], bi[d + lp2] = y2r * w2r - y2i * w2i, y2r * w2i + y2i * w2r
br[d + lp3], bi[d + lp3] = y3r * w3r - y3i * w3i, y3r * w3i + y3i * w3r
end
end
elseif p == 2 then
for k = 0, l1 - 1 do
for i = 0, ido - 1 do
local s = 1 + i + ido * 2 * k
local s = 1 + ido * 2 * k
local d = 1 + ido * k
do
local a0r, a0i = ar[s], ai[s]
local a1r, a1i = ar[s + ido], ai[s + ido]
br[d], bi[d] = a0r + a1r, a0i + a1i
br[d + lp], bi[d + lp] = a0r - a1r, a0i - a1i
end
for i1 = 2, ido do
s += 1
d += 1
local a0r, a0i = ar[s], ai[s]
local a1r, a1i = ar[s + ido], ai[s + ido]
local d = 1 + i + ido * k
br[d], bi[d] = a0r + a1r, a0i + a1i
local yr, yi = a0r - a1r, a0i - a1i
if i == 0 then
br[d + lp], bi[d + lp] = yr, yi
else
local wr, wi = tr[i + 1], ti[i + 1]
br[d + lp], bi[d + lp] = yr * wr - yi * wi, yr * wi + yi * wr
end
local wr, wi = tr[i1], ti[i1]
br[d + lp], bi[d + lp] = yr * wr - yi * wi, yr * wi + yi * wr
end
end
elseif p == 3 then
local c = 0.86602540378443864676 -- sin(pi / 3)
local ido2, lp2 = 2 * ido, 2 * lp
for k = 0, l1 - 1 do
for i = 0, ido - 1 do
local s = 1 + i + ido * 3 * k
local s = ido * 3 * k
local d = ido * k
for i1 = 1, ido do
s += 1
d += 1
local a0r, a0i = ar[s], ai[s]
local a1r, a1i = ar[s + ido], ai[s + ido]
local a2r, a2i = ar[s + 2 * ido], ai[s + 2 * ido]
local a2r, a2i = ar[s + ido2], ai[s + ido2]
local tr_, ti_ = a1r + a2r, a1i + a2i
local sr, si = a1r - a2r, a1i - a2i
local d = 1 + i + ido * k
br[d], bi[d] = a0r + tr_, a0i + ti_
local mr, mi = a0r - 0.5 * tr_, a0i - 0.5 * ti_
local y1r, y1i = mr + c * si, mi - c * sr
local y2r, y2i = mr - c * si, mi + c * sr
if i == 0 then
if i1 == 1 then
br[d + lp], bi[d + lp] = y1r, y1i
br[d + 2 * lp], bi[d + 2 * lp] = y2r, y2i
br[d + lp2], bi[d + lp2] = y2r, y2i
else
local w1r, w1i = tr[i + 1], ti[i + 1]
local w2r, w2i = tr[ido + i + 1], ti[ido + i + 1]
local w1r, w1i = tr[i1], ti[i1]
local w2r, w2i = tr[ido + i1], ti[ido + i1]
br[d + lp], bi[d + lp] = y1r * w1r - y1i * w1i, y1r * w1i + y1i * w1r
br[d + 2 * lp], bi[d + 2 * lp] = y2r * w2r - y2i * w2i, y2r * w2i + y2i * w2r
br[d + lp2], bi[d + lp2] = y2r * w2r - y2i * w2i, y2r * w2i + y2i * w2r
end
end
end
+35 -9
View File
@@ -108,16 +108,42 @@ function Mdct.forward(plan: Plan, x: { number }, xi: number, out: { number }, oi
end
function Mdct.inverse(plan: Plan, inp: { number }, ii: number, is: number?, y: { number }, yi: number, scale: number?)
local m = plan.m
local q, m = plan.n // 4, plan.m
local h = m // 2
local c = plan.c
Mdct.dct4(plan, inp, ii, is or 1, c, 1, 1, scale)
-- unfold: y = (C2, -C2 reversed, -C1 reversed, -C1)
for j = 0, h - 1 do
y[yi + j] = c[h + j + 1]
y[yi + h + j] = -c[m - j]
y[yi + m + j] = -c[h - j]
y[yi + 3 * h + j] = -c[j + 1]
local re, im, twc, tws = plan.re, plan.im, plan.twc, plan.tws
local us = is or 1
local sc = scale or 1
for p = 0, q - 1 do
local a, b = inp[ii + 2 * p * us], inp[ii + (m - 1 - 2 * p) * us]
local c, s = twc[p + 1], tws[p + 1]
re[p + 1] = a * c + b * s
im[p + 1] = b * c - a * s
end
Fft.forward(plan.fft, re, im)
-- the DCT-IV output C[j] (j = 2k and M - 1 - 2k) unfolded into y = (C2, -C2 reversed, -C1 reversed, -C1):
-- y[3h - 1 - j] = -C[j]; y[3h + j] = -C[j] (j < h), y[j - h] = C[j] (j >= h)
local b3 = yi + 3 * h
for k = 0, q - 1 do
local c, s = twc[k + 1], tws[k + 1]
local tr, ti = re[k + 1], im[k + 1]
local e = (tr * c + ti * s) * sc -- C[2k]
local o = -(ti * c - tr * s) * sc -- C[M - 1 - 2k]
local j = 2 * k
if j < h then
y[b3 - 1 - j] = -e
y[b3 + j] = -e
else
y[j - h + yi] = e
y[b3 - 1 - j] = -e
end
j = m - 1 - j
if j < h then
y[b3 - 1 - j] = -o
y[b3 + j] = -o
else
y[j - h + yi] = o
y[b3 - 1 - j] = -o
end
end
end
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/MidiRender: the MIDI tempo map, event timeline and the Decoder that plays a Midi through SoundFont.
-- Lengths as python mido computes them. Format 0 / 1: one tempo map from every track, tracks merged (ties keep track
-- order); format 2: each track is its own song, played one after another.
+20
View File
@@ -156,6 +156,7 @@ end
-- layer III side info (L3_read_side_info): fills GR_*, returns main_data_begin or -1
local function readSideInfo(h1: number, h2: number, h3: number): number
local GR_NLONG, GR_NSHORT, GR_RC, GR_SBG, GR_SFB, GR_TS = GR_NLONG, GR_NSHORT, GR_RC, GR_SBG, GR_SFB, GR_TS
local mono = band(h3, 0xC0) == 0xC0
local mpeg1 = band(h1, 8) ~= 0
local srIdx = band(rshift(h2, 2), 3) + (band(rshift(h1, 3), 1) + band(rshift(h1, 4), 1)) * 3
@@ -254,6 +255,7 @@ end
-- L3_read_scalefactors + L3_decode_scalefactors -> SCF[0 .. nLong + nShort)
local function decodeScalefactors(h1: number, h3: number, istOff: number, gr: number, ch: number)
local GR_SBG, ISCF, IST, SCFSIZE = GR_SBG, ISCF, IST, SCFSIZE
local mpeg1 = band(h1, 8) ~= 0
local nLong, nShort = GR_NLONG[gr], GR_NSHORT[gr]
local part = ((if nShort ~= 0 then 1 else 0) + (if nLong == 0 then 1 else 0)) * 28
@@ -335,6 +337,7 @@ end
-- layer III Huffman decoding (L3_huffman) into GRBUF[dst ..], reads MAIN through a 32-bit cache
local function huffman(dst: number, gr: number, limit: number)
local GRBUF, SCF, SFB = GRBUF, SCF, SFB
local b = bsBuf
local one = 0.0
local ireg = 0
@@ -482,6 +485,7 @@ end
-- L3_midside_stereo on GRBUF[left .. left + n) and its right channel at + 576
local function midside(left: number, n: number)
local GRBUF = GRBUF
for i = left * 4, (left + n - 1) * 4, 4 do
local a, c = rf(GRBUF, i), rf(GRBUF, i + 2304)
wf(GRBUF, i, a + c)
@@ -490,6 +494,7 @@ local function midside(left: number, n: number)
end
local function intensityStereo(h1: number, h3: number, gr: number)
local GRBUF, IST, MAXB = GRBUF, IST, MAXB
local mpeg1 = band(h1, 8) ~= 0
local ms = band(h3, 0x20) ~= 0
local nLong, nShort = GR_NLONG[gr], GR_NSHORT[gr]
@@ -559,6 +564,7 @@ local function intensityStereo(h1: number, h3: number, gr: number)
end
local function reorder(g: number, sfb: number)
local GRBUF, SYN = GRBUF, SYN
local src, dst = g, 0
while true do
local len = ru8(SFB, sfb)
@@ -586,6 +592,7 @@ for i = 0, 7 do
end
local function antialias(g: number, nbands: number)
local GRBUF = GRBUF
local o = g * 4
for _ = 1, nbands do
for i = 0, 7 do
@@ -627,6 +634,7 @@ end
-- L3_imdct36: GRBUF[g ..], OVL[ov ..] (float indices), window W (1-based, 18 values)
local function imdct36(g: number, ov: number, W: { number }, nbands: number)
local GRBUF = GRBUF
local w0 = W[1]
local w1 = W[2]
local w2 = W[3]
@@ -739,6 +747,7 @@ end
-- mp3d_DCT_II (scalar path) over the n time slots of a channel at GRBUF[g ..]: t[4][8] in locals
local function dctII(g: number, n: number)
local GRBUF = GRBUF
for k = 0, n - 1 do
local y = (g + k) * 4
local t0_0, t1_0, t2_0, t3_0
@@ -989,6 +998,7 @@ end
-- L3_imdct12: x = TMP f32 index (stride 3), dst buffer + float index, ov = OVL float index
local function imdct12(x: number, dst: buffer, d: number, ov: number)
local TMP = TMP
local xb = x * 4
local x0, x3, x6, x9, x12, x15 =
rf(TMP, xb), rf(TMP, xb + 12), rf(TMP, xb + 24), rf(TMP, xb + 36), rf(TMP, xb + 48), rf(TMP, xb + 60)
@@ -1017,6 +1027,7 @@ local function imdct12(x: number, dst: buffer, d: number, ov: number)
end
local function imdctShort(g: number, ov: number, nbands: number)
local GRBUF = GRBUF
for _ = 1, nbands do
bcopy(TMP, 0, GRBUF, g * 4, 72)
bcopy(GRBUF, g * 4, OVL, ov * 4, 24)
@@ -1085,6 +1096,7 @@ end
-- mp3d_synth_pair: z = SYN float index, dst = PCM float index
local function synthPair(dst: number, nch: number, z: number)
local SYN = SYN
local zb = z * 4
local a = (rf(SYN, zb + 14 * 256) - rf(SYN, zb)) * 29
a += (rf(SYN, zb + 1 * 256) + rf(SYN, zb + 13 * 256)) * 213
@@ -1109,6 +1121,7 @@ end
-- mp3d_synth: two time slots of 32 subbands -> 64 samples per channel
local function synth(xl: number, dstl: number, nch: number, lins: number)
local GRBUF, SYN, WINT = GRBUF, SYN, WINT
local xr = xl + 576 * (nch - 1)
local dstr = dstl + (nch - 1)
local zlin = lins + 15 * 64
@@ -1216,6 +1229,7 @@ local function synth(xl: number, dstl: number, nch: number, lins: number)
end
local function synthGranule(nbands: number, nch: number, pcm: number)
local SYN = SYN
for ch = 0, nch - 1 do
dctII(576 * ch, nbands)
end
@@ -1241,6 +1255,7 @@ local ALLOC_L2M1_LOW = { 44, 4, 2, 44, 3, 10 }
local totalBands, stereoBands = 0, 0
local function l12ReadScaleInfo(h1: number, h2: number, h3: number, kbpsHdr: number)
local BA = BA
local mode = rshift(h3, 6)
local sb = if mode == 3 then 0 elseif mode == 1 then band(rshift(h3, 4), 3) * 4 + 4 else 32
local layer1 = band(h1, 6) == 6
@@ -1331,6 +1346,7 @@ local function l12Dequantize(g: number, groupSize: number): number
end
local function l12ApplyScf(scfOff: number)
local GRBUF = GRBUF
local sb, tb = stereoBands, totalBands
bcopy(GRBUF, (576 + sb * 18) * 4, GRBUF, sb * 18 * 4, (tb - sb) * 18 * 4)
for i = 0, tb - 1 do
@@ -1389,6 +1405,7 @@ function Core.upmix(pcm: buffer, n: number)
end
function Core.decodeFrame(st: State, b: buffer, off: number, size: number, pcm: buffer): number
local FRAME, GRBUF, MAIN = FRAME, GRBUF, MAIN
if size < 4 or size > MAX_FRAME or off < 0 or off + size > buffer.len(b) then
st.needReset = true
return 0
@@ -1476,6 +1493,7 @@ function Core.decodeFrame(st: State, b: buffer, off: number, size: number, pcm:
end
function Core.probeL3(b: buffer, off: number, size: number): (number, number, number)
local FRAME = FRAME
local n = min(size, 64)
if size < 4 or off + n > buffer.len(b) then
return -1, 0, 0
@@ -1500,6 +1518,7 @@ end
-- Layer I / II: minimp3 drops a frame that needs more bits than it has
function Core.probeL12(b: buffer, off: number, size: number): boolean
local FRAME = FRAME
if size < 4 or size > MAX_FRAME or off + size > buffer.len(b) then
return false
end
@@ -1533,6 +1552,7 @@ end
-- mp3dec_check_vbrtag on the frame b[off, off + size): 0 = no tag, -1 = a Xing / Info tag without a frame
-- count, 1 = tag; then frames, encoder delay + 529, padding - 529 (both 0 without a LAME extension)
function Core.vbrTag(b: buffer, off: number, size: number): (number, number, number, number)
local FRAME = FRAME
if size < 12 or size > MAX_FRAME or off + size > buffer.len(b) then
return 0, 0, 0, 0
end
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Mpa/EncTables: the synthesis window D and the layer III Huffman trees of ISO/IEC 11172-3, from PDMP3
-- (public domain / unlicense, Krister Lagerstrom and contributors: licenses/PDMP3-UNLICENSE.txt). Tree entries with a
-- high byte are nodes (high byte: left offset, low byte: right offset), else leaves (x << 4 | y; count1: v w x y bits).
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Mpa/Tables: constant tables of minimp3 (https://github.com/lieff/minimp3, CC0), generated by tools/gen_tables.py.
-- Every list is flattened row-major and 0-based (t[0] is the first element), like the C arrays.
local T = {}
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Mpa: MPEG audio layers I / II / III (ISO/IEC 11172-3, 13818-3, MPEG-2.5) without the file around it.
-- Core is a port of minimp3 by lieff (CC0, licenses/minimp3-CC0.txt), absorbed from mp3-luau c755bbf; the layer II / III
-- encoders are new.
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Musepack/Tables: the Musepack SV7 / SV8 Huffman tables, synthesis window and requantisation constants.
-- Ported from libmpcdec (musepack_src_r475: huffman.c, synth_filter.c, requant.c, mpc_bits_reader.c; BSD-3-Clause,
-- (c) The Musepack Development Team, licenses/libmpcdec-BSD.txt). Huffman tables: flat { code, length, value, ... }
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Musepack: Musepack (MPC) stream versions 7 and 8.
-- A port of libmpcdec (musepack_src_r475: mpc_demux.c, streaminfo.c, mpc_decoder.c; BSD-3-Clause, (c) The Musepack
-- Development Team, licenses/libmpcdec-BSD.txt), gapless like libmpcdec. SV4 - SV6 are UNSUPPORTED.
+23
View File
@@ -193,6 +193,7 @@ local function laplaceDecode(dec: RD.Dec, fs: number, decay: number): number
end
local function unquantCoarseEnergy(st: any, start: number, endb: number, intra: boolean, dec: RD.Dec, C: number, LM: number)
local E_PROB = E_PROB
local oldE = st.oldBandE
local probBase = LM * 84 + (if intra then 42 else 0) -- e_prob_model[LM][intra], 0-based
local coef, beta
@@ -276,6 +277,7 @@ end
-- bit allocation (rate.c)
local function bits2pulses(bandI: number, LM: number, bits: number): number
local CACHE_BITS = CACHE_BITS
local base = CACHE_INDEX[(LM + 1) * NB_EBANDS + bandI + 1]
local lo, hi = 0, CACHE_BITS[base + 1]
bits -= 1
@@ -312,6 +314,7 @@ end
-- clt_compute_allocation + interp_bits2pulses (decoder side)
local function computeAllocation(start: number, endb: number, offsets: { number }, cap: { number }, allocTrim: number,
total: number, pulses: { number }, ebits: { number }, finePriority: { number }, C: number, LM: number, ec: RD.Dec)
local ALLOC, EB, LOG2_FRAC_TABLE = ALLOC, EB, LOG2_FRAC_TABLE
total = imax(total, 0)
local len = NB_EBANDS
local skipStart = start
@@ -541,6 +544,7 @@ end
-- PVQ (cwrs.c, vq.c)
local function cwrsi(n: number, k: number, i: number, iy: { number }): number
local U = U
local yy = 0
local idx = 1
local p, k0, val
@@ -618,6 +622,7 @@ local function cwrsi(n: number, k: number, i: number, iy: { number }): number
end
local function expRotation1(X: number, len: number, stride: number, c: number, s: number)
local W = W
local ms = -s
local p = X + 1
for _ = 0, len - stride - 1 do
@@ -672,6 +677,7 @@ local function expRotation(X: number, len: number, dir: number, stride: number,
end
local function renormaliseVector(X: number, N: number, gain: number)
local W = W
local E = 1e-15
for i = X + 1, X + N do
local v = W[i]
@@ -684,6 +690,7 @@ local function renormaliseVector(X: number, N: number, gain: number)
end
local function algUnquant(X: number, N: number, K: number, spread: number, B: number, dec: RD.Dec, gain: number): number
local U = U
local iy = IY
local ft = U[N * UK + K + 1] + U[N * UK + K + 2]
local Ryy = cwrsi(N, K, decUint(dec, ft), iy)
@@ -734,6 +741,7 @@ local ctx: Ctx = {
}
local function haar1(X: number, N0: number, stride: number)
local W = W
N0 = rshift(N0, 1)
local s = 0.70710678
for i = 0, stride - 1 do
@@ -749,6 +757,7 @@ local function haar1(X: number, N0: number, stride: number)
end
local function deinterleaveHadamard(X: number, N0: number, stride: number, hadamard: boolean)
local W = W
local N = N0 * stride
local tmp = TMP
if hadamard then
@@ -772,6 +781,7 @@ local function deinterleaveHadamard(X: number, N0: number, stride: number, hadam
end
local function interleaveHadamard(X: number, N0: number, stride: number, hadamard: boolean)
local W = W
local N = N0 * stride
local tmp = TMP
if hadamard then
@@ -810,6 +820,7 @@ local function computeQn(N: number, b: number, offset: number, pulseCap: number,
end
local function computeTheta(N: number, b: number, B: number, B0: number, LM: number, stereo: boolean, fill: number)
local ctx = ctx
local i = ctx.i
local ec = ctx.ec
local pulseCap = LOGN[i + 1] + LM * 8
@@ -889,6 +900,7 @@ local function computeTheta(N: number, b: number, B: number, B0: number, LM: num
end
local function quantBandN1(X: number, Y: number, lowbandOut: number): number
local W, ctx = W, ctx
local x = X
local ec = ctx.ec
for _ = 0, (if Y >= 0 then 1 else 0) do
@@ -907,6 +919,7 @@ local function quantBandN1(X: number, Y: number, lowbandOut: number): number
end
local function quantPartition(X: number, N: number, b: number, B: number, lowband: number, LM: number, gain: number, fill: number): number
local CACHE_BITS, W, ctx = CACHE_BITS, W, ctx
local i = ctx.i
local B0 = B
local cm = 0
@@ -999,6 +1012,7 @@ end
local function quantBand(X: number, N: number, b: number, B: number, lowband: number, LM: number, lowbandOut: number,
gain: number, lowbandScratch: number, fill: number): number
local BIT_INTERLEAVE, W = BIT_INTERLEAVE, W
local N0 = N
local NB = N
local B0 = B
@@ -1069,6 +1083,7 @@ local function quantBand(X: number, N: number, b: number, B: number, lowband: nu
end
local function stereoMerge(X: number, Y: number, mid: number, N: number)
local W = W
local xp, side = 0, 0
for j = 1, N do
local y = W[Y + j]
@@ -1097,6 +1112,7 @@ end
local function quantBandStereo(X: number, Y: number, N: number, b: number, B: number, lowband: number, LM: number,
lowbandOut: number, lowbandScratch: number, fill: number): number
local W, ctx = W, ctx
if N == 1 then
return quantBandN1(X, Y, lowbandOut)
end
@@ -1169,6 +1185,7 @@ local function quantAllBands(start: number, endb: number, hasY: boolean, N: numb
pulses: { number }, shortBlocks: boolean, spread: number, dualStereo: boolean, intensity: number,
tfRes: { number }, totalBits: number, balance: number, ec: RD.Dec, LM: number, codedBands: number,
seed: number, disableInv: boolean): number
local EB, W, ctx = EB, W, ctx
local M = lshift(1, LM)
local B = if shortBlocks then M else 1
local normOffset = M * EB[start + 1]
@@ -1278,6 +1295,7 @@ end
local function antiCollapse(st: any, C: number, size: number, LM: number, start: number, endb: number,
collapse: { number }, pulses: { number }, seed: number)
local EB = EB
local logE, prev1logE, prev2logE = st.oldBandE, st.oldLogE, st.oldLogE2
for i = start, endb - 1 do
local N0 = EB[i + 2] - EB[i + 1]
@@ -1318,6 +1336,7 @@ local function antiCollapse(st: any, C: number, size: number, LM: number, start:
end
local function denormaliseBands(X: number, freq: { number }, logE: { number }, eoff: number, start: number, endb: number, M: number, silence: boolean)
local EB = EB
local N = M * SHORT_MDCT
local bound = M * EB[endb + 1]
if silence then
@@ -1349,6 +1368,7 @@ local function denormaliseBands(X: number, freq: { number }, logE: { number }, e
end
local function celtSynthesis(st: any, C: number, CC: number, start: number, effEnd: number, isTransient: boolean, LM: number, silence: boolean, N: number)
local FREQ2, WINDOW = FREQ2, WINDOW
local M = lshift(1, LM)
local B, NB, shift
if isTransient then
@@ -1396,6 +1416,7 @@ end
-- comb_filter, in place on mem (1-based base index of sample 0)
local function combFilter(mem: { number }, base: number, T0: number, T1: number, N: number, g0: number, g1: number,
tapset0: number, tapset1: number, overlap: number)
local COMB_GAINS = COMB_GAINS
if g0 == 0 and g1 == 0 then
return
end
@@ -1527,6 +1548,7 @@ Celt.reset = resetState
-- noise-based concealment (celt_decode_lost, FRAME_PLC_NOISE)
local function decodeLost(st: any, N: number, LM: number)
local EB = EB
local C = st.channels
local start = st.start
local endb = st["end"]
@@ -1582,6 +1604,7 @@ end
-- celt_decode_with_ec (buf nil or len <= 1 conceals)
function Celt.decode(st: any, buf: buffer?, off: number, len: number, pcm: { number }, pbase: number, frameSize: number,
dec: RD.Dec?, accum: boolean): number
local EB = EB
local CC = st.channels
local C = st.stream_channels
local start = st.start
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Opus/CeltTables: from libopus celt/static_modes_float.h and celt/quant_bands.c (BSD, licenses/).
return {
logN400 = {
+39 -22
View File
@@ -1316,35 +1316,52 @@ local function decodeCore(ch: any, ctrl: any, xq: { number }, xo: number, pulses
A_Q12_tmp[6], A_Q12_tmp[7], A_Q12_tmp[8], A_Q12_tmp[9], A_Q12_tmp[10]
local wide = order == 16
local a10, a11, a12, a13, a14, a15 = A_Q12_tmp[11], A_Q12_tmp[12], A_Q12_tmp[13], A_Q12_tmp[14], A_Q12_tmp[15], A_Q12_tmp[16]
-- the last 16 sLPC_Q14 values in locals, newest first (s1 = sLPC_Q14[MAX_LPC_ORDER + i - 1] in C)
local s1, s2, s3, s4, s5, s6, s7, s8 = sLPC_Q14[16], sLPC_Q14[15], sLPC_Q14[14], sLPC_Q14[13], sLPC_Q14[12], sLPC_Q14[11], sLPC_Q14[10], sLPC_Q14[9]
local s9, s10, s11, s12, s13, s14, s15, s16 = sLPC_Q14[8], sLPC_Q14[7], sLPC_Q14[6], sLPC_Q14[5], sLPC_Q14[4], sLPC_Q14[3], sLPC_Q14[2], sLPC_Q14[1]
local half = order // 2
for i = 1, subfr_length do
local j = 16 + i -- 1-based index of sLPC_Q14[MAX_LPC_ORDER + i - 1 (C)] + 1
local pred = order // 2
pred += (sLPC_Q14[j - 1] * a0) // 65536
pred += (sLPC_Q14[j - 2] * a1) // 65536
pred += (sLPC_Q14[j - 3] * a2) // 65536
pred += (sLPC_Q14[j - 4] * a3) // 65536
pred += (sLPC_Q14[j - 5] * a4) // 65536
pred += (sLPC_Q14[j - 6] * a5) // 65536
pred += (sLPC_Q14[j - 7] * a6) // 65536
pred += (sLPC_Q14[j - 8] * a7) // 65536
pred += (sLPC_Q14[j - 9] * a8) // 65536
pred += (sLPC_Q14[j - 10] * a9) // 65536
local pred = half
pred += (s1 * a0) // 65536
pred += (s2 * a1) // 65536
pred += (s3 * a2) // 65536
pred += (s4 * a3) // 65536
pred += (s5 * a4) // 65536
pred += (s6 * a5) // 65536
pred += (s7 * a6) // 65536
pred += (s8 * a7) // 65536
pred += (s9 * a8) // 65536
pred += (s10 * a9) // 65536
if wide then
pred += (sLPC_Q14[j - 11] * a10) // 65536
pred += (sLPC_Q14[j - 12] * a11) // 65536
pred += (sLPC_Q14[j - 13] * a12) // 65536
pred += (sLPC_Q14[j - 14] * a13) // 65536
pred += (sLPC_Q14[j - 15] * a14) // 65536
pred += (sLPC_Q14[j - 16] * a15) // 65536
pred += (s11 * a10) // 65536
pred += (s12 * a11) // 65536
pred += (s13 * a12) // 65536
pred += (s14 * a13) // 65536
pred += (s15 * a14) // 65536
pred += (s16 * a15) // 65536
end
pred = i32(pred)
local v = addSat32(presArr[presOff + i], lshiftSat32(pred, 4))
sLPC_Q14[j] = v
s16 = s15
s15 = s14
s14 = s13
s13 = s12
s12 = s11
s11 = s10
s10 = s9
s9 = s8
s8 = s7
s7 = s6
s6 = s5
s5 = s4
s4 = s3
s3 = s2
s2 = s1
s1 = v
xq[pxq + i - 1] = sat16((mulww(v, Gain_Q10) + 128) // 256)
end
for i = 1, 16 do
sLPC_Q14[i] = sLPC_Q14[subfr_length + i]
end
sLPC_Q14[16], sLPC_Q14[15], sLPC_Q14[14], sLPC_Q14[13], sLPC_Q14[12], sLPC_Q14[11], sLPC_Q14[10], sLPC_Q14[9] = s1, s2, s3, s4, s5, s6, s7, s8
sLPC_Q14[8], sLPC_Q14[7], sLPC_Q14[6], sLPC_Q14[5], sLPC_Q14[4], sLPC_Q14[3], sLPC_Q14[2], sLPC_Q14[1] = s9, s10, s11, s12, s13, s14, s15, s16
pexc += subfr_length
pxq += subfr_length
end
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Opus: the Opus codec (RFC 6716) without its container.
-- A port of the libopus 1.6 decoder (BSD, licenses/libopus-BSD.txt), absorbed from opus-luau dd2b4e5; the CELT encoder is new.
local Opus = {
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Pcm: PCM plumbing shared by audio formats: raw sample formats, streaming Decoders, encoder input sources.
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
+70 -34
View File
@@ -105,7 +105,7 @@ type State = {
f: Filter,
step: number, -- M = from / g: the input advance per output frame, in units of 1 / L
L: number, -- to / g
hist: { number },
hist: { { number } }, -- per channel: input frame base + i - 1 at [i]
histFrames: number,
base: number,
inFrames: number,
@@ -115,6 +115,16 @@ type State = {
flush: any,
}
-- dot(x, xi, h, hi, n): sum of x[xi + k] * h[hi + k] for k = 0 .. n - 1, in that order
local function dot(x: { number }, xi: number, h: { number }, hi: number, n: number): number
local acc = 0
local d = xi - hi
for k = hi, hi + n - 1 do
acc += x[k + d] * h[k]
end
return acc
end
local function run(st: State, limitOut: number, out: buffer?): (buffer, number)
local f = st.f
local taps, half, h = f.taps, f.half, f.h
@@ -140,61 +150,78 @@ local function run(st: State, limitOut: number, out: buffer?): (buffer, number)
local b = if out and buffer.len(out) >= bytes then out else buffer.create(bytes)
local exact = f.exact
local phases = f.phases
local x0, x1 = hist[1], hist[2]
local o = 0
for j = j0, jmax - 1 do
local num = j * M
local ip = num // L -- floor(t)
local first = ip - half + 1
local rel = first - base
local num = j0 * M
local ip = num // L -- floor(t)
local ph = num - ip * L -- num mod L
for _ = j0, jmax - 1 do
local rel = ip - half + 1 - base
local kLo = if rel < 0 then -rel else 0
local kHi = taps - 1
if rel + kHi >= histFrames then
kHi = histFrames - 1 - rel
end
local cnt = kHi - kLo + 1
local xi = rel + kLo + 1
if exact then
local hb = (num - ip * L) * taps + 1 -- phase * taps + 1
for c = 0, ch - 1 do
local acc = 0
local hi = (rel + kLo) * ch + c + 1
for k = kLo, kHi do
acc += hist[hi] * h[hb + k]
hi += ch
local hb = ph * taps + kLo + 1
if ch == 2 then
local a0, a1 = 0, 0
local d = xi - hb
for k = hb, hb + cnt - 1 do
local hk = h[k]
a0 += x0[k + d] * hk
a1 += x1[k + d] * hk
end
writef32(b, o, acc)
writef32(b, o, a0)
writef32(b, o + 4, a1)
o += 8
elseif ch == 1 then
writef32(b, o, dot(x0, xi, h, hb, cnt))
o += 4
else
for c = 1, ch do
writef32(b, o, dot(hist[c], xi, h, hb, cnt))
o += 4
end
end
else
local fp = (num - ip * L) / L * phases
local fp = ph / L * phases
local p = floor(fp)
local a = fp - p
local hb = p * taps + 1
local hb = p * taps + kLo + 1
local hb2 = hb + taps
for c = 0, ch - 1 do
local acc, acc2 = 0, 0
local hi = (rel + kLo) * ch + c + 1
for k = kLo, kHi do
local x = hist[hi]
acc += x * h[hb + k]
acc2 += x * h[hb2 + k]
hi += ch
end
for c = 1, ch do
local acc = dot(hist[c], xi, h, hb, cnt)
local acc2 = dot(hist[c], xi, h, hb2, cnt)
writef32(b, o, acc + (acc2 - acc) * a)
o += 4
end
end
ph += M
if ph >= L then
local q = ph // L
ip += q
ph -= q * L
end
end
st.outPos = jmax
local keepFrom = (jmax * M) // L - half + 1
local drop = keepFrom - base
if drop > 0 then
if drop >= histFrames then
st.hist = {}
for c = 1, ch do
hist[c] = {}
end
st.histFrames = 0
st.base = base + histFrames
else
local nh: { number } = table.create((histFrames - drop) * ch) :: any
table.move(hist, drop * ch + 1, histFrames * ch, 1, nh)
st.hist = nh
for c = 1, ch do
local nh: { number } = table.create(histFrames - drop) :: any
table.move(hist[c], drop + 1, histFrames, 1, nh)
hist[c] = nh
end
st.histFrames = histFrames - drop
st.base = base + drop
end
@@ -208,10 +235,15 @@ local function process(self: State, src: buffer, frames: number, out: buffer?):
end
local ch = self.channels
frames = min(floor(frames), buffer.len(src) // (ch * 4))
local hist = self.hist
local p = self.histFrames * ch
for i = 0, frames * ch - 1 do
hist[p + i + 1] = readf32(src, i * 4)
local p = self.histFrames
for c = 1, ch do
local x = self.hist[c]
local o = (c - 1) * 4
local step = ch * 4
for i = p + 1, p + frames do
x[i] = readf32(src, o)
o += step
end
end
self.histFrames += frames
self.inFrames += frames
@@ -250,6 +282,10 @@ function Resample.new(from: number, to: number, channels: number, opts: Options?
local L: number, M: number = ti // g, fi // g
local exact = L <= MAX_EXACT
local phases = if exact then L else INTERP_PHASES
local hist = table.create(channels)
for c = 1, channels do
hist[c] = {}
end
local st: State = {
from = from,
to = to,
@@ -258,7 +294,7 @@ function Resample.new(from: number, to: number, channels: number, opts: Options?
f = if L == 1 and M == 1 then { taps = 2, half = 1, phases = 1, exact = true, h = { 1, 0 } } else makeFilter(to / from, phases, exact, q[1], q[2], cut),
step = M,
L = L,
hist = {},
hist = hist,
histFrames = 0,
base = 0,
inFrames = 0,
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/SoundFont/Fallback: the built-in General MIDI set (generated waveforms per GM family, drum kit on bank 128).
-- No third-party code.
local Parse = require("@rosetta/codecs/audio/SoundFont/Parse")
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/SoundFont/Parse: SoundFont 2.0x (sfbk RIFF) reader, zones merged into regions as TinySoundFont does.
-- After tsf.h v0.9 (MIT, (c) Bernhard Schelling; licenses/TinySoundFont-LICENSE.txt) tsf_load_presets /
-- tsf_region_operator / tsf_region_envtosecs. Unlike tsf.h, initialAttenuation is cB * 0.04 dB (FluidSynth's
@@ -1,5 +1,6 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/SoundFont/Synth: a SoundFont 2 sample synthesizer with MIDI channels.
-- A port of TinySoundFont (tsf.h v0.9, MIT, (c) 2017-2025 Bernhard Schelling, based on SFZero (c) 2012 Steve Folta;
-- licenses/TinySoundFont-LICENSE.txt). Unlike tsf.h, the envelope gain is ramped across each 64-sample block.
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/SoundFont: SoundFont 2 banks and the sample synth that plays MIDI through them.
-- The synth is a port of TinySoundFont (tsf.h v0.9, MIT, (c) 2017-2025 Bernhard Schelling; licenses/TinySoundFont-LICENSE.txt).
local Err = require("@rosetta/core/Err")
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Stream: the streaming Types.Decoder over a packetised codec (gapless trim, seeking with pre-roll).
-- New in rosetta.
local Err = require("@rosetta/core/Err")
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Tracker/Load: helpers mapping other tracker formats' effects onto the engine's S3M / IT or MOD / XM families.
-- Ported from OpenMPT's ModCommand helpers (ConvertModCommand, ConvertToVolCommand, FillInTwoCommands, GetEffectWeight,
-- CPattern::WriteEffect; BSD licence, licenses/openmpt-LICENSE.txt).
@@ -1,5 +1,6 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Tracker/Mixer: tracker voice mixing (loops, IT sustain loops, IT two-pole resonant filter, click ramps).
-- No third-party code.
local floor, ceil, min = math.floor, math.ceil, math.min
@@ -1,5 +1,6 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Tracker/Pcm: raw 8 / 16-bit tracker sample data (signed / unsigned, XM / IT 2.15 delta coding,
-- interleaved or planar stereo) <-> f32.
local readu8, readi8, readu16, readi16 = buffer.readu8, buffer.readi8, buffer.readu16, buffer.readi16
@@ -1,5 +1,6 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Tracker/Player: the tracker sequencer for the MOD, S3M, XM and IT effect families.
-- Written from the format documents (ProTracker 2.3 docs, the XM spec and FT2 behaviour notes, ST3 TECH.DOC, ITTECH.TXT);
-- no third-party code.
@@ -1,5 +1,6 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Tracker/Tables: tracker lookup tables, from the public format documents (ProTracker 2.3 effect docs,
-- FT2 / XM spec, ST3 TECH.DOC, ITTECH.TXT).
local Tables = {}
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Tracker: the shared tracker engine behind music/mod, s3m, xm and it, and the formats mapped onto them.
-- Written from the format documents (ProTracker 2.3, the XM spec + FT2 behaviour, ST3 TECH.DOC, ITTECH.TXT); libxmp /
-- OpenMPT were used only as reference players for tests.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Tta: the TTA1 (True Audio) lossless codec.
-- From the TTA1 format description (22-byte header and seek table with CRC-32s, frames of 256 / 245 seconds); no libtta /
-- ffmpeg code (LGPL). Format 2 (password) is UNSUPPORTED.
@@ -1,5 +1,6 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Vorbis/Setup: the three Vorbis header packets (Vorbis I spec section 4.2).
-- Ported from stb_vorbis 1.22 start_decoder (public domain / MIT, licenses/stb_vorbis-LICENSE.txt). Floor 0 is
-- rejected as unsupported, as stb_vorbis does.
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Vorbis/Tables: the floor 1 inverse dB table (Vorbis I specification section 10.1, as in stb_vorbis 1.22,
-- public domain / MIT, licenses/stb_vorbis-LICENSE.txt).
return table.freeze({
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/Vorbis: the Vorbis I decoder without its container.
-- A Luau port of stb_vorbis 1.22 (public domain / MIT, licenses/stb_vorbis-LICENSE.txt).
local Vorbis = {
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/VorbisComment: Vorbis comment fields ("KEY=value"; Vorbis, Opus, FLAC) <-> Types.Tags.
local VorbisComment = {}
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/audio/WavPack: the WavPack 4 / 5 audio codec (the blocks of .wv files, and of WavPack in Matroska).
-- Ported from WavPack 5.9.0 (David Bryant, BSD 3-clause, licenses/wavpack-LICENSE.txt). Not supported: DSD, pre-4.0
-- streams (version < 0x402).
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- containers/IsoBmff/Box: the box tree of an ISO BMFF / QuickTime file (ISO/IEC 14496-12 4.2): 32-bit, 64-bit
-- (size 1) and to-the-end (size 0) sizes, uuid boxes, full-box version / flags, and the recursion into the container
-- boxes (moov / trak / mdia / minf / stbl / udta / meta / ilst items / moof / traf / iprp / ipco / iref / dinf / ...).
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- containers/IsoBmff/Demux: tracks and sample tables of an ISO BMFF / QuickTime file (ISO/IEC 14496-12 8.3 - 8.8),
-- edit lists and fragments (mvex / trex, moof / traf / tfhd / tfdt / trun). The per-sample tables are columnar arrays,
-- offsets absolute in the reader's buffer. Never throws.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- containers/IsoBmff/Meta: the metadata boxes. HEIF / AVIF items (ISO/IEC 23008-12: pitm, iinf / infe, iloc
-- versions 0 - 2, iref, iprp / ipco / ipma, idat; the common properties ispe / pixi / colr / irot / imir / clap / auxC
-- decoded, codec configurations av1C / hvcC / avcC kept raw) and the iTunes tags (udta / meta / ilst: text, trkn /
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- containers/IsoBmff/Mux: writes a progressive ISO BMFF / MP4 / M4A file (ISO/IEC 14496-12): ftyp, moov first ("fast
-- start"), the samples interleaved in chunks in one mdat, udta / meta / ilst tags; co64 past 4 GB. Audio and video
-- sample entries with their configuration box, plus raw child boxes. Never throws: bad specs return nil, Err.ARG.
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- containers/IsoBmff/QtPalette: the depth and colour table of a QuickTime video sample description (Apple QuickTime
-- File Format, "Video sample description": depth at byte 82, color table ID at 84, then the table when the ID is 0).
-- Used by the MOV demuxer and by Matroska's V_QUICKTIME tracks (whose CodecPrivate is the same description). Depths 1,
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- containers/IsoBmff: ISO base media file format (ISO/IEC 14496-12) and its relatives: MP4 / M4A / M4V / MOV (QuickTime)
-- / 3GP, fragmented MP4 (moof), HEIF / AVIF items (ISO/IEC 23008-12), iTunes tags. A container (layer 2): m4a, mp4,
-- mov, heif, avif build on it. Written from the specifications; no third-party code.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/669: Composer 669 / UNIS 669 modules ("if" / "JN"), ported from OpenMPT's Load_669.cpp (BSD,
-- licenses/openmpt-LICENSE.txt). Supports: 8 channels, 64-row patterns, played as the "s3m" family with 669's quirks
-- (periods in Hz, auto portamentos, effects D..H, a fixed 78 BPM). Not supported: UNIS 669's speed 0, writing.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/ahx: AHX (Abyss' Highest eXperience) 4-channel Amiga synth modules, "THX\0" .. "THX\2", rendered through
-- codecs/audio/Hively (a port of HivelyTracker's hvl2wav, BSD-3-Clause; at 44.1 kHz, stereo 0, equal to hvl2wav).
-- Supports: tracks, positions with transposes, PList instruments, subsongs. Not supported: writing.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/far: Farandole Composer modules (FAR, 16 channels), ported from OpenMPT's Load_far.cpp (BSD,
-- licenses/openmpt-LICENSE.txt). Plays as the "s3m" family with FAR's quirks (periods in Hz, a slide step of
-- 36318 / 1024 Hz, OpenMPT's fixed 80 BPM). Not supported: FAR's tempo effects (D / E), note offset (C), writing.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/hmp: HMI SOS music: HMP ("HMIMIDIP", "HMIMIDIP013195") and HMI ("HMI-MIDISONG061595"), from the layout
-- notes (wildmidi only as a test reference). Decodes to a Types.Midi (format 1, a track per chunk; the tick rate at
-- HMP 0x38 / HMI byte 212, as division 60). Not supported: writing, HMP branch tables.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/hvl: HivelyTracker modules, "HVL\0" / "HVL\1" (AHX extended: 4..16 channels, two effect columns, ring
-- modulation, panning, stereo separation), rendered through codecs/audio/Hively (a port of hvl2wav, BSD-3-Clause; at
-- 44.1 kHz equal to hvl2wav). Not supported: writing.
+1
View File
@@ -1,5 +1,6 @@
--!strict
--!native
--!optimize 2
-- music/it/Itsex: IT 2.14 / 2.15 (double-delta) sample decompression, from ITTECH.TXT: 0x8000-byte output blocks, each
-- a u16 length then an LSB-first bit stream of deltas with three width-change methods.
local readu8, readu16, writef32 = buffer.readu8, buffer.readu16, buffer.writef32
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/it: Impulse Tracker modules (IMPM, ITTECH.TXT), played through codecs/audio/Tracker. Supports: 64 channels,
-- instrument / sample mode, old and new instrument headers, envelopes, NNAs, resonant filters (default Zxx macros),
-- IT 2.14 / 2.15 compressed samples, the message. Not supported: embedded MIDI macros, OpenMPT extensions, writing.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/med: MED / OctaMED modules (MMD0..MMD3), ported from OpenMPT's Load_med.cpp (BSD,
-- licenses/openmpt-LICENSE.txt); the first song plays as the "xm" family with Amiga periods. Supports: 1..64 tracks,
-- play sequences, hold / decay, IFF multi-octave samples. Not supported: synth / MIDI instruments, echo, writing.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/midi/Smf: SMF 1.0 MThd / MTrk <-> Types.Midi (running status also across meta / sysex, RIFF RMID unwrapped).
-- Writes what python mido writes: running status reset after meta / sysex, one end-of-track, track names at tick 0.
local Err = require("@rosetta/core/Err")
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/midi: Standard MIDI Files (SMF 1.0 formats 0 / 1 / 2, PPQ and SMPTE; RIFF RMID read), from the spec. Supports:
-- decode to a Types.Midi, .kar lyrics, encode (byte-identical to python mido by default), the tempo map, rendering
-- through codecs/audio/SoundFont. Not supported: RMID write, XMF / DLS banks, system common bytes in tracks.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/mod: ProTracker / NoiseTracker / StarTrekker MODs (M.K., M!K!, FLT4, xCHN, xxCH, xxCN, CD81, OKTA, TDZx;
-- 15-sample Soundtracker by name or extension), from the ProTracker 2.3 docs. Writes 31-sample MODs from "mod" modules
-- and XM ones that fit. Not supported: FLT8's split patterns (read as 8 channels), the E0x filter, PowerPacker files.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/mtm: MultiTracker modules (MTM, 1..32 channels, patterns of shared tracks), ported from OpenMPT's Load_mtm.cpp
-- (BSD, licenses/openmpt-LICENSE.txt); played as the "xm" family with Amiga periods, as OpenMPT does. Not supported:
-- MultiTracker's E9x / EDx retrigger timing and vibrato start phase (played as in XM), writing.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/mus: DMX MUS ("MUS\x1a"; Doom, Heretic, Hexen, Strife), from Vladimir Arnost's "MUS File Format" (wildmidi
-- only as a test reference). Decodes to a Types.Midi as the usual converters map it (MUS channel 15 -> 9; 140 ticks/s
-- as division 70 at 500000 us per quarter). Not supported: writing (encode as .mid with the midi format).
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/okt: Oktalyzer modules (OKTASONG, 4..8 channels: each Amiga channel may be a mixed pair), ported from OpenMPT's
-- Load_okt.cpp (BSD, licenses/openmpt-LICENSE.txt); played as the "it" family with Amiga periods, loops as sustain
-- loops (released by R), the three arpeggios and note slides. Not supported: the LED filter (F), writing.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/ptm: PolyTracker modules (PTMF, up to 32 channels), ported from OpenMPT's Load_ptm.cpp (BSD,
-- licenses/openmpt-LICENSE.txt); played as the "it" family with Amiga periods, as OpenMPT converts PTM. Not supported:
-- reverse sample offset (K), sample offsets scaled for 16-bit samples, writing.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/s3m: Scream Tracker 3 modules (SCRM, ST3 TECH.DOC). Supports: 32 PCM channels, 8 / 16-bit mono / stereo
-- samples, the panning table, ST3.00 fast slides, every ST3 effect; notes stored an octave up (S3M C-4 = note 61, as IT
-- shows S3M files). Not supported: AdLib (OPL) instruments (silent), writing.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/sf2: SoundFont 2 banks (SoundFont 2.04 spec), read-only, over codecs/audio/SoundFont (a TinySoundFont port).
-- Supports: INFO, 16 / 24-bit (sm24) samples, presets / instruments with global zones, generators merged into regions.
-- Not supported: writing, SF3 (Vorbis samples), modulators (skipped).
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/stm: Scream Tracker 2 modules (STM, "!Scream!" / BMOD2STM / WUZAMOD! / SWavePro), ported from OpenMPT's
-- Load_stm.cpp (BSD, licenses/openmpt-LICENSE.txt); played as the "s3m" family with ST2's tempo, no effect memory and
-- its deferred Bxx. Not supported: STX, ST2's noise for samples at volume 0 (silent), writing.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/ult: UltraTracker modules (MAS_UTrack_V001 .. V004, up to 32 channels), ported from OpenMPT's Load_ult.cpp
-- (BSD, licenses/openmpt-LICENSE.txt); played as the "it" family with Amiga periods, as OpenMPT converts ULT. Not
-- supported: reverse (5x2), "no loop" (5x1), E0x, offsets past 64 KiB, pre-1.4 log volumes, writing.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/umx: Unreal Engine 1 music packages (.umx, versions up to 128), ported from OpenMPT's UMXTools.cpp /
-- ContainerUMX.cpp (BSD, licenses/openmpt-LICENSE.txt): the IT / S3M / XM / MOD module of a "music" export; decode
-- needs opts.decodeModule (the api passes it). Not supported: Unreal Engine 2+ packages (.uax), writing.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/xm: FastTracker 2 Extended Modules (XM 1.04), from the XM spec and FT2 behaviour notes. Supports: 8 / 16-bit
-- delta and ModPlug ADPCM samples, envelopes, linear / Amiga tables, every FT2 effect; writes "xm" (byte-identical in
-- FT2's layout) and "mod" modules. Not supported: OpenMPT's stereo-sample / extra-effect extensions on write.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- music/xmi: Miles XMIDI (IFF: optional FORM XDIR, then CAT XMID of FORM XMID songs with TIMB / RBRN / EVNT), from
-- the XMIDI notes (Miles AIL, Ryan Nunn; wildmidi only as a test reference). Decodes one song to a Types.Midi at the
-- fixed 120 ticks per second (division 60). Not supported: writing, the RBRN branch table, MT-32 -> GM mapping.
+57 -28
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- ops/AudioOps: editing on the standard Audio: collect, gain, normalize, mix, remix, resample (codecs/audio/Resample),
-- trim, concat, fades, peak / RMS. New code.
-- Conventions: samples are the Audio's interleaved f32 (no clipping is applied: values past +-1 are kept, encoders
@@ -61,8 +62,8 @@ function AudioOps.gain(a: Types.Audio, factor: number): (Types.Audio?, Err.Err?)
local n = a.frames * a.channels
local out = like(a, a.channels, a.frames)
local s, d = a.samples, out.samples
for i = 0, n - 1 do
writef32(d, i * 4, readf32(s, i * 4) * factor)
for o = 0, n * 4 - 4, 4 do
writef32(d, o, readf32(s, o) * factor)
end
return out, nil
end
@@ -81,14 +82,16 @@ function AudioOps.peak(a: Types.Audio): (number?, { number } | Err.Err)
local ch = a.channels
local per = table.create(ch, 0)
local s = a.samples
for f = 0, a.frames - 1 do
local p = f * ch * 4
for c = 1, ch do
local v = abs(readf32(s, p + (c - 1) * 4))
if v > per[c] then
per[c] = v
local last, step = (a.frames - 1) * ch * 4, ch * 4
for c = 1, ch do
local pk = 0
for p = (c - 1) * 4, last + (c - 1) * 4, step do
local v = abs(readf32(s, p))
if v > pk then
pk = v
end
end
per[c] = pk
end
local m = 0
for c = 1, ch do
@@ -104,12 +107,14 @@ function AudioOps.rms(a: Types.Audio): (number?, { number } | Err.Err)
local ch = a.channels
local per = table.create(ch, 0)
local s = a.samples
for f = 0, a.frames - 1 do
local p = f * ch * 4
for c = 1, ch do
local v = readf32(s, p + (c - 1) * 4)
per[c] += v * v
local last, step = (a.frames - 1) * ch * 4, ch * 4
for c = 1, ch do
local acc = 0
for p = (c - 1) * 4, last + (c - 1) * 4, step do
local v = readf32(s, p)
acc += v * v
end
per[c] = acc
end
local total = 0
local n = max(1, a.frames)
@@ -290,23 +295,47 @@ function AudioOps.remix(a: Types.Audio, channels: number, m: { { number } }?): (
end
local out = like(a, channels, a.frames)
local s, d = a.samples, out.samples
local x = table.create(from, 0)
for f = 0, a.frames - 1 do
local p = f * from * 4
local frames = a.frames
local inStep, outStep = from * 4, channels * 4
for o = 1, channels do
-- the row's non-zero weights, in input order: v = sum w * x[i]
local row = mm[o]
local ws, offs = {}, {}
for i = 1, from do
x[i] = readf32(s, p + (i - 1) * 4)
local w = row[i]
if w and w ~= 0 then
table.insert(ws, w)
table.insert(offs, (i - 1) * 4)
end
end
local q = f * channels * 4
for o = 1, channels do
local row = mm[o]
local v = 0
for i = 1, from do
local w = row[i]
if w and w ~= 0 then
v += w * x[i]
local q = (o - 1) * 4
local nt = #ws
if nt == 0 then
for _ = 1, frames do
writef32(d, q, 0)
q += outStep
end
elseif nt == 1 then
local w0, o0 = ws[1], offs[1]
for p = 0, (frames - 1) * inStep, inStep do
writef32(d, q, 0 + w0 * readf32(s, p + o0))
q += outStep
end
elseif nt == 2 then
local w0, o0, w1, o1 = ws[1], offs[1], ws[2], offs[2]
for p = 0, (frames - 1) * inStep, inStep do
writef32(d, q, 0 + w0 * readf32(s, p + o0) + w1 * readf32(s, p + o1))
q += outStep
end
else
for p = 0, (frames - 1) * inStep, inStep do
local v = 0
for t = 1, nt do
v += ws[t] * readf32(s, p + offs[t])
end
writef32(d, q, v)
q += outStep
end
writef32(d, q + (o - 1) * 4, v)
end
end
return out, nil
@@ -437,8 +466,8 @@ function AudioOps.mix(list: { Types.Audio }, weights: { number }?): (Types.Audio
return bad("mix: weight " .. i .. " is not a number")
end
local s = a.samples
for k = 0, a.frames * ch - 1 do
writef32(d, k * 4, readf32(d, k * 4) + readf32(s, k * 4) * w)
for o = 0, a.frames * ch * 4 - 4, 4 do
writef32(d, o, readf32(d, o) + readf32(s, o) * w)
end
end
return out, nil
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- ops/MusicOps: building and editing tracker Modules over codecs/audio/Tracker without touching the engine's internal
-- layout (5-byte cells, f32 samples, tuning fields). New code.
-- Conventions: pattern numbers are 0-based (as the order list names them), rows 0-based, channels 1-based, sample
+265
View File
@@ -0,0 +1,265 @@
-- Audio throughput (luau -O2 tools/bench_audio.luau [-a filter]): decode / encode speed of every audio codec as x real
-- time and MB/s of f32 output, best of n runs. Fixtures: tests/gen (tools/fixtures.py).
local Bytes = require("@rosetta/core/Bytes")
local Types = require("@rosetta/Types")
local filter = ...
local runtime = if (_G :: any).jit ~= nil then "luajit" else "luau"
local jit = (_G :: any).jit
if jit and type(jit.opt) == "table" then
jit.opt.start("maxtrace=8000", "maxmcode=65536") -- as host/init.luau sets them for the api
end
local function want(label: string): boolean
return filter == nil or string.find(label, filter, 1, true) ~= nil
end
local function best(n: number, f: () -> ()): number
f()
local dt = math.huge
for _ = 1, n do
local t0 = os.clock()
f()
dt = math.min(dt, os.clock() - t0)
end
return dt
end
local function report(label: string, dt: number, secs: number, bytes: number)
print(string.format("%-8s %-34s %9.2f ms %8.1f x RT %8.2f MB/s", runtime, label, dt * 1000, secs / dt, bytes / dt / 1e6))
end
local function case(list: any, name: string): any
for _, c in list do
if c.name == name then
return c
end
end
error("no fixture " .. name)
end
-- decodeBench(label, module path, fixture, case name, runs): Module.decode(reader) on the case's bytes
local function decodeBench(label: string, mod: any, data: string, n: number)
if not want(label) then
return
end
local b = buffer.fromstring(data)
local a = assert(mod.decode(Bytes.reader(b)))
local dt = best(n, function()
mod.decode(Bytes.reader(b))
end)
report(label, dt, a.frames / a.sampleRate, a.frames * a.channels * 4)
end
local MC = require("@tests/gen/mp3_cases")
local Mp3 = require("@rosetta/audio/mp3")
for _, name in { "l3_cbr128_js", "l3_vbr_v2_48k", "l2_192_st" } do
decodeBench("mp3 " .. name, Mp3, case(MC, name).data, 5)
end
if want("mp3 stream") then
local b = buffer.fromstring(case(MC, "l3_cbr128_js").data)
local out = buffer.create(1152 * 2 * 4)
local frames, rate = 0, 44100
local dt = best(5, function()
local d = assert(Mp3.open(Bytes.reader(b)))
rate = d.sampleRate
frames = 0
while true do
local _, n = d:read(1152, out)
if n == 0 then
break
end
frames += n
end
end)
report("mp3 stream read(1152) cbr128", dt, frames / rate, frames * 8)
end
local Opus = require("@rosetta/audio/opus")
for _, c in {
{ "opus celt 128k", require("@tests/gen/opus_celt_st_128").data },
{ "opus silk wb", require("@tests/gen/opus_silk_wb_st").data },
{ "opus hybrid", require("@tests/gen/opus_hybrid_st").data },
{ "opus 48k music (ogg)", case(require("@tests/gen/ogg_cases"), "opus_stereo").data },
} do
decodeBench(c[1], Opus, c[2], 3)
end
local Vorbis = require("@rosetta/audio/vorbis")
decodeBench("vorbis q5 (10 s st)", Vorbis, require("@tests/gen/vorbis_bench").data, 3)
local M4a = require("@rosetta/audio/m4a")
local MA = require("@tests/gen/m4a_aac")
decodeBench("aac-lc m4a stereo 128k", M4a, case(MA, "aac_stereo_128k.m4a").data, 3)
decodeBench("aac fdk m4a stereo", M4a, case(MA, "fdk_stereo.m4a").data, 3)
decodeBench("alac m4a stereo 16", M4a, require("@tests/gen/m4a_alac")[1].data, 3)
local Flac = require("@rosetta/audio/flac")
local FC = require("@tests/gen/flac_cases")
decodeBench("flac s16_long", Flac, case(FC, "s16_long").data, 5)
decodeBench("flac s24_96k", Flac, case(FC, "s24_96k").data, 5)
local Wav = require("@rosetta/audio/wav")
do
local a = Types.Audio.new(44100, 2, 441000)
for i = 0, 882000 - 1 do
buffer.writef32(a.samples, i * 4, math.sin(i * 0.01) * 0.5)
end
if want("wav") then
for _, fmt in { "s16", "s24", "f32" } do
local file = assert(Wav.encode(a, { sampleFormat = fmt }))
local dt = best(5, function()
Wav.decode(Bytes.reader(file))
end)
report("wav decode " .. fmt .. " (10 s st)", dt, 10, buffer.len(a.samples))
dt = best(5, function()
Wav.encode(a, { sampleFormat = fmt })
end)
report("wav encode " .. fmt .. " (10 s st)", dt, 10, buffer.len(a.samples))
end
end
local Resample = require("@rosetta/codecs/audio/Resample")
for _, r in { { 44100, 48000 }, { 48000, 44100 }, { 32000, 48000 } } do
local label = string.format("resample %d->%d (10 s st)", r[1], r[2])
if want(label) then
local dt = best(3, function()
Resample.buffer(a.samples, 441000, 2, r[1], r[2])
end)
report(label, dt, 441000 / r[1], buffer.len(a.samples))
end
end
local AudioOps = require("@rosetta/ops/AudioOps")
if want("audioops") then
report("audioops gain (10 s st)", best(5, function()
AudioOps.gain(a, 0.5)
end), 10, buffer.len(a.samples))
report("audioops peak+rms (10 s st)", best(5, function()
AudioOps.peak(a)
AudioOps.rms(a)
end), 10, buffer.len(a.samples))
report("audioops downmix 2->1 (10 s)", best(5, function()
AudioOps.downmix(a, 1)
end), 10, buffer.len(a.samples))
report("audioops fade (10 s st)", best(5, function()
AudioOps.fade(a, 2, 2)
end), 10, buffer.len(a.samples))
end
end
local function lossless(label: string, path: any, cases: any, name: string, n: number)
if want(label) then
decodeBench(label, path, case(cases, name).data, n)
end
end
lossless("wavpack s16_long", require("@rosetta/audio/wavpack"), require("@tests/gen/wavpack_cases"), "s16_long", 3)
lossless("wavpack s16_hh", require("@rosetta/audio/wavpack"), require("@tests/gen/wavpack_cases"), "s16_hh", 3)
lossless("ape s16_long", require("@rosetta/audio/ape"), require("@tests/gen/ape_cases"), "s16_long", 3)
lossless("ape s16_long_c4000", require("@rosetta/audio/ape"), require("@tests/gen/ape_cases"), "s16_long_c4000", 3)
lossless("tta s16_long", require("@rosetta/audio/tta"), require("@tests/gen/tta_cases"), "s16_long", 3)
lossless("mpc sv8 q5 stereo", require("@rosetta/audio/mpc"), require("@tests/gen/mpc_cases"), "sv8_q5_stereo", 3)
lossless("mpc sv7 q5 stereo", require("@rosetta/audio/mpc"), require("@tests/gen/mpc_cases"), "sv7_q5_stereo", 3)
lossless("ac3 5.1_448", require("@rosetta/audio/ac3"), require("@tests/gen/ac3_cases"), "5.1_448", 3)
lossless("ac3 stereo_44k", require("@rosetta/audio/ac3"), require("@tests/gen/ac3_cases"), "stereo_44k", 3)
-- renders: x RT of stereo 44.1 kHz output (MB/s of f32 stereo)
local function render(label: string, n: number, mk: () -> any, maxFrames: number?)
if not want(label) then
return
end
local out = buffer.create(4096 * 8)
local frames = 0
local dt = best(n, function()
local dec = mk()
frames = 0
while true do
local _, k = dec:read(4096, out)
if k == 0 or (maxFrames and frames >= maxFrames) then
break
end
frames += k
end
end)
report(label, dt, frames / 44100, frames * 8)
end
do
local Midi = require("@rosetta/music/midi")
local Sf2 = require("@rosetta/music/sf2")
local SF = require("@tests/gen/sf2_cases")
local sf = assert(Sf2.decode(Bytes.reader(buffer.fromstring(SF.sf2))))
local song = assert(Midi.decode(Bytes.reader(buffer.fromstring(SF.song))))
render("midi render sf2 (test song)", 3, function()
return assert(Midi.render(song, { soundfont = sf, tail = 0 }))
end)
local fb = assert(Midi.decode(Bytes.reader(buffer.fromstring(case(require("@tests/gen/midi_cases"), "song").data))))
render("midi render fallback", 3, function()
return assert(Midi.render(fb, { tail = 0 }))
end)
if want("synth 32 voices") then
local SoundFont = require("@rosetta/codecs/audio/SoundFont")
local frames = 88200
local o = buffer.create(frames * 8)
local dt = best(3, function()
local s = SoundFont.synth(sf, 44100)
SoundFont.Synth.midi(s, 0xC0, 1, 0)
for k = 0, 31 do
SoundFont.Synth.midi(s, 0x90, 40 + k, 100)
end
for f = 0, frames - 1, 1024 do
SoundFont.Synth.render(s, o, f, 1024)
end
end)
report("synth 32 voices x 2 s", dt, 2, frames * 8)
end
end
do
local Tracker = require("@rosetta/codecs/audio/Tracker")
local function song(format: string, filter: boolean): any
local m = Tracker.newModule(format, 32)
m.order = { 0 }
m.patterns[1] = Tracker.newPattern(64, 32)
local s = Tracker.newSample("saw")
s.length = 1000
s.data = buffer.create(4000)
for i = 0, 999 do
buffer.writef32(s.data, i * 4, (i % 100) / 50 - 1)
end
s.loopStart, s.loopEnd, s.loopType = 0, 1000, 1
m.samples[1] = s
if format == "it" then
local ins = Tracker.newInstrument("nna")
for n = 1, 120 do
ins.sampleMap[n] = 1
end
ins.nna = 1
ins.fadeout = 64
if filter then
ins.cutoff, ins.resonance = 60, 40
end
m.instruments[1] = ins
end
for ch = 1, 32 do
for row = 0, 63, 16 do
Tracker.setCell(m, 0, row, ch, 37 + ch + row // 16, 1, 0, 0, 0)
end
end
return m
end
local mod, it = song("mod", false), song("it", true)
render("tracker 32 ch (mod)", 3, function()
return Tracker.render(mod)
end)
render("tracker 32 ch + NNA, filters (it)", 3, function()
return Tracker.render(it)
end)
end
do
local Ahx, Hvl = require("@rosetta/music/ahx"), require("@rosetta/music/hvl")
local AC, HC = require("@tests/gen/ahx_cases"), require("@tests/gen/hvl_cases")
for _, spec in { { Ahx, AC, "karma.ahx", "ahx render karma (4 ch)" }, { Hvl, HC, "demo", "hvl render demo (6 ch)" } } do
local c = case(spec[2], spec[3])
local s = assert(spec[1].decode(Bytes.reader(buffer.fromstring(c.data))))
render(spec[4], 3, function()
return s:render({ maxDuration = c.maxDuration })
end)
end
end