SoundService
A service that determines various aspects of how Sounds play in the experience. SoundService is also often used to store SoundGroups, although this is not mandatory for groups to work.
SoundService properties such as AmbientReverb, DistanceFactor, DopplerScale and RolloffScale can be used to change how all Sounds play in the experience, and the SetListener() function allows you to set the position from where sounds are heard.
Code Samples
local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")
local SoundService = game:GetService("SoundService")
local localPlayer = Players.LocalPlayer
local reverbTags = {
["reverb_Cave"] = Enum.ReverbType.Cave,
}
-- collect parts and group them by tag
local parts = {}
for reverbTag, reverbType in pairs(reverbTags) do
for _, part in pairs(CollectionService:GetTagged(reverbTag)) do
parts[part] = reverbType
end
end
-- function to check if a position is within a part's extents
local function positionInPart(part, position)
local extents = part.Size / 2
local offset = part.CFrame:PointToObjectSpace(position)
return offset.x < extents.x and offset.y < extents.y and offset.z < extents.z
end
local reverbType = SoundService.AmbientReverb
while true do
task.wait()
if not localPlayer then
return
end
local character = localPlayer.Character
-- default to no reverb
local newReverbType = Enum.ReverbType.NoReverb
if character and character.PrimaryPart then
local position = character.PrimaryPart.Position
-- go through all the indexed parts
for part, type in pairs(parts) do
-- see if the character is within them
if positionInPart(part, position) then
-- if so, pick that reverb type
newReverbType = type
break
end
end
end
-- set the reverb type if it has changed
if newReverbType ~= reverbType then
SoundService.AmbientReverb = newReverbType
reverbType = newReverbType
end
end
Summary
Properties
The ambient sound environment preset used by SoundService.
The number of studs to be considered a meter by SoundService when calculating volume attenuation of Sounds parented to a BasePart or Attachment.
Degree to which the pitch of a Sound varies due to the Doppler effect.
Sets whether Sound playback from a client will replicate to the server.
Sets how fast Sound volume attenuates.
Methods
Returns the current SoundService listener type and what is set as the listener.
Plays a Sound locally, meaning the sound will only be heard by the client calling this method, regardless of where it's parented to.
Sets the listener for the SoundService.
Properties
AmbientReverb
Each Enum.ReverbType option for this property corresponds to a preset available in the FMOD sound engine. For example, when AmbientReverb is set to Enum.ReverbType.Hangar, sound will reverberate differently to simulate being in a large enclosed space.
Changing AmbientReverb affects the following properties used by Roblox's sound engine.
- Reverberation decay time.
- Initial reflection delay time.
- Late reverberation delay time relative to initial reflection.
- Reference high frequency.
- High-frequency to mid-frequency decay time ratio.
- Value that controls the echo density in the late reverberation decay.
- Value that controls the modal density in the late reverberation decay.
- Reference low frequency.
- Relative room effect level at low frequencies.
- Relative room effect level at high frequencies.
- Early reflections level relative to room effect.
- Room effect level at mid frequencies.
DefaultListenerLocation
DistanceFactor
The number of studs to be considered a meter by SoundService when calculating volume attenuation of Sounds parented to a BasePart or Attachment.
By default, this property is 3.33, meaning that a meter is considered 3.33 studs for the purposes of volume attenuation. The greater the DistanceFactor, the more gradually sound will attenuate.
It's recommended that you only change this property if your experience uses a different scale. For example, when using larger custom characters, you may want to reduce DistanceFactor.
DopplerScale
This property determines the degree to which the pitch of a Sound varies due to the Doppler effect, a phenomenon whereby the pitch of a sound changes as the source and observer of the sound move further away or closer together. Only affects Sounds that are parented to a BasePart or Attachment.
Increasing this value exaggerates the impact of the Doppler effect, whereas decreasing it minimizes it. By default, the value of this property is 1.
RespectFilteringEnabled
This property determines whether Sound playback is replicated from the client to the server, and therefore from the server. In other words, when a LocalScript calls Play() and this property is true, the sound will only play on the respective client. If this property is false, other clients will also hear the sound.
Default is true, meaning filtering is enabled.
RolloffScale
Sets how fast Sound volume attenuates. The higher the RolloffScale, the more rapidly a sound's volume will attenuate as the distance between the listener and the sound increases.
Note that this property only applies to Sounds whose RollOffMode property is set to Inverse (default) or InverseTapered. Linear and LinearSquare modes use a different attenuation model which is not influenced by this property. This property also has no impact on Sounds which are not parented to a BasePart or Attachment.
VolumetricAudio
Methods
GetListener
This method returns the SoundService current listener type and what is set as the listener, meaning the point from which audio in the experience is "heard" by the player. By default, the listener is set to Workspace.CurrentCamera. The listener can be changed using SetListener().
The first result returned is the listener's Enum.ListenerType and the second result is dependent on that type:
Listener Type | Description |
---|---|
Enum.ListenerType.Camera | Does not return a listener object as CurrentCamera is always used. |
Enum.ListenerType.CFrame | Returns the CFrame used in SetListener(). |
Enum.ListenerType.ObjectPosition | Returns the BasePart used in SetListener(). |
Enum.ListenerType.ObjectCFrame | Returns the BasePart used in SetListener(). |
Returns
The current Enum.ListenerType and what the listener has been set to. Listener can be a BasePart, a CFrame, or nil.
OpenAttenuationCurveEditor
Parameters
Returns
OpenDirectionalCurveEditor
Parameters
Returns
PlayLocalSound
Plays a Sound locally, meaning the sound will only be heard by the client calling this method, regardless of where it's parented to. This method is most useful for playing a Sound locally in the Studio client, for instance in a Script for a Plugin.
Parameters
Returns
SetListener
Sets the listener used by the client, meaning the point from which audio in the experience is "heard" by the player. For Sounds parented to a BasePart or Attachment, the listener influences the volume and left/right balance of a playing sound.
By default, the listener is set to Workspace.CurrentCamera, but a range of different types of listeners can be used.
The listener can be retrieved using GetListener().
Parameters
The Enum.ListenerType of the listener.
Dependent on the Enum.ListenerType. Use a BasePart for Enum.ListenerType.ObjectPosition or Enum.ListenerType.ObjectCFrame, a CFrame for Enum.ListenerType.CFrame, or nil for Enum.ListenerType.Camera.