Merge pull request #11510 from reaperrr/move-loaders

Move audio loaders from engine to mod level
This commit is contained in:
Oliver Brakmann
2016-06-24 23:59:08 +02:00
committed by GitHub
9 changed files with 190 additions and 152 deletions

View File

@@ -68,7 +68,7 @@ namespace OpenRA.FileSystem
waveHeaderMemoryStream.Write(Encoding.ASCII.GetBytes("WAVE"));
waveHeaderMemoryStream.Write(Encoding.ASCII.GetBytes("fmt "));
waveHeaderMemoryStream.Write(BitConverter.GetBytes(16));
waveHeaderMemoryStream.Write(BitConverter.GetBytes((short)WavReader.WaveType.Pcm));
waveHeaderMemoryStream.Write(BitConverter.GetBytes((short)1));
waveHeaderMemoryStream.Write(BitConverter.GetBytes((short)channels));
waveHeaderMemoryStream.Write(BitConverter.GetBytes(entry.SampleRate));
waveHeaderMemoryStream.Write(BitConverter.GetBytes(2 * channels * entry.SampleRate));
@@ -90,7 +90,7 @@ namespace OpenRA.FileSystem
waveHeaderMemoryStream.Write(Encoding.ASCII.GetBytes("WAVE"));
waveHeaderMemoryStream.Write(Encoding.ASCII.GetBytes("fmt "));
waveHeaderMemoryStream.Write(BitConverter.GetBytes(20));
waveHeaderMemoryStream.Write(BitConverter.GetBytes((short)WavReader.WaveType.ImaAdpcm));
waveHeaderMemoryStream.Write(BitConverter.GetBytes((short)17));
waveHeaderMemoryStream.Write(BitConverter.GetBytes((short)channels));
waveHeaderMemoryStream.Write(BitConverter.GetBytes(entry.SampleRate));
waveHeaderMemoryStream.Write(BitConverter.GetBytes(bytesPerSec));

View File

@@ -95,7 +95,6 @@
<Compile Include="Activities\CallFunc.cs" />
<Compile Include="Actor.cs" />
<Compile Include="CacheStorage.cs" />
<Compile Include="FileFormats\VocLoader.cs" />
<Compile Include="FileSystem\IdxEntry.cs" />
<Compile Include="FileSystem\IPackage.cs" />
<Compile Include="LogProxy.cs" />
@@ -266,13 +265,10 @@
<Compile Include="FieldSaver.cs" />
<Compile Include="Manifest.cs" />
<Compile Include="Graphics\Vertex.cs" />
<Compile Include="FileFormats\ImaAdpcmLoader.cs" />
<Compile Include="FileFormats\AudLoader.cs" />
<Compile Include="FileFormats\Blast.cs" />
<Compile Include="FileFormats\Blowfish.cs" />
<Compile Include="FileFormats\BlowfishKeyProvider.cs" />
<Compile Include="FileFormats\CRC32.cs" />
<Compile Include="FileFormats\WavLoader.cs" />
<Compile Include="FileFormats\XccGlobalDatabase.cs" />
<Compile Include="FileFormats\XccLocalDatabase.cs" />
<Compile Include="FileFormats\HvaReader.cs" />

View File

@@ -0,0 +1,88 @@
#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;
using OpenRA.Mods.Common.FileFormats;
namespace OpenRA.Mods.Common.AudioLoaders
{
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;
}
});
}
}
}

View File

@@ -14,7 +14,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace OpenRA.FileFormats
namespace OpenRA.Mods.Common.AudioLoaders
{
public class VocLoader : ISoundLoader
{

View File

@@ -0,0 +1,85 @@
#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;
using OpenRA.Mods.Common.FileFormats;
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;
});
}
}
}

View File

@@ -12,7 +12,7 @@
using System;
using System.IO;
namespace OpenRA.FileFormats
namespace OpenRA.Mods.Common.FileFormats
{
[Flags]
enum SoundFlags
@@ -44,77 +44,6 @@ namespace OpenRA.FileFormats
}
}
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 };

View File

@@ -11,7 +11,7 @@
using System.IO;
namespace OpenRA.FileFormats
namespace OpenRA.Mods.Common.FileFormats
{
struct ImaAdpcmChunk
{
@@ -29,7 +29,9 @@ namespace OpenRA.FileFormats
}
}
public static class ImaAdpcmLoader
// 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 };
static readonly int[] StepTable =

View File

@@ -12,76 +12,8 @@
using System;
using System.IO;
namespace OpenRA.FileFormats
namespace OpenRA.Mods.Common.FileFormats
{
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;
@@ -220,7 +152,7 @@ namespace OpenRA.FileFormats
{
// 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]);
var decoded = ImaAdpcmReader.LoadImaAdpcmSound(chunk, ref index[c], ref predictor[c]);
// Interleave output, one sample per channel
var outOffsetChannel = outOffset + (2 * c);
@@ -245,4 +177,4 @@ namespace OpenRA.FileFormats
return output;
}
}
}
}

View File

@@ -146,6 +146,9 @@
<Compile Include="AI\States\ProtectionStates.cs" />
<Compile Include="AI\States\StateBase.cs" />
<Compile Include="AI\SupportPowerDecision.cs" />
<Compile Include="AudioLoaders\AudLoader.cs" />
<Compile Include="AudioLoaders\VocLoader.cs" />
<Compile Include="AudioLoaders\WavLoader.cs" />
<Compile Include="Effects\Beacon.cs" />
<Compile Include="Effects\Contrail.cs" />
<Compile Include="Effects\ContrailFader.cs" />
@@ -724,6 +727,9 @@
<Compile Include="Traits\World\EditorSelectionLayer.cs" />
<Compile Include="Graphics\DetectionCircleRenderable.cs" />
<Compile Include="Traits\Render\WithIdleAnimation.cs" />
<Compile Include="FileFormats\AudReader.cs" />
<Compile Include="FileFormats\ImaAdpcmReader.cs" />
<Compile Include="FileFormats\WavReader.cs" />
<Compile Include="FileFormats\VqaReader.cs" />
<Compile Include="FileFormats\LCWCompression.cs" />
<Compile Include="FileFormats\XORDeltaCompression.cs" />