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
+38 -1
View File
@@ -1,6 +1,7 @@
package sfz
import (
"errors"
"fmt"
"math"
"path"
@@ -60,6 +61,38 @@ func defaultRegion() Region {
}
}
// NewRegion returns a region of the sample that has the format's defaults for
// everything else: it plays from key 0 to 127 at any velocity, on attack, as
// the only note of its round robin. A [Region] made any other way has to set
// each of these itself, as [Build] and [Instrument.AddRegion] refuse a region
// that can't be read back as it is, such as one with an empty Trigger.
func NewRegion(sample string) Region {
r := defaultRegion()
r.Sample = sample
return r
}
// checkRegion returns an error saying why a region can't be written as it is:
// the opcodes for it would be empty, or would be read as something else, or
// would never match a note.
func checkRegion(r Region) error {
switch {
case r.Sample == "":
return errors.New("the sample is empty")
case r.Trigger == "":
return errors.New("the trigger is empty")
case r.OffMode != "fast" && r.OffMode != "time" && r.OffMode != "normal":
return fmt.Errorf("the off mode %q isn't fast, time or normal", r.OffMode)
case r.SequenceLength < 1 || r.SequencePosition < 1:
return errors.New("the sequence length and position start at 1")
case r.HighKey < r.LowKey:
return errors.New("the highest key is below the lowest")
case r.HighVelocity < r.LowVelocity:
return errors.New("the highest velocity is below the lowest")
}
return nil
}
// opcodeLine is an opcode to write, with its value as text.
type opcodeLine struct{ name, value string }
@@ -77,7 +110,8 @@ func scaled(v, scale float64) string { return num(math.Round(v*scale*1e9) / 1e9)
// for what the model doesn't hold: the opcodes it doesn't read, and which
// headers the regions were under. It returns an error wrapping [ErrInvalid] if
// something in the instrument, such as a sample path or a label, can't be
// written as the value of an opcode.
// written as the value of an opcode, or if a region can't be read back as it is
// (see [NewRegion]).
func Build(inst *Instrument, opts ...BuildOptions) (*Document, error) {
var o BuildOptions
if len(opts) > 0 {
@@ -87,6 +121,9 @@ func Build(inst *Instrument, opts ...BuildOptions) (*Document, error) {
regions := make([][]opcodeLine, len(inst.Regions))
for i := range inst.Regions {
if err := checkRegion(inst.Regions[i]); err != nil {
return nil, fmt.Errorf("%w: region %d: %w", ErrInvalid, i, err)
}
regions[i] = regionOpcodes(inst.Regions[i])
}