From 623690aaec4ac84247bf057fa699222fcc71d03f Mon Sep 17 00:00:00 2001 From: Simon G Date: Thu, 22 Apr 2021 15:08:38 +0200 Subject: [PATCH] - only send acknowledge message for veloctiy.on --- Lib.Audio/Channel.cs | 5 +++-- Lib.Audio/Controls/Buttons/Button.cs | 22 ++++++++++++++----- .../Controls/Buttons/Interfaces/IButton.cs | 3 +-- Lib.Audio/Controls/Buttons/MuteButton.cs | 15 +++++-------- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/Lib.Audio/Channel.cs b/Lib.Audio/Channel.cs index c8b8e29..c938ec5 100644 --- a/Lib.Audio/Channel.cs +++ b/Lib.Audio/Channel.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; +using Lib.Audio.Controls; using Lib.Audio.Controls.Buttons.Factories; using Lib.Audio.Controls.Buttons.Interfaces; using Lib.Audio.Controls.Factories; @@ -60,7 +61,7 @@ namespace Lib.Audio else { IButton? button = Buttons?.FirstOrDefault(b => b.NoteNumber == noteOnMessage.NoteNumber); - _acknowledgeMessage = button?.HandleOn(Controllable, noteOnMessage.Velocity); + _acknowledgeMessage = button?.Handle(Controllable, (Velocity) noteOnMessage.Velocity); } } else if (message is NoteMessage noteMessage) @@ -73,7 +74,7 @@ namespace Lib.Audio else { IButton? button = Buttons?.FirstOrDefault(b => b.NoteNumber == noteMessage.NoteNumber); - _acknowledgeMessage = button?.HandleOff(Controllable, noteMessage.Velocity); + _acknowledgeMessage = button?.Handle(Controllable, (Velocity) noteMessage.Velocity); } } else if (message is PitchWheelChangeMessage pitchWheelChangeMessage) diff --git a/Lib.Audio/Controls/Buttons/Button.cs b/Lib.Audio/Controls/Buttons/Button.cs index 9e8ae1e..fa8e383 100644 --- a/Lib.Audio/Controls/Buttons/Button.cs +++ b/Lib.Audio/Controls/Buttons/Button.cs @@ -20,11 +20,23 @@ namespace Lib.Audio.Controls.Buttons } public int NoteNumber { get; } + protected bool IsActive { get; private set; } - public virtual IMidiMessage HandleOn(IControllable? controllable, int velocity) => - new NoteOnMessage(0, _channelNumber, NoteNumber, velocity, 0); - - public virtual IMidiMessage HandleOff(IControllable? controllable, int velocity) => - new NoteMessage(0, _channelNumber, NoteNumber, velocity); + public virtual IMidiMessage? Handle(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); + } + + protected virtual void Handle(IControllable? controllable) + { + + } } } \ 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 c201b69..0aaed57 100644 --- a/Lib.Audio/Controls/Buttons/Interfaces/IButton.cs +++ b/Lib.Audio/Controls/Buttons/Interfaces/IButton.cs @@ -11,7 +11,6 @@ namespace Lib.Audio.Controls.Buttons.Interfaces { int NoteNumber { get; } - IMidiMessage HandleOn(IControllable? controllable, int velocity); - IMidiMessage HandleOff(IControllable? controllable, int velocity); + IMidiMessage? Handle(IControllable? controllable, Velocity velocity); } } \ No newline at end of file diff --git a/Lib.Audio/Controls/Buttons/MuteButton.cs b/Lib.Audio/Controls/Buttons/MuteButton.cs index f68a434..f5162bd 100644 --- a/Lib.Audio/Controls/Buttons/MuteButton.cs +++ b/Lib.Audio/Controls/Buttons/MuteButton.cs @@ -4,7 +4,6 @@ using Lib.Audio.Controls.Buttons.Interfaces; using Lib.Audio.Interfaces; -using Lib.Midi.Messages.Interfaces; namespace Lib.Audio.Controls.Buttons { @@ -16,16 +15,12 @@ namespace Lib.Audio.Controls.Buttons } - public override IMidiMessage HandleOn(IControllable? controllable, int velocity) + protected override void Handle(IControllable? controllable) { - controllable?.Mute(); - return base.HandleOn(controllable, velocity); - } - - public override IMidiMessage HandleOff(IControllable? controllable, int velocity) - { - controllable?.UnMute(); - return base.HandleOff(controllable, velocity); + if (IsActive) + controllable?.Mute(); + else + controllable?.UnMute(); } } } \ No newline at end of file