From 5c51932f6019a7b0b779008dc7ff7cf2ab10cd7e Mon Sep 17 00:00:00 2001 From: "5684185+vsariola@users.noreply.github.com" <5684185+vsariola@users.noreply.github.com> Date: Sun, 22 Sep 2024 10:20:52 +0300 Subject: [PATCH] fix(tracker): autofix malformed songs with useless params --- CHANGELOG.md | 2 ++ tracker/model.go | 31 +++++++++++++++++++++++++++++++ tracker/model_test.go | 3 +++ 3 files changed, 36 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c05f842..6bc7e94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/tracker/model.go b/tracker/model.go index 128718e..7a1e617 100644 --- a/tracker/model.go +++ b/tracker/model.go @@ -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 @@ -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 { diff --git a/tracker/model_test.go b/tracker/model_test.go index 58810d7..71a3993 100644 --- a/tracker/model_test.go +++ b/tracker/model_test.go @@ -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{}{}