Audio channel states visible in debug view + some sound fixes:
- a short "cooldown" after each footstep sound (prevents the sound playing multiple times during one footstep) - water flow sounds are only played by one of the hulls a gap is between - flow rate affects the range of the flow sound
This commit is contained in:
@@ -14,6 +14,8 @@ namespace Barotrauma.Sounds
|
||||
private static readonly List<int> alSources = new List<int>();
|
||||
private static readonly int[] alBuffers = new int[DefaultSourceCount];
|
||||
private static int lowpassFilterId;
|
||||
|
||||
private static readonly Sound[] soundsPlaying = new Sound[DefaultSourceCount];
|
||||
|
||||
private static AudioContext AC;
|
||||
|
||||
@@ -47,112 +49,33 @@ namespace Barotrauma.Sounds
|
||||
|
||||
LowPassHFGain = 1.0f;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
//public SoundManager(int bufferCount = DefaultSourceCount)
|
||||
//{
|
||||
// Stopwatch sw = new Stopwatch();
|
||||
// sw.Start();
|
||||
|
||||
// alSourceId = AL.GenSource();
|
||||
// //AL.Source(alSourceId, ALSourcei.Buffer, alBufferId);
|
||||
|
||||
// Volume = 1;
|
||||
|
||||
// if (ALHelper.Efx.IsInitialized)
|
||||
// {
|
||||
// alFilterId = ALHelper.Efx.GenFilter();
|
||||
// ALHelper.Efx.Filter(alFilterId, EfxFilteri.FilterType, (int)EfxFilterType.Lowpass);
|
||||
// ALHelper.Efx.Filter(alFilterId, EfxFilterf.LowpassGain, 1);
|
||||
// LowPassHFGain = 1;
|
||||
// }
|
||||
|
||||
// sw.Stop();
|
||||
// System.Diagnostics.Debug.WriteLine("oggsource: "+sw.ElapsedMilliseconds);
|
||||
|
||||
//}
|
||||
|
||||
// public static int Play(Sound sound, float volume = 1.0f)
|
||||
// {
|
||||
// return Play(sound, volume, new Vector2(0.0f, 0.0f));
|
||||
//}
|
||||
|
||||
public static int Play(Sound sound, float volume = 1.0f)
|
||||
{
|
||||
return Play(sound, Vector2.Zero, volume, 0.0f);
|
||||
//for (int i = 2; i < DefaultSourceCount; i++)
|
||||
//{
|
||||
// AL.SourceStop(alSources[i]);
|
||||
// AL.Source(alSources[i], ALSourceb.Looping, false);
|
||||
// System.Diagnostics.Debug.WriteLine(i + ": " + AL.GetSourceState(alSources[i]));
|
||||
// System.Diagnostics.Debug.WriteLine(AL.GetSourceType(alSources[i]));
|
||||
//}
|
||||
|
||||
//for (int i = 1; i < DefaultSourceCount; i++)
|
||||
//{
|
||||
// //find a source that's free to use (not playing or paused)
|
||||
// if (AL.GetSourceState(alSources[i]) == ALSourceState.Playing
|
||||
// || AL.GetSourceState(alSources[i]) == ALSourceState.Paused) continue;
|
||||
|
||||
// //if (position!=Vector2.Zero)
|
||||
// // position /= 1000.0f;
|
||||
|
||||
// alBuffers[i]=sound.AlBufferId;
|
||||
// AL.Source(alSources[i], ALSourceb.Looping, false);
|
||||
// AL.Source(alSources[i], ALSource3f.Position, 0.0f, 0.0f, 0.0f);
|
||||
// AL.Source(alSources[i], ALSourcei.Buffer, sound.AlBufferId);
|
||||
// AL.Source(alSources[i], ALSourcef.Gain, volume);
|
||||
// //AL.Source(alSources[i], ALSource3f.Position, position.X, position.Y, 0.0f);
|
||||
// AL.SourcePlay(alSources[i]);
|
||||
|
||||
// //sound.sourceIndex = i;
|
||||
|
||||
// return i;
|
||||
//}
|
||||
|
||||
//return -1;
|
||||
}
|
||||
|
||||
public static int Play(Sound sound, Vector2 position, float volume = 1.0f, float lowPassGain = 0.0f, bool loop=false)
|
||||
{
|
||||
//for (int i = 2; i < DefaultSourceCount; i++)
|
||||
//{
|
||||
// AL.SourceStop(alSources[i]);
|
||||
// AL.Source(alSources[i], ALSourceb.Looping, false);
|
||||
// System.Diagnostics.Debug.WriteLine(i + ": " + AL.GetSourceState(alSources[i]));
|
||||
// System.Diagnostics.Debug.WriteLine(AL.GetSourceType(alSources[i]));
|
||||
//}
|
||||
|
||||
|
||||
|
||||
for (int i = 1; i < DefaultSourceCount; i++)
|
||||
{
|
||||
//find a source that's free to use (not playing or paused)
|
||||
if (OpenTK.Audio.OpenAL.AL.GetSourceState(alSources[i]) == OpenTK.Audio.OpenAL.ALSourceState.Playing
|
||||
|| OpenTK.Audio.OpenAL.AL.GetSourceState(alSources[i]) == OpenTK.Audio.OpenAL.ALSourceState.Paused) continue;
|
||||
|
||||
//if (position!=Vector2.Zero)
|
||||
// position /= 1000.0f;
|
||||
soundsPlaying[i] = sound;
|
||||
|
||||
alBuffers[i] = sound.AlBufferId;
|
||||
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);
|
||||
|
||||
//AL.Source(alSources[i], ALSource3f.Position, position.X, position.Y, 0.0f);
|
||||
OpenTK.Audio.OpenAL.AL.SourcePlay(alSources[i]);
|
||||
|
||||
|
||||
|
||||
|
||||
//sound.sourceIndex = i;
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -174,44 +97,17 @@ namespace Barotrauma.Sounds
|
||||
if (sourceIndex<1)
|
||||
{
|
||||
sourceIndex = Play(sound, position, volume, 0.0f, true);
|
||||
//if (sourceIndex>0)
|
||||
//{
|
||||
// AL.Source(alSources[sourceIndex], ALSourceb.Looping, true);
|
||||
// AL.Source(alSources[sourceIndex], ALSourcef.Gain, volume);
|
||||
//}
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateSoundPosition(sourceIndex, position, volume);
|
||||
|
||||
//position /= 1000.0f;
|
||||
|
||||
//OpenTK.Audio.OpenAL.AL.Source(alSources[sourceIndex], OpenTK.Audio.OpenAL.ALSource3f.Position, position.X, position.Y, 0.0f);
|
||||
AL.Source(alSources[sourceIndex], ALSourceb.Looping, true);
|
||||
//AL.Source(alSources[sourceIndex], ALSourcef.Gain, volume);
|
||||
|
||||
}
|
||||
|
||||
ALHelper.Check();
|
||||
return sourceIndex;
|
||||
}
|
||||
|
||||
//public static int Loop(int sourceIndex, float volume = 1.0f)
|
||||
//{
|
||||
|
||||
// if (sourceIndex > 0 && alSources[sourceIndex]>0)
|
||||
// {
|
||||
// ALSourceState state = AL.GetSourceState(alSources[sourceIndex]);
|
||||
// ALHelper.Check();
|
||||
// if (state == ALSourceState.Playing) return sourceIndex;
|
||||
// }
|
||||
|
||||
// int newSourceIndex = Play(sound, volume);
|
||||
// AL.Source(alSources[sourceIndex], ALSourceb.Looping, true);
|
||||
|
||||
// return sourceIndex;
|
||||
//}
|
||||
|
||||
public static void Pause(int sourceIndex)
|
||||
{
|
||||
if (AL.GetSourceState(alSources[sourceIndex]) != ALSourceState.Playing)
|
||||
@@ -239,9 +135,21 @@ namespace Barotrauma.Sounds
|
||||
{
|
||||
AL.SourceStop(alSources[sourceIndex]);
|
||||
AL.Source(alSources[sourceIndex], ALSourceb.Looping, false);
|
||||
|
||||
soundsPlaying[sourceIndex] = null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Sound GetPlayingSound(int sourceIndex)
|
||||
{
|
||||
if (sourceIndex < 1 || sourceIndex>alSources.Count-1) return null;
|
||||
|
||||
if (AL.GetSourceState(alSources[sourceIndex]) != ALSourceState.Playing) return null;
|
||||
|
||||
return soundsPlaying[sourceIndex];
|
||||
}
|
||||
|
||||
|
||||
public static bool IsPlaying(int sourceIndex)
|
||||
{
|
||||
if (sourceIndex < 1 || sourceIndex>alSources.Count-1) return false;
|
||||
@@ -249,14 +157,23 @@ namespace Barotrauma.Sounds
|
||||
return (state == ALSourceState.Playing);
|
||||
}
|
||||
|
||||
public static bool IsLooping(int sourceIndex)
|
||||
{
|
||||
if (sourceIndex < 1 || sourceIndex > alSources.Count - 1) return false;
|
||||
|
||||
bool isLooping;
|
||||
|
||||
OpenTK.Audio.OpenAL.AL.GetSource(alSources[sourceIndex], OpenTK.Audio.OpenAL.ALSourceb.Looping, out isLooping);
|
||||
|
||||
return isLooping;
|
||||
}
|
||||
|
||||
public static void Volume(int sourceIndex, float volume)
|
||||
{
|
||||
AL.Source(alSources[sourceIndex], ALSourcef.Gain, volume * MasterVolume);
|
||||
ALHelper.Check();
|
||||
}
|
||||
|
||||
//int alFilterId;
|
||||
|
||||
static float lowPassHfGain;
|
||||
public static float LowPassHFGain
|
||||
{
|
||||
@@ -281,16 +198,6 @@ namespace Barotrauma.Sounds
|
||||
}
|
||||
}
|
||||
|
||||
//float volume;
|
||||
//public float Volume
|
||||
//{
|
||||
// get { return volume; }
|
||||
// set
|
||||
// {
|
||||
// AL.Source(alSourceId, ALSourcef.Gain, volume = value);
|
||||
// ALHelper.Check();
|
||||
// }
|
||||
//}
|
||||
|
||||
public static void UpdateSoundPosition(int sourceIndex, Vector2 position, float baseVolume = 1.0f)
|
||||
{
|
||||
@@ -301,9 +208,7 @@ namespace Barotrauma.Sounds
|
||||
position = Vector2.Zero;
|
||||
}
|
||||
|
||||
//Resume(sourceIndex);
|
||||
|
||||
position/= 1000.0f;
|
||||
position /= 1000.0f;
|
||||
|
||||
OpenTK.Audio.OpenAL.AL.Source(alSources[sourceIndex], OpenTK.Audio.OpenAL.ALSourcef.Gain, baseVolume * MasterVolume);
|
||||
OpenTK.Audio.OpenAL.AL.Source(alSources[sourceIndex], OpenTK.Audio.OpenAL.ALSource3f.Position, position.X, position.Y, 0.0f);
|
||||
@@ -313,8 +218,6 @@ namespace Barotrauma.Sounds
|
||||
ALHelper.Efx.Filter(lowpassFilterId, OpenTK.Audio.OpenAL.EfxFilterf.LowpassGainHF, lowPassGain);
|
||||
ALHelper.Efx.BindFilterToSource(alSources[sourceIndex], lowpassFilterId);
|
||||
ALHelper.Check();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static OggStream StartStream(string file, float volume = 1.0f)
|
||||
|
||||
Reference in New Issue
Block a user