Object oriented sound loader approach

Reshapes the ISoundLoader interface and
adds a new ISoundFormat interface to allow streaming in the near future
This commit is contained in:
teees
2016-02-02 10:18:50 +01:00
committed by Paul Chote
parent c32bf9f8f7
commit 0193ee5b3c
7 changed files with 180 additions and 151 deletions

View File

@@ -21,7 +21,16 @@ namespace OpenRA
{
public interface ISoundLoader
{
bool TryParseSound(Stream stream, string fileName, out byte[] rawData, out int channels, out int sampleBits, out int sampleRate);
bool TryParseSound(Stream stream, out ISoundFormat sound);
}
public interface ISoundFormat
{
int Channels { get; }
int SampleBits { get; }
int SampleRate { get; }
float LengthInSeconds { get; }
Stream GetPCMInputStream();
}
public sealed class Sound : IDisposable
@@ -35,7 +44,7 @@ namespace OpenRA
public Sound(string engineName)
{
var enginePath = Platform.ResolvePath(Path.Combine(".", "OpenRA.Platforms." + engineName + ".dll"));
var enginePath = Platform.ResolvePath(".", "OpenRA.Platforms." + engineName + ".dll");
soundEngine = CreateDevice(Assembly.LoadFile(enginePath));
}
@@ -60,16 +69,17 @@ namespace OpenRA
using (var stream = fileSystem.Open(filename))
{
byte[] rawData;
int channels;
int sampleBits;
int sampleRate;
foreach (var loader in loaders)
if (loader.TryParseSound(stream, filename, out rawData, out channels, out sampleBits, out sampleRate))
return soundEngine.AddSoundSourceFromMemory(rawData, channels, sampleBits, sampleRate);
throw new InvalidDataException(filename + " is not a valid sound file!");
ISoundFormat soundFormat;
foreach (var loader in Game.ModData.SoundLoaders)
{
stream.Position = 0;
if (loader.TryParseSound(stream, out soundFormat))
return soundEngine.AddSoundSourceFromMemory(
soundFormat.GetPCMInputStream().ReadAllBytes(), soundFormat.Channels, soundFormat.SampleBits, soundFormat.SampleRate);
}
}
throw new InvalidDataException(filename + " is not a valid sound file!");
}
public void Initialize(ISoundLoader[] loaders, IReadOnlyFileSystem fileSystem)