merge ws/audio
This commit is contained in:
commit
fa98dd77c2
43 files changed
+8963
-19
No files matched your search
@@ -0,0 +1,308 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Pcm/Convert: raw sample formats <-> interleaved f32 (the hot loops of codecs/Pcm). Integers map to
|
||||
-- v / 2^(bits-1) (as ffmpeg does); encoding rounds to nearest and clips. G.711 A-law / mu-law per ITU-T G.711 (the
|
||||
-- classic Sun g711.c algorithm, written from the spec).
|
||||
--
|
||||
-- Formats: u8 s8 s16le s16be s24le s24be s32le s32be f32le f32be f64le f64be alaw ulaw (u8: offset binary).
|
||||
-- Never throws: callers check ranges (Convert.SIZE[fmt] * count bytes) before calling.
|
||||
local Convert = {}
|
||||
|
||||
local readu8, readi8, readi16, readi32, readu32 = buffer.readu8, buffer.readi8, buffer.readi16, buffer.readi32, buffer.readu32
|
||||
local readf32, readf64, writef32, writef64 = buffer.readf32, buffer.readf64, buffer.writef32, buffer.writef64
|
||||
local writeu8, writeu16, writeu32, writei16, writei32 = buffer.writeu8, buffer.writeu16, buffer.writeu32, buffer.writei16, buffer.writei32
|
||||
local floor = math.floor
|
||||
local byteswap = bit32.byteswap
|
||||
|
||||
local SIZE: { [string]: number } = {
|
||||
u8 = 1,
|
||||
s8 = 1,
|
||||
s16le = 2,
|
||||
s16be = 2,
|
||||
s24le = 3,
|
||||
s24be = 3,
|
||||
s32le = 4,
|
||||
s32be = 4,
|
||||
f32le = 4,
|
||||
f32be = 4,
|
||||
f64le = 8,
|
||||
f64be = 8,
|
||||
alaw = 1,
|
||||
ulaw = 1,
|
||||
}
|
||||
Convert.SIZE = SIZE
|
||||
-- the integer bit depth of each format (float: 32 / 64; G.711: 16 after expansion)
|
||||
Convert.BITS = {
|
||||
u8 = 8,
|
||||
s8 = 8,
|
||||
s16le = 16,
|
||||
s16be = 16,
|
||||
s24le = 24,
|
||||
s24be = 24,
|
||||
s32le = 32,
|
||||
s32be = 32,
|
||||
f32le = 32,
|
||||
f32be = 32,
|
||||
f64le = 64,
|
||||
f64be = 64,
|
||||
alaw = 16,
|
||||
ulaw = 16,
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ G.711
|
||||
local ALAW = table.create(256, 0) -- code -> linear s16
|
||||
local ULAW = table.create(256, 0)
|
||||
for code = 0, 255 do
|
||||
-- A-law
|
||||
local a = bit32.bxor(code, 0x55)
|
||||
local t = bit32.band(a, 0x0F) * 16
|
||||
local seg = bit32.rshift(bit32.band(a, 0x70), 4)
|
||||
if seg == 0 then
|
||||
t += 8
|
||||
elseif seg == 1 then
|
||||
t += 0x108
|
||||
else
|
||||
t = (t + 0x108) * 2 ^ (seg - 1)
|
||||
end
|
||||
ALAW[code + 1] = if bit32.band(a, 0x80) ~= 0 then t else -t
|
||||
-- mu-law
|
||||
local u = bit32.bxor(code, 0xFF)
|
||||
local m = (bit32.band(u, 0x0F) * 8 + 0x84) * 2 ^ bit32.rshift(bit32.band(u, 0x70), 4)
|
||||
ULAW[code + 1] = if bit32.band(u, 0x80) ~= 0 then 0x84 - m else m - 0x84
|
||||
end
|
||||
|
||||
local SEG_AEND = { 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF }
|
||||
local SEG_UEND = { 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF }
|
||||
local function segment(v: number, ends: { number }): number
|
||||
for i = 1, 8 do
|
||||
if v <= ends[i] then
|
||||
return i - 1
|
||||
end
|
||||
end
|
||||
return 8
|
||||
end
|
||||
|
||||
-- linear s16 -> A-law code
|
||||
local function alawEncode(pcm: number): number
|
||||
local mask
|
||||
local v = floor(pcm / 8) -- 13-bit magnitude domain
|
||||
if v >= 0 then
|
||||
mask = 0xD5
|
||||
else
|
||||
mask = 0x55
|
||||
v = -v - 1
|
||||
end
|
||||
local seg = segment(v, SEG_AEND)
|
||||
local aval
|
||||
if seg >= 8 then
|
||||
aval = 0x7F
|
||||
else
|
||||
aval = seg * 16
|
||||
if seg < 2 then
|
||||
aval += bit32.band(floor(v / 2), 0x0F)
|
||||
else
|
||||
aval += bit32.band(floor(v / 2 ^ seg), 0x0F)
|
||||
end
|
||||
end
|
||||
return bit32.bxor(aval, mask)
|
||||
end
|
||||
|
||||
-- linear s16 -> mu-law code
|
||||
local function ulawEncode(pcm: number): number
|
||||
local v = floor(pcm / 4) -- 14-bit
|
||||
local mask
|
||||
if v < 0 then
|
||||
v = -v
|
||||
mask = 0x7F
|
||||
else
|
||||
mask = 0xFF
|
||||
end
|
||||
if v > 8159 then
|
||||
v = 8159
|
||||
end
|
||||
v += 0x21
|
||||
local seg = segment(v, SEG_UEND)
|
||||
local uval
|
||||
if seg >= 8 then
|
||||
uval = 0x7F
|
||||
else
|
||||
uval = seg * 16 + bit32.band(floor(v / 2 ^ (seg + 1)), 0x0F)
|
||||
end
|
||||
return bit32.bxor(uval, mask)
|
||||
end
|
||||
Convert.alawEncode = alawEncode
|
||||
Convert.ulawEncode = ulawEncode
|
||||
Convert.ALAW = ALAW
|
||||
Convert.ULAW = ULAW
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- decode(src, so, fmt, count, dst, do_) -> true | false (unknown fmt): count samples at src[so] -> f32 at dst[do_]
|
||||
function Convert.decode(src: buffer, so: number, fmt: string, count: number, dst: buffer, d: number): boolean
|
||||
if fmt == "s16le" then
|
||||
for i = 0, count - 1 do
|
||||
writef32(dst, d + i * 4, readi16(src, so + i * 2) / 32768)
|
||||
end
|
||||
elseif fmt == "s16be" then
|
||||
for i = 0, count - 1 do
|
||||
local p = so + i * 2
|
||||
local v = readu8(src, p) * 256 + readu8(src, p + 1)
|
||||
if v >= 32768 then
|
||||
v -= 65536
|
||||
end
|
||||
writef32(dst, d + i * 4, v / 32768)
|
||||
end
|
||||
elseif fmt == "u8" then
|
||||
for i = 0, count - 1 do
|
||||
writef32(dst, d + i * 4, (readu8(src, so + i) - 128) / 128)
|
||||
end
|
||||
elseif fmt == "s8" then
|
||||
for i = 0, count - 1 do
|
||||
writef32(dst, d + i * 4, readi8(src, so + i) / 128)
|
||||
end
|
||||
elseif fmt == "s24le" then
|
||||
for i = 0, count - 1 do
|
||||
local p = so + i * 3
|
||||
local v = readu8(src, p) + readu8(src, p + 1) * 256 + readi8(src, p + 2) * 65536
|
||||
writef32(dst, d + i * 4, v / 8388608)
|
||||
end
|
||||
elseif fmt == "s24be" then
|
||||
for i = 0, count - 1 do
|
||||
local p = so + i * 3
|
||||
local v = readi8(src, p) * 65536 + readu8(src, p + 1) * 256 + readu8(src, p + 2)
|
||||
writef32(dst, d + i * 4, v / 8388608)
|
||||
end
|
||||
elseif fmt == "s32le" then
|
||||
for i = 0, count - 1 do
|
||||
writef32(dst, d + i * 4, readi32(src, so + i * 4) / 2147483648)
|
||||
end
|
||||
elseif fmt == "s32be" then
|
||||
for i = 0, count - 1 do
|
||||
local v = byteswap(readu32(src, so + i * 4))
|
||||
if v >= 2147483648 then
|
||||
v -= 4294967296
|
||||
end
|
||||
writef32(dst, d + i * 4, v / 2147483648)
|
||||
end
|
||||
elseif fmt == "f32le" then
|
||||
buffer.copy(dst, d, src, so, count * 4)
|
||||
elseif fmt == "f32be" then
|
||||
for i = 0, count - 1 do
|
||||
writeu32(dst, d + i * 4, byteswap(readu32(src, so + i * 4)))
|
||||
end
|
||||
elseif fmt == "f64le" then
|
||||
for i = 0, count - 1 do
|
||||
writef32(dst, d + i * 4, readf64(src, so + i * 8))
|
||||
end
|
||||
elseif fmt == "f64be" then
|
||||
local tmp = buffer.create(8)
|
||||
for i = 0, count - 1 do
|
||||
local p = so + i * 8
|
||||
writeu32(tmp, 0, byteswap(readu32(src, p + 4)))
|
||||
writeu32(tmp, 4, byteswap(readu32(src, p)))
|
||||
writef32(dst, d + i * 4, readf64(tmp, 0))
|
||||
end
|
||||
elseif fmt == "alaw" then
|
||||
for i = 0, count - 1 do
|
||||
writef32(dst, d + i * 4, ALAW[readu8(src, so + i) + 1] / 32768)
|
||||
end
|
||||
elseif fmt == "ulaw" then
|
||||
for i = 0, count - 1 do
|
||||
writef32(dst, d + i * 4, ULAW[readu8(src, so + i) + 1] / 32768)
|
||||
end
|
||||
else
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local function q(x: number, s: number): number
|
||||
local v = floor(x * s + 0.5)
|
||||
if v ~= v then
|
||||
return 0
|
||||
end
|
||||
if v >= s then
|
||||
return s - 1
|
||||
elseif v < -s then
|
||||
return -s
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
-- encode(src, so, count, fmt, dst, do_) -> true | false: count f32 samples at src[so] -> fmt at dst[do_]
|
||||
function Convert.encode(src: buffer, so: number, count: number, fmt: string, dst: buffer, d: number): boolean
|
||||
if fmt == "s16le" then
|
||||
for i = 0, count - 1 do
|
||||
writei16(dst, d + i * 2, q(readf32(src, so + i * 4), 32768))
|
||||
end
|
||||
elseif fmt == "s16be" then
|
||||
for i = 0, count - 1 do
|
||||
local v = q(readf32(src, so + i * 4), 32768) % 65536
|
||||
writeu16(dst, d + i * 2, (v % 256) * 256 + v // 256)
|
||||
end
|
||||
elseif fmt == "u8" then
|
||||
for i = 0, count - 1 do
|
||||
writeu8(dst, d + i, q(readf32(src, so + i * 4), 128) + 128)
|
||||
end
|
||||
elseif fmt == "s8" then
|
||||
for i = 0, count - 1 do
|
||||
writeu8(dst, d + i, q(readf32(src, so + i * 4), 128) % 256)
|
||||
end
|
||||
elseif fmt == "s24le" then
|
||||
for i = 0, count - 1 do
|
||||
local v = q(readf32(src, so + i * 4), 8388608) % 16777216
|
||||
local p = d + i * 3
|
||||
writeu8(dst, p, v % 256)
|
||||
writeu8(dst, p + 1, (v // 256) % 256)
|
||||
writeu8(dst, p + 2, v // 65536)
|
||||
end
|
||||
elseif fmt == "s24be" then
|
||||
for i = 0, count - 1 do
|
||||
local v = q(readf32(src, so + i * 4), 8388608) % 16777216
|
||||
local p = d + i * 3
|
||||
writeu8(dst, p, v // 65536)
|
||||
writeu8(dst, p + 1, (v // 256) % 256)
|
||||
writeu8(dst, p + 2, v % 256)
|
||||
end
|
||||
elseif fmt == "s32le" then
|
||||
for i = 0, count - 1 do
|
||||
writei32(dst, d + i * 4, q(readf32(src, so + i * 4), 2147483648))
|
||||
end
|
||||
elseif fmt == "s32be" then
|
||||
for i = 0, count - 1 do
|
||||
writeu32(dst, d + i * 4, byteswap(q(readf32(src, so + i * 4), 2147483648) % 4294967296))
|
||||
end
|
||||
elseif fmt == "f32le" then
|
||||
buffer.copy(dst, d, src, so, count * 4)
|
||||
elseif fmt == "f32be" then
|
||||
for i = 0, count - 1 do
|
||||
writeu32(dst, d + i * 4, byteswap(readu32(src, so + i * 4)))
|
||||
end
|
||||
elseif fmt == "f64le" then
|
||||
for i = 0, count - 1 do
|
||||
writef64(dst, d + i * 8, readf32(src, so + i * 4))
|
||||
end
|
||||
elseif fmt == "f64be" then
|
||||
local tmp = buffer.create(8)
|
||||
for i = 0, count - 1 do
|
||||
writef64(tmp, 0, readf32(src, so + i * 4))
|
||||
local p = d + i * 8
|
||||
writeu32(dst, p, byteswap(readu32(tmp, 4)))
|
||||
writeu32(dst, p + 4, byteswap(readu32(tmp, 0)))
|
||||
end
|
||||
elseif fmt == "alaw" then
|
||||
for i = 0, count - 1 do
|
||||
writeu8(dst, d + i, alawEncode(q(readf32(src, so + i * 4), 32768)))
|
||||
end
|
||||
elseif fmt == "ulaw" then
|
||||
for i = 0, count - 1 do
|
||||
writeu8(dst, d + i, ulawEncode(q(readf32(src, so + i * 4), 32768)))
|
||||
end
|
||||
else
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
return Convert
|
||||
@@ -0,0 +1,234 @@
|
||||
--!strict
|
||||
-- codecs/Pcm: PCM plumbing shared by audio formats: raw sample formats, streaming Decoders, encoder input sources.
|
||||
-- An encoder accepts a Types.Audio (fully decoded) or a Types.Decoder (streamed); Pcm.source turns either into one
|
||||
-- pull interface, so encoders stream in blocks without caring which they got. Uncompressed containers (aiff, au,
|
||||
-- voc, ...) get their Decoder from Pcm.rawDecoder (over a sample region of the file) or Pcm.audioDecoder.
|
||||
--
|
||||
-- local src, err = Pcm.source(audioOrDecoder) -- nil, Err.ARG for anything else
|
||||
-- local buf, n = src:read(4096) -- interleaved f32, n frames (0 at the end), like Decoder:read
|
||||
-- local audio = Pcm.collect(src) -- read everything into a Types.Audio
|
||||
-- local dec = Pcm.rawDecoder(buf, offset, frames, channels, rate, "s16be", tags?) -- a Types.Decoder
|
||||
-- local dec = Pcm.audioDecoder(audio) -- a Types.Decoder over a decoded Audio
|
||||
-- local raw = Pcm.encode(src, "s24le", writer) -- every frame of src appended to a core/Bytes Writer
|
||||
-- Pcm.Convert.decode / encode / SIZE / BITS -- the raw sample loops (codecs/Pcm/Convert)
|
||||
-- local s16 = Pcm.toInt(x, 16) -- round, clip to [-32768, 32767]
|
||||
-- local x = Pcm.fromInt(s16, 16) -- s / 32768
|
||||
--
|
||||
-- Sample formats: u8 s8 s16le s16be s24le s24be s32le s32be f32le f32be f64le f64be alaw ulaw.
|
||||
-- Never throws (a decoder's read that throws is caught and ends the stream).
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Convert = require("@formats/codecs/Pcm/Convert")
|
||||
|
||||
local Pcm = {}
|
||||
Pcm.Convert = Convert
|
||||
|
||||
export type Source = {
|
||||
sampleRate: number,
|
||||
channels: number,
|
||||
frames: number, -- total frames, 0 = unknown (a streaming decoder without an index)
|
||||
tags: Types.Tags,
|
||||
-- read(n, out?) -> interleaved f32 buffer, frames read (0 at the end)
|
||||
read: (self: Source, n: number, out: buffer?) -> (buffer, number),
|
||||
err: Err.Err?, -- set when the underlying decoder failed mid-stream
|
||||
}
|
||||
|
||||
local function audioRead(self: any, n: number, out: buffer?): (buffer, number)
|
||||
local a = self._audio
|
||||
local ch = a.channels
|
||||
local left = a.frames - self._pos
|
||||
if n > left then
|
||||
n = left
|
||||
end
|
||||
if n <= 0 then
|
||||
return out or buffer.create(0), 0
|
||||
end
|
||||
local bytes = n * ch * 4
|
||||
local b = if out and buffer.len(out) >= bytes then out else buffer.create(bytes)
|
||||
buffer.copy(b, 0, a.samples, self._pos * ch * 4, bytes)
|
||||
self._pos += n
|
||||
return b, n
|
||||
end
|
||||
|
||||
local function decoderRead(self: any, n: number, out: buffer?): (buffer, number)
|
||||
if self._done then
|
||||
return out or buffer.create(0), 0
|
||||
end
|
||||
local ok, b, got = pcall(self._dec.read, self._dec, n, out)
|
||||
if not ok then
|
||||
self._done = true
|
||||
self.err = Err.new(Err.CORRUPT, "source decoder failed: " .. tostring(b))
|
||||
return out or buffer.create(0), 0
|
||||
end
|
||||
if type(b) ~= "buffer" or type(got) ~= "number" or got <= 0 then
|
||||
self._done = true
|
||||
return out or buffer.create(0), 0
|
||||
end
|
||||
local ch = self.channels
|
||||
if buffer.len(b) < got * ch * 4 then
|
||||
got = buffer.len(b) // (ch * 4)
|
||||
end
|
||||
return b, got
|
||||
end
|
||||
|
||||
-- source(v) -> Source | nil, Err.ARG: v is a Types.Audio or a Types.Decoder (anything with sampleRate, channels, read)
|
||||
function Pcm.source(v: any): (Source?, Err.Err?)
|
||||
if type(v) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "needs an Audio or an audio Decoder")
|
||||
end
|
||||
local sr, ch = v.sampleRate, v.channels
|
||||
if type(sr) ~= "number" or sr <= 0 or sr ~= sr or type(ch) ~= "number" or ch < 1 or ch ~= math.floor(ch) then
|
||||
return nil, Err.new(Err.ARG, "bad sampleRate or channels")
|
||||
end
|
||||
if v.kind == "audio" and type(v.samples) == "buffer" then
|
||||
local frames = v.frames
|
||||
if type(frames) ~= "number" or frames < 0 or buffer.len(v.samples) < frames * ch * 4 then
|
||||
return nil, Err.new(Err.ARG, "the Audio's samples buffer is shorter than frames * channels * 4")
|
||||
end
|
||||
return { sampleRate = sr, channels = ch, frames = frames, tags = v.tags or {}, read = audioRead, _audio = v, _pos = 0 } :: any, nil
|
||||
end
|
||||
if type(v.read) == "function" then
|
||||
local frames = if type(v.frames) == "number" and v.frames > 0 then v.frames else 0
|
||||
return { sampleRate = sr, channels = ch, frames = frames, tags = v.tags or {}, read = decoderRead, _dec = v, _done = false } :: any, nil
|
||||
end
|
||||
return nil, Err.new(Err.ARG, "needs an Audio or an audio Decoder")
|
||||
end
|
||||
|
||||
-- collect(src, maxFrames?) -> Types.Audio | nil, Err (everything the source has; Err.LIMIT past maxFrames)
|
||||
function Pcm.collect(src: Source, maxFrames: number?): (Types.Audio?, Err.Err?)
|
||||
local ch = src.channels
|
||||
local cap = math.max(src.frames, 4096)
|
||||
local data = buffer.create(cap * ch * 4)
|
||||
local total = 0
|
||||
local limit = maxFrames or 2 ^ 31
|
||||
local tmp: buffer? = nil
|
||||
while true do
|
||||
local b, n = src:read(16384, tmp)
|
||||
if n <= 0 then
|
||||
break
|
||||
end
|
||||
tmp = b
|
||||
if total + n > limit then
|
||||
return nil, Err.new(Err.LIMIT, string.format("more than %d frames", limit))
|
||||
end
|
||||
if total + n > cap then
|
||||
cap = math.max(cap * 2, total + n)
|
||||
local nd = buffer.create(cap * ch * 4)
|
||||
buffer.copy(nd, 0, data, 0, total * ch * 4)
|
||||
data = nd
|
||||
end
|
||||
buffer.copy(data, total * ch * 4, b, 0, n * ch * 4)
|
||||
total += n
|
||||
end
|
||||
if src.err then
|
||||
return nil, src.err
|
||||
end
|
||||
local a = Types.Audio.new(src.sampleRate, ch, 0)
|
||||
a.frames = total
|
||||
if buffer.len(data) == total * ch * 4 then
|
||||
a.samples = data
|
||||
else
|
||||
a.samples = buffer.create(total * ch * 4)
|
||||
buffer.copy(a.samples, 0, data, 0, total * ch * 4)
|
||||
end
|
||||
a.tags = table.clone(src.tags)
|
||||
return a, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decoders
|
||||
-- rawDecoder(buf, offset, frames, channels, sampleRate, fmt, tags?) -> Types.Decoder | nil, Err.ARG: a streaming
|
||||
-- decoder over frames * channels samples of fmt at buf[offset] (the caller has checked the range is inside buf)
|
||||
function Pcm.rawDecoder(buf: buffer, offset: number, frames: number, channels: number, sampleRate: number, fmt: string, tags: Types.Tags?): (Types.Decoder?, Err.Err?)
|
||||
local size = Convert.SIZE[fmt]
|
||||
if not size then
|
||||
return nil, Err.new(Err.ARG, "unknown sample format " .. tostring(fmt))
|
||||
end
|
||||
if channels < 1 or sampleRate <= 0 or frames < 0 or offset < 0 or offset + frames * channels * size > buffer.len(buf) then
|
||||
return nil, Err.new(Err.ARG, "bad raw PCM layout")
|
||||
end
|
||||
local pos = 0
|
||||
local frameBytes = channels * size
|
||||
local dec = {
|
||||
sampleRate = sampleRate,
|
||||
channels = channels,
|
||||
frames = frames,
|
||||
duration = frames / sampleRate,
|
||||
bitrate = sampleRate * frameBytes * 8,
|
||||
tags = tags or {},
|
||||
}
|
||||
function dec.read(_self: any, n: number, out: buffer?): (buffer, number)
|
||||
n = math.clamp(math.floor(n), 0, frames - pos)
|
||||
local bytes = n * channels * 4
|
||||
local b = if out and buffer.len(out) >= bytes then out else buffer.create(bytes)
|
||||
if n > 0 then
|
||||
Convert.decode(buf, offset + pos * frameBytes, fmt, n * channels, b, 0)
|
||||
pos += n
|
||||
end
|
||||
return b, n
|
||||
end
|
||||
function dec.seek(_self: any, seconds: number)
|
||||
local f = math.floor(seconds * sampleRate + 0.5)
|
||||
pos = if f ~= f then 0 else math.clamp(f, 0, frames)
|
||||
end
|
||||
function dec.position(_self: any): number
|
||||
return pos / sampleRate
|
||||
end
|
||||
return dec :: any, nil
|
||||
end
|
||||
|
||||
-- audioDecoder(audio) -> Types.Decoder over a decoded Audio (no copy of the samples)
|
||||
function Pcm.audioDecoder(a: Types.Audio): Types.Decoder
|
||||
local d = Pcm.rawDecoder(a.samples, 0, a.frames, a.channels, a.sampleRate, "f32le", a.tags) :: any
|
||||
d.bitrate = a.sampleRate * a.channels * 32
|
||||
return d
|
||||
end
|
||||
|
||||
-- encode(src, fmt, w) -> frames written | nil, Err: drains src into the Writer w as fmt samples
|
||||
function Pcm.encode(src: Source, fmt: string, w: Bytes.Writer): (number?, Err.Err?)
|
||||
local size = Convert.SIZE[fmt]
|
||||
if not size then
|
||||
return nil, Err.new(Err.ARG, "unknown sample format " .. tostring(fmt))
|
||||
end
|
||||
local ch = src.channels
|
||||
local total = 0
|
||||
local tmp: buffer? = nil
|
||||
while true do
|
||||
local b, n = src:read(8192, tmp)
|
||||
if n <= 0 then
|
||||
break
|
||||
end
|
||||
tmp = b
|
||||
local at = w:reserve(n * ch * size)
|
||||
Convert.encode(b, 0, n * ch, fmt, w.buf, at)
|
||||
w.pos = at + n * ch * size
|
||||
total += n
|
||||
end
|
||||
if src.err then
|
||||
return nil, src.err
|
||||
end
|
||||
return total, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ conversions
|
||||
-- toInt(x, bits) -> the nearest bits-wide signed integer to x * 2^(bits-1), clipped (NaN -> 0)
|
||||
function Pcm.toInt(x: number, bits: number): number
|
||||
local s = 2 ^ (bits - 1)
|
||||
local v = math.floor(x * s + 0.5)
|
||||
if v ~= v then
|
||||
return 0
|
||||
end
|
||||
if v >= s then
|
||||
return s - 1
|
||||
elseif v < -s then
|
||||
return -s
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
-- fromInt(v, bits) -> v / 2^(bits-1)
|
||||
function Pcm.fromInt(v: number, bits: number): number
|
||||
return v / 2 ^ (bits - 1)
|
||||
end
|
||||
|
||||
return Pcm
|
||||
Reference in new issue
Block a user