-- ops/MusicOps tests (F.music.new / addPattern / setCell / getCell / addSample / addInstrument / note) and the -- music-as-audio bridges of the api: F.save / F.convert of a Module or a Midi to an audio format (rendered, render -- options through opts), F.audio.open / F.audio.load of music bytes. Everything is built here (no fixtures). local T = require("@tests/T") local F = require("@rosetta") local Err = require("@rosetta/core/Err") local t = T.suite("musicops") -- a 1 s 440 Hz sine at 8363 Hz (MOD's C-4 rate) local function sine(rate: number, frames: number): any local a = F.Types.Audio.new(rate, 1, frames) for i = 0, frames - 1 do buffer.writef32(a.samples, i * 4, math.sin(i / rate * 2 * math.pi * 440) * 0.5) end return a end -- note names do t:eq(F.music.note("C-0"), 1, "C-0") t:eq(F.music.note("C-4"), 49, "C-4") t:eq(F.music.note("c#4"), 50, "C#4") t:eq(F.music.note("Db4"), 50, "Db4") t:eq(F.music.note("B-9"), 120, "B-9") t:eq(F.music.note("off"), 255, "key off") t:eq(F.music.note("cut"), 254, "note cut") t:eq(F.music.note(61), 61, "a number") for _, badNote in { "H-4", "C-", 121, "Cb0", {}, -1 } do local n, e = F.music.note(badNote) t:check(n == nil and e and e.code == Err.ARG, "bad note " .. tostring(badNote)) end end -- a MOD built from scratch: saved, reloaded, rendered local a = sine(8363, 8363) local song = assert(F.music.new({ format = "mod", channels = 4, title = "demo", speed = 3 })) do t:eq(song.kind, "music", "kind") t:eq(song.meta.source.format, "mod", "source format") local s = assert(F.music.addSample(song, { name = "sine", audio = a, loop = "forward", volume = 48 })) t:eq(s, 1, "sample number") local p = assert(F.music.addPattern(song)) t:eq(p, 0, "pattern number") t:eq(#song.order, 1, "appended to the order") t:check(F.music.setCell(song, p, 0, 1, { note = "C-4", instrument = s }), "setCell") t:check(F.music.setCell(song, p, 32, 2, { note = "E-4", instrument = s, effect = 0xC, param = 32 }), "setCell 2") local c = assert(F.music.getCell(song, p, 32, 2)) t:check(c.note == 53 and c.instrument == 1 and c.effect == 0xC and c.param == 32 and c.volume == 0, "getCell") -- bad arguments local _, e1 = F.music.setCell(song, p, 64, 1, {}) t:check(e1 and e1.code == Err.ARG, "row out of range") local _, e2 = F.music.setCell(song, p, 0, 5, {}) t:check(e2 and e2.code == Err.ARG, "channel out of range") local _, e3 = F.music.setCell(song, 7, 0, 1, {}) t:check(e3 and e3.code == Err.ARG, "no such pattern") local _, e4 = F.music.setCell(song, p, 0, 1, { param = 300 }) t:check(e4 and e4.code == Err.ARG, "param out of range") local _, e5 = F.music.addInstrument(song, { sample = 1 }) t:check(e5 and e5.code == Err.ARG, "MOD has no instruments") local _, e6 = F.music.new({ format = "mid" }) t:check(e6 and e6.code == Err.ARG, "unknown tracker format") local _, e7 = F.music.addSample(song, { audio = F.Types.Audio.new(8000, 3, 4) }) t:check(e7 and e7.code == Err.ARG, "3-channel sample") local _, e8 = F.music.addPattern({} :: any) t:check(e8 and e8.code == Err.ARG, "not a module") local mod = assert(F.save(song)) -- the source format t:eq(F.detect(mod), "mod", "saved as MOD") local back = assert(F.music.load(mod)) t:eq(back.title, "demo", "title round trip") t:check(back.samples[1].loopType == 1 and back.samples[1].volume == 48, "sample loop / volume round trip") local c2 = assert(F.music.getCell(back, 0, 32, 2)) t:check(c2.note == 53 and c2.instrument == 1 and c2.effect == 0xC and c2.param == 32, "cells round trip") local dec = assert(song:render({ sampleRate = 22050 })) local au = assert(F.ops.audio.toAudio(dec)) t:check(au.frames > 22050 and F.ops.audio.peak(au) > 0.05, "renders sound") end -- an XM with an instrument and a volume envelope do local xm = assert(F.music.new({ format = "xm", channels = 2 })) local s = assert(F.music.addSample(xm, { audio = a, rate = 16726 })) -- an octave up: relativeNote 12 t:eq(xm.samples[s].relativeNote, 12, "rate -> relativeNote") t:eq(xm.samples[s].finetune, 0, "rate -> finetune") local i = assert(F.music.addInstrument(xm, { name = "lead", sample = s, fadeout = 512, envelope = { points = { { 0, 64 }, { 10, 32 }, { 40, 0 } }, sustain = 2 } })) local p = assert(F.music.addPattern(xm, 32)) assert(F.music.setCell(xm, p, 0, 1, { note = "C-4", instrument = i })) assert(F.music.setCell(xm, p, 16, 1, { note = "off" })) local back = assert(F.music.load(assert(F.save(xm)))) t:check(back.format == "xm" and #back.instruments == 1 and back.instruments[1].name == "lead", "XM instrument round trip") t:check(back.instruments[1].volEnv and back.instruments[1].volEnv.enabled and #back.instruments[1].volEnv.ticks == 3, "XM envelope round trip") local _, ebad = F.music.addInstrument(xm, { sample = 9 }) t:check(ebad and ebad.code == Err.ARG, "instrument on a missing sample") local _, eenv = F.music.addInstrument(xm, { envelope = { points = { { 5, 10 }, { 5, 20 } } } }) t:check(eenv and eenv.code == Err.ARG, "envelope ticks must increase") end -- music saved and converted as audio: rendered, with the render options from opts do local wav = assert(F.save(song, nil, { format = "wav", sampleRate = 22050 })) local au = assert(F.audio.load(wav)) t:check(au.sampleRate == 22050 and au.channels == 2 and au.frames > 0, "F.save(module, wav) renders") local mod = assert(F.save(song)) local w2 = assert(F.convert(mod, "wav", { sampleRate = 11025 })) t:eq(F.detect(w2), "wav", "F.convert(mod, wav)") t:eq((assert(F.audio.load(w2))).sampleRate, 11025, "convert passes sampleRate to the render") t:check(F.audio.save(song, nil, { format = "flac" }) ~= nil, "F.audio.save(module)") local m = F.Types.Midi.new(480) table.insert(m.tracks, { events = { { tick = 0, type = "noteOn", channel = 0, data = { 60, 100 } }, { tick = 480, type = "noteOff", channel = 0, data = { 60, 0 } }, } }) local mw = assert(F.save(m, nil, { format = "wav", tail = 0.5 })) -- a hand-built Midi (no render method) local ma = assert(F.audio.load(mw)) t:eq(ma.frames, math.floor(1.0 * 44100 + 0.5), "Midi rendered: 0.5 s of notes + 0.5 s tail") local mid = assert(F.save(m, nil, { format = "midi" })) t:eq(F.detect(assert(F.convert(mid, "wav"))), "wav", "F.convert(midi, wav)") -- F.audio.open / load of music bytes: the rendered Decoder local d1 = assert(F.audio.open(mid, { tail = 0 })) t:check(d1.channels == 2 and d1.sampleRate == 44100 and d1.frames == 22050, "F.audio.open(midi)") local d2 = assert(F.audio.open(mod)) t:check(d2.channels == 2 and d2.frames > 0, "F.audio.open(mod)") local a2 = assert(F.audio.load(mod, { sampleRate = 8000 })) t:check(a2.kind == "audio" and a2.sampleRate == 8000, "F.audio.load(mod)") local d3 = assert(F.audio.open(mod, { format = "mod" })) t:check(d3.frames > 0, "F.audio.open with format = mod") -- still an error for data no format reads local _, e = F.audio.open(buffer.fromstring("not audio at all")) t:check(e and e.code == Err.UNKNOWN_FORMAT, "unknown data") -- and a soundfont is not audio local _, e2 = F.audio.save(m, nil, { format = "png" }) t:check(e2 ~= nil, "music to a non-audio format fails") end -- no throws on random cell / sample arguments do local rng = T.rng(7) local ok = true for _ = 1, 300 do local vals = { rng(300) - 20, "C-4", nil, {}, -1, 0.5, 1e9 } local pick = function(): any return vals[rng(#vals) + 1] end local pok = pcall(function() F.music.setCell(song, pick(), pick(), pick(), { note = pick(), instrument = pick(), param = pick() }) F.music.getCell(song, pick(), pick(), pick()) F.music.addSample(song, { data = buffer.create(rng(16)), length = pick(), loop = "forward", loopStart = pick(), loopEnd = pick() }) end) ok = ok and pok end t:check(ok, "random arguments never throw") end t:done()