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; 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; MidiFile midiFile;
@ -27,7 +35,12 @@ namespace MusicBoxConverter
public void Output(string destinationFilename) public void Output(string destinationFilename)
{ {
SvgWriter svg = new SvgWriter(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()) foreach(Note note in AllNotes())
{ {
@ -44,11 +57,6 @@ namespace MusicBoxConverter
svg.Complete(); svg.Complete();
} }
public int TrackLength()
{
return (int)AllNotes().Max((Note arg) => arg.Time);
}
public IEnumerable<Note> AllNotes() public IEnumerable<Note> AllNotes()
{ {
foreach(TrackChunk chunk in midiFile.Chunks.OfType<TrackChunk>()) foreach(TrackChunk chunk in midiFile.Chunks.OfType<TrackChunk>())

View File

@ -30,6 +30,11 @@ namespace MusicBoxConverter
xml.Close(); 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) public void WritePath(string pathData, string strokeStyle = "green", int strokeWidth = 3)
{ {
xml.WriteStartElement("path"); xml.WriteStartElement("path");