refactor(tracker): group Model methods, with each group in one source file

This commit is contained in:
5684185+vsariola@users.noreply.github.com
2026-01-25 13:08:45 +02:00
parent b93304adab
commit 86ca3fb300
44 changed files with 4813 additions and 4482 deletions

View File

@ -11,6 +11,228 @@ import (
"gopkg.in/yaml.v3"
)
// Params returns the Param view of the Model, containing methods to manipulate
// the parameters.
func (m *Model) Params() *ParamModel { return (*ParamModel)(m) }
type ParamModel Model
// Wires returns the wires of the current instrument, telling which parameters
// are connected to which.
func (m *ParamModel) Wires(yield func(wire Wire) bool) {
i := m.d.InstrIndex
if i < 0 || i >= len(m.derived.patch) {
return
}
for _, wire := range m.derived.patch[i].wires {
wire.Highlight = (wire.FromSet && m.d.UnitIndex == wire.From) || (wire.ToSet && m.d.UnitIndex == wire.To.Y && m.d.ParamIndex == wire.To.X)
if !yield(wire) {
return
}
}
}
// chooseSendSource
type chooseSendSource struct {
ID int
*Model
}
func (m *ParamModel) IsChoosingSendTarget() bool {
return m.d.SendSource > 0
}
func (m *ParamModel) ChooseSendSource(id int) Action {
return MakeAction(chooseSendSource{ID: id, Model: (*Model)(m)})
}
func (s chooseSendSource) Do() {
defer (*Model)(s.Model).change("ChooseSendSource", NoChange, MinorChange)()
if s.Model.d.SendSource == s.ID {
s.Model.d.SendSource = 0 // unselect
return
}
s.Model.d.SendSource = s.ID
}
// chooseSendTarget
type chooseSendTarget struct {
ID int
Port int
*Model
}
func (m *ParamModel) ChooseSendTarget(id int, port int) Action {
return MakeAction(chooseSendTarget{ID: id, Port: port, Model: (*Model)(m)})
}
func (s chooseSendTarget) Do() {
defer (*Model)(s.Model).change("ChooseSendTarget", SongChange, MinorChange)()
sourceID := (*Model)(s.Model).d.SendSource
s.d.SendSource = 0
if sourceID <= 0 || s.ID <= 0 || s.Port < 0 || s.Port > 7 {
return
}
si, su, err := s.d.Song.Patch.FindUnit(sourceID)
if err != nil {
return
}
s.d.Song.Patch[si].Units[su].Parameters["target"] = s.ID
s.d.Song.Patch[si].Units[su].Parameters["port"] = s.Port
}
// paramsColumns
type paramsColumns Model
func (m *ParamModel) Columns() List { return List{(*paramsColumns)(m)} }
func (pt *paramsColumns) Selected() int { return pt.d.ParamIndex }
func (pt *paramsColumns) Selected2() int { return pt.d.ParamIndex }
func (pt *paramsColumns) SetSelected(index int) { pt.d.ParamIndex = index }
func (pt *paramsColumns) SetSelected2(index int) {}
func (pt *paramsColumns) Count() int { return (*ParamModel)(pt).Width() }
// Model and Params methods
func (pt *ParamModel) Table() Table { return Table{pt} }
func (pt *ParamModel) Cursor() Point { return Point{pt.d.ParamIndex, pt.d.UnitIndex} }
func (pt *ParamModel) Cursor2() Point { return Point{pt.d.ParamIndex, pt.d.UnitIndex2} }
func (pt *ParamModel) SetCursor(p Point) {
pt.d.ParamIndex = max(min(p.X, pt.Width()-1), 0)
pt.d.UnitIndex = max(min(p.Y, pt.Height()-1), 0)
}
func (pt *ParamModel) SetCursor2(p Point) {
pt.d.ParamIndex = max(min(p.X, pt.Width()-1), 0)
pt.d.UnitIndex2 = max(min(p.Y, pt.Height()-1), 0)
}
func (pt *ParamModel) Width() int {
if pt.d.InstrIndex < 0 || pt.d.InstrIndex >= len(pt.derived.patch) {
return 0
}
// TODO: we hack the +1 so that we always have one extra cell to draw the
// comments. Refactor the gioui side so that we can specify the width and
// height regardless of the underlying table size
return pt.derived.patch[pt.d.InstrIndex].paramsWidth + 1
}
func (pt *ParamModel) RowWidth(y int) int {
if pt.d.InstrIndex < 0 || pt.d.InstrIndex >= len(pt.derived.patch) || y < 0 || y >= len(pt.derived.patch[pt.d.InstrIndex].params) {
return 0
}
return len(pt.derived.patch[pt.d.InstrIndex].params[y])
}
func (pt *ParamModel) Height() int { return (*Model)(pt).Unit().List().Count() }
func (pt *ParamModel) MoveCursor(dx, dy int) (ok bool) {
p := pt.Cursor()
p.X += dx
p.Y += dy
pt.SetCursor(p)
return p == pt.Cursor()
}
func (pt *ParamModel) Item(p Point) Parameter {
if pt.d.InstrIndex < 0 || pt.d.InstrIndex >= len(pt.derived.patch) || p.Y < 0 || p.Y >= len(pt.derived.patch[pt.d.InstrIndex].params) || p.X < 0 || p.X >= len(pt.derived.patch[pt.d.InstrIndex].params[p.Y]) {
return Parameter{}
}
return pt.derived.patch[pt.d.InstrIndex].params[p.Y][p.X]
}
func (pt *ParamModel) clear(p Point) {
q := pt.Item(p)
q.Reset()
}
func (pt *ParamModel) set(p Point, value int) {
q := pt.Item(p)
q.SetValue(value)
}
func (pt *ParamModel) add(rect Rect, delta int, largeStep bool) (ok bool) {
for y := rect.TopLeft.Y; y <= rect.BottomRight.Y; y++ {
for x := rect.TopLeft.X; x <= rect.BottomRight.X; x++ {
p := Point{x, y}
q := pt.Item(p)
if !q.Add(delta, largeStep) {
return false
}
}
}
return true
}
type paramsTable struct {
Params [][]int `yaml:",flow"`
}
func (pt *ParamModel) marshal(rect Rect) (data []byte, ok bool) {
width := rect.BottomRight.X - rect.TopLeft.X + 1
height := rect.BottomRight.Y - rect.TopLeft.Y + 1
var table = paramsTable{Params: make([][]int, 0, width)}
for x := 0; x < width; x++ {
table.Params = append(table.Params, make([]int, 0, rect.BottomRight.Y-rect.TopLeft.Y+1))
for y := 0; y < height; y++ {
p := pt.Item(Point{x + rect.TopLeft.X, y + rect.TopLeft.Y})
table.Params[x] = append(table.Params[x], p.Value())
}
}
ret, err := yaml.Marshal(table)
if err != nil {
return nil, false
}
return ret, true
}
func (pt *ParamModel) unmarshal(data []byte) (paramsTable, bool) {
var table paramsTable
yaml.Unmarshal(data, &table)
if len(table.Params) == 0 {
return paramsTable{}, false
}
for i := 0; i < len(table.Params); i++ {
if len(table.Params[i]) > 0 {
return table, true
}
}
return paramsTable{}, false
}
func (pt *ParamModel) unmarshalAtCursor(data []byte) (ret bool) {
table, ok := pt.unmarshal(data)
if !ok {
return false
}
for i := 0; i < len(table.Params); i++ {
for j, q := range table.Params[i] {
x := i + pt.Cursor().X
y := j + pt.Cursor().Y
p := pt.Item(Point{x, y})
ret = p.SetValue(q) || ret
}
}
return ret
}
func (pt *ParamModel) unmarshalRange(rect Rect, data []byte) (ret bool) {
table, ok := pt.unmarshal(data)
if !ok {
return false
}
if len(table.Params) == 0 || len(table.Params[0]) == 0 {
return false
}
width := rect.BottomRight.X - rect.TopLeft.X + 1
height := rect.BottomRight.Y - rect.TopLeft.Y + 1
if len(table.Params) < width {
return false
}
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
if len(table.Params[0]) < height {
return false
}
p := pt.Item(Point{x + rect.TopLeft.X, y + rect.TopLeft.Y})
ret = p.SetValue(table.Params[x][y]) || ret
}
}
return ret
}
func (pt *ParamModel) change(kind string, severity ChangeSeverity) func() {
return (*Model)(pt).change(kind, PatchChange, severity)
}
func (pt *ParamModel) cancel() {
pt.changeCancel = true
}
type (
// Parameter represents a parameter of a unit. To support polymorphism
// without causing allocations, it has a vtable that defines the methods for
@ -27,7 +249,7 @@ type (
parameterVtable interface {
Value(*Parameter) int
SetValue(*Parameter, int) bool
Range(*Parameter) IntRange
Range(*Parameter) RangeInclusive
Type(*Parameter) ParameterType
Name(*Parameter) string
Hint(*Parameter) ParameterHint
@ -35,9 +257,6 @@ type (
RoundToGrid(*Parameter, int, bool) int
}
Params Model
ParamVertList Model
// different parameter vtables to handle different types of parameters.
// Casting struct{} to interface does not cause allocations.
namedParameter struct{}
@ -99,9 +318,9 @@ func (p *Parameter) Add(delta int, snapToGrid bool) bool {
return p.SetValue(newVal)
}
func (p *Parameter) Range() IntRange {
func (p *Parameter) Range() RangeInclusive {
if p.vtable == nil {
return IntRange{}
return RangeInclusive{}
}
return p.vtable.Range(p)
}
@ -145,161 +364,6 @@ func (p *Parameter) UnitID() int {
return p.unit.ID
}
//
func (m *Model) ParamVertList() *ParamVertList { return (*ParamVertList)(m) }
func (pt *ParamVertList) List() List { return List{pt} }
func (pt *ParamVertList) Selected() int { return pt.d.ParamIndex }
func (pt *ParamVertList) Selected2() int { return pt.d.ParamIndex }
func (pt *ParamVertList) SetSelected(index int) { pt.d.ParamIndex = index }
func (pt *ParamVertList) SetSelected2(index int) {}
func (pt *ParamVertList) Count() int { return (*Params)(pt).Width() }
// Model and Params methods
func (m *Model) Params() *Params { return (*Params)(m) }
func (pt *Params) Table() Table { return Table{pt} }
func (pt *Params) Cursor() Point { return Point{pt.d.ParamIndex, pt.d.UnitIndex} }
func (pt *Params) Cursor2() Point { return Point{pt.d.ParamIndex, pt.d.UnitIndex2} }
func (pt *Params) SetCursor(p Point) {
pt.d.ParamIndex = max(min(p.X, pt.Width()-1), 0)
pt.d.UnitIndex = max(min(p.Y, pt.Height()-1), 0)
}
func (pt *Params) SetCursor2(p Point) {
pt.d.ParamIndex = max(min(p.X, pt.Width()-1), 0)
pt.d.UnitIndex2 = max(min(p.Y, pt.Height()-1), 0)
}
func (pt *Params) Width() int {
if pt.d.InstrIndex < 0 || pt.d.InstrIndex >= len(pt.derived.patch) {
return 0
}
// TODO: we hack the +1 so that we always have one extra cell to draw the
// comments. Refactor the gioui side so that we can specify the width and
// height regardless of the underlying table size
return pt.derived.patch[pt.d.InstrIndex].paramsWidth + 1
}
func (pt *Params) RowWidth(y int) int {
if pt.d.InstrIndex < 0 || pt.d.InstrIndex >= len(pt.derived.patch) || y < 0 || y >= len(pt.derived.patch[pt.d.InstrIndex].params) {
return 0
}
return len(pt.derived.patch[pt.d.InstrIndex].params[y])
}
func (pt *Params) Height() int { return (*Model)(pt).Units().Count() }
func (pt *Params) MoveCursor(dx, dy int) (ok bool) {
p := pt.Cursor()
p.X += dx
p.Y += dy
pt.SetCursor(p)
return p == pt.Cursor()
}
func (pt *Params) Item(p Point) Parameter {
if pt.d.InstrIndex < 0 || pt.d.InstrIndex >= len(pt.derived.patch) || p.Y < 0 || p.Y >= len(pt.derived.patch[pt.d.InstrIndex].params) || p.X < 0 || p.X >= len(pt.derived.patch[pt.d.InstrIndex].params[p.Y]) {
return Parameter{}
}
return pt.derived.patch[pt.d.InstrIndex].params[p.Y][p.X]
}
func (pt *Params) clear(p Point) {
q := pt.Item(p)
q.Reset()
}
func (pt *Params) set(p Point, value int) {
q := pt.Item(p)
q.SetValue(value)
}
func (pt *Params) add(rect Rect, delta int, largeStep bool) (ok bool) {
for y := rect.TopLeft.Y; y <= rect.BottomRight.Y; y++ {
for x := rect.TopLeft.X; x <= rect.BottomRight.X; x++ {
p := Point{x, y}
q := pt.Item(p)
if !q.Add(delta, largeStep) {
return false
}
}
}
return true
}
type paramsTable struct {
Params [][]int `yaml:",flow"`
}
func (pt *Params) marshal(rect Rect) (data []byte, ok bool) {
width := rect.BottomRight.X - rect.TopLeft.X + 1
height := rect.BottomRight.Y - rect.TopLeft.Y + 1
var table = paramsTable{Params: make([][]int, 0, width)}
for x := 0; x < width; x++ {
table.Params = append(table.Params, make([]int, 0, rect.BottomRight.Y-rect.TopLeft.Y+1))
for y := 0; y < height; y++ {
p := pt.Item(Point{x + rect.TopLeft.X, y + rect.TopLeft.Y})
table.Params[x] = append(table.Params[x], p.Value())
}
}
ret, err := yaml.Marshal(table)
if err != nil {
return nil, false
}
return ret, true
}
func (pt *Params) unmarshal(data []byte) (paramsTable, bool) {
var table paramsTable
yaml.Unmarshal(data, &table)
if len(table.Params) == 0 {
return paramsTable{}, false
}
for i := 0; i < len(table.Params); i++ {
if len(table.Params[i]) > 0 {
return table, true
}
}
return paramsTable{}, false
}
func (pt *Params) unmarshalAtCursor(data []byte) (ret bool) {
table, ok := pt.unmarshal(data)
if !ok {
return false
}
for i := 0; i < len(table.Params); i++ {
for j, q := range table.Params[i] {
x := i + pt.Cursor().X
y := j + pt.Cursor().Y
p := pt.Item(Point{x, y})
ret = p.SetValue(q) || ret
}
}
return ret
}
func (pt *Params) unmarshalRange(rect Rect, data []byte) (ret bool) {
table, ok := pt.unmarshal(data)
if !ok {
return false
}
if len(table.Params) == 0 || len(table.Params[0]) == 0 {
return false
}
width := rect.BottomRight.X - rect.TopLeft.X + 1
height := rect.BottomRight.Y - rect.TopLeft.Y + 1
if len(table.Params) < width {
return false
}
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
if len(table.Params[0]) < height {
return false
}
p := pt.Item(Point{x + rect.TopLeft.X, y + rect.TopLeft.Y})
ret = p.SetValue(table.Params[x][y]) || ret
}
}
return ret
}
func (pt *Params) change(kind string, severity ChangeSeverity) func() {
return (*Model)(pt).change(kind, PatchChange, severity)
}
func (pt *Params) cancel() {
pt.changeCancel = true
}
// namedParameter vtable
func (n *namedParameter) Value(p *Parameter) int { return p.unit.Parameters[p.up.Name] }
@ -308,8 +372,8 @@ func (n *namedParameter) SetValue(p *Parameter, value int) bool {
p.unit.Parameters[p.up.Name] = value
return true
}
func (n *namedParameter) Range(p *Parameter) IntRange {
return IntRange{Min: p.up.MinValue, Max: p.up.MaxValue}
func (n *namedParameter) Range(p *Parameter) RangeInclusive {
return RangeInclusive{Min: p.up.MinValue, Max: p.up.MaxValue}
}
func (n *namedParameter) Type(p *Parameter) ParameterType {
if p.up == nil || !p.up.CanSet {
@ -353,6 +417,25 @@ func (n *namedParameter) Reset(p *Parameter) {
p.unit.Parameters[p.up.Name] = v
}
// GmDlsEntry is a single sample entry from the gm.dls file
type GmDlsEntry struct {
Start int // sample start offset in words
LoopStart int // loop start offset in words
LoopLength int // loop length in words
SuggestedTranspose int // suggested transpose in semitones, so that all samples play at same pitch
Name string // sample Name
}
// gmDlsEntryMap is a reverse map, to find the index of the GmDlsEntry in the
var gmDlsEntryMap = make(map[vm.SampleOffset]int)
func init() {
for i, e := range GmDlsEntries {
key := vm.SampleOffset{Start: uint32(e.Start), LoopStart: uint16(e.LoopStart), LoopLength: uint16(e.LoopLength)}
gmDlsEntryMap[key] = i
}
}
// gmDlsEntryParameter vtable
func (g *gmDlsEntryParameter) Value(p *Parameter) int {
@ -378,8 +461,8 @@ func (g *gmDlsEntryParameter) SetValue(p *Parameter, v int) bool {
p.unit.Parameters["transpose"] = 64 + e.SuggestedTranspose
return true
}
func (g *gmDlsEntryParameter) Range(p *Parameter) IntRange {
return IntRange{Min: 0, Max: len(GmDlsEntries)}
func (g *gmDlsEntryParameter) Range(p *Parameter) RangeInclusive {
return RangeInclusive{Min: 0, Max: len(GmDlsEntries)}
}
func (g *gmDlsEntryParameter) Type(p *Parameter) ParameterType {
return IntegerParameter
@ -429,11 +512,11 @@ func (d *delayTimeParameter) SetValue(p *Parameter, v int) bool {
p.unit.VarArgs[p.index] = v
return true
}
func (d *delayTimeParameter) Range(p *Parameter) IntRange {
func (d *delayTimeParameter) Range(p *Parameter) RangeInclusive {
if p.unit.Parameters["notetracking"] == 2 {
return IntRange{Min: 1, Max: 576}
return RangeInclusive{Min: 1, Max: 576}
}
return IntRange{Min: 1, Max: 65535}
return RangeInclusive{Min: 1, Max: 65535}
}
func (d *delayTimeParameter) Hint(p *Parameter) ParameterHint {
val := d.Value(p)
@ -511,7 +594,9 @@ func (d *delayLinesParameter) SetValue(p *Parameter, v int) bool {
p.unit.VarArgs = p.unit.VarArgs[:targetLines]
return true
}
func (d *delayLinesParameter) Range(p *Parameter) IntRange { return IntRange{Min: 1, Max: 32} }
func (d *delayLinesParameter) Range(p *Parameter) RangeInclusive {
return RangeInclusive{Min: 1, Max: 32}
}
func (d *delayLinesParameter) Type(p *Parameter) ParameterType { return IntegerParameter }
func (d *delayLinesParameter) Name(p *Parameter) string { return "delaylines" }
func (r *delayLinesParameter) RoundToGrid(p *Parameter, val int, up bool) int { return val }
@ -525,6 +610,20 @@ func (d *delayLinesParameter) Reset(p *Parameter) {}
// reverbParameter vtable
type delayPreset struct {
name string
stereo int
varArgs []int
}
var reverbs = []delayPreset{
{"stereo", 1, []int{1116, 1188, 1276, 1356, 1422, 1492, 1556, 1618,
1140, 1212, 1300, 1380, 1446, 1516, 1580, 1642,
}},
{"left", 0, []int{1116, 1188, 1276, 1356, 1422, 1492, 1556, 1618}},
{"right", 0, []int{1140, 1212, 1300, 1380, 1446, 1516, 1580, 1642}},
}
func (r *reverbParameter) Value(p *Parameter) int {
i := slices.IndexFunc(reverbs, func(d delayPreset) bool {
return d.stereo == p.unit.Parameters["stereo"] && p.unit.Parameters["notetracking"] == 0 && slices.Equal(d.varArgs, p.unit.VarArgs)
@ -543,7 +642,9 @@ func (r *reverbParameter) SetValue(p *Parameter, v int) bool {
copy(p.unit.VarArgs, entry.varArgs)
return true
}
func (r *reverbParameter) Range(p *Parameter) IntRange { return IntRange{Min: 0, Max: len(reverbs)} }
func (r *reverbParameter) Range(p *Parameter) RangeInclusive {
return RangeInclusive{Min: 0, Max: len(reverbs)}
}
func (r *reverbParameter) Type(p *Parameter) ParameterType { return IntegerParameter }
func (r *reverbParameter) Name(p *Parameter) string { return "reverb" }
func (r *reverbParameter) RoundToGrid(p *Parameter, val int, up bool) int { return val }