Unstable 1.2.4.0

This commit is contained in:
Markus Isberg
2023-11-30 13:53:00 +02:00
parent 8a2e2ea0ae
commit fb5ea537bf
210 changed files with 4201 additions and 1283 deletions
@@ -1,15 +1,16 @@
using System;
using NVorbis;
using OpenAL;
using NVorbis;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Barotrauma.Sounds
{
sealed class OggSound : Sound
{
private VorbisReader streamReader;
private readonly VorbisReader streamReader;
public long MaxStreamSamplePos => streamReader == null ? 0 : streamReader.TotalSamples * streamReader.Channels * 2;
private List<float> playbackAmplitude;
private const int AMPLITUDE_SAMPLE_COUNT = 4410; //100ms in a 44100hz file
@@ -101,7 +102,7 @@ namespace Barotrauma.Sounds
if (!Stream) { throw new Exception("Called FillStreamBuffer on a non-streamed sound!"); }
if (streamReader == null) { throw new Exception("Called FillStreamBuffer when the reader is null!"); }
if (samplePos >= streamReader.TotalSamples * streamReader.Channels * 2) return 0;
if (samplePos >= MaxStreamSamplePos) { return 0; }
samplePos /= streamReader.Channels * 2;
streamReader.DecodedPosition = samplePos;
@@ -444,6 +444,18 @@ namespace Barotrauma.Sounds
}
}
public long MaxStreamSeekPos
{
get
{
if (!IsStream || Sound is not OggSound oggSound)
{
return 0;
}
return oggSound.MaxStreamSamplePos;
}
}
private readonly object mutex;
public bool IsPlaying
@@ -564,7 +576,7 @@ namespace Barotrauma.Sounds
throw new Exception("Generated streamBuffer[" + i.ToString() + "] is invalid! " + debugName);
}
}
Sound.Owner.InitStreamThread();
Sound.Owner.InitUpdateChannelThread();
SetProperties();
}
}
@@ -609,6 +621,7 @@ namespace Barotrauma.Sounds
public void FadeOutAndDispose()
{
FadingOutAndDisposing = true;
Sound.Owner.InitUpdateChannelThread();
}
public void Dispose()
@@ -39,7 +39,7 @@ namespace Barotrauma.Sounds
public bool Disconnected { get; private set; }
private Thread streamingThread;
private Thread updateChannelsThread;
private Vector3 listenerPosition;
public Vector3 ListenerPosition
@@ -201,7 +201,7 @@ namespace Barotrauma.Sounds
public SoundManager()
{
loadedSounds = new List<Sound>();
streamingThread = null;
updateChannelsThread = null;
sourcePools = new SoundSourcePool[2];
playingChannels[(int)SourcePoolIndex.Default] = new SoundChannel[SOURCE_COUNT];
@@ -696,7 +696,7 @@ namespace Barotrauma.Sounds
CompressionDynamicRangeGain = 1.0f;
}
if (streamingThread == null || streamingThread.ThreadState.HasFlag(ThreadState.Stopped))
if (updateChannelsThread == null || updateChannelsThread.ThreadState.HasFlag(ThreadState.Stopped))
{
bool startedStreamThread = false;
for (int i = 0; i < playingChannels.Length; i++)
@@ -708,7 +708,7 @@ namespace Barotrauma.Sounds
if (playingChannels[i][j] == null) { continue; }
if (playingChannels[i][j].IsStream && playingChannels[i][j].IsPlaying)
{
InitStreamThread();
InitUpdateChannelThread();
startedStreamThread = true;
}
if (startedStreamThread) { break; }
@@ -727,37 +727,43 @@ namespace Barotrauma.Sounds
SetCategoryGainMultiplier("music", GameSettings.CurrentConfig.Audio.MusicVolume, 0);
SetCategoryGainMultiplier("voip", Math.Min(GameSettings.CurrentConfig.Audio.VoiceChatVolume, 1.0f), 0);
}
public void InitStreamThread()
/// <summary>
/// Initializes the thread that handles streaming audio and fading out and disposing channels that are no longer needed.
/// </summary>
public void InitUpdateChannelThread()
{
if (Disabled) { return; }
bool isStreamThreadDying;
bool isUpdateChannelsThreadDying;
lock (threadDeathMutex)
{
isStreamThreadDying = !areStreamsPlaying;
isUpdateChannelsThreadDying = !needsUpdateChannels;
}
if (streamingThread == null || streamingThread.ThreadState.HasFlag(ThreadState.Stopped) || isStreamThreadDying)
if (updateChannelsThread == null || updateChannelsThread.ThreadState.HasFlag(ThreadState.Stopped) || isUpdateChannelsThreadDying)
{
if (streamingThread != null && !streamingThread.Join(1000))
if (updateChannelsThread != null && !updateChannelsThread.Join(1000))
{
DebugConsole.ThrowError("Sound stream thread join timed out!");
DebugConsole.ThrowError("SoundManager.UpdateChannels thread join timed out!");
}
areStreamsPlaying = true;
streamingThread = new Thread(UpdateStreaming)
needsUpdateChannels = true;
updateChannelsThread = new Thread(UpdateChannels)
{
Name = "SoundManager Streaming Thread",
Name = "SoundManager.UpdateChannels Thread",
IsBackground = true //this should kill the thread if the game crashes
};
streamingThread.Start();
updateChannelsThread.Start();
}
}
bool areStreamsPlaying = false;
ManualResetEvent streamMre = null;
private bool needsUpdateChannels = false;
private ManualResetEvent updateChannelsMre = null;
void UpdateStreaming()
/// <summary>
/// Handles streaming audio and fading out and disposing channels that are no longer needed.
/// </summary>
private void UpdateChannels()
{
streamMre = new ManualResetEvent(false);
updateChannelsMre = new ManualResetEvent(false);
bool killThread = false;
while (!killThread)
{
@@ -784,6 +790,7 @@ namespace Barotrauma.Sounds
}
else if (playingChannels[i][j].FadingOutAndDisposing)
{
killThread = false;
playingChannels[i][j].Gain -= 0.1f;
if (playingChannels[i][j].Gain <= 0.0f)
{
@@ -794,18 +801,18 @@ namespace Barotrauma.Sounds
}
}
}
streamMre.WaitOne(10);
streamMre.Reset();
updateChannelsMre.WaitOne(10);
updateChannelsMre.Reset();
lock (threadDeathMutex)
{
areStreamsPlaying = !killThread;
needsUpdateChannels = !killThread;
}
}
}
public void ForceStreamUpdate()
{
streamMre?.Set();
updateChannelsMre?.Set();
}
private void ReloadSounds()
@@ -824,12 +831,12 @@ namespace Barotrauma.Sounds
{
for (int j = 0; j < playingChannels[i].Length; j++)
{
if (playingChannels[i][j] != null) playingChannels[i][j].Dispose();
playingChannels[i][j]?.Dispose();
}
}
}
streamingThread?.Join();
updateChannelsThread?.Join();
for (int i = loadedSounds.Count - 1; i >= 0; i--)
{
if (keepSounds)
@@ -709,6 +709,11 @@ namespace Barotrauma
{
musicChannel[i].StreamSeekPos = targetMusic[i].PreviousTime;
}
else if (targetMusic[i].StartFromRandomTime)
{
musicChannel[i].StreamSeekPos =
(int)(musicChannel[i].MaxStreamSeekPos * Rand.Range(0.0f, 1.0f, Rand.RandSync.Unsynced));
}
musicChannel[i].Looping = true;
}
}
@@ -241,6 +241,7 @@ namespace Barotrauma
public readonly bool MuteIntensityTracks;
public readonly float? ForceIntensityTrack;
public readonly bool StartFromRandomTime;
public readonly bool ContinueFromPreviousTime;
public int PreviousTime;
@@ -255,6 +256,7 @@ namespace Barotrauma
ForceIntensityTrack = element.GetAttributeFloat(nameof(ForceIntensityTrack), 0.0f);
}
Volume = element.GetAttributeFloat(nameof(Volume), 1.0f);
StartFromRandomTime = element.GetAttributeBool(nameof(StartFromRandomTime), false);
ContinueFromPreviousTime = element.GetAttributeBool(nameof(ContinueFromPreviousTime), false);
}
}