Move audio loaders from engine to mod level
This commit is contained in:
241
OpenRA.Mods.Common/AudioLoaders/AudLoader.cs
Normal file
241
OpenRA.Mods.Common/AudioLoaders/AudLoader.cs
Normal file
@@ -0,0 +1,241 @@
|
||||
#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.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)
|
||||
{
|
||||
var start = s.Position;
|
||||
s.Position += 10;
|
||||
var readFlag = s.ReadByte();
|
||||
var readFormat = s.ReadByte();
|
||||
s.Position = start;
|
||||
|
||||
if (!Enum.IsDefined(typeof(SoundFlags), readFlag))
|
||||
return false;
|
||||
|
||||
return Enum.IsDefined(typeof(SoundFormat), readFormat);
|
||||
}
|
||||
|
||||
bool ISoundLoader.TryParseSound(Stream stream, out ISoundFormat sound)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsAud(stream))
|
||||
{
|
||||
sound = new AudFormat(stream);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Not a supported AUD
|
||||
}
|
||||
|
||||
sound = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class AudFormat : ISoundFormat
|
||||
{
|
||||
public int Channels { get { return 1; } }
|
||||
public int SampleBits { get { return 16; } }
|
||||
public int SampleRate { get { return sampleRate; } }
|
||||
public float LengthInSeconds { get { return AudReader.SoundLength(stream); } }
|
||||
public Stream GetPCMInputStream() { return new MemoryStream(rawData.Value); }
|
||||
|
||||
int sampleRate;
|
||||
Lazy<byte[]> rawData;
|
||||
|
||||
Stream stream;
|
||||
|
||||
public AudFormat(Stream stream)
|
||||
{
|
||||
this.stream = stream;
|
||||
|
||||
var position = stream.Position;
|
||||
rawData = Exts.Lazy(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] data;
|
||||
if (!AudReader.LoadSound(stream, out data, out sampleRate))
|
||||
throw new InvalidDataException();
|
||||
return data;
|
||||
}
|
||||
finally
|
||||
{
|
||||
stream.Position = position;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
99
OpenRA.Mods.Common/AudioLoaders/ImaAdpcmLoader.cs
Normal file
99
OpenRA.Mods.Common/AudioLoaders/ImaAdpcmLoader.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
#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.IO;
|
||||
|
||||
namespace OpenRA.Mods.Common.AudioLoaders
|
||||
{
|
||||
struct ImaAdpcmChunk
|
||||
{
|
||||
public int CompressedSize;
|
||||
public int OutputSize;
|
||||
|
||||
public static ImaAdpcmChunk Read(Stream s)
|
||||
{
|
||||
ImaAdpcmChunk c;
|
||||
c.CompressedSize = s.ReadUInt16();
|
||||
c.OutputSize = s.ReadUInt16();
|
||||
if (s.ReadUInt32() != 0xdeaf)
|
||||
throw new InvalidDataException("Chunk header is bogus");
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ImaAdpcmLoader
|
||||
{
|
||||
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 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;
|
||||
|
||||
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[] LoadImaAdpcmSound(byte[] raw, ref int index)
|
||||
{
|
||||
var currentSample = 0;
|
||||
return LoadImaAdpcmSound(raw, ref index, ref currentSample);
|
||||
}
|
||||
|
||||
public static byte[] LoadImaAdpcmSound(byte[] raw, ref int index, ref int currentSample)
|
||||
{
|
||||
var s = new MemoryStream(raw);
|
||||
var dataSize = raw.Length;
|
||||
var outputSize = raw.Length * 4;
|
||||
|
||||
var output = new byte[outputSize];
|
||||
var offset = 0;
|
||||
|
||||
while (dataSize-- > 0)
|
||||
{
|
||||
var b = s.ReadUInt8();
|
||||
|
||||
var t = DecodeImaAdpcmSample(b, ref index, ref currentSample);
|
||||
output[offset++] = (byte)t;
|
||||
output[offset++] = (byte)(t >> 8);
|
||||
|
||||
t = DecodeImaAdpcmSample((byte)(b >> 4), ref index, ref currentSample);
|
||||
output[offset++] = (byte)t;
|
||||
output[offset++] = (byte)(t >> 8);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
||||
360
OpenRA.Mods.Common/AudioLoaders/VocLoader.cs
Normal file
360
OpenRA.Mods.Common/AudioLoaders/VocLoader.cs
Normal file
@@ -0,0 +1,360 @@
|
||||
#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.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenRA.Mods.Common.AudioLoaders
|
||||
{
|
||||
public class VocLoader : ISoundLoader
|
||||
{
|
||||
bool ISoundLoader.TryParseSound(Stream stream, out ISoundFormat sound)
|
||||
{
|
||||
try
|
||||
{
|
||||
sound = new VocFormat(stream);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Not a (supported) WAV
|
||||
}
|
||||
|
||||
sound = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class VocFormat : ISoundFormat
|
||||
{
|
||||
public int SampleBits { get { return 8; } }
|
||||
public int Channels { get { return 1; } }
|
||||
public int SampleRate { get; private set; }
|
||||
public float LengthInSeconds { get { return (float)totalSamples / SampleRate; } }
|
||||
public Stream GetPCMInputStream() { return new VocStream(this); }
|
||||
|
||||
int totalSamples = 0;
|
||||
int samplePosition = 0;
|
||||
|
||||
Stream stream;
|
||||
List<VocBlock> blocks = new List<VocBlock>();
|
||||
IEnumerator<VocBlock> currentBlock;
|
||||
int samplesLeftInBlock = 0;
|
||||
byte[] buffer = new byte[4096];
|
||||
|
||||
struct VocFileHeader
|
||||
{
|
||||
public string Description;
|
||||
public int DatablockOffset;
|
||||
public int Version;
|
||||
public int ID;
|
||||
|
||||
public static VocFileHeader Read(Stream s)
|
||||
{
|
||||
VocFileHeader vfh;
|
||||
vfh.Description = s.ReadASCII(20);
|
||||
vfh.DatablockOffset = s.ReadUInt16();
|
||||
vfh.Version = s.ReadUInt16();
|
||||
vfh.ID = s.ReadUInt16();
|
||||
return vfh;
|
||||
}
|
||||
}
|
||||
|
||||
struct VocBlock
|
||||
{
|
||||
public int Code;
|
||||
public int Length;
|
||||
public VocSampleBlock SampleBlock;
|
||||
public VocLoopBlock LoopBlock;
|
||||
}
|
||||
|
||||
struct VocSampleBlock
|
||||
{
|
||||
public int Rate;
|
||||
public int Samples;
|
||||
public long Offset;
|
||||
}
|
||||
|
||||
struct VocLoopBlock
|
||||
{
|
||||
public int Count;
|
||||
}
|
||||
|
||||
public VocFormat(Stream stream)
|
||||
{
|
||||
this.stream = stream;
|
||||
|
||||
CheckVocHeader();
|
||||
Preload();
|
||||
}
|
||||
|
||||
void CheckVocHeader()
|
||||
{
|
||||
var vfh = VocFileHeader.Read(stream);
|
||||
|
||||
if (!vfh.Description.StartsWith("Creative Voice File"))
|
||||
throw new InvalidDataException("Voc header description not recognized");
|
||||
if (vfh.DatablockOffset != 26)
|
||||
throw new InvalidDataException("Voc header offset is wrong");
|
||||
if (vfh.Version != 0x010A)
|
||||
throw new InvalidDataException("Voc header version not recognized");
|
||||
if (vfh.ID != ~vfh.Version + 0x1234)
|
||||
throw new InvalidDataException("Voc header id is bogus - expected: " +
|
||||
(~vfh.Version + 0x1234).ToString("X") + " but value is : " + vfh.ID.ToString("X"));
|
||||
}
|
||||
|
||||
int GetSampleRateFromVocRate(int vocSampleRate)
|
||||
{
|
||||
if (vocSampleRate == 256)
|
||||
throw new InvalidDataException("Invalid frequency divisor 256 in voc file");
|
||||
if (vocSampleRate == 0xa5 || vocSampleRate == 0xa6)
|
||||
return 11025;
|
||||
else if (vocSampleRate == 0xd2 || vocSampleRate == 0xd3)
|
||||
return 22050;
|
||||
else
|
||||
return (int)(1000000L / (256L - vocSampleRate));
|
||||
}
|
||||
|
||||
void Preload()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
VocBlock block = new VocBlock();
|
||||
try
|
||||
{
|
||||
block.Code = stream.ReadByte();
|
||||
block.Length = 0;
|
||||
}
|
||||
catch (EndOfStreamException)
|
||||
{
|
||||
// Stream is allowed to end without a last block
|
||||
break;
|
||||
}
|
||||
|
||||
if (block.Code == 0 || block.Code > 9)
|
||||
break;
|
||||
|
||||
block.Length = stream.ReadByte();
|
||||
block.Length |= stream.ReadByte() << 8;
|
||||
block.Length |= stream.ReadByte() << 16;
|
||||
|
||||
var skip = 0;
|
||||
switch (block.Code)
|
||||
{
|
||||
// Sound data
|
||||
case 1:
|
||||
{
|
||||
if (block.Length < 2)
|
||||
throw new InvalidDataException("Invalid sound data block length in voc file");
|
||||
var freqDiv = stream.ReadByte();
|
||||
block.SampleBlock.Rate = GetSampleRateFromVocRate(freqDiv);
|
||||
var codec = stream.ReadByte();
|
||||
if (codec != 0)
|
||||
throw new InvalidDataException("Unhandled codec used in voc file");
|
||||
skip = block.Length - 2;
|
||||
block.SampleBlock.Samples = skip;
|
||||
block.SampleBlock.Offset = stream.Position;
|
||||
|
||||
// See if last block contained additional information
|
||||
if (blocks.Count > 0)
|
||||
{
|
||||
var b = blocks.Last();
|
||||
if (b.Code == 8)
|
||||
{
|
||||
block.SampleBlock.Rate = b.SampleBlock.Rate;
|
||||
blocks.Remove(b);
|
||||
}
|
||||
}
|
||||
|
||||
SampleRate = Math.Max(SampleRate, block.SampleBlock.Rate);
|
||||
break;
|
||||
}
|
||||
|
||||
// Silence
|
||||
case 3:
|
||||
{
|
||||
if (block.Length != 3)
|
||||
throw new InvalidDataException("Invalid silence block length in voc file");
|
||||
block.SampleBlock.Offset = 0;
|
||||
block.SampleBlock.Samples = stream.ReadUInt16() + 1;
|
||||
var freqDiv = stream.ReadByte();
|
||||
block.SampleBlock.Rate = GetSampleRateFromVocRate(freqDiv);
|
||||
break;
|
||||
}
|
||||
|
||||
// Repeat start
|
||||
case 6:
|
||||
{
|
||||
if (block.Length != 2)
|
||||
throw new InvalidDataException("Invalid repeat start block length in voc file");
|
||||
block.LoopBlock.Count = stream.ReadUInt16() + 1;
|
||||
break;
|
||||
}
|
||||
|
||||
// Repeat end
|
||||
case 7:
|
||||
break;
|
||||
|
||||
// Extra info
|
||||
case 8:
|
||||
{
|
||||
if (block.Length != 4)
|
||||
throw new InvalidDataException("Invalid info block length in voc file");
|
||||
int freqDiv = stream.ReadUInt16();
|
||||
if (freqDiv == 65536)
|
||||
throw new InvalidDataException("Invalid frequency divisor 65536 in voc file");
|
||||
var codec = stream.ReadByte();
|
||||
if (codec != 0)
|
||||
throw new InvalidDataException("Unhandled codec used in voc file");
|
||||
var channels = stream.ReadByte() + 1;
|
||||
if (channels != 1)
|
||||
throw new InvalidDataException("Unhandled number of channels in voc file");
|
||||
block.SampleBlock.Offset = 0;
|
||||
block.SampleBlock.Samples = 0;
|
||||
block.SampleBlock.Rate = (int)(256000000L / (65536L - freqDiv));
|
||||
break;
|
||||
}
|
||||
|
||||
// Sound data (New format)
|
||||
case 9:
|
||||
default:
|
||||
throw new InvalidDataException("Unhandled code in voc file");
|
||||
}
|
||||
|
||||
if (skip > 0)
|
||||
stream.Seek(skip, SeekOrigin.Current);
|
||||
blocks.Add(block);
|
||||
}
|
||||
|
||||
// Check validity and calculated total number of samples
|
||||
foreach (var b in blocks)
|
||||
{
|
||||
if (b.Code == 8)
|
||||
throw new InvalidDataException("Unused block 8 in voc file");
|
||||
if (b.Code != 1 && b.Code != 9)
|
||||
continue;
|
||||
if (b.SampleBlock.Rate != SampleRate)
|
||||
throw new InvalidDataException("Voc file contains chunks with different sample rate");
|
||||
totalSamples += b.SampleBlock.Samples;
|
||||
}
|
||||
|
||||
Rewind();
|
||||
}
|
||||
|
||||
void Rewind()
|
||||
{
|
||||
currentBlock = blocks.GetEnumerator();
|
||||
samplesLeftInBlock = 0;
|
||||
samplePosition = 0;
|
||||
|
||||
while (currentBlock.MoveNext())
|
||||
{
|
||||
if (currentBlock.Current.Code == 1)
|
||||
{
|
||||
stream.Seek(currentBlock.Current.SampleBlock.Offset, SeekOrigin.Begin);
|
||||
samplesLeftInBlock = currentBlock.Current.SampleBlock.Samples;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool EndOfData { get { return currentBlock.Current.Equals(blocks.Last()) && samplesLeftInBlock == 0; } }
|
||||
|
||||
int FillBuffer(int maxSamples)
|
||||
{
|
||||
var bufferedSamples = 0;
|
||||
var offset = 0;
|
||||
|
||||
maxSamples = Math.Min(buffer.Length, maxSamples);
|
||||
|
||||
while (maxSamples > 0 && !EndOfData)
|
||||
{
|
||||
var len = Math.Min(maxSamples, samplesLeftInBlock);
|
||||
stream.ReadBytes(buffer, offset, len);
|
||||
offset += len;
|
||||
var samplesRead = len;
|
||||
bufferedSamples += samplesRead;
|
||||
maxSamples -= samplesRead;
|
||||
samplesLeftInBlock -= samplesRead;
|
||||
samplePosition += len;
|
||||
|
||||
UpdateBlockIfNeeded();
|
||||
}
|
||||
|
||||
return bufferedSamples;
|
||||
}
|
||||
|
||||
void UpdateBlockIfNeeded()
|
||||
{
|
||||
if (samplesLeftInBlock == 0)
|
||||
{
|
||||
while (currentBlock.MoveNext())
|
||||
{
|
||||
if (currentBlock.Current.Code != 1 && currentBlock.Current.Code != 9)
|
||||
continue;
|
||||
stream.Seek(currentBlock.Current.SampleBlock.Offset, SeekOrigin.Begin);
|
||||
samplesLeftInBlock = currentBlock.Current.SampleBlock.Samples;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
var bytesWritten = 0;
|
||||
var samplesLeft = Math.Min(count, buffer.Length - offset);
|
||||
while (samplesLeft > 0)
|
||||
{
|
||||
var len = FillBuffer(samplesLeft);
|
||||
if (len == 0)
|
||||
break;
|
||||
Buffer.BlockCopy(this.buffer, 0, buffer, offset, len);
|
||||
samplesLeft -= len;
|
||||
offset += len;
|
||||
bytesWritten += len;
|
||||
}
|
||||
|
||||
return bytesWritten;
|
||||
}
|
||||
|
||||
public class VocStream : Stream
|
||||
{
|
||||
VocFormat format;
|
||||
public VocStream(VocFormat format)
|
||||
{
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public override bool CanRead { get { return format.samplePosition < format.totalSamples; } }
|
||||
public override bool CanSeek { get { return false; } }
|
||||
public override bool CanWrite { get { return false; } }
|
||||
|
||||
public override long Length { get { return format.totalSamples; } }
|
||||
public override long Position
|
||||
{
|
||||
get { return format.samplePosition; }
|
||||
set { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
return format.Read(buffer, offset, count);
|
||||
}
|
||||
|
||||
public override void Flush() { throw new NotImplementedException(); }
|
||||
public override long Seek(long offset, SeekOrigin origin) { throw new NotImplementedException(); }
|
||||
public override void SetLength(long value) { throw new NotImplementedException(); }
|
||||
public override void Write(byte[] buffer, int offset, int count) { throw new NotImplementedException(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
248
OpenRA.Mods.Common/AudioLoaders/WavLoader.cs
Normal file
248
OpenRA.Mods.Common/AudioLoaders/WavLoader.cs
Normal file
@@ -0,0 +1,248 @@
|
||||
#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.AudioLoaders
|
||||
{
|
||||
public class WavLoader : ISoundLoader
|
||||
{
|
||||
bool IsWave(Stream s)
|
||||
{
|
||||
var start = s.Position;
|
||||
var type = s.ReadASCII(4);
|
||||
s.Position += 4;
|
||||
var format = s.ReadASCII(4);
|
||||
s.Position = start;
|
||||
|
||||
return type == "RIFF" && format == "WAVE";
|
||||
}
|
||||
|
||||
bool ISoundLoader.TryParseSound(Stream stream, out ISoundFormat sound)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsWave(stream))
|
||||
{
|
||||
sound = new WavFormat(stream);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Not a (supported) WAV
|
||||
}
|
||||
|
||||
sound = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class WavFormat : ISoundFormat
|
||||
{
|
||||
public int Channels { get { return reader.Value.Channels; } }
|
||||
public int SampleBits { get { return reader.Value.BitsPerSample; } }
|
||||
public int SampleRate { get { return reader.Value.SampleRate; } }
|
||||
public float LengthInSeconds { get { return WavReader.WaveLength(stream); } }
|
||||
public Stream GetPCMInputStream() { return new MemoryStream(reader.Value.RawOutput); }
|
||||
|
||||
Lazy<WavReader> reader;
|
||||
|
||||
readonly Stream stream;
|
||||
|
||||
public WavFormat(Stream stream)
|
||||
{
|
||||
this.stream = stream;
|
||||
|
||||
var position = stream.Position;
|
||||
reader = Exts.Lazy(() =>
|
||||
{
|
||||
var wavReader = new WavReader();
|
||||
try
|
||||
{
|
||||
if (!wavReader.LoadSound(stream))
|
||||
throw new InvalidDataException();
|
||||
}
|
||||
finally
|
||||
{
|
||||
stream.Position = position;
|
||||
}
|
||||
|
||||
return wavReader;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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 = ImaAdpcmLoader.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user