From 8e6310a62886d82ee728be4a3348fce9ab0f90ef Mon Sep 17 00:00:00 2001 From: AlekseyMartynov Date: Mon, 11 Sep 2023 12:04:00 +0300 Subject: [PATCH] `sampleCount` difference puzzle (9dd7b3680) solved --- Tagging/Sig.cs | 5 +---- Test/SignatureComparisonTests.cs | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Tagging/Sig.cs b/Tagging/Sig.cs index 6ae90e1..a047fcc 100644 --- a/Tagging/Sig.cs +++ b/Tagging/Sig.cs @@ -10,9 +10,6 @@ namespace Project; static class Sig { public static byte[] Write(int sampleRate, int sampleCount, PeakFinder finder) { - // https://github.com/marin-m/SongRec/blob/0.3.2/src/fingerprinting/signature_format.rs#L238 - var sampleCountPadding = sampleRate * 6 / 25; - using(var mem = new MemoryStream()) using(var writer = new BinaryWriter(mem)) { writer.Write(0xCAFE2580); // Dialling "2580" on your phone and holding it up to the music, https://www.shazam.com/company @@ -25,7 +22,7 @@ public static byte[] Write(int sampleRate, int sampleCount, PeakFinder finder) { writer.Write(GetSampleRateCode(sampleRate) << 27); writer.Write(0); writer.Write(0); - writer.Write(sampleCount + sampleCountPadding); + writer.Write(sampleCount); writer.Write(0x007C0000); writer.Write(0x40000000); writer.Write(-1); diff --git a/Test/SignatureComparisonTests.cs b/Test/SignatureComparisonTests.cs index 6abb1db..f6e7c4d 100644 --- a/Test/SignatureComparisonTests.cs +++ b/Test/SignatureComparisonTests.cs @@ -16,6 +16,7 @@ public void Ref_SigX_10_1_3() { CreateFromWaveFile( Path.Combine(TestHelper.DATA_DIR, "test2.wav"), out var mySampleCount, + out var myRemainingSampleCount, out var myPeaks ); @@ -26,12 +27,17 @@ out var myPeaks out var refPeaks ); - Assert.Equal(refSampleCount, mySampleCount); + // Official signature has more peaks + // Most of them are at edges < RADIUS_TIME - var anchorBin = 38; - var myStripe = myPeaks.FindOne(anchorBin).StripeIndex; - var refStripe = refPeaks.FindOne(anchorBin).StripeIndex; - var refStripeOffset = refStripe - myStripe; + // Possible explanation for padding in official signature + // start - first chunk completes the FFT window so it is immediately ready for analysis + // end - to absorb remaining samples? for symmetry? + + var refSampleCountPadding = Analysis.WINDOW_SIZE - Analysis.CHUNK_SIZE; + var refStripeOffset = refSampleCountPadding / Analysis.CHUNK_SIZE; + + Assert.Equal(refSampleCount - 2 * refSampleCountPadding, mySampleCount + myRemainingSampleCount); var hitCount = 0; var missCount = 0; @@ -61,7 +67,7 @@ out var refPeaks Assert.True(Math.Abs(magnFitIntercept) < 10); } - static void CreateFromWaveFile(string path, out int sampleCount, out IReadOnlyList peaks) { + static void CreateFromWaveFile(string path, out int sampleCount, out int remainingSampleCount, out IReadOnlyList peaks) { var analysis = new Analysis(); var finder = new PeakFinder(analysis); @@ -69,7 +75,6 @@ static void CreateFromWaveFile(string path, out int sampleCount, out IReadOnlyLi var sampleProvider = wave.ToSampleProvider(); var chunk = new float[Analysis.CHUNK_SIZE]; - var remainingSampleCount = 0; while(true) { var readCount = sampleProvider.Read(chunk, 0, chunk.Length); @@ -82,7 +87,7 @@ static void CreateFromWaveFile(string path, out int sampleCount, out IReadOnlyLi analysis.AddChunk(chunk); } - var sigBytes = Sig.Write(Analysis.SAMPLE_RATE, analysis.ProcessedSamples + remainingSampleCount, finder); + var sigBytes = Sig.Write(Analysis.SAMPLE_RATE, analysis.ProcessedSamples, finder); LoadBinary(sigBytes, out sampleCount, out peaks); }