165 lines
4.4 KiB
Luau
165 lines
4.4 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- audio/wav/Codecs: PCM / float / G.711 sample loops into interleaved f32 and back; absorbed from wav-luau fd9a909.
|
|
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: 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
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
function Codecs.copy(src: buffer, sOff: number, dst: buffer, dOff: number, count: number)
|
|
buffer.copy(dst, dOff * 4, src, sOff * 4, count * 4)
|
|
end
|
|
|
|
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
|