Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  Version bump
  Minor refactoring.
  Fix for QueryResultCoverageCalculator.GetCoverages()
  Replace EstimateCoverage() with EstimateIncreasingCoverages() in SplitTrackMatchedRegions().
  EstimateCoverage() invokes LisOld. Add EstimateIncreasingCoverages() which uses LisNew.
  Expect the caller to pass the query and track lengths, because we cannot calculate them from the match regions.
  Output of a function given at the input should return the same result.
  • Loading branch information
AddictedCS committed Jul 30, 2020
2 parents 389b710 + 36d33f3 commit fc89017
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/SoundFingerprinting.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("4cac962e-ebc5-4006-a1e0-7ffb3e2483c2")]
[assembly: AssemblyVersion("7.4.7.100")]
[assembly: AssemblyInformationalVersion("7.4.7.100")]
[assembly: AssemblyVersion("7.4.10.100")]
[assembly: AssemblyInformationalVersion("7.4.10.100")]
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,18 @@ public void BruteForceTest()
var results = LisNew.GetIncreasingSequences(Generate(pairs), 1000).ToArray();

Assert.AreEqual(1, results.Length);

var matchedWiths = results.First().ToList();
var noSideEffects = LisNew.GetIncreasingSequences(matchedWiths).First().ToList();

Assert.AreEqual(matchedWiths.Count, noSideEffects.Count);
foreach (var pair in matchedWiths.Zip(noSideEffects, (a, b) => (a, b)))
{
Assert.AreEqual(pair.a.QueryMatchAt, pair.b.QueryMatchAt);
Assert.AreEqual(pair.a.TrackMatchAt, pair.b.TrackMatchAt);
Assert.AreEqual(pair.a.QuerySequenceNumber, pair.b.QuerySequenceNumber);
Assert.AreEqual(pair.a.TrackSequenceNumber, pair.b.TrackSequenceNumber);
}
}

private static void AssertResult((int q, int t, double s)[] pairs, IEnumerable<MatchedWith> result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class QueryCoverageCalculatorTest
[Test]
public void ShouldNotFailWithEmptyEntries()
{
var qrc = new QueryResultCoverageCalculator(new LongestIncreasingTrackSequence());
var qrc = new QueryResultCoverageCalculator();

var track = new TrackData("1234", "artist", "title", 120d, new ModelReference<int>(1));

Expand Down
4 changes: 3 additions & 1 deletion src/SoundFingerprinting.Tests/Unit/Query/MatchesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ public void ShouldSplitMatchedRegionsToGapRegions()

float fingerprintLengthInSeconds = 1f;
double permittedGap = 2.5;
var coverages = matches.SplitTrackMatchedRegions(permittedGap, fingerprintLengthInSeconds).ToList();
double trackLength = 40;
double queryLength = 40;
var coverages = matches.SplitTrackMatchedRegions(queryLength, trackLength, fingerprintLengthInSeconds, permittedGap).ToList();

Assert.AreEqual(2, coverages.Count);
Assert.AreEqual(10 + fingerprintLengthInSeconds, coverages.First().CoverageWithPermittedGapsLength);
Expand Down
2 changes: 1 addition & 1 deletion src/SoundFingerprinting/Configuration/SpectrogramConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal abstract class SpectrogramConfig
/// <summary>
/// Gets or sets overlap between the consecutively computed spectrum images
/// </summary>
/// <remarks>64 at 5512 sample rate is aproximatelly 11.6ms</remarks>
/// <remarks>64 at 5512 sample rate is approximately 11.6ms</remarks>
public ushort Overlap { get; set; }

/// <summary>
Expand Down
14 changes: 2 additions & 12 deletions src/SoundFingerprinting/LCS/QueryResultCoverageCalculator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace SoundFingerprinting.LCS
{
using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -10,13 +9,6 @@

internal class QueryResultCoverageCalculator : IQueryResultCoverageCalculator
{
private readonly ILongestIncreasingTrackSequence longestIncreasingTrackSequence;

public QueryResultCoverageCalculator(ILongestIncreasingTrackSequence longestIncreasingTrackSequence)
{
this.longestIncreasingTrackSequence = longestIncreasingTrackSequence;
}

public IEnumerable<Coverage> GetCoverages(TrackData trackData, GroupedQueryResults groupedQueryResults, QueryConfiguration configuration)
{
var fingerprintConfiguration = configuration.FingerprintConfiguration;
Expand All @@ -31,10 +23,8 @@ public IEnumerable<Coverage> GetCoverages(TrackData trackData, GroupedQueryResul

if (configuration.AllowMultipleMatchesOfTheSameTrackInQuery)
{
double allowedGap = Math.Min(trackData.Length, queryLength);

var sequences = longestIncreasingTrackSequence.FindAllIncreasingTrackSequences(matches, allowedGap);
return sequences.Select(sequence => new Coverage(sequence, queryLength, trackData.Length, fingerprintConfiguration.FingerprintLengthInSeconds, configuration.PermittedGap));
return matches.EstimateIncreasingCoverages(queryLength, trackData.Length,
fingerprintConfiguration.FingerprintLengthInSeconds, configuration.PermittedGap);
}

return new[] { matches.EstimateCoverage(queryLength, trackData.Length, fingerprintConfiguration.FingerprintLengthInSeconds, configuration.PermittedGap) };
Expand Down
4 changes: 2 additions & 2 deletions src/SoundFingerprinting/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
[assembly: InternalsVisibleTo("SoundFingerprinting.FFT.FFTW")]
[assembly: InternalsVisibleTo("SoundFingerprinting.FFT.FFTW.Tests")]

[assembly: AssemblyVersion("7.4.7.100")]
[assembly: AssemblyInformationalVersion("7.4.7.100")]
[assembly: AssemblyVersion("7.4.10.100")]
[assembly: AssemblyInformationalVersion("7.4.10.100")]
14 changes: 7 additions & 7 deletions src/SoundFingerprinting/Query/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public static double StdDev(this IEnumerable<double> values)
return ret;
}

public static IEnumerable<Coverage> SplitTrackMatchedRegions(this IEnumerable<MatchedWith> entries, double permittedGap, double fingerprintLength)
public static IEnumerable<Coverage> SplitTrackMatchedRegions(this IEnumerable<MatchedWith> entries,
double queryLength, double trackLength, double fingerprintLength, double permittedGap)
{
var list = new List<Coverage>();
var ordered = entries.OrderBy(_ => _.TrackMatchAt).ToList();
Expand All @@ -40,23 +41,22 @@ public static IEnumerable<Coverage> SplitTrackMatchedRegions(this IEnumerable<Ma
var prev = stack.Peek();
if (SubFingerprintsToSeconds.GapLengthToSeconds(matchedWith.TrackMatchAt, prev.TrackMatchAt, fingerprintLength) > permittedGap)
{
list.Add(GetMatchedWithsFromStack(stack, permittedGap, fingerprintLength));
list.AddRange(GetMatchedWithsFromStack(stack, queryLength, trackLength, fingerprintLength, permittedGap));
stack = new Stack<MatchedWith>();
}

stack.Push(matchedWith);
}

list.Add(GetMatchedWithsFromStack(stack, permittedGap, fingerprintLength));
list.AddRange(GetMatchedWithsFromStack(stack, queryLength, trackLength, fingerprintLength, permittedGap));
return list;
}

private static Coverage GetMatchedWithsFromStack(Stack<MatchedWith> stack, double permittedGap, double fingerprintLengthInSeconds)
private static IEnumerable<Coverage> GetMatchedWithsFromStack(Stack<MatchedWith> stack,
double queryLength, double trackLength, double fingerprintLengthInSeconds, double permittedGap)
{
var matchedWiths = ((IEnumerable<MatchedWith>) stack.ToList()).Reverse().ToList();
var queryLength = SubFingerprintsToSeconds.MatchLengthToSeconds(matchedWiths.Last().QueryMatchAt, matchedWiths.First().QueryMatchAt, fingerprintLengthInSeconds);
var trackLength = SubFingerprintsToSeconds.MatchLengthToSeconds(matchedWiths.Last().TrackMatchAt, matchedWiths.First().TrackMatchAt, fingerprintLengthInSeconds);
return matchedWiths.EstimateCoverage(queryLength, trackLength, fingerprintLengthInSeconds, permittedGap);
return matchedWiths.EstimateIncreasingCoverages(queryLength, trackLength, fingerprintLengthInSeconds, permittedGap);
}

public static IEnumerable<Gap> FindQueryGaps(this IEnumerable<MatchedWith> entries, double permittedGap, double fingerprintLength)
Expand Down
13 changes: 12 additions & 1 deletion src/SoundFingerprinting/Query/MatchedWithExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
namespace SoundFingerprinting.Query
using System.Linq;

namespace SoundFingerprinting.Query
{
using SoundFingerprinting.LCS;
using System.Collections.Generic;

public static class MatchedWithExtensions
{
private static readonly ILongestIncreasingTrackSequence LongestIncreasingTrackSequence = new LongestIncreasingTrackSequence();

/// <summary>
/// Estimates track coverage assuming there is only one match of the target Track in the query.
/// i.e. [----xxx----] but never [----xxxx-----xxxx] where x is 'complete' match.
Expand All @@ -24,5 +28,12 @@ public static Coverage EstimateCoverage(this IEnumerable<MatchedWith> matchedEnt
var bestPath = LisOld.GetBestPath(matchedEntries, queryLength, trackLength, fingerprintLength);
return new Coverage(bestPath, queryLength, trackLength, fingerprintLength, permittedGap);
}

public static IEnumerable<Coverage> EstimateIncreasingCoverages(this IEnumerable<MatchedWith> matches, double queryLength, double trackLength, double fingerprintLength, double permittedGap)
{
double allowedGap = System.Math.Min(trackLength, queryLength);
var sequences = LongestIncreasingTrackSequence.FindAllIncreasingTrackSequences(matches, allowedGap);
return sequences.Select(sequence => new Coverage(sequence, queryLength, trackLength, fingerprintLength, permittedGap));
}
}
}
2 changes: 1 addition & 1 deletion src/SoundFingerprinting/Query/QueryMath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal QueryMath(IQueryResultCoverageCalculator queryResultCoverageCalculator,
this.confidenceCalculator = confidenceCalculator;
}

public static QueryMath Instance { get; } = new QueryMath(new QueryResultCoverageCalculator(new LongestIncreasingTrackSequence()), new ConfidenceCalculator());
public static QueryMath Instance { get; } = new QueryMath(new QueryResultCoverageCalculator(), new ConfidenceCalculator());

public List<ResultEntry> GetBestCandidates(GroupedQueryResults groupedQueryResults, int maxNumberOfMatchesToReturn, IModelService modelService, QueryConfiguration queryConfiguration)
{
Expand Down
4 changes: 2 additions & 2 deletions src/SoundFingerprinting/SoundFingerprinting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<Nullable>enable</Nullable>
<PackageLicenseUrl>https://opensource.org/licenses/MIT</PackageLicenseUrl>
<PackageVersion>7.4.7</PackageVersion>
<PackageVersion>7.4.10</PackageVersion>
<Authors>Sergiu Ciumac</Authors>
<PackageDescription>SoundFingerprinting is a C# framework that implements an efficient algorithm of audio fingerprinting and identification. Designed for developers, enthusiasts, researchers in the fields of audio processing, data mining, digital signal processing.</PackageDescription>
<PackageProjectUrl>https://github.com/addictedcs/soundfingerprinting</PackageProjectUrl>
<RepositoryUrl>https://github.com/AddictedCS/soundfingerprinting</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>Version 7.4.6 adds the ability to read hashes for a particular track.</PackageReleaseNotes>
<PackageReleaseNotes>Version 7.4.10 adds the ability to split result entries into multiple result entries for streaming use cases.</PackageReleaseNotes>
<PackageTags>Audio Identification Fingerprinting Digital Signal Processing Music Recognition Data Mining Content Sound Shazam</PackageTags>
<LangVersion>8</LangVersion>
</PropertyGroup>
Expand Down

0 comments on commit fc89017

Please sign in to comment.