Skip to content

Commit

Permalink
sampleCount difference puzzle (9dd7b36) solved
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekseyMartynov committed Sep 11, 2023
1 parent 6532a10 commit 8e6310a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
5 changes: 1 addition & 4 deletions Tagging/Sig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
21 changes: 13 additions & 8 deletions Test/SignatureComparisonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand All @@ -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;
Expand Down Expand Up @@ -61,15 +67,14 @@ out var refPeaks
Assert.True(Math.Abs(magnFitIntercept) < 10);
}

static void CreateFromWaveFile(string path, out int sampleCount, out IReadOnlyList<PeakInfo> peaks) {
static void CreateFromWaveFile(string path, out int sampleCount, out int remainingSampleCount, out IReadOnlyList<PeakInfo> peaks) {
var analysis = new Analysis();
var finder = new PeakFinder(analysis);

using var wave = new WaveFileReader(path);
var sampleProvider = wave.ToSampleProvider();

var chunk = new float[Analysis.CHUNK_SIZE];
var remainingSampleCount = 0;

while(true) {
var readCount = sampleProvider.Read(chunk, 0, chunk.Length);
Expand All @@ -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);
}

Expand Down

0 comments on commit 8e6310a

Please sign in to comment.