From 390311cebb51c787dd3b8f87e8eab622586b44e4 Mon Sep 17 00:00:00 2001 From: Hiroyuki Adachi Date: Wed, 16 Mar 2022 22:02:51 +0900 Subject: [PATCH] refactor: #13 keeping codes clean. --- MidiPlayer.Droid/MainActivity.cs | 29 ++++- MidiPlayer/Mixer.cs | 181 +++++++++++++++++-------------- 2 files changed, 124 insertions(+), 86 deletions(-) diff --git a/MidiPlayer.Droid/MainActivity.cs b/MidiPlayer.Droid/MainActivity.cs index 29bbfdf..290bbee 100644 --- a/MidiPlayer.Droid/MainActivity.cs +++ b/MidiPlayer.Droid/MainActivity.cs @@ -124,13 +124,38 @@ protected override void OnCreate(Bundle? savedInstanceState) { }; /// - /// add a callback function to be called when the mixer updated. + /// add a callback function to be called when the mixer selected. /// Mixer.Selected += (object sender, PropertyChangedEventArgs e) => { if (e.PropertyName is nameof(Mixer.Current)) { loadFader(); } }; + + /// + /// add a callback function to be called when the mixer updated. + /// + Mixer.Updated += (object sender, PropertyChangedEventArgs e) => { + return; + // FIXME: + if ((((Mixer.Fader) sender).Index == Mixer.Current)) { + if (e.PropertyName is nameof(Mixer.Fader.Name)) { + Log.Debug($"fadar {Mixer.Current}'s Name is updated."); + } + if (e.PropertyName is nameof(Mixer.Fader.Bank)) { + Log.Debug($"fadar {Mixer.Current}'s Bank is updated."); + } + if (e.PropertyName is nameof(Mixer.Fader.Program)) { + Log.Debug($"fadar {Mixer.Current}'s Program is updated."); + } + if (e.PropertyName is nameof(Mixer.Fader.Volume)) { + Log.Debug($"fadar {Mixer.Current}'s Volume is updated."); + } + if (e.PropertyName is nameof(Mixer.Fader.Pan)) { + Log.Debug($"fadar {Mixer.Current}'s Pan is updated."); + } + } + }; } /// @@ -314,7 +339,7 @@ void updateList(Synth.Track track) { listItem.Channel = track.Channel.ToString(); // update a fader. - Fader fader = Mixer.GetBy(trackIdx); + var fader = Mixer.GetBy(trackIdx); fader.Program = track.Program + 1; // zero base to one base; fader.Pan = track.Pan + 1; // zero base to one base; fader.Volume = track.Volume + 1; // zero base to one base; diff --git a/MidiPlayer/Mixer.cs b/MidiPlayer/Mixer.cs index 322fddc..c78853b 100644 --- a/MidiPlayer/Mixer.cs +++ b/MidiPlayer/Mixer.cs @@ -32,19 +32,32 @@ static Mixer() { _mixer = new(); _current = 0; Enumerable.Range(MIDI_TRACK_BASE, MIDI_TRACK_COUNT).ToList().ForEach(x => { - _mixer.Add(x, new Fader(x)); + Fader fader = new(x); + fader.Updated += Updated; + _mixer.Add(x, fader); }); } - /////////////////////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////////////////////// // Events [verb, verb phrase] /// - /// static selected event handler. + /// selected event handler. /// + /// + /// called when Mixer's channel is clicked.
+ ///
public static event PropertyChangedEventHandler? Selected; - /////////////////////////////////////////////////////////////////////////////////////////// + /// + /// updated event handler. + /// + /// + /// called when Fader's properties change.
+ ///
+ public static event PropertyChangedEventHandler? Updated; + + /////////////////////////////////////////////////////////////////////////////////////////////// // public static Properties [noun, noun phrase, adjective] /// @@ -63,7 +76,7 @@ public static int Current { } } - /////////////////////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////////////////////// // public static Methods [verb, verb phrases] /// @@ -86,117 +99,117 @@ public static Fader GetPrevious() { public static Fader GetBy(int index) { return _mixer[index]; } - } - /// - /// Fader class. - /// - public class Fader { + /// + /// Fader class. + /// + public class Fader { #nullable enable - /////////////////////////////////////////////////////////////////////////////////////////////// - // Fields [nouns, noun phrases] + /////////////////////////////////////////////////////////////////////////////////////////// + // Fields [nouns, noun phrases] - int _index = -1; + int _index = -1; - bool _sounds = true; // mute param. + bool _sounds = true; // mute param. - string _name = "undefined"; + string _name = "undefined"; - int _channel = -1; + int _channel = -1; - int _bank = 0; + int _bank = 0; - int _program = 0; + int _program = 0; - int _volume = 104; + int _volume = 104; - int _pan = 64; // center + int _pan = 64; // center - /////////////////////////////////////////////////////////////////////////////////////////// - // Constructor + /////////////////////////////////////////////////////////////////////////////////////////// + // Constructor - public Fader(int index) { - _index = index; - } + public Fader(int index) { + _index = index; + } - /////////////////////////////////////////////////////////////////////////////////////////// - // Events [verb, verb phrase] + /////////////////////////////////////////////////////////////////////////////////////////// + // Events [verb, verb phrase] - /// - /// updated event handler. - /// - public event PropertyChangedEventHandler? Updated; + /// + /// updated event handler. + /// + public event PropertyChangedEventHandler? Updated; - /////////////////////////////////////////////////////////////////////////////////////////// - // Properties [noun, noun phrase, adjective] + /////////////////////////////////////////////////////////////////////////////////////////// + // Properties [noun, noun phrase, adjective] - public int Index { - get => _index; - set { - _index = value; - Updated?.Invoke(this, new(nameof(Index))); + public int Index { + get => _index; + set { + _index = value; // TODO: Compare if the value is the same value as the previous. + Updated?.Invoke(this, new(nameof(Index))); // FIXME: not Invoke if two values are the same. + } } - } - public bool Sounds { - get => _sounds; - set { - _sounds = value; - Updated?.Invoke(this, new(nameof(Sounds))); + public bool Sounds { + get => _sounds; + set { + _sounds = value; // TODO: Compare if the value is the same value as the previous. + Updated?.Invoke(this, new(nameof(Sounds))); // FIXME: not Invoke if two values are the same. + } } - } - public string Name { - get => _name; - set { - _name = value; - Updated?.Invoke(this, new(nameof(Name))); + public string Name { + get => _name; + set { + _name = value; // TODO: Compare if the value is the same value as the previous. + Updated?.Invoke(this, new(nameof(Name))); // FIXME: not Invoke if two values are the same. + } } - } - public int Channel { - get => _channel; - set { - _channel = value; - Updated?.Invoke(this, new(nameof(Channel))); + public int Channel { + get => _channel; + set { + _channel = value; // TODO: Compare if the value is the same value as the previous. + Updated?.Invoke(this, new(nameof(Channel))); // FIXME: not Invoke if two values are the same. + } } - } - public int Bank { - get { - if (_channel == 9 && _bank != 128) { - return 128; // Drum + public int Bank { + get { + if (_channel == 9 && _bank != 128) { + return 128; // Drum + } + return _bank; + } + set { + _bank = value; // TODO: Compare if the value is the same value as the previous. + Updated?.Invoke(this, new(nameof(Bank))); // FIXME: not Invoke if two values are the same. } - return _bank; - } - set { - _bank = value; - Updated?.Invoke(this, new(nameof(Bank))); } - } - public int Program { - get => _program; - set { - _program = value; - Updated?.Invoke(this, new(nameof(Program))); + public int Program { + get => _program; + set { + _program = value; // TODO: Compare if the value is the same value as the previous. + Updated?.Invoke(this, new(nameof(Program))); // FIXME: not Invoke if two values are the same. + } } - } - public int Volume { - get => _volume; - set { - _volume = value; - Updated?.Invoke(this, new(nameof(Volume))); + public int Volume { + get => _volume; + set { + _volume = value; // TODO: Compare if the value is the same value as the previous. + Updated?.Invoke(this, new(nameof(Volume))); // FIXME: not Invoke if two values are the same. + } } - } - public int Pan { - get => _pan; - set { - _pan = value; - Updated?.Invoke(this, new(nameof(Pan))); + public int Pan { + get => _pan; + set { + _pan = value; // TODO: Compare if the value is the same value as the previous. + Updated?.Invoke(this, new(nameof(Pan))); // FIXME: not Invoke if two values are the same. + } } } }