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,12 +46,29 @@ 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
{ {
sound = new AudFormat(stream); if (IsAud(stream))
return true; {
sound = new AudFormat(stream);
return true;
}
} }
catch catch
{ {
@@ -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;
try rawData = Exts.Lazy(() =>
{ {
if (!AudReader.LoadSound(stream, out rawData, out sampleRate)) try
throw new InvalidDataException(); {
} byte[] data;
finally if (!AudReader.LoadSound(stream, out data, out sampleRate))
{ throw new InvalidDataException();
stream.Position = position; return data;
} }
finally
{
stream.Position = position;
}
});
} }
} }