diff --git a/Lib.Audio/Channel.cs b/Lib.Audio/Channel.cs index 8f12fe1..b9fbe18 100644 --- a/Lib.Audio/Channel.cs +++ b/Lib.Audio/Channel.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using Lib.Audio.Controls; +using Lib.Audio.Controls.Buttons; using Lib.Audio.Controls.Buttons.Factories; using Lib.Audio.Controls.Buttons.Interfaces; using Lib.Audio.Controls.Factories; @@ -50,9 +51,9 @@ namespace Lib.Audio if (xmlChannel.Knob != null) Knob = knobFactory.Create(); + Buttons = new List(); if (xmlChannel.Buttons != null) { - Buttons = new List(); foreach (var button in xmlChannel.Buttons) Buttons.Add(buttonFactory.Create(button, _channelNumber)); } @@ -63,16 +64,19 @@ namespace Lib.Audio public IFader? Fader { get; } public IKnob? Knob { get; } - public List? Buttons { get; } + public List Buttons { get; } public IControllable? Controllable { get; private set; } public void MapControllable(IControllable controllable) { Controllable = controllable; - ToggleSelectButtonLed(); + ToggleButtonLed(LedState.On); + + if (!Controllable.IsValid) + return; - if (Controllable.IsValid) - InitializeFader(); + InitializeFader(); + ToggleButtonLed(LedState.On); } private void UnMapControllable() @@ -80,7 +84,8 @@ namespace Lib.Audio if (Controllable == null) return; - ToggleSelectButtonLed(); + ToggleButtonLed(LedState.Off); + ToggleButtonLed(LedState.Off); } public void HandleMessage(IMidiMessage message) @@ -91,8 +96,8 @@ namespace Lib.Audio Fader.IsTouched = true; else { - IButton? button = Buttons?.FirstOrDefault(b => b.NoteNumber == noteOnMessage.NoteNumber); - _acknowledgeMessage = button?.Handle(Controllable, (Velocity) noteOnMessage.Velocity); + IButton? button = Buttons.FirstOrDefault(b => b.NoteNumber == noteOnMessage.NoteNumber); + _acknowledgeMessage = button?.HandlePressed(Controllable, (Velocity) noteOnMessage.Velocity); } } else if (message is NoteMessage noteMessage) @@ -104,8 +109,8 @@ namespace Lib.Audio } else { - IButton? button = Buttons?.FirstOrDefault(b => b.NoteNumber == noteMessage.NoteNumber); - _acknowledgeMessage = button?.Handle(Controllable, (Velocity) noteMessage.Velocity); + IButton? button = Buttons.FirstOrDefault(b => b.NoteNumber == noteMessage.NoteNumber); + _acknowledgeMessage = button?.HandlePressed(Controllable, (Velocity) noteMessage.Velocity); } } else if (message is PitchWheelChangeMessage pitchWheelChangeMessage) @@ -135,10 +140,10 @@ namespace Lib.Audio _midiCommunication.Send(new PitchWheelChangeMessage(0, _channelNumber, Fader.Position)); } - private void ToggleSelectButtonLed() + private void ToggleButtonLed(LedState ledState) where TButton : IButton { - ISelectButton? selectButton = Buttons?.OfType().FirstOrDefault(); - IMidiMessage? selectMessage = selectButton?.Select(); + TButton? button = Buttons.OfType().FirstOrDefault(); + IMidiMessage? selectMessage = button?.ToggleLed(ledState); if (selectMessage != null) _midiCommunication.Send(selectMessage); @@ -161,8 +166,11 @@ namespace Lib.Audio return; Controllable = _controllableCollector.GetControllableForExecutable(Controllable.ExecutablePath); - if (Controllable != null) - InitializeFader(); + if (Controllable == null) + return; + + InitializeFader(); + ToggleButtonLed(LedState.On); } private void OnProcessManagerProcessExited(object? sender, IObservedProcess process) @@ -173,7 +181,8 @@ namespace Lib.Audio if (!Controllable.ExecutablePath.Equals(process.FileName)) return; - Controllable.IsValid = false; //TODO: Toggle Record button led + Controllable.IsValid = false; + ToggleButtonLed(LedState.Off); } public override string ToString() => $"Channel {_channelNumber}"; @@ -185,6 +194,9 @@ namespace Lib.Audio UnMapControllable(); _midiCommunication.Send(new PitchWheelChangeMessage(0, _channelNumber, 0)); + + foreach (IMidiMessage ledOffMessage in Buttons.Select(b => b.ToggleLed(LedState.Off))) + _midiCommunication.Send(ledOffMessage); } } } \ No newline at end of file diff --git a/Lib.Audio/Controls/Buttons/Button.cs b/Lib.Audio/Controls/Buttons/Button.cs index fa8e383..3077316 100644 --- a/Lib.Audio/Controls/Buttons/Button.cs +++ b/Lib.Audio/Controls/Buttons/Button.cs @@ -22,21 +22,24 @@ namespace Lib.Audio.Controls.Buttons public int NoteNumber { get; } protected bool IsActive { get; private set; } - public virtual IMidiMessage? Handle(IControllable? controllable, Velocity velocity) + public virtual IMidiMessage? HandlePressed(IControllable? controllable, Velocity velocity) { if (velocity != Velocity.On) return null; IsActive = !IsActive; Handle(controllable); - - return IsActive ? new NoteOnMessage(0, _channelNumber, NoteNumber, (int) Velocity.On, 0) - : new NoteMessage(0, _channelNumber, NoteNumber, (int) Velocity.Off); + + return IsActive ? LedOn() : LedOff(); } + public IMidiMessage ToggleLed(LedState ledState) => ledState == LedState.Off ? LedOff() : LedOn(); protected virtual void Handle(IControllable? controllable) { } + + private IMidiMessage LedOn() => new NoteOnMessage(0, _channelNumber, NoteNumber, (int) Velocity.On, 0); + private IMidiMessage LedOff() => new NoteMessage(0, _channelNumber, NoteNumber, (int) Velocity.Off); } } \ No newline at end of file diff --git a/Lib.Audio/Controls/Buttons/Interfaces/IButton.cs b/Lib.Audio/Controls/Buttons/Interfaces/IButton.cs index 0aaed57..23a98c0 100644 --- a/Lib.Audio/Controls/Buttons/Interfaces/IButton.cs +++ b/Lib.Audio/Controls/Buttons/Interfaces/IButton.cs @@ -11,6 +11,7 @@ namespace Lib.Audio.Controls.Buttons.Interfaces { int NoteNumber { get; } - IMidiMessage? Handle(IControllable? controllable, Velocity velocity); + IMidiMessage? HandlePressed(IControllable? controllable, Velocity velocity); + IMidiMessage ToggleLed(LedState ledState); } } \ No newline at end of file diff --git a/Lib.Audio/Controls/Buttons/Interfaces/IRecordButton.cs b/Lib.Audio/Controls/Buttons/Interfaces/IRecordButton.cs index 153cd78..e1d8686 100644 --- a/Lib.Audio/Controls/Buttons/Interfaces/IRecordButton.cs +++ b/Lib.Audio/Controls/Buttons/Interfaces/IRecordButton.cs @@ -2,12 +2,10 @@ // Created: 2021-04-26 // Copyright(c) 2021 SimonG. All Rights Reserved. -using Lib.Midi.Messages.Interfaces; - namespace Lib.Audio.Controls.Buttons.Interfaces { public interface IRecordButton : IButton { - IMidiMessage? Record(); + } } \ No newline at end of file diff --git a/Lib.Audio/Controls/Buttons/Interfaces/ISelectButton.cs b/Lib.Audio/Controls/Buttons/Interfaces/ISelectButton.cs index f61b339..cbe04c4 100644 --- a/Lib.Audio/Controls/Buttons/Interfaces/ISelectButton.cs +++ b/Lib.Audio/Controls/Buttons/Interfaces/ISelectButton.cs @@ -2,12 +2,10 @@ // Created: 2021-04-17 // Copyright(c) 2021 SimonG. All Rights Reserved. -using Lib.Midi.Messages.Interfaces; - namespace Lib.Audio.Controls.Buttons.Interfaces { public interface ISelectButton : IButton { - IMidiMessage? Select(); + } } \ No newline at end of file diff --git a/Lib.Audio/Controls/Buttons/LedState.cs b/Lib.Audio/Controls/Buttons/LedState.cs new file mode 100644 index 0000000..240d65c --- /dev/null +++ b/Lib.Audio/Controls/Buttons/LedState.cs @@ -0,0 +1,12 @@ +// Author: Gockner, Simon +// Created: 2021-04-26 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +namespace Lib.Audio.Controls.Buttons +{ + public enum LedState + { + Off, + On + } +} \ No newline at end of file diff --git a/Lib.Audio/Controls/Buttons/RecordButton.cs b/Lib.Audio/Controls/Buttons/RecordButton.cs index 9ba1510..fbe2381 100644 --- a/Lib.Audio/Controls/Buttons/RecordButton.cs +++ b/Lib.Audio/Controls/Buttons/RecordButton.cs @@ -16,7 +16,6 @@ namespace Lib.Audio.Controls.Buttons } - public override IMidiMessage? Handle(IControllable? controllable, Velocity velocity) => null; //for now don't do anything when pressed - public IMidiMessage? Record() => base.Handle(null, Velocity.On); + public override IMidiMessage? HandlePressed(IControllable? controllable, Velocity velocity) => null; //for now don't do anything when pressed } } \ No newline at end of file diff --git a/Lib.Audio/Controls/Buttons/SelectButton.cs b/Lib.Audio/Controls/Buttons/SelectButton.cs index 7420d2f..4c3c047 100644 --- a/Lib.Audio/Controls/Buttons/SelectButton.cs +++ b/Lib.Audio/Controls/Buttons/SelectButton.cs @@ -16,7 +16,6 @@ namespace Lib.Audio.Controls.Buttons } - public override IMidiMessage? Handle(IControllable? controllable, Velocity velocity) => null; //for now don't do anything when pressed - public IMidiMessage? Select() => base.Handle(null, Velocity.On); + public override IMidiMessage? HandlePressed(IControllable? controllable, Velocity velocity) => null; //for now don't do anything when pressed } } \ No newline at end of file