Apply changes to a typed region as patches to its own header

Instrument.Patches works out the opcode changes that make region i read as an
updated Region, checking them against the reader, and Instrument.Apply makes
them in the document. Only the changed settings are touched, always in the
region's own header, so unknown opcodes and what other regions inherit stay as
they are. A change the header can't hold is an error and changes nothing.

FlatRegion gains Own, Inherited and Header to trace a region to its source, and
regionLines lists a region's opcodes with the field each sets.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 18:42:17 +00:00
co-authored by Claude Sonnet 5.5
parent 359de59033
commit 83c905633c
7 changed files with 704 additions and 102 deletions
+79 -96
View File
@@ -285,141 +285,124 @@ func hoist(regions [][]opcodeLine, min int) []opcodeLine {
return defines
}
// fieldLine is an opcode to write, with the field of [Region] that it sets.
// The field of an envelope's setting is Envelope.Delay and so on.
type fieldLine struct {
field string
opcodeLine
}
// regionOpcodes returns the opcodes that make a region, starting with its
// sample, with those that are the format's defaults left out.
func regionOpcodes(r Region) []opcodeLine {
lines := regionLines(r, false)
out := make([]opcodeLine, len(lines))
for i, l := range lines {
out[i] = l.opcodeLine
}
return out
}
// regionLines returns the opcodes that make a region, starting with its sample.
// Those that are the format's defaults are left out unless all is set.
func regionLines(r Region, all bool) []fieldLine {
d := defaultRegion()
ops := []opcodeLine{{"sample", r.Sample}}
add := func(name, value string) { ops = append(ops, opcodeLine{name, value}) }
if r.LowKey != d.LowKey {
add("lokey", strconv.Itoa(r.LowKey))
}
if r.HighKey != d.HighKey {
add("hikey", strconv.Itoa(r.HighKey))
out := []fieldLine{{"Sample", opcodeLine{"sample", r.Sample}}}
add := func(field string, nonDefault bool, name, value string) {
if all || nonDefault {
out = append(out, fieldLine{field, opcodeLine{name, value}})
}
}
add("LowKey", r.LowKey != d.LowKey, "lokey", strconv.Itoa(r.LowKey))
add("HighKey", r.HighKey != d.HighKey, "hikey", strconv.Itoa(r.HighKey))
if r.RootFromSample {
add("pitch_keycenter", "sample")
} else if r.RootKey != d.RootKey {
add("pitch_keycenter", strconv.Itoa(r.RootKey))
}
if r.LowVelocity != d.LowVelocity {
add("lovel", strconv.Itoa(r.LowVelocity))
}
if r.HighVelocity != d.HighVelocity {
add("hivel", strconv.Itoa(r.HighVelocity))
}
if r.KeyTrack != d.KeyTrack {
add("pitch_keytrack", num(r.KeyTrack))
}
if r.Tune != 0 {
add("tune", num(r.Tune))
}
if r.Volume != 0 {
add("volume", num(r.Volume))
}
if r.Pan != 0 {
add("pan", num(r.Pan))
add("RootKey", true, "pitch_keycenter", "sample")
} else {
add("RootKey", r.RootKey != d.RootKey, "pitch_keycenter", strconv.Itoa(r.RootKey))
}
add("LowVelocity", r.LowVelocity != d.LowVelocity, "lovel", strconv.Itoa(r.LowVelocity))
add("HighVelocity", r.HighVelocity != d.HighVelocity, "hivel", strconv.Itoa(r.HighVelocity))
add("KeyTrack", r.KeyTrack != d.KeyTrack, "pitch_keytrack", num(r.KeyTrack))
add("Tune", r.Tune != 0, "tune", num(r.Tune))
add("Volume", r.Volume != 0, "volume", num(r.Volume))
add("Pan", r.Pan != 0, "pan", num(r.Pan))
if r.Width != nil {
add("width", num(*r.Width))
}
if r.Offset != 0 {
add("offset", strconv.FormatInt(r.Offset, 10))
}
if r.VelocityTracking != d.VelocityTracking {
add("amp_veltrack", num(r.VelocityTracking))
add("Width", true, "width", num(*r.Width))
}
add("Offset", r.Offset != 0, "offset", strconv.FormatInt(r.Offset, 10))
add("VelocityTracking", r.VelocityTracking != d.VelocityTracking, "amp_veltrack", num(r.VelocityTracking))
for _, p := range r.VelocityCurve {
add(fmt.Sprintf("amp_velcurve_%d", p.Velocity), num(p.Gain))
add("VelocityCurve", true, fmt.Sprintf("amp_velcurve_%d", p.Velocity), num(p.Gain))
}
crossfade := func(c *Crossfade, which string) {
crossfade := func(field string, c *Crossfade, which string) {
if c == nil {
return
}
add("xfin_lo"+which, strconv.Itoa(c.FadeInLow))
add("xfin_hi"+which, strconv.Itoa(c.FadeInHigh))
add("xfout_lo"+which, strconv.Itoa(c.FadeOutLow))
add("xfout_hi"+which, strconv.Itoa(c.FadeOutHigh))
add(field, true, "xfin_lo"+which, strconv.Itoa(c.FadeInLow))
add(field, true, "xfin_hi"+which, strconv.Itoa(c.FadeInHigh))
add(field, true, "xfout_lo"+which, strconv.Itoa(c.FadeOutLow))
add(field, true, "xfout_hi"+which, strconv.Itoa(c.FadeOutHigh))
if c.Curve == CrossfadeGain {
add("xf_"+which+"curve", "gain")
add(field, true, "xf_"+which+"curve", "gain")
}
}
crossfade(r.KeyCrossfade, "key")
crossfade(r.VelocityCrossfade, "vel")
crossfade("KeyCrossfade", r.KeyCrossfade, "key")
crossfade("VelocityCrossfade", r.VelocityCrossfade, "vel")
e, de := r.Envelope, d.Envelope
for _, s := range []struct {
name string
v, d float64
field, name string
v, d float64
}{
{"ampeg_delay", e.Delay, de.Delay}, {"ampeg_attack", e.Attack, de.Attack}, {"ampeg_hold", e.Hold, de.Hold},
{"ampeg_decay", e.Decay, de.Decay}, {"ampeg_sustain", e.Sustain, de.Sustain}, {"ampeg_release", e.Release, de.Release},
{"Envelope.Delay", "ampeg_delay", e.Delay, de.Delay}, {"Envelope.Attack", "ampeg_attack", e.Attack, de.Attack},
{"Envelope.Hold", "ampeg_hold", e.Hold, de.Hold}, {"Envelope.Decay", "ampeg_decay", e.Decay, de.Decay},
{"Envelope.Sustain", "ampeg_sustain", e.Sustain, de.Sustain}, {"Envelope.Release", "ampeg_release", e.Release, de.Release},
} {
if s.v != s.d {
add(s.name, num(s.v))
}
}
if r.Trigger != d.Trigger {
add("trigger", r.Trigger)
add(s.field, s.v != s.d, s.name, num(s.v))
}
add("Trigger", r.Trigger != d.Trigger, "trigger", r.Trigger)
if r.LoopMode != "" {
add("loop_mode", r.LoopMode)
add("LoopMode", true, "loop_mode", r.LoopMode)
}
if r.LoopStart != nil {
add("loop_start", strconv.FormatInt(*r.LoopStart, 10))
add("LoopStart", true, "loop_start", strconv.FormatInt(*r.LoopStart, 10))
}
if r.LoopEnd != nil {
add("loop_end", strconv.FormatInt(*r.LoopEnd, 10))
}
if r.ReleaseDecay != 0 {
add("rt_decay", num(r.ReleaseDecay))
}
if r.SequenceLength != d.SequenceLength {
add("seq_length", strconv.Itoa(r.SequenceLength))
}
if r.SequencePosition != d.SequencePosition {
add("seq_position", strconv.Itoa(r.SequencePosition))
}
if r.LowRandom != 0 {
add("lorand", num(r.LowRandom))
}
if r.HighRandom != d.HighRandom {
add("hirand", num(r.HighRandom))
}
if r.Group != 0 {
add("group", strconv.Itoa(r.Group))
add("LoopEnd", true, "loop_end", strconv.FormatInt(*r.LoopEnd, 10))
}
add("ReleaseDecay", r.ReleaseDecay != 0, "rt_decay", num(r.ReleaseDecay))
add("SequenceLength", r.SequenceLength != d.SequenceLength, "seq_length", strconv.Itoa(r.SequenceLength))
add("SequencePosition", r.SequencePosition != d.SequencePosition, "seq_position", strconv.Itoa(r.SequencePosition))
add("LowRandom", r.LowRandom != 0, "lorand", num(r.LowRandom))
add("HighRandom", r.HighRandom != d.HighRandom, "hirand", num(r.HighRandom))
add("Group", r.Group != 0, "group", strconv.Itoa(r.Group))
if r.OffBy != nil {
add("off_by", strconv.Itoa(*r.OffBy))
add("OffBy", true, "off_by", strconv.Itoa(*r.OffBy))
}
if r.Polyphony != nil {
add("polyphony", strconv.Itoa(*r.Polyphony))
add("Polyphony", true, "polyphony", strconv.Itoa(*r.Polyphony))
}
if r.NotePolyphony != nil {
add("note_polyphony", strconv.Itoa(*r.NotePolyphony))
}
if r.OffMode != d.OffMode {
add("off_mode", r.OffMode)
}
if r.OffTime != d.OffTime {
add("off_time", num(r.OffTime))
add("NotePolyphony", true, "note_polyphony", strconv.Itoa(*r.NotePolyphony))
}
add("OffMode", r.OffMode != d.OffMode, "off_mode", r.OffMode)
add("OffTime", r.OffTime != d.OffTime, "off_time", num(r.OffTime))
for _, p := range []struct {
name string
v *float64
}{{"bend_up", r.BendUp}, {"bend_down", r.BendDown}, {"bend_step", r.BendStep}} {
field, name string
v *float64
}{{"BendUp", "bend_up", r.BendUp}, {"BendDown", "bend_down", r.BendDown}, {"BendStep", "bend_step", r.BendStep}} {
if p.v != nil {
add(p.name, num(*p.v))
add(p.field, true, p.name, num(*p.v))
}
}
for _, c := range r.Conditions {
scale, lo, hi := conditionOpcodes(c)
if lo == "" {
add(fmt.Sprintf("lohdcc%d", c.Controller), num(c.Low))
add(fmt.Sprintf("hihdcc%d", c.Controller), num(c.High))
add("Conditions", true, fmt.Sprintf("lohdcc%d", c.Controller), num(c.Low))
add("Conditions", true, fmt.Sprintf("hihdcc%d", c.Controller), num(c.High))
continue
}
add(lo, scaled(c.Low, scale))
add(hi, scaled(c.High, scale))
add("Conditions", true, lo, scaled(c.Low, scale))
add("Conditions", true, hi, scaled(c.High, scale))
}
if k := r.Keyswitch; k != nil {
for _, s := range []struct {
@@ -427,17 +410,17 @@ func regionOpcodes(r Region) []opcodeLine {
v *int
}{{"sw_last", k.Last}, {"sw_previous", k.Previous}, {"sw_down", k.Down}, {"sw_up", k.Up}} {
if s.v != nil {
add(s.name, strconv.Itoa(*s.v))
add("Keyswitch", true, s.name, strconv.Itoa(*s.v))
}
}
}
for _, m := range r.Modulations {
add(fmt.Sprintf("%s_oncc%d", m.Target, m.Controller), num(m.Depth))
add("Modulations", true, fmt.Sprintf("%s_oncc%d", m.Target, m.Controller), num(m.Depth))
if m.Curve != 0 {
add(fmt.Sprintf("%s_curvecc%d", m.Target, m.Controller), strconv.Itoa(m.Curve))
add("Modulations", true, fmt.Sprintf("%s_curvecc%d", m.Target, m.Controller), strconv.Itoa(m.Curve))
}
}
return ops
return out
}
// conditionOpcodes returns the opcodes that limit a source that isn't a