fix(tracker): autofix malformed songs with useless params

This commit is contained in:
2024-10-11 20:34:04 +03:00
parent 773655ef9c
commit 5c51932f60
3 changed files with 36 additions and 0 deletions
+31
View File
@@ -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 {