Small refactor

This commit is contained in:
ConfusedPolarBear 2022-05-16 23:08:20 -05:00
parent 2feba9b582
commit cf1878382b
2 changed files with 28 additions and 18 deletions

View File

@ -12,13 +12,23 @@ public class Intro
/// Initializes a new instance of the <see cref="Intro"/> class. /// Initializes a new instance of the <see cref="Intro"/> class.
/// </summary> /// </summary>
/// <param name="episode">Episode.</param> /// <param name="episode">Episode.</param>
/// <param name="start">Intro start time.</param> /// <param name="intro">Introduction time range.</param>
/// <param name="end">Intro end time.</param> public Intro(Guid episode, TimeRange intro)
public Intro(Guid episode, double start, double end)
{ {
EpisodeId = episode; EpisodeId = episode;
IntroStart = start; IntroStart = intro.Start;
IntroEnd = end; IntroEnd = intro.End;
}
/// <summary>
/// Initializes a new instance of the <see cref="Intro"/> class.
/// </summary>
/// <param name="episode">Episode.</param>
public Intro(Guid episode)
{
EpisodeId = episode;
IntroStart = 0;
IntroEnd = 0;
} }
/// <summary> /// <summary>

View File

@ -232,49 +232,49 @@ public class FingerprinterTask : IScheduledTask
/// Analyze two episodes to find an introduction sequence shared between them. /// Analyze two episodes to find an introduction sequence shared between them.
/// </summary> /// </summary>
/// <param name="lhsId">First episode id.</param> /// <param name="lhsId">First episode id.</param>
/// <param name="lhs">First episode to analyze.</param> /// <param name="lhsPoints">First episode fingerprint points.</param>
/// <param name="rhsId">Second episode id.</param> /// <param name="rhsId">Second episode id.</param>
/// <param name="rhs">Second episode to analyze.</param> /// <param name="rhsPoints">Second episode fingerprint points.</param>
/// <returns>Intros for the first and second episodes.</returns> /// <returns>Intros for the first and second episodes.</returns>
public (Intro Lhs, Intro Rhs) FingerprintEpisodes( public (Intro Lhs, Intro Rhs) FingerprintEpisodes(
Guid lhsId, Guid lhsId,
ReadOnlyCollection<uint> lhs, ReadOnlyCollection<uint> lhsPoints,
Guid rhsId, Guid rhsId,
ReadOnlyCollection<uint> rhs) ReadOnlyCollection<uint> rhsPoints)
{ {
var lhsRanges = new List<TimeRange>(); var lhsRanges = new List<TimeRange>();
var rhsRanges = new List<TimeRange>(); var rhsRanges = new List<TimeRange>();
// Compare all elements of the shortest fingerprint to the other fingerprint. // Compare all elements of the shortest fingerprint to the other fingerprint.
var limit = Math.Min(lhs.Count, rhs.Count); var limit = Math.Min(lhsPoints.Count, rhsPoints.Count);
// First, test if an intro can be found within the first 5 seconds of the episodes (±5/0.128 = ±40 samples). // First, test if an intro can be found within the first 5 seconds of the episodes (±5/0.128 = ±40 samples).
var (lhsContiguous, rhsContiguous) = ShiftEpisodes(lhs, rhs, -40, 40); var (lhsContiguous, rhsContiguous) = ShiftEpisodes(lhsPoints, rhsPoints, -40, 40);
lhsRanges.AddRange(lhsContiguous); lhsRanges.AddRange(lhsContiguous);
rhsRanges.AddRange(rhsContiguous); rhsRanges.AddRange(rhsContiguous);
// If no valid ranges were found, re-analyze the episodes considering all possible shifts. // If no valid ranges were found, re-analyze the episodes considering all possible shifts.
if (lhsRanges.Count == 0) if (lhsRanges.Count == 0)
{ {
_logger.LogDebug("using full scan"); _logger.LogDebug("quick scan unsuccessful, falling back to full scan");
(lhsContiguous, rhsContiguous) = ShiftEpisodes(lhs, rhs, -1 * limit, limit); (lhsContiguous, rhsContiguous) = ShiftEpisodes(lhsPoints, rhsPoints, -1 * limit, limit);
lhsRanges.AddRange(lhsContiguous); lhsRanges.AddRange(lhsContiguous);
rhsRanges.AddRange(rhsContiguous); rhsRanges.AddRange(rhsContiguous);
} }
else else
{ {
_logger.LogDebug("intro found with quick scan"); _logger.LogDebug("quick scan successful");
} }
if (lhsRanges.Count == 0) if (lhsRanges.Count == 0)
{ {
_logger.LogDebug( _logger.LogDebug(
"Unable to find a shared introduction sequence {LHS} and {RHS}", "Unable to find a shared introduction sequence between {LHS} and {RHS}",
lhsId, lhsId,
rhsId); rhsId);
return (new Intro(lhsId, 0, 0), new Intro(rhsId, 0, 0)); return (new Intro(lhsId), new Intro(rhsId));
} }
// After comparing both episodes at all possible shift positions, store the longest time range as the intro. // After comparing both episodes at all possible shift positions, store the longest time range as the intro.
@ -295,11 +295,11 @@ public class FingerprinterTask : IScheduledTask
rhsIntro.Start = 0; rhsIntro.Start = 0;
} }
return (new Intro(lhsId, lhsIntro.Start, lhsIntro.End), new Intro(rhsId, rhsIntro.Start, rhsIntro.End)); return (new Intro(lhsId, lhsIntro), new Intro(rhsId, rhsIntro));
} }
/// <summary> /// <summary>
/// Shifts episodes through the range of provided shift amounts and returns discovered contiguous time ranges. /// Shifts a pair of episodes through the range of provided shift amounts and returns discovered contiguous time ranges.
/// </summary> /// </summary>
/// <param name="lhs">First episode fingerprint.</param> /// <param name="lhs">First episode fingerprint.</param>
/// <param name="rhs">Second episode fingerprint.</param> /// <param name="rhs">Second episode fingerprint.</param>