Files
sfz/README.md
T

46 lines
3.2 KiB
Markdown
Raw Normal View History

# sfz
A Go library that reads, validates and writes [SFZ](https://sfzformat.com/) instruments. It handles the format as data, covering SFZ v1, v2 and the ARIA extensions. It doesn't play instruments or load samples.
```go
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 `#define` and `#include` unexpanded. Each `#include` that was read holds its file, so a document is a tree of files. Writing a document gives the same text back.
- **Flat.** `Document.Flatten` reads each `#include` where 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.Instrument` reads the regions into a typed model, with settings taken at the controllers' starting values. Opcodes the model has no field for are counted in `Unread`.
## Opcodes
The library knows every opcode and header in the format, from the data behind [sfzformat.com](https://sfzformat.com/): [docs/opcodes.md](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`.
2026-12-01 17:41:34 +00:00
## Editing and writing
- `Document.Headers` returns the headers of every file. `Header.Get`, `Set` and `Delete` change 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.AddHeader` adds a header.
- `Header.Set` on an opcode that uses a variable writes the value in its place. `Document.SetDefine` changes the variable, and so each opcode that uses it.
- `Document.Write` and `WriteFiles` take `WriteOptions`: `InlineIncludes` makes one file, `ExpandVariables` replaces variables with their values, and `LineEnding` sets the line break. The zero value writes each file as it was read.
- `Build` makes a document from an `Instrument`, with `BuildOptions`: `SplitFiles` puts regions in included files, `Variables` hoists repeated values into `#define`s and the samples' folder into `default_path`, and `Indent` sets 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
2026-12-01 17:41:34 +00:00
The document, flattening, typed model, opcode registry, validation, editing, writing and building are in.
## Licence
MIT.