void SendMessages(Drums newState, XInput.GamepadEx state) { if (device == null) { return; } var channel = Midi.Enums.Channel.Channel10; foreach (Drums d in Enum.GetValues(typeof(Drums))) { var note = DrumNoteMap[d]; if (newState.HasFlag(d) && !drumState.HasFlag(d)) { var velocity = VelocityFunctions[d](state); if (velocity > 127) { velocity = 127; } if (velocity < 32) { velocity = 32; } device.SendNoteOn(channel, (Midi.Enums.Pitch)note, velocity); } else if (!newState.HasFlag(d) && drumState.HasFlag(d)) { device.SendNoteOff(channel, (Midi.Enums.Pitch)note, 0); } } }
private void Mon_OnStateChanged(XInput.GamepadEx state) { var drums = HitDrums(state.wButtons); SendMessages(drums, state); drumState = drums; }
private void Mon_OnStateChanged(XInput.GamepadEx state) { var(keys, sustain, velocities) = GamepadToKeyState(state); if (state.wButtons.HasFlag(XInput.Buttons.DPAD_LEFT)) { offset = Math.Max(offset - 12, 0); baseNoteLabel.Text = Enum.GetName(typeof(Midi.Enums.Pitch), (Midi.Enums.Pitch)offset); } else if (state.wButtons.HasFlag(XInput.Buttons.DPAD_RIGHT)) { offset = Math.Min(offset + 12, 127); baseNoteLabel.Text = Enum.GetName(typeof(Midi.Enums.Pitch), (Midi.Enums.Pitch)offset); } SendMessages(keys, sustain, velocities); sustainState = sustain; }
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); }