Skip to content

Commit

Permalink
Merge pull request #77 from hiroxpepe/develop
Browse files Browse the repository at this point in the history
refactor: #13 keeping codes clean.
  • Loading branch information
hiroxpepe authored Mar 12, 2022
2 parents 04468f9 + 4bac36b commit 479f592
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 41 deletions.
2 changes: 2 additions & 0 deletions MidiPlayer.Droid/ListViewAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ListTitle {
/// an Adapter class for ListView.
/// </summary>
public class ListTitleAdapter : ArrayAdapter<ListTitle> {
#nullable enable

///////////////////////////////////////////////////////////////////////////////////////////////
// Constructor
Expand Down Expand Up @@ -72,6 +73,7 @@ public class ListItem {
/// an Adapter class for ListView.
/// </summary>
public class ListItemAdapter : ArrayAdapter<ListItem> {
#nullable enable

///////////////////////////////////////////////////////////////////////////////////////////////
// Constructor
Expand Down
77 changes: 49 additions & 28 deletions MidiPlayer.Droid/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,26 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Xamarin.Essentials;

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

///////////////////////////////////////////////////////////////////////////////////////////////
// Const [nouns]

const int VIEW_REFRESH_TIME = 2000; // msec.

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

Expand All @@ -35,29 +45,30 @@ public partial class MainActivity : AppCompatActivity {

List<ListItem> _truckList;

Task _timer;
Task _refreshTimer;

///////////////////////////////////////////////////////////////////////////////////////////////
// Constructor

public MainActivity() {
_playList = new();
_truckList = new();
_refreshTimer = createRefreshTask();
}

///////////////////////////////////////////////////////////////////////////////////////////////
// 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 All @@ -68,15 +79,7 @@ protected override void OnCreate(Bundle? savedInstanceState) {

initializeComponent();
Conf.Load();

// load previous setting.
if (Env.ExistsSoundFont && Env.ExistsMidiFile) {
Synth.SoundFontPath = Env.SoundFontPath;
Synth.MidiFilePath = Env.MidiFilePath;
Title = $"MidiPlayer: {Synth.MidiFilePath.ToFileName()} {Synth.SoundFontPath.ToFileName()}";
_soundFontPath = Env.SoundFontPath;
_midiFilePath = Env.MidiFilePath;
}
loadPreviousSetting();

/// <summary>
/// add a callback function to be called when the synth is playback.
Expand All @@ -93,25 +96,14 @@ protected override void OnCreate(Bundle? savedInstanceState) {
MainThread.BeginInvokeOnMainThread(() => {
Title = $"MidiPlayer: {Synth.MidiFilePath.ToFileName()} {Synth.SoundFontPath.ToFileName()}";
});
// refresh the viewlist in a few seconds.
_timer = new Task(async () => {
while (true) {
var truckListView = FindViewById<ListView>(Resource.Id.list_view_truck);
var listItemAdapter = (ListItemAdapter) truckListView.Adapter;
RunOnUiThread(() => {
listItemAdapter.NotifyDataSetChanged();
});
await Task.Delay(2000);
}
});
_timer.Start();
_refreshTimer.Start();
};

/// <summary>
/// add a callback function to be called when the synth ended.
/// </summary>
Synth.Ended += () => {
Log.Info("Ended called.");
Log.Info("Ended called.");
if (!_playList.Ready) {
Synth.Stop();
Synth.Start();
Expand Down Expand Up @@ -254,7 +246,20 @@ static string getActualPathBy(Intent data) {
}

/// <summary>
/// play the song.
/// load previous setting.
/// </summary>
void loadPreviousSetting() {
if (Env.ExistsSoundFont && Env.ExistsMidiFile) {
Synth.SoundFontPath = Env.SoundFontPath;
Synth.MidiFilePath = Env.MidiFilePath;
Title = $"MidiPlayer: {Synth.MidiFilePath.ToFileName()} {Synth.SoundFontPath.ToFileName()}";
_soundFontPath = Env.SoundFontPath;
_midiFilePath = Env.MidiFilePath;
}
}

/// <summary>
/// play a song.
/// </summary>
async void playSong() {
try {
Expand Down Expand Up @@ -298,6 +303,22 @@ void updateList(Synth.Track track) {
listItem.Instrument = Synth.GetVoice(track.Index);
}

/// <summary>
/// refresh the view in a few seconds.
/// </summary>
Task createRefreshTask() {
return new(async () => {
var truckListView = FindViewById<ListView>(Resource.Id.list_view_truck);
var listItemAdapter = (ListItemAdapter) truckListView.Adapter;
while (true) {
RunOnUiThread(() => {
listItemAdapter.NotifyDataSetChanged();
});
await Task.Delay(VIEW_REFRESH_TIME);
}
});
}

/// <summary>
/// show memory information to log.
/// FIXME: delete
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 479f592

Please sign in to comment.