From ad77dca12a4b8d869f4965753abcaa817cd275f5 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Thu, 23 Jun 2016 01:55:01 +0200 Subject: [PATCH 1/5] Remove WavReader dependency from BagFile reader --- OpenRA.Game/FileSystem/BagFile.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OpenRA.Game/FileSystem/BagFile.cs b/OpenRA.Game/FileSystem/BagFile.cs index 351fa4ccd4..074b56024a 100644 --- a/OpenRA.Game/FileSystem/BagFile.cs +++ b/OpenRA.Game/FileSystem/BagFile.cs @@ -68,7 +68,7 @@ namespace OpenRA.FileSystem waveHeaderMemoryStream.Write(Encoding.ASCII.GetBytes("WAVE")); waveHeaderMemoryStream.Write(Encoding.ASCII.GetBytes("fmt ")); waveHeaderMemoryStream.Write(BitConverter.GetBytes(16)); - waveHeaderMemoryStream.Write(BitConverter.GetBytes((short)WavReader.WaveType.Pcm)); + waveHeaderMemoryStream.Write(BitConverter.GetBytes((short)1)); waveHeaderMemoryStream.Write(BitConverter.GetBytes((short)channels)); waveHeaderMemoryStream.Write(BitConverter.GetBytes(entry.SampleRate)); waveHeaderMemoryStream.Write(BitConverter.GetBytes(2 * channels * entry.SampleRate)); @@ -90,7 +90,7 @@ namespace OpenRA.FileSystem waveHeaderMemoryStream.Write(Encoding.ASCII.GetBytes("WAVE")); waveHeaderMemoryStream.Write(Encoding.ASCII.GetBytes("fmt ")); waveHeaderMemoryStream.Write(BitConverter.GetBytes(20)); - waveHeaderMemoryStream.Write(BitConverter.GetBytes((short)WavReader.WaveType.ImaAdpcm)); + waveHeaderMemoryStream.Write(BitConverter.GetBytes((short)17)); waveHeaderMemoryStream.Write(BitConverter.GetBytes((short)channels)); waveHeaderMemoryStream.Write(BitConverter.GetBytes(entry.SampleRate)); waveHeaderMemoryStream.Write(BitConverter.GetBytes(bytesPerSec)); From ed210b380fc780b8dc2b8abf277244ee04f7be50 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Thu, 23 Jun 2016 01:55:57 +0200 Subject: [PATCH 2/5] Move audio loaders from engine to mod level --- OpenRA.Game/OpenRA.Game.csproj | 4 ---- .../AudioLoaders}/AudLoader.cs | 2 +- .../AudioLoaders}/ImaAdpcmLoader.cs | 2 +- .../AudioLoaders}/VocLoader.cs | 2 +- .../AudioLoaders}/WavLoader.cs | 2 +- OpenRA.Mods.Common/FileFormats/VqaReader.cs | 1 + OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 4 ++++ 7 files changed, 9 insertions(+), 8 deletions(-) rename {OpenRA.Game/FileFormats => OpenRA.Mods.Common/AudioLoaders}/AudLoader.cs (99%) rename {OpenRA.Game/FileFormats => OpenRA.Mods.Common/AudioLoaders}/ImaAdpcmLoader.cs (98%) rename {OpenRA.Game/FileFormats => OpenRA.Mods.Common/AudioLoaders}/VocLoader.cs (99%) rename {OpenRA.Game/FileFormats => OpenRA.Mods.Common/AudioLoaders}/WavLoader.cs (99%) diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index 060a15d2ab..534f05ec3d 100644 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -95,7 +95,6 @@ - @@ -266,13 +265,10 @@ - - - diff --git a/OpenRA.Game/FileFormats/AudLoader.cs b/OpenRA.Mods.Common/AudioLoaders/AudLoader.cs similarity index 99% rename from OpenRA.Game/FileFormats/AudLoader.cs rename to OpenRA.Mods.Common/AudioLoaders/AudLoader.cs index 5ff08becd6..3f7c5ced4f 100644 --- a/OpenRA.Game/FileFormats/AudLoader.cs +++ b/OpenRA.Mods.Common/AudioLoaders/AudLoader.cs @@ -12,7 +12,7 @@ using System; using System.IO; -namespace OpenRA.FileFormats +namespace OpenRA.Mods.Common.AudioLoaders { [Flags] enum SoundFlags diff --git a/OpenRA.Game/FileFormats/ImaAdpcmLoader.cs b/OpenRA.Mods.Common/AudioLoaders/ImaAdpcmLoader.cs similarity index 98% rename from OpenRA.Game/FileFormats/ImaAdpcmLoader.cs rename to OpenRA.Mods.Common/AudioLoaders/ImaAdpcmLoader.cs index 3e110fbb70..0aa47be2cf 100644 --- a/OpenRA.Game/FileFormats/ImaAdpcmLoader.cs +++ b/OpenRA.Mods.Common/AudioLoaders/ImaAdpcmLoader.cs @@ -11,7 +11,7 @@ using System.IO; -namespace OpenRA.FileFormats +namespace OpenRA.Mods.Common.AudioLoaders { struct ImaAdpcmChunk { diff --git a/OpenRA.Game/FileFormats/VocLoader.cs b/OpenRA.Mods.Common/AudioLoaders/VocLoader.cs similarity index 99% rename from OpenRA.Game/FileFormats/VocLoader.cs rename to OpenRA.Mods.Common/AudioLoaders/VocLoader.cs index e291d68bc8..142ad41069 100644 --- a/OpenRA.Game/FileFormats/VocLoader.cs +++ b/OpenRA.Mods.Common/AudioLoaders/VocLoader.cs @@ -14,7 +14,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; -namespace OpenRA.FileFormats +namespace OpenRA.Mods.Common.AudioLoaders { public class VocLoader : ISoundLoader { diff --git a/OpenRA.Game/FileFormats/WavLoader.cs b/OpenRA.Mods.Common/AudioLoaders/WavLoader.cs similarity index 99% rename from OpenRA.Game/FileFormats/WavLoader.cs rename to OpenRA.Mods.Common/AudioLoaders/WavLoader.cs index a5bbdc571a..c9c5df9cfe 100644 --- a/OpenRA.Game/FileFormats/WavLoader.cs +++ b/OpenRA.Mods.Common/AudioLoaders/WavLoader.cs @@ -12,7 +12,7 @@ using System; using System.IO; -namespace OpenRA.FileFormats +namespace OpenRA.Mods.Common.AudioLoaders { public class WavLoader : ISoundLoader { diff --git a/OpenRA.Mods.Common/FileFormats/VqaReader.cs b/OpenRA.Mods.Common/FileFormats/VqaReader.cs index eec719f02e..f5a9e584f4 100644 --- a/OpenRA.Mods.Common/FileFormats/VqaReader.cs +++ b/OpenRA.Mods.Common/FileFormats/VqaReader.cs @@ -12,6 +12,7 @@ using System; using System.IO; using OpenRA.FileFormats; +using OpenRA.Mods.Common.AudioLoaders; namespace OpenRA.Mods.Common.FileFormats { diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index d9488d42f6..e4d2a4967f 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -146,6 +146,10 @@ + + + + From 415e0bb54ce1fe83cb7799b6ec7d1c93fa330184 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Thu, 23 Jun 2016 23:22:45 +0200 Subject: [PATCH 3/5] Split AudReader from AudLoader and move it to FileFormats We already separated loading from file format reading for sprites and videos. --- OpenRA.Mods.Common/AudioLoaders/AudLoader.cs | 155 +---------------- OpenRA.Mods.Common/FileFormats/AudReader.cs | 170 +++++++++++++++++++ OpenRA.Mods.Common/FileFormats/VqaReader.cs | 1 - OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 1 + 4 files changed, 172 insertions(+), 155 deletions(-) create mode 100644 OpenRA.Mods.Common/FileFormats/AudReader.cs diff --git a/OpenRA.Mods.Common/AudioLoaders/AudLoader.cs b/OpenRA.Mods.Common/AudioLoaders/AudLoader.cs index 3f7c5ced4f..5561f15400 100644 --- a/OpenRA.Mods.Common/AudioLoaders/AudLoader.cs +++ b/OpenRA.Mods.Common/AudioLoaders/AudLoader.cs @@ -11,39 +11,10 @@ using System; using System.IO; +using OpenRA.Mods.Common.FileFormats; namespace OpenRA.Mods.Common.AudioLoaders { - [Flags] - enum SoundFlags - { - Stereo = 0x1, - _16Bit = 0x2, - } - - enum SoundFormat - { - WestwoodCompressed = 1, - ImaAdpcm = 99, - } - - struct Chunk - { - public int CompressedSize; - public int OutputSize; - - public static Chunk Read(Stream s) - { - Chunk c; - c.CompressedSize = s.ReadUInt16(); - c.OutputSize = s.ReadUInt16(); - - if (s.ReadUInt32() != 0xdeaf) - throw new InvalidDataException("Chunk header is bogus"); - return c; - } - } - public class AudLoader : ISoundLoader { bool IsAud(Stream s) @@ -114,128 +85,4 @@ namespace OpenRA.Mods.Common.AudioLoaders }); } } - - public class AudReader - { - static readonly int[] IndexAdjust = { -1, -1, -1, -1, 2, 4, 6, 8 }; - static readonly int[] StepTable = - { - 7, 8, 9, 10, 11, 12, 13, 14, 16, - 17, 19, 21, 23, 25, 28, 31, 34, 37, - 41, 45, 50, 55, 60, 66, 73, 80, 88, - 97, 107, 118, 130, 143, 157, 173, 190, 209, - 230, 253, 279, 307, 337, 371, 408, 449, 494, - 544, 598, 658, 724, 796, 876, 963, 1060, 1166, - 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, - 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, - 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, - 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 - }; - - static short DecodeSample(byte b, ref int index, ref int current) - { - var sb = (b & 8) != 0; - b &= 7; - - var delta = (StepTable[index] * b) / 4 + StepTable[index] / 8; - if (sb) delta = -delta; - - current += delta; - if (current > short.MaxValue) current = short.MaxValue; - if (current < short.MinValue) current = short.MinValue; - - index += IndexAdjust[b]; - if (index < 0) index = 0; - if (index > 88) index = 88; - - return (short)current; - } - - public static byte[] LoadSound(byte[] raw, ref int index) - { - var s = new MemoryStream(raw); - var dataSize = raw.Length; - var outputSize = raw.Length * 4; - - var output = new byte[outputSize]; - var offset = 0; - var currentSample = 0; - - while (dataSize-- > 0) - { - var b = s.ReadUInt8(); - - var t = DecodeSample(b, ref index, ref currentSample); - output[offset++] = (byte)t; - output[offset++] = (byte)(t >> 8); - - t = DecodeSample((byte)(b >> 4), ref index, ref currentSample); - output[offset++] = (byte)t; - output[offset++] = (byte)(t >> 8); - } - - return output; - } - - public static float SoundLength(Stream s) - { - var sampleRate = s.ReadUInt16(); - /*var dataSize = */ s.ReadInt32(); - var outputSize = s.ReadInt32(); - var flags = (SoundFlags)s.ReadByte(); - - var samples = outputSize; - if (0 != (flags & SoundFlags.Stereo)) samples /= 2; - if (0 != (flags & SoundFlags._16Bit)) samples /= 2; - return samples / sampleRate; - } - - public static bool LoadSound(Stream s, out byte[] rawData, out int sampleRate) - { - rawData = null; - - sampleRate = s.ReadUInt16(); - var dataSize = s.ReadInt32(); - var outputSize = s.ReadInt32(); - - var readFlag = s.ReadByte(); - if (!Enum.IsDefined(typeof(SoundFlags), readFlag)) - return false; - - var readFormat = s.ReadByte(); - if (!Enum.IsDefined(typeof(SoundFormat), readFormat)) - return false; - - var output = new byte[outputSize]; - var offset = 0; - var index = 0; - var currentSample = 0; - - while (dataSize > 0) - { - var chunk = Chunk.Read(s); - for (var n = 0; n < chunk.CompressedSize; n++) - { - var b = s.ReadUInt8(); - - var t = DecodeSample(b, ref index, ref currentSample); - output[offset++] = (byte)t; - output[offset++] = (byte)(t >> 8); - - if (offset < outputSize) - { - /* possible that only half of the final byte is used! */ - t = DecodeSample((byte)(b >> 4), ref index, ref currentSample); - output[offset++] = (byte)t; - output[offset++] = (byte)(t >> 8); - } - } - - dataSize -= 8 + chunk.CompressedSize; - } - - rawData = output; - return true; - } - } } diff --git a/OpenRA.Mods.Common/FileFormats/AudReader.cs b/OpenRA.Mods.Common/FileFormats/AudReader.cs new file mode 100644 index 0000000000..8e568fe4e2 --- /dev/null +++ b/OpenRA.Mods.Common/FileFormats/AudReader.cs @@ -0,0 +1,170 @@ +#region Copyright & License Information +/* + * Copyright 2007-2016 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System; +using System.IO; + +namespace OpenRA.Mods.Common.FileFormats +{ + [Flags] + enum SoundFlags + { + Stereo = 0x1, + _16Bit = 0x2, + } + + enum SoundFormat + { + WestwoodCompressed = 1, + ImaAdpcm = 99, + } + + struct Chunk + { + public int CompressedSize; + public int OutputSize; + + public static Chunk Read(Stream s) + { + Chunk c; + c.CompressedSize = s.ReadUInt16(); + c.OutputSize = s.ReadUInt16(); + + if (s.ReadUInt32() != 0xdeaf) + throw new InvalidDataException("Chunk header is bogus"); + return c; + } + } + + public class AudReader + { + static readonly int[] IndexAdjust = { -1, -1, -1, -1, 2, 4, 6, 8 }; + static readonly int[] StepTable = + { + 7, 8, 9, 10, 11, 12, 13, 14, 16, + 17, 19, 21, 23, 25, 28, 31, 34, 37, + 41, 45, 50, 55, 60, 66, 73, 80, 88, + 97, 107, 118, 130, 143, 157, 173, 190, 209, + 230, 253, 279, 307, 337, 371, 408, 449, 494, + 544, 598, 658, 724, 796, 876, 963, 1060, 1166, + 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, + 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, + 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, + 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 + }; + + static short DecodeSample(byte b, ref int index, ref int current) + { + var sb = (b & 8) != 0; + b &= 7; + + var delta = (StepTable[index] * b) / 4 + StepTable[index] / 8; + if (sb) delta = -delta; + + current += delta; + if (current > short.MaxValue) current = short.MaxValue; + if (current < short.MinValue) current = short.MinValue; + + index += IndexAdjust[b]; + if (index < 0) index = 0; + if (index > 88) index = 88; + + return (short)current; + } + + public static byte[] LoadSound(byte[] raw, ref int index) + { + var s = new MemoryStream(raw); + var dataSize = raw.Length; + var outputSize = raw.Length * 4; + + var output = new byte[outputSize]; + var offset = 0; + var currentSample = 0; + + while (dataSize-- > 0) + { + var b = s.ReadUInt8(); + + var t = DecodeSample(b, ref index, ref currentSample); + output[offset++] = (byte)t; + output[offset++] = (byte)(t >> 8); + + t = DecodeSample((byte)(b >> 4), ref index, ref currentSample); + output[offset++] = (byte)t; + output[offset++] = (byte)(t >> 8); + } + + return output; + } + + public static float SoundLength(Stream s) + { + var sampleRate = s.ReadUInt16(); + /*var dataSize = */ s.ReadInt32(); + var outputSize = s.ReadInt32(); + var flags = (SoundFlags)s.ReadByte(); + + var samples = outputSize; + if (0 != (flags & SoundFlags.Stereo)) samples /= 2; + if (0 != (flags & SoundFlags._16Bit)) samples /= 2; + return samples / sampleRate; + } + + public static bool LoadSound(Stream s, out byte[] rawData, out int sampleRate) + { + rawData = null; + + sampleRate = s.ReadUInt16(); + var dataSize = s.ReadInt32(); + var outputSize = s.ReadInt32(); + + var readFlag = s.ReadByte(); + if (!Enum.IsDefined(typeof(SoundFlags), readFlag)) + return false; + + var readFormat = s.ReadByte(); + if (!Enum.IsDefined(typeof(SoundFormat), readFormat)) + return false; + + var output = new byte[outputSize]; + var offset = 0; + var index = 0; + var currentSample = 0; + + while (dataSize > 0) + { + var chunk = Chunk.Read(s); + for (var n = 0; n < chunk.CompressedSize; n++) + { + var b = s.ReadUInt8(); + + var t = DecodeSample(b, ref index, ref currentSample); + output[offset++] = (byte)t; + output[offset++] = (byte)(t >> 8); + + if (offset < outputSize) + { + /* possible that only half of the final byte is used! */ + t = DecodeSample((byte)(b >> 4), ref index, ref currentSample); + output[offset++] = (byte)t; + output[offset++] = (byte)(t >> 8); + } + } + + dataSize -= 8 + chunk.CompressedSize; + } + + rawData = output; + return true; + } + } +} diff --git a/OpenRA.Mods.Common/FileFormats/VqaReader.cs b/OpenRA.Mods.Common/FileFormats/VqaReader.cs index f5a9e584f4..eec719f02e 100644 --- a/OpenRA.Mods.Common/FileFormats/VqaReader.cs +++ b/OpenRA.Mods.Common/FileFormats/VqaReader.cs @@ -12,7 +12,6 @@ using System; using System.IO; using OpenRA.FileFormats; -using OpenRA.Mods.Common.AudioLoaders; namespace OpenRA.Mods.Common.FileFormats { diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index e4d2a4967f..eb41d49bb9 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -728,6 +728,7 @@ + From f1882e2dd6604a01510275c6529719d9d0fadc16 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Thu, 23 Jun 2016 23:34:04 +0200 Subject: [PATCH 4/5] Rename ImaAdpcmLoader to *Reader and move it to FileFormats --- OpenRA.Mods.Common/AudioLoaders/WavLoader.cs | 3 ++- .../ImaAdpcmLoader.cs => FileFormats/ImaAdpcmReader.cs} | 6 ++++-- OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) rename OpenRA.Mods.Common/{AudioLoaders/ImaAdpcmLoader.cs => FileFormats/ImaAdpcmReader.cs} (92%) diff --git a/OpenRA.Mods.Common/AudioLoaders/WavLoader.cs b/OpenRA.Mods.Common/AudioLoaders/WavLoader.cs index c9c5df9cfe..c1d593e3d8 100644 --- a/OpenRA.Mods.Common/AudioLoaders/WavLoader.cs +++ b/OpenRA.Mods.Common/AudioLoaders/WavLoader.cs @@ -11,6 +11,7 @@ using System; using System.IO; +using OpenRA.Mods.Common.FileFormats; namespace OpenRA.Mods.Common.AudioLoaders { @@ -220,7 +221,7 @@ namespace OpenRA.Mods.Common.AudioLoaders { // Decode 4 bytes (to 16 bytes of output) per channel var chunk = s.ReadBytes(4); - var decoded = ImaAdpcmLoader.LoadImaAdpcmSound(chunk, ref index[c], ref predictor[c]); + var decoded = ImaAdpcmReader.LoadImaAdpcmSound(chunk, ref index[c], ref predictor[c]); // Interleave output, one sample per channel var outOffsetChannel = outOffset + (2 * c); diff --git a/OpenRA.Mods.Common/AudioLoaders/ImaAdpcmLoader.cs b/OpenRA.Mods.Common/FileFormats/ImaAdpcmReader.cs similarity index 92% rename from OpenRA.Mods.Common/AudioLoaders/ImaAdpcmLoader.cs rename to OpenRA.Mods.Common/FileFormats/ImaAdpcmReader.cs index 0aa47be2cf..7fb8c0a6a2 100644 --- a/OpenRA.Mods.Common/AudioLoaders/ImaAdpcmLoader.cs +++ b/OpenRA.Mods.Common/FileFormats/ImaAdpcmReader.cs @@ -11,7 +11,7 @@ using System.IO; -namespace OpenRA.Mods.Common.AudioLoaders +namespace OpenRA.Mods.Common.FileFormats { struct ImaAdpcmChunk { @@ -29,7 +29,9 @@ namespace OpenRA.Mods.Common.AudioLoaders } } - public static class ImaAdpcmLoader + // Mostly a duplicate of AudReader, with some difference when loading + // TODO: Investigate whether they can be fused to get rid of some duplication + public class ImaAdpcmReader { static readonly int[] IndexAdjust = { -1, -1, -1, -1, 2, 4, 6, 8 }; static readonly int[] StepTable = diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index eb41d49bb9..335e4c4e9f 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -147,7 +147,6 @@ - @@ -729,6 +728,7 @@ + From 61acbe70d666002978fc9dc75d3c53b5952c3fcd Mon Sep 17 00:00:00 2001 From: reaperrr Date: Thu, 23 Jun 2016 23:47:14 +0200 Subject: [PATCH 5/5] Extract WavReader to own file and move it to FileFormats --- OpenRA.Mods.Common/AudioLoaders/WavLoader.cs | 164 ----------------- OpenRA.Mods.Common/FileFormats/WavReader.cs | 180 +++++++++++++++++++ OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 1 + 3 files changed, 181 insertions(+), 164 deletions(-) create mode 100644 OpenRA.Mods.Common/FileFormats/WavReader.cs diff --git a/OpenRA.Mods.Common/AudioLoaders/WavLoader.cs b/OpenRA.Mods.Common/AudioLoaders/WavLoader.cs index c1d593e3d8..e651c21083 100644 --- a/OpenRA.Mods.Common/AudioLoaders/WavLoader.cs +++ b/OpenRA.Mods.Common/AudioLoaders/WavLoader.cs @@ -82,168 +82,4 @@ namespace OpenRA.Mods.Common.AudioLoaders }); } } - - public class WavReader - { - public int FileSize; - public string Format; - - public int FmtChunkSize; - public int AudioFormat; - public int Channels; - public int SampleRate; - public int ByteRate; - public int BlockAlign; - public int BitsPerSample; - - public int UncompressedSize; - public int DataSize; - public byte[] RawOutput; - - public enum WaveType { Pcm = 0x1, ImaAdpcm = 0x11 } - public static WaveType Type { get; private set; } - - public 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 - - type = s.ReadASCII(4); - switch (type) - { - case "fmt ": - FmtChunkSize = s.ReadInt32(); - AudioFormat = s.ReadInt16(); - Type = (WaveType)AudioFormat; - - if (!Enum.IsDefined(typeof(WaveType), Type)) - throw new NotSupportedException("Compression type {0} is not supported.".F(AudioFormat)); - - Channels = s.ReadInt16(); - SampleRate = s.ReadInt32(); - ByteRate = s.ReadInt32(); - BlockAlign = s.ReadInt16(); - BitsPerSample = s.ReadInt16(); - - s.ReadBytes(FmtChunkSize - 16); - break; - case "fact": - var chunkSize = s.ReadInt32(); - UncompressedSize = s.ReadInt32(); - s.ReadBytes(chunkSize - 4); - break; - case "data": - DataSize = s.ReadInt32(); - RawOutput = s.ReadBytes(DataSize); - break; - default: - var unknownChunkSize = s.ReadInt32(); - s.ReadBytes(unknownChunkSize); - break; - } - } - - if (Type == WaveType.ImaAdpcm) - { - RawOutput = DecodeImaAdpcmData(); - BitsPerSample = 16; - } - - return true; - } - - public static float WaveLength(Stream s) - { - s.Position = 12; - var fmt = s.ReadASCII(4); - - if (fmt != "fmt ") - return 0; - - s.Position = 22; - var channels = s.ReadInt16(); - var sampleRate = s.ReadInt32(); - - s.Position = 34; - var bitsPerSample = s.ReadInt16(); - var length = s.Length * 8; - - return length / (channels * sampleRate * bitsPerSample); - } - - public byte[] DecodeImaAdpcmData() - { - var s = new MemoryStream(RawOutput); - - var numBlocks = DataSize / BlockAlign; - var blockDataSize = BlockAlign - (Channels * 4); - var outputSize = UncompressedSize * Channels * 2; - - var outOffset = 0; - var output = new byte[outputSize]; - - var predictor = new int[Channels]; - var index = new int[Channels]; - - // Decode each block of IMA ADPCM data in RawOutput - for (var block = 0; block < numBlocks; block++) - { - // Each block starts with a initial state per-channel - for (var c = 0; c < Channels; c++) - { - predictor[c] = s.ReadInt16(); - index[c] = s.ReadUInt8(); - /* unknown/reserved */ s.ReadUInt8(); - - // Output first sample from input - output[outOffset++] = (byte)predictor[c]; - output[outOffset++] = (byte)(predictor[c] >> 8); - - if (outOffset >= outputSize) - return output; - } - - // Decode and output remaining data in this block - var blockOffset = 0; - while (blockOffset < blockDataSize) - { - for (var c = 0; c < Channels; c++) - { - // Decode 4 bytes (to 16 bytes of output) per channel - var chunk = s.ReadBytes(4); - var decoded = ImaAdpcmReader.LoadImaAdpcmSound(chunk, ref index[c], ref predictor[c]); - - // Interleave output, one sample per channel - var outOffsetChannel = outOffset + (2 * c); - for (var i = 0; i < decoded.Length; i += 2) - { - var outOffsetSample = outOffsetChannel + i; - if (outOffsetSample >= outputSize) - return output; - - output[outOffsetSample] = decoded[i]; - output[outOffsetSample + 1] = decoded[i + 1]; - outOffsetChannel += 2 * (Channels - 1); - } - - blockOffset += 4; - } - - outOffset += 16 * Channels; - } - } - - return output; - } - } } \ No newline at end of file diff --git a/OpenRA.Mods.Common/FileFormats/WavReader.cs b/OpenRA.Mods.Common/FileFormats/WavReader.cs new file mode 100644 index 0000000000..1c3f5c2b38 --- /dev/null +++ b/OpenRA.Mods.Common/FileFormats/WavReader.cs @@ -0,0 +1,180 @@ +#region Copyright & License Information +/* + * Copyright 2007-2016 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System; +using System.IO; + +namespace OpenRA.Mods.Common.FileFormats +{ + public class WavReader + { + public int FileSize; + public string Format; + + public int FmtChunkSize; + public int AudioFormat; + public int Channels; + public int SampleRate; + public int ByteRate; + public int BlockAlign; + public int BitsPerSample; + + public int UncompressedSize; + public int DataSize; + public byte[] RawOutput; + + public enum WaveType { Pcm = 0x1, ImaAdpcm = 0x11 } + public static WaveType Type { get; private set; } + + public 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 + + type = s.ReadASCII(4); + switch (type) + { + case "fmt ": + FmtChunkSize = s.ReadInt32(); + AudioFormat = s.ReadInt16(); + Type = (WaveType)AudioFormat; + + if (!Enum.IsDefined(typeof(WaveType), Type)) + throw new NotSupportedException("Compression type {0} is not supported.".F(AudioFormat)); + + Channels = s.ReadInt16(); + SampleRate = s.ReadInt32(); + ByteRate = s.ReadInt32(); + BlockAlign = s.ReadInt16(); + BitsPerSample = s.ReadInt16(); + + s.ReadBytes(FmtChunkSize - 16); + break; + case "fact": + var chunkSize = s.ReadInt32(); + UncompressedSize = s.ReadInt32(); + s.ReadBytes(chunkSize - 4); + break; + case "data": + DataSize = s.ReadInt32(); + RawOutput = s.ReadBytes(DataSize); + break; + default: + var unknownChunkSize = s.ReadInt32(); + s.ReadBytes(unknownChunkSize); + break; + } + } + + if (Type == WaveType.ImaAdpcm) + { + RawOutput = DecodeImaAdpcmData(); + BitsPerSample = 16; + } + + return true; + } + + public static float WaveLength(Stream s) + { + s.Position = 12; + var fmt = s.ReadASCII(4); + + if (fmt != "fmt ") + return 0; + + s.Position = 22; + var channels = s.ReadInt16(); + var sampleRate = s.ReadInt32(); + + s.Position = 34; + var bitsPerSample = s.ReadInt16(); + var length = s.Length * 8; + + return length / (channels * sampleRate * bitsPerSample); + } + + public byte[] DecodeImaAdpcmData() + { + var s = new MemoryStream(RawOutput); + + var numBlocks = DataSize / BlockAlign; + var blockDataSize = BlockAlign - (Channels * 4); + var outputSize = UncompressedSize * Channels * 2; + + var outOffset = 0; + var output = new byte[outputSize]; + + var predictor = new int[Channels]; + var index = new int[Channels]; + + // Decode each block of IMA ADPCM data in RawOutput + for (var block = 0; block < numBlocks; block++) + { + // Each block starts with a initial state per-channel + for (var c = 0; c < Channels; c++) + { + predictor[c] = s.ReadInt16(); + index[c] = s.ReadUInt8(); + /* unknown/reserved */ s.ReadUInt8(); + + // Output first sample from input + output[outOffset++] = (byte)predictor[c]; + output[outOffset++] = (byte)(predictor[c] >> 8); + + if (outOffset >= outputSize) + return output; + } + + // Decode and output remaining data in this block + var blockOffset = 0; + while (blockOffset < blockDataSize) + { + for (var c = 0; c < Channels; c++) + { + // Decode 4 bytes (to 16 bytes of output) per channel + var chunk = s.ReadBytes(4); + var decoded = ImaAdpcmReader.LoadImaAdpcmSound(chunk, ref index[c], ref predictor[c]); + + // Interleave output, one sample per channel + var outOffsetChannel = outOffset + (2 * c); + for (var i = 0; i < decoded.Length; i += 2) + { + var outOffsetSample = outOffsetChannel + i; + if (outOffsetSample >= outputSize) + return output; + + output[outOffsetSample] = decoded[i]; + output[outOffsetSample + 1] = decoded[i + 1]; + outOffsetChannel += 2 * (Channels - 1); + } + + blockOffset += 4; + } + + outOffset += 16 * Channels; + } + } + + return output; + } + } +} diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index 335e4c4e9f..66c553c47d 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -729,6 +729,7 @@ +