From a3a3619cbb0df5fbf70ac75308cab43b71ddbd04 Mon Sep 17 00:00:00 2001 From: Simon G Date: Thu, 22 Apr 2021 18:09:15 +0200 Subject: [PATCH] - initialize faders with initial volumes --- Lib.Audio/Channel.cs | 20 +++++++++++++++++++- Lib.Audio/Controllable.cs | 2 ++ Lib.Audio/Controls/Fader.cs | 6 +++++- Lib.Audio/Controls/Interfaces/IFader.cs | 2 +- Lib.Audio/Device.cs | 2 +- Lib.Audio/Interfaces/IChannel.cs | 4 ++-- Lib.Audio/Interfaces/IControllable.cs | 1 + Lib.Audio/MasterControllable.cs | 5 +++++ 8 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Lib.Audio/Channel.cs b/Lib.Audio/Channel.cs index 7fc1fdc..3fd3ead 100644 --- a/Lib.Audio/Channel.cs +++ b/Lib.Audio/Channel.cs @@ -58,9 +58,10 @@ namespace Lib.Audio { Controllable = controllable; ToggleSelectButtonLed(); + InitializeFader(controllable); } - public void UnMapControllable() + private void UnMapControllable() { if (Controllable == null) return; @@ -109,6 +110,17 @@ namespace Lib.Audio _acknowledgeMessage = null; } + private void InitializeFader(IControllable controllable) + { + if (Fader == null) + return; + + float volume = controllable.GetVolume(); + Fader.RelativePosition = volume; + + _midiCommunication.Send(new PitchWheelChangeMessage(0, _channelNumber, Fader.Position)); + } + private void ToggleSelectButtonLed() { ISelectButton? selectButton = Buttons?.OfType().FirstOrDefault(); @@ -127,5 +139,11 @@ namespace Lib.Audio } public override string ToString() => $"Channel {_channelNumber}"; + + public void Dispose() + { + UnMapControllable(); + _midiCommunication.Send(new PitchWheelChangeMessage(0, _channelNumber, 0)); + } } } \ No newline at end of file diff --git a/Lib.Audio/Controllable.cs b/Lib.Audio/Controllable.cs index dc644de..e948e27 100644 --- a/Lib.Audio/Controllable.cs +++ b/Lib.Audio/Controllable.cs @@ -23,6 +23,8 @@ namespace Lib.Audio public string? IconPath => _audioSessionControls.FirstOrDefault(c => !string.IsNullOrEmpty(c.IconPath))?.IconPath; public void SetVolume(float volume) => _audioSessionControls.ForEach(c => c.SimpleAudioVolume.Volume = volume); + public float GetVolume() => _audioSessionControls[0].SimpleAudioVolume.Volume; //TODO: Can we always take 0? + public void Mute() => _audioSessionControls.ForEach(c => c.SimpleAudioVolume.Mute = true); public void UnMute() => _audioSessionControls.ForEach(c => c.SimpleAudioVolume.Mute = false); diff --git a/Lib.Audio/Controls/Fader.cs b/Lib.Audio/Controls/Fader.cs index 8560564..c4024bb 100644 --- a/Lib.Audio/Controls/Fader.cs +++ b/Lib.Audio/Controls/Fader.cs @@ -29,7 +29,11 @@ namespace Lib.Audio.Controls } public bool IsTouched { get; set; } - public float RelativePosition => Position / (MAX_POSITION - MIN_POSITION); + public float RelativePosition + { + get => Position / (MAX_POSITION - MIN_POSITION); + set => _position = (int) (value * (MAX_POSITION - MIN_POSITION)); + } public event EventHandler? PositionChanged; } diff --git a/Lib.Audio/Controls/Interfaces/IFader.cs b/Lib.Audio/Controls/Interfaces/IFader.cs index 9243443..90c2c46 100644 --- a/Lib.Audio/Controls/Interfaces/IFader.cs +++ b/Lib.Audio/Controls/Interfaces/IFader.cs @@ -13,7 +13,7 @@ namespace Lib.Audio.Controls.Interfaces bool IsTouched { get; set; } - float RelativePosition { get; } + float RelativePosition { get; set; } event EventHandler? PositionChanged; } diff --git a/Lib.Audio/Device.cs b/Lib.Audio/Device.cs index 27acda0..f0ae440 100644 --- a/Lib.Audio/Device.cs +++ b/Lib.Audio/Device.cs @@ -50,7 +50,7 @@ namespace Lib.Audio public void Dispose() { - Channels.ForEach(c => c.UnMapControllable()); + Channels.ForEach(c => c.Dispose()); _midiCommunication.Close(); } } diff --git a/Lib.Audio/Interfaces/IChannel.cs b/Lib.Audio/Interfaces/IChannel.cs index e062d96..9b19e3a 100644 --- a/Lib.Audio/Interfaces/IChannel.cs +++ b/Lib.Audio/Interfaces/IChannel.cs @@ -2,6 +2,7 @@ // Created: 2021-04-07 // Copyright(c) 2021 SimonG. All Rights Reserved. +using System; using System.Collections.Generic; using Lib.Audio.Controls.Buttons.Interfaces; using Lib.Audio.Controls.Interfaces; @@ -9,7 +10,7 @@ using Lib.Midi.Messages.Interfaces; namespace Lib.Audio.Interfaces { - public interface IChannel + public interface IChannel : IDisposable { IFader? Fader { get; } IKnob? Knob { get; } @@ -18,7 +19,6 @@ namespace Lib.Audio.Interfaces IControllable? Controllable { get; } void MapControllable(IControllable controllable); - void UnMapControllable(); void HandleMessage(IMidiMessage message); void SendAcknowledge(); } diff --git a/Lib.Audio/Interfaces/IControllable.cs b/Lib.Audio/Interfaces/IControllable.cs index c28f7be..86d4ff8 100644 --- a/Lib.Audio/Interfaces/IControllable.cs +++ b/Lib.Audio/Interfaces/IControllable.cs @@ -10,6 +10,7 @@ namespace Lib.Audio.Interfaces string? IconPath { get; } void SetVolume(float volume); + float GetVolume(); void Mute(); void UnMute(); } diff --git a/Lib.Audio/MasterControllable.cs b/Lib.Audio/MasterControllable.cs index a265d70..f8ec36f 100644 --- a/Lib.Audio/MasterControllable.cs +++ b/Lib.Audio/MasterControllable.cs @@ -43,6 +43,11 @@ namespace Lib.Audio // } } + public float GetVolume() + { + throw new System.NotImplementedException(); + } + public void Mute() => _audioEndpointVolume.Mute = true; public void UnMute() => _audioEndpointVolume.Mute = false;