Roblox's New Audio API: A Somewhat Deep Dive

Hey there. I’m Koolaid, and approximately about a few days ago as I am writing this, Roblox released their new audio API out of studio beta. Overall, it’s really neat in it’s own way, and I have learned how to use the new API for my projects. However, for people using the Audio API for the first time, it can be quite tricky to understand, so I wrote this guide to help you learn how to use Roblox’s audio API correctly.

The Basic Audio API Diagram

I have created 3 diagrams to visualize how the audio API works.

This is the basic setup of an AudioPlayer that plays music. Inside the Camera, there is an AudioListener and an AudioDeviceOutput object. This is how sounds are played in the 3D world.

The arrows pointing to each graph represents a Wire object. It comes out of the Source instance (left) to the Target instance (right). Wires send sound data to other audio objects if correctly configured. The following connections using Wires should be valid:

This list is displayed as SourceInstance > TargetInstance

  • AudioListener > AudioDeviceOutput

  • AudioPlayer > AudioEmitter

  • Any sound effect object > Any other sound effect object

  • Any sound effect object > AudioEmitter

  • AudioDeviceInput > AudioEmitter

May be updated soon

Applying Effects

This is a diagram which visualizes how different audio modifiers are applied. For this, we connect an AudioPlayer to the AudioReverb object using a Wire, and then do the same thing but we connect the AudioReverb object to an AudioEmitter using another Wire. This will give our sound a reverb effect.

Utilizing Voice Chat

This last diagram visualizes how you can use Voice Chat with the Audio API. For this, we create an AudioDeviceInput object that is connected to an AudioEmitter object via a Wire. We set the Player property of the AudioDeviceInput to the player we want to retrieve the sound from, and we make sure that they’re not muted by setting the Muted property to false.

Scripting

Camera Listener Script Setup

Now that we got through a visualization on how the Audio API works, let’s set it up! First, we need to add a way for the game to pick up audios and make it so that everyone can hear it. To do this, we add this code which creates an AudioListener and an AudioDeviceOutput object inside of the Workspace.CurrentCamera. We hook both the device output and the listener together with a Wire.

:desktop_computer: This script functions properly when it’s RunContext is set to Client or if it’s a LocalScript.

-- Get the camera currently being used by the Workspace service
local camera = workspace.CurrentCamera

-- We add the necessary audio objects inside the Camera
local listener = Instance.new("AudioListener", camera)
local audioOut = Instance.new("AudioDeviceOutput", listener)

-- We make a new wire inside the listener and connect the two audio objects together
local wire = Instance.new("Wire", listener)

wire.SourceInstance = listener
wire.TargetInstance = audioOut

This is how we make audios play within the world.

Playing Sounds through Scripts

While doing this without code is a way to setup sounds with the Audio API, you can also do something similar via scripts. For this script, we create an AudioPlayer and an AudioEmitter inside of a Part and wire them together.

-- A function that wires a Source sound instance to a target sound instance
function connectDevices(source, target)
	local wire = Instance.new("Wire")
	wire.Parent = source
	wire.SourceInstance = source
	wire.TargetInstance = target
end

-- Make an AudioPlayer that plays Life in an Elevator
local audioPlayer = Instance.new("AudioPlayer", script.Parent)
audioPlayer.AssetId = "rbxassetid://1841647093" -- Change the ID to anything you want (Optional)

-- Make an AudioEmitter that will play the sound
local emitter = Instance.new("AudioEmitter", script.Parent)

-- Connect the devices
connectDevices(audioPlayer, emitter)

-- Play the song
audioPlayer:Play()

When running the game, the game should now play Life in an Elevator, a song from the Roblox toolbox.

Basic Voice Chat Setup

The code below is a basic voice chat setup for if you have VoiceChatService.UseAudioApi set to Enabled and VoiceChatService.LoadDefaultVoice is false. This is also how Roblox sets up voice chat for when the Audio API is enabled.

-- Function that automatically creates a Wire
function connectDevices(src, target)
	local wire = Instance.new("Wire")
	wire.Parent = target
	wire.SourceInstance = src
	wire.TargetInstance = target
end

game.Players.PlayerAdded:Connect(function(player)
	
	-- Create a new AudioDeviceInput
	local micIn = Instance.new("AudioDeviceInput")
	micIn.Parent = player
	micIn.Player = player
	micIn.Muted = false
	
	-- Assign a CharacterAdded event incase the player dies and respawns
	player.CharacterAdded:Connect(function(character)
		
		-- Make a new AudioEmitter with its Parent being the Character
		local emitter = Instance.new("AudioEmitter")
		emitter.Parent = character
		
		-- Connect the devices together
		connectDevices(micIn, emitter)
		
	end)
end)

In this script, when a player joins, we make a new AudioDeviceInput object inside of the player that joined the game, set it’s Player property to player, and automatically set Muted to false for the AudioDeviceInput.

Next, we connect a CharacterAdded event to the player incase the player resets or dies from losing health. This event creates an AudioEmitter inside of the Character model, and connects the AudioDeviceInput object to the AudioEmitter. That way, other players can hear that player speak through their microphone.

Conclusion

And that basically wraps it up! I hope this guide helped you understand how the Audio API works and how to use it. I’ll add more stuff here when I get the time, but for now, feedback on this tutorial is much appreciated!

7 Likes

This tutorial only tells you how you can replicate the default Sound Instances using the new Audio Api, you didn’t say anything about the possibility to connect AudioPlayer right to the AudioDeviceOutput. Or that you can connect AudioListened to AudioEmitter which would make a microphonw effect. Tho i didn’t read the whole post only some parts so my reply might be wrong.