Skip to content

Commit

Permalink
Removes support for MP3 for SoX on OSX
Browse files Browse the repository at this point in the history
We debugged with a MacOS machine and could not find anyway of getting MP3 to work with our current version of SoX.

FFmpeg supports MP3 so we'll now fall back to that in all cases on OSX.

Also includes fixes to lfs targets that failed when a file was deleted but not yet updated in the git index.

Work done for #196
  • Loading branch information
atruskie committed Mar 30, 2020
1 parent a3091c0 commit 2a3f654
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 34 deletions.
3 changes: 0 additions & 3 deletions lib/audio-utils/osx-x64/sox/libmad.0.2.1.dylib

This file was deleted.

3 changes: 0 additions & 3 deletions lib/audio-utils/osx-x64/sox/libmad.dylib

This file was deleted.

3 changes: 0 additions & 3 deletions lib/audio-utils/osx-x64/sox/libmp3lame.0.dylib

This file was deleted.

3 changes: 0 additions & 3 deletions lib/audio-utils/osx-x64/sox/libmp3lame.dylib

This file was deleted.

4 changes: 2 additions & 2 deletions src/AP.RequireLfsAssets.targets
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<!-- Register our task that as something to run before standard build target -->
<Target Name="APRequireLfsAssets" BeforeTargets="PrepareForBuild">
<!-- <Message Text="[APVersionBeforeBuild] Debug: $(RuntimeIdentifier)" Importance="High" /> -->
<Exec Command="pwd; git lfs ls-files" ConsoleToMSBuild="True" EchoOff="false" StandardOutputImportance="high" WorkingDirectory="$(MSBuildThisFileDirectory)..">
<Exec Command="git lfs ls-files" ConsoleToMSBuild="True" EchoOff="false" StandardOutputImportance="low" WorkingDirectory="$(MSBuildThisFileDirectory)..">
<Output TaskParameter="ConsoleOutput" ItemName="FileStatus" />
</Exec>
<Error File="$([System.String]::new(%(FileStatus.Identity)).Substring(13).Trim())" Code="AP001" Text="Git LFS BLOB has not been restored and only contains a LFS pointer to the file." Condition="$([System.String]::new(%(FileStatus.Identity)).Contains(' - '))" ContinueOnError="ErrorAndContinue"/>
<Error File="$([System.String]::new(%(FileStatus.Identity)).Substring(13).Trim())" Code="AP001" Text="Git LFS BLOB has not been restored and only contains a LFS pointer to the file." Condition="$([System.String]::new(%(FileStatus.Identity)).Contains(' - ')) And Exists('$(MSBuildThisFileDirectory)../$([System.String]::new(%(FileStatus.Identity)).Substring(13).Trim())')" ContinueOnError="ErrorAndContinue"/>

<Error Condition="'$(MSBuildLastTaskResult)'=='false'" Text="AP build cannot continue there are Git LFS assets that have not been restored. Please follow the instructions at https://github.com/QutEcoacoustics/audio-analysis/blob/master/CONTRIBUTING.md#AP001" />
</Target>
Expand Down
2 changes: 1 addition & 1 deletion src/AP.VersionBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<Message Text="[APVersionBeforeBuild] BuildDate: $(BuildDate)" Importance="High" />
<Message Text="[APVersionBeforeBuild] SelfContained: $(MsBuildSelfContained), Build RuntimeIdentifier:$(MsBuildRuntimeIdentifer)" Importance="High" />
<Message Text="[APVersionBeforeBuild] git_version.ps1 stdout: %(VersionMetadata.Identity)" Importance="High" Condition="$(GeneratedMetadata) == ''"/>
<Error Text="AP Version script failed to set MsBuildRuntimeIdentifer. This value is needed for correct function in compiled code" Condition="$(MsBuildRuntimeIdentifer) == '' And $(SelfContained)" />
<Error Text="AP Version script failed to set MsBuildRuntimeIdentifer. This value is needed for correct function in compiled code" Condition="$(MsBuildRuntimeIdentifer) == '' And $(SelfContained)" />
<Error Text="AP Version metadata generation failed. Check you have PowerShell 6+ &amp; Git installed and available on the system PATH." Condition="$(GeneratedMetadata) == ''"/>
</Target>
</Project>
6 changes: 5 additions & 1 deletion src/Acoustics.Tools/Audio/MasterAudioUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ public override AudioUtilityInfo Info(FileInfo source)

info = this.Combine(this.wvunpackUtility.Info(source), this.ffmpegUtility.Info(source));
}
else if (mediaType == MediaTypes.MediaTypeMp3 || mediaType == MediaTypes.MediaTypeWav)
else if (mediaType == MediaTypes.MediaTypeMp3 && this.soxUtility.SupportsMp3)
{
info = this.Combine(this.soxUtility.Info(source), this.ffmpegUtility.Info(source));
}
else if (mediaType == MediaTypes.MediaTypeWav)
{
info = this.Combine(this.soxUtility.Info(source), this.ffmpegUtility.Info(source));
}
Expand Down
34 changes: 32 additions & 2 deletions src/Acoustics.Tools/Audio/SoxAudioUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace Acoustics.Tools.Audio
/// </summary>
public class SoxAudioUtility : AbstractAudioUtility, IAudioUtility
{
public const string Mp3NotSupportedOnOSX = "Working with MP3 in SoX is not supported on OSX.";

private readonly bool enableShortNameHack;
/*
* Some things to test out/try:
Expand Down Expand Up @@ -130,10 +132,23 @@ public enum SoxResampleQuality
/// </summary>
public SoxResampleQuality? ResampleQuality { get; private set; }

public bool SupportsMp3 => !AppConfigHelper.IsMacOsX;

/// <summary>
/// Gets the valid source media types.
/// </summary>
protected override IEnumerable<string> ValidSourceMediaTypes => new[] { MediaTypes.MediaTypeWav, MediaTypes.MediaTypeMp3, MediaTypes.MediaTypeFlacAudio };
protected override IEnumerable<string> ValidSourceMediaTypes
{
get
{
yield return MediaTypes.MediaTypeWav;
yield return MediaTypes.MediaTypeFlacAudio;
if (this.SupportsMp3)
{
yield return MediaTypes.MediaTypeMp3;
}
}
}

/// <summary>
/// Gets the invalid source media types.
Expand All @@ -143,7 +158,17 @@ public enum SoxResampleQuality
/// <summary>
/// Gets the valid output media types.
/// </summary>
protected override IEnumerable<string> ValidOutputMediaTypes => new[] { MediaTypes.MediaTypeWav, MediaTypes.MediaTypeMp3 };
protected override IEnumerable<string> ValidOutputMediaTypes
{
get
{
yield return MediaTypes.MediaTypeWav;
if (this.SupportsMp3)
{
yield return MediaTypes.MediaTypeMp3;
}
}
}

/// <summary>
/// Gets the invalid output media types.
Expand Down Expand Up @@ -448,6 +473,11 @@ protected override AudioUtilityInfo GetInfo(FileInfo source, ProcessRunner proce

protected override void CheckRequestValid(FileInfo source, string sourceMimeType, FileInfo output, string outputMediaType, AudioUtilityRequest request)
{
if (AppConfigHelper.IsMacOsX && (sourceMimeType == MediaTypes.MediaTypeMp3 || outputMediaType == MediaTypes.MediaTypeMp3))
{
throw new AudioFormatNotSupportedException(Mp3NotSupportedOnOSX);
}

AudioUtilityInfo info = null;

if (request?.BitDepth != null)
Expand Down
14 changes: 8 additions & 6 deletions tests/Acoustics.Test/Tools/AudioUtilityInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ public void InfoFailsForMaster(string file, Type exception, string message)
[Ignore("deprecated audio tool")]
public void InfoWorksShnTool(string file)
{
// var util = TestHelper.GetAudioUtilityShntool();
// var util = TestHelper.GetAudioUtilityShntool();

// var source = TestHelper.GetAudioFile(file);
// var info = util.Info(source);
// var source = TestHelper.GetAudioFile(file);
// var info = util.Info(source);

// var expected = TestHelper.AudioDetails[file];
// var expected = TestHelper.AudioDetails[file];

// TestHelper.CheckAudioUtilityInfo(expected, info);
// TestHelper.CheckAudioUtilityInfo(expected, info);
}

[DataTestMethod]
Expand Down Expand Up @@ -237,10 +237,12 @@ public void InfoFailsForSoxTool(string file)
{
var util = TestHelper.GetAudioUtilitySox();

var mp3Supported = (util as SoxAudioUtility).SupportsMp3 ? ", mp3 (audio/mpeg)" : string.Empty;

var source = TestHelper.GetAudioFile(file);
TestHelper.ExceptionMatches<NotSupportedException>(
() => util.Info(source),
"cannot be processed. Valid formats are: wav (audio/wav), mp3 (audio/mpeg), flac (audio/flac).");
$"cannot be processed. Valid formats are: wav (audio/wav), flac (audio/flac){mp3Supported}.");
}

[DataTestMethod]
Expand Down
54 changes: 47 additions & 7 deletions tests/Acoustics.Test/Tools/AudioUtilityMp3Tests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
// <copyright file="AudioUtilityMp3Tests.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.IO;
using Acoustics.Shared;
using Acoustics.Test.TestHelpers;
using Acoustics.Tools;
using Acoustics.Tools.Audio;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelpers;
using static Acoustics.Test.TestHelpers.PlatformSpecificTestMethod;

[TestClass]
public class AudioUtilityMp3Tests
{
[TestMethod]
[PlatformSpecificTestMethod(OSX)]
public void SegmentsMp3NotAvailableOnOsxWithSox()
{
var expected = new AudioUtilityInfo
{
Duration = TimeSpan.FromSeconds(30),
SampleRate = 11025,
ChannelCount = 1,
MediaType = MediaTypes.MediaTypeMp3,
BitsPerSecond = 16000,
};

var request = new AudioUtilityRequest
{
MixDownToMono = true,
OffsetStart = TimeSpan.FromSeconds(20),
OffsetEnd = TimeSpan.FromSeconds(50),
TargetSampleRate = 11025,
};

var util = TestHelper.GetAudioUtilitySox();

var source = TestHelper.GetAudioFile("Currawongs_curlew_West_Knoll_Bees_20091102-183000.mp3");
var output = PathHelper.GetTempFile(MediaTypes.ExtMp3);

Assert.ThrowsException<AudioFormatNotSupportedException>(
() => util.Info(source),
"Working with MP3 in SoX is not supported on OSX.");

Assert.ThrowsException<AudioFormatNotSupportedException>(
() => util.Modify(source, MediaTypes.GetMediaType(source.Extension), output, MediaTypes.GetMediaType(output.Extension), request),
"Working with MP3 in SoX is not supported on OSX.");
}

[PlatformSpecificTestMethod(NotOSX)]
public void SegmentsMp3Correctly1Sox()
{
var expected = new AudioUtilityInfo
Expand Down Expand Up @@ -44,7 +84,7 @@ public void SegmentsMp3Correctly1Sox()
TestHelper.CheckAudioUtilityInfo(expected, actual);
}

[TestMethod]
[PlatformSpecificTestMethod(NotOSX)]
public void SegmentsMp3Correctly2Sox()
{
var expected = new AudioUtilityInfo
Expand Down Expand Up @@ -77,7 +117,7 @@ public void SegmentsMp3Correctly2Sox()
TestHelper.CheckAudioUtilityInfo(expected, actual);
}

[TestMethod]
[PlatformSpecificTestMethod(NotOSX)]
public void SegmentsMp3Correctly3Sox()
{
var expected = new AudioUtilityInfo
Expand Down Expand Up @@ -111,7 +151,7 @@ public void SegmentsMp3Correctly3Sox()
TestHelper.CheckAudioUtilityInfo(expected, actual);
}

[TestMethod]
[PlatformSpecificTestMethod(NotOSX)]
public void SegmentsMp3Correctly4Sox()
{
var expected = new AudioUtilityInfo
Expand Down Expand Up @@ -145,7 +185,7 @@ public void SegmentsMp3Correctly4Sox()
TestHelper.CheckAudioUtilityInfo(expected, actual);
}

[TestMethod]
[PlatformSpecificTestMethod(NotOSX)]
public void SegmentsMp3Correctly5Sox()
{
var expected = new AudioUtilityInfo
Expand Down Expand Up @@ -178,7 +218,7 @@ public void SegmentsMp3Correctly5Sox()
TestHelper.CheckAudioUtilityInfo(expected, actual);
}

[TestMethod]
[PlatformSpecificTestMethod(NotOSX)]
public void SegmentsMp3Correctly6Sox()
{
var expected = new AudioUtilityInfo
Expand Down
8 changes: 6 additions & 2 deletions tests/Acoustics.Test/Tools/AudioUtilityWavpackTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
namespace Acoustics.Test.Tools
// <copyright file="AudioUtilityWavpackTests.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.IO;
using Acoustics.Shared;
using Acoustics.Test.TestHelpers;
using Acoustics.Tools;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelpers;

/// <summary>
/// The audio utility wavpack tests.
Expand Down
2 changes: 1 addition & 1 deletion tests/Acoustics.Test/Tools/SoxUtilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace Acoustics.Test.Tools
using System.IO;
using System.Threading;
using Acoustics.Shared;
using Acoustics.Test.TestHelpers;
using Acoustics.Tools;
using Acoustics.Tools.Audio;
using Acoustics.Tools.Wav;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelpers;

[TestClass]
public class SoxUtilityTests : OutputDirectoryTest
Expand Down

0 comments on commit 2a3f654

Please sign in to comment.