Fix Repeated Effect Voice List Scans

This commit is contained in:
copecone committed 2026-10-13 12:02:48 +09:00
1 parent 5055e4dfbb
commit d0f6fa9ecd
1 file changed
+9 -2
+9 -2
View File
@@ -32,7 +32,10 @@ internal sealed class OfflineAudioMixer : IDisposable
/// <summary>Additional gain of effect sounds (registered with <see cref="RegisterSoundEvent"/>).</summary> /// <summary>Additional gain of effect sounds (registered with <see cref="RegisterSoundEvent"/>).</summary>
private const double EffectVolume = 2.0 / 3.0; private const double EffectVolume = 2.0 / 3.0;
/// <summary>Number of tracked effect voices at which finished ones are removed from the list used by <see cref="StopAllEffects"/>.</summary> /// <summary>
/// Smallest number of tracked effect voices at which finished ones are removed from the list used by <see cref="StopAllEffects"/>.
/// After each removal the next threshold is twice the remaining count (at least this value).
/// </summary>
private const int EffectVoicePruneCount = 1024; private const int EffectVoicePruneCount = 1024;
/// <summary>Longest wait for a commit before checking for cancellation again, in milliseconds.</summary> /// <summary>Longest wait for a commit before checking for cancellation again, in milliseconds.</summary>
@@ -49,6 +52,7 @@ internal sealed class OfflineAudioMixer : IDisposable
// Main thread. // Main thread.
private ConditionalWeakTable<AudioSource, Voice> _sourceVoices = new(); private ConditionalWeakTable<AudioSource, Voice> _sourceVoices = new();
private readonly List<Voice> _effectVoices = new(); private readonly List<Voice> _effectVoices = new();
private int _effectVoicePruneAt = EffectVoicePruneCount;
private AudioClipSource? _bgmSource; private AudioClipSource? _bgmSource;
private string _bgmKey = ""; private string _bgmKey = "";
private Voice? _bgmVoice; private Voice? _bgmVoice;
@@ -193,8 +197,11 @@ internal sealed class OfflineAudioMixer : IDisposable
var voice = _mixer.Play(clipSource, clipName, startTime, volume * ClipVolume * EffectVolume, pitch, loop, now); var voice = _mixer.Play(clipSource, clipName, startTime, volume * ClipVolume * EffectVolume, pitch, loop, now);
if (loop) voice.SetEndTime(endTime, now); if (loop) voice.SetEndTime(endTime, now);
if (_effectVoices.Count >= EffectVoicePruneCount) if (_effectVoices.Count >= _effectVoicePruneAt)
{
_effectVoices.RemoveAll(v => v.IsFinished); _effectVoices.RemoveAll(v => v.IsFinished);
_effectVoicePruneAt = Math.Max(EffectVoicePruneCount, _effectVoices.Count * 2);
}
_effectVoices.Add(voice); _effectVoices.Add(voice);
if (source) MapSource(source!, voice); if (source) MapSource(source!, voice);