Skip to content

Commit

Permalink
Adds test for BARLT metadata parsing
Browse files Browse the repository at this point in the history
FLAC custom metadata fields are output as is by ffprobe. Because they did not have an `=` character the `Substring` call in `GetInfo` threw an exception because the `IndexOf` call returned `-1` and that was not a valid index for a string.

I've added test audio for a BARLT and a test dedicated to parsing that metadata. At this stage we have no actual use for the metadata.
  • Loading branch information
atruskie committed Aug 26, 2019
1 parent 68d58fe commit efc4a44
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 7 deletions.
4 changes: 1 addition & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ docs/**/*.jpg filter=lfs diff=lfs merge=lfs -text
*.dll binary
*.zip filter=lfs diff=lfs merge=lfs -text
*.mp3 filter=lfs diff=lfs merge=lfs -text

# all binaries in lib should be tracked by git-lfs
lib/**/*.exe filter=lfs diff=lfs merge=lfs -text
lib/**/*.dll filter=lfs diff=lfs merge=lfs -text
lib/audio-utils/macosx/**/* filter=lfs diff=lfs merge=lfs -text

# assets in tests/Fixtures should generally be in lfs
tests/Fixtures/**/*.zip filter=lfs diff=lfs merge=lfs -text
tests/Fixtures/**/*.csv filter=lfs diff=lfs merge=lfs -text
tests/Fixtures/**/*.bin filter=lfs diff=lfs merge=lfs -text
tests/Fixtures/**/*.json filter=lfs diff=lfs merge=lfs -text
tests/Fixtures/**/*.png filter=lfs diff=lfs merge=lfs -text
tests/Fixtures/**/*.png filter=lfs diff=lfs merge=lfs -text
1 change: 1 addition & 0 deletions AudioAnalysis.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=dedupe/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ffmpeg/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ffprobe/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=flac/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Redirector/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=spectrograms/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Submatrix/@EntryIndexedValue">True</s:Boolean>
Expand Down
6 changes: 6 additions & 0 deletions src/Acoustics.Shared/Extensions/ExtensionsString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,12 @@ public static string NormalizeToCrLf(this string str)
return normalized;
}

public static string[] SplitOnAnyNewLine(this string str)
{
string[] newLines = { "\r\n", "\n" };
return str.Split(newLines, StringSplitOptions.RemoveEmptyEntries);
}

public static string FormatList(this IEnumerable<string> strings)
{
var builder = new StringBuilder("\n", 1000);
Expand Down
7 changes: 5 additions & 2 deletions src/Acoustics.Tools/Audio/FfmpegAudioUtility.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="FfmpegAudioUtility.cs" company="QutEcoacoustics">
// <copyright file="FfmpegAudioUtility.cs" company="QutEcoacoustics">
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group).
// </copyright>

Expand Down Expand Up @@ -270,7 +270,10 @@ protected override AudioUtilityInfo GetInfo(FileInfo source, ProcessRunner proce
// start of a block
currentBlockName = line.Trim('[', ']');
}
else

// make sure line actually has a delimiter (and at least one character for a key)
var delimiterIndex = line.IndexOf('=');
if (delimiterIndex > 0)
{
// key=value
var key = currentBlockName + " " + line.Substring(0, line.IndexOf('='));
Expand Down
1 change: 1 addition & 0 deletions tests/Acoustics.Test/Acoustics.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@
<Compile Include="Tools\AudioUtilityWavpackTests.cs" />
<Compile Include="Tools\AudioUtilityChannelSelection\AudioUtilityChannelTests.cs" />
<Compile Include="Tools\AudioUtilityWavTests.cs" />
<Compile Include="Tools\FfmpegUtilityTests.cs" />
<Compile Include="Tools\MasterAudioUtilityTests.cs" />
<Compile Include="EnumerableExtensionsTests.cs" />
<Compile Include="Tools\SoxUtilityTests.cs" />
Expand Down
16 changes: 14 additions & 2 deletions tests/Acoustics.Test/TestHelpers/TestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Acoustics.Test.TestHelpers
namespace Acoustics.Test.TestHelpers
{
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -261,7 +261,19 @@ public static class TestHelper
//BitsPerSample = 16,
}
},
};
{
"20190401T000000+1000_REC [19.21440152.8811].flac",
new AudioUtilityInfo()
{
Duration = TimeSpan.FromSeconds(60.0),
SampleRate = 22050,
ChannelCount = 1,
BitsPerSecond = 170 * 8,
MediaType = MediaTypes.MediaTypeFlacAudio,
BitsPerSample = 16,
}
},
};

/// <summary>
/// Tests that an exception is thrown, and that it is of
Expand Down
71 changes: 71 additions & 0 deletions tests/Acoustics.Test/Tools/FfmpegUtilityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// <copyright file="FfmpegUtilityTests.cs" company="QutEcoacoustics">
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group).
// </copyright>

namespace Acoustics.Test.Tools
{
using System;
using System.Collections.Generic;
using System.Linq;
using Acoustics.Test.TestHelpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class FfmpegUtilityTests
{
[TestMethod]
public void FfmpegGracefullyIgnoresMetadataLinesOfUnexpectedFormat()
{
var utility = TestHelper.GetAudioUtilityFfmpeg();
var testFilename = "20190401T000000+1000_REC [19.21440152.8811].flac";
var testFile = PathHelper.ResolveAsset("BARLT", testFilename);
var testInfo = TestHelper.AudioDetails[testFilename];

// this used to fail because FLAC custom fields are not output in the standard
// ffmpeg `key=value` format.
var info = utility.Info(testFile);

Assert.IsNotNull(info);

// and we should be able to parse the data

var raw = @"Metadata:
SENSORUID : 000010
:
MICROPHONEBUILDDATE2: unknown
MICROPHONEUID2 : unknown
MICROPHONEUID1 : 000629
SENSORFIRMWAREVERSION: Firmware: V3.08
SENSORLOCATION : +19.2144 +152.8811
SDCARDCID : 9E42453531324742100000004D012BE5
MICROPHONEBUILDDATE1: 2019-02-22
MICROPHONETYPE2 : unknown
CHANNELGAIN1 : 50dB
MICROPHONETYPE1 : STD AUDIO MIC
BATTERYLEVEL : 100p 12.83V
RECORDINGEND : 2019-04-01 01:59:56
RECORDINGSTART : 2019-04-01 00:00:01
CHANNELGAIN2 : 0dB";

IEnumerable<(string Key, string Value)> Clean(string line)
{
var parts = line.Split(new[] { ':', ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2)
{
yield break;
}

yield return (parts[0], parts[1]);
}

var values = raw.SplitOnAnyNewLine().SelectMany(Clean);

foreach (var keyAndValue in values)
{
string ffmpegKey = "FORMAT TAG:" + keyAndValue.Key;
Assert.IsTrue(info.RawData.ContainsKey(ffmpegKey));
Assert.AreEqual(keyAndValue.Value, info.RawData[ffmpegKey]);
}
}
}
}
Git LFS file not shown
4 changes: 4 additions & 0 deletions tests/Fixtures/BARLT/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
20190401T000000+1000_REC [19.21440152.8811].flac:
Is a trimmed down version of an originally 2 hour long file.
It's not a true output of a BAR LT sensor because we've modified it with
audacity. It's enough though for us to test the metadata parsing.

0 comments on commit efc4a44

Please sign in to comment.