Skip to content

Commit

Permalink
v0.9.7a release
Browse files Browse the repository at this point in the history
  • Loading branch information
sfryers authored Feb 24, 2024
1 parent c62f9e6 commit d379137
Show file tree
Hide file tree
Showing 40 changed files with 10,079 additions and 8,418 deletions.
187 changes: 162 additions & 25 deletions src/MT32Editor/ConfigFile.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
using System.Reflection;

namespace MT32Edit;
namespace MT32Edit;

/// <summary>
/// Saves and load system parameters from .ini file
/// </summary>
internal static class ConfigFile
{
// MT32Edit: ConfigFile class (static)
// S.Fryers Jan 2024
// S.Fryers Feb 2024

private const string MIDI_IN = "Midi In";
private const string MIDI_OUT = "Midi Out";
private const string SHOW_CONSOLE = "Show Console";
private const string VERBOSE_MESSAGES = "Verbose messages";
private const string HARDWARE_CONNECTED = "Hardware MT-32 connected";
private const string ALLOW_RESET = "Allow MT-32 reset";
private const string SEND_MESSAGES = "Send messages to MT-32";
private const string IGNORE_SYSTEM_ON_LOAD = "Ignore system area on SysEx load";
private const string EXCLUDE_SYSTEM_ON_SAVE = "Exclude system area on SysEx save";
private const string AUTOSAVE = "Autosave every 5 mins";

private static readonly string applicationPath = Path.GetDirectoryName(Application.ExecutablePath);
private static readonly string iniFileLocation = Path.Combine(applicationPath, "MT32Edit.ini");
private static readonly string iniFileName = "MT32Edit.ini";
private static readonly string iniFileLocation = Path.Combine($"{FileTools.applicationPath}", iniFileName);

public static string[] Load()
{
string[] midiDevices = { "", "" };
string[] midiDeviceNames = { "", "" };
if (!File.Exists(iniFileLocation))
{
//return blank device names if no config file found
Console.WriteLine("ini file not found- using default MIDI devices.");
return midiDevices;
ConsoleMessage.SendLine("ini file not found- using default MIDI devices.");
return midiDeviceNames;
}

StreamReader fs = new StreamReader(iniFileLocation);
Console.WriteLine($"Loading MIDI device settings from {iniFileLocation}");
ConsoleMessage.SendLine($"Loading settings from {iniFileLocation}");
while (!fs.EndOfStream)
{
string? fileLine = fs.ReadLine();
if (fileLine == null)
if (fileLine is null)
{
continue;
}
Expand All @@ -38,21 +47,141 @@ public static string[] Load()
{
continue;
}
else if (ParseTools.LeftMost(fileLine, 7) == "Midi In")

else if (ParseTools.LeftMost(fileLine, MIDI_IN.Length).ToLower() == MIDI_IN.ToLower())
{
midiDeviceNames[0] = GetMidiDeviceName(fileLine);
}

else if (ParseTools.LeftMost(fileLine, MIDI_OUT.Length).ToLower() == MIDI_OUT.ToLower())
{
fileLine = ParseTools.RightOfChar(fileLine, '[');
fileLine = ParseTools.LeftMost(fileLine, fileLine.Length - 1);
midiDevices[0] = fileLine;
midiDeviceNames[1] = GetMidiDeviceName(fileLine);
}
else if (ParseTools.LeftMost(fileLine, 8) == "Midi Out")

else if (ParseTools.LeftMost(fileLine, SHOW_CONSOLE.Length).ToLower() == SHOW_CONSOLE.ToLower())
{
CheckConsoleSetting(fileLine);
}

else if (ParseTools.LeftMost(fileLine, VERBOSE_MESSAGES.Length).ToLower() == VERBOSE_MESSAGES.ToLower())
{
CheckVerboseMessagesSetting(fileLine);
}

else if (ParseTools.LeftMost(fileLine, HARDWARE_CONNECTED.Length).ToLower() == HARDWARE_CONNECTED.ToLower())
{
CheckHardwareConnectedSetting(fileLine);
}

else if (ParseTools.LeftMost(fileLine, ALLOW_RESET.Length).ToLower() == ALLOW_RESET.ToLower())
{
CheckAllowResetSetting(fileLine);
}

else if (ParseTools.LeftMost(fileLine, SEND_MESSAGES.Length).ToLower() == SEND_MESSAGES.ToLower())
{
fileLine = ParseTools.RightOfChar(fileLine, '[');
fileLine = ParseTools.LeftMost(fileLine, fileLine.Length - 1);
midiDevices[1] = fileLine;
CheckSendMessagesSetting(fileLine);
}

else if (ParseTools.LeftMost(fileLine, IGNORE_SYSTEM_ON_LOAD.Length).ToLower() == IGNORE_SYSTEM_ON_LOAD.ToLower())
{
CheckIgnoreOnLoadSetting(fileLine);
}

else if (ParseTools.LeftMost(fileLine, EXCLUDE_SYSTEM_ON_SAVE.Length).ToLower() == EXCLUDE_SYSTEM_ON_SAVE.ToLower())
{
CheckExcludeOnSaveSetting(fileLine);
}

else if (ParseTools.LeftMost(fileLine, AUTOSAVE.Length).ToLower() == AUTOSAVE.ToLower())
{
CheckAutoSaveSetting(fileLine);
}
}

fs.Close();
return midiDevices;
return midiDeviceNames;

string GetMidiDeviceName(string inputString)
{
string deviceName = inputString;
deviceName = ParseTools.RightOfChar(deviceName, '[');
deviceName = ParseTools.LeftMost(deviceName, deviceName.Length - 1);
return deviceName;
}

void CheckIgnoreOnLoadSetting(string fileLine)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
LoadSysExFile.ignoreSystemArea = (bool)status;
}
}

void CheckExcludeOnSaveSetting(string fileLine)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
SaveSysExFile.excludeSystemArea = (bool)status;
}
}

void CheckConsoleSetting(string fileLine)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
ConsoleMessage.SetVisibility((bool)status);
}
}

void CheckVerboseMessagesSetting(string fileLine)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
ConsoleMessage.SetVerbose((bool)status);
}
}

void CheckHardwareConnectedSetting(string fileLine)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
Midi.hardwareMT32Connected = (bool)status;
}
}

void CheckAllowResetSetting(string fileLine)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
MT32SysEx.allowReset = (bool)status;
}
}

void CheckSendMessagesSetting(string fileLine)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
MT32SysEx.sendTextToMT32 = (bool)status;
}
}


void CheckAutoSaveSetting(string fileLine)
{
bool? status = ParseTools.StringToBool(fileLine);
if (status.HasValue)
{
SaveSysExFile.autoSave = (bool)status;
}
}
}

public static void Save()
Expand All @@ -64,16 +193,24 @@ public static void Save()
{
File.Create(iniFileLocation).Dispose();
}
ConsoleMessage.SendLine($"Saving MIDI device settings to {iniFileLocation}");
ConsoleMessage.SendVerboseLine($"Saving settings to {iniFileLocation}");
StreamWriter fs = new StreamWriter(iniFileLocation, false);
fs.WriteLine("##### MT-32 Timbre Editor configuration settings #####");
fs.WriteLine("Midi In = [" + Midi.GetInputDeviceName(Midi.InDeviceIndex) + "]");
fs.WriteLine("Midi Out = [" + Midi.GetOutputDeviceName(Midi.OutDeviceIndex) + "]");
fs.WriteLine("##### MT-32 Editor configuration settings #####");
fs.WriteLine($"{MIDI_IN} = [{Midi.GetInputDeviceName(Midi.InDeviceIndex)}]");
fs.WriteLine($"{MIDI_OUT} = [{Midi.GetOutputDeviceName(Midi.OutDeviceIndex)}]");
fs.WriteLine($"{SHOW_CONSOLE} = {ConsoleMessage.Visible()}");
fs.WriteLine($"{VERBOSE_MESSAGES} = {ConsoleMessage.Verbose()}");
fs.WriteLine($"{HARDWARE_CONNECTED} = {Midi.hardwareMT32Connected}");
fs.WriteLine($"{ALLOW_RESET} = {MT32SysEx.allowReset}");
fs.WriteLine($"{SEND_MESSAGES} = {MT32SysEx.sendTextToMT32}");
fs.WriteLine($"{IGNORE_SYSTEM_ON_LOAD} = {LoadSysExFile.ignoreSystemArea}");
fs.WriteLine($"{EXCLUDE_SYSTEM_ON_SAVE} = {SaveSysExFile.excludeSystemArea}");
fs.WriteLine($"{AUTOSAVE} = {SaveSysExFile.autoSave}");
fs.Close();
}
catch
{
MessageBox.Show("Couldn't create MT32Edit.ini: Check that you have read/write access to the folder that MT32Edit.exe is running from. MIDI device settings have not been saved.", "MT-32 Editor");
MessageBox.Show($"Unable to create {iniFileName}.{Environment.NewLine}{Environment.NewLine}Check that you have read/write access to the folder that the application is running from ({FileTools.applicationPath}){Environment.NewLine}{Environment.NewLine}MIDI device settings have not been saved.", "MT-32 Editor");
}
}
}
68 changes: 52 additions & 16 deletions src/MT32Editor/ConsoleMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,77 @@
internal static class ConsoleMessage
{
// MT32Edit: ConsoleMessage class (static)
// S.Fryers Apr 2023
private static bool consoleEnabled = false;
// S.Fryers Jan 2024
private static bool verboseEnabled = false; //Determines whether messages are sent to console.
private static bool consoleVisible = false; //Determines whether entire console is visible or not.

public static void Enable()
public static void EnableVerbose()
{
consoleEnabled = true;
verboseEnabled = true;
}

public static void Disable()
public static void DisableVerbose()
{
consoleEnabled = false;
verboseEnabled = false;
}

public static bool Enabled()
public static bool Verbose()
{
return consoleEnabled;
return verboseEnabled;
}

public static void SetVerbose(bool state)
{
verboseEnabled = state;
}

public static void Show()
{
consoleVisible = true;
}

public static void Hide()
{
consoleVisible = false;
}

public static bool Visible()
{
return consoleVisible;
}

public static void SetVisibility(bool state)
{
consoleVisible = state;
}

public static void SendString(string message, ConsoleColor color = ConsoleColor.Gray)
{
if (consoleEnabled)
Console.ForegroundColor = color;
Console.Write(message);
Console.ForegroundColor = ConsoleColor.Gray;
}

public static void SendVerboseString(string message, ConsoleColor color = ConsoleColor.Gray)
{
if (verboseEnabled)
{
Console.ForegroundColor = color;
Console.Write(message);
Console.ForegroundColor = ConsoleColor.Gray;
SendString(message, color);
}
}

public static void SendLine(string message, ConsoleColor color = ConsoleColor.Gray)
{
if (consoleEnabled)
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ForegroundColor = ConsoleColor.Gray;
}

public static void SendVerboseLine(string message, ConsoleColor color = ConsoleColor.Gray)
{
if (verboseEnabled)
{
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ForegroundColor = ConsoleColor.Gray;
SendLine(message, color);
}
}
}
44 changes: 44 additions & 0 deletions src/MT32Editor/DrawingTools.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace MT32Edit;

/// <summary>
/// Custom drawing tools for MT-32 Editor UI
/// </summary>
internal class DrawingTools
{
// MT32Edit: DrawingTools class
// S.Fryers Feb 2024

public void DrawStructureList(DrawItemEventArgs e, bool isPartial12, bool droppedDown, float UIScale)
{
//custom comboBox draw method- creates vertical divider between structure type and structure description
if (e.Index < 0)
{
return;
}

e.DrawBackground();
string partialConfigType = $"{e.Index + 1}: {MT32Strings.partialConfig[e.Index]}";
string partialConfigDescription;
if (isPartial12)
{
partialConfigDescription = MT32Strings.partialConfig12Desc[e.Index];
}
else
{
partialConfigDescription = MT32Strings.partialConfig34Desc[e.Index];
}

int xLeft = e.Bounds.Location.X;
int xMid = (int)(58 * UIScale);
int yTop = e.Bounds.Location.Y;
int yBottom = yTop + e.Bounds.Height;

TextRenderer.DrawText(e.Graphics, partialConfigType, e.Font, new Point(xLeft, yTop), e.ForeColor);
if (droppedDown)
{
e.Graphics.DrawLine(SystemPens.ButtonFace, xMid, yTop, xMid, yBottom);
TextRenderer.DrawText(e.Graphics, partialConfigDescription, e.Font, new Point(xMid + 5, yTop), e.ForeColor, TextFormatFlags.Left);
e.DrawFocusRectangle();
}
}
}
Loading

0 comments on commit d379137

Please sign in to comment.