Remove duplication between IMA and AUD readers
These parts were identical duplicates, so I removed them and made AudReader depend on ImaAdpcmReader instead. Applied some style fixes while I was at it.
This commit is contained in:
@@ -13,7 +13,7 @@ using System.IO;
|
||||
|
||||
namespace OpenRA.Mods.Common.FileFormats
|
||||
{
|
||||
struct ImaAdpcmChunk
|
||||
public struct ImaAdpcmChunk
|
||||
{
|
||||
public int CompressedSize;
|
||||
public int OutputSize;
|
||||
@@ -23,14 +23,14 @@ namespace OpenRA.Mods.Common.FileFormats
|
||||
ImaAdpcmChunk c;
|
||||
c.CompressedSize = s.ReadUInt16();
|
||||
c.OutputSize = s.ReadUInt16();
|
||||
|
||||
if (s.ReadUInt32() != 0xdeaf)
|
||||
throw new InvalidDataException("Chunk header is bogus");
|
||||
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
// 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 };
|
||||
@@ -48,21 +48,28 @@ namespace OpenRA.Mods.Common.FileFormats
|
||||
16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
|
||||
};
|
||||
|
||||
static short DecodeImaAdpcmSample(byte b, ref int index, ref int current)
|
||||
public static short DecodeImaAdpcmSample(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;
|
||||
if (sb)
|
||||
delta = -delta;
|
||||
|
||||
current += delta;
|
||||
if (current > short.MaxValue) current = short.MaxValue;
|
||||
if (current < short.MinValue) current = short.MinValue;
|
||||
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;
|
||||
if (index < 0)
|
||||
index = 0;
|
||||
|
||||
if (index > 88)
|
||||
index = 88;
|
||||
|
||||
return (short)current;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user