You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
From what I understand, when you initialize a Key, all the chords are generated inside the init.
However, if you want to create all the available keys for a certain note, the performance is pretty bad.
Here is a piece of code that illustrates this concept:
This code creates an array of keys for all the scales of a given note.
Proposed Solution
I would suggest implementing lazy chord creation. This would allow users to request the chords for a specific key only when needed, instead of generating them automatically during initialization. I believe that generating chords during initialization is unnecessarily complex and pollutes the domain of the Key Object, in addition, it also impacts the performance.
/// All the traditional triads representable root, third, and fifth from each note in the keypublicfunc primaryTriads()->[Chord]{lettable=ChordTable.shared
varprimaryTriads:[Chord]=[]letallowablePrimaryTriads:[ChordType]=[.majorTriad,.minorTriad,.diminishedTriad,.augmentedTriad]
for (_, chord) in table.chords where chord.noteClassSet.isSubset(of: noteSet.noteClassSet){
if allowablePrimaryTriads.contains(chord.type){
primaryTriads.append(Chord(chord.root, type: chord.type))}}letprimaryTriadsStartingWithC= primaryTriads.sorted(by:{ $0.root.letter < $1.root.letter })letrootPosition= primaryTriadsStartingWithC.firstIndex(where:{ $0.root == root })??0returnArray(primaryTriadsStartingWithC.rotatingLeft(positions: rootPosition))}/// All chords that fit in the keypublicfunc chords()->[Chord]{lettable=ChordTable.shared
varchords:[Chord]=[]
for (_, chord) in table.chords where chord.noteClassSet.isSubset(of: noteSet.noteClassSet){
chords.append(Chord(chord.root, type: chord.type))}return chords
}
We could also think about adding a static global cache, similar to Chordtable, that stores a key and all the corresponding chords globally for faster access.
Wdyt?
The text was updated successfully, but these errors were encountered:
Description
From what I understand, when you initialize a
Key
, all the chords are generated inside the init.However, if you want to create all the available keys for a certain note, the performance is pretty bad.
Here is a piece of code that illustrates this concept:
This code creates an array of keys for all the scales of a given note.
Proposed Solution
I would suggest implementing lazy chord creation. This would allow users to request the chords for a specific key only when needed, instead of generating them automatically during initialization. I believe that generating chords during initialization is unnecessarily complex and pollutes the domain of the
Key
Object, in addition, it also impacts the performance.We could also think about adding a static global cache, similar to
Chordtable
, that stores a key and all the corresponding chords globally for faster access.Wdyt?
The text was updated successfully, but these errors were encountered: