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
+53 -38
View File
@@ -10,29 +10,31 @@ import (
)
// BuildOptions change how [Build] lays out a document. The zero value makes one
// file, with the values written literally and two spaces of indent.
// file, with each value written out where it is used and an indent of two
// spaces.
type BuildOptions struct {
// Indent is what an opcode's line starts with. The default is two spaces.
// Indent is what the line of an opcode starts with. The default is two
// spaces.
Indent string
// SplitFiles puts the regions into included files, RegionsPerFile at a
// time, and leaves the controls and variables in the main file.
// SplitFiles puts the regions into included files, RegionsPerFile at a time,
// and leaves the controls, curves and variables in the main file.
SplitFiles bool
// RegionsPerFile is how many regions each included file holds. The default
// is 64.
// RegionsPerFile is how many regions each included file holds, when
// SplitFiles is set. The default is 64.
RegionsPerFile int
// FileNamer returns the path of an included file, given its number from 0,
// relative to the main file's folder. The default is regions/001.sfz,
// regions/002.sfz and so on.
// relative to the main file's folder, when SplitFiles is set. The default
// gives regions/001.sfz, regions/002.sfz and so on.
FileNamer func(index int) string
// Variables writes a #define for each value that more than MinRepeat
// regions share, and uses default_path for the folder that all samples are
// in.
// Variables writes a #define for each value that at least MinRepeat regions
// share, and sets default_path to the folder that all the samples are in.
Variables bool
// MinRepeat is how many regions must share an opcode's value to make it a
// variable. The default is 8.
// MinRepeat is how many regions must share an opcode's value for the value
// to become a variable, when Variables is set. The default is 8.
MinRepeat int
}
// defaults sets each option that has its zero value to its default.
func (o *BuildOptions) defaults() {
if o.Indent == "" {
o.Indent = " "
@@ -48,7 +50,8 @@ func (o *BuildOptions) defaults() {
}
}
// defaultRegion is a region that sets nothing: the format's defaults.
// defaultRegion returns a region that sets nothing, which has the format's
// defaults.
func defaultRegion() Region {
return Region{
HighKey: 127, RootKey: defaultRootKey, LowVelocity: defaultLowVelocity, HighVelocity: 127,
@@ -57,21 +60,24 @@ func defaultRegion() Region {
}
}
// opcodeLine is an opcode to write.
// opcodeLine is an opcode to write, with its value as text.
type opcodeLine struct{ name, value string }
// num writes a number with the digits it needs to read back as it was.
// num returns a number as text, with the fewest digits that read back as the
// same number.
func num(v float64) string { return strconv.FormatFloat(v, 'f', -1, 64) }
// scaled writes a condition's limit in the unit of its opcode, which is the
// limit times scale, rounded so that a limit that was a whole number stays
// one.
// scaled returns a condition's limit as text in the unit of its opcode, which
// is the limit times scale. It is rounded so that a limit that was a whole
// number in that unit is written as one.
func scaled(v, scale float64) string { return num(math.Round(v*scale*1e9) / 1e9) }
// Build makes a document that describes the instrument. Reading it with
// Build makes a new document that describes the instrument. Reading it with
// [Document.Flatten] and [Flat.Instrument] gives the instrument back, except
// for what the model doesn't hold: unread opcodes, and which headers regions
// were under.
// for what the model doesn't hold: the opcodes it doesn't read, and which
// headers the regions were under. It returns an error wrapping [ErrInvalid] if
// something in the instrument, such as a sample path or a label, can't be
// written as the value of an opcode.
func Build(inst *Instrument, opts ...BuildOptions) (*Document, error) {
var o BuildOptions
if len(opts) > 0 {
@@ -132,7 +138,7 @@ func Build(inst *Instrument, opts ...BuildOptions) (*Document, error) {
return &Document{Root: b.file("")}, nil
}
// The main file includes a file for each run of regions.
// Each run of regions goes in a file that the main file includes.
if b.err != nil {
return nil, b.err
}
@@ -159,19 +165,24 @@ func Build(inst *Instrument, opts ...BuildOptions) (*Document, error) {
return &Document{Root: main}, nil
}
// builder writes text and lexes it into files.
// builder writes the text of a file and lexes it into a [File].
type builder struct {
// indent is what the line of an opcode starts with.
indent string
text strings.Builder
// text is what has been written.
text strings.Builder
// err is the first thing that couldn't be written.
err error
}
// line writes a line of text.
func (b *builder) line(s string) {
b.text.WriteString(s)
b.text.WriteString("\n")
}
// header writes a header, and a line for each of its opcodes. It sets err for
// an opcode that can't be read back as it is written.
func (b *builder) header(name string, ops []opcodeLine) {
b.line("<" + name + ">")
for _, op := range ops {
@@ -182,6 +193,7 @@ func (b *builder) header(name string, ops []opcodeLine) {
}
}
// define writes a #define, or sets err if it can't be read back as written.
func (b *builder) define(name, value string) {
if err := checkDefine(name, value); err != nil && b.err == nil {
b.err = err
@@ -189,7 +201,7 @@ func (b *builder) define(name, value string) {
b.line("#define " + name + " " + value)
}
// file returns what the builder has written as a file with the path.
// file returns what the builder has written, lexed, as a file with the path p.
func (b *builder) file(p string) *File {
f := &File{Path: p, Nodes: lex(b.text.String())}
return f
@@ -221,7 +233,7 @@ func (b *builder) curves(inst *Instrument) {
}
// samplePrefix returns the folder, with its slash, that every sample is in, or
// "" if there is none.
// "" if there is no folder that they all are.
func samplePrefix(regions []Region) string {
if len(regions) == 0 {
return ""
@@ -238,10 +250,10 @@ func samplePrefix(regions []Region) string {
return prefix + "/"
}
// hoist replaces values that at least min regions share with variables, and
// returns their definitions. The sample is the first opcode of a region and is
// left alone. The names end in a run of three letters, so that none is the
// start of another.
// hoist replaces the values that at least min regions share with variables, and
// returns the definitions of the variables. The sample, the first opcode of a
// region, and values with spaces or variables are left alone. The names end in
// a run of three letters, so that none is the start of another.
func hoist(regions [][]opcodeLine, min int) []opcodeLine {
type key struct{ name, value string }
counts := map[key]int{}
@@ -285,15 +297,15 @@ 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.
// fieldLine is an opcode to write, with the field of [Region] it is read into.
// The field for a setting of the envelope is named as Envelope.Delay is.
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.
// sample, leaving out those that would only set the format's defaults.
func regionOpcodes(r Region) []opcodeLine {
lines := regionLines(r, false)
out := make([]opcodeLine, len(lines))
@@ -303,8 +315,10 @@ func regionOpcodes(r Region) []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.
// regionLines returns the opcodes that make a region, starting with its sample,
// each with the field it is read into. The opcodes that would only set the
// format's defaults are left out unless all is set. An optional setting that
// the region doesn't have, such as a loop mode, has no opcode either way.
func regionLines(r Region, all bool) []fieldLine {
d := defaultRegion()
out := []fieldLine{{"Sample", opcodeLine{"sample", r.Sample}}}
@@ -424,8 +438,9 @@ func regionLines(r Region, all bool) []fieldLine {
}
// conditionOpcodes returns the opcodes that limit a source that isn't a
// numbered controller, and the number their values are the range's times. A
// controller has none, and is written as lohdcc and hihdcc.
// numbered controller, and the number that their values are the range's limits
// times. A controller has no such opcodes, so lo and hi are empty for it, and
// it is written as lohdcc and hihdcc.
func conditionOpcodes(c Condition) (scale float64, lo, hi string) {
for _, b := range controllerBounds {
if b.source == c.Source {