Class ClientAudioSettings
The client's audio volumes: one level per AudioChannel, persisted to configuration, applied at boot, and readable by anything that plays a sound.
public static class ClientAudioSettings
- Inheritance
-
ClientAudioSettings
- Inherited Members
Remarks
Only Master is offered today. See PlayableChannels: the other five levels are stored and applied correctly but nothing in the client plays through them yet, so the options panel does not show sliders that cannot be heard.
Why this is not an AudioMixer. A mixer would be the natural home for per-channel levels, but it has to exist as an asset with a matching exposed parameter per group, and a mixer parameter set before the mixer has been loaded is silently dropped. This keeps the levels as plain data that any caller can read synchronously and that survives having no audio asset loaded at all — which is the state the client boots in. Master is the one level with somewhere to go on its own: UnityEngine.AudioListener.volume scales everything the scene plays, whether or not the caller knew about channels.
Levels are stored, and applied, as a perceptual curve. Loudness is not linear in amplitude: a slider at half travel that halves the amplitude sounds far quieter than half. ToAmplitude(float) squares the slider value, which is the cheap approximation everything from game options to mixing desks uses, so the middle of the slider lands near the middle of the perceived range. The stored value is always the slider position, so the curve can be changed later without invalidating anybody's settings.
Muting when unfocused is a volume decision, not a pause: the client keeps simulating, and a player who alt-tabs to a browser should not have to hear it. It is applied on top of Master rather than by writing zero into it, so the saved level is not destroyed by switching windows.
Fields
ChannelLabels
Player-facing label for each channel, indexed by AudioChannel.
public static readonly string[] ChannelLabels
Field Value
- string[]
PlayableChannels
The channels the client currently routes audio through, and therefore the only ones the options panel offers.
public static readonly AudioChannel[] PlayableChannels
Field Value
Remarks
Master alone, because Master is the only one that reaches anything. It is
applied to UnityEngine.AudioListener.volume, which scales every sound the scene plays
whether or not the caller knew about channels. The other five have no consumer: nothing
in the client owns an AudioSource yet, so a Music or Effects slider would save its
value perfectly and change nothing a player could hear. A control that does nothing is
worse than a missing one — it teaches the player that the settings screen lies.
The rest of this class deliberately stays whole. Every channel keeps its key, its default, its stored level and its change event, so wiring up the audio system later is adding entries to this one array rather than rebuilding the model — and a level saved by a build that offered more channels is still read back correctly by one that offers fewer.
Properties
MuteWhenUnfocused
Mute the client while its window is not focused.
public static bool MuteWhenUnfocused { get; set; }
Property Value
Methods
ApplySaved()
Reads every channel from configuration and applies the result.
public static void ApplySaved()
DefaultVolume(AudioChannel)
The default level for a channel.
public static float DefaultVolume(AudioChannel channel)
Parameters
channelAudioChannel
Returns
EffectiveVolume(AudioChannel)
The amplitude a sound on this channel should actually be played at.
public static float EffectiveVolume(AudioChannel channel)
Parameters
channelAudioChannelThe channel the sound belongs to.
Returns
- float
A multiplier in the range 0..1, ready to assign to
AudioSource.volume.
Remarks
Master is not folded in here. It is already applied to the UnityEngine.AudioListener, and applying it twice would square it — a master at 0.5 would play everything at a quarter. Callers therefore pass their own channel and get only that channel's contribution.
GetVolume(AudioChannel)
The slider position saved for a channel, in the range 0..1.
public static float GetVolume(AudioChannel channel)
Parameters
channelAudioChannel
Returns
KeyFor(AudioChannel)
The configuration key a channel's level is stored under.
public static string KeyFor(AudioChannel channel)
Parameters
channelAudioChannel
Returns
LabelFor(AudioChannel)
The player-facing label for a channel.
public static string LabelFor(AudioChannel channel)
Parameters
channelAudioChannel
Returns
ResetToDefaults()
Restores the offered channels to their defaults and persists the result.
public static void ResetToDefaults()
Remarks
Scoped to PlayableChannels, which is what the button the player pressed actually offers. Resetting the whole enum would write a key for each of the five channels the client cannot yet play through — settings the player was never shown, appearing in Configuration.cfg because they pressed reset on the one they were. A channel with no stored key already resolves to DefaultVolume(AudioChannel), so there is nothing to put back for the ones left out.
SetVolume(AudioChannel, float)
Sets a channel's level, persists it, and applies it.
public static void SetVolume(AudioChannel channel, float value)
Parameters
channelAudioChannelThe channel to change.
valuefloatSlider position in the range 0..1. Clamped.
ToAmplitude(float)
Converts a slider position into an amplitude multiplier.
public static float ToAmplitude(float sliderValue)
Parameters
sliderValuefloat
Returns
Remarks
Squared, so the slider's travel maps roughly onto perceived loudness rather than onto raw amplitude. Zero stays exactly zero, which matters — a channel dragged to the bottom must be silent, not merely quiet.
Events
OnVolumeChanged
Raised whenever any channel's level changes.
public static event Action<AudioChannel> OnVolumeChanged
Event Type
Remarks
Playback that has already started cannot be rescaled by reading a number later, so a looping source — music, an ambient bed — has to be told. One-shots do not need to subscribe; they read the level as they are fired.