--!strict --!native -- codecs/audio/Tracker/Pcm: sample data conversions shared by the tracker formats. Raw 8 / 16-bit PCM (signed or unsigned, -- plain or delta coded as in XM / IT 2.15, interleaved or planar stereo) to the engine's f32 samples, and back. -- -- Never throws: reads past the source are refused up front (decode returns nil). local readu8, readi8, readu16, readi16 = buffer.readu8, buffer.readi8, buffer.readu16, buffer.readi16 local writef32, readf32, writeu8, writei16 = buffer.writef32, buffer.readf32, buffer.writeu8, buffer.writei16 local floor = math.floor local Pcm = {} export type Spec = { bits: number, -- 8 | 16 signed: boolean, delta: boolean?, -- each value is the difference to the previous one (XM, IT 2.15 "delta" flag) channels: number?, -- 1 (default) | 2 planar: boolean?, -- stereo stored as all left then all right (XM, IT); default interleaved bigEndian: boolean?, } -- bytes(frames, spec) -> the size of the raw data function Pcm.bytes(frames: number, spec: Spec): number return frames * (spec.channels or 1) * (spec.bits // 8) end -- decode(buf, off, frames, spec) -> f32 buffer (frames * channels, interleaved) | nil when the source is too short function Pcm.decode(buf: buffer, off: number, frames: number, spec: Spec): buffer? local nch = spec.channels or 1 local bps = spec.bits // 8 local total = frames * nch if frames < 0 or off < 0 or off + total * bps > buffer.len(buf) then return nil end local out = buffer.create(total * 4) local planar = spec.planar and nch == 2 for c = 0, nch - 1 do local acc = 0 for i = 0, frames - 1 do local k = if planar then c * frames + i else i * nch + c local v if bps == 1 then if spec.signed then v = readi8(buf, off + k) else v = readu8(buf, off + k) - 128 end if spec.delta then acc = (acc + v + 128) % 256 - 128 v = acc end writef32(out, (i * nch + c) * 4, v / 128) else local p = off + k * 2 if spec.bigEndian then v = readu8(buf, p) * 256 + readu8(buf, p + 1) if spec.signed and v >= 32768 then v -= 65536 end elseif spec.signed then v = readi16(buf, p) else v = readu16(buf, p) end if not spec.signed then v -= 32768 end if spec.delta then acc = (acc + v + 32768) % 65536 - 32768 v = acc end writef32(out, (i * nch + c) * 4, v / 32768) end end end return out end -- encode(samples, frames, spec) -> raw PCM (signed or unsigned, optional delta, interleaved or planar) function Pcm.encode(samples: buffer, frames: number, spec: Spec): buffer local nch = spec.channels or 1 local bps = spec.bits // 8 local out = buffer.create(frames * nch * bps) local planar = spec.planar and nch == 2 local avail = buffer.len(samples) // 4 for c = 0, nch - 1 do local prev = 0 for i = 0, frames - 1 do local si = i * nch + c local x = if si < avail then readf32(samples, si * 4) else 0 local k = if planar then c * frames + i else i * nch + c if bps == 1 then local v = math.clamp(floor(x * 128 + 0.5), -128, 127) local w = v if spec.delta then w = (v - prev) % 256 prev = v else w = if spec.signed then v % 256 else v + 128 end writeu8(out, k, w) else local v = math.clamp(floor(x * 32768 + 0.5), -32768, 32767) local w = v if spec.delta then w = (v - prev + 32768) % 65536 - 32768 prev = v elseif not spec.signed then w = v + 32768 if w >= 32768 then w -= 65536 end end writei16(out, k * 2, w) end end end return out end -- bits(samples) -> 8 when every value is exactly representable in 8 bits (data loaded from 8-bit), else 16 function Pcm.bitsNeeded(samples: buffer): number for i = 0, buffer.len(samples) // 4 - 1 do local x = readf32(samples, i * 4) * 128 if x ~= floor(x) then return 16 end end return 8 end return Pcm