Skip to content

Commit

Permalink
'Fat' signature density research
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekseyMartynov committed Sep 12, 2023
1 parent 172024d commit b9ad17c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
39 changes: 30 additions & 9 deletions Tagging/Sig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,45 @@ public static void Read(byte[] data, out int sampleRate, out int sampleCount, ou
writableBands[i] = new();

while(mem.Position < mem.Length) {
var bandIndex = reader.ReadInt32() - 0x60030040;
if(bandIndex < 0 || bandIndex > 3)
throw new InvalidOperationException();
var fat = false;
var bandIndex = 0;
var header = reader.ReadInt32();

if(header == 0x60023e80) {
fat = true;
} else {
bandIndex = header - 0x60030040;
if(bandIndex < 0 || bandIndex > 3)
throw new InvalidOperationException();
}

var len = reader.ReadInt32();
var end = mem.Position + len;
var stripe = 0;

while(mem.Position < end) {
if(end - mem.Position >= 5) {
var x = reader.ReadByte();
if(x == 255) {
if(fat) {
stripe = reader.ReadInt32();
x = reader.ReadByte();
} else {
var x = reader.ReadByte();
if(x == 255) {
stripe = reader.ReadInt32();
x = reader.ReadByte();
}
stripe += x;
}

var word1 = reader.ReadUInt16();
var word2 = reader.ReadUInt16();

if(fat) {
mem.Position += 4;
(word1, word2) = (word2, word1);
}
stripe += x;

var magn = reader.ReadUInt16();
var bin = reader.ReadUInt16() / 64;
var magn = word1;
var bin = word2 / 64;

if(bin == 0 || magn == 0)
throw new InvalidOperationException();
Expand Down Expand Up @@ -228,6 +248,7 @@ static int GetSampleRateCode(int sampleRate) {
}

static int SampleRateFromCode(int code) {
code &= 0xf;
return code switch {
1 => 8000,
3 => 16000,
Expand Down
22 changes: 18 additions & 4 deletions Test/PeakDensityResearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,22 @@ public PeakDensityResearch(ITestOutputHelper output) {
}

[Fact]
public void Run() {
LoadPeakMap(out var bandedPeakRates);
public void Normal() {
Run("gabin-full-sigx-10.1.3.bin");
}

[Fact]
public void Fat() {
// Generated using libsigx.so from Android app v7.11
// SigType.SINGLE_FILE, SigOptions.FAT
// 'Fat' signature, presumably, contains raw peaks
// - before bin interpolation
// - before rate limit
Run("gabin-60s-sigx-5.1.0-fat.bin");
}

void Run(string fileName) {
LoadPeakMap(fileName, out var bandedPeakRates);

for(var i = 0; i < bandedPeakRates.Count; i++)
Output.WriteLine($"Band {i} peak rate {bandedPeakRates[i]:0.#} peak/sec");
Expand Down Expand Up @@ -70,8 +84,8 @@ public void Run() {
Output.WriteLine($"{bin}\t{stripe}");
}

void LoadPeakMap(out IReadOnlyList<double> bandedRates) {
var path = Path.Combine(TestHelper.DATA_DIR, "gabin-full-sigx-10.1.3.bin");
void LoadPeakMap(string fileName, out IReadOnlyList<double> bandedRates) {
var path = Path.Combine(TestHelper.DATA_DIR, fileName);

Sig.Read(
File.ReadAllBytes(path),
Expand Down
Binary file added TestData/gabin-60s-sigx-5.1.0-fat.bin
Binary file not shown.

0 comments on commit b9ad17c

Please sign in to comment.