TExcelTAD
Home › Godot 4 with GDScript › Audio & Audio Buses

Audio & Audio Buses

Sound in Godot comes from player nodes that play an AudioStream (an imported .wav, .ogg or .mp3) and send the result into an audio bus, a mixer channel with a volume, mute/solo switches and a chain of effects. Buses are how a game offers separate "Music" and "Effects" volume sliders, ducks the music under dialogue, or adds reverb to everything inside a cave.

The player nodes

Node Positional? Use for
AudioStreamPlayer no, same volume everywhere music, UI clicks, announcer voice
AudioStreamPlayer2D yes, panned and attenuated by distance in 2D footsteps, explosions in a 2D world
AudioStreamPlayer3D yes, 3D position, attenuation, doppler, area reverb anything in a 3D world

Useful properties: stream, volume_db, pitch_scale, autoplay, bus and max_polyphony (how many overlapping plays one player allows, handy for rapid gunfire). Call play(), stop(), and connect finished to know when a one-shot ends.

Import choices matter: WAV for short effects (no decoding cost), Ogg Vorbis for music and long ambience (small files, streamed). For music, enable Loop in the Import dock (re-import after changing it).

Buses: the Audio panel

The bus layout lives in a resource, default_bus_layout.tres, edited in the Audio panel at the bottom of the editor:

  1. Open the Audio tab in the bottom panel. You see the Master bus.
  2. Click Add Bus twice and rename the new buses Music and SFX. Each bus's output (the dropdown at its bottom) goes to Master.
  3. On SFX, click Add Effect and pick Reverb (or Compressor, LowPassFilter, Delay, Chorus, Limiter...).
  4. Select each AudioStreamPlayer and set its Bus property to Music or SFX. The dropdown only lists buses from the current layout.

Signal flow: player → its bus → (effects) → the bus it sends to → ... → Master → speakers. Volumes are in decibels: 0 dB is unchanged, −6 dB is about half the amplitude, and −80 dB is effectively silent.

The same setup from code (verified)

AudioServer is the singleton behind that panel. Its bus API works without a sound card, so this example builds the layout, adds an effect and maps volume sliders to decibels:

gdscript · try it yourself
extends Node

# frames: 0
func _ready():
	print("buses at start: ", AudioServer.bus_count, " (", AudioServer.get_bus_name(0), ")")
	for bus_name in ["Music", "SFX"]:
		AudioServer.add_bus()
		var i := AudioServer.bus_count - 1
		AudioServer.set_bus_name(i, bus_name)
		AudioServer.set_bus_send(i, "Master")
	var sfx := AudioServer.get_bus_index("SFX")
	var reverb := AudioEffectReverb.new()
	reverb.room_size = 0.6
	AudioServer.add_bus_effect(sfx, reverb)
	for i in AudioServer.bus_count:
		print(i, ": ", AudioServer.get_bus_name(i), " -> ", AudioServer.get_bus_send(i), " effects=", AudioServer.get_bus_effect_count(i))
	for slider in [1.0, 0.5, 0.25, 0.0]:
		print("slider ", slider, " -> ", snappedf(linear_to_db(slider), 0.01), " dB")
	set_volume("Music", 0.5)
	set_volume("SFX", 0.0)
	print("Music ", snappedf(AudioServer.get_bus_volume_db(1), 0.01), " dB, SFX muted=", AudioServer.is_bus_mute(2))
	var player := AudioStreamPlayer.new()
	player.bus = &"SFX"
	player.volume_db = -3.0
	var rnd := AudioStreamRandomizer.new()
	rnd.random_pitch = 1.1
	player.stream = rnd
	add_child(player)
	print("player bus ", player.bus, " stream ", player.stream.get_class())
	print("missing bus index: ", AudioServer.get_bus_index("Voice"))

func set_volume(bus_name: String, linear: float) -> void:
	var i := AudioServer.get_bus_index(bus_name)
	AudioServer.set_bus_volume_db(i, linear_to_db(linear))
	AudioServer.set_bus_mute(i, linear <= 0.0)
buses at start: 1 (Master)
0: Master ->  effects=0
1: Music -> Master effects=0
2: SFX -> Master effects=1
slider 1.0 -> 0.0 dB
slider 0.5 -> -6.02 dB
slider 0.25 -> -12.04 dB
slider 0.0 -> -inf dB
Music -6.02 dB, SFX muted=true
player bus SFX stream AudioStreamRandomizer
missing bus index: -1

What to take from it:

  • Sliders are linear, buses are in dB. A slider from 0 to 1 converted with linear_to_db feels natural; a slider straight in dB does not. Recent 4.x versions also offer AudioServer.set_bus_volume_linear() and volume_linear on players, which do the conversion for you.
  • linear_to_db(0.0) is -inf. Mute the bus as well, as set_volume does, so "0" really means silence and is easy to test.
  • Look buses up by name with get_bus_index(): indices change when someone reorders buses in the editor. A missing name returns -1, and using -1 as an index prints errors, so check it if the name comes from data.
  • AudioStreamRandomizer wraps several streams and picks one at random with a random pitch and volume range: the cheap cure for "every footstep sounds the same".

A volume settings menu

Tie three HSlider nodes (range 0–1, step 0.05) to the buses and save the values with ConfigFile (see Saving):

gdscript
extends Control

@onready var music_slider: HSlider = %MusicSlider
@onready var sfx_slider: HSlider = %SfxSlider

func _ready() -> void:
	music_slider.value = db_to_linear(AudioServer.get_bus_volume_db(AudioServer.get_bus_index("Music")))
	music_slider.value_changed.connect(_set_bus.bind("Music"))
	sfx_slider.value_changed.connect(_set_bus.bind("SFX"))

func _set_bus(value: float, bus_name: String) -> void:
	var i := AudioServer.get_bus_index(bus_name)
	AudioServer.set_bus_volume_db(i, linear_to_db(value))
	AudioServer.set_bus_mute(i, value <= 0.0)

bind appends the bus name after the signal's own argument, so one handler serves every slider. %MusicSlider finds a node marked Access as Unique Name anywhere in the scene.

Practical tips

  • One-shot sounds on nodes that die. If an enemy plays its death sound and then calls queue_free(), the sound is cut off. Play it from a player that outlives the enemy (an autoload sound manager, or reparent a temporary player to the level and free it on finished).
  • Pause. Players stop processing when the tree is paused unless their process_mode is PROCESS_MODE_ALWAYS. Give music and menu clicks ALWAYS.
  • Ducking. Put a Compressor on the Music bus with its sidechain set to the Voice bus: music gets quieter while someone speaks.
  • Web exports. Browsers start audio only after the first click or key press; a title screen with "Press any key" solves it (Exporting).
  • Interactive music. Godot 4.3 added AudioStreamInteractive, AudioStreamPlaylist and AudioStreamSynchronized for music that changes with the game state.

Test yourself

A volume slider goes from 0 to 1. How should its value reach the bus?

  1. set_bus_volume_db(i, value)
  2. set_bus_volume_db(i, linear_to_db(value)), and mute the bus at 0
  3. set_bus_volume_db(i, value * 100)
  4. Set pitch_scale instead

Why look buses up with AudioServer.get_bus_index("SFX") instead of hard-coding index 2?

  1. Indices are slower
  2. Bus indices change when buses are added or reordered in the Audio panel; names stay stable
  3. Index 2 is reserved
  4. get_bus_index also creates the bus

An enemy plays its death sound and immediately calls queue_free(). What happens?

  1. The sound finishes normally
  2. The sound is cut off, because its AudioStreamPlayer is freed with the enemy
  3. Godot delays the free until the sound ends
  4. The sound plays twice