Skip to content

Commit

Permalink
Fix global audio
Browse files Browse the repository at this point in the history
Audio state handling is still a hot mess though at least this makes it less buggy.
  • Loading branch information
metalgearsloth committed Nov 28, 2023
1 parent a09a60e commit b61e16b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
10 changes: 5 additions & 5 deletions Robust.Client/Audio/AudioSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ private void OnAudioStartup(EntityUid uid, AudioComponent component, ComponentSt
if (!TryGetAudio(component.FileName, out var audioResource))
{
Log.Error($"Error creating audio source for {audioResource}, can't find file {component.FileName}");
component.Source = new DummyAudioSource();
return;
}

Expand All @@ -150,20 +149,21 @@ private void OnAudioStartup(EntityUid uid, AudioComponent component, ComponentSt
{
Log.Error($"Error creating audio source for {audioResource}");
DebugTools.Assert(false);
source = new DummyAudioSource();
source = component.Source;
}

// Need to set all initial data for first frame.
component.Source = source;
ApplyAudioParams(component.Params, component);
component.Global = component.Global;
source.Global = component.Global;

component.Source = source;
// Don't play until first frame so occlusion etc. are correct.
component.Gain = 0f;

// If audio came into range then start playback at the correct position.
var offset = (Timing.CurTime - component.AudioStart).TotalSeconds % GetAudioLength(component.FileName).TotalSeconds;

if (offset != 0)
if (offset > 0)
{
component.PlaybackPosition = (float) offset;
}
Expand Down
1 change: 1 addition & 0 deletions Robust.Server/Audio/AudioSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public override (EntityUid Entity, AudioComponent Component)? PlayGlobal(string
var entity = Spawn("Audio", MapCoordinates.Nullspace);
var audio = SetupAudio(entity, filename, audioParams);
AddAudioFilter(entity, audio, playerFilter);
audio.Global = true;

return (entity, audio);
}
Expand Down
5 changes: 3 additions & 2 deletions Robust.Shared/Audio/Components/AudioComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Robust.Shared.Audio.Components;
/// <summary>
/// Stores the audio data for an audio entity.
/// </summary>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState, Access(typeof(SharedAudioSystem))]
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true), Access(typeof(SharedAudioSystem))]
public sealed partial class AudioComponent : Component, IAudioSource
{
#region Filter
Expand Down Expand Up @@ -68,7 +68,7 @@ public sealed partial class AudioComponent : Component, IAudioSource
/// Audio source that interacts with OpenAL.
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
internal IAudioSource Source = default!;
internal IAudioSource Source = new DummyAudioSource();

/// <summary>
/// Auxiliary entity to pass audio to.
Expand Down Expand Up @@ -115,6 +115,7 @@ public bool Looping
/// <see cref="IAudioSource.Global"/>
/// </summary>
[AutoNetworkedField]
[Access(typeof(SharedAudioSystem))]
public bool Global { get; set; }

/// <summary>
Expand Down

0 comments on commit b61e16b

Please sign in to comment.