--!strict -- codecs/audio/WavPack: the WavPack 4 / 5 audio codec (the blocks of .wv files, and of WavPack in Matroska). A codec -- (layer 3). Ported from WavPack 5.9.0 (David Bryant, BSD 3-clause, licenses/wavpack-LICENSE.txt); the hot code is in -- codecs/audio/WavPack/Block. Lossless and hybrid (lossy, and lossless with the .wvc correction blocks), integer 8 - 32 -- bit (with the extended int32 modes) and 32-bit float, mono / stereo / multichannel (one block per mono or stereo -- stream, INITIAL .. FINAL), every decorrelation pass (terms 1 - 8, 17, 18, -1 .. -3), joint stereo, cross -- decorrelation, noise shaping, block CRCs and block checksums. Not supported: DSD, pre-4.0 streams (version < 0x402). -- -- local h, err = WavPack.header(buf, at, limit) -- the 32-byte block header at `at` (nil, message if none) -- local info, err = WavPack.info(buf, at, limit) -- stream facts from the first audio block (channels, rate, ...) -- local dec = WavPack.decoder(info.channels) -- local frames, err = dec:frame(buf, blocks, cbuf?, cblocks?) -- blocks = { offset, ... } of one frame (+ wvc) -- -- dec.pcm: interleaved samples (integers in the container range, or float values), WAV channel order -- -- Never throws: bad data returns nil, Err. local Err = require("@formats/core/Err") local Block = require("@formats/codecs/audio/WavPack/Block") local WavPack = {} export type Header = { size: number, -- the whole block (ckSize + 8) version: number, index: number, -- the first sample of the block (40 bits) total: number, -- total samples of the file (-1: unknown) samples: number, flags: number, crc: number, } local RATES = { 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000, 192000 } WavPack.MONO, WavPack.HYBRID, WavPack.FLOAT, WavPack.INITIAL, WavPack.FINAL = 4, 8, 0x80, 0x800, 0x1000 WavPack.DSD = 0x80000000 -- header(buf, at, limit?) -> Header | nil, message: a valid "wvpk" block header at byte `at` whose block ends by limit function WavPack.header(buf: buffer, at: number, limit: number?): (Header?, string?) local lim = math.min(limit or buffer.len(buf), buffer.len(buf)) if at < 0 or at + 32 > lim then return nil, "truncated block header" end if buffer.readstring(buf, at, 4) ~= "wvpk" then return nil, "no block header" end local ck = buffer.readu32(buf, at + 4) local version = buffer.readu16(buf, at + 8) if ck % 2 == 1 or ck < 24 or ck > 0x1000000 then return nil, "bad block size" end if version < 0x402 or version > 0x410 then return nil, "unsupported: stream version " .. string.format("0x%x", version) end local total = buffer.readu32(buf, at + 12) local tu8 = buffer.readu8(buf, at + 11) local h: Header = { size = ck + 8, version = version, index = buffer.readu32(buf, at + 16) + buffer.readu8(buf, at + 10) * 4294967296, total = if total == 0xFFFFFFFF then -1 else total + tu8 * 4294967296 - tu8, samples = buffer.readu32(buf, at + 20), flags = buffer.readu32(buf, at + 24), crc = buffer.readu32(buf, at + 28), } if at + h.size > lim then return nil, "truncated block" end return h, nil end export type Info = { channels: number, channelMask: number, sampleRate: number, bitsPerSample: number, -- the source depth (container bytes * 8 - shift) bytesPerSample: number, -- the container: 1 .. 4 float: boolean, hybrid: boolean, md5: string?, -- 32 hex digits when stored version: number, } -- info(buf, at, limit?) -> Info | nil, Err: the facts of the stream whose first audio block is at `at` function WavPack.info(buf: buffer, at: number, limit: number?): (Info?, Err.Err?) local h, herr = WavPack.header(buf, at, limit) if not h then return nil, Err.from(herr, at) end local m, merr = Block.meta(buf, at, h.size) if not m then return nil, Err.from(merr, at) end local flags = h.flags if bit32.band(flags, WavPack.DSD) ~= 0 then return nil, Err.new(Err.UNSUPPORTED, "DSD audio") end local bytes = bit32.band(flags, 3) + 1 local info: Info = { channels = if bit32.band(flags, WavPack.MONO) ~= 0 then 1 else 2, channelMask = 0, sampleRate = 44100, bitsPerSample = bytes * 8 - bit32.band(bit32.rshift(flags, 13), 31), bytesPerSample = bytes, float = bit32.band(flags, WavPack.FLOAT) ~= 0, hybrid = bit32.band(flags, WavPack.HYBRID) ~= 0, version = h.version, } info.channelMask = if info.channels == 1 then 4 else 3 local sr = bit32.band(bit32.rshift(flags, 23), 15) if sr < 15 then info.sampleRate = RATES[sr + 1] end local o = m[0x27] -- ID_SAMPLE_RATE if o and (m[0x27 + 256] == 3 or m[0x27 + 256] == 4) then local rate = buffer.readu8(buf, o) + buffer.readu8(buf, o + 1) * 256 + buffer.readu8(buf, o + 2) * 65536 if m[0x27 + 256] == 4 then rate += bit32.band(buffer.readu8(buf, o + 3), 0x7F) * 16777216 end info.sampleRate = rate end o = m[0xD] -- ID_CHANNEL_INFO if o then local len = m[0xD + 256] if len == 0 or len > 7 then return nil, Err.new(Err.CORRUPT, "bad channel info", o) end local mask = 0 if len >= 6 then info.channels = buffer.readu8(buf, o) + bit32.band(buffer.readu8(buf, o + 2), 0xF) * 256 + 1 mask = buffer.readu8(buf, o + 3) + buffer.readu8(buf, o + 4) * 256 + buffer.readu8(buf, o + 5) * 65536 if len == 7 then mask += buffer.readu8(buf, o + 6) * 16777216 end else info.channels = buffer.readu8(buf, o) for k = 1, len - 1 do mask += buffer.readu8(buf, o + k) * 256 ^ (k - 1) end end info.channelMask = mask end if info.channels < 1 then return nil, Err.new(Err.CORRUPT, "no channels") end if info.sampleRate < 1 then return nil, Err.new(Err.CORRUPT, "bad sample rate") end return info, nil end -- md5(buf, at, limit?) -> 32 hex digits | nil: an ID_MD5_CHECKSUM in the block at `at` function WavPack.md5(buf: buffer, at: number, limit: number?): string? local h = WavPack.header(buf, at, limit) if not h then return nil end local m = Block.meta(buf, at, h.size) if not m then return nil end local o = m[0x26] if not o or m[0x26 + 256] ~= 16 then return nil end local hex = table.create(16, "") for k = 0, 15 do hex[k + 1] = string.format("%02x", buffer.readu8(buf, o + k)) end return table.concat(hex) end -- ------------------------------------------------------------------------------------------------ decoder export type Decoder = { channels: number, pcm: { number }, -- the last frame's samples, interleaved (integers in the container range, or floats) frame: (self: Decoder, buf: buffer, blocks: { number }, cbuf: buffer?, cblocks: { number }?) -> (number?, Err.Err?), state: Block.State, } -- frame(buf, blocks, cbuf?, cblocks?) -> frames | nil, Err: decode one frame (the blocks of one sample range, one per -- mono / stereo stream, in channel order) into dec.pcm; cblocks are the matching .wvc blocks (hybrid lossless) local function frame(self: Decoder, buf: buffer, blocks: { number }, cbuf: buffer?, cblocks: { number }?): (number?, Err.Err?) local C = self.channels local n = -1 local c0 = 0 for i, at in blocks do if c0 >= C then break end local h, herr = WavPack.header(buf, at) if not h then return nil, Err.from(herr, at) end if n < 0 then n = h.samples if n * C > 16777216 then return nil, Err.new(Err.LIMIT, "a frame of " .. n .. " samples x " .. C .. " channels", at) end if #self.pcm < n * C then self.pcm = table.create(n * C, 0) end elseif h.samples ~= n then return nil, Err.new(Err.CORRUPT, "blocks of a frame differ in length", at) end local cb, cp, cs = nil, nil, nil if cbuf and cblocks and cblocks[i] then local ch = WavPack.header(cbuf, cblocks[i]) if ch and ch.samples == h.samples and ch.index == h.index then cb, cp, cs = cbuf, cblocks[i], ch.size end end local got, msg = Block.decode(self.state, buf, at, h.size, self.pcm, c0, C, cb, cp, cs) if not got then return nil, Err.from(msg, at) end c0 += got end if n < 0 then return 0, nil end if c0 < C then return nil, Err.new(Err.CORRUPT, "a frame is missing channels") end return n, nil end -- decoder(channels) -> Decoder function WavPack.decoder(channels: number): Decoder return { channels = channels, pcm = table.create(4096 * channels, 0), frame = frame, state = Block.state(), } end WavPack.checksum = Block.checksum WavPack.meta = Block.meta return WavPack