Skip to content

Commit

Permalink
refactor: #13 keeping codes clean.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroxpepe committed Mar 18, 2022
1 parent f6053d8 commit f4c29ce
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 149 deletions.
9 changes: 5 additions & 4 deletions MidiPlayer.Droid/MainActivity.Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@ void initializeComponent() {
_numberPickerProg.MaxValue = 128;
_numberPickerProg.ValueChanged += (object sender, NumberPicker.ValueChangeEventArgs e) => {
var fader = Mixer.GetCurrent();
fader.Program = _numberPickerProg.Value;
Log.Debug($"_numberPickerProg.Value: {_numberPickerProg.Value}");
fader.ProgramAsOneBased = _numberPickerProg.Value;
};
_numberPickerPan = FindViewById<NumberPicker>(Resource.Id.number_picker_pan);
_numberPickerPan.MinValue = 1;
_numberPickerPan.MaxValue = 128;
_numberPickerPan.Value = 65;
_numberPickerPan.ValueChanged += (object sender, NumberPicker.ValueChangeEventArgs e) => {
var fader = Mixer.GetCurrent();
Log.Debug($"_numberPickerPan.Value: {_numberPickerPan.Value}");
fader.Pan = _numberPickerPan.Value;
};
_numberPickerVol = FindViewById<NumberPicker>(Resource.Id.number_picker_vol);
Expand All @@ -86,6 +88,7 @@ void initializeComponent() {
_numberPickerVol.Value = 104;
_numberPickerVol.ValueChanged += (object sender, NumberPicker.ValueChangeEventArgs e) => {
var fader = Mixer.GetCurrent();
Log.Debug($"_numberPickerVol.Value: {_numberPickerVol.Value}");
fader.Volume = _numberPickerVol.Value;
};
_checkBoxMute = FindViewById<CheckBox>(Resource.Id.check_box_mute);
Expand All @@ -110,9 +113,7 @@ void initializeComponent() {
var listItemAdapter = new ListItemAdapter(this, 0, _itemList);
_itemListView.Adapter = listItemAdapter;
_itemListView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) => {
var item = _itemListView.GetItemAtPosition(e.Position);
ListItem listItem = item.Cast<ListItem>();
Log.Info($"setected: {e.Position}");
Log.Debug($"setected: {e.Position}");
Mixer.Current = e.Position;
};
}
Expand Down
17 changes: 9 additions & 8 deletions MidiPlayer.Droid/MainActivity.EventCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,17 @@ void buttonDeletePlaylist_Click(object sender, EventArgs e) {
void buttonSendSynth_Click(object sender, EventArgs e) {
Log.Info("buttonSendSynth clicked.");
try {
var midiChannel = int.Parse(_textViewChannel.Text);
Log.Info($"send a data to MIDI {midiChannel} channel.");
Log.Info($"prog: {_numberPickerProg.Value} pan: {_numberPickerPan.Value} vol: {_numberPickerVol.Value}.");
Mixer.Fader fader = Mixer.GetCurrent();
Log.Debug($"track index {fader.Index}: send a data to MIDI {fader.Channel} channel.");
Log.Debug($"prog: {fader.Program} pan: {fader.Pan} vol: {fader.Volume}.");
Data data = new() {
Program = _numberPickerProg.Value,
Pan = _numberPickerPan.Value,
Volume = _numberPickerVol.Value,
Mute = _checkBoxMute.Checked
Channel = fader.Channel,
Program = fader.Program,
Pan = fader.Pan,
Volume = fader.Volume,
Mute = !fader.Sounds
};
EventQueue.Enqueue(midiChannel, data);
EventQueue.Enqueue(fader.Channel, data); // FIXME: Channel to track.
} catch (Exception ex) {
Log.Error(ex.Message);
}
Expand Down
104 changes: 49 additions & 55 deletions MidiPlayer.Droid/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,39 +118,80 @@ protected override void OnCreate(Bundle? savedInstanceState) {
/// <summary>
/// add a callback function to be called when the synth updated.
/// </summary>
/// <remarks>
/// update listitem values by track values.
/// </remarks>
Synth.Updated += (object sender, PropertyChangedEventArgs e) => {
var track = (Synth.Track) sender;
updateList(track);
ListItem listItem = _itemList[track.IndexWithExcludingConductor];
listItem.Name = track.Name;
listItem.Instrument = Synth.GetVoice(track.Index);
listItem.Channel = track.ChannelAsOneBased.ToString();
};

/// <summary>
/// add a callback function to be called when the synth updated.
/// </summary>
/// <remarks>
/// update fader values by track values.
/// </remarks>
Synth.Updated += (object sender, PropertyChangedEventArgs e) => {
var track = (Synth.Track) sender;
Mixer.Fader fader = Mixer.GetBy(track.IndexWithExcludingConductor);
if (e.PropertyName is nameof(Synth.Track.Channel)) {
Log.Debug($"Synth.Updated: track {track.Index} Channel is {track.Channel}");
fader.Channel = track.Channel;
}
if (e.PropertyName is nameof(Synth.Track.Program)) {
Log.Debug($"Synth.Updated: track {track.Index} Program is {track.Program}");
fader.Program = track.Program;
}
if (e.PropertyName is nameof(Synth.Track.Pan)) {
Log.Debug($"Synth.Updated: track {track.Index} Pan is {track.Pan}");
fader.Pan = track.Pan;
}
if (e.PropertyName is nameof(Synth.Track.Volume)) {
Log.Debug($"Synth.Updated: track {track.Index} Volume is {track.Volume}");
fader.Volume = track.Volume;
}
};

/// <summary>
/// add a callback function to be called when the mixer selected.
/// </summary>
Mixer.Selected += (object sender, PropertyChangedEventArgs e) => {
if (e.PropertyName is nameof(Mixer.Current)) {
loadFader();
Mixer.Fader fader = Mixer.GetCurrent();
_textViewNo.Text = fader.IndexAsOneBased.ToString();
_textViewChannel.Text = fader.ChannelAsOneBased.ToString();
_numberPickerProg.Value = fader.ProgramAsOneBased;
_numberPickerPan.Value = fader.Pan;
_numberPickerVol.Value = fader.Volume;
_checkBoxMute.Checked = !fader.Sounds;
}
};

/// <summary>
/// add a callback function to be called when the mixer updated.
/// </summary>
Mixer.Updated += (object sender, PropertyChangedEventArgs e) => {
if ((((Mixer.Fader) sender).Index == Mixer.Current)) {
var fader = (Mixer.Fader) sender;
if (fader.Index == Mixer.Current) {
Log.Debug($"Mixer.Updated: mixer.current {Mixer.Current}: fader.Index {fader.Index}");
if (e.PropertyName is nameof(Mixer.Fader.Name)) {
Log.Debug($"fadar {Mixer.Current} Name is updated.");
Log.Debug($"fadar {fader.Index} Name is {fader.Name}");
}
if (e.PropertyName is nameof(Mixer.Fader.Bank)) {
Log.Debug($"fadar {Mixer.Current} Bank is updated.");
Log.Debug($"fadar {fader.Index} Bank is {fader.Bank}");
}
if (e.PropertyName is nameof(Mixer.Fader.Program)) {
Log.Debug($"fadar {Mixer.Current} Program is updated.");
Log.Debug($"fadar {fader.Index} Program is {fader.Program}");
}
if (e.PropertyName is nameof(Mixer.Fader.Volume)) {
Log.Debug($"fadar {Mixer.Current} Volume is updated.");
Log.Debug($"fadar {fader.Index} Volume is {fader.Volume}");
}
if (e.PropertyName is nameof(Mixer.Fader.Pan)) {
Log.Debug($"fadar {Mixer.Current} Pan is updated.");
Log.Debug($"fadar {fader.Index} Pan is {fader.Pan}");
}
}
};
Expand Down Expand Up @@ -325,24 +366,6 @@ async void stopSong() {
}
}

/// <summary>
/// a callback function to be called when the synth updated.
/// </summary>
void updateList(Synth.Track track) {
var trackIdx = track.Index - 1; // exclude conductor track;
//Log.Info($"index: {trackIdx} name: {track.Name} Voice: {Synth.GetVoice(track.Index)} Chan: {track.Channel}");
var listItem = _itemList[trackIdx];
listItem.Name = track.Name;
listItem.Instrument = Synth.GetVoice(track.Index);
listItem.Channel = track.Channel.ToString();

// update a fader.
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;
}

/// <summary>
/// refresh the view in a few seconds.
/// </summary>
Expand All @@ -368,35 +391,6 @@ void initializeListItem() {
});
}

/// <summary>
/// load the current fader value to control value.
/// </summary>
void loadFader() {
saveFader();
var fader = Mixer.GetCurrent();
_textViewNo.Text = (Mixer.Current + 1).ToString(); // mixer is 0 base.
ListItem listItem = _itemListView.GetItemAtPosition(Mixer.Current).Cast<ListItem>();
_textViewChannel.Text = listItem.Channel;
_numberPickerProg.Value = fader.Program;
_numberPickerPan.Value = fader.Pan;
_numberPickerVol.Value = fader.Volume;
_checkBoxMute.Checked = !fader.Sounds;
}

/// <summary>
/// save control value as the previous fader value.
/// </summary>
void saveFader() {
var fader = Mixer.GetPrevious();
if (int.TryParse(_textViewChannel.Text, out int channel)) {
fader.Channel = channel; // MEMO:
fader.Program = _numberPickerProg.Value;
fader.Pan = _numberPickerPan.Value;
fader.Volume = _numberPickerVol.Value;
fader.Sounds = !_checkBoxMute.Checked;
}
}

/// <summary>
/// show memory information to log.
/// FIXME: delete
Expand Down
50 changes: 33 additions & 17 deletions MidiPlayer.FluidSynth/Synth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class Synth {
const int PAN_MSB = 10;

const int MUTE_VOLUME = 0;
const int TO_ONE_BASED = 1;

///////////////////////////////////////////////////////////////////////////////////////////////
// static Fields [nouns, noun phrases]
Expand All @@ -59,7 +60,7 @@ public class Synth {

static Action _onEnded;

static Action<object, PropertyChangedEventArgs> _onUpdated;
static PropertyChangedEventHandler _onUpdated;

static string _soundFontPath = string.Empty;

Expand Down Expand Up @@ -170,7 +171,7 @@ public static event Action Ended {
remove => _onEnded -= value;
}

public static event Action<object, PropertyChangedEventArgs> Updated {
public static event PropertyChangedEventHandler Updated {
add => _onUpdated += value;
remove => _onUpdated -= value;
}
Expand Down Expand Up @@ -207,9 +208,6 @@ public static void Init() {
return;
}
Multi.StandardMidiFile = _standardMidiFile;
Enumerable.Range(MIDI_TRACK_BASE, MIDI_TRACK_COUNT).ToList().ForEach(x => {
Multi.GetBy(x).PropertyChanged += onPropertyChanged;
});
int result = fluid_player_add(_player, MidiFilePath);
if (result == FLUID_FAILED) {
Log.Error("failed to load the midi file.");
Expand Down Expand Up @@ -439,16 +437,17 @@ internal static Track GetBy(int index) {
static void init() {
_trackMap.Clear();
Enumerable.Range(MIDI_TRACK_BASE, MIDI_TRACK_COUNT).ToList().ForEach(x => _trackMap.Add(x, new Track(x)));
_trackMap[0].Name = _standardMidiFile.GetTrackName(0); // a song name.
Enumerable.Range(MIDI_TRACK_BASE, MIDI_TRACK_COUNT).ToList().ForEach(x => GetBy(x).Updated += onPropertyChanged);
var list = _standardMidiFile.MidiChannelList;
_trackMap[0].Name = _standardMidiFile.GetTrackName(0);
for (var idx = 0; idx < MidiChannelList.Count; idx++) {
_trackMap[idx + 1].Channel = list[idx]; // exclude conductor track;
_trackMap[idx + 1].Name = _standardMidiFile.GetTrackName(idx + 1);
}
}
}

public class Track : INotifyPropertyChanged {
public class Track {
#nullable enable

///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -478,48 +477,65 @@ internal Track(int index) {
}

///////////////////////////////////////////////////////////////////////////////////////////
// Events [verb, verb phrase]
//internal Events [verb, verb phrase]

/// <summary>
/// implementation for INotifyPropertyChanged
/// </summary>
public event PropertyChangedEventHandler? PropertyChanged;
internal event PropertyChangedEventHandler? Updated;

///////////////////////////////////////////////////////////////////////////////////////////
// Properties [noun, noun phrase, adjective]

/// <summary>
/// a track index value of an smf file.
/// </summary>
public int Index {
get => _index;
set {
_index = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Index)));
Updated?.Invoke(this, new(nameof(Index)));
}
}

/// <summary>
/// a track index value of an smf file exclude conductor track.
/// </summary>
public int IndexWithExcludingConductor {
get => Index - 1;
}

public bool Sounds {
get => _sounds;
set {
_sounds = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Sounds)));
Updated?.Invoke(this, new(nameof(Sounds)));
}
}

public string Name {
get => _name;
set {
_name = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
Updated?.Invoke(this, new(nameof(Name)));
}
}

public int Channel {
get => _channel;
set {
_channel = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Channel)));
Updated?.Invoke(this, new(nameof(Channel)));
}
}

/// <summary>
/// a midi channel number of a track as one-based value.
/// </summary>
public int ChannelAsOneBased {
get => Channel + TO_ONE_BASED;
}

public int Bank {
get {
if (_channel == 9 && _bank != 128) {
Expand All @@ -529,31 +545,31 @@ public int Bank {
}
set {
_bank = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Bank)));
Updated?.Invoke(this, new(nameof(Bank)));
}
}

public int Program {
get => _program;
set {
_program = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Program)));
Updated?.Invoke(this, new(nameof(Program)));
}
}

public int Volume {
get => _volume;
set {
_volume = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Volume)));
Updated?.Invoke(this, new(nameof(Volume)));
}
}

public int Pan {
get => _pan;
set {
_pan = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Pan)));
Updated?.Invoke(this, new(nameof(Pan)));
}
}
}
Expand Down
Loading

0 comments on commit f4c29ce

Please sign in to comment.