193 lines
4.8 KiB
Go
193 lines
4.8 KiB
Go
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
|
||
|
|
TypeInteger
|
||
|
|
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 bool
|
||
|
|
Description string
|
||
|
|
}
|
||
|
|
|
||
|
|
// OpcodeInfo describes an opcode of the format. A name that stands for a
|
||
|
|
// family has uppercase placeholders: N and X stand for a number, as in
|
||
|
|
// loccN and egN_timeX, 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 bool
|
||
|
|
// Category is where the format lists it, such as "Performance Parameters /
|
||
|
|
// Amplifier".
|
||
|
|
Category string
|
||
|
|
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 string
|
||
|
|
}
|
||
|
|
|
||
|
|
// matcher finds the registry entry for an opcode name.
|
||
|
|
type matcher struct {
|
||
|
|
re *regexp.Regexp
|
||
|
|
info *OpcodeInfo
|
||
|
|
}
|
||
|
|
|
||
|
|
var (
|
||
|
|
registryOnce sync.Once
|
||
|
|
byName map[string]*OpcodeInfo
|
||
|
|
patterns []matcher
|
||
|
|
headerByName map[string]*HeaderInfo
|
||
|
|
)
|
||
|
|
|
||
|
|
// patternRE reads a name's placeholders: a run of N and X letters, or a *.
|
||
|
|
var patternRE = regexp.MustCompile(`[NX]+|\*`)
|
||
|
|
|
||
|
|
// 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())
|
||
|
|
}
|
||
|
|
|
||
|
|
func loadRegistry() {
|
||
|
|
registryOnce.Do(func() {
|
||
|
|
byName = map[string]*OpcodeInfo{}
|
||
|
|
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
|
||
|
|
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, found by its name or
|
||
|
|
// one of its aliases. 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
|
||
|
|
}
|
||
|
|
|
||
|
|
// KnownOpcodes returns every opcode of the format, in the order of its listing.
|
||
|
|
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.
|
||
|
|
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, given its name
|
||
|
|
// 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
|
||
|
|
}
|