Move TrackLength to cached property

This commit is contained in:
Starbeamrainbowlabs 2017-12-02 14:38:49 +00:00
parent 5aa7c6a0c3
commit eb92ce143b
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 19 additions and 6 deletions

View file

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

View file

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