Skip to content

Commit

Permalink
Merge pull request #61 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 2, 2022
2 parents 0e0abba + b155dbc commit 43d72e2
Show file tree
Hide file tree
Showing 17 changed files with 853 additions and 853 deletions.
440 changes: 220 additions & 220 deletions MidiPlayer.Droid/MainActivity.Component.cs

Large diffs are not rendered by default.

168 changes: 84 additions & 84 deletions MidiPlayer.Droid/MainActivity.cs

Large diffs are not rendered by default.

286 changes: 143 additions & 143 deletions MidiPlayer.FluidSynth/Synth.cs

Large diffs are not rendered by default.

84 changes: 42 additions & 42 deletions MidiPlayer.Midi/StandardMidiFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ public class StandardMidiFile {
///////////////////////////////////////////////////////////////////////////////////////////////
// Fields [nouns, noun phrases]

Sequence sequence;
Sequence _sequence;

Map<int, (string name, int channel)> nameAndMidiChannelMap;
Map<int, (string name, int channel)> _nameAndMidiChannelMap;

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

public StandardMidiFile(string target) {
sequence = new Sequence();
sequence.Format = 1;
sequence.Load(target);
Map<int, (string name, int channel)> _nameAndMidiChannelMap;
_nameAndMidiChannelMap = new Map<int, (string name, int channel)>();
Enumerable.Range(0, sequence.Count).ToList().ForEach(x => {
_nameAndMidiChannelMap.Add(x, getTrackNameAndMidiChannel(x));
});
_sequence = new Sequence();
_sequence.Format = 1;
_sequence.Load(target);
Map<int, (string name, int channel)> nameAndMidiChannelMap;
nameAndMidiChannelMap = new Map<int, (string name, int channel)>();
var _idx = 0;
_nameAndMidiChannelMap.ToList().ForEach(x => {
Enumerable.Range(0, _sequence.Count).ToList().ForEach(x => {
nameAndMidiChannelMap.Add(x, getTrackNameAndMidiChannel(x));
});
this._nameAndMidiChannelMap = new Map<int, (string name, int channel)>();
var idx = 0;
nameAndMidiChannelMap.ToList().ForEach(x => {
if (!x.Value.name.Equals("System Setup") && !(x.Value.name.Equals("") && x.Value.channel == -1)) { // no need track
nameAndMidiChannelMap.Add(_idx++, x.Value);
this._nameAndMidiChannelMap.Add(idx++, x.Value);
}
});
}
Expand All @@ -42,59 +42,59 @@ public StandardMidiFile(string target) {
// Properties [noun, noun phrase, adjective]

public int TrackCount {
get => nameAndMidiChannelMap.Where(x => x.Value.channel != -1).Count(); // exclude conductor track;
get => _nameAndMidiChannelMap.Where(x => x.Value.channel != -1).Count(); // exclude conductor track;
}

public List<int> MidiChannelList {
get => nameAndMidiChannelMap.Where(x => x.Value.channel != -1).Select(x => x.Value.channel).ToList();
get => _nameAndMidiChannelMap.Where(x => x.Value.channel != -1).Select(x => x.Value.channel).ToList();
}

///////////////////////////////////////////////////////////////////////////////////////////////
// public Methods [verb, verb phrases]

public string GetTrackName(int track) {
return nameAndMidiChannelMap[track].name;
public string GetTrackName(int trackIndex) {
return _nameAndMidiChannelMap[trackIndex].name;
}

///////////////////////////////////////////////////////////////////////////////////////////////
// private Methods [verb, verb phrases]

string getTrackName(int track) {
var _trackName = "undefined";
var _track = sequence[track];
for (var _idx = 0; _idx < _track.Count; _idx++) {
var _evt = _track.GetMidiEvent(_idx);
var _msg = _evt.MidiMessage;
if (_msg.MessageType == MessageType.Meta) {
var _metaMsg = (MetaMessage) _msg;
if (_metaMsg.MetaType == MetaType.TrackName) {
var _data = _metaMsg.GetBytes();
var _text = Encoding.UTF8.GetString(_data);
_trackName = _text;
string getTrackName(int trackIndex) {
var trackName = "undefined";
var track = _sequence[trackIndex];
for (var idx = 0; idx < track.Count; idx++) {
var evt = track.GetMidiEvent(idx);
var msg = evt.MidiMessage;
if (msg.MessageType == MessageType.Meta) {
var metaMsg = (MetaMessage) msg;
if (metaMsg.MetaType == MetaType.TrackName) {
var data = metaMsg.GetBytes();
var text = Encoding.UTF8.GetString(data);
trackName = text;
break;
}
}
}
return _trackName;
return trackName;
}

int getMidiChannel(int track) {
var _channel = -1; // conductor track gets -1;
var _track = sequence[track];
for (var _idx = 0; _idx < _track.Count; _idx++) {
var _evt = _track.GetMidiEvent(_idx);
var _msg = _evt.MidiMessage;
if (_msg.MessageType == MessageType.Channel) {
var _chanMsg = (ChannelMessage) _msg;
_channel = _chanMsg.MidiChannel;
int getMidiChannel(int trackIndex) {
var channel = -1; // conductor track gets -1;
var track = _sequence[trackIndex];
for (var idx = 0; idx < track.Count; idx++) {
var evt = track.GetMidiEvent(idx);
var msg = evt.MidiMessage;
if (msg.MessageType == MessageType.Channel) {
var chanMsg = (ChannelMessage) msg;
channel = chanMsg.MidiChannel;
break;
}
}
return _channel;
return channel;
}

(string name, int channel) getTrackNameAndMidiChannel(int track) {
return (getTrackName(track), getMidiChannel(track));
(string name, int channel) getTrackNameAndMidiChannel(int trackIndex) {
return (getTrackName(trackIndex), getMidiChannel(trackIndex));
}
}
}
36 changes: 18 additions & 18 deletions MidiPlayer.SoundFont/SoundFont .cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ public class SoundFontInfo {
///////////////////////////////////////////////////////////////////////////////////////////////
// Fields [nouns, noun phrases]

NAudio.SoundFont.SoundFont soundFont;
NAudio.SoundFont.SoundFont _soundFont;

Map<int, List<Voice>> map;
Map<int, List<Voice>> _map;

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

public SoundFontInfo(string target) {
try {
soundFont = new NAudio.SoundFont.SoundFont(target);
var _map = new Map<int, List<Voice>>();
soundFont.Presets.ToList().ForEach(x => {
if (!_map.ContainsKey(x.Bank)) {
var _newList = new List<Voice>();
_newList.Add(new Voice() { Prog = x.PatchNumber, Name = x.Name });
_map.Add(x.Bank, _newList); // new bank and new voice
_soundFont = new NAudio.SoundFont.SoundFont(target);
var map = new Map<int, List<Voice>>();
_soundFont.Presets.ToList().ForEach(x => {
if (!map.ContainsKey(x.Bank)) {
var newList = new List<Voice>();
newList.Add(new Voice() { Prog = x.PatchNumber, Name = x.Name });
map.Add(x.Bank, newList); // new bank and new voice
} else {
_map[x.Bank].Add(new Voice() { Prog = x.PatchNumber, Name = x.Name }); // exists bank and new voice
map[x.Bank].Add(new Voice() { Prog = x.PatchNumber, Name = x.Name }); // exists bank and new voice
}
});
map = new Map<int, List<Voice>>();
_map.OrderBy(x => x.Key).ToList().ForEach(x => { // sort bank
map.Add(x.Key, x.Value.OrderBy(_x => _x.Prog).ToList()); // sort prog
this._map = new Map<int, List<Voice>>();
map.OrderBy(x => x.Key).ToList().ForEach(x => { // sort bank
this._map.Add(x.Key, x.Value.OrderBy(_x => _x.Prog).ToList()); // sort prog
});
} catch (Exception ex) {
Log.Error(ex.Message);
Expand All @@ -45,12 +45,12 @@ public SoundFontInfo(string target) {
// public Methods [verb, verb phrases]

public string GetVoice(int bank, int prog) {
var _voice = map[bank];
var _result = _voice.Where(x => x.Prog == prog);
if (_result.Count() == 0) {
return map[0].Where(x => x.Prog == prog).First().Name; // return default bank's voice.
var voice = _map[bank];
var result = voice.Where(x => x.Prog == prog);
if (result.Count() == 0) {
return _map[0].Where(x => x.Prog == prog).First().Name; // return default bank's voice.
} else {
return _voice.Where(x => x.Prog == prog).First().Name;
return voice.Where(x => x.Prog == prog).First().Name;
}
}

Expand Down
112 changes: 56 additions & 56 deletions MidiPlayer.Win64/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 43d72e2

Please sign in to comment.