Make WavLoader implement ISoundLoader
This commit is contained in:
@@ -13,48 +13,53 @@ using System.IO;
|
|||||||
|
|
||||||
namespace OpenRA.FileFormats
|
namespace OpenRA.FileFormats
|
||||||
{
|
{
|
||||||
public class WavLoader
|
public class WavLoader : ISoundLoader
|
||||||
{
|
{
|
||||||
public readonly int FileSize;
|
public int FileSize;
|
||||||
public readonly string Format;
|
public string Format;
|
||||||
|
|
||||||
public readonly int FmtChunkSize;
|
public int FmtChunkSize;
|
||||||
public readonly int AudioFormat;
|
public int AudioFormat;
|
||||||
public readonly int Channels;
|
public int Channels;
|
||||||
public readonly int SampleRate;
|
public int SampleRate;
|
||||||
public readonly int ByteRate;
|
public int ByteRate;
|
||||||
public readonly int BlockAlign;
|
public int BlockAlign;
|
||||||
public readonly int BitsPerSample;
|
public int BitsPerSample;
|
||||||
|
|
||||||
public readonly int UncompressedSize;
|
public int UncompressedSize;
|
||||||
public readonly int DataSize;
|
public int DataSize;
|
||||||
public readonly byte[] RawOutput;
|
public byte[] RawOutput;
|
||||||
|
|
||||||
public enum WaveType { Pcm = 0x1, ImaAdpcm = 0x11 }
|
public enum WaveType { Pcm = 0x1, ImaAdpcm = 0x11 }
|
||||||
public static WaveType Type { get; private set; }
|
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)
|
while (s.Position < s.Length)
|
||||||
{
|
{
|
||||||
if ((s.Position & 1) == 1)
|
if ((s.Position & 1) == 1)
|
||||||
s.ReadByte(); // Alignment
|
s.ReadByte(); // Alignment
|
||||||
|
|
||||||
var type = s.ReadASCII(4);
|
type = s.ReadASCII(4);
|
||||||
switch (type)
|
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 ":
|
case "fmt ":
|
||||||
FmtChunkSize = s.ReadInt32();
|
FmtChunkSize = s.ReadInt32();
|
||||||
AudioFormat = s.ReadInt16();
|
AudioFormat = s.ReadInt16();
|
||||||
Type = (WaveType)AudioFormat;
|
Type = (WaveType)AudioFormat;
|
||||||
|
|
||||||
if (Type != WaveType.Pcm && Type != WaveType.ImaAdpcm)
|
if (Type != WaveType.Pcm && Type != WaveType.ImaAdpcm)
|
||||||
throw new NotSupportedException("Compression type is not supported.");
|
throw new NotSupportedException("Compression type is not supported.");
|
||||||
|
|
||||||
Channels = s.ReadInt16();
|
Channels = s.ReadInt16();
|
||||||
SampleRate = s.ReadInt32();
|
SampleRate = s.ReadInt32();
|
||||||
ByteRate = s.ReadInt32();
|
ByteRate = s.ReadInt32();
|
||||||
@@ -91,6 +96,8 @@ namespace OpenRA.FileFormats
|
|||||||
RawOutput = DecodeImaAdpcmData();
|
RawOutput = DecodeImaAdpcmData();
|
||||||
BitsPerSample = 16;
|
BitsPerSample = 16;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static float WaveLength(Stream s)
|
public static float WaveLength(Stream s)
|
||||||
@@ -176,5 +183,22 @@ namespace OpenRA.FileFormats
|
|||||||
|
|
||||||
return output;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user