|
|
|
|
@ -15,19 +15,16 @@ 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 |
|
|
|
|
@ -48,24 +45,36 @@ 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(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void 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); |
|
|
|
|
_midiOut?.Close(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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)); |
|
|
|
|
|
|
|
|
|
|