Refactor to make it nicer

This commit is contained in:
Starbeamrainbowlabs 2017-11-30 23:03:55 +00:00
parent 50a9527597
commit 8cdb9591ab
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
3 changed files with 43 additions and 22 deletions

View File

@ -1,15 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Melanchall.DryWetMidi.Smf;
using Melanchall.DryWetMidi.Smf.Interaction;
using SBRL.Utilities;
namespace MusicBoxConverter
{
public class MusicBoxScoreGenerator
{
public float horizontalScaleFactor = 0.1f;
public float verticalScaleFactor = 4f;
public Vector2 offset { get; set; } = new Vector2(10, 10);
public Vector2 scaleFactor { get; set; } = new Vector2(0.1f, 4f);
public int holeSize { get; set; } = 5;
MidiFile midiFile;
@ -22,24 +28,38 @@ namespace MusicBoxConverter
{
SvgWriter svg = new SvgWriter(destinationFilename);
foreach(TrackChunk chunk in midiFile.Chunks.OfType<TrackChunk>())
{
using(NotesManager notesManager = new NotesManager(chunk.Events))
{
foreach(Note note in notesManager.Notes)
foreach(Note note in AllNotes())
{
Console.WriteLine("[{0}] {1}/{2}", note.Time, note.NoteName, note.NoteNumber);
svg.WriteCircle(
(int)(note.Time * horizontalScaleFactor),
(int)(note.NoteNumber * verticalScaleFactor),
5
new Vector2(
offset.X + note.Time * scaleFactor.X,
offset.Y + note.NoteNumber * scaleFactor.Y
),
holeSize // radius
);
}
}
}
svg.Complete();
}
public IEnumerable<Note> AllNotes()
{
foreach(TrackChunk chunk in midiFile.Chunks.OfType<TrackChunk>())
{
using(NotesManager notesManager = new NotesManager(chunk.Events))
{
foreach(Note note in notesManager.Notes)
yield return note;
}
}
}
public int TrackLength()
{
//notesManager.Notes.OfType<TrackChunk>().Select((TrackChunk arg) => arg.Events.O;
return 1;
}
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Xml;
using SBRL.Utilities;
namespace MusicBoxConverter
{
@ -38,11 +39,11 @@ namespace MusicBoxConverter
xml.WriteEndElement();
}
public void WriteCircle(int cx, int cy, int radius, string fillStyle = "blue")
public void WriteCircle(Vector2 centre, int radius, string fillStyle = "blue")
{
xml.WriteStartElement("circle");
xml.WriteAttributeString("cx", cx.ToString());
xml.WriteAttributeString("cy", cy.ToString());
xml.WriteAttributeString("cx", centre.X.ToString());
xml.WriteAttributeString("cy", centre.Y.ToString());
xml.WriteAttributeString("r", radius.ToString());
xml.WriteAttributeString("fill", fillStyle);
xml.WriteEndElement();

View File

@ -21,14 +21,14 @@ namespace SBRL.Utilities
/// <summary>
/// The X coordinate.
/// </summary>
public int X { get; set; }
public float X { get; set; }
/// <summary>
/// The Y coordinate.
/// </summary>
public int Y { get; set; }
public float Y { get; set; }
public Vector2(int x, int y)
public Vector2(float x, float y)
{
X = x;
Y = y;