fix: assign new IDs to loaded instruments

Fixes #146.
This commit is contained in:
2024-09-06 20:19:27 +03:00
parent 569958547e
commit d46605c638
3 changed files with 36 additions and 0 deletions
+3
View File
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## Unreleased
### Fixed
- Assign new IDs to loaded instruments, and fix ID collisions in case they
somehow still appear ([#146][i146])
- In x86 templates, do not optimize away phase modulations when unisons are used
even if all phase inputs are zeros, as unisons use the phase modulation
mechanism to offset the different oscillators
@@ -176,3 +178,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
[i129]: https://github.com/vsariola/sointu/issues/129
[i130]: https://github.com/vsariola/sointu/issues/130
[i139]: https://github.com/vsariola/sointu/issues/139
[i146]: https://github.com/vsariola/sointu/issues/146
+1
View File
@@ -171,6 +171,7 @@ success:
for len(m.d.Song.Patch) <= m.d.InstrIndex {
m.d.Song.Patch = append(m.d.Song.Patch, defaultInstrument.Copy())
}
m.assignUnitIDs(instrument.Units)
m.d.Song.Patch[m.d.InstrIndex] = instrument
if m.d.Song.Patch[m.d.InstrIndex].Comment != "" {
m.commentExpanded = true
+32
View File
@@ -225,6 +225,7 @@ func (m *Model) change(kind string, t ChangeType, severity ChangeSeverity) func(
m.send(m.d.Song.Score.Copy())
}
if m.changeType&PatchChange != 0 {
m.fixIDCollisions()
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
@@ -441,6 +442,37 @@ func (m *Model) assignUnitIDs(units []sointu.Unit) {
}
}
func (m *Model) fixIDCollisions() {
// loop over all instruments and units and check if two units have the same
// ID. If so, give the later units new IDs.
usedIDs := map[int]bool{}
needsFix := false
maxID := 0
for i, instr := range m.d.Song.Patch {
for j, unit := range instr.Units {
if usedIDs[unit.ID] {
m.d.Song.Patch[i].Units[j].ID = 0
needsFix = true
}
if unit.ID > maxID {
maxID = unit.ID
}
usedIDs[unit.ID] = true
}
}
if needsFix {
m.Alerts().Add("Some units had duplicate IDs, they were fixed", Error)
for i, instr := range m.d.Song.Patch {
for j, unit := range instr.Units {
if unit.ID == 0 {
maxID++
m.d.Song.Patch[i].Units[j].ID = maxID
}
}
}
}
}
func (m *Model) updatePatternUseCount() {
for i, track := range m.d.Song.Score.Tracks {
for len(m.cachePatternUseCount) <= i {