--!strict --!native --!optimize 2 -- audio/wav/Codecs: sample conversion loops (native) into interleaved f32, and back (the encoder's quantizers). -- Absorbed from wav-luau fd9a909 (same author); the encoder loops are new. They never throw: audio/wav (init.luau) -- only calls them with ranges it has checked against the source length and the output size. local readu8, readi8, readu16, readi16, readu32, readi32 = buffer.readu8, buffer.readi8, buffer.readu16, buffer.readi16, buffer.readu32, buffer.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 Codecs = {} -- Integer PCM: count samples of `bytes` bytes each (1 = unsigned 8-bit, 2 / 3 / 4 = signed), little- or big-endian, -- from src[off] to dst (f32) starting at sample index d. function Codecs.int(src: buffer, off: number, dst: buffer, d: number, count: number, bytes: number, be: boolean) local o = d * 4 if bytes == 1 then for i = 0, count - 1 do writef32(dst, o + i * 4, (readu8(src, off + i) - 128) / 128) end elseif bytes == 2 then if be then for i = 0, count - 1 do local s = off + i * 2 local v = readi8(src, s) * 256 + readu8(src, s + 1) writef32(dst, o + i * 4, v / 32768) end else for i = 0, count - 1 do writef32(dst, o + i * 4, readi16(src, off + i * 2) / 32768) end end elseif bytes == 3 then if be then for i = 0, count - 1 do local s = off + i * 3 local v = readi8(src, s) * 65536 + readu8(src, s + 1) * 256 + readu8(src, s + 2) writef32(dst, o + i * 4, v / 8388608) end else for i = 0, count - 1 do local s = off + i * 3 local v = readi8(src, s + 2) * 65536 + readu16(src, s) writef32(dst, o + i * 4, v / 8388608) end end else if be then for i = 0, count - 1 do local v = byteswap(readu32(src, off + i * 4)) if v >= 0x80000000 then v -= 0x100000000 end writef32(dst, o + i * 4, v / 2147483648) end else for i = 0, count - 1 do writef32(dst, o + i * 4, readi32(src, off + i * 4) / 2147483648) end end end end -- IEEE float, 4 or 8 bytes. tmp: an 8-byte scratch buffer for byte-swapping big-endian values. function Codecs.float(src: buffer, off: number, dst: buffer, d: number, count: number, bytes: number, be: boolean, tmp: buffer) local o = d * 4 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)) end else for i = 0, count - 1 do writef32(dst, o + i * 4, readf32(src, off + i * 4)) end end else if be then for i = 0, count - 1 do local s = off + i * 8 writeu32(tmp, 0, byteswap(readu32(src, s + 4))) writeu32(tmp, 4, byteswap(readu32(src, s))) writef32(dst, o + i * 4, readf64(tmp, 0)) end else for i = 0, count - 1 do writef32(dst, o + i * 4, readf64(src, off + i * 8)) end end end end -- 8-bit companded (A-law / mu-law) through a 256-entry f32 table 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)) end end local IMA_STEP = buffer.create(89 * 4) do local steps = { 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767, } for i, s in steps do buffer.writei32(IMA_STEP, (i - 1) * 4, s) end end local IMA_INDEX = buffer.create(16 * 4) do local t = { -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8 } for i, v in t do buffer.writei32(IMA_INDEX, (i - 1) * 4, v) end end -- IMA ADPCM (Microsoft / WAV layout), one block at src[off] of blockAlign bytes with `avail` bytes present (a -- truncated last block decodes the whole 8-sample groups it has). Writes interleaved f32 to dst from frame 0 and -- returns the frame count. function Codecs.imaBlock(src: buffer, off: number, avail: number, ch: number, spb: number, dst: buffer): number if avail < 4 * ch then return 0 end -- groups of 8 samples per channel (4 bytes each) after the per-channel headers local groups = (avail - 4 * ch) // (4 * ch) local frames = math.min(spb, 1 + groups * 8) for c = 0, ch - 1 do local pred = readi16(src, off + c * 4) local index = readu8(src, off + c * 4 + 2) if index > 88 then index = 88 end writef32(dst, c * 4, pred / 32768) local f = 1 local g = 0 while f < frames do local p = off + 4 * ch + (g * ch + c) * 4 for k = 0, 7 do if f >= frames then break end local b = readu8(src, p + rshift(k, 1)) local nib = if band(k, 1) == 0 then band(b, 15) else rshift(b, 4) local step = buffer.readi32(IMA_STEP, index * 4) local diff = rshift(step, 3) if band(nib, 1) ~= 0 then diff += rshift(step, 2) end if band(nib, 2) ~= 0 then diff += rshift(step, 1) end if band(nib, 4) ~= 0 then diff += step end if band(nib, 8) ~= 0 then pred -= diff else pred += diff end if pred > 32767 then pred = 32767 elseif pred < -32768 then pred = -32768 end index += buffer.readi32(IMA_INDEX, nib * 4) if index < 0 then index = 0 elseif index > 88 then index = 88 end writef32(dst, (f * ch + c) * 4, pred / 32768) f += 1 end g += 1 end end return frames end local MS_ADAPT = buffer.create(16 * 4) do local t = { 230, 230, 230, 230, 307, 409, 512, 614, 768, 614, 512, 409, 307, 230, 230, 230 } for i, v in t do buffer.writei32(MS_ADAPT, (i - 1) * 4, v) end end -- MS ADPCM, one block. coefs: i32 pairs (coef1, coef2) x ncoef (256 = 1.0). state: 5 * ch i32 scratch. -- Returns the frame count (a truncated last block decodes the whole bytes it has). function Codecs.msBlock(src: buffer, off: number, avail: number, ch: number, spb: number, dst: buffer, coefs: buffer, ncoef: number, state: buffer): number local hdr = 7 * ch if avail < hdr or ncoef < 1 then return 0 end local frames = math.min(spb, 2 + ((avail - hdr) * 2) // ch) -- state per channel: c1, c2, idelta, s1, s2 (5 i32) for c = 0, ch - 1 do local pi = readu8(src, off + c) if pi >= ncoef then pi = ncoef - 1 end local idelta = readi16(src, off + ch + c * 2) local s1 = readi16(src, off + 3 * ch + c * 2) local s2 = readi16(src, off + 5 * ch + c * 2) local so = c * 20 buffer.writei32(state, so, buffer.readi32(coefs, pi * 8)) buffer.writei32(state, so + 4, buffer.readi32(coefs, pi * 8 + 4)) buffer.writei32(state, so + 8, idelta) buffer.writei32(state, so + 12, s1) buffer.writei32(state, so + 16, s2) writef32(dst, c * 4, s2 / 32768) if frames > 1 then writef32(dst, (ch + c) * 4, s1 / 32768) end end -- nibbles, high first, channels interleaved per nibble local total = (frames - 2) * ch for k = 0, total - 1 do local b = readu8(src, off + hdr + rshift(k, 1)) local nib = if band(k, 1) == 0 then rshift(b, 4) else band(b, 15) local c = k % ch local so = c * 20 local c1, c2 = buffer.readi32(state, so), buffer.readi32(state, so + 4) local idelta = buffer.readi32(state, so + 8) local s1, s2 = buffer.readi32(state, so + 12), buffer.readi32(state, so + 16) local p = s1 * c1 + s2 * c2 p = if p >= 0 then p // 256 else -((-p) // 256) local sn = if nib >= 8 then nib - 16 else nib p += sn * idelta if p > 32767 then p = 32767 elseif p < -32768 then p = -32768 end idelta = (buffer.readi32(MS_ADAPT, nib * 4) * idelta) // 256 if idelta < 16 then idelta = 16 elseif idelta > 2796202 then -- INT_MAX / 768, as ffmpeg idelta = 2796202 end buffer.writei32(state, so + 8, idelta) buffer.writei32(state, so + 12, p) buffer.writei32(state, so + 16, s1) writef32(dst, (2 * ch + k) * 4, p / 32768) end return frames end -- copy frames from a block cache into the output: count f32 values function Codecs.copy(src: buffer, sOff: number, dst: buffer, dOff: number, count: number) buffer.copy(dst, dOff * 4, src, sOff * 4, count * 4) end -- Encoder: count f32 samples from src (sample index s) to dst[off] as `kind` ("u8", "s16", "s24", "s32", "f32"), -- little-endian, rounding to nearest and clamping to the integer range. Ranges are checked by the caller. function Codecs.quantize(src: buffer, s: number, dst: buffer, off: number, count: number, kind: string) local so = s * 4 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) if v > 32767 then v = 32767 elseif v < -32768 then v = -32768 elseif v ~= v then v = 0 end writei16(dst, off + i * 2, v) end elseif kind == "s24" then for i = 0, count - 1 do local v = math.floor(readf32(src, so + i * 4) * 8388608 + 0.5) if v > 8388607 then v = 8388607 elseif v < -8388608 then v = -8388608 elseif v ~= v then v = 0 end if v < 0 then v += 16777216 end local o = off + i * 3 writeu16(dst, o, v % 65536) writeu8(dst, o + 2, v // 65536) end elseif kind == "s32" then for i = 0, count - 1 do local v = math.floor(readf32(src, so + i * 4) * 2147483648 + 0.5) if v > 2147483647 then v = 2147483647 elseif v < -2147483648 then v = -2147483648 elseif v ~= v then v = 0 end writei32(dst, off + i * 4, v) end else -- u8 for i = 0, count - 1 do local v = math.floor(readf32(src, so + i * 4) * 128 + 0.5) if v > 127 then v = 127 elseif v < -128 then v = -128 elseif v ~= v then v = 0 end writeu8(dst, off + i, v + 128) end end end return Codecs