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:
1 parent
25d8e4ff25
commit
10bd6b021f
7 files changed
+692
-16
No files matched your search
+2
-2
@@ -250,5 +250,5 @@ visualisers.
|
||||
number.
|
||||
- `F.save(decoder)` without `opts.format` or a destination fails with `Err.ARG`, because a Decoder has no source
|
||||
format.
|
||||
- `F.audio.load` does not take music files (MOD, MIDI). Use `F.music.open(src)` for their audio
|
||||
([music.md](music.md)).
|
||||
- Music files (MOD, S3M, XM, IT, MIDI) are audio too: `F.audio.open` / `F.audio.load` render them, with the
|
||||
render options in `opts` ([music.md](music.md)).
|
||||
+72
-10
@@ -142,8 +142,8 @@ end
|
||||
| `F.music.load(src, opts?)` / `F.load` | a Module, a Midi or a SoundFont |
|
||||
| `F.music.open(src, opts?)` / `F.open` | the rendered audio Decoder (decode and render options together) |
|
||||
|
||||
`F.audio.open` and `F.audio.load` do not take music files; use `F.music.open`, or `F.open`, which detects any
|
||||
category.
|
||||
Every music file that renders is also audio: `F.audio.open(src)` gives the same rendered Decoder as `F.music.open`,
|
||||
and `F.audio.load(src)` the whole render as an Audio. Decode and render options go in the same `opts`.
|
||||
|
||||
| Option | Formats | Meaning |
|
||||
|---|---|---|
|
||||
@@ -157,6 +157,8 @@ assert(dec.sampleRate == 22050 and dec.channels == 2)
|
||||
assert(math.abs(dec.duration - 3) < 0.01) -- 2 s of notes + the 1 s release tail
|
||||
local d2 = assert(F.open(modBytes)) -- any category: a Decoder for a module
|
||||
assert(d2.channels == 2)
|
||||
local d3 = assert(F.audio.open(midiBytes, { sampleRate = 22050 })) -- music is audio too
|
||||
assert(d3.sampleRate == 22050)
|
||||
```
|
||||
|
||||
## Rendering
|
||||
@@ -199,20 +201,20 @@ assert(first.frames == 22050)
|
||||
|
||||
A Midi is plain data: build one with `F.Types.Midi.new(division?)` and save it as `.mid`. Events need absolute
|
||||
ticks; the writer sorts nothing, so keep each track's events in tick order. A track's `name` is read from its
|
||||
track-name meta event (0x03); the writer writes events only, so put the name in an event.
|
||||
track-name meta event (0x03); the writer writes `track.name` as one at tick 0 when the track has no 0x03 event of
|
||||
its own.
|
||||
|
||||
```luau
|
||||
local m = F.Types.Midi.new(480) -- 480 ticks per quarter note, SMF type 1
|
||||
local events = {
|
||||
{ tick = 0, type = "meta", meta = 0x51, data = { 0x07, 0xA1, 0x20 } }, -- tempo: 500000 us per quarter
|
||||
{ tick = 0, type = "meta", meta = 0x03, data = { string.byte("Arpeggio", 1, -1) } }, -- the track name
|
||||
{ tick = 0, type = "program", channel = 0, data = { 0 } },
|
||||
}
|
||||
for i, note in { 60, 64, 67, 72 } do
|
||||
table.insert(events, { tick = (i - 1) * 240, type = "noteOn", channel = 0, data = { note, 100 } })
|
||||
table.insert(events, { tick = i * 240, type = "noteOff", channel = 0, data = { note, 0 } })
|
||||
end
|
||||
table.insert(m.tracks, { events = events })
|
||||
table.insert(m.tracks, { name = "Arpeggio", events = events }) -- written as a 0x03 event
|
||||
local bytes = assert(F.save(m, nil, { format = "midi" }))
|
||||
local back = assert(F.music.load(bytes))
|
||||
assert(back.tracks[1].name == "Arpeggio" and back.meta.length == 1) -- 4 eighth notes at 120 BPM
|
||||
@@ -232,6 +234,69 @@ local _, err = F.save(song, nil, { format = "it" })
|
||||
assert(err.code == F.Err.UNSUPPORTED) -- read only
|
||||
```
|
||||
|
||||
Saving a Module or a Midi to an **audio** format renders it first, with the render options (see
|
||||
[Rendering](#rendering)) taken from the same `opts`; `F.convert` does the same from music bytes:
|
||||
|
||||
```luau
|
||||
local song = assert(F.music.load(modBytes))
|
||||
local wav = assert(F.save(song, nil, { format = "wav", sampleRate = 22050 }))
|
||||
assert((assert(F.audio.load(wav))).sampleRate == 22050)
|
||||
local flac = assert(F.convert(midiBytes, "flac", { soundfont = sf2Bytes, tail = 0.5 }))
|
||||
assert(F.detect(flac) == "flac")
|
||||
```
|
||||
|
||||
### Building a module
|
||||
|
||||
`F.music` has a small builder for tracker modules, so you never touch the pattern buffers or the sample tuning
|
||||
fields yourself. Pattern numbers are 0-based (as the order list names them), rows 0-based, channels 1-based, and
|
||||
sample and instrument numbers 1-based (0 = none in a cell). Every call returns `nil, Err.ARG` for bad arguments.
|
||||
|
||||
| Call | Returns |
|
||||
|---|---|
|
||||
| `F.music.new({ format?, channels?, title?, speed?, tempo? })` | an empty Module: `format` `"mod"` (default), `"xm"`, `"s3m"` or `"it"`; 4 channels by default |
|
||||
| `F.music.addPattern(m, rows?, { order? }?)` | the new pattern's number; `rows` default 64 (MOD needs 64); appended to `m.order` unless `order = false` |
|
||||
| `F.music.setCell(m, pattern, row, channel, cell)` | `true`; `cell = { note, instrument, volume, effect, param }`, missing fields 0 |
|
||||
| `F.music.getCell(m, pattern, row, channel)` | the cell, with `note` as a number |
|
||||
| `F.music.addSample(m, spec)` | the new sample's number |
|
||||
| `F.music.addInstrument(m, spec)` | the new instrument's number (XM / IT only; MOD and S3M cells name samples) |
|
||||
| `F.music.note(name)` | the note number: `"C-4"` = 49, `"C#4"` / `"Db4"` = 50, `"off"` 255, `"cut"` 254, `"fade"` 253 |
|
||||
|
||||
`addSample` takes `audio` (a mono or stereo Audio) or `data` (f32 frames) with `length` and `channels`; `rate`
|
||||
(the Hz the sample plays at on C-4 for MOD / XM, C-5 for S3M / IT; default the Audio's rate, else 8363), `loop`
|
||||
(`"none"`, `"forward"`, `"pingpong"`), `loopStart` / `loopEnd` (frames), `volume` 0..64, `pan` 0..256 and `bits`
|
||||
(8 or 16, what a writer stores). MOD files keep only a finetune of -1..+7/8 semitone, so give MOD samples a rate
|
||||
near 8363 Hz. `addInstrument` takes `name`, `sample` (every note) or `samples` (a `{ [note] = sample }` keymap),
|
||||
`fadeout`, and a volume `envelope = { points = { { tick, 0..64 }, ... }, sustain?, loopStart?, loopEnd? }`.
|
||||
|
||||
```luau
|
||||
local tone = F.Types.Audio.new(8363, 1, 8363) -- 1 s of a 440 Hz sine at MOD's C-4 rate
|
||||
for i = 0, 8362 do
|
||||
buffer.writef32(tone.samples, i * 4, 0.5 * math.sin(i / 8363 * 2 * math.pi * 440))
|
||||
end
|
||||
local song = assert(F.music.new({ format = "mod", channels = 4, title = "beeps" }))
|
||||
local s = assert(F.music.addSample(song, { name = "sine", audio = tone, loop = "forward" }))
|
||||
local p = assert(F.music.addPattern(song)) -- pattern 0, 64 rows, in the order list
|
||||
for row = 0, 63, 8 do
|
||||
assert(F.music.setCell(song, p, row, 1, { note = if row % 16 == 0 then "C-4" else "G-4", instrument = s }))
|
||||
end
|
||||
assert(F.music.setCell(song, p, 63, 2, { effect = 0xC, param = 0 })) -- effect C00: volume 0
|
||||
assert(F.music.getCell(song, p, 8, 1).note == F.music.note("G-4"))
|
||||
local mod = assert(F.save(song)) -- a .mod (the module's format)
|
||||
local wav = assert(F.save(song, nil, { format = "wav" })) -- or rendered
|
||||
assert(F.detect(mod) == "mod" and F.detect(wav) == "wav")
|
||||
|
||||
local xm = assert(F.music.new({ format = "xm", channels = 2 }))
|
||||
local smp = assert(F.music.addSample(xm, { audio = tone }))
|
||||
local lead = assert(F.music.addInstrument(xm, {
|
||||
name = "lead", sample = smp, fadeout = 512,
|
||||
envelope = { points = { { 0, 64 }, { 10, 40 }, { 40, 0 } }, sustain = 2 },
|
||||
}))
|
||||
local p2 = assert(F.music.addPattern(xm, 32))
|
||||
assert(F.music.setCell(xm, p2, 0, 1, { note = "C-4", instrument = lead }))
|
||||
assert(F.music.setCell(xm, p2, 16, 1, { note = "off" }))
|
||||
assert(F.music.load(assert(F.save(xm))).instruments[1].name == "lead")
|
||||
```
|
||||
|
||||
| Format | Encoder options |
|
||||
|---|---|
|
||||
| midi | `runningStatus` (default true: the bytes python's mido writes) |
|
||||
@@ -252,11 +317,8 @@ See [FORMATS.md](../FORMATS.md) for each format's capabilities.
|
||||
|
||||
## Gotchas
|
||||
|
||||
- **Saving music as audio needs a render.** `F.save(song, nil, { format = "wav" })` and `F.convert(modBytes, "wav")`
|
||||
fail with `UNKNOWN_FORMAT` ("no format named wav for music"). Render first:
|
||||
`F.save(song:render(), nil, { format = "wav" })`.
|
||||
- `F.audio.open(midiBytes)` fails with `UNKNOWN_FORMAT`; use `F.music.open` or `F.open`.
|
||||
- `track.name` is not written: add a meta 0x03 event to name a track.
|
||||
- Saving music to an audio format renders it with the options in `opts`: `F.save(song, nil, { format = "wav",
|
||||
loop = true })` would render up to `maxDuration` (an hour by default), so leave `loop` off when saving.
|
||||
- A Midi you build yourself has no `render` method; `F.format("midi").render(midi, opts)` renders it.
|
||||
- MIDI event ticks are absolute, not the deltas stored in the file. Pitch bend `data` is `{ lsb, msb }`, not a
|
||||
signed value: `(msb * 128 + lsb) - 8192`.
|
||||
|
||||
Reference in new issue
Block a user