From 0214fcbe0974940f954b7fda641f928f85967786 Mon Sep 17 00:00:00 2001 From: Simon G Date: Sat, 10 Apr 2021 17:58:38 +0200 Subject: [PATCH] - use driver to get correct midi device --- Lib.Audio/Device.cs | 2 +- .../Factories/IMidiCommunicationFactory.cs | 3 +- Lib.Midi/Lib.Midi.csproj | 4 +++ Lib.Midi/MidiCommunication.cs | 31 +++++++++++++++++-- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Lib.Audio/Device.cs b/Lib.Audio/Device.cs index 93f9f48..d4d9fed 100644 --- a/Lib.Audio/Device.cs +++ b/Lib.Audio/Device.cs @@ -23,7 +23,7 @@ namespace Lib.Audio public Device(IDriver driver, IMidiCommunicationFactory midiCommunicationFactory, IChannelFactory channelFactory) { _driver = driver; - _midiCommunication = midiCommunicationFactory.Create(); + _midiCommunication = midiCommunicationFactory.Create(driver); _channelFactory = channelFactory; Channels = InitializeChannels(); diff --git a/Lib.Midi/Factories/IMidiCommunicationFactory.cs b/Lib.Midi/Factories/IMidiCommunicationFactory.cs index b228134..26df81e 100644 --- a/Lib.Midi/Factories/IMidiCommunicationFactory.cs +++ b/Lib.Midi/Factories/IMidiCommunicationFactory.cs @@ -2,12 +2,13 @@ // Created: 2021-04-09 // Copyright(c) 2021 SimonG. All Rights Reserved. +using Lib.Driver.Interfaces; using Lib.Midi.Interfaces; namespace Lib.Midi.Factories { public interface IMidiCommunicationFactory { - IMidiCommunication Create(); + IMidiCommunication Create(IDriver driver); } } \ No newline at end of file diff --git a/Lib.Midi/Lib.Midi.csproj b/Lib.Midi/Lib.Midi.csproj index 71db116..28dab81 100644 --- a/Lib.Midi/Lib.Midi.csproj +++ b/Lib.Midi/Lib.Midi.csproj @@ -9,4 +9,8 @@ + + + + diff --git a/Lib.Midi/MidiCommunication.cs b/Lib.Midi/MidiCommunication.cs index 50209dd..d38db53 100644 --- a/Lib.Midi/MidiCommunication.cs +++ b/Lib.Midi/MidiCommunication.cs @@ -3,6 +3,9 @@ // Copyright(c) 2021 SimonG. All Rights Reserved. using System; +using System.Collections.Generic; +using System.Linq; +using Lib.Driver.Interfaces; using Lib.Midi.Interfaces; using Lib.Midi.Messages.Factories; using Lib.Midi.Messages.Interfaces; @@ -16,11 +19,33 @@ namespace Lib.Midi private readonly MidiIn _midiIn; private readonly MidiOut _midiOut; - public MidiCommunication(IMidiMessageFactory midiMessageFactory) + public MidiCommunication(IDriver driver, IMidiMessageFactory midiMessageFactory) { _midiMessageFactory = midiMessageFactory; - _midiIn = new MidiIn(0); //TODO: don't always use device 0? - _midiOut = new MidiOut(0); + + 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 + { + for (int i = 0; i < MidiIn.NumberOfDevices; i++) + yield return (MidiIn.DeviceInfo(i), i); + } + } + + private IEnumerable<(MidiOutCapabilities midiOutCapabilities, int index)> MidiOutputs + { + get + { + for (int i = 0; i < MidiOut.NumberOfDevices; i++) + yield return (MidiOut.DeviceInfo(i), i); + } } public void Open()