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
+23
View File
@@ -67,6 +67,29 @@ func ExampleInstrument_Apply() {
// volume=-6
}
// This example adds a region, then moves it first, keeping what the document
// has around the other regions.
func ExampleInstrument_AddRegion() {
doc, err := sfz.Parse(strings.NewReader("<region> sample=a.wav // the first\n<region> sample=b.wav\n"))
if err != nil {
log.Fatal(err)
}
inst := doc.Flatten().Instrument()
if err := inst.AddRegion(sfz.NewRegion("c.wav")); err != nil {
log.Fatal(err)
}
if err := inst.MoveRegion(2, 0); err != nil {
log.Fatal(err)
}
fmt.Print(doc)
// Output:
// <region>
// sample=c.wav
// <region> sample=a.wav // the first
// <region> sample=b.wav
}
// This example changes an opcode of a header directly, with the text around it
// as it was.
func ExampleHeader_Set() {