Parse keeps a file exactly as written, with #define and #include unexpanded and each include as its own file in a tree. Flatten resolves directives and header inheritance, and Flat.Instrument reads regions into the typed model ported from Zampler's internal/sfz. The opcode registry is generated from the SFZ format's syntax data and drives Lookup, GetAs and Validate. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
192 lines
5.1 KiB
Go
192 lines
5.1 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
|
|
}
|
|
|
|
// 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 the header of a <region>, which becomes a region when the next
|
|
// header starts or the text ends.
|
|
region *Node
|
|
file string
|
|
}
|
|
|
|
// flattener reads a document's nodes in order.
|
|
type flattener struct {
|
|
flat *Flat
|
|
defines map[string]string
|
|
// 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{}},
|
|
defines: map[string]string{},
|
|
}
|
|
for i := range f.levels {
|
|
f.levels[i] = Opcodes{}
|
|
}
|
|
f.file(d.Root)
|
|
f.closeRegion()
|
|
return f.flat
|
|
}
|
|
|
|
func (f *flattener) file(file *File) {
|
|
for _, n := range file.Nodes {
|
|
switch n.Kind {
|
|
case KindDefine:
|
|
f.defines[n.Name] = strings.TrimSpace(substitute(f.defines, n.Value))
|
|
case KindInclude:
|
|
if n.File != nil {
|
|
f.file(n.File)
|
|
}
|
|
case KindHeader:
|
|
f.header(n, file.Path)
|
|
case KindOpcode:
|
|
if !strings.Contains(n.Name+n.Value, "$") {
|
|
f.opcode(n.Name, n.Value)
|
|
continue
|
|
}
|
|
f.expand(n, file.Path, n.Name+"="+n.Value)
|
|
case KindText:
|
|
if strings.Contains(n.Raw, "$") {
|
|
f.expand(n, file.Path, n.Raw)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// expand reads again the text that a node makes once its variables are
|
|
// replaced, as a variable's value can hold opcodes or headers.
|
|
func (f *flattener) expand(n *Node, file, text string) {
|
|
for _, m := range lex(substitute(f.defines, text)) {
|
|
switch m.Kind {
|
|
case KindHeader:
|
|
f.header(&Node{Name: m.Name, Line: n.Line}, file)
|
|
case KindOpcode:
|
|
f.opcode(m.Name, m.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 == nil {
|
|
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.region.Line})
|
|
}
|
|
|
|
func (f *flattener) header(n *Node, file string) {
|
|
f.closeRegion()
|
|
name := strings.ToLower(n.Name)
|
|
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 = n
|
|
}
|
|
}
|
|
}
|
|
|
|
func (f *flattener) opcode(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
|
|
}
|