ws-audio2: music/far (Farandole Composer): s3m family with Hz periods (36318 / 1024 Hz per step), auto tone portamento, variable-length patterns with break rows
This commit is contained in:
1 parent
bac37fb157
commit
6b5e8ec6cf
6 files changed
+378
No files matched your search
@@ -842,6 +842,18 @@ local list: { Entry } = {
|
||||
return require("@formats/music/669")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "far",
|
||||
category = "music",
|
||||
path = "music/far",
|
||||
extensions = { "far" },
|
||||
mime = { "audio/x-far" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Tracker", "codecs/audio/Tracker/Load" },
|
||||
caps = { decode = true, encode = false, render = true, channels = { 16, 16 } },
|
||||
load = function()
|
||||
return require("@formats/music/far")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "it",
|
||||
category = "music",
|
||||
|
||||
@@ -0,0 +1,232 @@
|
||||
--!strict
|
||||
-- music/far: Farandole Composer modules (FAR, Daniel Potter's DOS tracker; 16 channels, up to 256 patterns of any
|
||||
-- length with a break row, 64 samples). Ported from OpenMPT's Load_far.cpp (BSD, licenses/openmpt-LICENSE.txt). Reads
|
||||
-- into the codecs/audio/Tracker Module and plays as the "s3m" effect family with Farandole's quirks (flags): periods
|
||||
-- in Hz (a slide step moves the frequency 36318 / 1024 Hz), a tone portamento that keeps going, fine pitch slides,
|
||||
-- retrigger, vibrato depth / speed, volume slides, volume portamento (as a set volume), panning, speed; the fixed
|
||||
-- tempo OpenMPT uses (80 BPM in tracker terms).
|
||||
-- Not supported: Farandole's own tempo model (fine tempo up / down, D / E), the note-offset effect (C), writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Tracker = require("@formats/codecs/audio/Tracker")
|
||||
local Load = require("@formats/codecs/audio/Tracker/Load")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Far = {
|
||||
name = "far",
|
||||
category = "music",
|
||||
extensions = { "far" },
|
||||
mime = { "audio/x-far" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Tracker", "codecs/audio/Tracker/Load" },
|
||||
caps = { decode = true, encode = false, render = true, channels = { 16, 16 } },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse more sample data in total (f32 bytes, default 2^28)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local IT = Load.IT
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Far.sniff(r: Bytes.Reader): number
|
||||
if r:size() >= 98 and r:peek(4) == "FAR\xFE" and r:peek(3, 44) == "\r\n\x1A" and r:peekU8(47) + r:peekU8(48) * 256 >= 98 then
|
||||
return 1
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- a pattern cell's effect byte -> effect code, parameter, volume byte (S3M coding, 0 = none) (OpenMPT's table)
|
||||
local function effect(e: number): (number, number, number)
|
||||
local cmd, p = e // 16, e % 16
|
||||
if cmd == 1 then
|
||||
return IT.portaup, 0xF0 + p, 0
|
||||
elseif cmd == 2 then
|
||||
return IT.portadown, 0xF0 + p, 0
|
||||
elseif cmd == 3 then
|
||||
return IT.toneporta, if p ~= 0 then 60 // p else 0, 0
|
||||
elseif cmd == 4 then
|
||||
return IT.retrig, 6 // (1 + p) + 1, 0
|
||||
elseif cmd == 5 or cmd == 9 then
|
||||
return IT.vibrato, p, 0
|
||||
elseif cmd == 6 then
|
||||
return IT.vibrato, p * 8, 0
|
||||
elseif cmd == 7 then
|
||||
return IT.volslide, p * 8, 0
|
||||
elseif cmd == 8 then
|
||||
return IT.volslide, p, 0
|
||||
elseif cmd == 0xA then
|
||||
return 0, 0, p * 4 + 4 + 1 -- volume portamento: a set volume
|
||||
elseif cmd == 0xB then
|
||||
return IT.s3mex, 0x80 + p, 0
|
||||
elseif cmd == 0xF then
|
||||
return IT.speed, p, 0
|
||||
end
|
||||
return 0, 0, 0
|
||||
end
|
||||
|
||||
function Far.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local budget = { left = opts and opts.maxBytes or 2 ^ 28 }
|
||||
if Far.sniff(r) == 0 then
|
||||
return nil, Err.new(if r:size() < 98 then Err.TRUNCATED else Err.CORRUPT, "not a Farandole module")
|
||||
end
|
||||
r:skip(4)
|
||||
local title = r:fixed(40)
|
||||
r:skip(3)
|
||||
local headerLength, version = r:u16le(), r:u8()
|
||||
local onOff, pans = {}, {}
|
||||
for i = 1, 16 do
|
||||
onOff[i] = r:u8()
|
||||
end
|
||||
r:skip(9)
|
||||
local speed = r:u8()
|
||||
for i = 1, 16 do
|
||||
pans[i] = r:u8()
|
||||
end
|
||||
r:skip(4)
|
||||
local msgLen = r:u16le()
|
||||
local message = r:string(msgLen)
|
||||
local orders = {}
|
||||
for i = 1, 256 do
|
||||
orders[i] = r:u8()
|
||||
end
|
||||
local _npat, nord, restart = r:u8(), r:u8(), r:u8()
|
||||
local sizes = {}
|
||||
for i = 1, 256 do
|
||||
sizes[i] = r:u16le()
|
||||
end
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
local m = Tracker.newModule("far", 16, "s3m")
|
||||
m.title = title
|
||||
m.meta.message = message
|
||||
m.speed, m.tempo = if speed > 0 then speed else 4, 80
|
||||
m.mixVolume = 32 -- OpenMPT's pre-amp for FAR
|
||||
m.linear = false
|
||||
m.instrumentMode = false
|
||||
m.flags.periods, m.flags.hzSlide = "hz", 36318 / 1024
|
||||
m.flags.autoTonePorta = true
|
||||
for i = 1, 16 do
|
||||
m.channelMute[i] = onOff[i] == 0
|
||||
m.channelPan[i] = (pans[i] % 16) * 16 + 8
|
||||
end
|
||||
for i = 1, nord do
|
||||
m.order[i] = orders[i]
|
||||
end
|
||||
while #m.order > 0 and m.order[#m.order] == 255 do
|
||||
table.remove(m.order)
|
||||
end
|
||||
m.restart = restart
|
||||
-- patterns: (size - 2) / 64 rows of 16 channels x (note, instrument, volume, effect), after a break row byte
|
||||
if not r:seek(headerLength) then
|
||||
return nil, r.err
|
||||
end
|
||||
local last = -1
|
||||
for p = 0, 255 do
|
||||
if sizes[p + 1] > 0 then
|
||||
last = p
|
||||
end
|
||||
end
|
||||
for p = 0, last do
|
||||
local size = sizes[p + 1]
|
||||
local rows = math.clamp((size - 2) // 64, 1, 1024)
|
||||
local pat = Tracker.newPattern(if size > 0 then rows else 64, 16)
|
||||
m.patterns[p + 1] = pat
|
||||
if size > 0 then
|
||||
local pr = r:slice(size)
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
local br = pr:u8()
|
||||
pr:skip(1)
|
||||
local d = pat.data
|
||||
for row = 0, rows - 1 do
|
||||
for ch = 0, 15 do
|
||||
local note, ins, vol, e = pr:u8(), pr:u8(), pr:u8(), pr:u8()
|
||||
if pr.err then
|
||||
break
|
||||
end
|
||||
local q = (row * 16 + ch) * 5
|
||||
if note > 0 and note <= 72 then
|
||||
buffer.writeu8(d, q, note + 36)
|
||||
buffer.writeu8(d, q + 1, ins + 1)
|
||||
end
|
||||
if vol > 0 and vol <= 16 then
|
||||
buffer.writeu8(d, q + 2, (vol - 1) * 64 // 15 + 1)
|
||||
end
|
||||
local fx, param, setVol = effect(e)
|
||||
if setVol > 0 then
|
||||
buffer.writeu8(d, q + 2, setVol)
|
||||
end
|
||||
buffer.writeu8(d, q + 3, fx)
|
||||
buffer.writeu8(d, q + 4, param)
|
||||
end
|
||||
end
|
||||
if br > 0 and br < rows - 2 then
|
||||
Load.writeEffect(m, p, br + 1, IT["break"], 0, "next")
|
||||
end
|
||||
end
|
||||
end
|
||||
-- samples: a 64-bit map of the stored ones, each a 48-byte header + signed 8 / 16-bit data
|
||||
local map = {}
|
||||
for i = 1, 8 do
|
||||
map[i] = r:u8()
|
||||
end
|
||||
r.err = nil
|
||||
for i = 0, 63 do
|
||||
if bit32.band(map[i // 8 + 1] or 0, bit32.lshift(1, i % 8)) ~= 0 then
|
||||
local h = r:slice(48)
|
||||
if r.err then
|
||||
break
|
||||
end
|
||||
local s = Tracker.newSample(h:fixed(32))
|
||||
local len = h:u32le()
|
||||
h:skip(1)
|
||||
local vol = h:u8()
|
||||
local ls, le = h:u32le(), h:u32le()
|
||||
local typ, loop = h:u8(), h:u8()
|
||||
local bits = if typ % 2 == 1 then 16 else 8
|
||||
local frames = if bits == 16 then len // 2 else len
|
||||
if bits == 16 then
|
||||
ls, le = ls // 2, le // 2
|
||||
end
|
||||
s.c5speed = 8363 * 2
|
||||
s.volume = math.min(vol * 4, 64)
|
||||
local ok, err = Load.readSample(r.buf, r.pos, frames, { bits = bits, signed = true, limit = r.limit }, s, budget)
|
||||
if not ok then
|
||||
return nil, err
|
||||
end
|
||||
r:skip(math.min(len, r:remaining()))
|
||||
if bit32.band(loop, 8) ~= 0 and le > ls then
|
||||
s.loopStart, s.loopEnd, s.loopType = ls, math.min(le, s.length), 1
|
||||
end
|
||||
for k = #m.samples + 1, i do
|
||||
m.samples[k] = Tracker.newSample()
|
||||
end
|
||||
m.samples[i + 1] = s
|
||||
end
|
||||
end
|
||||
m.meta.source = { format = "far", version = version }
|
||||
local ok, verr = Tracker.validate(m)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.CORRUPT, verr and verr.message or "invalid module")
|
||||
end
|
||||
return m, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ open
|
||||
-- open(r, opts) -> the module rendered as an audio Decoder (opts: Tracker.RenderOptions + DecodeOptions)
|
||||
function Far.open(r: Bytes.Reader, opts: any?): (Types.Decoder?, Err.Err?)
|
||||
local m, err = Far.decode(r, opts)
|
||||
if not m then
|
||||
return nil, err
|
||||
end
|
||||
return Tracker.render(m, opts)
|
||||
end
|
||||
|
||||
return Far
|
||||
Reference in new issue
Block a user