Add proper CLI
This commit is contained in:
parent
564b649204
commit
0408e2a536
6 changed files with 304 additions and 33 deletions
13
MusicBoxConverter/Help.txt
Normal file
13
MusicBoxConverter/Help.txt
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
MusicBoxConverter
|
||||||
|
by Starbeamrainbowlabs
|
||||||
|
|
||||||
|
Converts MIDI files to SVG music box strips.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
mono [--debug] MusicBoxConverter.exe {options}
|
||||||
|
|
||||||
|
Options:
|
||||||
|
-h --help Shows this help message.
|
||||||
|
-i --input {filename} Specifies the input midi file to convert.
|
||||||
|
-o --output {filename} Specified the file path to output the SVG music box strip to.
|
||||||
|
--debug Activates additional debugging output.
|
|
@ -30,10 +30,10 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="Melanchall.DryWetMidi">
|
|
||||||
<HintPath>..\packages\Melanchall.DryWetMidi.2.0.0\lib\net461\Melanchall.DryWetMidi.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
|
<Reference Include="Melanchall.DryWetMidi">
|
||||||
|
<HintPath>..\packages\Melanchall.DryWetMidi.2.0.1\lib\net45\Melanchall.DryWetMidi.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
|
@ -43,12 +43,16 @@
|
||||||
<Compile Include="Utilities\Vector2.cs" />
|
<Compile Include="Utilities\Vector2.cs" />
|
||||||
<Compile Include="MusicBox.cs" />
|
<Compile Include="MusicBox.cs" />
|
||||||
<Compile Include="Utilities\NoteUtilities.cs" />
|
<Compile Include="Utilities\NoteUtilities.cs" />
|
||||||
|
<Compile Include="Utilities\EmbeddedFiles.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Utilities\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Utilities\" />
|
<EmbeddedResource Include="Help.txt" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
</Project>
|
</Project>
|
|
@ -10,10 +10,12 @@ namespace MusicBoxConverter
|
||||||
{
|
{
|
||||||
public class MusicBoxScoreGenerator
|
public class MusicBoxScoreGenerator
|
||||||
{
|
{
|
||||||
public Vector2 offset { get; set; } = new Vector2(10, 10);
|
public bool Debug { get; set; } = false;
|
||||||
public Vector2 scaleFactor { get; set; } = new Vector2(0.03f, 4f);
|
|
||||||
|
|
||||||
public float holeSize { get; set; } = 1f;
|
public Vector2 Offset { get; set; } = new Vector2(10, 10);
|
||||||
|
public Vector2 ScaleFactor { get; set; } = new Vector2(0.03f, 4f);
|
||||||
|
|
||||||
|
public float HoleSize { get; set; } = 1f;
|
||||||
|
|
||||||
private int trackLength;
|
private int trackLength;
|
||||||
public int TrackLength
|
public int TrackLength
|
||||||
|
@ -28,40 +30,41 @@ namespace MusicBoxConverter
|
||||||
|
|
||||||
public int MaxNoteNumber {
|
public int MaxNoteNumber {
|
||||||
get {
|
get {
|
||||||
return MusicBox.HighestNote.NoteNumber;
|
return SelectedMusicBox.HighestNote.NoteNumber;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public int MinNoteNumber {
|
public int MinNoteNumber {
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return MusicBox.LowestNote.NoteNumber;
|
return SelectedMusicBox.LowestNote.NoteNumber;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public MusicBox MusicBox { get; private set; }
|
public MusicBox SelectedMusicBox { get; private set; }
|
||||||
MidiFile midiFile;
|
|
||||||
|
private MidiFile midiFile;
|
||||||
|
|
||||||
public MusicBoxScoreGenerator(string filename, MusicBox inMusicBox)
|
public MusicBoxScoreGenerator(string filename, MusicBox inMusicBox)
|
||||||
{
|
{
|
||||||
MusicBox = inMusicBox;
|
SelectedMusicBox = inMusicBox;
|
||||||
midiFile = MidiFile.Read(filename);
|
midiFile = MidiFile.Read(filename);
|
||||||
|
|
||||||
// Set the scale factor based on the strip height of the music box
|
// Set the scale factor based on the strip height of the music box
|
||||||
scaleFactor = new Vector2(
|
ScaleFactor = new Vector2(
|
||||||
scaleFactor.X,
|
ScaleFactor.X,
|
||||||
MusicBox.StripHeightMm / (MusicBox.NoteCount-1)
|
SelectedMusicBox.StripHeightMm / (SelectedMusicBox.NoteCount-1)
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach(Note note in AllNotes()) {
|
foreach(Note note in AllNotes()) {
|
||||||
if (!MusicBox.IsValidNote(note))
|
if (!SelectedMusicBox.IsValidNote(note))
|
||||||
Console.Error.WriteLine($"Warning: The note {note} at {note.Time} can't be played by the {MusicBox}.");
|
Console.Error.WriteLine($"Warning: The note {note} at {note.Time} can't be played by the {SelectedMusicBox}.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Output(string destinationFilename)
|
public void Output(string destinationFilename)
|
||||||
{
|
{
|
||||||
Vector2 area = new Vector2(TrackLength, MusicBox.NoteCount-1).Multiply(scaleFactor);
|
Vector2 area = new Vector2(TrackLength, SelectedMusicBox.NoteCount-1).Multiply(ScaleFactor);
|
||||||
Vector2 size = area.Add(offset.Multiply(2));
|
Vector2 size = area.Add(Offset.Multiply(2));
|
||||||
|
|
||||||
SvgWriter svg = new SvgWriter(
|
SvgWriter svg = new SvgWriter(
|
||||||
destinationFilename,
|
destinationFilename,
|
||||||
|
@ -69,23 +72,32 @@ namespace MusicBoxConverter
|
||||||
);
|
);
|
||||||
svg.UnitSuffix = "mm";
|
svg.UnitSuffix = "mm";
|
||||||
|
|
||||||
for(float i = 0; i < area.Y; i += scaleFactor.Y)
|
for(float i = 0; i < area.Y; i += ScaleFactor.Y)
|
||||||
{
|
{
|
||||||
Vector2 start = offset.Add(new Vector2(0, i));
|
Vector2 start = Offset.Add(new Vector2(0, i));
|
||||||
svg.WriteLine(start, start.Add(new Vector2(area.X, 0)), "darkgreen", 0.75f);
|
svg.WriteLine(start, start.Add(new Vector2(area.X, 0)), "darkgreen", 0.75f);
|
||||||
}
|
}
|
||||||
|
|
||||||
svg.WriteRectangle(offset, area, "red", 0.75f);
|
svg.WriteRectangle(Offset, area, "red", 0.75f);
|
||||||
|
|
||||||
foreach(Note note in AllNotes())
|
foreach(Note note in AllNotes())
|
||||||
{
|
{
|
||||||
Console.WriteLine("[{0}] {1}{2}/{3}", note.Time, note.NoteName, note.Octave, note.NoteNumber);
|
if(Debug) {
|
||||||
|
Console.WriteLine(
|
||||||
|
"[Note] {0}: {1}{2}/{3}",
|
||||||
|
note.Time,
|
||||||
|
note.NoteName,
|
||||||
|
note.Octave,
|
||||||
|
note.NoteNumber
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
svg.WriteCircle(
|
svg.WriteCircle(
|
||||||
new Vector2(
|
new Vector2(
|
||||||
offset.X + note.Time * scaleFactor.X,
|
Offset.X + note.Time * ScaleFactor.X,
|
||||||
offset.Y + ((MusicBox.NoteCount-1) - MusicBox.NoteToBoxNumber(note)) * scaleFactor.Y
|
Offset.Y + ((SelectedMusicBox.NoteCount-1) - SelectedMusicBox.NoteToBoxNumber(note)) * ScaleFactor.Y
|
||||||
),
|
),
|
||||||
holeSize // radius
|
HoleSize // radius
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,19 +1,72 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Reflection;
|
|
||||||
using Melanchall.DryWetMidi.Common;
|
using Melanchall.DryWetMidi.Common;
|
||||||
|
|
||||||
|
using SBRL.Utilities;
|
||||||
|
|
||||||
namespace MusicBoxConverter
|
namespace MusicBoxConverter
|
||||||
{
|
{
|
||||||
class MainClass
|
class MainClass
|
||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static int Main(string[] args)
|
||||||
{
|
{
|
||||||
|
string inputFilename = "";
|
||||||
|
string outputFilename = "";
|
||||||
|
bool debug = false;
|
||||||
|
|
||||||
|
for(int i = 0; i < args.Length; i++)
|
||||||
|
{
|
||||||
|
switch(args[i])
|
||||||
|
{
|
||||||
|
case "-i":
|
||||||
|
case "--input":
|
||||||
|
inputFilename = args[++i];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "-o":
|
||||||
|
case "--output":
|
||||||
|
outputFilename = args[++i];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "--auto-dev":
|
||||||
|
inputFilename = "/home/sbrl/Music/Sheets/HappyBirthday.midi";
|
||||||
|
//inputFilename = "/tmp/Scale.midi";
|
||||||
|
|
||||||
|
outputFilename = "/tmp/test.svg";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "--debug":
|
||||||
|
debug = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "-h":
|
||||||
|
case "--help":
|
||||||
|
Console.Error.WriteLine(EmbeddedFiles.ReadAllText("MusicBoxConverter.Help.txt"));
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
default:
|
||||||
|
Console.Error.WriteLine("Error: Unknown option {0}.", args[i]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(inputFilename == string.Empty) {
|
||||||
|
Console.Error.WriteLine("Error: No input file specified.");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if(outputFilename == string.Empty) {
|
||||||
|
Console.Error.WriteLine("Error: No output file specified.");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
MusicBoxScoreGenerator converter = new MusicBoxScoreGenerator(
|
MusicBoxScoreGenerator converter = new MusicBoxScoreGenerator(
|
||||||
//"/home/sbrl/Music/Sheets/HappyBirthday.midi",
|
inputFilename,
|
||||||
"/tmp/Scale.midi",
|
|
||||||
MusicBox.Note30
|
MusicBox.Note30
|
||||||
);
|
);
|
||||||
converter.Output("/tmp/test.svg");
|
converter.Debug = debug;
|
||||||
|
converter.Output(outputFilename);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
189
MusicBoxConverter/Utilities/EmbeddedFiles.cs
Normal file
189
MusicBoxConverter/Utilities/EmbeddedFiles.cs
Normal file
|
@ -0,0 +1,189 @@
|
||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace SBRL.Utilities
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A collection of static methods for manipulating embedded resources.
|
||||||
|
/// </summary>
|
||||||
|
/// <description>
|
||||||
|
/// v0.5, by Starbeamrainbowlabs <feedback@starbeamrainbowlabs.com>
|
||||||
|
/// Last updated 8th August 2016.
|
||||||
|
/// Licensed under MPL-2.0.
|
||||||
|
///
|
||||||
|
/// Changelog:
|
||||||
|
/// v0.1 (25th July 2016):
|
||||||
|
/// - Initial release.
|
||||||
|
/// v0.2 (8th August 2016):
|
||||||
|
/// - Changed namespace.
|
||||||
|
/// v0.3 (21st January 2017):
|
||||||
|
/// - Added GetRawReader().
|
||||||
|
/// v0.4 (8th April 2017):
|
||||||
|
/// - Removed unnecessary using statement.
|
||||||
|
/// v0.5 (3rd September 2017):
|
||||||
|
/// - Changed namespace
|
||||||
|
/// </description>
|
||||||
|
public static class EmbeddedFiles
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// An array of the filenames of all the resources embedded in the calling assembly.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The resource list.</value>
|
||||||
|
public static string[] ResourceList {
|
||||||
|
get {
|
||||||
|
return Assembly.GetCallingAssembly().GetManifestResourceNames();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetResourceListText()
|
||||||
|
{
|
||||||
|
StringWriter result = new StringWriter();
|
||||||
|
result.WriteLine("Files embedded in {0}:", Assembly.GetCallingAssembly().GetName().Name);
|
||||||
|
foreach(string filename in ResourceList)
|
||||||
|
result.WriteLine(" - {0}", filename);
|
||||||
|
return result.ToString();
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Writes a list of embedded resources to the Console's standard output.
|
||||||
|
/// </summary>
|
||||||
|
public static void WriteResourceList()
|
||||||
|
{
|
||||||
|
Console.WriteLine(GetResourceListText());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a StreamReader attached to the specified embedded resource.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">The filename of the embedded resource to get a StreamReader of.</param>
|
||||||
|
/// <returns>A StreamReader attached to the specified embedded resource.</returns>
|
||||||
|
public static StreamReader GetReader(string filename)
|
||||||
|
{
|
||||||
|
return new StreamReader(GetRawReader(filename));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a raw Stream that's attached to the specified embedded resource.
|
||||||
|
/// Useful when you want to copy an embedded resource to some other stream.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">The path to the embedded resource.</param>
|
||||||
|
/// <returns>A raw Stream object attached to the specified file..</returns>
|
||||||
|
public static Stream GetRawReader(string filename)
|
||||||
|
{
|
||||||
|
return Assembly.GetCallingAssembly().GetManifestResourceStream(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the specified embedded resource's content as a byte array.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">The filename of the embedded resource to get conteent of.</param>
|
||||||
|
/// <returns>The specified embedded resource's content as a byte array.</returns>
|
||||||
|
public static byte[] ReadAllBytes(string filename)
|
||||||
|
{
|
||||||
|
// Referencing the Result property will block until the async method completes
|
||||||
|
return ReadAllBytesAsync(filename).Result;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the specified embedded resource's content as a byte array asynchronously.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">The filename of the embedded resource to get conteent of.</param>
|
||||||
|
/// <returns>The specified embedded resource's content as a byte array.</returns>
|
||||||
|
public static async Task<byte[]> ReadAllBytesAsync(string filename)
|
||||||
|
{
|
||||||
|
using(Stream resourceStream = Assembly.GetCallingAssembly().GetManifestResourceStream(filename))
|
||||||
|
using(MemoryStream temp = new MemoryStream())
|
||||||
|
{
|
||||||
|
await resourceStream.CopyToAsync(temp);
|
||||||
|
return temp.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets all the text stored in the specified embedded resource.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">The filename to fetch the content of.</param>
|
||||||
|
/// <returns>All the text stored in the specified embedded resource.</returns>
|
||||||
|
public static string ReadAllText(string filename)
|
||||||
|
{
|
||||||
|
using(StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
|
||||||
|
{
|
||||||
|
return resourceReader.ReadToEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Gets all the text stored in the specified embedded resource asynchronously.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">The filename to fetch the content of.</param>
|
||||||
|
/// <returns>All the text stored in the specified embedded resource.</returns>
|
||||||
|
public static async Task<string> ReadAllTextAsync(string filename)
|
||||||
|
{
|
||||||
|
using(StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
|
||||||
|
{
|
||||||
|
return await resourceReader.ReadToEndAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Enumerates the lines of text in the specified embedded resource.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">The filename of the embedded resource to enumerate.</param>
|
||||||
|
/// <returns>An IEnumerator that enumerates the specified embedded resource.</returns>
|
||||||
|
public static IEnumerator<string> EnumerateLines(string filename)
|
||||||
|
{
|
||||||
|
using(StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
|
||||||
|
{
|
||||||
|
string nextLine;
|
||||||
|
while((nextLine = resourceReader.ReadLine()) != null)
|
||||||
|
{
|
||||||
|
yield return nextLine;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Enumerates the lines of text in the specified embedded resource asynchronously.
|
||||||
|
/// Each successive call returns a task that, when complete, returns the next line of text stored
|
||||||
|
/// in the embedded resource.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">The filename of the embedded resource to enumerate.</param>
|
||||||
|
/// <returns>An IEnumerator that enumerates the specified embedded resource.</returns>
|
||||||
|
public static IEnumerable<Task<string>> EnumerateLinesAsync(string filename)
|
||||||
|
{
|
||||||
|
using(StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
|
||||||
|
{
|
||||||
|
while(!resourceReader.EndOfStream)
|
||||||
|
{
|
||||||
|
yield return resourceReader.ReadLineAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets all the lines of text in the specified embedded resource.
|
||||||
|
/// You might find EnumerateLines(string filename) more useful depending on your situation.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">The filename to obtain the lines of text from.</param>
|
||||||
|
/// <returns>A list of lines in the specified embedded resource.</returns>
|
||||||
|
public static List<string> GetAllLines(string filename)
|
||||||
|
{
|
||||||
|
// Referencing the Result property will block until the async method completes
|
||||||
|
return GetAllLinesAsync(filename).Result;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Gets all the lines of text in the specified embedded resource asynchronously.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">The filename to obtain the lines of text from.</param>
|
||||||
|
/// <returns>A list of lines in the specified embedded resource.</returns>
|
||||||
|
public static async Task<List<string>> GetAllLinesAsync(string filename)
|
||||||
|
{
|
||||||
|
List<string> lines = new List<string>();
|
||||||
|
IEnumerable<Task<string>> lineIterator = EnumerateLinesAsync(filename);
|
||||||
|
foreach(Task<string> nextLine in lineIterator)
|
||||||
|
{
|
||||||
|
lines.Add(await nextLine);
|
||||||
|
}
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<packages>
|
<packages>
|
||||||
<package id="Melanchall.DryWetMidi" version="2.0.0" targetFramework="net461" />
|
<package id="Melanchall.DryWetMidi" version="2.0.1" targetFramework="net461" />
|
||||||
</packages>
|
</packages>
|
Loading…
Reference in a new issue