Skip to content

Commit

Permalink
fix: assign new IDs to loaded instruments
Browse files Browse the repository at this point in the history
Fixes #146.
  • Loading branch information
vsariola committed Sep 6, 2024
1 parent 5699585 commit d46605c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 change: 1 addition & 0 deletions tracker/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 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(
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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d46605c

Please sign in to comment.