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 12, 2022
1 parent fd2da28 commit 4bac36b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
14 changes: 10 additions & 4 deletions MidiPlayer.Droid/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -54,15 +60,15 @@ public MainActivity() {
// EventHandler

/// <summary>
/// Activity OnRequestPermissionsResult().
/// Activity OnRequestPermissionsResult.
/// </summary>
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults) {
Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

/// <summary>
/// Activity OnCreate()
/// Activity OnCreate.
/// </summary>
protected override void OnCreate(Bundle? savedInstanceState) {
base.OnCreate(savedInstanceState);
Expand Down Expand Up @@ -253,7 +259,7 @@ void loadPreviousSetting() {
}

/// <summary>
/// play the song.
/// play a song.
/// </summary>
async void playSong() {
try {
Expand Down
41 changes: 28 additions & 13 deletions MidiPlayer.FluidSynth/Synth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
});
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -312,6 +325,7 @@ static void onPropertyChanged(object sender, PropertyChangedEventArgs e) {
// inner Classes

static class Multi {
#nullable enable

///////////////////////////////////////////////////////////////////////////////////////////
// static Fields [nouns, noun phrases]
Expand Down Expand Up @@ -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;
Expand All @@ -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++) {
Expand All @@ -419,6 +433,7 @@ static void init() {
}

public class Track : INotifyPropertyChanged {
#nullable enable

///////////////////////////////////////////////////////////////////////////////////////////
// Fields [nouns, noun phrases]
Expand Down

0 comments on commit 4bac36b

Please sign in to comment.