Exemplo n.º 1
0
        public void ControllerEventHandler(XInput.GamepadEx state)
        {
            var newState = ProcessState(state);

            if (lastState == null)
            {
                lastState = newState;
                return;
            }
            if (state.wButtons.HasFlag(XInput.Buttons.A))
            {
                OnControlChange?.Invoke(Channel.Channel1, Control.AllNotesOff, 0);
            }
            for (int i = 0; i < 6; i++)
            {
                if (lastState.Velocities[i] != newState.Velocities[i])
                {
                    newPitches[i] = (byte)(newState.Frets[i] + StandardTuning[i]);
                    OnNoteOff?.Invoke(Channel.Channel1, (Pitch)lastPitches[i]);
                    OnNoteOn?.Invoke(Channel.Channel1, (Pitch)newPitches[i], newState.Velocities[i]);
                }
            }
            // swap note buffers
            var temp = lastPitches;

            lastPitches = newPitches;
            newPitches  = temp;
            // swap guitar state
            lastState = newState;
        }
Exemplo n.º 2
0
        public void ControllerEventHandler(XInput.GamepadEx state)
        {
            var drums = HitDrums(state.wButtons);

            SendMessages(drums, state);
            drumState = drums;
        }
Exemplo n.º 3
0
        private void PollThread()
        {
            var sleepTime = TimeSpan.FromMilliseconds(0.5);

            while (!cancel)
            {
                if (!Connected)
                {
                    // Do less while waiting for the controller to come back online.
                    Thread.Sleep(250);
                    continue;
                }
                var state = new XInput.StateEx();
                if (0 != XInput.XInputGetStateEx(controllerIndex, ref state))
                {
                    Connected = false;
                    OnDisconnect?.ThreadSafeInvoke();
                    continue;
                }
                var gp = state.Gamepad;
                if (gp.bLeftTrigger != lastState.bLeftTrigger ||
                    gp.bRightTrigger != lastState.bRightTrigger ||
                    gp.sThumbLX != lastState.sThumbLX ||
                    gp.sThumbLY != lastState.sThumbLY ||
                    gp.sThumbRX != lastState.sThumbRX ||
                    gp.sThumbRY != lastState.sThumbRY ||
                    gp.wButtons != lastState.wButtons)
                {
                    lastState = gp;
                    OnStateChanged.ThreadSafeInvoke(lastState);
                }
                Thread.Sleep(sleepTime);
            }
        }
Exemplo n.º 4
0
 public void ControllerEventHandler(XInput.GamepadEx state)
 {
     var(keys, sustain, velocities) = GamepadToKeyState(state);
     if (state.wButtons.HasFlag(XInput.Buttons.DPAD_LEFT))
     {
         offset = Math.Max(offset - 12, 0);
         OnBasePitchChanged.ThreadSafeInvoke((Midi.Enums.Pitch)offset);
     }
     else if (state.wButtons.HasFlag(XInput.Buttons.DPAD_RIGHT))
     {
         offset = Math.Min(offset + 12, 127);
         OnBasePitchChanged.ThreadSafeInvoke((Midi.Enums.Pitch)offset);
     }
     SendMessages(keys, sustain, velocities);
     sustainState = sustain;
 }
Exemplo n.º 5
0
        public ControllerMonitor(Controller c)
        {
            var state = new XInput.StateEx();

            if (0 != XInput.XInputGetStateEx(c.Index, ref state))
            {
                throw new Exception($"Controller {c.Index} is not connected");
            }
            lastState       = state.Gamepad;
            cancel          = false;
            controllerIndex = c.Index;
            Connected       = true;
            thread          = new Thread(PollThread)
            {
                Name = $"Controller {c.Index} poll thread"
            };
            thread.Start();
        }
Exemplo n.º 6
0
 void SendMessages(Drums newState, XInput.GamepadEx state)
 {
     foreach (Drums d in Enum.GetValues(typeof(Drums)))
     {
         var note = DrumNoteMap[d](newState);
         if (newState.HasFlag(d) && !drumState.HasFlag(d))
         {
             var velocity = VelocityFunctions[d](state);
             if (velocity > 127)
             {
                 velocity = 127;
             }
             if (velocity < 32)
             {
                 velocity = 32;
             }
             OnNoteOn?.Invoke(Channel, (Midi.Enums.Pitch)note, velocity);
         }
         else if (!newState.HasFlag(d) && drumState.HasFlag(d))
         {
             OnNoteOff?.Invoke(Channel, (Midi.Enums.Pitch)note);
         }
     }
 }
Exemplo n.º 7
0
        public static (bool[] keys, bool sustain, byte[] velocities) GamepadToKeyState(XInput.GamepadEx state)
        {
            bool sustain    = (state.sThumbRY & 0x80) == 0x80;
            var  velocities = new byte[] {
                (byte)(state.sThumbLX >> 8 & 0x7F),
                (byte)(state.sThumbLY & 0x7F),
                (byte)(state.sThumbLY >> 8 & 0x7F),
                (byte)(state.sThumbRX & 0x7F),
                (byte)(state.sThumbRX >> 8 & 0x7F),
            };

            var keys     = new bool[25];
            int appended =
                (state.bLeftTrigger << 17) |
                (state.bRightTrigger << 9) |
                ((state.sThumbLX & 0xFF) << 1) |
                ((state.sThumbLX >> 15) & 0x1);
            int test = 1 << 24;
            int key  = 0;

            for (var i = 0; i < 25; i++)
            {
                int mask = test >> i;
                keys[key++] = (appended & mask) == mask;
            }
#if DEBUG
            Console.WriteLine(
                $"Keys: {Convert.ToString(appended, 2).PadLeft(25, '0')} " +
                $"Sustain: {(sustain ? 1 : 0)} " +
                $"Velocities: {velocities.Select(x => x.ToString().PadLeft(3)).Aggregate((x, y) => x + " " + y)} " +
                $"State: {(int)state.wButtons:X4}");
#endif
            return(keys, sustain, velocities);
        }