/// <summary> /// Constructor. /// </summary> public MidiController() { // Get an instance to the event handler and subscribe to the SequencerPositionChanged event. this.globalEventHandlerInstance = GlobalEventHandler.GetInstance(); // Selected MIDI device. deviceSelectorString = MidiInPort.GetDeviceSelector(); // Activate device watcher and add callbacks for state changes. deviceWatcher = DeviceInformation.CreateWatcher(deviceSelectorString); deviceWatcher.Added += DeviceWatcher_Added; deviceWatcher.Removed += DeviceWatcher_Removed; deviceWatcher.Updated += DeviceWatcher_Updated; deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted; // Subscribe to changed MIDI device. this.globalEventHandlerInstance.SelectedMidiDeviceChanged += this.SelectedMidiDeviceChanged; // Subscribe to event to learn MIDI messages. this.globalEventHandlerInstance.LearnMidiEvent += this.LearnMidiEvent; // Initialize MIDI event triggers. for (int i = 0; i < Enum.GetNames(typeof(MidiEventType)).Length; i++) { learnedMidiTriggers[i] = new MidiEventTrigger(); } }
/// <summary> /// Load the MIDI event list from the settings. /// </summary> /// <param name="sender">Button the event was received from</param> /// <param name="e">RoutedEventArgs</param> private void LoadMidiSettings(object sender, RoutedEventArgs e) { // Iterate through all MIDI event types. for (int i = 0; i < 12; i++) { // Create a new MidiEventTrigger object. MidiEventTrigger tempMidiEventTrigger = new MidiEventTrigger(); // Set the type based on the current step. tempMidiEventTrigger.type = (MidiEventType)i; // Get trigger id from the storage. tempMidiEventTrigger.id = (int)localSettings.Values[i.ToString()]; // Set the extracted data to the active MIDI triggers. midiController.learnedMidiTriggers[i] = tempMidiEventTrigger; // Find the relevant button for this type. Button currentButtonElement = (Button)this.FindName("MidiEventType" + i.ToString()); if (tempMidiEventTrigger.id != 0) { // If this is an active trigger, highlight it. currentButtonElement.Background = new SolidColorBrush(Color.FromArgb(255, 0, 71, 138)); } else { // If not, use the standard color for the button. currentButtonElement.Background = this.themeButtonColor; } } // Done, set status text. StatusTextControl.Text = "MIDI settings restored."; }
/// <summary> /// A new MIDI message was received. /// Check if the message is relevant by checking the trained MIDI events /// and if so, send a notification. /// </summary> /// <param name="sender">MIDI port that sent the message</param> /// <param name="args">MidiMessageReceivedEventArgs</param> private void MidiMessageReceived(MidiInPort sender, MidiMessageReceivedEventArgs args) { // Get the message as simple IMidiMessage. IMidiMessage rawMidiMessage = (IMidiMessage)args.Message; // Get the id and relevant value of the MIDI message. int midiMessageID = this.ExtractMidiMessageID(rawMidiMessage); int midiMessageValue = this.ExtractMidiMessageValue(rawMidiMessage); // First check if we are in MIDI learn mode. // If so, do not interpret the message, but instead use the message to associate // its type with an event type. if (this.midiLearningActive) { // Initialize new MIDI event. MidiEventTrigger learnedMidiEvent = new MidiEventTrigger(); learnedMidiEvent.type = this.midiLearningType; learnedMidiEvent.rawOriginalMessage = rawMidiMessage; // Set the ID of the message. learnedMidiEvent.id = midiMessageID; // Store identified MIDI event. this.learnedMidiTriggers[(int)this.midiLearningType] = learnedMidiEvent; this.midiLearningActive = false; // Notify that the event has been learned. this.globalEventHandlerInstance.NotifyMidiEventLearned(this.midiLearningType); // Do not continue to analyze the input. return; } // Iterate through all types if the message was recognized. for (int i = 0; i < Enum.GetNames(typeof(MidiEventType)).Length; i++) { if (this.learnedMidiTriggers[i].id == midiMessageID) { // Relevant event found, send the notification. MidiEvent midiEvent = new MidiEvent(); midiEvent.type = this.learnedMidiTriggers[i].type; midiEvent.value = midiMessageValue; this.globalEventHandlerInstance.NotifyMidiEventReceived(midiEvent); } } }
private void ResetMidiSettings(object sender, RoutedEventArgs e) { // Iterate through all MIDI event types. for (int i = 0; i < 12; i++) { // Create a new MidiEventTrigger object and fill it with empty data. MidiEventTrigger tempMidiEventTrigger = new MidiEventTrigger(); tempMidiEventTrigger.type = (MidiEventType)i; tempMidiEventTrigger.id = 0; // Set the extracted data to the active MIDI triggers. midiController.learnedMidiTriggers[i] = tempMidiEventTrigger; // Find the relevant button for this type and use the standard color. Button currentButtonElement = (Button)this.FindName("MidiEventType" + i.ToString()); currentButtonElement.Background = this.themeButtonColor; } }