Skip to content

Commit

Permalink
Add signature evolution samples to SignatureComparisonTests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekseyMartynov committed Sep 16, 2023
1 parent bea0bd3 commit be82d47
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 15 deletions.
8 changes: 7 additions & 1 deletion Tagging/PeakFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ static readonly float
LOG_MIN_MAGN_SQUARED = MathF.Log(MIN_MAGN_SQUARED);

readonly Analysis Analysis;
readonly bool Interpolation;
readonly IReadOnlyList<List<PeakInfo>> Bands;

public PeakFinder(Analysis analysis) {
public PeakFinder(Analysis analysis, bool interpolation = true) {
analysis.SetStripeAddedCallback(Analysis_StripeAddedCallback);

Analysis = analysis;
Interpolation = interpolation;

Bands = Enumerable.Range(0, BAND_FREQS.Count - 1)
.Select(_ => new List<PeakInfo>())
Expand Down Expand Up @@ -98,6 +100,10 @@ static readonly float
}

PeakInfo CreatePeakAt(int stripe, int bin) {
if(!Interpolation) {
return new PeakInfo(stripe, bin, GetLogMagnitude(stripe, bin));
}

// Quadratic Interpolation of Spectral Peaks
// https://stackoverflow.com/a/59140547
// https://ccrma.stanford.edu/~jos/sasp/Quadratic_Interpolation_Spectral_Peaks.html
Expand Down
39 changes: 32 additions & 7 deletions Tagging/Sig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,42 @@ static class Sig {
using var mem = new MemoryStream(data);
using var reader = new BinaryReader(mem);

if(reader.ReadUInt32() != 0xcafe2580)
throw new NotSupportedException();
var legacyFormat = false;

mem.Position = 7 * 4;
sampleRate = SampleRateFromCode(reader.ReadInt32() >> 27);
if(reader.ReadUInt32() != 0xcafe2580) {
if(reader.ReadUInt32() == 0x789abc05) {
legacyFormat = true;
} else {
throw new NotSupportedException();
}
}

mem.Position = 10 * 4;
sampleCount = reader.ReadInt32();
if(legacyFormat) {
sampleRate = 16000;

mem.Position = 14 * 4;
mem.Position = 21 * 4;
sampleCount = 2 * reader.ReadInt32();

mem.Position = 26 * 4;
} else {
mem.Position = 7 * 4;
sampleRate = SampleRateFromCode(reader.ReadInt32() >> 27);

mem.Position = 10 * 4;
sampleCount = reader.ReadInt32();

mem.Position = 14 * 4;
}

var writableBands = new List<PeakInfo>[4];
for(var i = 0; i < 4; i++)
writableBands[i] = new();

while(mem.Position < mem.Length) {
if(legacyFormat) {
mem.Position += 4;
}

var fat = false;
var bandIndex = 0;
var header = reader.ReadInt32();
Expand All @@ -149,6 +169,11 @@ static class Sig {
}

var len = reader.ReadInt32();

if(legacyFormat) {
mem.Position += 3 * 4;
}

var end = mem.Position + len;
var stripe = 0;

Expand Down
18 changes: 11 additions & 7 deletions Test/SignatureComparisonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ namespace Project.Test {

public class SignatureComparisonTests {

[Fact]
public void Ref_SigX_10_1_3() {
[Theory]
[InlineData("10.1.3")] // APK v13.45
[InlineData("7.2.0")] // APK v8.66
[InlineData("6.0.4", false)] // APK v8.3
[InlineData("5.1.0", false, 3)] // APK v7.11
public void Ref_SigX(string version, bool interpolation = true, int bandCount = 4) {
CreateFromFile(
Path.Combine(TestHelper.DATA_DIR, "test.mp3"),
interpolation,
out var mySampleCount,
out var myRemainingSampleCount,
out var myBands
);

LoadBinary(
// Generated using libsigx.so from Android app v13.45
Path.Combine(TestHelper.DATA_DIR, "test-sigx-10.1.3.bin"),
Path.Combine(TestHelper.DATA_DIR, $"test-sigx-{version}.bin"),
out var refSampleCount,
out var refBands
);
Expand All @@ -37,7 +41,7 @@ public class SignatureComparisonTests {
var myMagnList = new List<double>();
var refMagnList = new List<double>();

var myPeaks = myBands.SelectMany(i => i).ToList();
var myPeaks = myBands.Take(bandCount).SelectMany(i => i).ToList();
var refPeaks = refBands.SelectMany(i => i).ToList();

//var tsvMy = ToTSV(myPeaks);
Expand Down Expand Up @@ -68,9 +72,9 @@ public class SignatureComparisonTests {
Assert.True(Math.Abs(magnFitIntercept) < 10);
}

static void CreateFromFile(string path, out int sampleCount, out int remainingSampleCount, out Bands bands) {
static void CreateFromFile(string path, bool interpolation, out int sampleCount, out int remainingSampleCount, out Bands bands) {
var analysis = new Analysis();
var finder = new PeakFinder(analysis);
var finder = new PeakFinder(analysis, interpolation);

using var captureHelper = new FileCaptureHelper(path);
captureHelper.Start();
Expand Down
Binary file added TestData/test-sigx-5.1.0.bin
Binary file not shown.
Binary file added TestData/test-sigx-6.0.4.bin
Binary file not shown.
Binary file added TestData/test-sigx-7.2.0.bin
Binary file not shown.

0 comments on commit be82d47

Please sign in to comment.