requestNew method
Implementation
Voice? requestNew(InstrumentRegion region, int channel) {
// If the voice is an exclusive class, reuse the existing one so that only one note
// will be played at a time.
int exclusiveClass = region.exclusiveClass();
if (exclusiveClass != 0) {
for (var i = 0; i < _activeVoiceCount; i++) {
var voice = voices[i];
if (voice.exclusiveClass() == exclusiveClass && voice.channel() == channel) {
return voice;
}
}
}
if (_activeVoiceCount < voices.length) {
var free = voices[_activeVoiceCount];
_activeVoiceCount++;
return free;
}
// Too many active voices...
// Find one which has the lowest priority.
Voice? low = null;
var lowestPriority = double.maxFinite;
for (int i = 0; i < _activeVoiceCount; i++) {
var voice = voices[i];
var priority = voice.priority();
if (priority < lowestPriority) {
lowestPriority = priority;
low = voice;
} else if (priority == lowestPriority) {
// Same priority...
// The older one should be more suitable for reuse.
if (low == null || voice.voiceLength() > low.voiceLength()) {
low = voice;
}
}
}
return low;
}