- Header.Set, Document.SetDefine and Document.AddHeader return an error for a value or name that can't be written as one opcode, define or header, and Build returns the same error for what it can't write. - Set puts a new opcode after the last one's trailing comment, and never where text on its line would become part of its value. Delete keeps the line breaks it doesn't own. - Node.Line and Node.Col are replaced by File.Position, which works lines and columns out when asked, and a node remembers where it was in its file, so an edit no longer renumbers the file or searches it from the start. - Add FuzzEdit checks that an edited file reads back as its nodes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.
Editing and writing
Document.Headersreturns the headers of every file.Header.Get,SetandDeletechange an opcode and keep the comments and spacing around it; a new opcode goes after the header's last and is laid out like it.Document.AddHeaderadds a header.Header.Seton an opcode that uses a variable writes the value in its place.Document.SetDefinechanges the variable, and so each opcode that uses it.Document.WriteandWriteFilestakeWriteOptions:InlineIncludesmakes one file,ExpandVariablesreplaces variables with their values, andLineEndingsets the line break. The zero value writes each file as it was read.Buildmakes a document from anInstrument, withBuildOptions:SplitFilesputs regions in included files,Variableshoists repeated values into#defines and the samples' folder intodefault_path, andIndentsets the indent. Reading the result gives the instrument back, except for opcodes the typed model doesn't hold.
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, validation, editing, writing and building are in.
Licence
MIT.