requestNew method

Voice? requestNew(
  1. InstrumentRegion region,
  2. int channel
)

Implementation

Voice? requestNew(InstrumentRegion region, int channel)
{
    Voice? free;
    Voice? low;

    double lowestPriority = double.maxFinite;

    int exclusiveClass = region.exclusiveClass();

    if (exclusiveClass == 0)
    {
        for (var i = 0; i < _activeVoiceCount; i++)
        {
            var voice = voices[i];

            if (voice.priority() < lowestPriority)
            {
                lowestPriority = voice.priority();
                low = voice;
            }
        }
    }
    else
    {
        for (var i = 0; i < _activeVoiceCount; i++)
        {
            var voice = voices[i];

            if (voice.exclusiveClass() == exclusiveClass && voice.channel() == channel)
            {
                voice.kill();
                free = voice;
            }
            if (voice.priority() < lowestPriority)
            {
                lowestPriority = voice.priority();
                low = voice;
            }
        }
    }

    if (free != null)
    {
        return free;
    }

    if (_activeVoiceCount < voices.length)
    {
        free = voices[_activeVoiceCount];
        _activeVoiceCount++;
        return free;
    }
    else
    {
        return low;
    }
}