Here are two common “sound-detection” setups in Godot:
Microphone / audio-input detection (e.g. a rhythm game, voice-control feature)
In-game “AI hearing” (e.g. NPCs notice when the player makes noise)
Pick the one you need, or mix & match!
1. Microphone / Audio-Input Detection
A. Setup an “input” bus
Open Project → Project Settings → Audio → Buses.
Create a new bus called MicBus (above “Master” or wherever you like).
On MicBus, click Add Effect → AudioEffectRecord.
In the record effect’s properties, set target bus to Master (so the recording is audible, if you want).
B. GDScript: read the PCM buffer & get its peak
gdscript
extends Node
# How loud must it be to “detect”?
const THRESHOLD := 0.2 # normalized 0.0–1.0
var recorder: AudioEffectRecord
var bus_idx: int
func _ready():
# 1) Find your bus index
bus_idx = AudioServer.get_bus_index("MicBus")
# 2) Grab the AudioEffectRecord
for i in range(AudioServer.get_bus_effect_count(bus_idx)):
var fx = AudioServer.get_bus_effect(bus_idx, i)
if fx is AudioEffectRecord:
recorder = fx
recorder.recording = true
break
func _process(_dt):
if not recorder:
return
# This returns raw PCM bytes (little-endian 16-bit)
var raw_data: PoolByteArray = recorder.get_recording()
var max_amp := 0
# Walk through every 2 bytes → one sample
for i in range(0, raw_data.size(), 2):
var lo = raw_data[i]
var hi = raw_data[i + 1] << 8
var sample = lo | hi
# Convert to signed
if sample & 0x8000:
sample -= 0x10000
max_amp = max(max_amp, abs(sample))
var level = float(max_amp) / 32767.0
if level > THRESHOLD:
print("🎙️ Sound detected! Level:", level)
Tip (Godot 4.x): you can also simply call AudioServer.get_bus_peak_db(bus_idx)
and compare the dB value against a threshold like -30 dB.
2. In-Game “AI Hearing”
Here you don’t read the mic—you’re simulating how NPCs hear sounds generated by your game’s events (footsteps, gunshots, etc.).
func _should_hear_with_occlusion(source_pos, listener_pos, base_volume, threshold) -> bool:
var space = get_world().direct_space_state
var result = space.intersect_ray(
source_pos,
listener_pos,
[], # exclude nothing
collision_mask = 1 # your world geometry layer
)
# If something is in the way, dampen the sound
var occlusion_factor = result.empty() ? 1.0 : 0.5
var dist = source_pos.distance_to(listener_pos)
var level = base_volume / max(dist * dist, 0.0001) * occlusion_factor
return level >= threshold
C. Triggering on sound play
gdscript
func play_sound_and_alert(source: Node3D, audio_player: AudioStreamPlayer3D, base_vol := 1.0):
audio_player.volume_db = linear2db(base_vol)
audio_player.play()
# go through all your NPCs
for npc in get_tree().get_nodes_in_group("enemies"):
if _should_hear_with_occlusion(
source.global_transform.origin,
npc.global_transform.origin,
base_vol,
threshold = 0.05
):
npc.on_sound_heard(source.global_transform.origin)
👀 Next steps / tuning:
Adjust threshold so only “loud” sounds get picked up.
Use multiple rays for more realistic occlusion (e.g. wide‐angle checks).
Bake in frequency filtering (e.g. footsteps vs. shouting) by tagging sounds and giving NPCs different sensitivities.
Which approach are you leaning toward? Let me know if you need more details on any part!