Browse Source

Move TrackLength to cached property

main
Starbeamrainbowlabs 4 years ago
parent
commit
eb92ce143b
Signed by: sbrl GPG Key ID: 1BE5172E637709C2
  1. 20
      MusicBoxConverter/MusicBoxScoreGenerator.cs
  2. 5
      MusicBoxConverter/SvgWriter.cs

20
MusicBoxConverter/MusicBoxScoreGenerator.cs

@ -16,6 +16,14 @@ namespace MusicBoxConverter
public int holeSize { get; set; } = 5;
private int trackLength;
public int TrackLength {
get {
if(trackLength == null)
trackLength = (int)AllNotes().Max((Note arg) => arg.Time);
return trackLength;
}
}
MidiFile midiFile;
@ -27,7 +35,12 @@ namespace MusicBoxConverter
public void Output(string destinationFilename)
{
SvgWriter svg = new SvgWriter(destinationFilename);
svg.WriteRectangle(offset, new Vector2(TrackLength(), 128).Multiply(scaleFactor));
svg.WriteRectangle(offset, new Vector2(TrackLength, 128).Multiply(scaleFactor));
for(int i = 0; i < 255; i += 5)
{
Vector2 start = offset.Add(new Vector2(0, i));
svg.WriteLine(start, start.Add(new Vector2(TrackLength, 0)));
}
foreach(Note note in AllNotes())
{
@ -44,11 +57,6 @@ namespace MusicBoxConverter
svg.Complete();
}
public int TrackLength()
{
return (int)AllNotes().Max((Note arg) => arg.Time);
}
public IEnumerable<Note> AllNotes()
{
foreach(TrackChunk chunk in midiFile.Chunks.OfType<TrackChunk>())

5
MusicBoxConverter/SvgWriter.cs

@ -30,6 +30,11 @@ namespace MusicBoxConverter
xml.Close();
}
public void WriteLine(Vector2 start, Vector2 end, string strokeStyle = "darkgreen", int strokeWidth = 3)
{
WritePath($"M {start.X} {start.Y} L {end.X} {end.Y}", strokeStyle, strokeWidth);
}
public void WritePath(string pathData, string strokeStyle = "green", int strokeWidth = 3)
{
xml.WriteStartElement("path");

Loading…
Cancel
Save