From f92eea20b30d0b456f9c38b1addfa53c32a1b142 Mon Sep 17 00:00:00 2001 From: ConfusedPolarBear <33811686+ConfusedPolarBear@users.noreply.github.com> Date: Fri, 26 Aug 2022 00:50:45 -0500 Subject: [PATCH] Cache inverted indexes --- .../TestAudioFingerprinting.cs | 4 +++- ConfusedPolarBear.Plugin.IntroSkipper/Chromaprint.cs | 12 +++++++++++- .../ScheduledTasks/AnalyzeEpisodesTask.cs | 12 +++++++----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestAudioFingerprinting.cs b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestAudioFingerprinting.cs index a67e858..140ebd7 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestAudioFingerprinting.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestAudioFingerprinting.cs @@ -2,6 +2,7 @@ * which supports both chromaprint and the "-fp_format raw" flag. */ +using System; using System.Collections.Generic; using Xunit; using Microsoft.Extensions.Logging; @@ -77,7 +78,7 @@ public class TestAudioFingerprinting {77, 5}, }; - var actual = Chromaprint.CreateInvertedIndex(fpr); + var actual = Chromaprint.CreateInvertedIndex(Guid.NewGuid(), fpr); Assert.Equal(expected, actual); } @@ -111,6 +112,7 @@ public class TestAudioFingerprinting { return new QueuedEpisode() { + EpisodeId = Guid.NewGuid(), Path = "../../../" + path, FingerprintDuration = 60 }; diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Chromaprint.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Chromaprint.cs index 68fd809..ffe3602 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Chromaprint.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/Chromaprint.cs @@ -20,6 +20,8 @@ public static class Chromaprint private static Dictionary ChromaprintLogs { get; set; } = new(); + private static Dictionary> InvertedIndexCache { get; set; } = new(); + /// /// Check that the installed version of ffmpeg supports chromaprint. /// @@ -124,10 +126,16 @@ public static class Chromaprint /// /// Transforms a Chromaprint into an inverted index of fingerprint points to the last index it appeared at. /// + /// Episode ID. /// Chromaprint fingerprint. /// Inverted index. - public static Dictionary CreateInvertedIndex(uint[] fingerprint) + public static Dictionary CreateInvertedIndex(Guid id, uint[] fingerprint) { + if (InvertedIndexCache.TryGetValue(id, out var cached)) + { + return cached; + } + var invIndex = new Dictionary(); for (int i = 0; i < fingerprint.Length; i++) @@ -139,6 +147,8 @@ public static class Chromaprint invIndex[point] = i; } + InvertedIndexCache[id] = invIndex; + return invIndex; } diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/AnalyzeEpisodesTask.cs b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/AnalyzeEpisodesTask.cs index 941f8b0..e2a0f78 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/AnalyzeEpisodesTask.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/AnalyzeEpisodesTask.cs @@ -268,8 +268,6 @@ public class AnalyzeEpisodesTask : IScheduledTask } } - // TODO: cache inverted indexes - // TODO: implementing bucketing // For all episodes @@ -357,7 +355,7 @@ public class AnalyzeEpisodesTask : IScheduledTask // Creates an inverted fingerprint point index for both episodes. // For every point which is a 100% match, search for an introduction at that point. - var (lhsRanges, rhsRanges) = SearchInvertedIndex(lhsPoints, rhsPoints); + var (lhsRanges, rhsRanges) = SearchInvertedIndex(lhsId, lhsPoints, rhsId, rhsPoints); if (lhsRanges.Count > 0) { @@ -417,19 +415,23 @@ public class AnalyzeEpisodesTask : IScheduledTask /// /// Search for a shared introduction sequence using inverted indexes. /// + /// LHS ID. /// Left episode fingerprint points. + /// RHS ID. /// Right episode fingerprint points. /// List of shared TimeRanges between the left and right episodes. private (List Lhs, List Rhs) SearchInvertedIndex( + Guid lhsId, uint[] lhsPoints, + Guid rhsId, uint[] rhsPoints) { var lhsRanges = new List(); var rhsRanges = new List(); // Generate inverted indexes for the left and right episodes. - var lhsIndex = Chromaprint.CreateInvertedIndex(lhsPoints); - var rhsIndex = Chromaprint.CreateInvertedIndex(rhsPoints); + var lhsIndex = Chromaprint.CreateInvertedIndex(lhsId, lhsPoints); + var rhsIndex = Chromaprint.CreateInvertedIndex(rhsId, rhsPoints); var indexShifts = new HashSet(); // For all audio points in the left episode, check if the right episode has a point which matches exactly.