ws-audio: audio/aiff (AIFF/AIFC incl. sowt/fl32/fl64/ulaw/alaw), audio/au, audio/voc: decode/open/encode; codecs/Pcm folder (+ Convert: raw sample formats <-> f32, G.711, rawDecoder/audioDecoder/encode); fixtures vs ffmpeg/sox, encoder outputs decoded by ffmpeg in the fixture step
This commit is contained in:
1 parent
bff4999208
commit
0f395e3833
14 files changed
+1940
-6
No files matched your search
@@ -0,0 +1,166 @@
|
||||
--!strict
|
||||
-- audio/au: Sun / NeXT audio (.au / .snd), big endian. New code from the Sun audio file format description.
|
||||
-- Supports: decode / open of encodings 1 (G.711 mu-law), 2 (8-bit), 3 (16-bit), 4 (24-bit), 5 (32-bit linear),
|
||||
-- 6 (32-bit float), 7 (64-bit float), 27 (G.711 A-law); an unknown data size (0xFFFFFFFF: to the end of the file);
|
||||
-- the annotation as tags.comment. Encode (Audio or Decoder, streamed) to any of those encodings (default 16-bit).
|
||||
-- Not supported: G.721 / G.722 / G.723 ADPCM and the other NeXT encodings (Err.UNSUPPORTED).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Pcm = require("@formats/codecs/Pcm")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Au = {
|
||||
name = "au",
|
||||
category = "audio",
|
||||
extensions = { "au", "snd" },
|
||||
mime = { "audio/basic", "audio/x-au" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Pcm" },
|
||||
caps = { decode = true, encode = true, streaming = true, depths = { 8, 16, 24, 32 }, float = true, lossy = false },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxFrames: number?, -- refuse more sample frames (default 2^28)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
sampleFormat: ("s8" | "s16" | "s24" | "s32" | "f32" | "f64" | "ulaw" | "alaw")?, -- default "s16"
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local ENCODINGS: { [number]: string } = {
|
||||
[1] = "ulaw",
|
||||
[2] = "s8",
|
||||
[3] = "s16be",
|
||||
[4] = "s24be",
|
||||
[5] = "s32be",
|
||||
[6] = "f32be",
|
||||
[7] = "f64be",
|
||||
[27] = "alaw",
|
||||
}
|
||||
local WRITE = {
|
||||
ulaw = { 1, "ulaw" },
|
||||
s8 = { 2, "s8" },
|
||||
s16 = { 3, "s16be" },
|
||||
s24 = { 4, "s24be" },
|
||||
s32 = { 5, "s32be" },
|
||||
f32 = { 6, "f32be" },
|
||||
f64 = { 7, "f64be" },
|
||||
alaw = { 27, "alaw" },
|
||||
}
|
||||
local BITS = { ulaw = 8, alaw = 8, s8 = 8, s16be = 16, s24be = 24, s32be = 32, f32be = 32, f64be = 64 }
|
||||
|
||||
local function be32(b: buffer, o: number): number
|
||||
return bit32.byteswap(buffer.readu32(b, o))
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Au.sniff(r: Bytes.Reader): number
|
||||
return if r:peek(4) == ".snd" then 1 else 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Au.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Audio?, Err.Err?)
|
||||
local dec, err = Au.open(r, opts)
|
||||
if not dec then
|
||||
return nil, err
|
||||
end
|
||||
local d = dec :: any
|
||||
local a = Types.Audio.new(d.sampleRate, d.channels, d.frames)
|
||||
d:read(d.frames, a.samples)
|
||||
a.tags = d.tags
|
||||
a.meta.source = d.source
|
||||
return a, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
function Au.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local src, serr = Pcm.source(value)
|
||||
if not src then
|
||||
return nil, serr
|
||||
end
|
||||
local o: EncodeOptions = opts or {}
|
||||
local spec = WRITE[o.sampleFormat or "s16"]
|
||||
if not spec then
|
||||
return nil, Err.new(Err.ARG, "unknown sampleFormat " .. tostring(o.sampleFormat))
|
||||
end
|
||||
if src.sampleRate ~= math.floor(src.sampleRate) or src.sampleRate >= 2 ^ 32 then
|
||||
return nil, Err.new(Err.ARG, "the sample rate must be an integer")
|
||||
end
|
||||
-- annotation: the comment tag, NUL-terminated, padded to a multiple of 8 (at least 4 bytes, as Sun requires)
|
||||
local note = if type(src.tags.comment) == "string" then src.tags.comment else ""
|
||||
local alen = #note + 1
|
||||
alen = math.max(8, alen + (8 - alen % 8) % 8)
|
||||
local w = Bytes.writer(65536)
|
||||
w:string(".snd")
|
||||
w:u32be(24 + alen)
|
||||
w:u32be(0xFFFFFFFF) -- patched
|
||||
w:u32be(spec[1])
|
||||
w:u32be(src.sampleRate)
|
||||
w:u32be(src.channels)
|
||||
w:string(note)
|
||||
w:fill(0, alen - #note)
|
||||
local frames, perr = Pcm.encode(src, spec[2], w)
|
||||
if not frames then
|
||||
return nil, perr
|
||||
end
|
||||
w:setU32be(8, w:tell() - 24 - alen)
|
||||
return w:finish(), nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ open
|
||||
function Au.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.Err?)
|
||||
local b, base, limit = r.buf, r.base, r.limit
|
||||
if limit - base < 24 or buffer.readstring(b, base, 4) ~= ".snd" then
|
||||
return nil, Err.new(Err.CORRUPT, "bad magic", base)
|
||||
end
|
||||
local off, size, enc, rate, ch = be32(b, base + 4), be32(b, base + 8), be32(b, base + 12), be32(b, base + 16), be32(b, base + 20)
|
||||
local fmt = ENCODINGS[enc]
|
||||
if not fmt then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "encoding " .. enc, base + 12)
|
||||
end
|
||||
if ch < 1 or ch > 65535 then
|
||||
return nil, Err.new(Err.CORRUPT, "bad channel count " .. ch, base + 20)
|
||||
end
|
||||
if rate < 1 then
|
||||
return nil, Err.new(Err.CORRUPT, "bad sample rate", base + 16)
|
||||
end
|
||||
if off < 24 or off > limit - base then
|
||||
return nil, Err.new(Err.CORRUPT, "bad data offset", base + 4)
|
||||
end
|
||||
local tags: Types.Tags = {}
|
||||
if off > 24 then
|
||||
local s = buffer.readstring(b, base + 24, off - 24)
|
||||
local z = string.find(s, "\0", 1, true)
|
||||
if z then
|
||||
s = string.sub(s, 1, z - 1)
|
||||
end
|
||||
if #s > 0 then
|
||||
tags.comment = s
|
||||
end
|
||||
end
|
||||
local avail = limit - base - off
|
||||
local truncated = false
|
||||
if size == 0xFFFFFFFF then
|
||||
size = avail
|
||||
elseif size > avail then
|
||||
size = avail
|
||||
truncated = true
|
||||
end
|
||||
local frameBytes = ch * Pcm.Convert.SIZE[fmt]
|
||||
local frames = size // frameBytes
|
||||
local maxFrames = opts and opts.maxFrames or 2 ^ 28
|
||||
if frames > maxFrames then
|
||||
return nil, Err.new(Err.LIMIT, string.format("%d frames is over the %d frame limit", frames, maxFrames))
|
||||
end
|
||||
local dec, derr = Pcm.rawDecoder(b, base + off, frames, ch, rate, fmt, tags)
|
||||
if not dec then
|
||||
return nil, derr
|
||||
end
|
||||
local d = dec :: any
|
||||
d.source = { format = "au", encoding = enc, sampleFormat = fmt, bitDepth = BITS[fmt], truncated = truncated or nil }
|
||||
return dec, nil
|
||||
end
|
||||
|
||||
return Au
|
||||
Reference in new issue
Block a user