Files
sfz/flatten.go
T

204 lines
6.5 KiB
Go
Raw Normal View History

package sfz
import "strings"
// Opcodes are the opcodes set at one point of an instrument, by name, with
// their values as written and any variables in them replaced.
type Opcodes map[string]string
// with returns a copy of the opcodes of o, with those of more set over them.
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 opcodes, those of the headers above it, and
// the <control> settings that apply to it. They are Inherited with Own set
// over them.
Opcodes Opcodes
// 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.
File string
Line int
// Own are the opcodes that follow the region's header, and Inherited are
// those that the region takes from the headers above it.
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.
// It has no comments and no layout.
type Flat struct {
// Control holds the opcodes of the <control> headers, the later setting of
// an opcode replacing the earlier.
Control Opcodes
// Curves holds the opcodes of each <curve> header, in order.
Curves []Opcodes
// Regions are the <region> headers, with what they inherit, in order.
Regions []FlatRegion
// Headers counts, by lowercase name, the headers that aren't <control>,
// <curve>, <global>, <master>, <group> or <region>.
Headers map[string]int
}
// 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.
type level int
// The levels of the headers that regions inherit from.
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, as opposed to those that name controllers or set their values.
var controlSettings = []string{"default_path", "note_offset", "octave_offset"}
// target is where the opcodes after a header go.
type target struct {
// ops are the opcodes set so far.
ops Opcodes
// region is whether the header is a <region>, which becomes a region when
// the next header starts or the text ends.
region bool
// line, file and header are where the <region> header is, and its node, if a
// variable didn't make it.
line int
file string
header *Header
}
// flattener is the [visitor] that builds a [Flat] from a document's nodes.
type flattener struct {
flat *Flat
// levels are the opcodes set at each level by the headers read so far.
levels [levelRegion + 1]Opcodes
// into is where the opcodes that come next go, or nil before any header.
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 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.
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
}
// 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.
func (f *flattener) text(*File, *Node, string) {}
// header starts the header, which is on the line of n even if a variable made
// it.
func (f *flattener) header(file *File, n *Node, name string, variable bool) {
// A header that a variable made has no node of its own to edit.
line, _ := file.Position(n)
var own *Header
if !variable {
own = &Header{File: file, Node: n}
}
f.startHeader(name, line, file, own)
}
// opcode sets the opcode in the header being read.
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, and later <control> headers don't change them.
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,
})
}
// 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.
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
}
}
}
// setOpcode sets an opcode in the header being read.
func (f *flattener) setOpcode(name, value string) {
// Opcodes before any header belong to no header, so no region has them, as in
// sfizz.
if f.into == nil {
return
}
f.into.ops[name] = value
}