Fixed "OpenAL not found" exceptions if no audio devices are found

This commit is contained in:
Regalis
2017-02-25 14:28:12 +02:00
parent 7aafdadae5
commit 2a60f9d052
2 changed files with 48 additions and 26 deletions
+4 -22
View File
@@ -40,7 +40,7 @@ namespace Barotrauma
if (loadedSound.filePath == file) oggSound = loadedSound.oggSound; if (loadedSound.filePath == file) oggSound = loadedSound.oggSound;
} }
if (oggSound == null) if (oggSound == null && !SoundManager.Disabled)
{ {
try try
{ {
@@ -210,26 +210,6 @@ namespace Barotrauma
} }
} }
//public int Loop(float volume = 1.0f)
//{
// return SoundManager.Loop(this, volume);
//}
//public void Pause()
//{
// SoundManager.Pause(this);
//}
//public void Resume()
//{
// SoundManager.Resume(this);
//}
//public void Stop()
//{
// SoundManager.Stop(this);
//}
public static void OnGameEnd() public static void OnGameEnd()
{ {
List<Sound> removableSounds = loadedSounds.FindAll(s => s.destroyOnGameEnd); List<Sound> removableSounds = loadedSounds.FindAll(s => s.destroyOnGameEnd);
@@ -268,17 +248,19 @@ namespace Barotrauma
public static void StartStream(string file, float volume = 1.0f) public static void StartStream(string file, float volume = 1.0f)
{ {
if (SoundManager.Disabled) return;
stream = SoundManager.StartStream(file, volume); stream = SoundManager.StartStream(file, volume);
} }
public static void StreamVolume(float volume = 1.0f) public static void StreamVolume(float volume = 1.0f)
{ {
if (SoundManager.Disabled) return;
stream.Volume = volume; stream.Volume = volume;
} }
public static void StopStream() public static void StopStream()
{ {
if (stream!=null) SoundManager.StopStream(); if (stream != null) SoundManager.StopStream();
} }
public static void Dispose() public static void Dispose()
+44 -4
View File
@@ -9,6 +9,12 @@ namespace Barotrauma.Sounds
{ {
static class SoundManager static class SoundManager
{ {
public static bool Disabled
{
get;
private set;
}
public const int DefaultSourceCount = 16; public const int DefaultSourceCount = 16;
private static readonly List<int> alSources = new List<int>(); private static readonly List<int> alSources = new List<int>();
@@ -26,9 +32,17 @@ namespace Barotrauma.Sounds
public static void Init() public static void Init()
{ {
var availableDevices = AudioContext.AvailableDevices;
if (availableDevices.Count == 0)
{
DebugConsole.ThrowError("No audio devices found. Disabling audio playback.");
Disabled = true;
return;
}
try try
{ {
AC = new AudioContext(); AC = new AudioContext();
ALHelper.Check(); ALHelper.Check();
} }
catch (DllNotFoundException e) catch (DllNotFoundException e)
@@ -55,11 +69,14 @@ namespace Barotrauma.Sounds
public static int Play(Sound sound, float volume = 1.0f) public static int Play(Sound sound, float volume = 1.0f)
{ {
if (Disabled) return -1;
return Play(sound, Vector2.Zero, volume, 0.0f); return Play(sound, Vector2.Zero, volume, 0.0f);
} }
public static int Play(Sound sound, Vector2 position, float volume = 1.0f, float lowPassGain = 0.0f, bool loop=false) public static int Play(Sound sound, Vector2 position, float volume = 1.0f, float lowPassGain = 0.0f, bool loop=false)
{ {
if (Disabled) return -1;
for (int i = 1; i < DefaultSourceCount; i++) for (int i = 1; i < DefaultSourceCount; i++)
{ {
//find a source that's free to use (not playing or paused) //find a source that's free to use (not playing or paused)
@@ -85,11 +102,15 @@ namespace Barotrauma.Sounds
public static int Loop(Sound sound, int sourceIndex, float volume = 1.0f) public static int Loop(Sound sound, int sourceIndex, float volume = 1.0f)
{ {
if (Disabled) return -1;
return Loop(sound,sourceIndex, Vector2.Zero, volume); return Loop(sound,sourceIndex, Vector2.Zero, volume);
} }
public static int Loop(Sound sound, int sourceIndex, Vector2 position, float volume = 1.0f) public static int Loop(Sound sound, int sourceIndex, Vector2 position, float volume = 1.0f)
{ {
if (Disabled) return -1;
if (!MathUtils.IsValid(volume)) if (!MathUtils.IsValid(volume))
{ {
volume = 0.0f; volume = 0.0f;
@@ -111,6 +132,8 @@ namespace Barotrauma.Sounds
public static void Pause(int sourceIndex) public static void Pause(int sourceIndex)
{ {
if (Disabled) return;
if (AL.GetSourceState(alSources[sourceIndex]) != ALSourceState.Playing) if (AL.GetSourceState(alSources[sourceIndex]) != ALSourceState.Playing)
return; return;
@@ -120,6 +143,8 @@ namespace Barotrauma.Sounds
public static void Resume(int sourceIndex) public static void Resume(int sourceIndex)
{ {
if (Disabled) return;
if (AL.GetSourceState(alSources[sourceIndex]) != ALSourceState.Paused) if (AL.GetSourceState(alSources[sourceIndex]) != ALSourceState.Paused)
return; return;
@@ -129,6 +154,8 @@ namespace Barotrauma.Sounds
public static void Stop(int sourceIndex) public static void Stop(int sourceIndex)
{ {
if (Disabled) return;
if (sourceIndex < 1) return; if (sourceIndex < 1) return;
var state = AL.GetSourceState(alSources[sourceIndex]); var state = AL.GetSourceState(alSources[sourceIndex]);
@@ -143,6 +170,8 @@ namespace Barotrauma.Sounds
public static Sound GetPlayingSound(int sourceIndex) public static Sound GetPlayingSound(int sourceIndex)
{ {
if (Disabled) return null;
if (sourceIndex < 1 || sourceIndex>alSources.Count-1) return null; if (sourceIndex < 1 || sourceIndex>alSources.Count-1) return null;
if (AL.GetSourceState(alSources[sourceIndex]) != ALSourceState.Playing) return null; if (AL.GetSourceState(alSources[sourceIndex]) != ALSourceState.Playing) return null;
@@ -152,6 +181,8 @@ namespace Barotrauma.Sounds
public static bool IsPlaying(int sourceIndex) public static bool IsPlaying(int sourceIndex)
{ {
if (Disabled) return false;
if (sourceIndex < 1 || sourceIndex>alSources.Count-1) return false; if (sourceIndex < 1 || sourceIndex>alSources.Count-1) return false;
return AL.GetSourceState(alSources[sourceIndex]) == ALSourceState.Playing; return AL.GetSourceState(alSources[sourceIndex]) == ALSourceState.Playing;
@@ -159,6 +190,8 @@ namespace Barotrauma.Sounds
public static bool IsPaused(int sourceIndex) public static bool IsPaused(int sourceIndex)
{ {
if (Disabled) return false;
if (sourceIndex < 1 || sourceIndex > alSources.Count - 1) return false; if (sourceIndex < 1 || sourceIndex > alSources.Count - 1) return false;
return AL.GetSourceState(alSources[sourceIndex]) == ALSourceState.Paused; return AL.GetSourceState(alSources[sourceIndex]) == ALSourceState.Paused;
@@ -166,6 +199,7 @@ namespace Barotrauma.Sounds
public static bool IsLooping(int sourceIndex) public static bool IsLooping(int sourceIndex)
{ {
if (Disabled) return false;
if (sourceIndex < 1 || sourceIndex > alSources.Count - 1) return false; if (sourceIndex < 1 || sourceIndex > alSources.Count - 1) return false;
bool isLooping; bool isLooping;
@@ -177,6 +211,8 @@ namespace Barotrauma.Sounds
public static void Volume(int sourceIndex, float volume) public static void Volume(int sourceIndex, float volume)
{ {
if (Disabled) return;
AL.Source(alSources[sourceIndex], ALSourcef.Gain, volume * MasterVolume); AL.Source(alSources[sourceIndex], ALSourcef.Gain, volume * MasterVolume);
ALHelper.Check(); ALHelper.Check();
} }
@@ -187,6 +223,7 @@ namespace Barotrauma.Sounds
get { return lowPassHfGain; } get { return lowPassHfGain; }
set set
{ {
if (Disabled) return;
if (ALHelper.Efx.IsInitialized) if (ALHelper.Efx.IsInitialized)
{ {
lowPassHfGain = value; lowPassHfGain = value;
@@ -200,7 +237,6 @@ namespace Barotrauma.Sounds
ALHelper.Efx.BindFilterToSource(alSources[i], lowpassFilterId); ALHelper.Efx.BindFilterToSource(alSources[i], lowpassFilterId);
ALHelper.Check(); ALHelper.Check();
} }
} }
} }
} }
@@ -208,7 +244,7 @@ namespace Barotrauma.Sounds
public static void UpdateSoundPosition(int sourceIndex, Vector2 position, float baseVolume = 1.0f) public static void UpdateSoundPosition(int sourceIndex, Vector2 position, float baseVolume = 1.0f)
{ {
if (sourceIndex < 1) return; if (sourceIndex < 1 || Disabled) return;
if (!MathUtils.IsValid(position)) if (!MathUtils.IsValid(position))
{ {
@@ -229,6 +265,8 @@ namespace Barotrauma.Sounds
public static OggStream StartStream(string file, float volume = 1.0f) public static OggStream StartStream(string file, float volume = 1.0f)
{ {
if (Disabled) return null;
if (oggStreamer == null) if (oggStreamer == null)
oggStreamer = new OggStreamer(); oggStreamer = new OggStreamer();
@@ -244,7 +282,7 @@ namespace Barotrauma.Sounds
public static void StopStream() public static void StopStream()
{ {
if (oggStream!=null) oggStream.Stop(); if (oggStream != null) oggStream.Stop();
} }
public static void ClearAlSource(int bufferId) public static void ClearAlSource(int bufferId)
@@ -260,6 +298,8 @@ namespace Barotrauma.Sounds
public static void Dispose() public static void Dispose()
{ {
if (Disabled) return;
if (ALHelper.Efx.IsInitialized) if (ALHelper.Efx.IsInitialized)
ALHelper.Efx.DeleteFilter(lowpassFilterId); ALHelper.Efx.DeleteFilter(lowpassFilterId);