Пример #1
0
        public void ShouldBufferCorrectly()
        {
            // below instruction repeat 128 times
            // thus, last entry has to have at least 2048 elements
            // window  -----
            //          -----
            //                                 ----- // last one
            // 128 * 64 (overlap) = 8192 + 2048
            //
            // the length of 1 fingerprint is 10240 as this is the minimal length that will allow generating full 128 * 32 log-image
            var stride = new IncrementalStaticStride(256);

            const int minSize            = 10240;
            var       realtimeAggregator = new RealtimeAudioSamplesAggregator(stride, minSize);

            float[] prev = new float[minSize];
            for (int i = 0; i < 100; ++i)
            {
                float[] next         = TestUtilities.GenerateRandomFloatArray(minSize);
                var     audioSamples = realtimeAggregator.Aggregate(new AudioSamples(next, "cnn", 5512));
                if (i == 0)
                {
                    Assert.AreSame(audioSamples.Samples, next);
                    prev = next;
                    continue;
                }

                VerifyEndingsAreAttached(prev, next, audioSamples, minSize, stride.NextStride);
                prev = next;
            }
        }
        private async Task <double> QueryAndHash(CancellationToken cancellationToken, IQueryFingerprintService service)
        {
            var realtimeSamplesAggregator = new RealtimeAudioSamplesAggregator(configuration.Stride, MinSamplesForOneFingerprint);
            var resultsAggregator         = new StatefulRealtimeResultEntryAggregator(configuration.ResultEntryFilter, configuration.PermittedGap);

            double queryLength = 0d;

            while (!realtimeCollection.IsFinished && !cancellationToken.IsCancellationRequested)
            {
                AudioSamples audioSamples;
                try
                {
                    if (!realtimeCollection.TryTake(out audioSamples, configuration.MillisecondsDelay, cancellationToken))
                    {
                        continue;
                    }
                }
                catch (OperationCanceledException)
                {
                    return(queryLength);
                }

                if (audioSamples.SampleRate != SupportedFrequency)
                {
                    throw new ArgumentException($"{nameof(audioSamples)} should be provided down sampled to {SupportedFrequency}Hz");
                }

                queryLength += audioSamples.Duration;

                var prefixed = realtimeSamplesAggregator.Aggregate(audioSamples);
                var hashes   = await CreateQueryFingerprints(fingerprintCommandBuilder, prefixed);

                InvokeHashedFingerprintsCallback(hashes);

                if (!TryQuery(service, hashes, out var queryResults))
                {
                    continue;
                }

                foreach (var queryResult in queryResults)
                {
                    var aggregatedResult = resultsAggregator.Consume(queryResult.ResultEntries, prefixed.Duration);
                    InvokeSuccessHandler(aggregatedResult);
                    InvokeDidNotPassFilterHandler(aggregatedResult);
                }
            }

            return(queryLength);
        }
Пример #3
0
        public void ShouldBufferFirstEntryCorrectly()
        {
            const int minSize     = 10240;
            const int incrementBy = 1024;

            var realtimeAggregator = new RealtimeAudioSamplesAggregator(new IncrementalStaticStride(incrementBy), minSize);

            var a = realtimeAggregator.Aggregate(new AudioSamples(TestUtilities.GenerateRandomFloatArray(minSize), "cnn", 5512));

            Assert.AreEqual(minSize, a.Samples.Length);

            var b = realtimeAggregator.Aggregate(new AudioSamples(TestUtilities.GenerateRandomFloatArray(minSize), "cnn", 5512));

            Assert.AreEqual(minSize - incrementBy + minSize, b.Samples.Length);
        }
Пример #4
0
        private async Task <double> QueryAndHash(CancellationToken cancellationToken, IQueryFingerprintService service)
        {
            var realtimeSamplesAggregator = new RealtimeAudioSamplesAggregator(configuration.Stride, MinSamplesForOneFingerprint);
            var resultsAggregator         = new StatefulRealtimeResultEntryAggregator(configuration.ResultEntryFilter, configuration.QueryConfiguration);

            double       queryLength = 0d;
            AudioSamples?audioSamples;

            while ((audioSamples = await realtimeCollection.TryReadAsync(cancellationToken)) != null)
            {
                if (audioSamples.SampleRate != SupportedFrequency)
                {
                    throw new ArgumentException($"{nameof(audioSamples)} should be provided down sampled to {SupportedFrequency}Hz");
                }

                queryLength += audioSamples.Duration;

                var prefixed = realtimeSamplesAggregator.Aggregate(audioSamples);
                var hashes   = await CreateQueryFingerprints(fingerprintCommandBuilder, prefixed);

                InvokeHashedFingerprintsCallback(hashes);

                if (!TryQuery(service, hashes, out var queryResults))
                {
                    continue;
                }

                foreach (var queryResult in queryResults)
                {
                    var aggregatedResult = resultsAggregator.Consume(queryResult.ResultEntries, hashes.DurationInSeconds, audioSamples.Duration - prefixed.Duration);
                    InvokeSuccessHandler(aggregatedResult);
                    InvokeDidNotPassFilterHandler(aggregatedResult);
                }
            }

            var purged = resultsAggregator.Consume(Enumerable.Empty <ResultEntry>(), 0, 0);

            InvokeSuccessHandler(purged);
            InvokeDidNotPassFilterHandler(purged);
            return(queryLength);
        }
        private async Task <double> QueryAndHash(CancellationToken cancellationToken, IQueryFingerprintService service)
        {
            var realtimeSamplesAggregator = new RealtimeAudioSamplesAggregator(configuration.Stride, MinSamplesForOneFingerprint);
            var resultsAggregator         = new StatefulRealtimeResultEntryAggregator(configuration.ResultEntryFilter, configuration.QueryConfiguration);

            double queryLength = 0d;

            await foreach (var audioSamples in realtimeCollection.WithCancellation(cancellationToken))
            {
                if (audioSamples.SampleRate != SupportedFrequency)
                {
                    throw new ArgumentException($"{nameof(audioSamples)} should be provided down sampled to {SupportedFrequency}Hz");
                }

                queryLength += audioSamples.Duration;

                var prefixed = realtimeSamplesAggregator.Aggregate(audioSamples);
                var hashes   = (await CreateQueryFingerprints(fingerprintCommandBuilder, prefixed)).WithStreamId(streamId);
                hashes = hashesInterceptor(hashes).WithTimeOffset(audioSamples.Duration - hashes.DurationInSeconds);

                if (!TryQuery(service, hashes, out var queryResults))
                {
                    continue;
                }

                foreach (var queryResult in queryResults)
                {
                    var aggregatedResult = resultsAggregator.Consume(queryResult.ResultEntries, queryResult.QueryHashes.DurationInSeconds, queryResult.QueryHashes.TimeOffset);
                    InvokeSuccessHandler(aggregatedResult);
                    InvokeDidNotPassFilterHandler(aggregatedResult);
                }
            }

            var purged = resultsAggregator.Purge();

            InvokeSuccessHandler(purged);
            InvokeDidNotPassFilterHandler(purged);
            return(queryLength);
        }
Пример #6
0
        public void ShouldNotAllowSmallerThanMinSizeEntriesAtTheInput()
        {
            var realtimeAggregator = new RealtimeAudioSamplesAggregator(new IncrementalStaticStride(256), 10240);

            Assert.Throws <ArgumentException>(() => realtimeAggregator.Aggregate(new AudioSamples(TestUtilities.GenerateRandomFloatArray(10240 - 1), "cnn", 5512)));
        }