Let AddRegion clear inherited opcodes with an empty global header

When the headers above the end of the document set what a region's own
opcodes can't cancel, such as modulations or note_polyphony, AddRegion writes
an empty <global> before the new region, which clears the levels below it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 01:45:14 +00:00
co-authored by Claude Sonnet 5.5
parent 8db34cc16c
commit 3dd99bd1ed
3 changed files with 69 additions and 15 deletions
+2 -2
View File
@@ -45,9 +45,9 @@ 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, so a comment on the lines above a `<region>` stays with the header before it. They read the document again, so they refuse an instrument whose regions were changed other than by `Apply`, or whose document was edited since it was read. 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.
- `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, so a comment on the lines above a `<region>` stays with the header before it. They read the document again, so they refuse an instrument whose regions were changed other than by `Apply`, or whose document was edited since it was read. A new region goes at the end of the main file. If the headers above it set what a region can't cancel, such as modulations, `AddRegion` writes an empty `<global>` before it, which clears what the `<global>`, `<master>` and `<group>` above set. 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>` or over a `#define` that changes it, or text that wouldn't read back as the edited nodes, such as a comment that never ends.
- `ErrUnpatchable` is for a region that would read differently where it ends up: a new region whose sample a `default_path` changes, or a region moved under another `<group>` or over a `#define` that changes it, or text that wouldn't read back as the edited nodes, such as a comment that never ends.
- `Node` positions are worked out when asked for, with `File.Position`, so edits stay cheap.
## Writing and building
+22 -13
View File
@@ -22,26 +22,29 @@ func (inst *Instrument) Source(i int) (*FlatRegion, bool) {
// AddRegion adds a <region> for r at the end of the document that the
// instrument was read from, and makes it the last of the instrument's regions.
// It writes only the opcodes that differ from the format's defaults if they
// read back as r, and every opcode if the headers above it need to be
// overridden. It changes nothing, and returns an error, if r can't be written
// so that it reads back as r: [ErrInvalid] if r or its sample can't be written
// as it is, [ErrUnpatchable] if the headers above the new region or a
// default_path change it, and [ErrNoSource] if the instrument wasn't read from
// a document.
// read back as r. If the headers above the end of the document change the
// region, it writes every opcode to override them, and if that doesn't help,
// such as when they set modulations, it writes an empty <global> header first,
// which clears what the <global>, <master> and <group> headers above set. It
// changes nothing, and returns an error, if r can't be written so that it
// reads back as r: [ErrInvalid] if r or its sample can't be written as it is,
// [ErrUnpatchable] if a default_path changes it, and [ErrNoSource] if the
// instrument wasn't read from a document.
//
// Afterwards the instrument is as the document reads, with Skipped, Unread and
// Controls too. It returns an error wrapping [ErrNoSource], and changes nothing,
// if the document was edited since the instrument was read, or the instrument's
// regions were changed other than with [Instrument.Apply]. Use [Instrument.Source] to set opcodes the typed model doesn't
// hold on the new region's header.
// regions were changed other than with [Instrument.Apply]. Use
// [Instrument.Source] to set opcodes the typed model doesn't hold on the new
// region's header.
func (inst *Instrument) AddRegion(r Region) error {
if err := checkRegion(r); err != nil {
return fmt.Errorf("%w: %w", ErrInvalid, err)
}
var err error
for _, all := range []bool{false, true} {
for _, how := range []struct{ reset, all bool }{{false, false}, {false, true}, {true, false}} {
err = inst.restructure(
func() error { return inst.writeRegion(r, all) },
func() error { return inst.writeRegion(r, how.reset, how.all) },
func(old []Region) []Region { return append(slices.Clone(old), r) })
if !errors.Is(err, ErrUnpatchable) {
return err
@@ -50,10 +53,16 @@ func (inst *Instrument) AddRegion(r Region) error {
return err
}
// writeRegion adds a <region> for r at the end of the main file. It writes the
// opcodes that the format's defaults would give too if all is set.
func (inst *Instrument) writeRegion(r Region, all bool) error {
// writeRegion adds a <region> for r at the end of the main file, after an empty
// <global> if reset is set. It writes the opcodes that the format's defaults
// would give too if all is set.
func (inst *Instrument) writeRegion(r Region, reset, all bool) error {
defaultPath := strings.ReplaceAll(strings.TrimSpace(inst.doc.Flatten().Control["default_path"]), `\`, "/")
if reset {
if _, err := inst.doc.AddHeader("global", nil); err != nil {
return err
}
}
h, err := inst.doc.AddHeader("region", nil)
if err != nil {
return err
+45
View File
@@ -4,6 +4,7 @@ import (
"errors"
"math/rand"
"os"
"path"
"path/filepath"
"reflect"
"strconv"
@@ -129,6 +130,39 @@ func TestAddRegionOverridesInherited(t *testing.T) {
}
}
// TestAddRegionResetsInheritance checks that a region added under headers that
// set what a region can't cancel gets an empty <global> before it, once, and
// that the other regions are as they were.
func TestAddRegionResetsInheritance(t *testing.T) {
text := "<global> note_polyphony=2 volume=-6\n<group> ampeg_release=3 volume_oncc7=-12\n<region> sample=a.wav\n"
doc := mustParse(t, text, nil)
inst := readInstrument(t, doc)
a := inst.Regions[0]
r := NewRegion("b.wav")
if err := inst.AddRegion(r); err != nil {
t.Fatal(err)
}
if want := text + "<global>\n<region>\nsample=b.wav\n"; doc.String() != want {
t.Errorf("document %q, want %q", doc.String(), want)
}
if wrong := differing(inst.Regions[0], a); len(wrong) > 0 {
t.Errorf("the first region changed in %v", wrong)
}
if wrong := differing(inst.Regions[1], r); len(wrong) > 0 {
t.Errorf("the new region differs in %v", wrong)
}
// The next region inherits nothing, so it needs no header.
if err := inst.AddRegion(NewRegion("c.wav")); err != nil {
t.Fatal(err)
}
if got := strings.Count(doc.String(), "<global>"); got != 2 {
t.Errorf("%d <global> headers in %q", got, doc.String())
}
if err := inst.MoveRegion(2, 1); err != nil {
t.Errorf("moving the added regions: %v", err)
}
}
// TestRegionEditRefusals checks that an edit that can't be made says why and
// leaves the document as it was.
func TestRegionEditRefusals(t *testing.T) {
@@ -276,6 +310,17 @@ func TestRegionEditsOnFixtures(t *testing.T) {
{"swap neighbours", func() error { return inst.MoveRegion(pair+1, pair) }, n, pair, second},
{"remove one", func() error { return inst.RemoveRegion(pair) }, n - 1, pair, first},
}
// A region added at the end gets an empty <global>, as the last group sets
// what a region can't cancel.
// Its sample is in the folder that the file's default_path names.
added := NewRegion(path.Dir(inst.Regions[n-1].Sample) + "/Z.wav")
steps = append(steps, struct {
name string
do func() error
n int
at int
want string
}{"add", func() error { return inst.AddRegion(added) }, n, n - 1, added.Sample})
for _, s := range steps {
if err := s.do(); err != nil {
t.Fatalf("%s: %v", s.name, err)