Files
OldThink/Content.Client/Audio/ContentAudioSystem.cs

178 lines
5.2 KiB
C#
Raw Permalink Normal View History

2022-08-12 12:53:28 +10:00
using Content.Shared.Audio;
2023-12-11 21:26:55 +11:00
using Content.Shared.GameTicking;
using AudioComponent = Robust.Shared.Audio.Components.AudioComponent;
2022-08-12 12:53:28 +10:00
namespace Content.Client.Audio;
2023-05-29 10:44:11 +10:00
public sealed partial class ContentAudioSystem : SharedContentAudioSystem
2022-08-12 12:53:28 +10:00
{
2023-05-29 10:44:11 +10:00
// Need how much volume to change per tick and just remove it when it drops below "0"
private readonly Dictionary<EntityUid, float> _fadingOut = new();
2022-08-12 12:53:28 +10:00
2023-05-29 10:44:11 +10:00
// Need volume change per tick + target volume.
private readonly Dictionary<EntityUid, (float VolumeChange, float TargetVolume)> _fadingIn = new();
2023-05-29 10:44:11 +10:00
private readonly List<EntityUid> _fadeToRemove = new();
2023-05-29 10:44:11 +10:00
private const float MinVolume = -32f;
private const float DefaultDuration = 2f;
2023-12-09 15:18:15 +11:00
/*
* Gain multipliers for specific audio sliders.
* The float value will get multiplied by this when setting
* i.e. a gain of 0.5f x 3 will equal 1.5f which is supported in OpenAL.
*/
public const float MasterVolumeMultiplier = 3f;
public const float MidiVolumeMultiplier = 0.25f;
public const float AmbienceMultiplier = 3f;
public const float AmbientMusicMultiplier = 3f;
public const float LobbyMultiplier = 3f;
public const float InterfaceMultiplier = 2f;
2023-05-29 10:44:11 +10:00
public override void Initialize()
{
base.Initialize();
2023-05-29 10:44:11 +10:00
UpdatesOutsidePrediction = true;
InitializeAmbientMusic();
InitializeLobbyMusic();
2023-12-11 21:26:55 +11:00
SubscribeNetworkEvent<RoundRestartCleanupEvent>(OnRoundCleanup);
}
private void OnRoundCleanup(RoundRestartCleanupEvent ev)
{
_fadingOut.Clear();
// Preserve lobby music but everything else should get dumped.
var lobbyMusic = _lobbySoundtrackInfo?.MusicStreamEntityUid;
TryComp(lobbyMusic, out AudioComponent? lobbyMusicComp);
var oldMusicGain = lobbyMusicComp?.Gain;
var restartAudio = _lobbyRoundRestartAudioStream;
TryComp(restartAudio, out AudioComponent? restartComp);
var oldAudioGain = restartComp?.Gain;
2023-12-11 21:26:55 +11:00
SilenceAudio();
if (oldMusicGain != null)
{
Audio.SetGain(lobbyMusic, oldMusicGain.Value, lobbyMusicComp);
}
if (oldAudioGain != null)
2023-12-11 21:26:55 +11:00
{
Audio.SetGain(restartAudio, oldAudioGain.Value, restartComp);
2023-12-11 21:26:55 +11:00
}
PlayRestartSound(ev);
2023-05-29 10:44:11 +10:00
}
public override void Shutdown()
{
base.Shutdown();
ShutdownAmbientMusic();
ShutdownLobbyMusic();
2023-05-29 10:44:11 +10:00
}
public override void Update(float frameTime)
{
base.Update(frameTime);
if (!_timing.IsFirstTimePredicted)
return;
UpdateAmbientMusic();
UpdateLobbyMusic();
2023-05-29 10:44:11 +10:00
UpdateFades(frameTime);
}
#region Fades
public void FadeOut(EntityUid? stream, AudioComponent? component = null, float duration = DefaultDuration)
2023-05-29 10:44:11 +10:00
{
if (stream == null || duration <= 0f || !Resolve(stream.Value, ref component))
2023-05-29 10:44:11 +10:00
return;
// Just in case
// TODO: Maybe handle the removals by making it seamless?
_fadingIn.Remove(stream.Value);
var diff = component.Volume - MinVolume;
_fadingOut.Add(stream.Value, diff / duration);
2023-05-29 10:44:11 +10:00
}
public void FadeIn(EntityUid? stream, AudioComponent? component = null, float duration = DefaultDuration)
2023-05-29 10:44:11 +10:00
{
if (stream == null || duration <= 0f || !Resolve(stream.Value, ref component) || component.Volume < MinVolume)
2023-05-29 10:44:11 +10:00
return;
_fadingOut.Remove(stream.Value);
var curVolume = component.Volume;
var change = (MinVolume - curVolume) / duration;
_fadingIn.Add(stream.Value, (change, component.Volume));
component.Volume = MinVolume;
2023-05-29 10:44:11 +10:00
}
private void UpdateFades(float frameTime)
{
_fadeToRemove.Clear();
foreach (var (stream, change) in _fadingOut)
{
if (!TryComp(stream, out AudioComponent? component))
2023-05-29 10:44:11 +10:00
{
_fadeToRemove.Add(stream);
continue;
}
var volume = component.Volume - change * frameTime;
volume = MathF.Max(MinVolume, volume);
_audio.SetVolume(stream, volume, component);
2023-05-29 10:44:11 +10:00
if (component.Volume.Equals(MinVolume))
2023-05-29 10:44:11 +10:00
{
_audio.Stop(stream);
2023-05-29 10:44:11 +10:00
_fadeToRemove.Add(stream);
}
}
foreach (var stream in _fadeToRemove)
{
_fadingOut.Remove(stream);
}
_fadeToRemove.Clear();
foreach (var (stream, (change, target)) in _fadingIn)
{
// Cancelled elsewhere
if (!TryComp(stream, out AudioComponent? component))
2023-05-29 10:44:11 +10:00
{
_fadeToRemove.Add(stream);
continue;
}
var volume = component.Volume - change * frameTime;
volume = MathF.Min(target, volume);
_audio.SetVolume(stream, volume, component);
2023-05-29 10:44:11 +10:00
if (component.Volume.Equals(target))
2023-05-29 10:44:11 +10:00
{
_fadeToRemove.Add(stream);
}
}
foreach (var stream in _fadeToRemove)
{
_fadingIn.Remove(stream);
}
}
#endregion
2022-08-12 12:53:28 +10:00
}
2023-06-27 21:28:51 +10:00
/// <summary>
/// Raised whenever ambient music tries to play.
/// </summary>
[ByRefEvent]
public record struct PlayAmbientMusicEvent(bool Cancelled = false);