Skip to content

Commit

Permalink
We need to pass OriginalPoint to HashedFingerprinting after filter …
Browse files Browse the repository at this point in the history
…transformations applied on it.
  • Loading branch information
AddictedCS committed May 8, 2020
1 parent 939586e commit 1390c08
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 81 deletions.
2 changes: 1 addition & 1 deletion src/SoundFingerprinting.Tests/AbstractTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class AbstractTest

protected Hashes GetGenericHashes()
{
return new Hashes(new[] {new HashedFingerprint(GenericHashBuckets(), 0, 0f)}, 1.48f, DateTime.Now, Enumerable.Empty<string>());
return new Hashes(new[] {new HashedFingerprint(GenericHashBuckets(), 0, 0f, Array.Empty<byte>())}, 1.48f, DateTime.Now, Enumerable.Empty<string>());
}

protected int[] GenericHashBuckets()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public void ShouldSerializeAndIncrementNextIdCorrectly()
var modelService = new InMemoryModelService();

var firstTrack = new TrackInfo("id1", "title", "artist");
modelService.Insert(firstTrack, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 1, 0f) }, 1.48, DateTime.Now, Enumerable.Empty<string>()));
modelService.Insert(firstTrack, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 1, 0f, Array.Empty<byte>()) }, 1.48, DateTime.Now, Enumerable.Empty<string>()));

var tempFile = Path.GetTempFileName();
modelService.Snapshot(tempFile);

var fromFileService = new InMemoryModelService(tempFile);

var secondTrack = new TrackInfo("id2", "title", "artist");
fromFileService.Insert(secondTrack, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 1, 0f) }, 1.48, DateTime.Now, Enumerable.Empty<string>()));
fromFileService.Insert(secondTrack, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 1, 0f, Array.Empty<byte>()) }, 1.48, DateTime.Now, Enumerable.Empty<string>()));

var tracks = fromFileService.ReadAllTracks().ToList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void InsertTrackTest()
{
var track = new TrackInfo("id", "title", "artist");

modelService.Insert(track, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 0, 0f) }, 1.48, DateTime.Now, Enumerable.Empty<string>()));
modelService.Insert(track, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 0, 0f, Array.Empty<byte>()) }, 1.48, DateTime.Now, Enumerable.Empty<string>()));

Assert.IsNotNull(modelService.ReadTrackById("id"));
}
Expand All @@ -34,7 +34,7 @@ public void InsertTrackTest()
public void ReadTrackByTrackIdTest()
{
var track = new TrackInfo("id", "title", "artist");
modelService.Insert(track, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 0, 0f) }, 1.48, DateTime.Now, Enumerable.Empty<string>()));
modelService.Insert(track, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 0, 0f, Array.Empty<byte>()) }, 1.48, DateTime.Now, Enumerable.Empty<string>()));

var first = modelService.ReadTrackById("id");
AssertTracksAreEqual(track, first);
Expand All @@ -50,7 +50,7 @@ public void ReadTrackByArtistAndTitleTest()
{
var track = new TrackInfo("id", "title", "artist");

modelService.Insert(track, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 0, 0f) }, 1.48d, DateTime.Now, Enumerable.Empty<string>()));
modelService.Insert(track, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 0, 0f, Array.Empty<byte>()) }, 1.48d, DateTime.Now, Enumerable.Empty<string>()));

var actualTracks = modelService.ReadTrackByTitle("title").ToList();

Expand All @@ -65,7 +65,7 @@ public void ReadMultipleTracksTest()
for (int i = 0; i < numberOfTracks; i++)
{
var track = new TrackInfo($"id{i}", "title", "artist");
modelService.Insert(track, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 0, 0f) }, 1.48d, DateTime.Now, Enumerable.Empty<string>()));
modelService.Insert(track, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 0, 0f, Array.Empty<byte>()) }, 1.48d, DateTime.Now, Enumerable.Empty<string>()));
}

var actualTracks = modelService.ReadAllTracks().ToList();
Expand All @@ -77,7 +77,7 @@ public void ReadMultipleTracksTest()
public void DeleteTrackTest()
{
var track = new TrackInfo("id", "title", "artist");
modelService.Insert(track, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 0, 0f) }, 1.48, DateTime.Now, Enumerable.Empty<string>()));
modelService.Insert(track, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 0, 0f, Array.Empty<byte>()) }, 1.48, DateTime.Now, Enumerable.Empty<string>()));

modelService.DeleteTrack("id");

Expand All @@ -91,7 +91,7 @@ public void DeleteTrackTest()
public void InsertHashDataTest()
{
var expectedTrack = new TrackInfo("id", "title", "artist");
modelService.Insert(expectedTrack, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 0, 0f) }, 1.48, DateTime.Now, Enumerable.Empty<string>()));
modelService.Insert(expectedTrack, new Hashes(new[] { new HashedFingerprint(GenericHashBuckets(), 0, 0f, Array.Empty<byte>()) }, 1.48, DateTime.Now, Enumerable.Empty<string>()));

var subFingerprints = modelService.Query(GetGenericHashes(), new DefaultQueryConfiguration()).ToList();

Expand All @@ -110,15 +110,15 @@ public void ReadSubFingerprintsByHashBucketsHavingThresholdTest()
int[] firstTrackBuckets = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
int[] secondTrackBuckets = { 2, 2, 4, 5, 6, 7, 7, 9, 10, 11, 12, 13, 14, 14, 16, 17, 18, 19, 20, 20, 22, 23, 24, 25, 26 };

var firstHashData = new HashedFingerprint(firstTrackBuckets, 1, 0.928f);
var secondHashData = new HashedFingerprint(secondTrackBuckets, 1, 0.928f);
var firstHashData = new HashedFingerprint(firstTrackBuckets, 1, 0.928f, Array.Empty<byte>());
var secondHashData = new HashedFingerprint(secondTrackBuckets, 1, 0.928f, Array.Empty<byte>());

modelService.Insert(t1, new Hashes(new[] { firstHashData }, 1.48d, DateTime.Now, Enumerable.Empty<string>()));
modelService.Insert(t2, new Hashes(new[] { secondHashData }, 1.48d, DateTime.Now, Enumerable.Empty<string>()));

// query buckets are similar with 5 elements from first track and 4 elements from second track
int[] queryBuckets = { 3, 2, 5, 6, 7, 8, 7, 10, 11, 12, 13, 14, 15, 14, 17, 18, 19, 20, 21, 20, 23, 24, 25, 26, 25 };
var queryHashes = new Hashes(new[]{new HashedFingerprint(queryBuckets, 0, 0f), }, 1.48d, DateTime.Now, Enumerable.Empty<string>());
var queryHashes = new Hashes(new[]{new HashedFingerprint(queryBuckets, 0, 0f, Array.Empty<byte>()), }, 1.48d, DateTime.Now, Enumerable.Empty<string>());

var subFingerprints = modelService.Query(queryHashes, new LowLatencyQueryConfiguration()).ToList();

Expand All @@ -132,15 +132,15 @@ public void ReadSubFingerprintsByHashBucketsHavingThresholdWithClustersTest()
var secondTrack = new TrackInfo("id2", "title", "artist", new Dictionary<string, string>{{ "group-id", "second-group-id" }});
int[] firstTrackBuckets = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
int[] secondTrackBuckets = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
var firstHashData = new HashedFingerprint(firstTrackBuckets, 1, 0.928f);
var secondHashData = new HashedFingerprint(secondTrackBuckets, 1, 0.928f);
var firstHashData = new HashedFingerprint(firstTrackBuckets, 1, 0.928f, Array.Empty<byte>());
var secondHashData = new HashedFingerprint(secondTrackBuckets, 1, 0.928f, Array.Empty<byte>());

modelService.Insert(firstTrack, new Hashes(new[] { firstHashData }, 1.48d, DateTime.Now, Enumerable.Empty<string>()));
modelService.Insert(secondTrack, new Hashes(new[] { secondHashData }, 1.48d, DateTime.Now, Enumerable.Empty<string>()));

// query buckets are similar with 5 elements from first track and 4 elements from second track
int[] queryBuckets = { 3, 2, 5, 6, 7, 8, 7, 10, 11, 12, 13, 14, 15, 14, 17, 18, 19, 20, 21, 20, 23, 24, 25, 26, 25 };
var queryHashes = new Hashes(new[]{new HashedFingerprint(queryBuckets, 0, 0f), }, 1.48d, DateTime.Now, Enumerable.Empty<string>());
var queryHashes = new Hashes(new[]{new HashedFingerprint(queryBuckets, 0, 0f, Array.Empty<byte>()), }, 1.48d, DateTime.Now, Enumerable.Empty<string>());
var subFingerprints = modelService.Query(queryHashes,
new DefaultQueryConfiguration
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public void ShouldInsertAndReadSubFingerprints()
.Select(sequenceNumber => new HashedFingerprint(
genericHashBuckets,
(uint)sequenceNumber,
sequenceNumber * 0.928f));
sequenceNumber * 0.928f,
Array.Empty<byte>()));

InsertHashedFingerprintsForTrack(hashedFingerprints, trackReference);

Expand Down Expand Up @@ -180,7 +181,7 @@ public async Task ReadHashDataByTrackTest()

private static Func<SubFingerprintData, HashedFingerprint> ToHashedFingerprint()
{
return _ => new HashedFingerprint(_.Hashes, _.SequenceNumber, _.SequenceAt);
return _ => new HashedFingerprint(_.Hashes, _.SequenceNumber, _.SequenceAt, _.OriginalPoint);
}

private void InsertHashedFingerprintsForTrack(IEnumerable<HashedFingerprint> hashedFingerprints, IModelReference trackReference)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public async Task QueryIsBuiltFromFileCorrectly()
var hashedFingerprints =new Hashes(new List<HashedFingerprint>(
new[]
{
new HashedFingerprint(GenericHashBuckets(), 0, 0),
new HashedFingerprint(GenericHashBuckets(), 1, 0.928f),
new HashedFingerprint(GenericHashBuckets(), 2, 0.928f * 2)
new HashedFingerprint(GenericHashBuckets(), 0, 0, Array.Empty<byte>()),
new HashedFingerprint(GenericHashBuckets(), 1, 0.928f, Array.Empty<byte>()),
new HashedFingerprint(GenericHashBuckets(), 2, 0.928f * 2, Array.Empty<byte>())
}), 0.928 * 3, DateTime.Now, Enumerable.Empty<string>());

fingerprintCommandBuilder.Setup(builder => builder.BuildFingerprintCommand()).Returns(fingerprintingSource.Object);
Expand All @@ -91,9 +91,9 @@ public async Task QueryIsBuiltFromFileStartingAtAtSpecificSecondCorrectly()
var hashDatas = new Hashes(new List<HashedFingerprint>(
new[]
{
new HashedFingerprint(GenericHashBuckets(), 0, 0),
new HashedFingerprint(GenericHashBuckets(), 1, 0.928f),
new HashedFingerprint(GenericHashBuckets(), 2, 0.928f * 2)
new HashedFingerprint(GenericHashBuckets(), 0, 0, Array.Empty<byte>()),
new HashedFingerprint(GenericHashBuckets(), 1, 0.928f, Array.Empty<byte>()),
new HashedFingerprint(GenericHashBuckets(), 2, 0.928f * 2, Array.Empty<byte>())
}), 0.928 * 3, DateTime.Now, Enumerable.Empty<string>());
fingerprintCommandBuilder.Setup(builder => builder.BuildFingerprintCommand()).Returns(fingerprintingSource.Object);
fingerprintingSource.Setup(source => source.From(pathToFile, secondsToQuery, startAtSecond)).Returns(withAlgorithmConfiguration.Object);
Expand Down
28 changes: 14 additions & 14 deletions src/SoundFingerprinting.Tests/Unit/Data/HashesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ public void ShouldMerge()
float one = 8192f / 5512;
var a = new Hashes(new List<HashedFingerprint>
{
new HashedFingerprint(new[] {1}, 1, one),
new HashedFingerprint(new[] {1}, 2, 2 * one),
new HashedFingerprint(new[] {1}, 0, 0)
new HashedFingerprint(new[] {1}, 1, one, Array.Empty<byte>()),
new HashedFingerprint(new[] {1}, 2, 2 * one, Array.Empty<byte>()),
new HashedFingerprint(new[] {1}, 0, 0, Array.Empty<byte>())
}, one * 3 + FingerprintCount, DateTime.Parse("01/15/2019 10:00:00", dtfi), Enumerable.Empty<string>());

var b = new Hashes(new List<HashedFingerprint>
{
new HashedFingerprint(new[] {2}, 1, one),
new HashedFingerprint(new[] {2}, 2, 2 * one),
new HashedFingerprint(new[] {2}, 0, 0)
new HashedFingerprint(new[] {2}, 1, one, Array.Empty<byte>()),
new HashedFingerprint(new[] {2}, 2, 2 * one, Array.Empty<byte>()),
new HashedFingerprint(new[] {2}, 0, 0, Array.Empty<byte>())
},
one * 3 + FingerprintCount, DateTime.Parse("01/15/2019 10:00:01", dtfi), Enumerable.Empty<string>());

Expand All @@ -50,9 +50,9 @@ public void ShouldMergeCorrectly()
float acc = 8192 / 5512f;
var a = Hashes.Empty;
var dateTime = DateTime.Now;
var b = new Hashes(new List<HashedFingerprint>(new[] {new HashedFingerprint(new[] {1}, 0, 0)}), acc, dateTime, Enumerable.Empty<string>());
var c = new Hashes(new List<HashedFingerprint>(new[] {new HashedFingerprint(new[] {2}, 0, 0f)}), acc, dateTime.AddSeconds(acc), Enumerable.Empty<string>());
var d = new Hashes(new List<HashedFingerprint>(new[] {new HashedFingerprint(new[] {3}, 0, 0f)}), acc, dateTime.AddSeconds(2 * acc), Enumerable.Empty<string>());
var b = new Hashes(new List<HashedFingerprint>(new[] {new HashedFingerprint(new[] {1}, 0, 0f, Array.Empty<byte>())}), acc, dateTime, Enumerable.Empty<string>());
var c = new Hashes(new List<HashedFingerprint>(new[] {new HashedFingerprint(new[] {2}, 0, 0f, Array.Empty<byte>())}), acc, dateTime.AddSeconds(acc), Enumerable.Empty<string>());
var d = new Hashes(new List<HashedFingerprint>(new[] {new HashedFingerprint(new[] {3}, 0, 0f, Array.Empty<byte>())}), acc, dateTime.AddSeconds(2 * acc), Enumerable.Empty<string>());

Assert.IsTrue(a.MergeWith(b, out var x));
Assert.IsTrue(x.MergeWith(c, out var y));
Expand All @@ -73,8 +73,8 @@ public void ShouldMergeLongSequences()
float one = 8192f / 5512;
for (int i = 0; i < 100; ++i)
{
first.Add(new HashedFingerprint(new[] {1}, (uint) i, i * one));
second.Add(new HashedFingerprint(new[] {2}, (uint) i, i * one));
first.Add(new HashedFingerprint(new[] {1}, (uint) i, i * one, Array.Empty<byte>()));
second.Add(new HashedFingerprint(new[] {2}, (uint) i, i * one, Array.Empty<byte>()));
}

var r = new Random();
Expand Down Expand Up @@ -105,14 +105,14 @@ public void CantMergeSinceTheGapIsTooBig()
var dtfi = CultureInfo.GetCultureInfo("en-US").DateTimeFormat;
var a = new Hashes(new List<HashedFingerprint>
{
new HashedFingerprint(new[] {1}, 0, 0)
new HashedFingerprint(new[] {1}, 0, 0, Array.Empty<byte>())
},
FingerprintCount,
DateTime.Parse("01/15/2019 10:00:00", dtfi), Enumerable.Empty<string>());

var b = new Hashes(new List<HashedFingerprint>
{
new HashedFingerprint(new[] {2}, 0, 0)
new HashedFingerprint(new[] {2}, 0, 0, Array.Empty<byte>())
},
FingerprintCount,
DateTime.Parse("01/15/2019 10:01:00", dtfi), Enumerable.Empty<string>());
Expand Down Expand Up @@ -170,7 +170,7 @@ private static List<HashedFingerprint> GetHashedFingerprints()
hashes[j] = hash;
}

list.Add(new HashedFingerprint(hashes, (uint) (i + 1), i * 1.48f));
list.Add(new HashedFingerprint(hashes, (uint) (i + 1), i * 1.48f, Array.Empty<byte>()));
}

return list;
Expand Down
3 changes: 2 additions & 1 deletion src/SoundFingerprinting.Tests/Unit/FingerprintServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace SoundFingerprinting.Tests.Unit
{
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -56,7 +57,7 @@ public void CreateFingerprints()
waveletDecomposition.Setup(service => service.DecomposeImageInPlace(It.IsAny<float[]>(), 128, 32, fingerprintConfig.HaarWaveletNorm));
fingerprintDescriptor.Setup(descriptor => descriptor.ExtractTopWavelets(It.IsAny<float[]>(), fingerprintConfig.TopWavelets, It.IsAny<ushort[]>())).Returns(new TinyFingerprintSchema(8192).SetTrueAt(0, 1));
localitySensitiveHashingAlgorithm.Setup(service => service.Hash(It.IsAny<Fingerprint>(), fingerprintConfig.HashingConfig))
.Returns(new HashedFingerprint(new int[0], 1, 0f));
.Returns(new HashedFingerprint(new int[0], 1, 0f, Array.Empty<byte>()));

var fingerprints = fingerprintService.CreateFingerprintsFromAudioSamples(samples, fingerprintConfig)
.OrderBy(f => f.SequenceNumber)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void ShouldSerializeModelReferenceProviders()
[Test]
public void ShouldSerializeHashes()
{
var fingerprints = new[] {new HashedFingerprint(new[] {1, 2, 3, 4, 5}, 0, 0)};
var fingerprints = new[] {new HashedFingerprint(new[] {1, 2, 3, 4, 5}, 0, 0, Array.Empty<byte>())};
var origins = new[] {"test"};
var hashes = new Hashes(fingerprints, 1.48, DateTime.Now, origins, "CNN");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace SoundFingerprinting.Tests.Unit.InMemory
{
using System;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -30,7 +31,7 @@ public void ShouldInsertEntriesInThreadSafeManner()
var trackReference = new ModelReference<int>(i);
for (int j = 0; j < subFingerprintsPerTrack; ++j)
{
var hashed = new HashedFingerprint(longs, (uint)j, j * one);
var hashed = new HashedFingerprint(longs, (uint)j, j * one, Array.Empty<byte>());
storage.AddHashedFingerprint(hashed, trackReference);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void ShouldAccumulateResults()

Parallel.For(0, runs, i =>
{
var hashed = new HashedFingerprint(new int[0], (uint)i, i * 0.05f);
var hashed = new HashedFingerprint(new int[0], (uint)i, i * 0.05f, Array.Empty<byte>());
var candidate = new SubFingerprintData(new int[0], (uint)i, i * 0.07f, new ModelReference<uint>((uint)i), references[i % references.Length]);
groupedQueryResults.Add(hashed, candidate, i);
});
Expand Down Expand Up @@ -66,7 +66,7 @@ public void MatchesShouldBeOrderedByQueryAt()

Parallel.For(0, runs, i =>
{
var hashed = new HashedFingerprint(new int[0], (uint)i, i);
var hashed = new HashedFingerprint(new int[0], (uint)i, i, Array.Empty<byte>());
var candidate = new SubFingerprintData(new int[0], (uint)i, runs - i, new ModelReference<uint>((uint)i), reference);
groupedQueryResults.Add(hashed, candidate, i);
});
Expand All @@ -91,7 +91,7 @@ public void SameQueryHashGeneratesMultipleTrackMatches()
Parallel.For(0, runs, i =>
{
counts[i] = random.Next(5, 10);
var queryPoint = new HashedFingerprint(new int[25], (uint)i, i * 1.48f);
var queryPoint = new HashedFingerprint(new int[25], (uint)i, i * 1.48f, Array.Empty<byte>());
for (int j = 0; j < counts[i]; ++j)
{
var dbPoint = new SubFingerprintData(new int[25], (uint)k, k * 0.01f, new ModelReference<uint>((uint)Interlocked.Increment(ref k)), trackRef);
Expand Down
Loading

0 comments on commit 1390c08

Please sign in to comment.