Instrument.Patches works out the opcode changes that make region i read as an updated Region, checking them against the reader, and Instrument.Apply makes them in the document. Only the changed settings are touched, always in the region's own header, so unknown opcodes and what other regions inherit stay as they are. A change the header can't hold is an error and changes nothing. FlatRegion gains Own, Inherited and Header to trace a region to its source, and regionLines lists a region's opcodes with the field each sets. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
179 lines
5.2 KiB
Go
179 lines
5.2 KiB
Go
package sfz
|
|
|
|
import "strings"
|
|
|
|
// Opcodes are the opcodes set at one point of an instrument, by name.
|
|
type Opcodes map[string]string
|
|
|
|
// with returns the opcodes of o with those of more on top.
|
|
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 {
|
|
// Opcodes are the region's own and those of the headers above it, and the
|
|
// <control> settings that apply to it.
|
|
Opcodes Opcodes
|
|
// File and Line are where the <region> header is.
|
|
File string
|
|
Line int
|
|
// Own holds the opcodes of the region's own header, and Inherited those that
|
|
// it inherits, which Opcodes is the two of together.
|
|
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
|
|
}
|
|
|
|
// Flat is an instrument with its directives expanded and its headers resolved.
|
|
type Flat struct {
|
|
// Control holds the opcodes of the <control> headers.
|
|
Control Opcodes
|
|
// Curves holds the opcodes of each <curve> header.
|
|
Curves []Opcodes
|
|
// Regions are the regions, in order.
|
|
Regions []FlatRegion
|
|
// Headers counts the headers that aren't <control>, <curve>, <global>,
|
|
// <master>, <group> or <region>, by name.
|
|
Headers map[string]int
|
|
}
|
|
|
|
// level is how far down the header hierarchy a header is.
|
|
type level int
|
|
|
|
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
|
|
// them.
|
|
var controlSettings = []string{"default_path", "note_offset", "octave_offset"}
|
|
|
|
// target is where the opcodes after a header go.
|
|
type target struct {
|
|
ops Opcodes
|
|
// region is whether the header is a <region>, which becomes a region when
|
|
// the next header starts or the text ends, and line is where it is.
|
|
region bool
|
|
line int
|
|
file string
|
|
header *Header
|
|
}
|
|
|
|
// flattener reads a document's nodes in order.
|
|
type flattener struct {
|
|
flat *Flat
|
|
// levels are the opcodes set at each level.
|
|
levels [levelRegion + 1]Opcodes
|
|
into *target
|
|
}
|
|
|
|
// 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
|
|
// new header clears the ones below it, so a <group> starts with no group
|
|
// opcodes. Comments are dropped. An include that wasn't read is skipped.
|
|
func (d *Document) Flatten() *Flat {
|
|
f := &flattener{flat: &Flat{Control: Opcodes{}, Headers: map[string]int{}}}
|
|
for i := range f.levels {
|
|
f.levels[i] = Opcodes{}
|
|
}
|
|
walk(d.Root, defines{}, f)
|
|
f.closeRegion()
|
|
return f.flat
|
|
}
|
|
|
|
func (f *flattener) define(*File, *Node) {}
|
|
func (f *flattener) include(*File, *Node) {}
|
|
func (f *flattener) variables(*File, *Node) {}
|
|
func (f *flattener) text(*File, *Node, string) {}
|
|
|
|
func (f *flattener) header(file *File, n *Node, name string, variable bool) {
|
|
// A header that a variable made has no node of its own, so it takes the line
|
|
// of the node that made it.
|
|
line, _ := file.Position(n)
|
|
var own *Header
|
|
if !variable {
|
|
own = &Header{File: file, Node: n}
|
|
}
|
|
f.startHeader(name, line, file, own)
|
|
}
|
|
|
|
func (f *flattener) opcode(_ *File, _ *Node, name, value string, _ bool) { f.setOpcode(name, value) }
|
|
|
|
// closeRegion adds the region being read, if any. A region takes the <control>
|
|
// settings made before it, which later <control> headers don't change.
|
|
func (f *flattener) closeRegion() {
|
|
if f.into == nil || !f.into.region {
|
|
return
|
|
}
|
|
control := Opcodes{}
|
|
for _, name := range controlSettings {
|
|
if v, ok := f.flat.Control[name]; ok {
|
|
control[name] = v
|
|
}
|
|
}
|
|
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,
|
|
})
|
|
}
|
|
|
|
func (f *flattener) startHeader(header string, line int, file *File, own *Header) {
|
|
f.closeRegion()
|
|
name := strings.ToLower(header)
|
|
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{}
|
|
}
|
|
f.into = &target{ops: f.levels[lvl], file: file.Path}
|
|
if lvl == levelRegion {
|
|
f.into.region, f.into.line, f.into.header = true, line, own
|
|
}
|
|
}
|
|
}
|
|
|
|
func (f *flattener) setOpcode(name, value string) {
|
|
if f.into == nil {
|
|
// Opcodes before any header apply to every region.
|
|
f.into = &target{ops: f.levels[levelGlobal]}
|
|
}
|
|
f.into.ops[name] = value
|
|
}
|