Browse Source

Refactor to make it nicer

main
Starbeamrainbowlabs 4 years ago
parent
commit
8cdb9591ab
Signed by: sbrl GPG Key ID: 1BE5172E637709C2
  1. 46
      MusicBoxConverter/MusicBoxScoreGenerator.cs
  2. 7
      MusicBoxConverter/SvgWriter.cs
  3. 6
      MusicBoxConverter/Utilities/Vector2.cs

46
MusicBoxConverter/MusicBoxScoreGenerator.cs

@ -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(Note note in AllNotes())
{
Console.WriteLine("[{0}] {1}/{2}", note.Time, note.NoteName, note.NoteNumber);
svg.WriteCircle(
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)
{
Console.WriteLine("[{0}] {1}/{2}", note.Time, note.NoteName, note.NoteNumber);
svg.WriteCircle(
(int)(note.Time * horizontalScaleFactor),
(int)(note.NoteNumber * verticalScaleFactor),
5
);
}
yield return note;
}
}
svg.Complete();
}
public int TrackLength()
{
//notesManager.Notes.OfType<TrackChunk>().Select((TrackChunk arg) => arg.Events.O;
return 1;
}
}
}

7
MusicBoxConverter/SvgWriter.cs

@ -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();

6
MusicBoxConverter/Utilities/Vector2.cs

@ -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;

Loading…
Cancel
Save