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>
2.2 KiB
sfz
A Go library that reads, validates and writes SFZ instruments. It handles the format as data, covering SFZ v1, v2 and the ARIA extensions. It doesn't play instruments or load samples.
doc, err := sfz.ParseFile(os.DirFS("Piano"), "Piano.sfz")
if err != nil {
return err
}
for _, p := range doc.Validate() {
log.Printf("%s:%d: %s", p.File, p.Line, p.Msg)
}
inst := doc.Flatten().Instrument()
Layers
- Document. A file exactly as written: headers, opcodes in order, comments, unknown opcodes, and
#defineand#includeunexpanded. Each#includethat was read holds its file, so a document is a tree of files. Writing a document gives the same text back. - Flat.
Document.Flattenreads each#includewhere it stands, replaces variables with their values as of where they are used, and gives each region the opcodes of the<global>,<master>and<group>above it. - Instrument.
Flat.Instrumentreads the regions into a typed model, with settings taken at the controllers' starting values. Opcodes the model has no field for are counted inUnread.
Opcodes
The library knows every opcode and header in the format, from the data behind sfzformat.com: docs/opcodes.md lists them. Lookup returns an opcode's type, range, unit, options and version, finding numbered families such as loccN and aliases such as loopmode. Validate checks values against it. GetAs reads any opcode's value as an int, float or string. An opcode the registry doesn't know is kept, read and written as text like any other.
The registry is generated: replace internal/gen/syntax.yml and run go generate ./..., then go test -update.
Includes
Parse reads includes with the resolver from WithResolver; ParseFile reads them from the folder of the main file. An include that can't be read is a Problem from Validate, and WithIncludes(sfz.IncludesFail) makes it an error. Includes nest at most 16 deep, and an instrument may hold 16 MB of text; WithLimits changes both.
Status
The document, flattening, typed model, opcode registry and validation are in. Not yet in: editing a document, and building one from the typed model.
Licence
MIT.