← All postsAdaptive Music in Godot 4: A Complete Tutorial
Read in:EnglishفارسیFrançais

Adaptive Music in Godot 4: A Complete Tutorial

· 2 min readgodottutorialadaptive musicunity alternative

Godot 4's built-in audio bus system is powerful but low-level — it gives you volume, effects, and routing, not a concept of "game states" or crossfades. This tutorial builds that layer from scratch using a score exported from Mowjera, so you end up with a reusable AudioManager autoload that any of your future projects can reuse.

What you'll have at the end

A singleton (autoload) that exposes one function — transition_to(state_name) — and handles crossfading between an arbitrary number of looping music states, sample-accurately, with zero audible seams.


Step 1: Export a score for Godot

From any Mowjera score's detail page, click Get bundle. The ZIP includes a godot/ folder with Mowjera.gd and a stems/ folder with one WAV per track per state:

Mowjera_MyScore/
  godot/
    Mowjera.gd
  stems/
    Explore_melody.wav
    Explore_bass.wav
    Combat_melody.wav
    Combat_bass.wav
    ...

Step 2: Import audio with looping enabled

Godot doesn't loop WAV files by default. For each stem:

  1. Select the file in the FileSystem dock.
  2. Open the Import tab.
  3. Under Loop Mode, set it to Forward.
  4. Click Reimport.

Missing this step is the single most common reason a "looping" track has an audible click every time it repeats.

Step 3: Add the autoload

  1. Copy godot/Mowjera.gd into your project's scripts/ folder.
  2. Go to Project → Project Settings → Autoload.
  3. Add the script, name it AudioManager.

Step 4: Call transitions from gameplay code

func _on_enemy_spotted():
    AudioManager.transition_to("Combat")

func _on_area_cleared():
    AudioManager.transition_to("Explore")

func _on_boss_defeated():
    AudioManager.transition_to("Victory")

The autoload handles fading the previous state's AudioStreamPlayer nodes out and the new state's nodes in, timed to the next bar boundary at your score's BPM — the same crossfade logic Mowjera previews live in the browser editor.


Common Godot-specific gotchas

Testing the crossfade

Play your scene, trigger a state change, and listen on headphones. If you hear a "double" of the melody for a fraction of a second before the old state fades fully out, that's expected — it's the crossfade working. If you hear a hard cut or a click, check that both states' stems share the exact same BPM and loop length (Mowjera guarantees this automatically; hand-edited stems don't).

Browse adaptive scores on the Mowjera marketplace — every one ships with a Godot bundle ready for this exact workflow.

Share

Hear it instead of reading about it

Every score on the marketplace plays live in your browser — and the ✨ AI composer drafts an adaptive score from one sentence.

Browse the marketplace