Godot and FMOD can be a dream team for indie game projects. Unfortunately, there is not currently an official FMOD integration for the engine. There is a GDExtension project (https://github.com/utopia-rise/fmod-gdextension) but it is not accessible out of the box from C# scripts. Mega Crit solved this by writing GDScript wrappers around the fmod-gdextension functions and calling the GDScript functions from their C# scripts.
C# script -> GDScript wrapper -> GDExtension -> FMOD Studio API.
Luckily with .NET Godot we can simplify this to C# script -> FMOD API allowing for a more direct integration, because I'd rather debug my own convoluted audio integration code than someone else's. Luckily for us the basic setup is not too difficult to pull off.
Step 1: Download FMOD Engine
You can download the studio and core API from https://www.fmod.com/download#fmodengine. For the purpose of this tutorial click on "FMOD Engine". Be sure to match your API version with whatever version of FMOD Studio you are using in order to avoid errors.
You're going to need to copy the following .cs files all found under \api\core\inc\
fmod_dsp.cs
fmod_errors.cs
fmod.cs
fmod_studio.cs
You'll also need the following dll files
\api\core\lib\x64\fmod.dll
\api\studio\lib\x64\fmodstudio.dll
Assuming you're compiling for a 64-bit system. If you're compiling for 32-bit in current year you're probably smarter than I am and can figure it out on your own.
I personally copy these all into a location such as \FMOD\Studio API\ under the project root folder.
Step 2: Add the libraries to your C# project
In the root folder of your Godot project you'll find a {project name}.csproj file. I use this setup copied from Ben Driscoll's post on setting up FMOD for Monogame that copies the .dll files to your build folder and links the libraries.
<ItemGroup>
<None Include="FMOD\Studio API\fmod.dll">
<Link>%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="FMOD\Studio API\fmodstudio.dll">
<Link>%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
If you set up your file structure differently, simply replace "FMOD\Studio API\" with your path to the .dll and .cs files.
You will also want to manually place fmod.dll and fmodstudio.dll into your project root folder. This is so they are accessible when the game is run from the Godot editor rather than a built executable. If you are getting errors while attempting to run from the editor this is probably the issue.
Step 3: Setup and FMOD system singleton
You should now be able to load banks, play events, set variables, etc with the FMOD API directly in your C# scripts. The simplest way to make this accessible throughout your project is to create a singleton for the FMOD system.
FSystem.cs:
using Godot;
using System;
using FMOD.Studio;
using System.Diagnostics;
// Fmod studio system singleton. Provides system initialization, bank loading, and limited utilities.
// System is updated in _Process
public partial class FSystem : Node
{
public static FMOD.Studio.System system;
[Export]
public int ChannelCount = 128;
[Export]
public string MasterBankPath = "Master.bank";
[Export]
public string MasterStringsPath = "Master.strings.bank";
public int ListenerCount = 1;
private FMOD.Studio.System initFmod()
{
FMOD.Studio.System.create(out var fmodStudioSystem);
fmodStudioSystem.getCoreSystem(out var fmodSystem);
fmodSystem.setDSPBufferSize(256, 4);
fmodStudioSystem.initialize(ChannelCount, FMOD.Studio.INITFLAGS.NORMAL, FMOD.INITFLAGS.NORMAL, (IntPtr)0);
fmodStudioSystem.setNumListeners(ListenerCount);
return fmodStudioSystem;
}
public static FMOD.Studio.Bank LoadBankFile(string bankPath)
{
FSystem.system.loadBankFile(
bankPath,
FMOD.Studio.LOAD_BANK_FLAGS.NORMAL,
out FMOD.Studio.Bank bank
);
return bank;
}
public static FMOD.VECTOR ToFVector(Vector2 vector)
{
return new FMOD.VECTOR {x = vector.X, y = vector.Y, z = 0};
}
public static FMOD.VECTOR ToFVector(Vector3 vector)
{
return new FMOD.VECTOR {x = vector.X, y = vector.Y, z = vector.Z};
}
public static void playOneshot(string path)
{
system.getEvent(path, out EventDescription evDesc);
evDesc.isOneshot(out bool oneshot);
Debug.Assert(oneshot == true, "Event must be a oneshot as defined by FMOD.");
evDesc.createInstance(out EventInstance evInst);
evInst.start();
evInst.release();
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
system = initFmod();
FSystem.LoadBankFile(MasterBankPath);
FSystem.LoadBankFile(MasterStringsPath);
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
system.update();
}
public override void _ExitTree()
{
FSystem.system.release();
base._ExitTree();
}
}
This will need to be registered as Global under Project->Project Settings->Globals:
You could use this to play oneshots from your scripts like so:
FSystem.playOneshot("event:/BigWetFart");
In order to utilize spatialization a (very) basic listener emitter system can be created.
FListener.cs
using Godot;
using System;
using FMOD.Studio;
public partial class FListener : Node2D
{
public int Index = 0;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
updateListenerAttributes();
}
private void updateListenerAttributes()
{
var parent = this.GetParent();
if (parent == null) {
return;
}
FMOD.VECTOR FPosition = FSystem.ToFVector(new Vector2(this.GlobalPosition.X, this.GlobalPosition.Y));
FMOD.VECTOR FVelocity = FSystem.ToFVector(Vector2.Zero);
FMOD.VECTOR FForward = FSystem.ToFVector(Vector2.Up);
FMOD.VECTOR FUp = FSystem.ToFVector(new Vector3 {X = 0.0f, Y = 0.0F, Z = 1.0f});
FMOD.ATTRIBUTES_3D A3D = new FMOD.ATTRIBUTES_3D {position = FPosition, velocity = FVelocity, forward = FForward, up = FUp};
FSystem.system.setListenerAttributes(Index, A3D);
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
updateListenerAttributes();
}
}
FEmitter.cs:
using Godot;
using System;
using FMOD.Studio;
public partial class FEmitter : Node2D
{
[Export]
public string EventPath;
[Export]
public bool IsLoop = false;
[Export]
public bool IsPositional = false;
public bool AllowFadeout = true;
private bool isInit = false;
public EventInstance FEventInst;
public FEmitter()
{
//empty constructor for nodes added in the editor
}
public FEmitter(string EPath, bool loop, bool positional)
{
EventPath = EPath;
IsLoop = loop;
IsPositional = positional;
initFEvent();
}
private void initFEvent()
{
FSystem.system.getEvent(EventPath, out EventDescription evDesc);
evDesc.createInstance(out EventInstance evInst);
FEventInst = evInst;
if (IsPositional == true)
{
update3DAttributes();
}
}
private void update3DAttributes()
{
FMOD.VECTOR FPosition = FSystem.ToFVector(new Vector2(this.GlobalPosition.X, this.GlobalPosition.Y));
FMOD.VECTOR FVelocity = FSystem.ToFVector(Vector2.Zero);
FMOD.VECTOR FForward = FSystem.ToFVector(Vector2.Right);
FMOD.VECTOR FUp = FSystem.ToFVector(Vector2.Up);
FMOD.ATTRIBUTES_3D A3D = new FMOD.ATTRIBUTES_3D {position = FPosition, velocity = FVelocity, forward = FForward, up = FUp};
FEventInst.set3DAttributes(A3D);
}
public void Play()
{
FEventInst.start();
}
public void Stop()
{
FEventInst.stop(STOP_MODE.ALLOWFADEOUT);
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
initFEvent();
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
if (IsPositional == true)
{
update3DAttributes();
}
FMOD.ATTRIBUTES_3D eventAtts;
FEventInst.get3DAttributes(out eventAtts);
}
public override void _ExitTree()
{
FEventInst.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
FEventInst.release();
base._ExitTree();
}
}
Add the listener to the player or wherever it makes the most sense. The emitter can be simply added as a child node to whatever object you would like to emit the sound and accessed like so:
using Godot;
using System;
public partial class emitter_test : Sprite2D
{
private FEmitter emitter;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
emitter = GetNode<FEmitter>("FEmitter");
emitter.Play();
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
(Godot scene layout for this demo)
Video Demo