godot-audio
它能做什么
该技能指导在 Godot 中播放音效和音乐,将其路由到总线并控制音量。适合添加声音、音乐和音频设置的 Godot 开发者。
适用场景
- ✓当你需要在 Godot 项目中播放音效或音乐时。
- ✓当你希望有可供玩家用滑块调节的音乐和音效总线时。
- ✓当游戏事件需要与音乐轨道的节拍对齐时。
技能链接
https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/godot/godot-audio/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我希望我的 Godot 游戏有独立的音乐和音效音量滑块,并且希望敌人的攻击与节拍对齐。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会为每个声音设置一个播放节点,并将其路由到名为 Music 或 SFX 的具名总线上。音量以分贝设置,滑块控制总线音量。对于节拍同步,它会根据音乐的播放位置安排事件。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Godot Audio (4.x)
Play SFX and music, route them through buses, control volume in decibels, and time gameplay to the beat. Targets Godot 4.7.
When to use
- Use when playing sound effects or music, routing audio to buses (Master/Music/SFX), adjusting volume/mute from code, adding bus effects (reverb, compressor), positional 3D audio, or syncing events to music.
When not to use: engine-agnostic audio design (adaptive music structure, mixing
philosophy, ducking patterns) → audio-design; importing/encoding assets outside Godot.
Core workflow
- Pick the player node:
AudioStreamPlayer— non-positional (music, UI, global SFX).AudioStreamPlayer2D/AudioStreamPlayer3D— positional; volume/pan from distance.
- Assign an
AudioStreamtostream(.oggfor music/loops,.wavfor short SFX) andplay(). Setautoplayfor music that starts with the scene. - Route to a bus. Set the player's
busto a named bus (e.g."Music","SFX"). Define buses in the Audio panel (bottom dock); each can have volume, mute, solo, and effects. - Control volume in dB, not linear (audio is logarithmic).
0 dB= unchanged,-80 dB≈ silent. Convert withlinear_to_db/db_to_linear, or use the linear helpers (volume_linearon players,AudioServer.set_bus_volume_linear()), which convert for you. - Drive volume/mute from code with
AudioServerby bus index. - For rhythm, compute precise playback time using output latency compensation.
Patterns
1. One-shot SFX (fire-and-forget)
@onready var sfx: AudioStreamPlayer = $Sfx # stream assigned in the editor
func play_jump() -> void:
sfx.pitch_scale = randf_range(0.95, 1.05) # slight variation avoids fatigue
sfx.play()
# For overlapping copies, raise sfx.max_polyphony (default 1), use an
# AudioStreamPolyphonic stream, or spawn short-lived players and free on `finished`.
2. Set a bus's volume and mute via AudioServer
func set_music_volume(linear_0_to_1: float) -> void:
var bus := AudioServer.get_bus_index("Music")
# Convert a 0..1 slider to decibels; clamp avoids -inf at 0.
AudioServer.set_bus_volume_db(bus, linear_to_db(maxf(linear_0_to_1, 0.0001)))
func toggle_sfx(muted: bool) -> void:
AudioServer.set_bus_mute(AudioServer.get_bus_index("SFX"), muted)
3. Crossfade between two music tracks
@onready var a: AudioStreamPlayer = $MusicA
@onready var b: AudioStreamPlayer = $MusicB
func crossfade_to(stream: AudioStream, secs := 1.5) -> void:
b.stream = stream
b.volume_db = -40.0
b.play()
var tw := create_tween().set_parallel(true)
tw.tween_property(a, "volume_db", -40.0, secs) # fade out current
tw.tween_property(b, "volume_db", 0.0, secs) # fade in next
tw.chain().tween_callback(a.stop)
var tmp := a; a = b; b = tmp # swap roles
4. Beat-accurate timing (compensate for output latency)
@onready var music: AudioStreamPlayer = $Music
func get_playback_time() -> float:
# Add time since the last audio mix, subtract output latency, for sub-frame accuracy.
var t := music.get_playback_position() + AudioServer.get_time_since_last_mix()
return t - AudioServer.get_output_latency()
Pitfalls
- Treating volume as linear.
volume_db/set_bus_volume_dbare decibels. Settingvolume_db = 0.5is nearly full volume, not half. Map sliders withlinear_to_db. linear_to_db(0.0)is-inf. It is silent, but tweening or doing math from-infproduces NaN. Clamp the linear value to a small minimum (e.g.0.0001) before converting, or special-case 0 → mute.set_bus_volume_linear(bus, 0.0)stores-inftoo.- Bus name typos fail quietly.
get_bus_index("Muisc")returns-1; calls then error or no-op. Match the exact bus name from the Audio panel. - Short SFX cut off when the same player is retriggered →
max_polyphonydefaults to 1. Raise it (e.g. 4) soplay()overlaps copies, or use anAudioStreamPolyphonic/ separate players freed onfinished. - Music doesn't loop unless the import/stream loop is enabled (
.oggimport has a Loop option;AudioStreamWAVhasloop_mode). - Syncing to
get_playback_position()alone is jittery — it updates per audio mix, not per frame; addget_time_since_last_mix()and subtractget_output_latency(). - 3D audio inaudible → no `AudioLi
常见问题
音乐应该使用哪种播放器?
该技能指出,非定位的 AudioStreamPlayer 适合音乐、界面和全局效果。
为什么音量用分贝表示?
该技能指出,响度是对数关系,因此分贝控制比线性数值听起来更自然。
它涵盖混音设计吗?
像闪避这样的混音理念由音频设计技能涵盖。
详情
作者的原始描述
Play and mix audio in Godot 4.7: AudioStreamPlayer (2D/3D variants), audio buses with volume/mute and effects, music vs SFX routing, db/linear volume, and precise sync-to-beat playback timing. Use when playing sounds or music in a Godot project, routing AudioStreamPlayer nodes to buses, adjusting bus volume via AudioServer, or syncing gameplay to the beat.
热度
- GitHub 星标
- 6.6k
- 排名
- #13819
- 许可证
- Apache-2.0
- 更新时间
- 2026/10/28
评论
- …