Add region lookup and structural edits, and reject regions Build can't write

Instrument.Source returns the flattened region and header behind a typed
region. AddRegion, RemoveRegion and MoveRegion edit the document and read
the instrument again, restoring the document if a region would read
differently. NewRegion returns a region with the format's defaults, and
Build refuses a region that can't be read back as it is.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 01:19:40 +00:00
co-authored by Claude Sonnet 5.5
parent 535856ba77
commit 49fd867283
8 changed files with 571 additions and 5 deletions
+6 -2
View File
@@ -29,7 +29,7 @@ The package documentation has runnable examples of reading, editing and building
## 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, and `Validate` reports it only as information.
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, and `Validate` reports it only as information. That includes opcodes that a host adds for its own data, such as `zampler_zone`: samplers ignore them, and the library keeps them through every edit. `Instrument.Source(i)` returns the flattened region behind typed region `i`, with its `Header`, so that a host can `Get` and `Set` them.
## The typed model
@@ -45,13 +45,17 @@ The model is read with each setting that a controller moves taken at the control
- `Instrument.Apply(i, region)` writes a change to a typed region back to the document that the instrument was read from, as changes to the region's own opcodes. It touches only the settings that changed, leaves unknown opcodes, comments and the other regions as they were, and puts every change in the region's own header, over what the region inherits. `Instrument.Patches` returns the changes without making them.
- It returns `ErrNoSource` if the region has no header to edit: a variable made the header, or an include or a variable brings in opcodes after it.
- It returns `ErrUnpatchable`, and changes nothing, for a change the header can't hold: removing an opcode the region inherits, or a sample outside `default_path`.
- `Instrument.AddRegion(region)`, `RemoveRegion(i)` and `MoveRegion(from, to)` change the document's structure and read the instrument again, so it is as the document reads. A removed or moved region takes the comments and spacing that follow its header. A new region goes at the end of the main file. Each changes nothing, and returns an error, if the result wouldn't read as intended.
- `ErrNoSource` is for a region that has no header to move or remove, or an instrument that wasn't read from a document.
- `ErrUnpatchable` is for a region that would read differently where it ends up: a new region that a `default_path` changes, or a region moved under another `<group>`.
- `Node` positions are worked out when asked for, with `File.Position`, so edits stay cheap.
## Writing and building
- `Document.Write` writes one file, and `WriteFiles` writes the main file and each file it includes, through a `FileWriter`. They take `WriteOptions`: `InlineIncludes` puts included files into the main file, `ExpandVariables` replaces variables with their values, and `LineEnding` sets the line break. The zero value writes each file as it was read.
- `Write` and `WriteTo` return an error for a document with includes unless `InlineIncludes` is set, as the files they hold would be lost. `WriteFiles` also returns an error for an include whose path is absolute or leaves the folder (`AllowParentPaths` opts in to `..` paths, not absolute ones), and for a file included more than once that would be written differently each time.
- `Build` makes a new 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 the opcodes and headers the typed model doesn't hold. It returns an error for a value that can't be written, such as a sample path with a line break in it.
- `Build` makes a new 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 the opcodes and headers the typed model doesn't hold. It returns an error wrapping `ErrInvalid` for a value that can't be written, and for a region that wouldn't read back as it is, such as a zero `Region` with an empty `Trigger`. `NewRegion(sample)` makes a region with the format's defaults.
- A `Region.Sample` is written as one opcode, so `Build` and `Apply` refuse one with a line break, `//`, `/*`, `<`, or a space followed by `=` (as in `a b=c.wav`). A backslash reads back as `/`. A host that has such file names needs to map them to safe ones.
## Includes