Exemple #1
0
 public override bool Equals(object obj)
 {
     if (obj is NoteRegistryKey)
     {
         NoteRegistryKey r = (NoteRegistryKey)obj;
         return(r.channel == this.channel && r.note == this.note);
     }
     return(false);
 }
        public void NoteOff(int channel, int note)
        {
            NoteRegistryKey r = new NoteRegistryKey((byte)channel, (byte)note);
            List <Voice>    voice;

            if (keyRegistry.TryGetValue(r, out voice))
            {
                if (voice.Count > 0)
                {
                    voice[0].Stop();
                    voice.RemoveAt(0);
                }
            }
        }
        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 occurrences
            {
                List <Voice> Vlist = new List <Voice>(maxnotepoly);
                Vlist.Add(freeVoice);
                keyRegistry.Add(r, Vlist);
            }
            freeVoice.Start(channel, note, velocity);
            activeVoices.AddLast(freeVoice);
        }
        private Voice getUsedVoice(NoteRegistryKey r)
        {
            List <Voice> voicelist;
            Voice        voice;

            if (keyRegistry.TryGetValue(r, out voicelist))
            {
                if (voicelist.Count > 0)
                {
                    voicelist[0].StopImmediately();
                    voice = voicelist[0];
                    voicelist.RemoveAt(0);
                    activeVoices.Remove(voice);
                    return(voice);
                }
            }
            return(null);
        }
Exemple #5
0
 public bool Equals(NoteRegistryKey obj)
 {
     return(obj.channel == this.channel && obj.note == this.note);
 }