Lazily parse Aud data.

This commit is contained in:
Paul Chote
2016-03-12 13:10:46 +00:00
parent 6bbe785019
commit 3e37b717a4

View File

@@ -46,13 +46,30 @@ namespace OpenRA.FileFormats
public class AudLoader : ISoundLoader 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) bool ISoundLoader.TryParseSound(Stream stream, out ISoundFormat sound)
{ {
try try
{
if (IsAud(stream))
{ {
sound = new AudFormat(stream); sound = new AudFormat(stream);
return true; return true;
} }
}
catch catch
{ {
// Not a supported AUD // Not a supported AUD
@@ -69,10 +86,10 @@ namespace OpenRA.FileFormats
public int SampleBits { get { return 16; } } public int SampleBits { get { return 16; } }
public int SampleRate { get { return sampleRate; } } public int SampleRate { get { return sampleRate; } }
public float LengthInSeconds { get { return AudReader.SoundLength(stream); } } public float LengthInSeconds { get { return AudReader.SoundLength(stream); } }
public Stream GetPCMInputStream() { return new MemoryStream(rawData); } public Stream GetPCMInputStream() { return new MemoryStream(rawData.Value); }
int sampleRate; int sampleRate;
byte[] rawData; Lazy<byte[]> rawData;
Stream stream; Stream stream;
@@ -81,15 +98,20 @@ namespace OpenRA.FileFormats
this.stream = stream; this.stream = stream;
var position = stream.Position; var position = stream.Position;
rawData = Exts.Lazy(() =>
{
try try
{ {
if (!AudReader.LoadSound(stream, out rawData, out sampleRate)) byte[] data;
if (!AudReader.LoadSound(stream, out data, out sampleRate))
throw new InvalidDataException(); throw new InvalidDataException();
return data;
} }
finally finally
{ {
stream.Position = position; stream.Position = position;
} }
});
} }
} }