AudioEmitter
AudioEmitter emits audio streams into the world. It provides a single Input pin that can be connected to by one or more Wires. Any streams wired to an AudioEmitter get broadcasted into the world from the emitter's parent's position. If the parent is an Attachment, Camera, or PVInstance, the parent's world-position will be used. If the parent is not one of these classes, the AudioEmitter is effectively silent.
AudioEmitters are heard by AudioListeners in order to implement 3D spatialization.
Code Samples
local part1 : BasePart = workspace.Speakers.Left
local part2 : BasePart = workspace.Speakers.Right
local player : AudioPlayer = workspace.AudioPlayer
local leftEmitter = Instance.new("AudioEmitter")
local rightEmitter = Instance.new("AudioEmitter")
local toLeft = Instance.new("Wire")
local toRight = Instance.new("Wire")
leftEmitter.Parent = part1
rightEmitter.Parent = part2
toLeft.Parent = leftEmitter
toLeft.SourceInstance = player
toLeft.TargetInstance = leftEmitter
toRight.Parent = rightEmitter
toRight.SourceInstance = player
toRight.TargetInstance = rightEmitter
player:Play()
Summary
Properties
Represents how the perceived volume of the emitted sound changes based on the angle between a AudioListener and the LookVector associated with the AudioEmitter.
Controls which AudioListeners are capable of hearing this AudioEmitter.
Represents how the perceived volume of the emitted sound changes as the distance between a AudioListener and the AudioEmitter increases.
Methods
Gets the angle attenuation curve that the AudioEmitter is using, or an empty table if it's using the default curve.
Returns an array of Wires that are connected to the specified pin.
Gets the distance attenuation curve that the AudioEmitter is using, or an empty table if it's using the default curve.
Sets the angle attenuation curve that the AudioEmitter should use, or uses a constant curve of volume 1 if none is provided.
Sets the distance attenuation curve that the AudioEmitter should use, or uses an inverse rolloff curve if none is provided.
Properties
AngleAttenuation
Represents a volume-over-angle curve that affects how loudly a AudioListener will hear the AudioEmitter, based on the angle between them and the LookVector associated with the AudioEmitter.
This property is internal and can't be accessed by scripts; it exists to support replication. See SetAngleAttenuation() for usage details.
AudioInteractionGroup
If an AudioEmitter and an AudioListener share an interaction group, then the listener is capable of hearing the emitter.
DistanceAttenuation
Represents a volume-over-distance curve that affects how loudly a AudioListener will hear the AudioEmitter, based on the distance between them.
This property is internal and can't be accessed by scripts; it exists to support replication. See SetDistanceAttenuation() for usage details.
Methods
GetAngleAttenuation
Returns a table mapping angle to volume. Keys are numbers between 0 and 180 (inclusive), while values are numbers between 0 and 1 (inclusive) describing how volume attenuates depending on direction. This method returns an empty table if the default angle attenuation curve is being used.
Returns
Table mapping angle to volume, as described above.
GetConnectedWires
Returns an array of Wires that are connected to the specified pin. AudioEmitter has one "Input" pin.
Parameters
Returns
GetDistanceAttenuation
Returns a table mapping distance to volume. Keys are numbers greater than or equal to 0, while values are numbers between 0 and 1 (inclusive) describing how volume attenuates over distance. This method returns an empty table if the default distance attenuation curve is being used.
Returns
GetInteractingListeners
Returns
SetAngleAttenuation
Sets a volume-over-angle curve that affects how loudly a AudioListener will hear the AudioEmitter, based on the angle between them and the LookVector associated with the AudioEmitter.
The curve is represented by a table mapping angle keys to volume values. Keys are expected to be unique numbers between 0 and 180 (inclusive), while values are expected to be numbers between 0 and 1 (inclusive). Tables containing up to 400 key-value pairs are supported.
The volume of the AudioEmitter from the perspective of a AudioListener at an angle a is determined by linearly interpolating between the volume levels for the points on the curve whose angle values are directly above and below a. If there is either no point below a or no point above a, the volume level of the other point is chosen. Essentially, the curve is a sequence of points connected by straight lines, and beyond its left and right endpoints the curve extends outward at their respective volume levels.
This volume will be multiplied with the volumes from all other attenuation curves (including the ones on the receiving AudioListener) to obtain the final audibility.
If the table is empty or nil, the AudioEmitter defaults to using an angle attenuation curve with the constant volume value of 1.
Parameters
Returns
SetDistanceAttenuation
Sets a volume-over-distance curve that affects how loudly a AudioListener will hear the AudioEmitter, based on the distance between them.
The curve is represented by a table mapping distance keys to volume values. Keys are expected to be unique numbers greater than or equal to 0, while values are expected to be numbers between 0 and 1 (inclusive). Tables containing up to 400 key-value pairs are supported.
The volume of the AudioEmitter from the perspective of a AudioListener at a distance d is determined by linearly interpolating between the volume levels for the points on the curve whose distance values are directly above and below d. If there is either no point below d or no point above d, the volume level of the other point is chosen. Essentially, the curve is a sequence of points connected by straight lines, and beyond its left and right endpoints the curve extends outward infinitely at their respective volume levels.
This volume will be multiplied with the volumes from all other attenuation curves (including the ones on the receiving AudioListener) to obtain the final audibility.
If the table is empty or nil, the AudioEmitter defaults to using a distance attenuation curve determined by the inverse-square law.
Parameters
Returns
Code Samples
local Players = game:GetService("Players")
local emitterPart = Instance.new("Part")
emitterPart.Anchored = true
emitterPart.Position = Vector3.new(0, 0, 0)
emitterPart.Parent = workspace
local emitter : AudioEmitter = Instance.new("AudioEmitter")
emitter.Parent = emitterPart
local curve = {}
curve[10] = 1 -- Within a distance of 10 studs, listeners hear this emitter at full volume
curve[100] = 0.4 -- At a distance of 100 studs, listeners hear this emitter at 40% volume
curve[300] = 0 -- At any distance farther than 300 studs, listeners cannot hear this emitter
emitter:SetDistanceAttenuation(curve)
-- Replicate the rolloff curve from the old voice implementation
-- Default voice without the new audio API uses quadratic rolloff ranging from 7 to 80 studs
local MIN_DISTANCE = 7
local MAX_DISTANCE = 80
local CURVE_STEP_SIZE = 2
local voiceCurve = {}
for i = MIN_DISTANCE, MAX_DISTANCE, CURVE_STEP_SIZE do
voiceCurve[i] = ((i - MIN_DISTANCE) - (MAX_DISTANCE - MIN_DISTANCE))^2 / (MAX_DISTANCE - MIN_DISTANCE)^2
end
voiceCurve[MAX_DISTANCE] = 0
local function setVoiceCurve(character)
local voiceEmitter : AudioEmitter = character:WaitForChild("AudioEmitter")
voiceEmitter:SetDistanceAttenuation(voiceCurve)
end
for _, player in Players:GetPlayers() do
if player.Character then
setVoiceCurve(player.Character)
end
player.CharacterAdded:Connect(setVoiceCurve)
end