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:
2026-12-01 17:38:10 +00:00
co-authored by Claude Sonnet 5.5
commit d124690ecf
29 changed files with 11677 additions and 0 deletions
+122
View File
@@ -0,0 +1,122 @@
package sfz
import (
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
)
// roundTripTexts are texts that are awkward to read: comments in odd places,
// CRLF and tabs, directives in the middle of lines, unknown headers and text
// that isn't SFZ.
var roundTripTexts = map[string]string{
"empty": "",
"spaces": " \n\t\n",
"comments": "// top\r\n/* a\r\nblock */<region>\tsample=a b.wav // end\r\nlokey=c#4 /* x */ hikey=60\r\n/* open",
"crlf": "<group>\r\nvolume=1\r\n<region> sample=a.wav\r\n",
"include": "#define $D Data\n<group> #include \"$D/a.txt\" lovel=1 #include \"b.txt\"\n#include broken\n#define $X\n",
"unknown": "<effect> type=reverb\n<weird_header>foo bar=1 baz\n<>\n<region",
"junk": "garbage ünïcode <region> sample=ü.wav\n\x00\xff=1 =2 x==3\n",
"no final": "<region> sample=a.wav",
"variable": "#define $A 1\n$A $B=2 label_cc$A=x\n",
}
func TestRoundTrip(t *testing.T) {
for name, text := range roundTripTexts {
doc, err := Parse(strings.NewReader(text))
if err != nil {
t.Errorf("%s: %v", name, err)
continue
}
if got := doc.String(); got != text {
t.Errorf("%s: wrote %q, want %q", name, got, text)
}
var b strings.Builder
if n, err := doc.WriteTo(&b); err != nil || n != int64(len(text)) || b.String() != text {
t.Errorf("%s: WriteTo %d %v %q", name, n, err, b.String())
}
}
}
// TestRoundTripFiles checks that every file of the fixtures, and every file
// they include, is written back as it was read.
func TestRoundTripFiles(t *testing.T) {
dir := filepath.Join("testdata", "splendid")
doc, err := ParseFile(os.DirFS(dir), "Splendid Grand Piano.sfz")
if err != nil {
t.Fatal(err)
}
files := doc.Files()
if len(files) != 1+1+5 {
t.Errorf("%d files", len(files))
}
for _, f := range files {
want, err := os.ReadFile(filepath.Join(dir, f.Path))
if err != nil {
t.Fatal(err)
}
if f.String() != string(want) {
t.Errorf("%s isn't written back as it was read", f.Path)
}
}
// The fixture's amplitude_oncc70=200 is past the 100 the format gives as
// the most, and is the only problem.
problems := doc.Validate()
if len(problems) != 1 || problems[0].Code != "value.range" || problems[0].Line != 82 {
t.Errorf("problems %+v", problems)
}
}
// TestIncludedTwice checks that a file included twice is two files of the
// document, each with the variables it was read with.
func TestIncludedTwice(t *testing.T) {
m := map[string]string{
"main.sfz": "#define $S a\n#include \"x.txt\"\n#define $S b\n#include \"x.txt\"\n",
"x.txt": "<region> sample=$S.wav\n",
}
doc := parseFiles(t, m)
if len(doc.Files()) != 3 {
t.Errorf("%d files", len(doc.Files()))
}
regions := doc.Flatten().Regions
if len(regions) != 2 || regions[0].Opcodes["sample"] != "a.wav" || regions[1].Opcodes["sample"] != "b.wav" {
t.Errorf("regions %+v", regions)
}
}
func TestValidate(t *testing.T) {
doc, err := Parse(strings.NewReader("lokey=1\n<region> sample=$GONE.wav\n#define $A 1\n<region> %%% sample=$A.wav\n"))
if err != nil {
t.Fatal(err)
}
var codes []string
for _, p := range doc.Validate() {
codes = append(codes, p.Code)
}
want := "opcode.no-header define.undefined text.unreadable"
if got := strings.Join(codes, " "); got != want {
t.Errorf("got %q, want %q", got, want)
}
}
// FuzzRoundTrip checks that any text is written back as it was read, and that
// flattening and validating it don't panic.
func FuzzRoundTrip(f *testing.F) {
for _, text := range roundTripTexts {
f.Add(text)
}
f.Fuzz(func(t *testing.T, text string) {
resolver := func(string) ([]byte, error) { return nil, fs.ErrNotExist }
doc, err := Parse(strings.NewReader(text), WithResolver(resolver))
if err != nil {
t.Skip()
}
if got := doc.String(); got != text {
t.Fatalf("wrote %q, want %q", got, text)
}
doc.Flatten().Instrument()
doc.Validate()
})
}