comments: ops LayerOps / MusicOps / Greedy
This commit is contained in:
1 parent
facdf124a1
commit
b8db3ed551
3 files changed
+5
-41
No files matched your search
@@ -2,13 +2,6 @@
|
||||
-- ops/LayerOps: editing on the Layered type (PSD, Aseprite): flatten a layer tree (or one animation frame) to an
|
||||
-- rgba8 Image with the blend modes of codecs/image/Blend, opacity, groups (isolated, or pass-through), clipping layers and
|
||||
-- grey layer masks; every frame as an animated Image; walking the leaves.
|
||||
--
|
||||
-- local LayerOps = require("@rosetta/ops/LayerOps")
|
||||
-- local img = LayerOps.flatten(layered) -- frame 1 / the layers
|
||||
-- local img3 = LayerOps.flatten(layered, { frame = 3 })
|
||||
-- local anim = LayerOps.flattenFrames(layered) -- an Image with frames (delays from the Layered frames)
|
||||
-- for layer, depth in LayerOps.leaves(layered.layers) do ... end
|
||||
--
|
||||
-- Never throws: bad values return nil, Err.ARG.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
@@ -22,7 +15,6 @@ export type FlattenOptions = {
|
||||
background: { number }?, -- r, g, b, a 0..255 under everything (default transparent)
|
||||
}
|
||||
|
||||
-- flatten(layered, opts?) -> rgba8 Image (layered.width x layered.height) | nil, Err
|
||||
function LayerOps.flatten(layered: Types.Layered, opts: FlattenOptions?): (Types.Image?, Err.Err?)
|
||||
if type(layered) ~= "table" or type(layered.width) ~= "number" or type(layered.height) ~= "number" or type(layered.layers) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "flatten: needs a Layered value")
|
||||
@@ -54,7 +46,6 @@ function LayerOps.flatten(layered: Types.Layered, opts: FlattenOptions?): (Types
|
||||
return img, nil
|
||||
end
|
||||
|
||||
-- flattenFrames(layered, opts?) -> an rgba8 Image whose frames are every flattened frame (frame 1 is the image)
|
||||
function LayerOps.flattenFrames(layered: Types.Layered, opts: FlattenOptions?): (Types.Image?, Err.Err?)
|
||||
local first, err = LayerOps.flatten(layered, opts)
|
||||
if not first then
|
||||
@@ -78,7 +69,6 @@ function LayerOps.flattenFrames(layered: Types.Layered, opts: FlattenOptions?):
|
||||
return first, nil
|
||||
end
|
||||
|
||||
-- leaves(layers) -> iterator of (layer, depth) over the non-group layers of a tree, bottom to top
|
||||
function LayerOps.leaves(layers: { Types.Layer }): () -> (Types.Layer?, number?)
|
||||
local stack: { { any } } = { { layers, 0, 0 } }
|
||||
return function(): (Types.Layer?, number?)
|
||||
|
||||
@@ -19,7 +19,7 @@ export type Out = {
|
||||
quads: number,
|
||||
}
|
||||
|
||||
-- run(data, sx, sy, sz, ox, oy, oz, scale) -> Out: vertex position = (cell corner + (ox, oy, oz)) * scale
|
||||
-- vertex position = (cell corner + (ox, oy, oz)) * scale
|
||||
function Greedy.run(data: buffer, sx: number, sy: number, sz: number, ox: number, oy: number, oz: number, scale: number): Out
|
||||
local cap = 1024
|
||||
local pos = buffer.create(cap * 48)
|
||||
@@ -89,7 +89,6 @@ function Greedy.run(data: buffer, sx: number, sy: number, sz: number, ox: number
|
||||
writei32(mask, (i + k + jj * nu) * 4, 0)
|
||||
end
|
||||
end
|
||||
-- emit the rectangle
|
||||
if quads == cap then
|
||||
local ncap = cap * 2
|
||||
local function grow2(b: buffer, per: number): buffer
|
||||
@@ -104,7 +103,6 @@ function Greedy.run(data: buffer, sx: number, sy: number, sz: number, ox: number
|
||||
corner[u + 1] = i
|
||||
corner[v + 1] = j
|
||||
local cx, cy, cz = corner[1], corner[2], corner[3]
|
||||
-- the u and v edge vectors
|
||||
local ux = if u == 0 then w else 0
|
||||
local uy = if u == 1 then w else 0
|
||||
local uz = if u == 2 then w else 0
|
||||
|
||||
@@ -1,16 +1,6 @@
|
||||
--!strict
|
||||
-- ops/MusicOps: building and editing tracker Modules (layer 5.5, over codecs/audio/Tracker) without touching the
|
||||
-- engine's internal layout (5-byte cells, f32 sample buffers, tuning fields): a new module, patterns, cells by name,
|
||||
-- samples from an Audio, instruments. New code. F.music.new / addPattern / setCell / getCell / addSample /
|
||||
-- addInstrument / note in the api (also F.ops.music).
|
||||
--
|
||||
-- local M = require("@rosetta/ops/MusicOps")
|
||||
-- local song = M.new({ format = "mod", channels = 4, title = "demo" })
|
||||
-- local s = M.addSample(song, { name = "beep", audio = someAudio, loop = "forward" }) -- sample 1
|
||||
-- local p = M.addPattern(song) -- pattern 0 (64 rows), appended to the order list
|
||||
-- M.setCell(song, p, 0, 1, { note = "C-4", instrument = s, effect = 0xC, param = 40 })
|
||||
-- local wav = F.save(song, nil, { format = "wav" }) -- rendered; or F.save(song) for the .mod
|
||||
--
|
||||
-- ops/MusicOps: building and editing tracker Modules over codecs/audio/Tracker without touching the engine's internal
|
||||
-- layout (5-byte cells, f32 samples, tuning fields). New code.
|
||||
-- Conventions: pattern numbers are 0-based (as the order list names them), rows 0-based, channels 1-based, sample
|
||||
-- and instrument numbers 1-based (0 = none in a cell). Notes are numbers (1 = C-0, 49 = C-4, 61 = C-5; 254 cut, 255
|
||||
-- key off, 253 fade) or names: "C-4", "C#4", "Db4", "off", "cut", "fade". Effects keep the format's own numbering
|
||||
@@ -35,7 +25,6 @@ local function int(v: any, lo: number, hi: number): boolean
|
||||
return type(v) == "number" and v == v and v % 1 == 0 and v >= lo and v <= hi
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ module
|
||||
export type NewOptions = {
|
||||
format: string?, -- "mod" (default), "xm", "s3m", "it": the effect family (mod and xm can be saved)
|
||||
channels: number?, -- 1..32 for mod, else 1..64 (default 4)
|
||||
@@ -44,7 +33,6 @@ export type NewOptions = {
|
||||
tempo: number?, -- BPM (default 125)
|
||||
}
|
||||
|
||||
-- new(opts?) -> an empty Module (kind "music") with a render method; meta.source.format = the format
|
||||
function MusicOps.new(opts: NewOptions?): (any, Err.Err?)
|
||||
local o: NewOptions = opts or {}
|
||||
local format = o.format or "mod"
|
||||
@@ -73,9 +61,6 @@ function MusicOps.new(opts: NewOptions?): (any, Err.Err?)
|
||||
return m, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ patterns
|
||||
-- addPattern(module, rows?, opts?) -> the new pattern's number (0-based) | nil, Err. rows default 64 (MOD needs 64).
|
||||
-- The pattern is appended to the order list unless opts.order == false.
|
||||
function MusicOps.addPattern(m: any, rows: number?, opts: { order: boolean? }?): (number?, Err.Err?)
|
||||
if not isModule(m) then
|
||||
return bad("addPattern: needs a tracker Module")
|
||||
@@ -95,8 +80,6 @@ end
|
||||
local NAMES: { [string]: number } = { c = 0, d = 2, e = 4, f = 5, g = 7, a = 9, b = 11 }
|
||||
local SPECIAL: { [string]: number } = { off = 255, ["==="] = 255, cut = 254, ["^^^"] = 254, fade = 253, ["~~~"] = 253 }
|
||||
|
||||
-- note(nameOrNumber) -> the note number (1 = C-0 .. 120 = B-9, 253 / 254 / 255) | nil, Err: "C-4" = 49, "C#4" = 50,
|
||||
-- "Db4" = 50, "off", "cut", "fade"
|
||||
function MusicOps.note(v: any): (number?, Err.Err?)
|
||||
if type(v) == "number" then
|
||||
if int(v, 0, 120) or v == 253 or v == 254 or v == 255 then
|
||||
@@ -148,7 +131,6 @@ local function patternAt(m: any, pattern: any, row: any, channel: any): (any, Er
|
||||
return p, nil
|
||||
end
|
||||
|
||||
-- setCell(module, pattern, row, channel, cell) -> true | nil, Err: writes one cell (missing fields = 0)
|
||||
function MusicOps.setCell(m: any, pattern: number, row: number, channel: number, cell: Cell): (boolean?, Err.Err?)
|
||||
local p, err = patternAt(m, pattern, row, channel)
|
||||
if not p then
|
||||
@@ -175,7 +157,6 @@ function MusicOps.setCell(m: any, pattern: number, row: number, channel: number,
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- getCell(module, pattern, row, channel) -> { note, instrument, volume, effect, param } (numbers) | nil, Err
|
||||
function MusicOps.getCell(m: any, pattern: number, row: number, channel: number): (Cell?, Err.Err?)
|
||||
local p, err = patternAt(m, pattern, row, channel)
|
||||
if not p then
|
||||
@@ -185,7 +166,6 @@ function MusicOps.getCell(m: any, pattern: number, row: number, channel: number)
|
||||
return { note = note, instrument = ins, volume = vol, effect = fx, param = param }, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ samples
|
||||
export type SampleSpec = {
|
||||
name: string?,
|
||||
audio: any?, -- an Audio (1 or 2 channels): the sample data and, unless rate is given, its rate
|
||||
@@ -203,9 +183,7 @@ export type SampleSpec = {
|
||||
|
||||
local LOOPS: { [string]: number } = { none = 0, forward = 1, pingpong = 2 }
|
||||
|
||||
-- addSample(module, spec) -> the new sample's number (1-based) | nil, Err. The rate sets the tuning: c5speed for
|
||||
-- S3M / IT, relativeNote + finetune (1/128 semitones from 8363 Hz) for MOD / XM (MOD files store only the finetune,
|
||||
-- -1..+7/8 semitone: use rates near 8363 Hz when saving a MOD).
|
||||
-- tuning: c5speed for S3M / IT; relativeNote + finetune (1/128 semitone from 8363 Hz) for MOD / XM (MOD stores only the finetune, -1..+7/8 semitone)
|
||||
function MusicOps.addSample(m: any, spec: SampleSpec): (number?, Err.Err?)
|
||||
if not isModule(m) then
|
||||
return bad("addSample: needs a tracker Module")
|
||||
@@ -283,7 +261,6 @@ function MusicOps.addSample(m: any, spec: SampleSpec): (number?, Err.Err?)
|
||||
return #m.samples, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ instruments
|
||||
export type InstrumentSpec = {
|
||||
name: string?,
|
||||
sample: number?, -- the sample every note plays (1-based)
|
||||
@@ -293,8 +270,7 @@ export type InstrumentSpec = {
|
||||
-- envelope: points { tick, 0..64 } with ticks increasing; sustain / loop points are 1-based point indexes
|
||||
}
|
||||
|
||||
-- addInstrument(module, spec) -> the new instrument's number (1-based) | nil, Err. XM and IT modules only (MOD /
|
||||
-- S3M cells name samples directly).
|
||||
-- instruments: XM and IT only (MOD / S3M cells name samples directly)
|
||||
function MusicOps.addInstrument(m: any, spec: InstrumentSpec): (number?, Err.Err?)
|
||||
if not isModule(m) then
|
||||
return bad("addInstrument: needs a tracker Module")
|
||||
|
||||
Reference in new issue
Block a user