feat: introduce "cache" for derived model information

This commit is contained in:
qm210
2024-11-09 02:19:52 +01:00
parent c266db17d9
commit 6f1cb5e7ea
7 changed files with 271 additions and 155 deletions

30
song.go
View File

@ -2,7 +2,6 @@ package sointu
import (
"errors"
"iter"
)
type (
@ -336,32 +335,3 @@ func TotalVoices[T any, S ~[]T, P NumVoicerPointer[T]](slice S) (ret int) {
}
return
}
func (s *Song) InstrumentForTrack(trackIndex int) (int, bool) {
voiceIndex := s.Score.FirstVoiceForTrack(trackIndex)
instrument, err := s.Patch.InstrumentForVoice(voiceIndex)
return instrument, err == nil
}
func (s *Song) AllTracksWithSameInstrument(trackIndex int) iter.Seq[int] {
return func(yield func(int) bool) {
currentInstrument, currentExists := s.InstrumentForTrack(trackIndex)
if !currentExists {
return
}
for i := 0; i < len(s.Score.Tracks); i++ {
instrument, exists := s.InstrumentForTrack(i)
if !exists {
return
}
if instrument != currentInstrument {
continue
}
if !yield(i) {
return
}
}
}
}