low pass filter to sounds playing at distance, "armored" limbs, moved playing damagesounds from attack to IDamageable

This commit is contained in:
Regalis
2015-06-01 22:20:17 +03:00
parent 95c0d41023
commit 1f42e4a4db
32 changed files with 331 additions and 175 deletions

View File

@@ -72,58 +72,74 @@ namespace Subsurface
return SoundManager.Play(this, volume);
}
public int Play(float volume, float range, Vector2 position)
public int Play(float baseVolume, float range, Vector2 position)
{
//position = new Vector2(position.X - CameraPos.X, position.Y - CameraPos.Y);
//volume = (range == 0.0f) ? 0.0f : MathHelper.Clamp(volume * (range - position.Length())/range, 0.0f, 1.0f);
int newIndex = SoundManager.Play(this, volume);
Vector2 relativePos = GetRelativePosition(position);
float volume = GetVolume(relativePos, range, baseVolume);
if (newIndex == -1) return -1;
return SoundManager.Play(this, relativePos, volume, volume);
return UpdatePosition(newIndex, position, range, volume);
//if (newIndex == -1) return -1;
//return UpdatePosition(newIndex, position, range, volume);
}
public int Play(float volume, float range, Body body)
{
Vector2 bodyPosition = ConvertUnits.ToDisplayUnits(body.Position);
//Vector2 bodyPosition = ConvertUnits.ToDisplayUnits(body.Position);
//bodyPosition.Y = -bodyPosition.Y;
return Play(volume, range, bodyPosition);
return Play(volume, range, ConvertUnits.ToDisplayUnits(body.Position));
}
public static int UpdatePosition(int sourceIndex, Vector2 position, float range, float baseVolume = 1.0f)
private float GetVolume(Vector2 relativePosition, float range, float baseVolume)
{
position = new Vector2(position.X - CameraPos.X, position.Y - CameraPos.Y);
float volume = (range == 0.0f) ? 0.0f : MathHelper.Clamp(baseVolume * (range - position.Length())/range, 0.0f, 1.0f);
float volume = (range == 0.0f) ? 0.0f : MathHelper.Clamp(baseVolume * (range - relativePosition.Length()) / range, 0.0f, 1.0f);
if (volume <= 0.0f)
{
if (sourceIndex > 0)
{
SoundManager.Stop(sourceIndex);
sourceIndex = -1;
}
return sourceIndex;
}
SoundManager.UpdateSoundPosition(sourceIndex, position, volume);
return sourceIndex;
return volume;
}
public int UpdatePosition(int sourceIndex, Body body, float range, float baseVolume = 1.0f)
private Vector2 GetRelativePosition(Vector2 position)
{
Vector2 bodyPosition = ConvertUnits.ToDisplayUnits(body.Position);
bodyPosition.Y = -bodyPosition.Y;
return UpdatePosition(sourceIndex, bodyPosition, range, baseVolume);
return new Vector2(position.X - CameraPos.X, position.Y - CameraPos.Y);
}
//public static int UpdatePosition(int sourceIndex, Vector2 position, float range, float baseVolume = 1.0f)
//{
// position = new Vector2(position.X - CameraPos.X, position.Y - CameraPos.Y);
// float volume = (range == 0.0f) ? 0.0f : MathHelper.Clamp(baseVolume * (range - position.Length())/range, 0.0f, 1.0f);
// if (volume <= 0.0f)
// {
// if (sourceIndex > 0)
// {
// SoundManager.Stop(sourceIndex);
// sourceIndex = -1;
// }
// return sourceIndex;
// }
// SoundManager.UpdateSoundPosition(sourceIndex, position, volume, volume);
// return sourceIndex;
//}
//public int UpdatePosition(int sourceIndex, Body body, float range, float baseVolume = 1.0f)
//{
// Vector2 bodyPosition = ConvertUnits.ToDisplayUnits(body.Position);
// bodyPosition.Y = -bodyPosition.Y;
// return UpdatePosition(sourceIndex, bodyPosition, range, baseVolume);
//}
public int Loop(int sourceIndex, float volume)
{
if (volume == 0.0f)
if (volume <= 0.0f)
{
if (sourceIndex > 0)
{
@@ -139,8 +155,12 @@ namespace Subsurface
return newIndex;
}
public int Loop(int sourceIndex, float volume, Vector2 position, float range)
public int Loop(int sourceIndex, float baseVolume, Vector2 position, float range)
{
Vector2 relativePos = GetRelativePosition(position);
float volume = GetVolume(relativePos, range, baseVolume);
if (volume <= 0.0f)
{
if (sourceIndex > 0)
@@ -152,9 +172,11 @@ namespace Subsurface
return sourceIndex;
}
int newIndex = SoundManager.Loop(this, sourceIndex, volume);
return UpdatePosition(newIndex, position, range, volume);
return SoundManager.Loop(this, sourceIndex, position, volume, volume);
//return UpdatePosition(newIndex, position, range, volume);
}

View File

@@ -3,6 +3,7 @@ using System.Diagnostics;
using Microsoft.Xna.Framework;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using System;
namespace Subsurface.Sounds
{
@@ -13,8 +14,15 @@ namespace Subsurface.Sounds
private static List<int> alSources = new List<int>();
private static int[] alBuffers = new int[DefaultSourceCount];
private static int lowpassFilterId;
private static float overrideLowPassGain;
public static float OverrideLowPassGain
{
get { return overrideLowPassGain; }
set { overrideLowPassGain = MathHelper.Clamp(overrideLowPassGain, 0.0f, 1.0f); }
}
static AudioContext AC;
public static OggStreamer oggStreamer;
@@ -34,7 +42,7 @@ namespace Subsurface.Sounds
lowpassFilterId = ALHelper.Efx.GenFilter();
//alFilters.Add(alFilterId);
ALHelper.Efx.Filter(lowpassFilterId, EfxFilteri.FilterType, (int)EfxFilterType.Lowpass);
//LowPassHFGain = 1;
}
@@ -72,6 +80,7 @@ namespace Subsurface.Sounds
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]);
@@ -80,6 +89,43 @@ namespace Subsurface.Sounds
// 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)
{
//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)
@@ -89,11 +135,21 @@ namespace Subsurface.Sounds
//if (position!=Vector2.Zero)
// position /= 1000.0f;
alBuffers[i]=sound.AlBufferId;
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);
position /= 1000.0f;
//System.Diagnostics.Debug.WriteLine("updatesoundpos: "+offset);
AL.Source(alSources[i], ALSourcef.Gain, volume);
AL.Source(alSources[i], ALSource3f.Position, position.X, position.Y, 0.0f);
AL.Source(alSources[i], ALSourcei.Buffer, sound.AlBufferId);
ALHelper.Efx.Filter(lowpassFilterId, EfxFilterf.LowpassGainHF, lowPassHfGain = Math.Min(lowPassGain, overrideLowPassGain));
ALHelper.Efx.BindFilterToSource(alSources[i], lowpassFilterId);
ALHelper.Check();
//AL.Source(alSources[i], ALSource3f.Position, position.X, position.Y, 0.0f);
AL.SourcePlay(alSources[i]);
@@ -105,22 +161,28 @@ namespace Subsurface.Sounds
return -1;
}
public static int Loop(Sound sound, int sourceIndex, float volume)
public static int Loop(Sound sound, int sourceIndex, float volume = 1.0f)
{
return Loop(sound,sourceIndex, Vector2.Zero, volume, 0.0f);
}
public static int Loop(Sound sound, int sourceIndex, Vector2 position, float volume = 1.0f, float lowPassGain = 0.0f)
{
if (sourceIndex<1)
{
sourceIndex = Play(sound, volume);
sourceIndex = Play(sound, position, volume, lowPassGain);
if (sourceIndex>0)
{
AL.Source(alSources[sourceIndex], ALSourceb.Looping, true);
AL.Source(alSources[sourceIndex], ALSourcef.Gain, volume);
}
ALHelper.Check();
return sourceIndex;
}
else
{
AL.Source(alSources[sourceIndex], ALSourceb.Looping, true);
AL.Source(alSources[sourceIndex], ALSourcef.Gain, volume);
ALHelper.Check();
return sourceIndex;
}
}
@@ -195,6 +257,7 @@ namespace Subsurface.Sounds
{
if (ALHelper.Efx.IsInitialized)
{
overrideLowPassGain = value;
for (int i = 0; i < DefaultSourceCount; i++)
{
//find a source that's free to use (not playing or paused)
@@ -221,17 +284,21 @@ namespace Subsurface.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, float lowPassGain = 0.0f)
{
if (sourceIndex < 1) return;
//Resume(sourceIndex);
position/= 1000.0f;
//System.Diagnostics.Debug.WriteLine("updatesoundpos: "+offset);
AL.Source(alSources[sourceIndex], ALSourcef.Gain, baseVolume);
AL.Source(alSources[sourceIndex], ALSource3f.Position, position.X, position.Y, 0.0f);
ALHelper.Efx.Filter(lowpassFilterId, EfxFilterf.LowpassGainHF, lowPassHfGain = Math.Min(lowPassGain, overrideLowPassGain));
ALHelper.Efx.BindFilterToSource(alSources[sourceIndex], lowpassFilterId);
ALHelper.Check();
}
public static OggStream StartStream(string file, float volume = 1.0f)