Skip to content

Commit

Permalink
2024.42.2
Browse files Browse the repository at this point in the history
- PairingsHandler pair calculation more efficient.
  • Loading branch information
simon-techkid committed May 11, 2024
1 parent e5cce5f commit 07e7f1a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
9 changes: 5 additions & 4 deletions SpotifyGPX/PairingsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ private static List<SongPoint> PairPoints(bool silent, List<ISongEntry> songs, L
.Where(spotifyEntry => spotifyEntry.WithinTimeFrame(track.Start, track.End)) // If the SpotifyEntry falls within the boundaries of the track
.Select(spotifyEntry => // Select the Spotify entry if it falls in range of the GPX track
{
SongPoint pair = track
.Select(point => new SongPoint(index, spotifyEntry, point, track.Track)) // For each point in the track's point list,
.OrderBy(pair => pair.AbsAccuracy) // Order the points by proximity between point and song
.First(); // Closest accuracy wins
IGpsPoint bestPoint = track.Points
.OrderBy(point => SongPoint.DisplacementCalculatorAbs(spotifyEntry.Time, point.Time))
.First();
SongPoint pair = new(index, spotifyEntry, bestPoint, track.Track);
if (!silent)
{
Console.WriteLine(pair.ToString());
Expand Down
24 changes: 24 additions & 0 deletions SpotifyGPX/SongPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ public SongPoint(int index, ISongEntry song, IGpsPoint point, TrackInfo origin)
/// </summary>
public readonly double Accuracy => (Song.Time - Point.Time).TotalSeconds;

/// <summary>
/// Calculate the difference between two times.
/// </summary>
/// <param name="time1"></param>
/// <param name="time2"></param>
/// <returns>A <see cref="TimeSpan"/> representing a time interval between the provided times.</returns>
public static TimeSpan DifferenceCalculator(DateTimeOffset time1, DateTimeOffset time2) => time1 - time2;

/// <summary>
/// Calculate the difference between two times.
/// </summary>
/// <param name="time1">The first time.</param>
/// <param name="time2">The second time.</param>
/// <returns>A <see langword="double"/> representing the difference in seconds between two times.</returns>
public static double DisplacementCalculator(DateTimeOffset time1, DateTimeOffset time2) => DifferenceCalculator(time1, time2).TotalSeconds;

/// <summary>
/// Calculate the absolute difference between two times.
/// </summary>
/// <param name="time1">The first time.</param>
/// <param name="time2">The second time.</param>
/// <returns>A <see langword="double"/> representing the difference in seconds between two times.</returns>
public static double DisplacementCalculatorAbs(DateTimeOffset time1, DateTimeOffset time2) => Math.Abs(DisplacementCalculator(time1, time2));

/// <summary>
/// The absolute value (in seconds) between the song and the point.
/// </summary>
Expand Down

0 comments on commit 07e7f1a

Please sign in to comment.