Skip to content

Commit

Permalink
MRU voice stealing mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilie Gillet committed Jan 23, 2019
1 parent 09ca850 commit 97fe4a7
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions algorithms/voice_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ enum VoiceAllocatorFlags {
ACTIVE_NOTE = 0x80
};

enum VoiceStealingMode {
VOICE_STEALING_MODE_LRU,
VOICE_STEALING_MODE_MRU
};

template<uint8_t capacity>
class VoiceAllocator {
public:
Expand All @@ -48,8 +53,12 @@ class VoiceAllocator {
size_ = 0;
Clear();
}

uint8_t NoteOn(uint8_t note) {
return NoteOn(note, VOICE_STEALING_MODE_LRU);
}

uint8_t NoteOn(uint8_t note, VoiceStealingMode voice_stealing_mode) {
if (size_ == 0) {
return NOT_ALLOCATED;
}
Expand All @@ -68,12 +77,15 @@ class VoiceAllocator {
}
}
}
// If all voices are active, use the least recently played note
// If all voices are active, use the least or most recently played note
// (voice-stealing).
if (voice == NOT_ALLOCATED) {
for (uint8_t i = 0; i < capacity; ++i) {
if (lru_[i] < size_) {
voice = lru_[i];
uint8_t candidate = voice_stealing_mode == VOICE_STEALING_MODE_LRU
? i
: capacity - 1 - i;
if (lru_[candidate] < size_) {
voice = lru_[candidate];
}
}
}
Expand Down

0 comments on commit 97fe4a7

Please sign in to comment.