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 17, 2022
1 parent 1d3462c commit f6053d8
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 28 deletions.
4 changes: 2 additions & 2 deletions MidiPlayer.Droid/MainActivity.EventCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ void buttonSendSynth_Click(object sender, EventArgs e) {
Log.Info($"send a data to MIDI {midiChannel} channel.");
Log.Info($"prog: {_numberPickerProg.Value} pan: {_numberPickerPan.Value} vol: {_numberPickerVol.Value}.");
Data data = new() {
Prog = _numberPickerProg.Value,
Program = _numberPickerProg.Value,
Pan = _numberPickerPan.Value,
Vol = _numberPickerVol.Value,
Volume = _numberPickerVol.Value,
Mute = _checkBoxMute.Checked
};
EventQueue.Enqueue(midiChannel, data);
Expand Down
14 changes: 7 additions & 7 deletions MidiPlayer.FluidSynth/Synth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,18 @@ static Synth() {
Multi.ApplyControlChange(channel, control, value);
}
});
Enumerable.Range(MIDI_TRACK_BASE, MIDI_TRACK_COUNT).ToList().ForEach(x => {
var eventData = EventQueue.Dequeue(x);
Enumerable.Range(MIDI_TRACK_BASE, MIDI_TRACK_COUNT).ToList().ForEach(trackIdx => {
var eventData = EventQueue.Dequeue(trackIdx);
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_program_change(_synth, trackIdx, eventData.Program);
fluid_synth_cc(_synth, trackIdx, (int) ControlChange.Pan, eventData.Pan);
if (eventData.Mute) {
fluid_synth_cc(_synth, x, (int) ControlChange.Volume, MUTE_VOLUME);
fluid_synth_cc(_synth, trackIdx, (int) ControlChange.Volume, MUTE_VOLUME);
} else {
fluid_synth_cc(_synth, x, (int) ControlChange.Volume, eventData.Vol);
fluid_synth_cc(_synth, trackIdx, (int) ControlChange.Volume, eventData.Volume);
}
Task.Run(() => {
Multi.ApplyProgramChange(x, eventData.Prog);
Multi.ApplyProgramChange(trackIdx, eventData.Program);
});
}
});
Expand Down
41 changes: 28 additions & 13 deletions MidiPlayer/EventQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public class Map<K, V> : Dictionary<K, V> {
public class EventQueue {
#nullable enable

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

const int MIDI_TRACK_BASE = 0;
const int MIDI_TRACK_COUNT = 16;

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

Expand All @@ -25,18 +31,20 @@ public class EventQueue {

static EventQueue() {
_queueMap = new();
Enumerable.Range(0, 16).ToList().ForEach(x => _queueMap.Add(x, new()));
Enumerable.Range(MIDI_TRACK_BASE, MIDI_TRACK_COUNT).ToList().ForEach(
trackIdx => _queueMap.Add(trackIdx, new())
);
}

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

public static void Enqueue(int idx, Data value) {
_queueMap[idx].Enqueue(value);
public static void Enqueue(int trackIdx, Data value) {
_queueMap[trackIdx].Enqueue(value);
}

public static Data Dequeue(int idx) {
return _queueMap[idx].Count == 0 ? null : _queueMap[idx].Dequeue();
public static Data Dequeue(int trackIdx) {
return _queueMap[trackIdx].Count == 0 ? null : _queueMap[trackIdx].Dequeue();
}
}

Expand All @@ -48,30 +56,37 @@ public class Data {
///////////////////////////////////////////////////////////////////////////////////////////////
// Properties [noun, noun phrase, adjective]

int _prog;
int _channel;

int _program;

int _pan;

int _vol;
int _volume;

bool _mute;

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

public int Prog {
get => _prog - 1;
set => _prog = value;
public int Channel {
get => _channel - 1;
set => _channel = value;
}

public int Program {
get => _program - 1;
set => _program = value;
}

public int Pan {
get => _pan - 1;
set => _pan = value;
}

public int Vol {
get => _vol - 1;
set => _vol = value;
public int Volume {
get => _volume - 1;
set => _volume = value;
}

public bool Mute {
Expand Down
117 changes: 111 additions & 6 deletions MidiPlayer/Mixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public static class Mixer {
/// <summary>
/// the current index value of the selected fader.
/// </summary>
/// <remarks>
/// base index value is 0.
/// </remarks>
static int _current;

/// <summary>
Expand Down Expand Up @@ -92,7 +95,7 @@ public static event PropertyChangedEventHandler? Updated {
/// get selected fader number.
/// </summary>
/// <remarks>
/// base is 0 value.
/// base index value is 0.
/// </remarks>
public static int Current {
get => _current;
Expand Down Expand Up @@ -122,7 +125,7 @@ public static Fader GetPrevious() {
}

/// <summary>
/// get a fader by index.
/// get a fader by 0 based index value.
/// </summary>
public static Fader GetBy(int index) {
return _mixer[index];
Expand Down Expand Up @@ -151,8 +154,11 @@ public class Fader {
// Fields [nouns, noun phrases]

/// <summary>
/// an index value of a fader.
/// a track index value of a fader.
/// </summary>
/// <remarks>
/// base index value is 0, maxim value is MIDI_TRACK_COUNT.
/// </remarks>
int _index = -1;

/// <summary>
Expand All @@ -166,28 +172,43 @@ public class Fader {
string _name = "undefined";

/// <summary>
///
/// a midi channel number of a fader.
/// </summary>
/// <remarks>
/// base index value is 0, maxim value is MIDI_TRACK_COUNT.
/// </remarks>
int _channel = -1;

/// <summary>
/// a midi bank number of a fader.
/// </summary>
int _bank = 0;
/// <remarks>
/// minimum value is 0, maxim value is 127.
/// </remarks>
int _bank = -1;

/// <summary>
/// a midi program number of a fader.
/// </summary>
/// <remarks>
/// minimum value is 0, maxim value is 127.
/// </remarks>
int _program = 0;

/// <summary>
/// a midi volume value of a fader.
/// </summary>
/// <remarks>
/// minimum value is 0, maxim value is 127.
/// </remarks>
int _volume = 104;

/// <summary>
/// a midi pan value of a fader.
/// </summary>
/// <remarks>
/// full left value is 0, center value is 64, full right value is 127.
/// </remarks>
int _pan = 64; // center

///////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -212,8 +233,11 @@ internal Fader(int index) {
// Properties [noun, noun phrase, adjective]

/// <summary>
/// an index value of a fader.
/// a track index value of a fader.
/// </summary>
/// <remarks>
/// base index is 0, maxim is MIDI_TRACK_COUNT value.
/// </remarks>
public int Index {
get => _index;
set {
Expand All @@ -224,6 +248,17 @@ public int Index {
}
}

/// <summary>
/// a track index value of a fader.
/// </summary>
/// <remarks>
/// one-based value of Index.
/// </remarks>
public int IndexAsOneBased {
get => Index + 1;
set => Index = value - 1;
}

/// <summary>
/// a value of whether the fader is on or off.
/// </summary>
Expand Down Expand Up @@ -253,6 +288,9 @@ public string Name {
/// <summary>
/// a midi channel number of a fader.
/// </summary>
/// <remarks>
/// base index value is 0, maxim value is MIDI_TRACK_COUNT.
/// </remarks>
public int Channel {
get => _channel;
set {
Expand All @@ -263,9 +301,23 @@ public int Channel {
}
}

/// <summary>
/// a midi channel number of a fader.
/// </summary>
/// <remarks>
/// one-based value of Channel.
/// </remarks>
public int ChannelAsOneBased {
get => Channel + 1;
set => Channel = value - 1;
}

/// <summary>
/// a midi bank number of a fader.
/// </summary>
/// <remarks>
/// minimum value is 0, maxim value is 127.
/// </remarks>
public int Bank {
get {
if (_channel == 9 && _bank != 128) {
Expand All @@ -281,9 +333,23 @@ public int Bank {
}
}

/// <summary>
/// a midi bank number of a fader.
/// </summary>
/// <remarks>
/// one-based value of Bank.
/// </remarks>
public int BankAsOneBased {
get => Bank + 1;
set => Bank = value - 1;
}

/// <summary>
/// a midi program number of a fader.
/// </summary>
/// <remarks>
/// minimum value is 0, maxim value is 127.
/// </remarks>
public int Program {
get => _program;
set {
Expand All @@ -294,9 +360,23 @@ public int Program {
}
}

/// <summary>
/// a midi program number of a fader.
/// </summary>
/// <remarks>
/// one-based value of Program.
/// </remarks>
public int ProgramAsOneBased {
get => Program + 1;
set => Program = value - 1;
}

/// <summary>
/// a midi volume value of a fader.
/// </summary>
/// <remarks>
/// minimum value is 0, maxim value is 127.
/// </remarks>
public int Volume {
get => _volume;
set {
Expand All @@ -307,9 +387,23 @@ public int Volume {
}
}

/// <summary>
/// a midi volume value of a fader.
/// </summary>
/// <remarks>
/// one-based value of Volume.
/// </remarks>
public int VolumeAsOneBased {
get => Volume + 1;
set => Volume = value - 1;
}

/// <summary>
/// a midi pan value of a fader.
/// </summary>
/// <remarks>
/// full left value is 0, center value is 64, full right value is 127.
/// </remarks>
public int Pan {
get => _pan;
set {
Expand All @@ -319,6 +413,17 @@ public int Pan {
}
}
}

/// <summary>
/// a midi pan value of a fader.
/// </summary>
/// <remarks>
/// one-based value of Pan.
/// </remarks>
public int PanAsOneBased {
get => Pan + 1;
set => Pan = value - 1;
}
}
}
}

0 comments on commit f6053d8

Please sign in to comment.