2026-12-01 17:38:10 +00:00
|
|
|
package sfz
|
|
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// Opcodes are the opcodes set at one point of an instrument, by name, with
|
|
|
|
|
// their values as written and any variables in them replaced.
|
2026-12-01 17:38:10 +00:00
|
|
|
type Opcodes map[string]string
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// with returns a copy of the opcodes of o, with those of more set over them.
|
2026-12-01 17:38:10 +00:00
|
|
|
func (o Opcodes) with(more Opcodes) Opcodes {
|
|
|
|
|
out := make(Opcodes, len(o)+len(more))
|
|
|
|
|
for k, v := range o {
|
|
|
|
|
out[k] = v
|
|
|
|
|
}
|
|
|
|
|
for k, v := range more {
|
|
|
|
|
out[k] = v
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FlatRegion is a <region> with the opcodes it inherits.
|
|
|
|
|
type FlatRegion struct {
|
2026-12-01 19:08:00 +00:00
|
|
|
// Opcodes are the region's own opcodes, those of the headers above it, and
|
|
|
|
|
// the <control> settings that apply to it. They are Inherited with Own set
|
|
|
|
|
// over them.
|
2026-12-01 17:38:10 +00:00
|
|
|
Opcodes Opcodes
|
2026-12-01 19:08:00 +00:00
|
|
|
// File is the path of the file that has the <region> header, as in
|
|
|
|
|
// [File.Path], and Line is the line the header is on.
|
2026-12-01 17:38:10 +00:00
|
|
|
File string
|
|
|
|
|
Line int
|
2026-12-01 19:08:00 +00:00
|
|
|
// Own are the opcodes that follow the region's header, and Inherited are
|
|
|
|
|
// those that the region takes from the headers above it.
|
2026-12-01 18:42:17 +00:00
|
|
|
Own, Inherited Opcodes
|
|
|
|
|
// Header is the <region> header, or nil if a variable made it, so that it
|
|
|
|
|
// has no node of its own.
|
|
|
|
|
Header *Header
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Flat is an instrument with its directives expanded and its headers resolved.
|
2026-12-01 19:08:00 +00:00
|
|
|
// It has no comments and no layout.
|
2026-12-01 17:38:10 +00:00
|
|
|
type Flat struct {
|
2026-12-01 19:08:00 +00:00
|
|
|
// Control holds the opcodes of the <control> headers, the later setting of
|
|
|
|
|
// an opcode replacing the earlier.
|
2026-12-01 17:38:10 +00:00
|
|
|
Control Opcodes
|
2026-12-01 19:08:00 +00:00
|
|
|
// Curves holds the opcodes of each <curve> header, in order.
|
2026-12-01 17:38:10 +00:00
|
|
|
Curves []Opcodes
|
2026-12-01 19:08:00 +00:00
|
|
|
// Regions are the <region> headers, with what they inherit, in order.
|
2026-12-01 17:38:10 +00:00
|
|
|
Regions []FlatRegion
|
2026-12-01 19:08:00 +00:00
|
|
|
// Headers counts, by lowercase name, the headers that aren't <control>,
|
|
|
|
|
// <curve>, <global>, <master>, <group> or <region>.
|
2026-12-01 17:38:10 +00:00
|
|
|
Headers map[string]int
|
2026-12-02 01:19:40 +00:00
|
|
|
|
|
|
|
|
// doc is the document that was flattened.
|
|
|
|
|
doc *Document
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// level is how far down the header hierarchy a header is, where the opcodes of
|
|
|
|
|
// each level are set over those of the levels before it.
|
2026-12-01 17:38:10 +00:00
|
|
|
type level int
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// The levels of the headers that regions inherit from.
|
2026-12-01 17:38:10 +00:00
|
|
|
const (
|
|
|
|
|
levelGlobal level = iota
|
|
|
|
|
levelMaster
|
|
|
|
|
levelGroup
|
|
|
|
|
levelRegion
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// headerLevels are the headers whose opcodes regions inherit.
|
|
|
|
|
var headerLevels = map[string]level{
|
|
|
|
|
"global": levelGlobal,
|
|
|
|
|
"master": levelMaster,
|
|
|
|
|
"group": levelGroup,
|
|
|
|
|
"region": levelRegion,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// controlSettings are the <control> opcodes that apply to the regions after
|
2026-12-01 19:08:00 +00:00
|
|
|
// them, as opposed to those that name controllers or set their values.
|
2026-12-01 17:38:10 +00:00
|
|
|
var controlSettings = []string{"default_path", "note_offset", "octave_offset"}
|
|
|
|
|
|
|
|
|
|
// target is where the opcodes after a header go.
|
|
|
|
|
type target struct {
|
2026-12-01 19:08:00 +00:00
|
|
|
// ops are the opcodes set so far.
|
2026-12-01 17:38:10 +00:00
|
|
|
ops Opcodes
|
2026-12-01 18:27:17 +00:00
|
|
|
// region is whether the header is a <region>, which becomes a region when
|
2026-12-01 19:08:00 +00:00
|
|
|
// the next header starts or the text ends.
|
2026-12-01 18:27:17 +00:00
|
|
|
region bool
|
2026-12-01 19:08:00 +00:00
|
|
|
// line, file and header are where the <region> header is, and its node, if a
|
|
|
|
|
// variable didn't make it.
|
2026-12-01 18:27:17 +00:00
|
|
|
line int
|
2026-12-01 17:38:10 +00:00
|
|
|
file string
|
2026-12-01 18:42:17 +00:00
|
|
|
header *Header
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// flattener is the [visitor] that builds a [Flat] from a document's nodes.
|
2026-12-01 17:38:10 +00:00
|
|
|
type flattener struct {
|
2026-12-01 18:20:35 +00:00
|
|
|
flat *Flat
|
2026-12-01 19:08:00 +00:00
|
|
|
// levels are the opcodes set at each level by the headers read so far.
|
2026-12-01 17:38:10 +00:00
|
|
|
levels [levelRegion + 1]Opcodes
|
2026-12-01 19:08:00 +00:00
|
|
|
// into is where the opcodes that come next go, or nil before any header.
|
|
|
|
|
into *target
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// Flatten returns the instrument with each #include read where it stands, each
|
|
|
|
|
// variable replaced by its value as of where it is used, and each region given
|
|
|
|
|
// the opcodes of the <global>, <master> and <group> above it. A header clears
|
|
|
|
|
// the opcodes of the levels below it, so a <group> starts with no group
|
|
|
|
|
// opcodes of its own. Comments are dropped, and an include that wasn't read is
|
|
|
|
|
// skipped.
|
2026-12-01 17:38:10 +00:00
|
|
|
func (d *Document) Flatten() *Flat {
|
2026-12-02 01:19:40 +00:00
|
|
|
f := &flattener{flat: &Flat{Control: Opcodes{}, Headers: map[string]int{}, doc: d}}
|
2026-12-01 17:38:10 +00:00
|
|
|
for i := range f.levels {
|
|
|
|
|
f.levels[i] = Opcodes{}
|
|
|
|
|
}
|
2026-12-01 18:20:35 +00:00
|
|
|
walk(d.Root, defines{}, f)
|
2026-12-01 17:38:10 +00:00
|
|
|
f.closeRegion()
|
|
|
|
|
return f.flat
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// define does nothing, as [walk] sets variables itself.
|
|
|
|
|
func (f *flattener) define(*File, *Node) {}
|
|
|
|
|
|
|
|
|
|
// include does nothing, as [walk] reads included files itself.
|
|
|
|
|
func (f *flattener) include(*File, *Node) {}
|
|
|
|
|
|
|
|
|
|
// variables does nothing, as [walk] replaces variables itself.
|
|
|
|
|
func (f *flattener) variables(*File, *Node) {}
|
|
|
|
|
|
|
|
|
|
// text does nothing, as a flattened instrument leaves out text that isn't SFZ.
|
2026-12-01 18:20:35 +00:00
|
|
|
func (f *flattener) text(*File, *Node, string) {}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// header starts the header, which is on the line of n even if a variable made
|
|
|
|
|
// it.
|
2026-12-01 18:20:35 +00:00
|
|
|
func (f *flattener) header(file *File, n *Node, name string, variable bool) {
|
2026-12-01 19:08:00 +00:00
|
|
|
// A header that a variable made has no node of its own to edit.
|
2026-12-01 18:27:17 +00:00
|
|
|
line, _ := file.Position(n)
|
2026-12-01 18:42:17 +00:00
|
|
|
var own *Header
|
|
|
|
|
if !variable {
|
|
|
|
|
own = &Header{File: file, Node: n}
|
|
|
|
|
}
|
|
|
|
|
f.startHeader(name, line, file, own)
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// opcode sets the opcode in the header being read.
|
2026-12-01 18:20:35 +00:00
|
|
|
func (f *flattener) opcode(_ *File, _ *Node, name, value string, _ bool) { f.setOpcode(name, value) }
|
2026-12-01 17:38:10 +00:00
|
|
|
|
|
|
|
|
// closeRegion adds the region being read, if any. A region takes the <control>
|
2026-12-01 19:08:00 +00:00
|
|
|
// settings made before it, and later <control> headers don't change them.
|
2026-12-01 17:38:10 +00:00
|
|
|
func (f *flattener) closeRegion() {
|
2026-12-01 18:27:17 +00:00
|
|
|
if f.into == nil || !f.into.region {
|
2026-12-01 17:38:10 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
control := Opcodes{}
|
|
|
|
|
for _, name := range controlSettings {
|
|
|
|
|
if v, ok := f.flat.Control[name]; ok {
|
|
|
|
|
control[name] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-12-01 18:42:17 +00:00
|
|
|
inherited := control.with(f.levels[levelGlobal]).with(f.levels[levelMaster]).with(f.levels[levelGroup])
|
|
|
|
|
f.flat.Regions = append(f.flat.Regions, FlatRegion{
|
|
|
|
|
Opcodes: inherited.with(f.into.ops), File: f.into.file, Line: f.into.line,
|
|
|
|
|
Own: f.into.ops, Inherited: inherited, Header: f.into.header,
|
|
|
|
|
})
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// startHeader ends the header being read and starts the one called header,
|
|
|
|
|
// which is on a line of file, and is own if it has a node of its own.
|
2026-12-01 18:42:17 +00:00
|
|
|
func (f *flattener) startHeader(header string, line int, file *File, own *Header) {
|
2026-12-01 17:38:10 +00:00
|
|
|
f.closeRegion()
|
2026-12-01 18:27:17 +00:00
|
|
|
name := strings.ToLower(header)
|
2026-12-01 17:38:10 +00:00
|
|
|
switch name {
|
|
|
|
|
case "control":
|
|
|
|
|
// A new <control> resets default_path, as ARIA does, but keeps the
|
|
|
|
|
// other control settings.
|
|
|
|
|
delete(f.flat.Control, "default_path")
|
|
|
|
|
f.into = &target{ops: f.flat.Control}
|
|
|
|
|
case "curve":
|
|
|
|
|
c := Opcodes{}
|
|
|
|
|
f.flat.Curves = append(f.flat.Curves, c)
|
|
|
|
|
f.into = &target{ops: c}
|
|
|
|
|
default:
|
|
|
|
|
lvl, ok := headerLevels[name]
|
|
|
|
|
if !ok {
|
|
|
|
|
f.flat.Headers[name]++
|
|
|
|
|
f.into = &target{ops: Opcodes{}}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for l := lvl; l <= levelRegion; l++ {
|
|
|
|
|
f.levels[l] = Opcodes{}
|
|
|
|
|
}
|
2026-12-01 18:42:17 +00:00
|
|
|
f.into = &target{ops: f.levels[lvl], file: file.Path}
|
2026-12-01 17:38:10 +00:00
|
|
|
if lvl == levelRegion {
|
2026-12-01 18:42:17 +00:00
|
|
|
f.into.region, f.into.line, f.into.header = true, line, own
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// setOpcode sets an opcode in the header being read.
|
2026-12-01 18:20:35 +00:00
|
|
|
func (f *flattener) setOpcode(name, value string) {
|
2026-12-01 20:38:57 +00:00
|
|
|
// Opcodes before any header belong to no header, so no region has them, as in
|
|
|
|
|
// sfizz.
|
2026-12-01 17:38:10 +00:00
|
|
|
if f.into == nil {
|
2026-12-01 20:38:57 +00:00
|
|
|
return
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
f.into.ops[name] = value
|
|
|
|
|
}
|