Skip to content

Commit

Permalink
fix(tracker): autofix malformed songs with useless params
Browse files Browse the repository at this point in the history
  • Loading branch information
vsariola committed Oct 11, 2024
1 parent 773655e commit 5c51932
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
the command line tools.

### Fixed
- If units have useless parameters in their parameter maps, from bugs or from a
malformed yaml file, they are removed and user is warned about it
- Pressing a or 1 when editing note values in hex mode created a note off line
([#162][i162])
- Warn about plugin sample rate being different from 44100 only after
Expand Down
31 changes: 31 additions & 0 deletions tracker/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func (m *Model) change(kind string, t ChangeType, severity ChangeSeverity) func(
}
if m.changeType&PatchChange != 0 {
m.fixIDCollisions()
m.fixUnitParams()
m.d.InstrIndex = clamp(m.d.InstrIndex, 0, len(m.d.Song.Patch)-1)
m.d.InstrIndex2 = clamp(m.d.InstrIndex2, 0, len(m.d.Song.Patch)-1)
unitCount := 0
Expand Down Expand Up @@ -481,6 +482,36 @@ func (m *Model) fixIDCollisions() {
}
}

var validParameters = map[string](map[string]bool){}

func init() {
for name, unitType := range sointu.UnitTypes {
validParameters[name] = map[string]bool{}
for _, param := range unitType {
validParameters[name][param.Name] = true
}
}
}

func (m *Model) fixUnitParams() {
// loop over all instruments and units and check that unit parameter table
// only has the parameters that are defined in the unit type
fixed := false
for i, instr := range m.d.Song.Patch {
for j, unit := range instr.Units {
for paramName := range unit.Parameters {
if !validParameters[unit.Type][paramName] {
delete(m.d.Song.Patch[i].Units[j].Parameters, paramName)
fixed = true
}
}
}
}
if fixed {
m.Alerts().AddNamed("InvalidUnitParameters", "Some units had invalid parameters, they were removed", Error)
}
}

func (m *Model) updatePatternUseCount() {
for i, track := range m.d.Song.Score.Tracks {
for len(m.cachePatternUseCount) <= i {
Expand Down
3 changes: 3 additions & 0 deletions tracker/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ func FuzzModel(f *testing.F) {
if a.Name == "IDCollision" {
t.Errorf("Path: %s Model has ID collisions", totalPath)
}
if a.Name == "InvalidUnitParameters" {
t.Errorf("Path: %s Model units with invalid parameters", totalPath)
}
})
}
closeChan <- struct{}{}
Expand Down

0 comments on commit 5c51932

Please sign in to comment.