public void NoteOn(int channel, int note, int velocity, int program)
{
// Grab a free voice
Voice freeVoice = getFreeVoice();
if (freeVoice == null)
{
// If there are no free voices steal an active one.
freeVoice = getUsedVoice(activeVoices.First.Value.getKey());
// If there are no voices to steal then leave this method.
if (freeVoice == null)
{
return;
}
}
// Create a key for this event
NoteRegistryKey r = new NoteRegistryKey((byte)channel, (byte)note);
// Get the correct instrument depending if it is a drum or not
if (channel == 9)
{
freeVoice.setInstrument(bank.getInstrument(program, true));
}
else
{
freeVoice.setInstrument(bank.getInstrument(program, false));
}
// Check if key exists
if (keyRegistry.ContainsKey(r))
{
if (keyRegistry[r].Count >= maxnotepoly)
{
keyRegistry[r][0].Stop();
keyRegistry[r].RemoveAt(0);
}
keyRegistry[r].Add(freeVoice);
}
else//The first noteOn of it's own type will create a list for multiple occurences
{
List <Voice> Vlist = new List <Voice>(maxnotepoly);
Vlist.Add(freeVoice);
keyRegistry.Add(r, Vlist);
}
freeVoice.Start(channel, note, velocity);
activeVoices.AddLast(freeVoice);
}