Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<PackageVersion Include="LazyCache" Version="2.4.0" />
<PackageVersion Include="Mages" Version="3.0.0" />
<PackageVersion Include="Markdig.Signed" Version="0.34.0" />
<PackageVersion Include="Melanchall.DryWetMidi" Version="7.2.0"/>
<!-- Including MessagePack to force version, since it's used by StreamJsonRpc but contains vulnerabilities. After StreamJsonRpc updates the version of MessagePack, we can upgrade StreamJsonRpc instead. -->
<PackageVersion Include="MessagePack" Version="3.1.3" />
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="9.0.0" />
Expand Down
1 change: 1 addition & 0 deletions src/common/FilePreviewCommon/FilePreviewCommon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup>
<PackageReference Include="Markdig.Signed" />
<PackageReference Include="Melanchall.DryWetMidi" />
<PackageReference Include="System.Text.Encoding.CodePages" />
<PackageReference Include="UTF.Unknown" />
</ItemGroup>
Expand Down
60 changes: 60 additions & 0 deletions src/common/FilePreviewCommon/PianoRoll/CoordinateHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Linq;

namespace Microsoft.PowerToys.FilePreviewCommon.PianoRoll
{
internal sealed class CoordinateHelper
{
public const int RESOLUTION = 480;
public const int PADDING = 4;

public int PixelPerBeat { get; set; }

public int NoteHeight { get; set; }

private int positionRangeStart;
private int positionRangeEnd;
private int minKey;
private int maxKey;

public void CalculateRange(Project project)
{
positionRangeStart = project.TrackList
.Where(tr => tr.NoteList.Count > 0)
.Min(tr => tr.NoteList[0].StartPos);
positionRangeEnd = project.TrackList
.SelectMany(tr => tr.NoteList)
.Max(n => n.StartPos + n.Length);
minKey = project.TrackList
.SelectMany(tr => tr.NoteList)
.Min(n => n.KeyNumber);
maxKey = project.TrackList
.SelectMany(tr => tr.NoteList)
.Max(n => n.KeyNumber);
}

public NotePositionParameters GetNotePositionParameters(Note note)
{
return new NotePositionParameters
{
Point1 = new Tuple<double, double>(
1.0 * (note.StartPos - positionRangeStart) * PixelPerBeat / RESOLUTION,
(maxKey - note.KeyNumber) * NoteHeight),
Point2 = new Tuple<double, double>(
1.0 * (note.StartPos + note.Length - positionRangeStart) * PixelPerBeat / RESOLUTION,
(maxKey - note.KeyNumber + 1) * NoteHeight),
};
}

public Tuple<double, double> GetSize()
{
return new Tuple<double, double>(
1.0 * (positionRangeEnd - positionRangeStart) * PixelPerBeat / RESOLUTION,
(maxKey - minKey + 1) * NoteHeight);
}
}
}
15 changes: 15 additions & 0 deletions src/common/FilePreviewCommon/PianoRoll/Note.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Microsoft.PowerToys.FilePreviewCommon.PianoRoll
{
internal sealed class Note
{
public int StartPos { get; set; }

public int Length { get; set; }

public int KeyNumber { get; set; }
}
}
15 changes: 15 additions & 0 deletions src/common/FilePreviewCommon/PianoRoll/NotePositionParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

namespace Microsoft.PowerToys.FilePreviewCommon.PianoRoll
{
internal sealed class NotePositionParameters
{
public required Tuple<double, double> Point1 { get; set; }

public required Tuple<double, double> Point2 { get; set; }
}
}
62 changes: 62 additions & 0 deletions src/common/FilePreviewCommon/PianoRoll/PianoRollHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Melanchall.DryWetMidi.Core;
using Melanchall.DryWetMidi.Interaction;
using MidiNote = Melanchall.DryWetMidi.Interaction.Note;

namespace Microsoft.PowerToys.FilePreviewCommon.PianoRoll
{
public static class PianoRollHelper
{
public static string MidiSvg(string midiPath)
{
var project = DecodeMidiFile(midiPath);
var svgFactory = new SvgEncoder
{
PixelPerBeat = 48,
NoteHeight = 24,
}.Generate(project);
return svgFactory.Dumps();
}

private static Project DecodeMidiFile(string midiPath)
{
var midiFile = MidiFile.Read(midiPath);
var resolution = (midiFile.TimeDivision as TicksPerQuarterNoteTimeDivision)?.TicksPerQuarterNote ?? 480;
return new Project
{
TrackList = midiFile.GetTrackChunks()
.Select(tr => DecodeMidiTrack(tr, resolution))
.ToList(),
};
}

private static Track DecodeMidiTrack(TrackChunk trackChunk, int resolution)
{
return new Track
{
NoteList = trackChunk.GetNotes()
.Select(n => DecodeMidiNote(n, resolution))
.ToList(),
};
}

private static Note DecodeMidiNote(MidiNote midiNote, int resolution)
{
int startPos = (int)(midiNote.Time * 480 / resolution);
return new Note
{
StartPos = startPos,
Length = (int)((midiNote.Time + midiNote.Length) * 480 / resolution) - startPos,
KeyNumber = midiNote.NoteNumber,
};
}
}
}
13 changes: 13 additions & 0 deletions src/common/FilePreviewCommon/PianoRoll/Project.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;

namespace Microsoft.PowerToys.FilePreviewCommon.PianoRoll
{
internal sealed class Project
{
public List<Track> TrackList { get; set; } = new List<Track>();
}
}
19 changes: 19 additions & 0 deletions src/common/FilePreviewCommon/PianoRoll/Rectangle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Microsoft.PowerToys.FilePreviewCommon.PianoRoll
{
internal sealed class Rectangle
{
public double X { get; set; }

public double Y { get; set; }

public double Height { get; set; }

public double Width { get; set; }

public double R { get; set; }
}
}
36 changes: 36 additions & 0 deletions src/common/FilePreviewCommon/PianoRoll/SvgEncoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Linq;

namespace Microsoft.PowerToys.FilePreviewCommon.PianoRoll
{
internal sealed class SvgEncoder
{
public int PixelPerBeat { get; set; }

public int NoteHeight { get; set; }

public SvgFactory Generate(Project project)
{
var coordinateHelper = new CoordinateHelper
{
PixelPerBeat = PixelPerBeat,
NoteHeight = NoteHeight,
};
coordinateHelper.CalculateRange(project);
var svgFactory = new SvgFactory()
{
CoordinateHelper = coordinateHelper,
};
svgFactory.ApplyStyle(project.TrackList.Count);
foreach (var track in project.TrackList.OfType<Track>())
{
svgFactory.DrawTrack(track);
}

return svgFactory;
}
}
}
123 changes: 123 additions & 0 deletions src/common/FilePreviewCommon/PianoRoll/SvgFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace Microsoft.PowerToys.FilePreviewCommon.PianoRoll
{
internal sealed class SvgFactory
{
private string[] noteColors = new string[]
{
"#1F77B4",
"#ff7f0e",
"#2ca02c",
"#d62728",
"#9467bd",
"#8c564b",
"#e377c2",
"#7f7f7f",
"#bcbd22",
"#17becf",
};

private List<Rectangle[]> tracksRectangles = new List<Rectangle[]>();

internal string Style { get; set; } = string.Empty;

public required CoordinateHelper CoordinateHelper { get; set; }

private int noteRound = 4;

public void ApplyStyle(int trackCount)
{
StringBuilder styleBuilder = new StringBuilder();
for (int trackId = 0; trackId < trackCount; trackId++)
{
var trackStyle = $@".note{trackId.ToString(CultureInfo.InvariantCulture)} {{
fill: {noteColors[trackId % noteColors.Length]}
}}
";
styleBuilder.Append(trackStyle);
}

Style = styleBuilder.ToString();
}

public void DrawTrack(Track track)
{
tracksRectangles.Add(track.NoteList.Select(DrawNote).ToArray());
}

public Rectangle DrawNote(Note note)
{
var parameters = CoordinateHelper.GetNotePositionParameters(note);
var rect = new Rectangle
{
X = parameters.Point1.Item1,
Y = parameters.Point1.Item2,
Width = parameters.Point2.Item1 - parameters.Point1.Item1,
Height = parameters.Point2.Item2 - parameters.Point1.Item2,
R = noteRound,
};
return rect;
}

public XmlDocument ToXmlDocument()
{
var svgFile = new XmlDocument();
var root = svgFile.CreateElement("svg", "http://www.w3.org/2000/svg");
root.SetAttribute("width", CoordinateHelper.GetSize().Item1.ToString(CultureInfo.InvariantCulture));
root.SetAttribute("height", CoordinateHelper.GetSize().Item2.ToString(CultureInfo.InvariantCulture));
svgFile.AppendChild(root);
var style = svgFile.CreateElement("style", svgFile.DocumentElement?.NamespaceURI);
style.InnerText = Style;
root.AppendChild(style);
for (int trackId = 0; trackId < tracksRectangles.Count; trackId++)
{
var trackRectangles = tracksRectangles[trackId];
foreach (var rectangleElement in trackRectangles)
{
var rectangle = svgFile.CreateElement("rect", svgFile.DocumentElement?.NamespaceURI);
rectangle.SetAttribute("class", $"note{trackId}");
rectangle.SetAttribute("x", rectangleElement.X.ToString(CultureInfo.InvariantCulture));
rectangle.SetAttribute("y", rectangleElement.Y.ToString(CultureInfo.InvariantCulture));
rectangle.SetAttribute("width", rectangleElement.Width.ToString(CultureInfo.InvariantCulture));
rectangle.SetAttribute("height", rectangleElement.Height.ToString(CultureInfo.InvariantCulture));
rectangle.SetAttribute("rx", rectangleElement.R.ToString(CultureInfo.InvariantCulture));
rectangle.SetAttribute("ry", rectangleElement.R.ToString(CultureInfo.InvariantCulture));
root.AppendChild(rectangle);
}
}

return svgFile;
}

public string Dumps()
{
StringWriter stringWriter = new StringWriter();
using (XmlWriter writer = XmlWriter.Create(stringWriter))
{
ToXmlDocument().WriteTo(writer);
writer.Flush();
}

return stringWriter.ToString();
}

public void Write(string path)
{
using (XmlWriter writer = XmlWriter.Create(path))
{
ToXmlDocument().WriteTo(writer);
writer.Flush();
}
}
}
}
13 changes: 13 additions & 0 deletions src/common/FilePreviewCommon/PianoRoll/Track.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;

namespace Microsoft.PowerToys.FilePreviewCommon.PianoRoll
{
internal sealed class Track
{
public List<Note> NoteList { get; set; } = new List<Note>();
}
}
Loading
Loading