diff --git a/Lib.Midi/Interfaces/IMidiCommunication.cs b/Lib.Midi/Interfaces/IMidiCommunication.cs index eaf6990..eaa1dd9 100644 --- a/Lib.Midi/Interfaces/IMidiCommunication.cs +++ b/Lib.Midi/Interfaces/IMidiCommunication.cs @@ -9,7 +9,7 @@ namespace Lib.Midi.Interfaces { public interface IMidiCommunication { - void Open(); + void Open(bool useMidiView); void Close(); void Send(IMidiMessage message); diff --git a/Lib.Midi/MidiCommunication.cs b/Lib.Midi/MidiCommunication.cs index d38db53..d3cdc0e 100644 --- a/Lib.Midi/MidiCommunication.cs +++ b/Lib.Midi/MidiCommunication.cs @@ -15,21 +15,18 @@ namespace Lib.Midi { public class MidiCommunication : IMidiCommunication { + private readonly IDriver _driver; private readonly IMidiMessageFactory _midiMessageFactory; - private readonly MidiIn _midiIn; - private readonly MidiOut _midiOut; + + private MidiIn? _midiIn; + private MidiOut? _midiOut; public MidiCommunication(IDriver driver, IMidiMessageFactory midiMessageFactory) { + _driver = driver; _midiMessageFactory = midiMessageFactory; - - var midiInput = MidiInputs.FirstOrDefault(i => i.midiInCapabilities.ProductId == driver.ProductId); - var midiOutput = MidiOutputs.FirstOrDefault(o => o.midiOutCapabilities.ProductId == driver.ProductId); - - _midiIn = new MidiIn(midiInput.index); - _midiOut = new MidiOut(midiOutput.index); } - + private IEnumerable<(MidiInCapabilities midiInCapabilities, int index)> MidiInputs { get @@ -48,8 +45,17 @@ namespace Lib.Midi } } - public void Open() + public void Open(bool useMidiView) { + int inputProductId = useMidiView ? MidiView.INPUT_PRODUCT_ID : _driver.InputProductId; + int outputProductId = useMidiView ? MidiView.OUTPUT_PRODUCT_ID : _driver.OutputProductId; + + var midiInput = MidiInputs.FirstOrDefault(i => i.midiInCapabilities.ProductId == inputProductId); + var midiOutput = MidiOutputs.FirstOrDefault(o => o.midiOutCapabilities.ProductId == outputProductId); + + _midiIn = new MidiIn(midiInput.index); + _midiOut = new MidiOut(midiOutput.index); + _midiIn.MessageReceived += OnMidiInMessageReceived; _midiIn.ErrorReceived += OnMidiInErrorReceived; _midiIn.Start(); @@ -57,15 +63,18 @@ namespace Lib.Midi public void Close() { - _midiIn.Stop(); - _midiIn.MessageReceived -= OnMidiInMessageReceived; - _midiIn.ErrorReceived -= OnMidiInErrorReceived; - - _midiIn.Close(); - _midiOut.Close(); + if (_midiIn != null) + { + _midiIn.Stop(); + _midiIn.MessageReceived -= OnMidiInMessageReceived; + _midiIn.ErrorReceived -= OnMidiInErrorReceived; + _midiIn.Close(); + } + + _midiOut?.Close(); } - public void Send(IMidiMessage message) => _midiOut.Send(message.RawMessage); + public void Send(IMidiMessage message) => _midiOut?.Send(message.RawMessage); private void OnMidiInMessageReceived(object? sender, MidiInMessageEventArgs args) => MessageReceived?.Invoke(this, _midiMessageFactory.Create(args)); private void OnMidiInErrorReceived(object? sender, MidiInMessageEventArgs args) => ErrorReceived?.Invoke(this, _midiMessageFactory.Create(args)); diff --git a/Lib.Midi/MidiView.cs b/Lib.Midi/MidiView.cs new file mode 100644 index 0000000..e0350b0 --- /dev/null +++ b/Lib.Midi/MidiView.cs @@ -0,0 +1,12 @@ +// Author: Simon Gockner +// Created: 2021-04-10 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +namespace Lib.Midi +{ + public class MidiView + { + public const int INPUT_PRODUCT_ID = 103; + public const int OUTPUT_PRODUCT_ID = 102; + } +} \ No newline at end of file