Files
sfz/registry.go
T

230 lines
6.6 KiB
Go
Raw Normal View History

package sfz
//go:generate go run -C internal/gen . -in syntax.yml -out ../../registry_data.go
import (
"iter"
"regexp"
"strings"
"sync"
)
// ValueType is the kind of value an opcode takes.
type ValueType int
// The value types.
const (
// TypeUnspecified is for an opcode whose value the format doesn't describe.
TypeUnspecified ValueType = iota
// TypeString is for a value that is text, which may have a fixed set of
// options.
TypeString
// TypeInteger is for a whole number, which may also be a note name.
TypeInteger
// TypeFloat is for a number that may have a fraction.
TypeFloat
)
// String returns the type's name.
func (t ValueType) String() string {
switch t {
case TypeString:
return "string"
case TypeInteger:
return "integer"
case TypeFloat:
return "float"
}
return "unspecified"
}
// HeaderInfo describes a header of the format.
type HeaderInfo struct {
// Name is the header's name without its brackets.
Name string
// Version is the first SFZ version or player that has it: "SFZ v1", "SFZ v2",
// "ARIA", and so on.
Version string
// Deprecated is whether the format has dropped the header.
Deprecated bool
// Description is what the format says about the header.
Description string
}
// OpcodeInfo describes an opcode of the format. A name that stands for a
// family has uppercase placeholders: N, X and Y stand for a number, as in
// loccN and egN_timeX_onccY, and * for any name, as in hint_*.
type OpcodeInfo struct {
// Name is the opcode's own name.
Name string
// Aliases are other names for it.
Aliases []string
// Version is the first SFZ version or player that has it: "SFZ v1", "SFZ
// v2", "ARIA", "sfizz", "Calfbox" or "LinuxSampler".
Version string
// Deprecated is whether the format has dropped the opcode.
Deprecated bool
// Category is where the format lists it, such as "Performance Parameters /
// Amplifier".
Category string
// Type is the kind of value the opcode takes.
Type ValueType
// Min and Max bound a numeric value, or are nil where the format gives no
// bound it can state as a number.
Min, Max *float64
// Default is the format's description of the default, which is for
// reading and not always a value.
Default string
// Unit is the value's unit, such as "seconds" or "cents".
Unit string
// Options are the values a string opcode may take, if it has a fixed set.
Options []string
// Note is whether an integer opcode also takes a note name, such as c4.
Note bool
// Description is what the format says about the opcode.
Description string
}
// matcher finds the registry entry for the names of an opcode family.
type matcher struct {
// re matches the names that the family's name stands for.
re *regexp.Regexp
info *OpcodeInfo
}
var (
// registryOnce makes the lookup tables below on the first lookup.
registryOnce sync.Once
// byName holds the opcodes that aren't families, by each of their names.
byName map[string]*OpcodeInfo
// patterns holds the opcode families, with those whose names have a * last
// so that a more exact family is found first.
patterns []matcher
// headerByName holds the headers, by lowercase name.
headerByName map[string]*HeaderInfo
// ownNames holds the own name of each opcode that has other names, by each
// other name. Families such as loccN are left out, and so is pitch, which
// the typed model adds to tune and doesn't take as another name for it.
ownNames map[string]string
)
// patternRE reads a name's placeholders: a run of N, X and Y letters, or a *.
var patternRE = regexp.MustCompile(`[NXY]+|\*`)
// pattern returns the expression that matches the names a family name stands
// for, or nil if the name is a single opcode's.
func pattern(name string) *regexp.Regexp {
locs := patternRE.FindAllStringIndex(name, -1)
if locs == nil {
return nil
}
var b strings.Builder
b.WriteString("^")
last := 0
for _, loc := range locs {
b.WriteString(regexp.QuoteMeta(name[last:loc[0]]))
if name[loc[0]] == '*' {
b.WriteString("[a-z0-9_]+")
} else {
b.WriteString("[0-9]+")
}
last = loc[1]
}
b.WriteString(regexp.QuoteMeta(name[last:]))
b.WriteString("$")
return regexp.MustCompile(b.String())
}
// loadRegistry makes the lookup tables from the generated data, once.
func loadRegistry() {
registryOnce.Do(func() {
byName = map[string]*OpcodeInfo{}
ownNames = map[string]string{}
headerByName = map[string]*HeaderInfo{}
for i := range headerData {
headerByName[headerData[i].Name] = &headerData[i]
}
var wild []matcher
for i := range opcodeData {
info := &opcodeData[i]
for _, name := range append([]string{info.Name}, info.Aliases...) {
re := pattern(name)
switch {
case re == nil:
byName[name] = info
if name != info.Name && pattern(info.Name) == nil && name != "pitch" {
ownNames[name] = info.Name
}
case strings.Contains(name, "*"):
wild = append(wild, matcher{re, info})
default:
patterns = append(patterns, matcher{re, info})
}
}
}
patterns = append(patterns, wild...)
})
}
// Lookup returns what the format says about an opcode, and whether it knows
// the opcode. The opcode is found by its name or one of its aliases, and by the
// name of a family it is in, such as locc64 in loccN. The info is the same for
// each name of the opcode, and its Name is the opcode's own.
func Lookup(name string) (OpcodeInfo, bool) {
loadRegistry()
if info, ok := byName[name]; ok {
return *info, true
}
for _, m := range patterns {
if m.re.MatchString(name) {
return *m.info, true
}
}
return OpcodeInfo{}, false
}
// ownName returns the name that the format gives an opcode, for any other name
// it is known by. It returns name for an opcode that has no other name.
func ownName(name string) string {
loadRegistry()
if own, ok := ownNames[name]; ok {
return own
}
return name
}
// KnownOpcodes returns every opcode of the format, in the order the format
// lists them.
func KnownOpcodes() iter.Seq[OpcodeInfo] {
return func(yield func(OpcodeInfo) bool) {
for _, info := range opcodeData {
if !yield(info) {
return
}
}
}
}
// KnownHeaders returns every header of the format, in the order the format
// lists them.
func KnownHeaders() iter.Seq[HeaderInfo] {
return func(yield func(HeaderInfo) bool) {
for _, h := range headerData {
if !yield(h) {
return
}
}
}
}
// LookupHeader returns what the format says about a header, and whether it
// knows the header. The name is given without brackets, in any case.
func LookupHeader(name string) (HeaderInfo, bool) {
loadRegistry()
h, ok := headerByName[strings.ToLower(name)]
if !ok {
return HeaderInfo{}, false
}
return *h, true
}