Skip to content

Commit

Permalink
Making sure non-completed realtime query matches are released even if…
Browse files Browse the repository at this point in the history
… full track does not match.
  • Loading branch information
AddictedCS committed Sep 14, 2023
1 parent 97acf9d commit 46576fe
Showing 1 changed file with 64 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public RealtimeQueryCommandTest()
}

[Test]
public async Task RealtimeQueryShouldMatchOnlySelectedClusters()
public async Task RealtimeQueryShouldMatchOnlySelectedMetaFieldsFilters()
{
var modelService = new InMemoryModelService();
int count = 10, foundWithClusters = 0, foundWithWrongClusters = 0, testWaitTime = 3000;
Expand All @@ -56,7 +56,9 @@ public async Task RealtimeQueryShouldMatchOnlySelectedClusters()
modelService.Insert(new TrackInfo("312", "Bohemian Rhapsody", "Queen", new Dictionary<string, string>{{ "country", "USA" }}), hashes);

var cancellationTokenSource = new CancellationTokenSource(testWaitTime);
var wrong = QueryCommandBuilder.Instance.BuildRealtimeQueryCommand()
var wrong = QueryCommandBuilder
.Instance
.BuildRealtimeQueryCommand()
.From(SimulateRealtimeAudioQueryData(data, jitterLength: 0))
.WithRealtimeQueryConfig(config =>
{
Expand Down Expand Up @@ -93,8 +95,7 @@ public async Task RealtimeQueryStrideShouldBeUsed()
var modelService = new InMemoryModelService();
int staticStride = 1024;
double permittedGap = (double) minSamplesPerFingerprint / sampleRate;
int count = 10, found = 0, didNotPassThreshold = 0, fingerprintsCount = 0;
int testWaitTime = 3000;
int count = 10, found = 0, didNotPassThreshold = 0, fingerprintsCount = 0, testWaitTime = 3000;
var data = GenerateRandomAudioChunks(count, 1, DateTime.UtcNow);
var concatenated = Concatenate(data);
var hashes = await FingerprintCommandBuilder.Instance
Expand All @@ -107,7 +108,8 @@ public async Task RealtimeQueryStrideShouldBeUsed()
var collection = SimulateRealtimeAudioQueryData(data, jitterLength: 0);
var cancellationTokenSource = new CancellationTokenSource(testWaitTime);

double duration = await QueryCommandBuilder.Instance.BuildRealtimeQueryCommand()
double duration = await QueryCommandBuilder.Instance
.BuildRealtimeQueryCommand()
.From(collection)
.WithRealtimeQueryConfig(config =>
{
Expand All @@ -129,13 +131,69 @@ public async Task RealtimeQueryStrideShouldBeUsed()
Assert.AreEqual((double)count * minSamplesPerFingerprint / 5512, duration, 0.00001);
}

[Test]
public async Task ShouldCaptureRealtimeQueryResultThatOccursOnTheEdgeOfQueryMatches()
{
var modelService = new InMemoryModelService();
int staticStride = 512;
double permittedGap = (double) minSamplesPerFingerprint / sampleRate, queryLength = 0d;
int count = 10, found = 0, didNotPassThreshold = 0, hashesCount = 0, testWaitTime = 3000;
var data = GenerateRandomAudioChunks(count, 1, DateTime.UtcNow);
var concatenated = Concatenate(data);
var hashes = await FingerprintCommandBuilder.Instance
.BuildFingerprintCommand()
.From(concatenated)
.Hash();

modelService.Insert(new TrackInfo("312", "Bohemian Rhapsody", "Queen"), hashes);

// query samples contain all but last chunk of the track and more random samples that should not match
var querySamples = data
.Take(count - 1)
.Concat(GenerateRandomAudioChunks(count, seed: 100, relativeTo: DateTime.UtcNow.AddSeconds(minSizeChunkDuration * (count - 1)))).ToList();

var collection = SimulateRealtimeAudioQueryData(querySamples, jitterLength: 0);
var cancellationTokenSource = new CancellationTokenSource(testWaitTime);

await QueryCommandBuilder.Instance
.BuildRealtimeQueryCommand()
.From(collection)
.WithRealtimeQueryConfig(config =>
{
config.ResultEntryFilter = new TrackRelativeCoverageLengthEntryFilter(0.2d, waitTillCompletion: true);
config.QueryConfiguration.Audio.Stride = new IncrementalStaticStride(staticStride);
config.SuccessCallback = _ =>
{
// we want to capture match not when final results are purged
// making sure we call the accumulators even on empty results
if (hashesCount <= count + 1)
{
Interlocked.Increment(ref found);
}
};
config.DidNotPassFilterCallback = _ => Interlocked.Increment(ref didNotPassThreshold);
config.QueryConfiguration.Audio.PermittedGap = permittedGap;
return config;
})
.InterceptHashes(fingerprints =>
{
Interlocked.Increment(ref hashesCount);
return fingerprints;
})
.UsingServices(modelService)
.Query(cancellationTokenSource.Token);

Assert.AreEqual(1, found);
Assert.AreEqual(count * 2 - 1, hashesCount);
}

[Test]
public async Task ShouldQueryInRealtime()
{
var modelService = new InMemoryModelService();

double minSizeChunk = (double)minSamplesPerFingerprint / sampleRate; // length in seconds of one query chunk ~1.8577
const double totalTrackLength = 210; // length of the track 3 minutes 30 seconds.
const double totalTrackLength = 210; // length of the track 3 minutes 30 seconds.
int count = (int)Math.Round(totalTrackLength / minSizeChunk), fingerprintsCount = 0, queryMatchLength = 10, ongoingCalls = 0;
var data = GenerateRandomAudioChunks(count, seed: 1, DateTime.UtcNow);
var concatenated = Concatenate(data);
Expand Down

0 comments on commit 46576fe

Please sign in to comment.