Skip to content

Commit

Permalink
Use test.mp3 in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekseyMartynov committed Sep 13, 2023
1 parent 47f4e1d commit 634df52
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CaptureHelpers/FileCaptureHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class FileCaptureHelper : ICaptureHelper {

WaveStream WaveStream;

public FileCaptureHelper(string filePath, TimeSpan startTime) {
public FileCaptureHelper(string filePath, TimeSpan startTime = default) {
FilePath = filePath;
StartTime = startTime;
}
Expand Down
16 changes: 8 additions & 8 deletions Test/SignatureComparisonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ public class SignatureComparisonTests {

[Fact]
public void Ref_SigX_10_1_3() {
CreateFromWaveFile(
Path.Combine(TestHelper.DATA_DIR, "test2.wav"),
CreateFromFile(
Path.Combine(TestHelper.DATA_DIR, "test.mp3"),
out var mySampleCount,
out var myRemainingSampleCount,
out var myBands
);

LoadBinary(
// Generated using libsigx.so from Android app v13.45
Path.Combine(TestHelper.DATA_DIR, "test2-sigx-10.1.3.bin"),
Path.Combine(TestHelper.DATA_DIR, "test-sigx-10.1.3.bin"),
out var refSampleCount,
out var refBands
);
Expand Down Expand Up @@ -63,25 +63,25 @@ public class SignatureComparisonTests {
refMagnList.Add(refPeak.LogMagnitude);
}

Assert.True(1d * hitCount / myPeaks.Count > 0.8);
Assert.True(1d * hitCount / myPeaks.Count > 0.75); // TODO

var (magnFitIntercept, magnFitSlope) = Fit.Line(myMagnList.ToArray(), refMagnList.ToArray());

Assert.True(Math.Abs(magnFitSlope - 1) < 0.001);
Assert.True(Math.Abs(magnFitIntercept) < 10);
}

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

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

var chunk = new float[Analysis.CHUNK_SIZE];

while(true) {
var readCount = sampleProvider.Read(chunk, 0, chunk.Length);
var readCount = captureHelper.SampleProvider.Read(chunk, 0, chunk.Length);

if(readCount < chunk.Length) {
remainingSampleCount = readCount;
Expand Down
17 changes: 17 additions & 0 deletions Test/TestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if DEBUG
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -16,5 +18,20 @@ static class TestHelper {
);
}

public static void SaveRaw(ISampleProvider sampleProvider, string path) {
using var rawFile = File.OpenWrite(path);

var wave = new SampleToWaveProvider16(sampleProvider);
var bufLen = 4096;
var buf = new byte[bufLen];

while(true) {
var readLen = wave.Read(buf, 0, bufLen);
rawFile.Write(buf, 0, readLen);
if(readLen < bufLen)
break;
}
}

}
#endif
Binary file added TestData/test-sigx-10.1.3.bin
Binary file not shown.
Binary file removed TestData/test2-sigx-10.1.3.bin
Binary file not shown.
Binary file removed TestData/test2.wav
Binary file not shown.

0 comments on commit 634df52

Please sign in to comment.