(f0d812055) v0.9.9.0

This commit is contained in:
Joonas Rikkonen
2020-04-23 19:19:37 +03:00
parent b647059b93
commit ac37a3b0e4
391 changed files with 15054 additions and 5420 deletions
@@ -97,6 +97,32 @@ namespace Barotrauma.Sounds
if (position != null)
{
if (float.IsNaN(position.Value.X))
{
throw new Exception("Failed to set source's position: " + debugName + ", position.X is NaN");
}
if (float.IsNaN(position.Value.Y))
{
throw new Exception("Failed to set source's position: " + debugName + ", position.Y is NaN");
}
if (float.IsNaN(position.Value.Z))
{
throw new Exception("Failed to set source's position: " + debugName + ", position.Z is NaN");
}
if (float.IsInfinity(position.Value.X))
{
throw new Exception("Failed to set source's position: " + debugName + ", position.X is Infinity");
}
if (float.IsInfinity(position.Value.Y))
{
throw new Exception("Failed to set source's position: " + debugName + ", position.Y is Infinity");
}
if (float.IsInfinity(position.Value.Z))
{
throw new Exception("Failed to set source's position: " + debugName + ", position.Z is Infinity");
}
uint alSource = Sound.Owner.GetSourceFromIndex(Sound.SourcePoolIndex, ALSourceIndex);
Al.Sourcei(alSource, Al.SourceRelative, Al.False);
int alError = Al.GetError();
@@ -179,7 +205,7 @@ namespace Barotrauma.Sounds
get { return gain; }
set
{
gain = Math.Max(Math.Min(value, 1.0f), 0.0f);
gain = Math.Clamp(value, 0.0f, 1.0f);
if (ALSourceIndex < 0) { return; }
@@ -378,12 +404,12 @@ namespace Barotrauma.Sounds
uint alSource = Sound.Owner.GetSourceFromIndex(Sound.SourcePoolIndex, ALSourceIndex);
if (!Al.IsSource(alSource)) return false;
Al.GetSourcei(alSource, Al.SourceState, out state);
bool playing = state == Al.Playing;
int alError = Al.GetError();
if (alError != Al.NoError)
{
throw new Exception("Failed to determine playing state from source: " + debugName + ", " + Al.GetErrorString(alError));
}
bool playing = state == Al.Playing;
return playing;
}
}
@@ -615,7 +641,7 @@ namespace Barotrauma.Sounds
uint alSource = Sound.Owner.GetSourceFromIndex(Sound.SourcePoolIndex, ALSourceIndex);
int state;
Al.GetSourcei(Sound.Owner.GetSourceFromIndex(Sound.SourcePoolIndex, ALSourceIndex), Al.SourceState, out state);
Al.GetSourcei(alSource, Al.SourceState, out state);
bool playing = state == Al.Playing;
int alError = Al.GetError();
if (alError != Al.NoError)
@@ -630,7 +656,7 @@ namespace Barotrauma.Sounds
{
throw new Exception("Failed to determine processed buffers from streamed source: " + debugName + ", " + Al.GetErrorString(alError));
}
Al.SourceUnqueueBuffers(alSource, unqueuedBufferCount, unqueuedBuffers);
alError = Al.GetError();
if (alError != Al.NoError)
@@ -727,9 +753,20 @@ namespace Barotrauma.Sounds
streamAmplitude = streamBufferAmplitudes[queueStartIndex];
Al.GetSourcei(alSource, Al.SourceState, out state);
alError = Al.GetError();
if (alError != Al.NoError)
{
throw new Exception("Failed to retrieve stream source state: " + debugName + ", " + Al.GetErrorString(alError));
}
if (state != Al.Playing)
{
Al.SourcePlay(alSource);
alError = Al.GetError();
if (alError != Al.NoError)
{
throw new Exception("Failed to start stream playback: " + debugName + ", " + Al.GetErrorString(alError));
}
}
}
@@ -738,6 +775,10 @@ namespace Barotrauma.Sounds
streamAmplitude = 0.0f;
}
}
catch (Exception e)
{
DebugConsole.ThrowError($"An exception was thrown when updating a sound stream ({debugName})", e);
}
finally
{
Monitor.Exit(mutex);
@@ -297,11 +297,11 @@ namespace Barotrauma.Sounds
return newSound;
}
public Sound LoadSound(XElement element, bool stream = false)
public Sound LoadSound(XElement element, bool stream = false, string overrideFilePath = null)
{
if (Disabled) { return null; }
string filePath = element.GetAttributeString("file", "");
string filePath = overrideFilePath ?? element.GetAttributeString("file", "");
if (!File.Exists(filePath))
{
throw new FileNotFoundException("Sound file \"" + filePath + "\" doesn't exist!");
@@ -621,7 +621,10 @@ namespace Barotrauma.Sounds
}
if (streamingThread == null || streamingThread.ThreadState.HasFlag(ThreadState.Stopped) || isStreamThreadDying)
{
streamingThread?.Join();
if (streamingThread != null && !streamingThread.Join(1000))
{
DebugConsole.ThrowError("Sound stream thread join timed out!");
}
areStreamsPlaying = true;
streamingThread = new Thread(UpdateStreaming)
{
@@ -639,10 +642,7 @@ namespace Barotrauma.Sounds
bool killThread = false;
while (!killThread)
{
lock (threadDeathMutex)
{
areStreamsPlaying = false;
}
killThread = true;
for (int i = 0; i < playingChannels.Length; i++)
{
lock (playingChannels[i])
@@ -654,10 +654,7 @@ namespace Barotrauma.Sounds
{
if (playingChannels[i][j].IsPlaying)
{
lock (threadDeathMutex)
{
areStreamsPlaying = true;
}
killThread = false;
playingChannels[i][j].UpdateStream();
}
else
@@ -678,7 +675,7 @@ namespace Barotrauma.Sounds
}
lock (threadDeathMutex)
{
killThread = !areStreamsPlaying;
areStreamsPlaying = !killThread;
}
Thread.Sleep(10); //TODO: use a separate thread for network audio?
}
@@ -73,8 +73,8 @@ namespace Barotrauma
private static float updateMusicTimer;
//ambience
private static List<Sound> waterAmbiences = new List<Sound>();
private static SoundChannel[] waterAmbienceChannels = new SoundChannel[2];
private static Sound waterAmbienceIn, waterAmbienceOut, waterAmbienceMoving;
private static SoundChannel[] waterAmbienceChannels = new SoundChannel[3];
private static float ambientSoundTimer;
private static Vector2 ambientSoundInterval = new Vector2(20.0f, 40.0f); //x = min, y = max
@@ -123,7 +123,7 @@ namespace Barotrauma
string filePathB = b.GetAttributeString("file", "").CleanUpPath();
float baseGainB = b.GetAttributeFloat("volume", 1.0f);
float rangeB = b.GetAttributeFloat("range", 1000.0f);
return a.Name.ToString().ToLowerInvariant() == b.Name.ToString().ToLowerInvariant() &&
return a.Name.ToString().Equals(b.Name.ToString(), StringComparison.OrdinalIgnoreCase) &&
filePathA == filePathB && MathUtils.NearlyEqual(baseGainA, baseGainB) &&
MathUtils.NearlyEqual(rangeA, rangeB);
}
@@ -148,10 +148,10 @@ namespace Barotrauma
}
soundElements.AddRange(mainElement.Elements());
}
SoundCount = 1 + soundElements.Count();
var startUpSoundElement = soundElements.Find(e => e.Name.ToString().ToLowerInvariant() == "startupsound");
var startUpSoundElement = soundElements.Find(e => e.Name.ToString().Equals("startupsound", StringComparison.OrdinalIgnoreCase));
if (startUpSoundElement != null)
{
startUpSound = GameMain.SoundManager.LoadSound(startUpSoundElement, false);
@@ -159,11 +159,13 @@ namespace Barotrauma
}
yield return CoroutineStatus.Running;
List<KeyValuePair<string, Sound>> miscSoundList = new List<KeyValuePair<string, Sound>>();
damageSounds = damageSounds ?? new List<DamageSound>();
musicClips = musicClips ?? new List<BackgroundMusic>();
damageSounds ??= new List<DamageSound>();
musicClips ??= new List<BackgroundMusic>();
bool firstWaterAmbienceLoaded = false;
foreach (XElement soundElement in soundElements)
{
yield return CoroutineStatus.Running;
@@ -182,7 +184,7 @@ namespace Barotrauma
musicClips.AddIfNotNull(newMusicClip);
if (loadedSoundElements != null)
{
if (newMusicClip.Type.ToLowerInvariant() == "menu")
if (newMusicClip.Type.Equals("menu", StringComparison.OrdinalIgnoreCase))
{
targetMusic[0] = newMusicClip;
}
@@ -195,17 +197,57 @@ namespace Barotrauma
FlowSounds.AddIfNotNull(GameMain.SoundManager.LoadSound(soundElement, false));
break;
case "waterambience":
waterAmbiences.AddIfNotNull(GameMain.SoundManager.LoadSound(soundElement, false));
//backwards compatibility (1st waterambience used to be played both inside and outside, 2nd when moving)
if (!firstWaterAmbienceLoaded)
{
waterAmbienceIn?.Dispose();
waterAmbienceOut?.Dispose();
if (File.Exists(soundElement.GetAttributeString("file", "")))
{
waterAmbienceIn = GameMain.SoundManager.LoadSound(soundElement, false);
waterAmbienceOut = GameMain.SoundManager.LoadSound(soundElement, false);
}
else
{
waterAmbienceIn = GameMain.SoundManager.LoadSound(soundElement, false, "Content/Sounds/Water/WaterAmbienceIn.ogg");
waterAmbienceOut = GameMain.SoundManager.LoadSound(soundElement, false, "Content/Sounds/Water/WaterAmbienceOut.ogg");
}
firstWaterAmbienceLoaded = true;
}
else
{
waterAmbienceMoving?.Dispose();
if (File.Exists(soundElement.GetAttributeString("file", "")))
{
waterAmbienceMoving = GameMain.SoundManager.LoadSound(soundElement, false);
}
else
{
waterAmbienceMoving = GameMain.SoundManager.LoadSound(soundElement, false, "Content/Sounds/Water/WaterAmbienceMoving.ogg");
}
}
break;
case "waterambiencein":
waterAmbienceIn?.Dispose();
waterAmbienceIn = GameMain.SoundManager.LoadSound(soundElement, false);
break;
case "waterambienceout":
waterAmbienceOut?.Dispose();
waterAmbienceOut = GameMain.SoundManager.LoadSound(soundElement, false);
break;
case "waterambiencemoving":
waterAmbienceMoving?.Dispose();
waterAmbienceMoving = GameMain.SoundManager.LoadSound(soundElement, false);
break;
case "damagesound":
Sound damageSound = GameMain.SoundManager.LoadSound(soundElement, false);
if (damageSound == null) { continue; }
string damageSoundType = soundElement.GetAttributeString("damagesoundtype", "None");
damageSounds.Add(new DamageSound(
damageSound,
soundElement.GetAttributeVector2("damagerange", Vector2.Zero),
damageSoundType,
damageSound,
soundElement.GetAttributeVector2("damagerange", Vector2.Zero),
damageSoundType,
soundElement.GetAttributeString("requiredtag", "")));
break;
@@ -221,12 +263,12 @@ namespace Barotrauma
catch (FileNotFoundException e)
{
DebugConsole.ThrowError("Error while initializing SoundPlayer.", e);
}
}
}
musicClips.RemoveAll(mc => !soundElements.Any(e => SoundElementsEquivalent(mc.Element, e)));
for (int i=0;i<currentMusic.Length;i++)
for (int i = 0; i < currentMusic.Length; i++)
{
if (currentMusic[i] != null && !musicClips.Any(mc => mc.File == currentMusic[i].Filename))
{
@@ -246,12 +288,6 @@ namespace Barotrauma
});
FlowSounds.RemoveAll(s => s.Disposed);
waterAmbiences.ForEach(s =>
{
if (!soundElements.Any(e => SoundElementsEquivalent(s.XElement, e))) { s.Dispose(); }
});
waterAmbiences.RemoveAll(s => s.Disposed);
damageSounds.ForEach(s =>
{
if (!soundElements.Any(e => SoundElementsEquivalent(s.sound.XElement, e))) { s.sound.Dispose(); }
@@ -273,7 +309,7 @@ namespace Barotrauma
fireVolumeLeft = new float[2];
fireVolumeRight = new float[2];
miscSounds = miscSoundList.ToLookup(kvp => kvp.Key, kvp => kvp.Value);
miscSounds = miscSoundList.ToLookup(kvp => kvp.Key, kvp => kvp.Value);
Initialized = true;
@@ -282,7 +318,7 @@ namespace Barotrauma
yield return CoroutineStatus.Success;
}
public static void Update(float deltaTime)
{
@@ -293,27 +329,27 @@ namespace Barotrauma
if (startUpSound != null && !GameMain.SoundManager.IsPlaying(startUpSound))
{
startUpSound.Dispose();
startUpSound = null;
startUpSound = null;
}
//stop water sounds if no sub is loaded
if (Submarine.MainSub == null || Screen.Selected != GameMain.GameScreen)
if (Submarine.MainSub == null || Screen.Selected != GameMain.GameScreen)
{
for (int i = 0; i < waterAmbienceChannels.Length; i++)
{
if (waterAmbienceChannels[i] == null) continue;
if (waterAmbienceChannels[i] == null) { continue; }
waterAmbienceChannels[i].FadeOutAndDispose();
waterAmbienceChannels[i] = null;
}
for (int i = 0; i < FlowSounds.Count; i++)
{
if (flowSoundChannels[i] == null) continue;
if (flowSoundChannels[i] == null) { continue; }
flowSoundChannels[i].FadeOutAndDispose();
flowSoundChannels[i] = null;
}
for (int i = 0; i < fireSoundChannels.Length; i++)
{
if (fireSoundChannels[i] == null) continue;
if (fireSoundChannels[i] == null) { continue; }
fireSoundChannels[i].FadeOutAndDispose();
fireSoundChannels[i] = null;
}
@@ -333,17 +369,18 @@ namespace Barotrauma
}
}
UpdateWaterAmbience(ambienceVolume);
UpdateWaterAmbience(ambienceVolume, deltaTime);
UpdateWaterFlowSounds(deltaTime);
UpdateRandomAmbience(deltaTime);
UpdateFireSounds(deltaTime);
UpdateFireSounds(deltaTime);
}
private static void UpdateWaterAmbience(float ambienceVolume)
private static void UpdateWaterAmbience(float ambienceVolume, float deltaTime)
{
//how fast the sub is moving, scaled to 0.0 -> 1.0
float movementSoundVolume = 0.0f;
float insideSubFactor = 0.0f;
foreach (Submarine sub in Submarine.Loaded)
{
float movementFactor = (sub.Velocity == Vector2.Zero) ? 0.0f : sub.Velocity.Length() / 10.0f;
@@ -352,7 +389,12 @@ namespace Barotrauma
if (Character.Controlled == null || Character.Controlled.Submarine != sub)
{
float dist = Vector2.Distance(GameMain.GameScreen.Cam.WorldViewCenter, sub.WorldPosition);
movementFactor = movementFactor / Math.Max(dist / 1000.0f, 1.0f);
movementFactor /= Math.Max(dist / 1000.0f, 1.0f);
insideSubFactor = Math.Max(1.0f / Math.Max(dist / 1000.0f, 1.0f), insideSubFactor);
}
else
{
insideSubFactor = 1.0f;
}
movementSoundVolume = Math.Max(movementSoundVolume, movementFactor);
@@ -365,28 +407,38 @@ namespace Barotrauma
}
}
if (waterAmbiences.Count > 1)
for (int i = 0; i < 3; i++)
{
if (waterAmbienceChannels[0] == null || !waterAmbienceChannels[0].IsPlaying)
float volume = 0.0f;
Sound sound = null;
switch (i)
{
waterAmbienceChannels[0] = waterAmbiences[0].Play(ambienceVolume * (1.0f - movementSoundVolume),"waterambience");
//waterAmbiences[0].Loop(waterAmbienceIndexes[0], ambienceVolume * (1.0f - movementSoundVolume));
waterAmbienceChannels[0].Looping = true;
}
else
{
waterAmbienceChannels[0].Gain = ambienceVolume * (1.0f - movementSoundVolume);
case 0:
volume = ambienceVolume * (1.0f - movementSoundVolume) * insideSubFactor;
sound = waterAmbienceIn;
break;
case 1:
volume = ambienceVolume * movementSoundVolume * insideSubFactor;
sound = waterAmbienceMoving;
break;
case 2:
volume = 1.0f - insideSubFactor;
sound = waterAmbienceOut;
break;
}
if (waterAmbienceChannels[1] == null || !waterAmbienceChannels[1].IsPlaying)
if ((waterAmbienceChannels[i] == null || !waterAmbienceChannels[i].IsPlaying) && volume > 0.01f)
{
waterAmbienceChannels[1] = waterAmbiences[1].Play(ambienceVolume * movementSoundVolume, "waterambience");
//waterAmbienceIndexes[1] = waterAmbiences[1].Loop(waterAmbienceIndexes[1], ambienceVolume * movementSoundVolume);
waterAmbienceChannels[1].Looping = true;
waterAmbienceChannels[i] = sound.Play(volume, "waterambience");
waterAmbienceChannels[i].Looping = true;
}
else
else if (waterAmbienceChannels[i] != null)
{
waterAmbienceChannels[1].Gain = ambienceVolume * movementSoundVolume;
waterAmbienceChannels[i].Gain += deltaTime * Math.Sign(volume - waterAmbienceChannels[i].Gain);
if (waterAmbienceChannels[i].Gain < 0.01f)
{
waterAmbienceChannels[i].FadeOutAndDispose();
}
}
}
}
@@ -419,11 +471,11 @@ namespace Barotrauma
//flow at the left side
if (diff.X < 0)
{
targetFlowLeft[flowSoundIndex] = 1.0f - distFallOff;
targetFlowLeft[flowSoundIndex] += 1.0f - distFallOff;
}
else
{
targetFlowRight[flowSoundIndex] = 1.0f - distFallOff;
targetFlowRight[flowSoundIndex] += 1.0f - distFallOff;
}
}
}
@@ -742,22 +794,30 @@ namespace Barotrauma
Screen.Selected == GameMain.LevelEditorScreen ||
Screen.Selected == GameMain.ParticleEditorScreen ||
Screen.Selected == GameMain.SpriteEditorScreen ||
Screen.Selected == GameMain.SubEditorScreen)
Screen.Selected == GameMain.SubEditorScreen ||
(Screen.Selected == GameMain.GameScreen && GameMain.GameSession?.GameMode is SubTestMode))
{
return "editor";
}
if (Screen.Selected != GameMain.GameScreen) { return "menu"; }
if (Character.Controlled != null &&
Level.Loaded != null && Level.Loaded.Ruins != null &&
Level.Loaded.Ruins.Any(r => r.Area.Contains(Character.Controlled.WorldPosition)))
if (Character.Controlled != null)
{
return "ruins";
if (Level.Loaded != null && Level.Loaded.Ruins != null &&
Level.Loaded.Ruins.Any(r => r.Area.Contains(Character.Controlled.WorldPosition)))
{
return "ruins";
}
if (Character.Controlled.Submarine?.Info?.IsWreck ?? false)
{
return "wreck";
}
}
Submarine targetSubmarine = Character.Controlled?.Submarine;
if ((targetSubmarine != null && targetSubmarine.AtDamageDepth) ||
(GameMain.GameScreen != null && Screen.Selected == GameMain.GameScreen && GameMain.GameScreen.Cam.Position.Y < SubmarineBody.DamageDepth))
{
@@ -44,13 +44,15 @@ namespace Barotrauma.Sounds
new BandpassFilter(VoipConfig.FREQUENCY, 2000)
};
private float gain;
public float Gain
{
get { return soundChannel == null ? 0.0f : soundChannel.Gain; }
get { return soundChannel == null ? 0.0f : gain; }
set
{
if (soundChannel == null) { return; }
soundChannel.Gain = value;
gain = value;
soundChannel.Gain = value * GameMain.Config.VoiceChatVolume;
}
}
@@ -59,8 +61,10 @@ namespace Barotrauma.Sounds
get { return soundChannel?.CurrentAmplitude ?? 0.0f; }
}
public VoipSound(SoundManager owner, VoipQueue q) : base(owner, "voip", true, true)
public VoipSound(string name, SoundManager owner, VoipQueue q) : base(owner, "voip", true, true)
{
Filename = $"VoIP ({name})";
VoipConfig.SetupEncoding();
ALFormat = Al.FormatMono16;
@@ -73,6 +77,7 @@ namespace Barotrauma.Sounds
SoundChannel chn = new SoundChannel(this, 1.0f, null, 0.4f, 1.0f, "voip", false);
soundChannel = chn;
Gain = 1.0f;
}
public override float GetAmplitudeAtPlaybackPos(int playbackPos)
@@ -92,26 +97,29 @@ namespace Barotrauma.Sounds
}
public void ApplyFilters(short[] buffer, int readSamples)
{
if (UseMuffleFilter)
{
ApplyFilters(radioFilters, buffer, readSamples);
}
if (UseRadioFilter)
{
ApplyFilters(radioFilters, buffer, readSamples);
}
}
private void ApplyFilters(IEnumerable<BiQuad> filters, short[] buffer, int readSamples)
{
for (int i = 0; i < readSamples; i++)
{
float fVal = ShortToFloat(buffer[i]);
foreach (var filter in filters)
if (gain * GameMain.Config.VoiceChatVolume > 1.0f) //TODO: take distance into account?
{
fVal = filter.Process(fVal);
fVal = Math.Clamp(fVal * gain * GameMain.Config.VoiceChatVolume, -1.0f, 1.0f);
}
if (UseMuffleFilter)
{
foreach (var filter in muffleFilters)
{
fVal = filter.Process(fVal);
}
}
if (UseRadioFilter)
{
foreach (var filter in radioFilters)
{
fVal = filter.Process(fVal);
}
}
buffer[i] = FloatToShort(fVal);
}
@@ -140,13 +148,21 @@ namespace Barotrauma.Sounds
public override int FillStreamBuffer(int samplePos, short[] buffer)
{
queue.RetrieveBuffer(bufferID, out int compressedSize, out byte[] compressedBuffer);
if (compressedSize > 0)
try
{
VoipConfig.Decoder.Decode(compressedBuffer, 0, compressedSize, buffer, 0, VoipConfig.BUFFER_SIZE);
bufferID++;
return VoipConfig.BUFFER_SIZE * 2;
if (compressedSize > 0)
{
VoipConfig.Decoder.Decode(compressedBuffer, 0, compressedSize, buffer, 0, VoipConfig.BUFFER_SIZE);
bufferID++;
return VoipConfig.BUFFER_SIZE * 2;
}
if (bufferID < queue.LatestBufferID - (VoipQueue.BUFFER_COUNT - 1)) bufferID = queue.LatestBufferID - (VoipQueue.BUFFER_COUNT - 1);
}
catch (Exception e)
{
DebugConsole.ThrowError($"Failed to decode Opus buffer (buffer size {compressedBuffer.Length}, packet size {compressedSize})", e);
bufferID = queue.LatestBufferID - (VoipQueue.BUFFER_COUNT - 1);
}
if (bufferID < queue.LatestBufferID - (VoipQueue.BUFFER_COUNT - 1)) bufferID = queue.LatestBufferID - (VoipQueue.BUFFER_COUNT - 1);
return 0;
}