ws-apifix: music is audio (F.save / F.convert of a Module or Midi to an audio format renders it, render options from opts; F.audio.open / load render music bytes), F.music builder (ops/MusicOps: new, addPattern, setCell / getCell, addSample, addInstrument, note; also F.ops.music), MIDI writer writes track.name as a 0x03 event

api (init.luau): detectFor() falls back from audio to rendering music formats; encoderFor finds audio formats for
music values; renderMusic() through codecs/audio/Tracker or music/midi render.
This commit is contained in:
4DBug committed 2026-10-31 13:56:21 -05:00
1 parent 25d8e4ff25
commit 10bd6b021f
7 files changed
+692 -16

No files matched your search

+62 -3
View File
@@ -129,6 +129,20 @@ local function detect(buf: buffer, name: string?, category: string?, format: str
return nil, Err.new(Err.UNKNOWN_FORMAT, "unrecognised data" .. (if name then " (" .. name .. ")" else "") .. (if category then " for " .. category else ""))
end
-- detectFor(buf, name, o) -> entry | nil, Err, rendered: detect with o.category / o.format; every music file that
-- renders (MOD, S3M, XM, IT, MIDI) is also audio, so an audio lookup that finds nothing falls back to them (rendered =
-- true: open the format, which renders it, instead of decoding it)
local function detectFor(buf: buffer, name: string?, o: { [string]: any }): (Registry.Entry?, Err.Err?, boolean)
local entry, err = detect(buf, name, o.category, o.format)
if not entry and o.category == "audio" then
local e = detect(buf, name, "music", o.format)
if e and e.caps.render then
return e, nil, true
end
end
return entry, err, false
end
local function tag(value: any, formatName: string)
if type(value) == "table" then
local meta = value.meta
@@ -314,7 +328,7 @@ function F.load(src: any, opts: Options?): (any, Err.Err?)
o = table.clone(o)
o.name = name -- formats may use the file name (single-stream archives name their entry after it)
end
local entry, derr = detect(buf, name, o.category, o.format)
local entry, derr, rendered = detectFor(buf, name, o)
if not entry then
return nil, derr
end
@@ -328,7 +342,7 @@ function F.load(src: any, opts: Options?): (any, Err.Err?)
o.resolve = siblings(name)
end
local value, err
if m.decode then
if m.decode and not rendered then
local ok, v, e = pcall(m.decode, Bytes.reader(buf), withHooks(o))
if not ok then
return nil, Err.wrap(Err.new(Err.CORRUPT, "decoder threw: " .. tostring(v)), entry.name)
@@ -362,7 +376,7 @@ function F.open(src: any, opts: Options?): (any, Err.Err?)
if not buf then
return nil, ierr
end
local entry, derr = detect(buf, name, o.category, o.format)
local entry, derr = detectFor(buf, name, o)
if not entry and o.category == "video" and o.format == nil then
entry = detect(buf, name, "image", nil) -- animated images open as frames-backed videos
end
@@ -398,6 +412,29 @@ end
local encodeTextures: (scene: any) -> Err.Err?
-- renderMusic(value, opts) -> the audio Decoder of a Module (codecs/audio/Tracker) or a Midi (music/midi render) |
-- nil, Err. opts are the render options: sampleRate, soundfont, gain, tail, interpolation, stereoSeparation, ...
local function renderMusic(value: any, o: Options): (any, Err.Err?)
local ok, dec, err
if value.kind == "midi" then
local m, merr = F.format("midi")
if not m then
return nil, merr
end
ok, dec, err = pcall(m.render, value, o)
else
local Tracker: any = require("@formats/codecs/audio/Tracker")
ok, dec, err = pcall(Tracker.render, value, o)
end
if not ok then
return nil, Err.new(Err.ARG, "render threw: " .. tostring(dec))
end
if dec == nil then
return nil, err or Err.new(Err.ARG, "render failed")
end
return dec, nil
end
-- the writable format for a value: opts.format, the destination's extension, or the source format
local function encoderFor(value: any, dest: string?, o: Options): (Registry.Entry?, any, Err.Err?, string?)
local category = o.category
@@ -415,6 +452,9 @@ local function encoderFor(value: any, dest: string?, o: Options): (Registry.Entr
if not entry and category == "image" and type(value) == "table" and value.kind == "image" then
entry = find(key, "video") -- an (animated) image written as a video: F.convert(gif, "avi")
end
if not entry and category == "music" and type(value) == "table" and (value.kind == "music" or value.kind == "midi") then
entry = find(key, "audio") -- a Module / Midi saved as audio is rendered first (F.save): F.convert(mod, "wav")
end
if not entry then
return nil, nil, Err.new(Err.UNKNOWN_FORMAT, "no format named " .. key .. (if category then " for " .. category else ""))
end
@@ -476,6 +516,13 @@ function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
end
value = v
end
if entry.category == "audio" and type(value) == "table" and (value.kind == "music" or value.kind == "midi") then
local dec, rerr = renderMusic(value, o) -- the render options (sampleRate, soundfont, loop, ...) come from opts
if not dec then
return nil, rerr
end
value = dec
end
if type(value) == "table" and value.kind == "scene" then
local terr = encodeTextures(value)
if terr then
@@ -616,6 +663,14 @@ for _, fn in { "toBodies", "fromBodies", "tessellate" } do
return a, b
end
end
-- F.music.new / addPattern / setCell / getCell / addSample / addInstrument / note: building and editing tracker
-- Modules (ops/MusicOps, loaded on first use): F.music.new({ format = "mod", channels = 4 }) -> Module
for _, fn in { "new", "addPattern", "setCell", "getCell", "addSample", "addInstrument", "note" } do
local name: string = fn
F.music[name] = function(...: any): (any, Err.Err?)
return (F.ops.music :: any)[name](...)
end
end
-- F.video.fromImage(img, opts?) -> a frames-backed VideoDecoder: an animated GIF / APNG / WebP (img.frames, composited
-- onto full canvases by ImageOps.coalesce) or a still image (one frame). opts.rgba is accepted (frames are rgba8).
F.video.fromImage = function(img: any, opts: Options?): (any, Err.Err?)
@@ -687,6 +742,7 @@ end
-- F.ops.layers ops/LayerOps flatten / flattenFrames a Layered (PSD, Aseprite), leaves
-- F.ops.audio ops/AudioOps toAudio, gain / gainDb / normalize, mix, remix / downmix / upmix, resample, trim,
-- concat, fade, peak, rms
-- F.ops.music ops/MusicOps new, addPattern, setCell / getCell, addSample, addInstrument, note (also on F.music)
-- F.ops.vector.rasterize(vector, w?, h?, opts?) -> rgba8 Image: the SVG renderer (vector/svg) for any Vector
local OPS: { [string]: () -> any } = {
image = function()
@@ -701,6 +757,9 @@ local OPS: { [string]: () -> any } = {
audio = function()
return require("@formats/ops/AudioOps")
end,
music = function()
return require("@formats/ops/MusicOps")
end,
vector = function()
return {
rasterize = function(v: any, w: number?, h: number?, opts: any?): (any, Err.Err?)