//private int GetChan(int chan) {
        //* filter out if not omni and channel does not match
        //  if (MidiPlay.KBInChan >= 0 && MidiPlay.KBInChan != chan) return -1;
        ////* check if channel needs changing
        //  if (MidiPlay.KBOutChan >= 0) return MidiPlay.KBOutChan; else return chan;
        //}

        private void CallbackKeyboard(byte[] b) //called for all messages from keyboard port
        //if ((b[0] & 0xf0) == 0xd0) Debug.WriteLine("Aftertouch: " + b[1]);  //channel pressure (aftertouch)
        {
            if (b[1] > 127 || b[2] > 127)
            {
                //LogicError.Throw(eStopError.Y002);
                LogicError.Throw(eLogicError.X102, "bytes = " + b[0] + b[1] + b[2]);
                //* try correcting the error!
                if (b[2] < 128 && (b[0] & 0xf0) == 0x90 && (b[1] & 0xf0) == 0x90) //ON status sent twice (may be vel 0)
                {
                    b[1] = b[2];                                                  //probably pitch
                    List <clsPlay.clsKBPitch> listkb = clsPlay.KBPitchList.GetKBPitches(b[1]);
                    if (listkb.Count > 0)
                    {
                        b[2] = 0; //assume OFF event was meant
                    }
                    else
                    {
                        b[2] = 80; //assume ON event
                    }
                }
                return;
            }

            //Ticks = clsPlay.GetTicks();
            if (SustainPedal(b))
            {
                return;
            }
            ApplyBezier(b);
            //if (P.F != null && P.frmSC != null && P.F.MidiFileLoaded) {

            bool?on = null;

            if ((b[0] & 0xf0) == 0x90 && b[2] > 0)
            {
                on = true;
            }
            else if (((b[0] & 0xf0) == 0x80) || ((b[0] & 0xf0) == 0x90) && b[2] == 0)
            {
                on = false;
            }

            //if (on.HasValue && CheckDoubleHit(b)) {
            //  Debug.WriteLine("DoubleHit detected at " + DateTime.Now);
            //  return;
            //}

            if (P.frmSCAlign != null && on.HasValue && on.Value) //ON
            {
                dSCAlign = new delegSCAlign(SCAlign);
                P.frmSCAlign.BeginInvoke(dSCAlign, b[1]);
            }

            //if (P.frmSC.Play != null || clsPlay.ManChordsActive) {
            if (P.frmSC.Play != null)
            {
                if (!CheckSwitch(b))
                {
                    P.frmSC.Play.InMidi(b);
                }
            }
            else //passthru' all (default)
            //MidiPlay.OutMKB.SendShortMsg(b[0], b[1], b[2]);
            {
                if (on.HasValue) //ON or OFF
                //if (P.F.AutoSync != null && CheckSwitch(b)) return;
                {
                    if (!CheckSwitch(b))
                    {
                        int pitch = b[1] + clsPlay.TransposeKBPitch;
                        lock (clsPlay.KBPitchList) {
                            if (on.Value) //ON
                            {
                                MidiPlay.OutMRec.SendShortMsg(b[0], pitch, b[2]);
                                clsPlay.KBPitchList.Add(pitch, pitch); //ON
                                P.frmSC.BeginInvoke(new delegRefresh(P.frmSC.picBottom.Refresh));
                                P.frmSC.BeginInvoke(new delegRefresh(P.frmSC.picChords.Refresh));
                            }
                            else
                            {
                                clsPlay.KBPitchList_MidiOff(new byte[] { b[0], (byte)pitch, b[2] }); //OFF
                            }
                        }
                    }
                }
                else
                {
                    MidiPlay.OutMRec.SendShortMsg(b[0], b[1], b[2]);
                }
            }

            //* set kb range using untransposed kb notes
            if (P.frmSCOctaves != null && //kb ranges
                on.HasValue && on.Value) //ON ev
            {
                dSetNoteName = new delegSetNoteName(P.frmSCOctaves.SetNoteName);
                P.frmSCOctaves.BeginInvoke(dSetNoteName, b[1]);
            }
        }
Exemple #2
0
        //internal static void ExecSwitch(int keyswitch, bool down) {
        //  //* same as clsMidiInKB.ExecSwitch, but without BeginInvoke...
        //  foreach (string action in Forms.frmSwitch.KeyToActions[keyswitch]) {
        //    if (action != "") {
        //      Forms.frmSwitch.Delegs[action](down);
        //      if (action == "Sustain") {
        //        clsMidiInKB.SetChkSwitch(P.frmSC.chkSwitchSustain, down);
        //      } else if (action == "KB Chord") {
        //        clsMidiInKB.SetChkSwitch(P.frmSC.chkSwitchKBChord, down);
        //      }
        //    }
        //  }
        //}

        internal void KeyUpDown(Keys keyData, bool keydown)
        {
            //MidiPlay.MidiInKB.Ticks = clsPlay.GetTicks();

            if (keyData.HasFlag(Keys.Shift))
            {
                keyData = (keyData & ~Keys.Shift);
            }
            if (keyData.HasFlag(Keys.Alt))
            {
                keyData = (keyData & ~Keys.Alt);
            }
            if (keyData.HasFlag(Keys.Control))
            {
                keyData = (keyData & ~Keys.Control);
            }

            //* check switchkey
            if (P.frmSC.Play != null)
            {
                if (keyData == Keys.Space)
                {
                    Forms.frmSwitch.ExecSwitch(12, keydown); //pedal
                    return;
                }
                else if (keyData >= Keys.F1 && keyData <= Keys.F12) //C-B
                {
                    Forms.frmSwitch.ExecSwitch(keyData - Keys.F1, keydown);
                    return;
                }
            }

            //* play midi pitch
            byte[] b = GetMidiIn(keyData, keydown);
            //Debug.WriteLine("clsPCKB: KeyUpDown: keydown = " + keydown + " b.Length = " + b.Length);
            if (b.Length != 3)
            {
                return;           //len 0: inactive or repeated key
            }
            if (P.frmSC.Play != null)
            {
                P.frmSC.Play.InMidi(b);
            }
            else
            {
                MidiPlay.OutMRec.SendShortMsg(b[0], b[1], b[2]);
                lock (clsPlay.KBPitchList) {
                    if (keydown)
                    {
                        clsPlay.KBPitchList.Add(b[1], b[1]); //ON
                        P.frmSC.BeginInvoke(new delegRefresh(P.frmSC.picBottom.Refresh));
                        P.frmSC.BeginInvoke(new delegRefresh(P.frmSC.picChords.Refresh));
                    }
                    else
                    {
                        clsPlay.KBPitchList_MidiOff(b); //OFF
                    }
                }
            }

            ////* activate frmPCKBIn (for next kb input)
            //if (P.frmPCKBIn != null && frm != null && frm != P.frmPCKBIn) {
            //  P.frmPCKBIn.Invoke(dAct, P.frmPCKBIn);
            //}

            //* set kb range using untransposed kb notes
            if (P.frmSCOctaves != null) //kb ranges
            {
                dSetNoteName = new delegSetNoteName(P.frmSCOctaves.SetNoteName);
                P.frmSCOctaves.BeginInvoke(dSetNoteName, b[1]);
            }
        }