Sounds can take over an audio channel that's playing at a lower volume if no free channels are found

This commit is contained in:
Regalis
2017-05-23 20:02:46 +03:00
parent c2b6632877
commit 3183c9fb03
+45 -25
View File
@@ -45,7 +45,7 @@ namespace Barotrauma.Sounds
AC = new AudioContext(); AC = new AudioContext();
ALHelper.Check(); ALHelper.Check();
} }
catch (DllNotFoundException e) catch (DllNotFoundException)
{ {
Program.CrashMessageBox("OpenAL32.dll not found"); Program.CrashMessageBox("OpenAL32.dll not found");
throw; throw;
@@ -77,27 +77,55 @@ namespace Barotrauma.Sounds
{ {
if (Disabled || sound.AlBufferId == -1) return -1; if (Disabled || sound.AlBufferId == -1) return -1;
for (int i = 1; i < DefaultSourceCount; i++) int sourceIndex = FindAudioSource(volume);
if (sourceIndex > -1)
{ {
//find a source that's free to use (not playing or paused) soundsPlaying[sourceIndex] = sound;
if (OpenTK.Audio.OpenAL.AL.GetSourceState(alSources[i]) == OpenTK.Audio.OpenAL.ALSourceState.Playing alBuffers[sourceIndex] = sound.AlBufferId;
|| OpenTK.Audio.OpenAL.AL.GetSourceState(alSources[i]) == OpenTK.Audio.OpenAL.ALSourceState.Paused) continue; OpenTK.Audio.OpenAL.AL.Source(alSources[sourceIndex], OpenTK.Audio.OpenAL.ALSourceb.Looping, loop);
OpenTK.Audio.OpenAL.AL.Source(alSources[sourceIndex], OpenTK.Audio.OpenAL.ALSourcei.Buffer, sound.AlBufferId);
soundsPlaying[i] = sound; UpdateSoundPosition(sourceIndex, position, volume);
alBuffers[i] = sound.AlBufferId; OpenTK.Audio.OpenAL.AL.SourcePlay(alSources[sourceIndex]);
OpenTK.Audio.OpenAL.AL.Source(alSources[i], OpenTK.Audio.OpenAL.ALSourceb.Looping, loop);
OpenTK.Audio.OpenAL.AL.Source(alSources[i], OpenTK.Audio.OpenAL.ALSourcei.Buffer, sound.AlBufferId);
UpdateSoundPosition(i, position, volume);
OpenTK.Audio.OpenAL.AL.SourcePlay(alSources[i]);
return i;
} }
return -1; return sourceIndex;
}
private static int FindAudioSource(float volume)
{
//find a source that's free to use (not playing or paused)
for (int i = 1; i < DefaultSourceCount; i++)
{
if (OpenTK.Audio.OpenAL.AL.GetSourceState(alSources[i]) == OpenTK.Audio.OpenAL.ALSourceState.Initial
|| OpenTK.Audio.OpenAL.AL.GetSourceState(alSources[i]) == OpenTK.Audio.OpenAL.ALSourceState.Stopped)
{
return i;
}
}
//not found -> take up the channel that is playing at the lowest volume
float lowestVolume = volume;
int quietestSourceIndex = -1;
for (int i = 1; i < DefaultSourceCount; i++)
{
float vol;
OpenTK.Audio.OpenAL.AL.GetSource(alSources[i], ALSourcef.Gain, out vol);
if (vol < lowestVolume)
{
quietestSourceIndex = i;
lowestVolume = vol;
}
}
if (quietestSourceIndex > -1)
{
Stop(quietestSourceIndex);
}
return quietestSourceIndex;
} }
public static int Loop(Sound sound, int sourceIndex, float volume = 1.0f) public static int Loop(Sound sound, int sourceIndex, float volume = 1.0f)
@@ -116,7 +144,7 @@ namespace Barotrauma.Sounds
volume = 0.0f; volume = 0.0f;
} }
if (sourceIndex<1) if (sourceIndex<1 || soundsPlaying[sourceIndex] != sound)
{ {
sourceIndex = Play(sound, position, volume, 0.0f, true); sourceIndex = Play(sound, position, volume, 0.0f, true);
} }
@@ -209,14 +237,6 @@ namespace Barotrauma.Sounds
return isLooping; return isLooping;
} }
public static void Volume(int sourceIndex, float volume)
{
if (Disabled) return;
AL.Source(alSources[sourceIndex], ALSourcef.Gain, volume * MasterVolume);
ALHelper.Check();
}
static float lowPassHfGain; static float lowPassHfGain;
public static float LowPassHFGain public static float LowPassHFGain
{ {