using Amphora; using System.Text; using UnitTestSharp; namespace Susurrus.UnitTests { public class SoundFormatsTests : TestFixture { public static byte[] Text(string text) { return Encoding.ASCII.GetBytes(text); } public static byte[] Padded(byte[] start, int length) { byte[] returnMe = new byte[length]; System.Array.Copy(start, returnMe, start.Length); return returnMe; } public static byte[] OggPageWith(byte[] firstPacket) { byte[] returnMe = new byte[28 + firstPacket.Length]; System.Array.Copy(Text("OggS"), returnMe, 4); returnMe[26] = 1; returnMe[27] = (byte)firstPacket.Length; System.Array.Copy(firstPacket, 0, returnMe, 28, firstPacket.Length); return returnMe; } public static SoundFormat DetectAll(byte[] header) { return SoundFormats.Detect(new ByteView(header)); } public class DetectTests : TestFixture { public void RiffWave_IsWav() { CheckEqual(SoundFormat.Wav, DetectAll(Text("RIFF\0\0\0\0WAVEfmt "))); } public void RifxWave_IsWav() { CheckEqual(SoundFormat.Wav, DetectAll(Text("RIFX\0\0\0\0WAVEfmt "))); } public void Rf64Wave_IsWav() { CheckEqual(SoundFormat.Wav, DetectAll(Text("RF64\0\0\0\0WAVEfmt "))); } public void RiffThatIsNotWave_IsUnknown() { CheckEqual(SoundFormat.Unknown, DetectAll(Text("RIFF\0\0\0\0AVI LIST"))); } public void OggWhoseFirstPacketIsVorbis_IsOggVorbis() { byte[] packet = { 0x01, (byte)'v', (byte)'o', (byte)'r', (byte)'b', (byte)'i', (byte)'s' }; CheckEqual(SoundFormat.OggVorbis, DetectAll(OggPageWith(packet))); } public void OggWhoseFirstPacketIsOpus_IsOggOpus() { CheckEqual(SoundFormat.OggOpus, DetectAll(OggPageWith(Text("OpusHead")))); } public void OggWhoseFirstPacketIsFlac_IsOggFlac() { byte[] packet = { 0x7F, (byte)'F', (byte)'L', (byte)'A', (byte)'C' }; CheckEqual(SoundFormat.OggFlac, DetectAll(OggPageWith(packet))); } public void OggWhoseFirstPacketIsSpeex_IsOggSpeex() { CheckEqual(SoundFormat.OggSpeex, DetectAll(OggPageWith(Text("Speex ")))); } public void OggWhoseFirstPacketIsAnythingElse_IsOggOther() { CheckEqual(SoundFormat.OggOther, DetectAll(OggPageWith(Text("theora!!")))); } public void OggWithTooFewBytesToShowItsCodec_IsOggOther() { CheckEqual(SoundFormat.OggOther, DetectAll(Text("OggS"))); } public void OnlyTheBytesInTheViewAreLookedAt() { byte[] opus = OggPageWith(Text("OpusHead")); CheckEqual(SoundFormat.OggOther, SoundFormats.Detect(new ByteView(opus, startIndex: 0, endIndex: 30))); } public void TheViewsStartIsTheStartOfTheFile() { byte[] padded = Padded(Text("xxxxRIFF\0\0\0\0WAVEfmt "), 40); ByteView window = new ByteView(padded, startIndex: 4, endIndex: 20); CheckEqual(SoundFormat.Wav, SoundFormats.Detect(window)); } public void DefaultView_IsUnknown() { CheckEqual(SoundFormat.Unknown, SoundFormats.Detect(default(ByteView))); } public void FlacMarker_IsFlac() { CheckEqual(SoundFormat.Flac, DetectAll(Text("fLaC\0\0\0\""))); } public void FormAiff_IsAiff() { CheckEqual(SoundFormat.Aiff, DetectAll(Text("FORM\0\0\0\0AIFFCOMM"))); } public void FormAifc_IsAiff() { CheckEqual(SoundFormat.Aiff, DetectAll(Text("FORM\0\0\0\0AIFCFVER"))); } public void FormThatIsNotAiff_IsUnknown() { CheckEqual(SoundFormat.Unknown, DetectAll(Text("FORM\0\0\0\0ILBMBMHD"))); } public void FtypAtOffsetFour_IsMp4() { CheckEqual(SoundFormat.Mp4, DetectAll(Text("\0\0\0 ftypM4A "))); } public void AsfHeaderGuid_IsWma() { byte[] guid = { 0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62 }; CheckEqual(SoundFormat.Wma, DetectAll(guid)); } public void Id3Tag_IsMp3() { CheckEqual(SoundFormat.Mp3, DetectAll(Text("ID3\u0004\0\0\0\0"))); } public void MpegOneLayerThreeFrame_IsMp3() { CheckEqual(SoundFormat.Mp3, DetectAll(new byte[] { 0xFF, 0xFB, 0x50, 0xC4 })); } public void MpegTwoLayerThreeFrame_IsMp3() { CheckEqual(SoundFormat.Mp3, DetectAll(new byte[] { 0xFF, 0xF3, 0x50, 0xC4 })); } public void MpegTwoPointFiveLayerThreeFrame_IsMp3() { CheckEqual(SoundFormat.Mp3, DetectAll(new byte[] { 0xFF, 0xE3, 0x50, 0xC4 })); } public void AdtsFrameWithoutProtection_IsAac() { CheckEqual(SoundFormat.Aac, DetectAll(new byte[] { 0xFF, 0xF1, 0x50, 0x80 })); } public void AdtsFrameWithProtection_IsAac() { CheckEqual(SoundFormat.Aac, DetectAll(new byte[] { 0xFF, 0xF9, 0x50, 0x80 })); } public void FrameSyncWithAReservedVersion_IsUnknown() { CheckEqual(SoundFormat.Unknown, DetectAll(new byte[] { 0xFF, 0xEB, 0x50, 0xC4 })); } public void FrameSyncWithLayerZeroThatIsNotAdts_IsUnknown() { CheckEqual(SoundFormat.Unknown, DetectAll(new byte[] { 0xFF, 0xE1, 0x50, 0xC4 })); } public void FirstByteOfASyncAlone_IsUnknown() { CheckEqual(SoundFormat.Unknown, DetectAll(new byte[] { 0xFF })); } public void ASecondByteWithoutTheSyncBits_IsUnknown() { CheckEqual(SoundFormat.Unknown, DetectAll(new byte[] { 0xFF, 0x1F, 0x50, 0xC4 })); } public void NoBytes_IsUnknown() { CheckEqual(SoundFormat.Unknown, DetectAll(new byte[0])); } } public class DescribeTests : TestFixture { public void NamesEachFormatInASentence() { CheckEqual("a WAV file", SoundFormats.Describe(SoundFormat.Wav)); CheckEqual("an Ogg Vorbis file", SoundFormats.Describe(SoundFormat.OggVorbis)); CheckEqual("an Ogg Opus file", SoundFormats.Describe(SoundFormat.OggOpus)); CheckEqual("an Ogg FLAC file", SoundFormats.Describe(SoundFormat.OggFlac)); CheckEqual("an Ogg Speex file", SoundFormats.Describe(SoundFormat.OggSpeex)); CheckEqual("an Ogg file that doesn't hold Vorbis", SoundFormats.Describe(SoundFormat.OggOther)); CheckEqual("an MP3 file", SoundFormats.Describe(SoundFormat.Mp3)); CheckEqual("an AAC file", SoundFormats.Describe(SoundFormat.Aac)); CheckEqual("a FLAC file", SoundFormats.Describe(SoundFormat.Flac)); CheckEqual("an AIFF file", SoundFormats.Describe(SoundFormat.Aiff)); CheckEqual("an MP4 or M4A file", SoundFormats.Describe(SoundFormat.Mp4)); CheckEqual("a WMA or other ASF file", SoundFormats.Describe(SoundFormat.Wma)); } public void Unknown_IsUnrecognized() { CheckEqual("an unrecognized format", SoundFormats.Describe(SoundFormat.Unknown)); } } } }