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
+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)