Add lossless SFZ document, flattening, typed model and opcode registry
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>
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
package sfz
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var update = flag.Bool("update", false, "rewrite docs/opcodes.md")
|
||||
|
||||
func TestLookup(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, canonical string
|
||||
}{
|
||||
{"lokey", "lokey"},
|
||||
{"loopmode", "loop_mode"},
|
||||
{"loop_mode", "loop_mode"},
|
||||
{"locc64", "loccN"},
|
||||
{"amp_velcurve_127", "amp_velcurve_N"},
|
||||
{"eg02_time3", "egN_timeX"},
|
||||
{"ampeg_attack_oncc12", "ampeg_attackccN"},
|
||||
{"v064", "vNNN"},
|
||||
{"hint_anything_here", "hint_*"},
|
||||
{"cutoff2_cc5", "cutoff2_ccN"},
|
||||
} {
|
||||
info, ok := Lookup(tc.name)
|
||||
if !ok || info.Name != tc.canonical {
|
||||
t.Errorf("Lookup(%q) = %q, %v; want %q", tc.name, info.Name, ok, tc.canonical)
|
||||
}
|
||||
}
|
||||
for _, name := range []string{"", "zampler_gain", "lokey2", "loccN7x", "lokey "} {
|
||||
if info, ok := Lookup(name); ok {
|
||||
t.Errorf("Lookup(%q) = %q", name, info.Name)
|
||||
}
|
||||
}
|
||||
info, _ := Lookup("lokey")
|
||||
if info.Type != TypeInteger || !info.Note || *info.Min != 0 || *info.Max != 127 {
|
||||
t.Errorf("lokey %+v", info)
|
||||
}
|
||||
if h, ok := LookupHeader("REGION"); !ok || h.Name != "region" {
|
||||
t.Errorf("header %+v", h)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegistryComplete checks the registry holds what the spec lists, each
|
||||
// name once, and that every name reads back as the opcode it belongs to.
|
||||
func TestRegistryComplete(t *testing.T) {
|
||||
count := 0
|
||||
for info := range KnownOpcodes() {
|
||||
count++
|
||||
for _, name := range append([]string{info.Name}, info.Aliases...) {
|
||||
if strings.ContainsAny(name, "*") || placeholders.MatchString(name) {
|
||||
continue
|
||||
}
|
||||
got, ok := Lookup(name)
|
||||
if !ok || got.Name != info.Name {
|
||||
t.Errorf("%s is %q", name, got.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
if count < 700 {
|
||||
t.Errorf("%d opcodes", count)
|
||||
}
|
||||
for _, h := range []string{"region", "group", "master", "global", "control", "curve", "effect", "midi", "sample"} {
|
||||
if _, ok := LookupHeader(h); !ok {
|
||||
t.Errorf("no header %s", h)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAs(t *testing.T) {
|
||||
ops := Opcodes{"lokey": "c4", "hikey": " 72 ", "loopmode": "one_shot", "volume": "-3.5", "offset": "12", "bad": "x"}
|
||||
if n, ok := GetAs[int](ops, "lokey"); !ok || n != 60 {
|
||||
t.Errorf("lokey %d %v", n, ok)
|
||||
}
|
||||
if n, ok := GetAs[int64](ops, "hikey"); !ok || n != 72 {
|
||||
t.Errorf("hikey %d %v", n, ok)
|
||||
}
|
||||
if s, ok := GetAs[string](ops, "loop_mode"); !ok || s != "one_shot" {
|
||||
t.Errorf("loop_mode %q %v", s, ok)
|
||||
}
|
||||
if f, ok := GetAs[float64](ops, "volume"); !ok || f != -3.5 {
|
||||
t.Errorf("volume %v %v", f, ok)
|
||||
}
|
||||
if n, ok := GetAs[int](ops, "offset"); !ok || n != 12 {
|
||||
t.Errorf("offset %d %v", n, ok)
|
||||
}
|
||||
if _, ok := GetAs[int](ops, "bad"); ok {
|
||||
t.Error("bad is an int")
|
||||
}
|
||||
if _, ok := GetAs[int](ops, "missing"); ok {
|
||||
t.Error("missing is set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateValues(t *testing.T) {
|
||||
text := "<region> sample=a.wav lokey=c4 hikey=60 lovel=100 hivel=20 volume=loud pan=300 loop_mode=sideways\n" +
|
||||
"<region> zampler_gain=3 locc7=1 pitch_keycenter=sample key=$K <wrong>\n" +
|
||||
"<region> lokey=10 hikey=9 amp_veltrack=1.5 ampeg_attack=$A\n"
|
||||
doc, err := Parse(strings.NewReader(text))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got []string
|
||||
for _, p := range doc.Validate() {
|
||||
got = append(got, fmt.Sprintf("%d:%s", p.Line, p.Code))
|
||||
}
|
||||
want := []string{
|
||||
"1:value.type", // volume=loud
|
||||
"1:value.range", // pan=300
|
||||
"1:value.option", // loop_mode=sideways
|
||||
"1:range.inverted", // lovel above hivel
|
||||
"2:opcode.unknown", // zampler_gain
|
||||
"2:define.undefined",
|
||||
"2:header.unknown", // <wrong>
|
||||
"3:range.inverted", // lokey above hikey
|
||||
"3:define.undefined", // $A
|
||||
}
|
||||
for _, w := range want {
|
||||
found := false
|
||||
for _, g := range got {
|
||||
if g == w {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("no %s in %v", w, got)
|
||||
}
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Errorf("problems %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOpcodeDoc checks docs/opcodes.md is the table the registry makes. Run
|
||||
// the tests with -update to rewrite it.
|
||||
func TestOpcodeDoc(t *testing.T) {
|
||||
var b strings.Builder
|
||||
b.WriteString("# Opcodes\n\n" +
|
||||
"Every opcode the library knows, from [sfzformat.com](https://sfzformat.com/)'s data. " +
|
||||
"**Typed** opcodes are read into the [`Instrument`](../instrument.go) model; the rest are known, so the " +
|
||||
"library validates them, and reads and writes them as text. This file is generated by `go test -update`.\n\n" +
|
||||
"| Opcode | Version | Value | Typed |\n|---|---|---|---|\n")
|
||||
for info := range KnownOpcodes() {
|
||||
value := info.Type.String()
|
||||
if info.Unit != "" {
|
||||
value += " (" + info.Unit + ")"
|
||||
}
|
||||
names := "`" + info.Name + "`"
|
||||
for _, a := range info.Aliases {
|
||||
names += ", `" + a + "`"
|
||||
}
|
||||
typed := ""
|
||||
if isTyped(info) {
|
||||
typed = "yes"
|
||||
}
|
||||
deprecated := ""
|
||||
if info.Deprecated {
|
||||
deprecated = ", deprecated"
|
||||
}
|
||||
fmt.Fprintf(&b, "| %s | %s%s | %s | %s |\n", names, info.Version, deprecated, value, typed)
|
||||
}
|
||||
if *update {
|
||||
if err := os.WriteFile("docs/opcodes.md", []byte(b.String()), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
got, err := os.ReadFile("docs/opcodes.md")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(got) != b.String() {
|
||||
t.Error("docs/opcodes.md is out of date; run go test -update")
|
||||
}
|
||||
}
|
||||
|
||||
// placeholders finds the placeholders in a family's name.
|
||||
var placeholders = patternRE
|
||||
|
||||
// isTyped reports whether the typed model reads any name of an opcode.
|
||||
func isTyped(info OpcodeInfo) bool {
|
||||
for _, name := range append([]string{info.Name}, info.Aliases...) {
|
||||
if strings.Contains(name, "*") {
|
||||
continue
|
||||
}
|
||||
if handled(placeholders.ReplaceAllString(name, "1")) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// TestTypedOpcodesKnown checks that the registry knows every opcode the typed
|
||||
// model reads by name.
|
||||
func TestTypedOpcodesKnown(t *testing.T) {
|
||||
for name := range handledNames {
|
||||
if _, ok := Lookup(name); !ok {
|
||||
t.Errorf("%s isn't in the registry", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user