diff --git a/Lib.Audio/Channel.cs b/Lib.Audio/Channel.cs index e1b4733..c8b8e29 100644 --- a/Lib.Audio/Channel.cs +++ b/Lib.Audio/Channel.cs @@ -3,6 +3,7 @@ // Copyright(c) 2021 SimonG. All Rights Reserved. using System.Collections.Generic; +using System.Linq; using Lib.Audio.Controls.Buttons.Factories; using Lib.Audio.Controls.Buttons.Interfaces; using Lib.Audio.Controls.Factories; @@ -40,7 +41,7 @@ namespace Lib.Audio { Buttons = new List(); foreach (var button in xmlChannel.Buttons) - Buttons.Add(buttonFactory.Create(button)); + Buttons.Add(buttonFactory.Create(button, _channelNumber)); } } @@ -56,6 +57,11 @@ namespace Lib.Audio { if (Fader?.NoteNumber == noteOnMessage.NoteNumber) Fader.IsTouched = true; + else + { + IButton? button = Buttons?.FirstOrDefault(b => b.NoteNumber == noteOnMessage.NoteNumber); + _acknowledgeMessage = button?.HandleOn(Controllable, noteOnMessage.Velocity); + } } else if (message is NoteMessage noteMessage) { @@ -64,6 +70,11 @@ namespace Lib.Audio Fader.IsTouched = false; _acknowledgeMessage = new PitchWheelChangeMessage(0, _channelNumber, Fader.Position); } + else + { + IButton? button = Buttons?.FirstOrDefault(b => b.NoteNumber == noteMessage.NoteNumber); + _acknowledgeMessage = button?.HandleOff(Controllable, 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 d0c8d20..9e8ae1e 100644 --- a/Lib.Audio/Controls/Buttons/Button.cs +++ b/Lib.Audio/Controls/Buttons/Button.cs @@ -3,11 +3,28 @@ // Copyright(c) 2021 SimonG. All Rights Reserved. using Lib.Audio.Controls.Buttons.Interfaces; +using Lib.Audio.Interfaces; +using Lib.Midi.Messages; +using Lib.Midi.Messages.Interfaces; namespace Lib.Audio.Controls.Buttons { - public class Button : IButton + public class Button : IButton //TODO: Make abstract? { + private readonly int _channelNumber; + public Button(int noteNumber, int channelNumber) + { + NoteNumber = noteNumber; + _channelNumber = channelNumber; + } + + public int NoteNumber { get; } + + 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); } } \ No newline at end of file diff --git a/Lib.Audio/Controls/Buttons/Factories/ButtonFactory.cs b/Lib.Audio/Controls/Buttons/Factories/ButtonFactory.cs index 88296b4..344ab33 100644 --- a/Lib.Audio/Controls/Buttons/Factories/ButtonFactory.cs +++ b/Lib.Audio/Controls/Buttons/Factories/ButtonFactory.cs @@ -9,9 +9,14 @@ namespace Lib.Audio.Controls.Buttons.Factories { public class ButtonFactory : IButtonFactory { - public IButton Create(XmlButton xmlButton) + public IButton Create(XmlButton xmlButton, int channelNumber) { - return new Button(); + if (xmlButton.ButtonType == "Select") + return new SelectButton(xmlButton.NoteNumber, channelNumber); + else if (xmlButton.ButtonType == "Mute") + return new MuteButton(xmlButton.NoteNumber, channelNumber); + + return new Button(xmlButton.NoteNumber, channelNumber); } } } \ 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 9ccf754..698ac74 100644 --- a/Lib.Audio/Controls/Buttons/Factories/IButtonFactory.cs +++ b/Lib.Audio/Controls/Buttons/Factories/IButtonFactory.cs @@ -9,6 +9,6 @@ namespace Lib.Audio.Controls.Buttons.Factories { public interface IButtonFactory { - IButton Create(XmlButton xmlButton); + IButton Create(XmlButton xmlButton, int channelNumber); } } \ 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 893a699..c201b69 100644 --- a/Lib.Audio/Controls/Buttons/Interfaces/IButton.cs +++ b/Lib.Audio/Controls/Buttons/Interfaces/IButton.cs @@ -2,10 +2,16 @@ // Created: 2021-04-07 // Copyright(c) 2021 SimonG. All Rights Reserved. +using Lib.Audio.Interfaces; +using Lib.Midi.Messages.Interfaces; + namespace Lib.Audio.Controls.Buttons.Interfaces { public interface IButton { - + int NoteNumber { get; } + + IMidiMessage HandleOn(IControllable? controllable, int velocity); + IMidiMessage HandleOff(IControllable? controllable, int velocity); } } \ No newline at end of file diff --git a/Lib.Audio/Controls/Buttons/Interfaces/ISelectButton.cs b/Lib.Audio/Controls/Buttons/Interfaces/ISelectButton.cs new file mode 100644 index 0000000..cbe04c4 --- /dev/null +++ b/Lib.Audio/Controls/Buttons/Interfaces/ISelectButton.cs @@ -0,0 +1,11 @@ +// Author: Simon Gockner +// Created: 2021-04-17 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +namespace Lib.Audio.Controls.Buttons.Interfaces +{ + public interface ISelectButton : IButton + { + + } +} \ No newline at end of file diff --git a/Lib.Audio/Controls/Buttons/MuteButton.cs b/Lib.Audio/Controls/Buttons/MuteButton.cs index 0fa3c35..f68a434 100644 --- a/Lib.Audio/Controls/Buttons/MuteButton.cs +++ b/Lib.Audio/Controls/Buttons/MuteButton.cs @@ -3,11 +3,29 @@ // Copyright(c) 2021 SimonG. All Rights Reserved. using Lib.Audio.Controls.Buttons.Interfaces; +using Lib.Audio.Interfaces; +using Lib.Midi.Messages.Interfaces; namespace Lib.Audio.Controls.Buttons { public class MuteButton : Button, IMuteButton { - + public MuteButton(int noteNumber, int channelNumber) + : base(noteNumber, channelNumber) + { + + } + + public override IMidiMessage HandleOn(IControllable? controllable, int velocity) + { + controllable?.Mute(); + return base.HandleOn(controllable, velocity); + } + + public override IMidiMessage HandleOff(IControllable? controllable, int velocity) + { + controllable?.UnMute(); + return base.HandleOff(controllable, velocity); + } } } \ No newline at end of file diff --git a/Lib.Audio/Controls/Buttons/SelectButton.cs b/Lib.Audio/Controls/Buttons/SelectButton.cs new file mode 100644 index 0000000..b1abf5e --- /dev/null +++ b/Lib.Audio/Controls/Buttons/SelectButton.cs @@ -0,0 +1,17 @@ +// Author: Simon Gockner +// Created: 2021-04-17 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +using Lib.Audio.Controls.Buttons.Interfaces; + +namespace Lib.Audio.Controls.Buttons +{ + public class SelectButton : Button, ISelectButton + { + public SelectButton(int noteNumber, int channelNumber) + : base(noteNumber, channelNumber) + { + + } + } +} \ No newline at end of file diff --git a/Lib.Driver/Xml/XmlButton.cs b/Lib.Driver/Xml/XmlButton.cs index 814347c..56fd204 100644 --- a/Lib.Driver/Xml/XmlButton.cs +++ b/Lib.Driver/Xml/XmlButton.cs @@ -11,5 +11,8 @@ namespace Lib.Driver.Xml { [XmlAttribute("Type")] public string? ButtonType { get; set; } + + [XmlAttribute("Note")] + public int NoteNumber { get; set; } } } \ No newline at end of file diff --git a/Lib.Midi/Messages/NoteMessage.cs b/Lib.Midi/Messages/NoteMessage.cs index 924963f..4f0f552 100644 --- a/Lib.Midi/Messages/NoteMessage.cs +++ b/Lib.Midi/Messages/NoteMessage.cs @@ -12,9 +12,16 @@ namespace Lib.Midi.Messages : base(noteEvent) { + } + + public NoteMessage(long absoluteTime, int channelNumber, int noteNumber, int velocity) + : base(new NoteEvent(absoluteTime, channelNumber, MidiCommandCode.NoteOff, noteNumber, velocity)) //TODO: don't always pass note off? + { + } private NoteEvent NoteEvent => (NoteEvent) _midiEvent; public int NoteNumber => NoteEvent.NoteNumber; + public int Velocity => NoteEvent.Velocity; } } \ No newline at end of file diff --git a/Lib.Midi/Messages/NoteOnMessage.cs b/Lib.Midi/Messages/NoteOnMessage.cs index 34e6a38..79e3062 100644 --- a/Lib.Midi/Messages/NoteOnMessage.cs +++ b/Lib.Midi/Messages/NoteOnMessage.cs @@ -14,7 +14,14 @@ namespace Lib.Midi.Messages } + public NoteOnMessage(long absoluteTime, int channelNumber, int noteNumber, int velocity, int duration) + : base(new NoteOnEvent(absoluteTime, channelNumber, noteNumber, velocity, duration)) + { + + } + private NoteOnEvent NoteOnEvent => (NoteOnEvent) _midiEvent; public int NoteNumber => NoteOnEvent.NoteNumber; + public int Velocity => NoteOnEvent.Velocity; } } \ No newline at end of file diff --git a/Mystify/Installers/AudioInstaller.cs b/Mystify/Installers/AudioInstaller.cs index 6ef9b50..28cd82f 100644 --- a/Mystify/Installers/AudioInstaller.cs +++ b/Mystify/Installers/AudioInstaller.cs @@ -31,6 +31,7 @@ namespace Mystify.Installers container.Register(); container.Register(); container.Register(); + container.Register(); //factories container.RegisterFactory();