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