codecs/audio: comments to specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
6dcce481bb
69 files changed
+523
-1604
No files matched your search
@@ -1,21 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/audio/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/audio/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("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Types = require("@rosetta/Types")
|
||||
@@ -41,11 +25,10 @@ Pcm.WAVE_FROM_VORBIS = {
|
||||
export type Source = {
|
||||
sampleRate: number,
|
||||
channels: number,
|
||||
frames: number, -- total frames, 0 = unknown (a streaming decoder without an index)
|
||||
frames: number,
|
||||
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
|
||||
err: Err.Err?,
|
||||
}
|
||||
|
||||
local function audioRead(self: any, n: number, out: buffer?): (buffer, number)
|
||||
@@ -86,7 +69,6 @@ local function decoderRead(self: any, n: number, out: buffer?): (buffer, number)
|
||||
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")
|
||||
@@ -109,7 +91,6 @@ function Pcm.source(v: any): (Source?, Err.Err?)
|
||||
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)
|
||||
@@ -117,7 +98,6 @@ function Pcm.collect(src: Source, maxFrames: number?): (Types.Audio?, Err.Err?)
|
||||
local total = 0
|
||||
local limit = maxFrames or 2 ^ 31
|
||||
local tmp: buffer? = nil
|
||||
-- a known length: one read straight into the result (decoders write into a big enough out buffer: no copy)
|
||||
if src.frames > 0 and src.frames <= limit then
|
||||
local b, n = src:read(src.frames, data)
|
||||
if n > 0 then
|
||||
@@ -160,9 +140,6 @@ function Pcm.collect(src: Source, maxFrames: number?): (Types.Audio?, Err.Err?)
|
||||
return a, nil
|
||||
end
|
||||
|
||||
-- drain(dec, source?, maxFrames?) -> Types.Audio | nil, Err: a format's decode() through its open(): every frame of
|
||||
-- the Decoder (dec.frames is only a hint: short or long streams keep what decodes), dec.tags, meta.source = source
|
||||
-- (default dec.source). A decoder that recorded a failure in dec.error (an Err) fails the drain.
|
||||
function Pcm.drain(dec: any, source: { [string]: any }?, maxFrames: number?): (Types.Audio?, Err.Err?)
|
||||
local src, err = Pcm.source(dec)
|
||||
if not src then
|
||||
@@ -180,9 +157,6 @@ function Pcm.drain(dec: any, source: { [string]: any }?, maxFrames: number?): (T
|
||||
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
|
||||
@@ -221,14 +195,12 @@ function Pcm.rawDecoder(buf: buffer, offset: number, frames: number, channels: n
|
||||
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
|
||||
@@ -254,9 +226,6 @@ function Pcm.encode(src: Source, fmt: string, w: Bytes.Writer): (number?, Err.Er
|
||||
return total, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ conversions
|
||||
-- reorder(buf, offset, frames, channels, map): in place, frame by frame, channel i of the result = channel map[i]
|
||||
-- (1-based) of the input; for converting between channel orders (Pcm.WAVE_FROM_VORBIS)
|
||||
function Pcm.reorder(buf: buffer, offset: number, frames: number, channels: number, map: { number })
|
||||
local tmp = table.create(channels, 0)
|
||||
local readf32, writef32 = buffer.readf32, buffer.writef32
|
||||
@@ -271,7 +240,6 @@ function Pcm.reorder(buf: buffer, offset: number, frames: number, channels: numb
|
||||
end
|
||||
end
|
||||
|
||||
-- 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)
|
||||
@@ -286,7 +254,6 @@ function Pcm.toInt(x: number, bits: number): number
|
||||
return v
|
||||
end
|
||||
|
||||
-- fromInt(v, bits) -> v / 2^(bits-1)
|
||||
function Pcm.fromInt(v: number, bits: number): number
|
||||
return v / 2 ^ (bits - 1)
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user