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

169 lines
5.0 KiB
C#
Raw 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 Robust.Client.Audio;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface;
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
{
[Dependency] private readonly IAudioManager _audioManager = default!;
[Dependency] private readonly IResourceCache _cache = default!;
[Dependency] private readonly IUserInterfaceManager _uiManager = default!;
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-12-09 15:18:15 +11:00
2023-05-29 10:44:11 +10:00
public override void Initialize()
{
base.Initialize();
UpdatesOutsidePrediction = true;
InitializeAmbientMusic();
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 lobbyStream = EntityManager.System<BackgroundAudioSystem>().LobbyStream;
TryComp(lobbyStream, out AudioComponent? audioComp);
var oldGain = audioComp?.Gain;
SilenceAudio();
if (oldGain != null)
{
Audio.SetGain(lobbyStream, oldGain.Value, audioComp);
}
2023-05-29 10:44:11 +10:00
}
public override void Shutdown()
{
base.Shutdown();
ShutdownAmbientMusic();
}
public override void Update(float frameTime)
{
base.Update(frameTime);
if (!_timing.IsFirstTimePredicted)
return;
UpdateAmbientMusic();
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;
2023-05-29 10:44:11 +10:00
var change = (curVolume - MinVolume) / 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;
component.Volume = MathF.Max(MinVolume, volume);
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;
component.Volume = MathF.Min(target, volume);
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);