From 4bac36b812d528ff5467f928c06d1f71912e150b Mon Sep 17 00:00:00 2001 From: Hiroyuki Adachi Date: Sat, 12 Mar 2022 10:07:11 +0900 Subject: [PATCH] refactor: #13 keeping codes clean. --- MidiPlayer.Droid/MainActivity.cs | 14 +++++++---- MidiPlayer.FluidSynth/Synth.cs | 41 ++++++++++++++++++++++---------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/MidiPlayer.Droid/MainActivity.cs b/MidiPlayer.Droid/MainActivity.cs index a9d51a5..eb288a8 100644 --- a/MidiPlayer.Droid/MainActivity.cs +++ b/MidiPlayer.Droid/MainActivity.cs @@ -19,7 +19,13 @@ namespace MidiPlayer.Droid { - [Activity(Label = "@string/app_name", Theme = "@style/Base.Theme.MaterialComponents.Light.DarkActionBar.Bridge", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)] + [Activity( + Label = "@string/app_name", + Theme = "@style/Base.Theme.MaterialComponents.Light.DarkActionBar.Bridge", + MainLauncher = true, + ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, + ScreenOrientation = ScreenOrientation.Portrait + )] public partial class MainActivity : AppCompatActivity { #nullable enable @@ -54,7 +60,7 @@ public MainActivity() { // EventHandler /// - /// Activity OnRequestPermissionsResult(). + /// Activity OnRequestPermissionsResult. /// public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults) { Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults); @@ -62,7 +68,7 @@ public override void OnRequestPermissionsResult(int requestCode, string[] permis } /// - /// Activity OnCreate() + /// Activity OnCreate. /// protected override void OnCreate(Bundle? savedInstanceState) { base.OnCreate(savedInstanceState); @@ -253,7 +259,7 @@ void loadPreviousSetting() { } /// - /// play the song. + /// play a song. /// async void playSong() { try { diff --git a/MidiPlayer.FluidSynth/Synth.cs b/MidiPlayer.FluidSynth/Synth.cs index 55891bb..ac99c64 100644 --- a/MidiPlayer.FluidSynth/Synth.cs +++ b/MidiPlayer.FluidSynth/Synth.cs @@ -25,6 +25,19 @@ public class Synth { const float SYNTH_GAIN = 0.5f; + const int MIDI_TRACK_BASE = 0; + const int MIDI_TRACK_COUNT = 16; + + const int NOTE_ON = 144; + const int NOTE_OFF = 128; + const int PROGRAM_CHANGE = 192; + const int CONTROL_CHANGE = 176; + + const int BANK_SELECT_MSB = 0; + const int BANK_SELECT_LSB = 32; + const int VOLUME_MSB = 7; + const int PAN_MSB = 10; + /////////////////////////////////////////////////////////////////////////////////////////////// // static Fields [nouns, noun phrases] @@ -63,9 +76,9 @@ public class Synth { static Synth() { _onPlaybacking += (void_ptr data, fluid_midi_event_t evt) => { - Enumerable.Range(0, 16).ToList().ForEach(x => { + Enumerable.Range(MIDI_TRACK_BASE, MIDI_TRACK_COUNT).ToList().ForEach(x => { var eventData = EventQueue.Dequeue(x); - if (!(eventData is null)) { + if (eventData is not null) { fluid_synth_program_change(_synth, x, eventData.Prog); fluid_synth_cc(_synth, x, (int) ControlChange.Pan, eventData.Pan); fluid_synth_cc(_synth, x, (int) ControlChange.Volume, eventData.Vol); @@ -76,17 +89,17 @@ static Synth() { var control = fluid_midi_event_get_control(evt); var value = fluid_midi_event_get_value(evt); var program = fluid_midi_event_get_program(evt); - if (type != 128 && type != 144) { // not note on or note off + if (type != NOTE_ON && type != NOTE_OFF) { // not note on or note off Log.Debug($"_type: {type} _channel: {channel} _control: {control} _value: {value} _program: {program}"); } Task.Run(() => { - if (type == 144) { // NOTE_ON = 144 + if (type == NOTE_ON) { // NOTE_ON = 144 Multi.ApplyNoteOn(channel); - } else if (type == 128) { // NOTE_OFF = 128 + } else if (type == NOTE_OFF) { // NOTE_OFF = 128 Multi.ApplyNoteOff(channel); - } else if (type == 192) { // PROGRAM_CHANGE = 192 + } else if (type == PROGRAM_CHANGE) { // PROGRAM_CHANGE = 192 Multi.ApplyProgramChange(channel, program); - } else if (type == 176) { // CONTROL_CHANGE = 176 + } else if (type == CONTROL_CHANGE) { // CONTROL_CHANGE = 176 Multi.ApplyControlChange(channel, control, value); } }); @@ -185,7 +198,7 @@ public static void Init() { return; } Multi.StandardMidiFile = _standardMidiFile; - Enumerable.Range(0, 16).ToList().ForEach(x => { + Enumerable.Range(MIDI_TRACK_BASE, MIDI_TRACK_COUNT).ToList().ForEach(x => { Multi.Get(x).PropertyChanged += onPropertyChanged; }); int result = fluid_player_add(_player, MidiFilePath); @@ -312,6 +325,7 @@ static void onPropertyChanged(object sender, PropertyChangedEventArgs e) { // inner Classes static class Multi { +#nullable enable /////////////////////////////////////////////////////////////////////////////////////////// // static Fields [nouns, noun phrases] @@ -379,19 +393,19 @@ public static void ApplyControlChange(int channel, int control, int value) { // PAN_MSB = 10 // _type: 176, _control: 10, _value: 64 switch (control) { - case 0: // BANK_SELECT_MSB + case BANK_SELECT_MSB: // BANK_SELECT_MSB if (channel == 9) { // Drum _trackMap.Where(x => x.Value.Channel == channel).ToList().ForEach(x => x.Value.Bank = value + 1); // 128 } break; - case 32: // BANK_SELECT_LSB + case BANK_SELECT_LSB: // BANK_SELECT_LSB if (channel != 9) { // not Drum _trackMap.Where(x => x.Value.Channel == channel).ToList().ForEach(x => x.Value.Bank = value); } break; - case 7: // VOLUME_MSB + case VOLUME_MSB: // VOLUME_MSB break; - case 10: // PAN_MSB + case PAN_MSB: // PAN_MSB break; default: break; @@ -408,7 +422,7 @@ public static Track Get(int index) { static void init() { _trackMap.Clear(); - Enumerable.Range(0, 16).ToList().ForEach(x => _trackMap.Add(x, new Track(x))); + Enumerable.Range(MIDI_TRACK_BASE, MIDI_TRACK_COUNT).ToList().ForEach(x => _trackMap.Add(x, new Track(x))); var list = _standardMidiFile.MidiChannelList; _trackMap[0].Name = _standardMidiFile.GetTrackName(0); for (var idx = 0; idx < MidiChannelList.Count; idx++) { @@ -419,6 +433,7 @@ static void init() { } public class Track : INotifyPropertyChanged { +#nullable enable /////////////////////////////////////////////////////////////////////////////////////////// // Fields [nouns, noun phrases]