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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user