Files
sfz/flatten.go
T

165 lines
4.7 KiB
Go
Raw Normal View History

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
}
// 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
}
// 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)
f.startHeader(name, line, file.Path)
}
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
}
}
ops := control.with(f.levels[levelGlobal]).with(f.levels[levelMaster]).with(f.levels[levelGroup]).with(f.into.ops)
f.flat.Regions = append(f.flat.Regions, FlatRegion{Opcodes: ops, File: f.into.file, Line: f.into.line})
}
func (f *flattener) startHeader(header string, line int, file string) {
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}
if lvl == levelRegion {
f.into.region, f.into.line = true, line
}
}
}
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
}