Rewrite the code comments for consistent tone and accuracy
Comments only: the tokens of every Go file are unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -13,40 +13,43 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrNoSource is returned for a region that can't be edited because its
|
||||
// opcodes aren't all in its own header: the instrument wasn't read from a
|
||||
// document, a variable made the header, or an include or a variable
|
||||
// brings in opcodes after it.
|
||||
// ErrNoSource is returned for a region that has no header to edit: the
|
||||
// instrument wasn't read from a document, a variable made the header, or an
|
||||
// include or a variable brings in opcodes after it.
|
||||
ErrNoSource = errors.New("no header to edit for the region")
|
||||
// ErrUnpatchable is returned for a region that its own header can't hold:
|
||||
// the changes would need to alter a header that other regions inherit, or a
|
||||
// setting that the typed model reads from more than one place.
|
||||
// ErrUnpatchable is returned for a change that the region's own header can't
|
||||
// hold: it would need an opcode that the region inherits to be removed, or a
|
||||
// setting that the typed model doesn't read from the header, such as a
|
||||
// sample outside default_path.
|
||||
ErrUnpatchable = errors.New("the region can't be written to its own header")
|
||||
)
|
||||
|
||||
// Patch is a change to one opcode of a region's own header.
|
||||
type Patch struct {
|
||||
// Name is the opcode's name.
|
||||
Name string
|
||||
// Value is what the opcode is set to, unless Delete is set.
|
||||
Value string
|
||||
Value string
|
||||
// Delete is whether the opcode is removed from the header.
|
||||
Delete bool
|
||||
}
|
||||
|
||||
// Patches returns the changes to the opcodes of the header of region i that
|
||||
// make it read as updated. Only the settings that updated changes are touched,
|
||||
// and the header's other opcodes, known or not, are left as they are. A change
|
||||
// is always made in the region's own header, over what it inherits. It returns
|
||||
// [ErrNoSource] or [ErrUnpatchable] if there is no way to make the change there,
|
||||
// and then nothing has changed.
|
||||
// make it read as updated. It only touches the settings that updated changes,
|
||||
// and leaves the header's other opcodes, known or not, as they are. Changes are
|
||||
// made in the region's own header, over what the region inherits, so other
|
||||
// regions are not affected. It returns [ErrNoSource] or [ErrUnpatchable] if the
|
||||
// header can't hold the change.
|
||||
func (inst *Instrument) Patches(i int, updated Region) ([]Patch, error) {
|
||||
patches, _, err := inst.plan(i, updated)
|
||||
return patches, err
|
||||
}
|
||||
|
||||
// Apply changes the document the instrument was read from so that region i
|
||||
// reads as updated, with the changes that [Instrument.Patches] returns. It
|
||||
// changes nothing and returns an error if they can't be made. The instrument
|
||||
// now holds updated, and what it was read from isn't read again.
|
||||
// Apply changes the document that the instrument was read from so that region
|
||||
// i reads as updated, with the changes that [Instrument.Patches] returns. It
|
||||
// changes nothing, and returns an error, if they can't be made. Afterwards the
|
||||
// instrument holds updated as region i, and its other regions are as they were.
|
||||
// The [Flat] it was read from isn't changed.
|
||||
func (inst *Instrument) Apply(i int, updated Region) error {
|
||||
patches, own, err := inst.plan(i, updated)
|
||||
if err != nil {
|
||||
@@ -73,20 +76,26 @@ func (inst *Instrument) Apply(i int, updated Region) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// plan works out the opcodes the region's own header ends with, and the patches
|
||||
// that get it there.
|
||||
// plan works out the opcodes that the header of region i has once it reads as
|
||||
// updated, and the patches that take it there from the opcodes it has. It
|
||||
// checks each plan by reading a region from the opcodes, so a plan that is
|
||||
// returned reads back as updated.
|
||||
func (inst *Instrument) plan(i int, updated Region) ([]Patch, Opcodes, error) {
|
||||
src, err := inst.source(i)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
changed := differing(inst.Regions[i], updated)
|
||||
// RootFromSample and RootKey are both set by pitch_keycenter.
|
||||
if slicesContains(changed, "RootFromSample") && !slicesContains(changed, "RootKey") {
|
||||
changed = append(changed, "RootKey")
|
||||
}
|
||||
own := maps.Clone(src.Own)
|
||||
defaultPath := strings.ReplaceAll(strings.TrimSpace(src.Opcodes["default_path"]), `\`, "/")
|
||||
lines := regionLines(updated, true)
|
||||
// Each changed field has the opcodes it owns replaced by those of the updated
|
||||
// region, with the defaults written out so that they override what the
|
||||
// region inherits.
|
||||
for _, field := range changed {
|
||||
for name := range own {
|
||||
if owns(field, name) {
|
||||
@@ -99,15 +108,17 @@ func (inst *Instrument) plan(i int, updated Region) ([]Patch, Opcodes, error) {
|
||||
}
|
||||
value := l.value
|
||||
if field == "Sample" {
|
||||
// The sample is read as relative to default_path.
|
||||
value = strings.TrimPrefix(value, defaultPath)
|
||||
}
|
||||
own[l.name] = value
|
||||
}
|
||||
}
|
||||
|
||||
// Settings that other opcodes move, such as a key shifted by an offset, a
|
||||
// volume that amplitude adds to, or a level that a clamp holds, are brought
|
||||
// to the value by the difference that is left.
|
||||
// A setting that other opcodes move, such as a key that an offset shifts or
|
||||
// a volume that an amplitude adds to, is read as other than what was
|
||||
// written. Each opcode is moved by the difference that is left, until the
|
||||
// setting reads as it should, or no opcode can move it further.
|
||||
var wrong []string
|
||||
for try := 0; ; try++ {
|
||||
got, reason := inst.eval.region(src.Inherited.with(own))
|
||||
@@ -132,6 +143,8 @@ func (inst *Instrument) plan(i int, updated Region) ([]Patch, Opcodes, error) {
|
||||
return nil, nil, fmt.Errorf("%w: %s", ErrUnpatchable, strings.Join(wrong, ", "))
|
||||
}
|
||||
|
||||
// The patches are the differences between the opcodes the header has and
|
||||
// those it should have.
|
||||
var patches []Patch
|
||||
for name, value := range own {
|
||||
if old, ok := src.Own[name]; !ok || old != value {
|
||||
@@ -147,6 +160,7 @@ func (inst *Instrument) plan(i int, updated Region) ([]Patch, Opcodes, error) {
|
||||
return patches, own, nil
|
||||
}
|
||||
|
||||
// slicesContains reports whether list has s.
|
||||
func slicesContains(list []string, s string) bool {
|
||||
for _, v := range list {
|
||||
if v == s {
|
||||
@@ -156,7 +170,8 @@ func slicesContains(list []string, s string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// source returns the flattened region i, if its header can be edited.
|
||||
// source returns the flattened region i if its header can be edited, or an
|
||||
// error wrapping [ErrNoSource].
|
||||
func (inst *Instrument) source(i int) (FlatRegion, error) {
|
||||
if i < 0 || i >= len(inst.Regions) || i >= len(inst.sources) {
|
||||
return FlatRegion{}, fmt.Errorf("%w: there is no region %d", ErrNoSource, i)
|
||||
@@ -178,8 +193,8 @@ func (inst *Instrument) source(i int) (FlatRegion, error) {
|
||||
return src, nil
|
||||
}
|
||||
|
||||
// scalarOpcodes are the numeric fields of a region, by the opcode that sets
|
||||
// each.
|
||||
// scalarOpcodes are the numeric fields of a region, each with the opcode that
|
||||
// sets it. A path such as Envelope.Attack names a field of the envelope.
|
||||
var scalarOpcodes = map[string]string{
|
||||
"LowKey": "lokey", "HighKey": "hikey", "RootKey": "pitch_keycenter", "LowVelocity": "lovel", "HighVelocity": "hivel",
|
||||
"KeyTrack": "pitch_keytrack", "Tune": "tune", "Volume": "volume", "Pan": "pan", "Width": "width", "Offset": "offset",
|
||||
@@ -189,8 +204,9 @@ var scalarOpcodes = map[string]string{
|
||||
"Envelope.Decay": "ampeg_decay", "Envelope.Sustain": "ampeg_sustain", "Envelope.Release": "ampeg_release",
|
||||
}
|
||||
|
||||
// adjust moves an opcode of the region's own header by how far a region read
|
||||
// from it is from the one wanted, and reports whether that changed it.
|
||||
// adjust moves an opcode of the region's own header by how far the field of the
|
||||
// region got is from that of the region want, and reports whether that changed
|
||||
// the opcode.
|
||||
func adjust(own Opcodes, opcode, field string, got, want Region) bool {
|
||||
g, ok := numberAt(reflect.ValueOf(got), field)
|
||||
w, ok2 := numberAt(reflect.ValueOf(want), field)
|
||||
@@ -212,8 +228,8 @@ func adjust(own Opcodes, opcode, field string, got, want Region) bool {
|
||||
return next != base
|
||||
}
|
||||
|
||||
// fieldAt returns the field of a struct at a path such as Envelope.Attack, with
|
||||
// a pointer followed.
|
||||
// fieldAt returns the field of a struct at a path such as Envelope.Attack,
|
||||
// following pointers, and whether there is one.
|
||||
func fieldAt(v reflect.Value, path string) (reflect.Value, bool) {
|
||||
for _, name := range strings.Split(path, ".") {
|
||||
for v.Kind() == reflect.Pointer && !v.IsNil() {
|
||||
@@ -227,8 +243,8 @@ func fieldAt(v reflect.Value, path string) (reflect.Value, bool) {
|
||||
return v, v.IsValid()
|
||||
}
|
||||
|
||||
// numberAt returns the number at a path in a region, or false if there is none,
|
||||
// as in a width that isn't set.
|
||||
// numberAt returns the number at a path in a region, and false if there is
|
||||
// none, as for a width that isn't set.
|
||||
func numberAt(v reflect.Value, path string) (float64, bool) {
|
||||
f, ok := fieldAt(v, path)
|
||||
if !ok {
|
||||
@@ -249,8 +265,8 @@ func numberAt(v reflect.Value, path string) (float64, bool) {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// differing returns the fields of two regions that differ, naming a setting of
|
||||
// the envelope as Envelope.Attack.
|
||||
// differing returns the names of the fields of two regions that differ, naming
|
||||
// a setting of the envelope as Envelope.Attack is named.
|
||||
func differing(a, b Region) []string {
|
||||
var out []string
|
||||
va, vb := reflect.ValueOf(a), reflect.ValueOf(b)
|
||||
@@ -270,8 +286,8 @@ func differing(a, b Region) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
// equalValues compares two values of a region, with numbers equal to within a
|
||||
// rounding error, and an empty list the same as none.
|
||||
// equalValues reports whether two values of a region are equal. Numbers are
|
||||
// equal to within a rounding error, and an empty list is the same as none.
|
||||
func equalValues(a, b reflect.Value) bool {
|
||||
switch a.Kind() {
|
||||
case reflect.Float64:
|
||||
@@ -302,8 +318,8 @@ func equalValues(a, b reflect.Value) bool {
|
||||
return a.Interface() == b.Interface()
|
||||
}
|
||||
|
||||
// ownedNames are the opcodes that only one field of a region is read from,
|
||||
// by field.
|
||||
// ownedNames are the opcodes that one field of a region is read from and no
|
||||
// other field, by the name of the field.
|
||||
var ownedNames = map[string]*regexp.Regexp{
|
||||
"Sample": regexp.MustCompile(`^sample$`), "LowKey": regexp.MustCompile(`^lokey$`), "HighKey": regexp.MustCompile(`^hikey$`),
|
||||
"RootKey": regexp.MustCompile(`^pitch_keycenter$`), "LowVelocity": regexp.MustCompile(`^lovel$`), "HighVelocity": regexp.MustCompile(`^hivel$`),
|
||||
@@ -327,12 +343,13 @@ var ownedNames = map[string]*regexp.Regexp{
|
||||
"Keyswitch": regexp.MustCompile(`^sw_(last|previous|down|up)$`),
|
||||
}
|
||||
|
||||
// controllerLimitRE reads the opcodes that limit a controller, and its number.
|
||||
// controllerLimitRE reads the name of an opcode that limits a controller, and
|
||||
// the controller's number.
|
||||
var controllerLimitRE = regexp.MustCompile(`^(?:lo|hi)(?:hd)?cc(\d+)$`)
|
||||
|
||||
// owns reports whether a field of a region is the only one that an opcode of
|
||||
// the region's header is read into, so that the opcode goes when the field is
|
||||
// written again.
|
||||
// owns reports whether a field of a region is the only one that an opcode is
|
||||
// read into. The opcodes that a field owns are removed when the field is
|
||||
// written again, so that none is left to set it otherwise.
|
||||
func owns(field, name string) bool {
|
||||
switch field {
|
||||
case "Conditions":
|
||||
|
||||
Reference in New Issue
Block a user