From 12b9bafa75e75d9576e9c99f3d929ffb24f65c3c Mon Sep 17 00:00:00 2001 From: Simon G Date: Sat, 10 Apr 2021 13:09:32 +0200 Subject: [PATCH] - use driver to initialize --- Lib.Audio/Channel.cs | 45 ++++++++++--------- .../Buttons/Factories/ButtonFactory.cs | 17 +++++++ .../Buttons/Factories/IButtonFactory.cs | 4 +- Lib.Audio/Device.cs | 17 ++++--- Lib.Audio/Factories/IChannelFactory.cs | 3 +- Lib.Audio/Factories/IDeviceFactory.cs | 3 +- Lib.Audio/Interfaces/IChannel.cs | 6 +-- Lib.Audio/Lib.Audio.csproj | 1 + Mystify/Installers/AudioInstaller.cs | 2 +- 9 files changed, 62 insertions(+), 36 deletions(-) create mode 100644 Lib.Audio/Controls/Buttons/Factories/ButtonFactory.cs diff --git a/Lib.Audio/Channel.cs b/Lib.Audio/Channel.cs index 3fbef69..303c7cc 100644 --- a/Lib.Audio/Channel.cs +++ b/Lib.Audio/Channel.cs @@ -8,44 +8,45 @@ using Lib.Audio.Controls.Buttons.Interfaces; using Lib.Audio.Controls.Factories; using Lib.Audio.Controls.Interfaces; using Lib.Audio.Interfaces; +using Lib.Driver.Xml; namespace Lib.Audio { public class Channel : IChannel { - private readonly uint _channelNumber; + private readonly int _channelNumber; - public Channel(uint channelNumber, - IDeviceButtonConfiguration buttonConfiguration, + public Channel(int channelNumber, + XmlChannel xmlChannel, IFaderFactory faderFactory, IKnobFactory knobFactory, IButtonFactory buttonFactory) { _channelNumber = channelNumber; - Fader = faderFactory.Create(); - Knob = knobFactory.Create(); - Buttons = InitializeButtons(buttonConfiguration, buttonFactory); - - Fader.PositionChanged += OnFaderPositionChanged; - } - public IFader Fader { get; } - public IKnob Knob { get; } - public List Buttons { get; } - public IControllable? Controllable { get; private set; } - - public void AllocateControllable(IControllable controllable) => Controllable = controllable; + if (xmlChannel.Fader != null) + { + Fader = faderFactory.Create(); + Fader.PositionChanged += OnFaderPositionChanged; + } - //TODO: fix button handling - private List InitializeButtons(IDeviceButtonConfiguration buttonConfiguration, IButtonFactory buttonFactory) - { - List buttons = new(); - for (int i = 0; i < buttonConfiguration.NumberOfButtons; i++) - buttons.Add(buttonFactory.Create()); + if (xmlChannel.Knob != null) + Knob = knobFactory.Create(); - return buttons; + if (xmlChannel.Buttons != null) + { + Buttons = new List(); + foreach (var button in xmlChannel.Buttons) + Buttons.Add(buttonFactory.Create(button)); + } } + + public IFader? Fader { get; } + public IKnob? Knob { get; } + public List? Buttons { get; } + public IControllable? Controllable { get; private set; } + public void AllocateControllable(IControllable controllable) => Controllable = controllable; private void OnFaderPositionChanged(object? sender, float position) => Controllable?.SetVolume(position); public override string ToString() => $"Channel {_channelNumber}"; diff --git a/Lib.Audio/Controls/Buttons/Factories/ButtonFactory.cs b/Lib.Audio/Controls/Buttons/Factories/ButtonFactory.cs new file mode 100644 index 0000000..88296b4 --- /dev/null +++ b/Lib.Audio/Controls/Buttons/Factories/ButtonFactory.cs @@ -0,0 +1,17 @@ +// Author: Simon Gockner +// Created: 2021-04-10 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +using Lib.Audio.Controls.Buttons.Interfaces; +using Lib.Driver.Xml; + +namespace Lib.Audio.Controls.Buttons.Factories +{ + public class ButtonFactory : IButtonFactory + { + public IButton Create(XmlButton xmlButton) + { + return new Button(); + } + } +} \ No newline at end of file diff --git a/Lib.Audio/Controls/Buttons/Factories/IButtonFactory.cs b/Lib.Audio/Controls/Buttons/Factories/IButtonFactory.cs index 9c2ae0f..9ccf754 100644 --- a/Lib.Audio/Controls/Buttons/Factories/IButtonFactory.cs +++ b/Lib.Audio/Controls/Buttons/Factories/IButtonFactory.cs @@ -3,12 +3,12 @@ // Copyright(c) 2021 SimonG. All Rights Reserved. using Lib.Audio.Controls.Buttons.Interfaces; +using Lib.Driver.Xml; namespace Lib.Audio.Controls.Buttons.Factories { public interface IButtonFactory { - IButton Create(); - // IMuteButton Create(); + IButton Create(XmlButton xmlButton); } } \ No newline at end of file diff --git a/Lib.Audio/Device.cs b/Lib.Audio/Device.cs index 77dce84..26318c1 100644 --- a/Lib.Audio/Device.cs +++ b/Lib.Audio/Device.cs @@ -3,8 +3,10 @@ // Copyright(c) 2021 SimonG. All Rights Reserved. using System.Collections.Generic; +using System.Linq; using Lib.Audio.Factories; using Lib.Audio.Interfaces; +using Lib.Driver.Interfaces; using Lib.Midi.Factories; using Lib.Midi.Interfaces; using Lib.Midi.Messages; @@ -14,17 +16,17 @@ namespace Lib.Audio { public class Device : IDevice { + private readonly IDriver _driver; private readonly IMidiCommunication _midiCommunication; - private readonly IDeviceButtonConfiguration _buttonConfiguration; + private readonly IChannelFactory _channelFactory; - public Device(IMidiCommunicationFactory midiCommunicationFactory, IChannelFactory channelFactory) + public Device(IDriver driver, IMidiCommunicationFactory midiCommunicationFactory, IChannelFactory channelFactory) { + _driver = driver; _midiCommunication = midiCommunicationFactory.Create(); - _buttonConfiguration = new DeviceButtonConfiguration {HasMuteButton = true, NumberOfButtons = 4}; //FixMe: remove hard coded config + _channelFactory = channelFactory; - Channels = new List(); - for (uint i = 0; i < 8; i++) //FixMe: remove hard coded config - Channels.Add(channelFactory.Create(i, _buttonConfiguration)); + Channels = InitializeChannels(); _midiCommunication.MessageReceived += OnMidiCommunicationMessageReceived; _midiCommunication.ErrorReceived += OnMidiCommunicationErrorReceived; @@ -39,6 +41,9 @@ namespace Lib.Audio _midiCommunication.Send(new ControlChangeMessage(0, 1, 127)); } + private List InitializeChannels() => _driver.Channels == null ? new List() + : _driver.Channels.Select(c => _channelFactory.Create(_driver.Channels.IndexOf(c), c)).ToList(); + private void OnMidiCommunicationMessageReceived(object? sender, IMidiMessage message) { int i = 0; diff --git a/Lib.Audio/Factories/IChannelFactory.cs b/Lib.Audio/Factories/IChannelFactory.cs index feb2cfb..7b00b53 100644 --- a/Lib.Audio/Factories/IChannelFactory.cs +++ b/Lib.Audio/Factories/IChannelFactory.cs @@ -3,11 +3,12 @@ // Copyright(c) 2021 SimonG. All Rights Reserved. using Lib.Audio.Interfaces; +using Lib.Driver.Xml; namespace Lib.Audio.Factories { public interface IChannelFactory { - IChannel Create(uint channelNumber, IDeviceButtonConfiguration deviceButtonConfiguration); + IChannel Create(int channelNumber, XmlChannel xmlChannel); } } \ No newline at end of file diff --git a/Lib.Audio/Factories/IDeviceFactory.cs b/Lib.Audio/Factories/IDeviceFactory.cs index 5a871a3..c653d44 100644 --- a/Lib.Audio/Factories/IDeviceFactory.cs +++ b/Lib.Audio/Factories/IDeviceFactory.cs @@ -3,11 +3,12 @@ // Copyright(c) 2021 SimonG. All Rights Reserved. using Lib.Audio.Interfaces; +using Lib.Driver.Interfaces; namespace Lib.Audio.Factories { public interface IDeviceFactory { - IDevice Create(); + IDevice Create(IDriver driver); } } \ No newline at end of file diff --git a/Lib.Audio/Interfaces/IChannel.cs b/Lib.Audio/Interfaces/IChannel.cs index 789e8d3..c3e465c 100644 --- a/Lib.Audio/Interfaces/IChannel.cs +++ b/Lib.Audio/Interfaces/IChannel.cs @@ -10,9 +10,9 @@ namespace Lib.Audio.Interfaces { public interface IChannel { - IFader Fader { get; } - IKnob Knob { get; } - List Buttons { get; } + IFader? Fader { get; } + IKnob? Knob { get; } + List? Buttons { get; } IControllable? Controllable { get; } diff --git a/Lib.Audio/Lib.Audio.csproj b/Lib.Audio/Lib.Audio.csproj index bb66a40..011ec4b 100644 --- a/Lib.Audio/Lib.Audio.csproj +++ b/Lib.Audio/Lib.Audio.csproj @@ -11,6 +11,7 @@ + diff --git a/Mystify/Installers/AudioInstaller.cs b/Mystify/Installers/AudioInstaller.cs index cb62746..2e9d4f5 100644 --- a/Mystify/Installers/AudioInstaller.cs +++ b/Mystify/Installers/AudioInstaller.cs @@ -40,7 +40,7 @@ namespace Mystify.Installers //control factories container.RegisterFactory(); container.RegisterFactory(); - container.RegisterFactory(); + container.Register(); } } } \ No newline at end of file