Skip to content

Commit

Permalink
OpenAL context initialization refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasFOG committed Jan 7, 2019
1 parent 02349f4 commit ae7526c
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 35 deletions.
2 changes: 1 addition & 1 deletion MonoGame.Framework/Audio/DynamicSoundEffectInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public DynamicSoundEffectInstance(int sampleRate, AudioChannels channels)
{
SoundEffect.Initialize();
if (SoundEffect._systemState != SoundEffect.SoundSystemState.Initialized)
return;
throw new NoAudioHardwareException("Audio has failed to initialize. Call SoundEffect.Initialize() before sound operation to get more specific errors.");

if ((sampleRate < 8000) || (sampleRate > 48000))
throw new ArgumentOutOfRangeException("sampleRate");
Expand Down
4 changes: 2 additions & 2 deletions MonoGame.Framework/Audio/OALSoundBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public double Duration

public void BindDataBuffer(byte[] dataBuffer, ALFormat format, int size, int sampleRate, int sampleAlignment = 0)
{
if ((format == ALFormat.MonoMSAdpcm || format == ALFormat.StereoMSAdpcm) && !OpenALSoundController.GetInstance.SupportsAdpcm)
if ((format == ALFormat.MonoMSAdpcm || format == ALFormat.StereoMSAdpcm) && !OpenALSoundController.Instance.SupportsAdpcm)
throw new InvalidOperationException("MS-ADPCM is not supported by this OpenAL driver");
if ((format == ALFormat.MonoIma4 || format == ALFormat.StereoIma4) && !OpenALSoundController.GetInstance.SupportsIma4)
if ((format == ALFormat.MonoIma4 || format == ALFormat.StereoIma4) && !OpenALSoundController.Instance.SupportsIma4)
throw new InvalidOperationException("IMA/ADPCM is not supported by this OpenAL driver");

openALFormat = format;
Expand Down
4 changes: 2 additions & 2 deletions MonoGame.Framework/Audio/OggStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public OggStream(string filename, Action finishedAction = null, int bufferCount

alBufferIds = AL.GenBuffers(bufferCount);
ALHelper.CheckError("Failed to generate buffers.");
alSourceId = OpenALSoundController.GetInstance.ReserveSource();
alSourceId = OpenALSoundController.Instance.ReserveSource();

if (OggStreamer.Instance.XRam.IsInitialized)
{
Expand Down Expand Up @@ -238,7 +238,7 @@ public void Dispose()

AL.Source(alSourceId, ALSourcei.Buffer, 0);
ALHelper.CheckError("Failed to free source from buffers.");
OpenALSoundController.GetInstance.RecycleSource(alSourceId);
OpenALSoundController.Instance.RecycleSource(alSourceId);
AL.DeleteBuffers(alBufferIds);
ALHelper.CheckError("Failed to delete buffer.");
if (OggStreamer.Instance.Efx.IsInitialized)
Expand Down
45 changes: 26 additions & 19 deletions MonoGame.Framework/Audio/OpenALSoundController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,29 +286,36 @@ Now use OpenSL ES to create an AudioPlayer with PCM buffer queue data locator.
return false;
}

public static OpenALSoundController GetInstance
public static void EnsureInitialized()
{
get
if (_instance == null)
{
if (_instance == null)
try
{
try
{
_instance = new OpenALSoundController();
}
catch (DllNotFoundException)
{
throw;
}
catch (NoAudioHardwareException)
{
throw;
}
catch (Exception ex)
{
throw (new NoAudioHardwareException("Failed to init OpenALSoundController", ex));
}
_instance = new OpenALSoundController();
}
catch (DllNotFoundException)
{
throw;
}
catch (NoAudioHardwareException)
{
throw;
}
catch (Exception ex)
{
throw (new NoAudioHardwareException("Failed to init OpenALSoundController", ex));
}
}
}


public static OpenALSoundController Instance
{
get
{
if (_instance == null)
throw new NoAudioHardwareException("OpenAL context has failed to initialize. Call SoundEffect.Initialize() before sound operation to get more specific errors.");
return _instance;
}
}
Expand Down
8 changes: 4 additions & 4 deletions MonoGame.Framework/Audio/SoundEffect.OpenAL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void PlatformInitializePcm(byte[] buffer, int offset, int count, int sam

private void PlatformInitializeIeeeFloat(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int loopStart, int loopLength)
{
if (!OpenALSoundController.GetInstance.SupportsIeee)
if (!OpenALSoundController.Instance.SupportsIeee)
{
// If 32-bit IEEE float is not supported, convert to 16-bit signed PCM
buffer = AudioLoader.ConvertFloatTo16(buffer, offset, count);
Expand All @@ -80,7 +80,7 @@ private void PlatformInitializeIeeeFloat(byte[] buffer, int offset, int count, i

private void PlatformInitializeAdpcm(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int blockAlignment, int loopStart, int loopLength)
{
if (!OpenALSoundController.GetInstance.SupportsAdpcm)
if (!OpenALSoundController.Instance.SupportsAdpcm)
{
// If MS-ADPCM is not supported, convert to 16-bit signed PCM
buffer = AudioLoader.ConvertMsAdpcmToPcm(buffer, offset, count, (int)channels, blockAlignment);
Expand All @@ -100,7 +100,7 @@ private void PlatformInitializeAdpcm(byte[] buffer, int offset, int count, int s

private void PlatformInitializeIma4(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int blockAlignment, int loopStart, int loopLength)
{
if (!OpenALSoundController.GetInstance.SupportsIma4)
if (!OpenALSoundController.Instance.SupportsIma4)
{
// If IMA/ADPCM is not supported, convert to 16-bit signed PCM
buffer = AudioLoader.ConvertIma4ToPcm(buffer, offset, count, (int)channels, blockAlignment);
Expand Down Expand Up @@ -237,7 +237,7 @@ private void PlatformDispose(bool disposing)

internal static void PlatformInitialize()
{
var inst = OpenALSoundController.GetInstance;
OpenALSoundController.EnsureInitialized();
}

internal static void PlatformShutdown()
Expand Down
8 changes: 4 additions & 4 deletions MonoGame.Framework/Audio/SoundEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private SoundEffect(Stream stream)
{
Initialize();
if (_systemState != SoundSystemState.Initialized)
return;
throw new NoAudioHardwareException("Audio has failed to initialize. Call SoundEffect.Initialize() before sound operation to get more specific errors.");

/*
The Stream object must point to the head of a valid PCM wave file. Also, this wave file must be in the RIFF bitstream format.
Expand All @@ -50,7 +50,7 @@ internal SoundEffect(byte[] header, byte[] buffer, int bufferSize, int durationM
{
Initialize();
if (_systemState != SoundSystemState.Initialized)
return;
throw new NoAudioHardwareException("Audio has failed to initialize. Call SoundEffect.Initialize() before sound operation to get more specific errors.");

_duration = TimeSpan.FromMilliseconds(durationMs);

Expand All @@ -74,7 +74,7 @@ internal SoundEffect(MiniFormatTag codec, byte[] buffer, int channels, int sampl
{
Initialize();
if (_systemState != SoundSystemState.Initialized)
return;
throw new NoAudioHardwareException("Audio has failed to initialize. Call SoundEffect.Initialize() before sound operation to get more specific errors.");

// Handle the common case... the rest is platform specific.
if (codec == MiniFormatTag.Pcm)
Expand Down Expand Up @@ -153,7 +153,7 @@ public SoundEffect(byte[] buffer, int offset, int count, int sampleRate, AudioCh
{
Initialize();
if (_systemState != SoundSystemState.Initialized)
return;
throw new NoAudioHardwareException("Audio has failed to initialize. Call SoundEffect.Initialize() before sound operation to get more specific errors.");

if (sampleRate < 8000 || sampleRate > 48000)
throw new ArgumentOutOfRangeException("sampleRate");
Expand Down
4 changes: 2 additions & 2 deletions MonoGame.Framework/Audio/SoundEffectInstance.OpenAL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal void PlatformInitialize(byte[] buffer, int sampleRate, int channels)
/// </summary>
internal void InitializeSound()
{
controller = OpenALSoundController.GetInstance;
controller = OpenALSoundController.Instance;
}

#endregion // Initialization
Expand Down Expand Up @@ -174,7 +174,7 @@ private void PlatformStop(bool immediate)

// Reset the SendFilter to 0 if we are NOT using reverb since
// sources are recycled
if (OpenALSoundController.GetInstance.SupportsEfx)
if (OpenALSoundController.Instance.SupportsEfx)
{
OpenALSoundController.Efx.BindSourceToAuxiliarySlot(SourceId, 0, 0, 0);
ALHelper.CheckError("Failed to unset reverb.");
Expand Down
2 changes: 1 addition & 1 deletion MonoGame.Framework/Media/Song.NVorbis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed partial class Song : IEquatable<Song>, IDisposable
private void PlatformInitialize(string fileName)
{
// init OpenAL if need be
var inst = OpenALSoundController.GetInstance;
OpenALSoundController.EnsureInitialized();

stream = new OggStream(fileName, OnFinishedPlaying);
stream.Prepare();
Expand Down

0 comments on commit ae7526c

Please sign in to comment.