From 5063e56786c7aa47dda640df6a8acf3d6a941c3a Mon Sep 17 00:00:00 2001 From: Pavel Penev Date: Thu, 19 Nov 2015 00:27:45 +0200 Subject: [PATCH] Make WavLoader implement ISoundLoader --- OpenRA.Game/FileFormats/WavLoader.cs | 66 +++++++++++++++++++--------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/OpenRA.Game/FileFormats/WavLoader.cs b/OpenRA.Game/FileFormats/WavLoader.cs index cdd47ceefe..6a3518fa5a 100644 --- a/OpenRA.Game/FileFormats/WavLoader.cs +++ b/OpenRA.Game/FileFormats/WavLoader.cs @@ -13,48 +13,53 @@ using System.IO; namespace OpenRA.FileFormats { - public class WavLoader + public class WavLoader : ISoundLoader { - public readonly int FileSize; - public readonly string Format; + public int FileSize; + public string Format; - public readonly int FmtChunkSize; - public readonly int AudioFormat; - public readonly int Channels; - public readonly int SampleRate; - public readonly int ByteRate; - public readonly int BlockAlign; - public readonly int BitsPerSample; + public int FmtChunkSize; + public int AudioFormat; + public int Channels; + public int SampleRate; + public int ByteRate; + public int BlockAlign; + public int BitsPerSample; - public readonly int UncompressedSize; - public readonly int DataSize; - public readonly byte[] RawOutput; + public int UncompressedSize; + public int DataSize; + public byte[] RawOutput; public enum WaveType { Pcm = 0x1, ImaAdpcm = 0x11 } public static WaveType Type { get; private set; } - public WavLoader(Stream s) + bool LoadSound(Stream s) { + var type = s.ReadASCII(4); + if (type != "RIFF") + return false; + + FileSize = s.ReadInt32(); + Format = s.ReadASCII(4); + if (Format != "WAVE") + return false; + while (s.Position < s.Length) { if ((s.Position & 1) == 1) s.ReadByte(); // Alignment - var type = s.ReadASCII(4); + type = s.ReadASCII(4); switch (type) { - case "RIFF": - FileSize = s.ReadInt32(); - Format = s.ReadASCII(4); - if (Format != "WAVE") - throw new NotSupportedException("Not a canonical WAVE file."); - break; case "fmt ": FmtChunkSize = s.ReadInt32(); AudioFormat = s.ReadInt16(); Type = (WaveType)AudioFormat; + if (Type != WaveType.Pcm && Type != WaveType.ImaAdpcm) throw new NotSupportedException("Compression type is not supported."); + Channels = s.ReadInt16(); SampleRate = s.ReadInt32(); ByteRate = s.ReadInt32(); @@ -91,6 +96,8 @@ namespace OpenRA.FileFormats RawOutput = DecodeImaAdpcmData(); BitsPerSample = 16; } + + return true; } public static float WaveLength(Stream s) @@ -176,5 +183,22 @@ namespace OpenRA.FileFormats return output; } + + public bool TryParseSound(Stream stream, string fileName, out byte[] rawData, out int channels, + out int sampleBits, out int sampleRate) + { + rawData = null; + channels = sampleBits = sampleRate = 0; + + if (!LoadSound(stream)) + return false; + + rawData = RawOutput; + channels = Channels; + sampleBits = BitsPerSample; + sampleRate = SampleRate; + + return true; + } } } \ No newline at end of file