fix(tracker): ensure numVoices of loaded instrument is ok

This commit is contained in:
5684185+vsariola@users.noreply.github.com 2024-09-07 15:23:57 +03:00
parent 74972b5ff4
commit 1daaf1829c
2 changed files with 14 additions and 0 deletions

View File

@ -9,6 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
resized ([#145][i145])
### Fixed
- When loading an instrument, make sure the total number of voices does not go
over the maximum number allowed by vm, and make sure a loaded instrument has
at least 1 voice
- Potential ID collisions when clearing unit or pasteing instruments
- Assign new IDs to loaded instruments, and fix ID collisions in case they
somehow still appear ([#146][i146])

View File

@ -12,6 +12,7 @@ import (
"gopkg.in/yaml.v3"
"github.com/vsariola/sointu"
"github.com/vsariola/sointu/vm"
)
func (m *Model) ReadSong(r io.ReadCloser) {
@ -171,6 +172,16 @@ success:
for len(m.d.Song.Patch) <= m.d.InstrIndex {
m.d.Song.Patch = append(m.d.Song.Patch, defaultInstrument.Copy())
}
m.d.Song.Patch[m.d.InstrIndex] = sointu.Instrument{}
numVoices := m.d.Song.Patch.NumVoices()
if numVoices >= vm.MAX_VOICES {
// this really shouldn't happen, as we have already cleared the
// instrument and assuming each instrument has at least 1 voice, it
// should have freed up some voices
m.Alerts().Add(fmt.Sprintf("The patch has already %d voices", vm.MAX_VOICES), Error)
return false
}
instrument.NumVoices = clamp(instrument.NumVoices, 1, 32-numVoices)
m.assignUnitIDs(instrument.Units)
m.d.Song.Patch[m.d.InstrIndex] = instrument
if m.d.Song.Patch[m.d.InstrIndex].Comment != "" {