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:
2026-12-01 19:08:00 +00:00
co-authored by Claude Sonnet 5.5
parent 41f752e1c1
commit 158661eefe
27 changed files with 716 additions and 437 deletions
+40 -29
View File
@@ -10,7 +10,10 @@ import (
"strings"
)
// Instrument reads the flattened instrument into the typed model.
// Instrument reads the flattened instrument into the typed model, with each
// setting that a controller moves taken at the controller's starting value.
// Regions that the typed model has no region for are counted in Skipped, and
// the opcodes and headers it doesn't read in Unread.
func (f *Flat) Instrument() *Instrument {
inst := &Instrument{Unread: map[string]int{}, Skipped: map[string]int{}}
for h, n := range f.Headers {
@@ -41,15 +44,18 @@ func (f *Flat) Instrument() *Instrument {
return inst
}
// evaluator turns regions' Opcodes into regions.
// evaluator turns the opcodes of regions into typed regions.
type evaluator struct {
cc map[int]float64
// cc are the starting values of the controllers, from 0 to 1.
cc map[int]float64
// curves are the curves that modulations can use, by index.
curves map[int]curve
// stops are the points of the custom curves, by index.
stops map[int][]CurveStop
}
// region returns the region the opcodes describe, or why it is skipped.
// region returns the region that the opcodes describe, or the reason, one of
// the Skipped constants, that there is none.
func (e evaluator) region(ops Opcodes) (Region, string) {
sample := strings.TrimSpace(ops["sample"])
switch {
@@ -122,16 +128,18 @@ func (e evaluator) region(ops Opcodes) (Region, string) {
}
return e.cc[n]
}
// A setting that the controllers move while a note plays has only what the
// note itself moves it by here, which is the controllers above 127, and the
// others are left to its modulations. The rest are taken at the starting
// controller values.
// own is the value of a setting that the controllers move while a note
// plays, with only what the note itself moves it by, which is the
// controllers above 127. The other controllers are left to the region's
// modulations.
own := func(target string, base float64) float64 {
if v, ok := parseNumber(ops[target]); ok {
base = v
}
return base + e.modulation(ops, target, cc, extendedOnly)
}
// value is the value of a setting that the controllers don't move while a
// note plays, with every controller taken at its starting value.
value := func(target string, base float64) float64 {
if v, ok := parseNumber(ops[target]); ok {
base = v
@@ -225,12 +233,12 @@ func (e evaluator) region(ops Opcodes) (Region, string) {
return r, ""
}
// velocityCurveRE reads the velocity of an amp_velcurve_N opcode.
// velocityCurveRE reads the velocity from the name of an amp_velcurve_N opcode.
var velocityCurveRE = regexp.MustCompile(`^amp_velcurve_(\d{1,3})$`)
// velocityCurve returns the points of a region's amp_velcurve_N Opcodes, with
// the ends the curve doesn't set at no level for velocity 0 and the full
// level for 127, or nil if the region sets none.
// velocityCurve returns the points of a region's amp_velcurve_N opcodes, in
// order. A curve that doesn't set the ends starts at no level for velocity 0
// and ends at the full level for 127. It returns nil if the region sets none.
func velocityCurve(ops Opcodes) []CurvePoint {
points := map[int]float64{}
for name, v := range ops {
@@ -260,10 +268,10 @@ func velocityCurve(ops Opcodes) []CurvePoint {
return out
}
// crossfade returns the crossfade of a region's xfin_ and xfout_ Opcodes over
// keys, or over velocities for which is "vel", or nil if it sets none. A
// fade in that isn't given starts and ends at 0, and a fade out at 127.
// shift moves keys, as the <control> header's offsets do.
// crossfade returns the crossfade of a region's xfin_ and xfout_ opcodes over
// keys, or over velocities if which is "vel". It returns nil if the region sets
// none. A fade in that isn't given starts and ends at 0, and a fade out starts
// and ends at 127. The shift moves keys, as the <control> header's offsets do.
func crossfade(ops Opcodes, which string, shift int) *Crossfade {
read := func(name string, fallback int) (int, bool) {
v, ok := ops[name]
@@ -298,8 +306,8 @@ func crossfade(ops Opcodes, which string, shift int) *Crossfade {
return c
}
// voiceLimit reads a number of voices, which is at least 1, or nil if v isn't
// one.
// voiceLimit reads a number of voices, which is at least 1. It returns nil if
// v isn't one.
func voiceLimit(v string) *int {
n, ok := parseNumber(v)
if !ok || n < 1 {
@@ -312,7 +320,7 @@ func voiceLimit(v string) *int {
// minVolume is the quietest a region plays, in dB, which stands for silence.
const minVolume = -144
// number returns the number an opcode sets, or nil if it doesn't set one.
// number returns the number that an opcode sets, or nil if it doesn't set one.
func number(ops Opcodes, name string) *float64 {
if v, ok := parseNumber(ops[name]); ok {
return &v
@@ -320,16 +328,18 @@ func number(ops Opcodes, name string) *float64 {
return nil
}
// ptr returns a pointer to a copy of v.
func ptr[T any](v T) *T { return &v }
// samplePath joins a region's default_path and sample into a clean relative
// path with forward slashes.
// samplePath joins a region's default_path and sample into a clean path with
// forward slashes. The path is relative unless the text makes it absolute.
func samplePath(defaultPath, sample string) string {
p := strings.ReplaceAll(strings.TrimSpace(defaultPath)+sample, `\`, "/")
return strings.TrimPrefix(path.Clean(p), "./")
}
// firstOf returns the value of the first of the Opcodes that is set.
// firstOf returns the value of the first of the named opcodes that is set, or
// an empty string if none is.
func firstOf(ops Opcodes, names ...string) string {
for _, n := range names {
if v, ok := ops[n]; ok {
@@ -339,7 +349,7 @@ func firstOf(ops Opcodes, names ...string) string {
return ""
}
// frames reads a sample position in frames, or returns nil if v isn't one.
// frames reads a sample position in frames. It returns nil if v isn't one.
func frames(v string) *int64 {
n, ok := parseNumber(v)
if !ok || n < 0 {
@@ -349,12 +359,13 @@ func frames(v string) *int64 {
return &f
}
// clamp returns v, or the nearest of lo and hi if v is outside them.
func clamp(v, lo, hi float64) float64 {
return math.Min(hi, math.Max(lo, v))
}
// handledNames are the opcodes the typed model reads, or uses to choose
// regions.
// handledNames are the opcodes that the typed model reads, or uses to choose
// regions, and so doesn't count as unread.
var handledNames = map[string]bool{
"sample": true, "default_path": true, "note_offset": true, "octave_offset": true,
"key": true, "lokey": true, "hikey": true, "pitch_keycenter": true, "lovel": true, "hivel": true,
@@ -374,15 +385,15 @@ var handledNames = map[string]bool{
"lochan": true, "hichan": true, "lobend": true, "hibend": true, "lochanaft": true, "hichanaft": true, "lopolyaft": true, "hipolyaft": true,
}
// modulatedTargets are the opcodes whose controller modulation the typed model
// reads.
// modulatedTargets are the opcodes whose modulation by controllers the typed
// model reads.
var modulatedTargets = []string{
"pitch_keytrack", "tune", "pitch", "volume", "gain", "amplitude", "pan", "width", "offset", "amp_veltrack",
"ampeg_delay", "ampeg_attack", "ampeg_hold", "ampeg_decay", "ampeg_sustain", "ampeg_release", "rt_decay",
}
// handledRE matches the opcodes the typed model reads that carry a controller
// number or only name things.
// handledRE matches the opcodes that the typed model reads and that carry a
// controller number, or only name something.
var handledRE = regexp.MustCompile(`^(?:(?:lo|hi)(?:hd)?cc\d+|set_(?:hd)?cc\d+|label_cc\d+|amp_velcurve_\d+|[a-z_]*_label)$`)
// handled reports whether the typed model reads an opcode.