Skip to content

Commit

Permalink
feat: Add programmer callbacks to FmodEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
piiertho committed Jul 16, 2024
1 parent 15908f2 commit 269a5d8
Show file tree
Hide file tree
Showing 18 changed files with 248 additions and 51 deletions.
18 changes: 18 additions & 0 deletions demo/high_level_2D/ChooseLanguageButton.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class_name ChooseLanguageButton extends OptionButton


@export var root_node: Node
var current_bank_loader: FmodBankLoader = null

func _enter_tree():
connect("item_selected", _on_item_selected)

func _on_item_selected(index: int):
if index == -1:
return
if (current_bank_loader != null):
root_node.remove_child(current_bank_loader)
var bank_path := "res://assets/Banks/Dialogue_%s.bank" % get_item_text(index)
current_bank_loader = FmodBankLoader.new()
current_bank_loader.bank_paths = [bank_path]
root_node.add_child(current_bank_loader)
28 changes: 14 additions & 14 deletions demo/high_level_2D/Emitter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ extends FmodEventEmitter2D
var isPlaying: bool = true

func _process(_delta):
if Input.is_action_just_pressed("space"):
isPlaying = !isPlaying
if(isPlaying):
print("Mower playing")
paused = false
else:
print("Mower paused")
paused = true
elif Input.is_action_just_pressed("kill_event"):
self.queue_free()
if Input.is_action_pressed("engine_power_up"):
self["event_parameter/RPM/value"] = self["event_parameter/RPM/value"] + 10
if Input.is_action_pressed("engine_power_down"):
self["event_parameter/RPM/value"] = self["event_parameter/RPM/value"] - 10
if Input.is_action_just_pressed("space"):
isPlaying = !isPlaying
if(isPlaying):
print("Mower playing")
paused = false
else:
print("Mower paused")
paused = true
elif Input.is_action_just_pressed("kill_event"):
self.queue_free()
if Input.is_action_pressed("engine_power_up"):
self["event_parameter/RPM/value"] = self["event_parameter/RPM/value"] + 10
if Input.is_action_pressed("engine_power_down"):
self["event_parameter/RPM/value"] = self["event_parameter/RPM/value"] - 10
30 changes: 28 additions & 2 deletions demo/high_level_2D/FmodNodesTest.tscn
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
[gd_scene load_steps=12 format=3 uid="uid://dl6g18ybwc83t"]
[gd_scene load_steps=14 format=3 uid="uid://dl6g18ybwc83t"]

[ext_resource type="Script" path="res://high_level_2D/sin_move.gd" id="1_2lkrj"]
[ext_resource type="Script" path="res://high_level_2D/Emitter.gd" id="2_5cntr"]
[ext_resource type="Texture2D" uid="uid://d4blkybu5ojj6" path="res://icon.png" id="2_llv2n"]
[ext_resource type="Texture2D" uid="uid://c872sqsokojxe" path="res://icon.png" id="2_llv2n"]
[ext_resource type="Script" path="res://high_level_2D/Kinematic.gd" id="3_dlbku"]
[ext_resource type="Script" path="res://high_level_2D/ChangeColor.gd" id="5_5p5kb"]
[ext_resource type="Script" path="res://low_level_2D/EnterAndLeave.gd" id="5_jxvuy"]
[ext_resource type="PackedScene" uid="uid://glfbseq2tmgg" path="res://high_level_2D/footstep.tscn" id="5_usogy"]
[ext_resource type="Script" path="res://low_level_2D/EnterandLeave2.gd" id="7_c28gt"]
[ext_resource type="Script" path="res://high_level_2D/ChooseLanguageButton.gd" id="9_syqwk"]
[ext_resource type="Script" path="res://high_level_2D/SayWelcomeButton.gd" id="10_gwsas"]

[sub_resource type="RectangleShape2D" id="1"]
size = Vector2(87.7038, 82.621)
Expand Down Expand Up @@ -158,4 +160,28 @@ Start when entering
Stop when exiting
Only one instance of that music can be played "

[node name="ChooseLanguageButton" type="OptionButton" parent="." node_paths=PackedStringArray("root_node")]
offset_left = 918.0
offset_top = 615.0
offset_right = 986.0
offset_bottom = 646.0
item_count = 3
popup/item_0/text = "CN"
popup/item_0/id = 0
popup/item_1/text = "EN"
popup/item_1/id = 1
popup/item_2/text = "JP"
popup/item_2/id = 2
script = ExtResource("9_syqwk")
root_node = NodePath("..")

[node name="SayWelcomeButton" type="Button" parent="." node_paths=PackedStringArray("welcome_option_button")]
offset_left = 987.0
offset_top = 615.0
offset_right = 1100.0
offset_bottom = 646.0
text = "Say welcome"
script = ExtResource("10_gwsas")
welcome_option_button = NodePath("../ChooseLanguageButton")

[connection signal="timeline_beat" from="FmodBankLoader/SoundArea2/FmodEventEmitter2D" to="FmodBankLoader/SoundArea2" method="change_color"]
16 changes: 16 additions & 0 deletions demo/high_level_2D/SayWelcomeButton.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
extends Button


@export var welcome_option_button: ChooseLanguageButton

func _enter_tree():
connect("pressed", _on_pressed)

func _on_pressed():
if welcome_option_button.current_bank_loader == null:
return
var event_emitter = FmodEventEmitter2D.new()
event_emitter.event_guid = "{9aa2ecc5-ea4b-4ebe-85c3-054b11b21dcd}"
event_emitter.autoplay = true
event_emitter.set_programmer_callback("welcome")
welcome_option_button.current_bank_loader.add_child(event_emitter)
4 changes: 2 additions & 2 deletions demo/high_level_2D/sin_move.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ extends Node2D


func _process(delta: float) -> void:
var time = Time.get_ticks_msec()/1000.0
self.position.x = 700 + 300 * sin(time)
var time = Time.get_ticks_msec()/1000.0
self.position.x = 700 + 300 * sin(time)
38 changes: 19 additions & 19 deletions demo/low_level_2D/Emitter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ var event: FmodEvent = null

# Called when the node enters the scene tree for the first time.
func _ready():
event = FmodServer.create_event_instance("event:/Vehicles/Car Engine")
event.set_2d_attributes(self.global_transform)
event.set_parameter_by_name("RPM", 600)
event.set_volume( 2)
event.start()
event = FmodServer.create_event_instance("event:/Vehicles/Car Engine")
event.set_2d_attributes(self.global_transform)
event.set_parameter_by_name("RPM", 600)
event.set_volume( 2)
event.start()
# warning-ignore:unused_argument
func _process(_delta):
if Input.is_action_just_pressed("space"):
isPlaying = !isPlaying
if(isPlaying):
print("Mower playing")
event.set_paused(false)
else:
print("Mower paused")
event.set_paused(true)
elif Input.is_action_just_pressed("kill_event"):
self.queue_free()
var time = Time.get_ticks_msec()/1000.0
self.position.x = 300 * sin(time)
event.set_2d_attributes(self.global_transform)
if Input.is_action_just_pressed("space"):
isPlaying = !isPlaying
if(isPlaying):
print("Mower playing")
event.set_paused(false)
else:
print("Mower paused")
event.set_paused(true)
elif Input.is_action_just_pressed("kill_event"):
self.queue_free()
var time = Time.get_ticks_msec()/1000.0
self.position.x = 300 * sin(time)
event.set_2d_attributes(self.global_transform)

29 changes: 27 additions & 2 deletions demo/low_level_2D/FmodScriptTest.tscn
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[gd_scene load_steps=11 format=3 uid="uid://cs8nm6h12whh1"]
[gd_scene load_steps=13 format=3 uid="uid://cs8nm6h12whh1"]

[ext_resource type="Script" path="res://low_level_2D/FmodTest.gd" id="1_oc8v3"]
[ext_resource type="Texture2D" uid="uid://d4blkybu5ojj6" path="res://icon.png" id="2_mamb7"]
[ext_resource type="Texture2D" uid="uid://c872sqsokojxe" path="res://icon.png" id="2_mamb7"]
[ext_resource type="Script" path="res://low_level_2D/Emitter.gd" id="3_fx7d3"]
[ext_resource type="Script" path="res://low_level_2D/Listener.gd" id="4_448uv"]
[ext_resource type="Script" path="res://low_level_2D/EnterAndLeave.gd" id="5_85yno"]
[ext_resource type="Script" path="res://low_level_2D/ChangeColor.gd" id="6_fyoq1"]
[ext_resource type="Script" path="res://low_level_2D/EnterandLeave2.gd" id="7_6vajh"]
[ext_resource type="Script" path="res://low_level_2D/LangChooseButton.gd" id="8_f6f5a"]
[ext_resource type="Script" path="res://low_level_2D/WelcomeButton.gd" id="9_jo4tj"]

[sub_resource type="RectangleShape2D" id="1"]
size = Vector2(87.7038, 82.621)
Expand Down Expand Up @@ -122,3 +124,26 @@ text = "File loaded as Music
Start when entering
Stop when exiting
Only one instance of that music can be played "

[node name="WelcomeOptionButton" type="OptionButton" parent="."]
offset_left = 918.0
offset_top = 615.0
offset_right = 986.0
offset_bottom = 646.0
item_count = 3
popup/item_0/text = "CN"
popup/item_0/id = 0
popup/item_1/text = "EN"
popup/item_1/id = 1
popup/item_2/text = "JP"
popup/item_2/id = 2
script = ExtResource("8_f6f5a")

[node name="WelcomeButton" type="Button" parent="."]
offset_left = 987.0
offset_top = 615.0
offset_right = 1100.0
offset_bottom = 646.0
text = "Say welcome"
script = ExtResource("9_jo4tj")
welcome_option_button_path = NodePath("../WelcomeOptionButton")
14 changes: 7 additions & 7 deletions demo/low_level_2D/FmodTest.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ extends Node

# Called when the node enters the scene tree for the first time.
func _enter_tree():
# load banks
# load banks
# warning-ignore:return_value_discarded
FmodServer.load_bank("res://assets/Banks/Master.strings.bank", FmodServer.FMOD_STUDIO_LOAD_BANK_NORMAL)
FmodServer.load_bank("res://assets/Banks/Master.strings.bank", FmodServer.FMOD_STUDIO_LOAD_BANK_NORMAL)
# warning-ignore:return_value_discarded
FmodServer.load_bank("res://assets/Banks/Master.bank", FmodServer.FMOD_STUDIO_LOAD_BANK_NORMAL)
FmodServer.load_bank("res://assets/Banks/Master.bank", FmodServer.FMOD_STUDIO_LOAD_BANK_NORMAL)
# warning-ignore:return_value_discarded
FmodServer.load_bank("res://assets/Banks/Music.bank", FmodServer.FMOD_STUDIO_LOAD_BANK_NORMAL)
FmodServer.load_bank("res://assets/Banks/Music.bank", FmodServer.FMOD_STUDIO_LOAD_BANK_NORMAL)
# warning-ignore:return_value_discarded
FmodServer.load_bank("res://assets/Banks/Vehicles.bank", FmodServer.FMOD_STUDIO_LOAD_BANK_NORMAL)
FmodServer.load_bank("res://assets/Banks/Vehicles.bank", FmodServer.FMOD_STUDIO_LOAD_BANK_NORMAL)
# warning-ignore:return_value_discarded
FmodServer.load_bank("res://assets/Banks/SFX.bank", FmodServer.FMOD_STUDIO_LOAD_BANK_NORMAL)
print("Fmod initialised.")
FmodServer.load_bank("res://assets/Banks/SFX.bank", FmodServer.FMOD_STUDIO_LOAD_BANK_NORMAL)
print("Fmod initialised.")
17 changes: 17 additions & 0 deletions demo/low_level_2D/LangChooseButton.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class_name WelcomeOptionButton extends OptionButton


var current_index := -1

func _enter_tree():
connect("item_selected", _on_item_selected)

func _on_item_selected(index: int):
if index == -1:
return
if (current_index != -1):
FmodServer.unload_bank("res://assets/Banks/%s" % ("Dialogue_%s.bank" % get_item_text(current_index)))
current_index = index
var bank_path := "res://assets/Banks/Dialogue_%s.bank" % get_item_text(index)
# warning-ignore:return_value_discarded
FmodServer.load_bank(bank_path, FmodServer.FMOD_STUDIO_LOAD_BANK_NORMAL)
18 changes: 18 additions & 0 deletions demo/low_level_2D/WelcomeButton.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
extends Button


@export var welcome_option_button_path: NodePath
var welcome_option_button: WelcomeOptionButton

func _enter_tree():
connect("pressed", _on_pressed)

func _ready():
welcome_option_button = get_node(welcome_option_button_path)

func _on_pressed():
if welcome_option_button.current_index == -1:
return
var event_instance = FmodServer.create_event_instance("event:/Character/Dialogue")
event_instance.set_programmer_callback("welcome")
event_instance.start()
30 changes: 29 additions & 1 deletion src/callback/event_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Callbacks {

FMOD_RESULT F_CALLBACK eventCallback(FMOD_STUDIO_EVENT_CALLBACK_TYPE type, FMOD_STUDIO_EVENTINSTANCE* event, void* parameters) {
FMOD_RESULT F_CALLBACK event_callback(FMOD_STUDIO_EVENT_CALLBACK_TYPE type, FMOD_STUDIO_EVENTINSTANCE* event, void* parameters) {
auto* instance = reinterpret_cast<FMOD::Studio::EventInstance*>(event);
godot::FmodEvent* event_instance;
instance->getUserData((void**) &event_instance);
Expand Down Expand Up @@ -43,4 +43,32 @@ namespace Callbacks {

return FMOD_OK;
}

FMOD_RESULT F_CALLBACK event_programmer_callback(FMOD_STUDIO_EVENT_CALLBACK_TYPE type, FMOD_STUDIO_EVENTINSTANCE* event, void* parameters) {
auto* instance = reinterpret_cast<FMOD::Studio::EventInstance*>(event);
godot::FmodEvent* event_instance;
instance->getUserData((void**) &event_instance);

if (event_instance) {
if (type == FMOD_STUDIO_EVENT_CALLBACK_CREATE_PROGRAMMER_SOUND) {
const godot::String& sound_key {event_instance->get_programmers_callback_sound_key()};
FMOD_STUDIO_SOUND_INFO sound_info {godot::FmodServer::get_singleton()->get_sound_info(sound_key)};
FMOD::Sound* sound {
godot::FmodServer::get_singleton()->create_sound(sound_info, FMOD_LOOP_NORMAL | FMOD_CREATECOMPRESSEDSAMPLE | FMOD_NONBLOCKING)
};

auto* props {(FMOD_STUDIO_PROGRAMMER_SOUND_PROPERTIES*) parameters};

props->sound = (FMOD_SOUND*) sound;
props->subsoundIndex = sound_info.subsoundindex;
}
if (type == FMOD_STUDIO_EVENT_CALLBACK_DESTROY_PROGRAMMER_SOUND) {
auto* props {(FMOD_STUDIO_PROGRAMMER_SOUND_PROPERTIES*) parameters};
auto* sound {(FMOD::Sound*) props->sound};
ERROR_CHECK(sound->release());
}
}

return FMOD_OK;
}
}// namespace Callbacks
3 changes: 2 additions & 1 deletion src/callback/event_callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#include <fmod_studio_common.h>

namespace Callbacks {
FMOD_RESULT F_CALLBACK eventCallback(FMOD_STUDIO_EVENT_CALLBACK_TYPE type, FMOD_STUDIO_EVENTINSTANCE* event, void* parameters);
FMOD_RESULT F_CALLBACK event_callback(FMOD_STUDIO_EVENT_CALLBACK_TYPE type, FMOD_STUDIO_EVENTINSTANCE* event, void* parameters);
FMOD_RESULT F_CALLBACK event_programmer_callback(FMOD_STUDIO_EVENT_CALLBACK_TYPE type, FMOD_STUDIO_EVENTINSTANCE* event, void* parameters);
}// namespace Callbacks

#endif// GODOTFMOD_GODOT_FMOD_CALLBACK_H
16 changes: 16 additions & 0 deletions src/fmod_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,22 @@ Ref<FmodSound> FmodServer::create_sound_instance(const String& path) {
return {};
}

FMOD_STUDIO_SOUND_INFO FmodServer::get_sound_info(const String& sound_key) const {
FMOD_STUDIO_SOUND_INFO sound_info;
ERROR_CHECK(
system->getSoundInfo(sound_key.utf8().get_data(), &sound_info)
);
return sound_info;
}

FMOD::Sound* FmodServer::create_sound(FMOD_STUDIO_SOUND_INFO& sound_info, FMOD_MODE mode) const {
FMOD::Sound* sound {nullptr};
ERROR_CHECK(
coreSystem->createSound(sound_info.name_or_data, mode | sound_info.mode, &sound_info.exinfo, &sound)
);
return sound;
}

void FmodServer::set_sound_3d_settings(const Ref<FmodSound3DSettings>& p_settings) {
float distance_factor = p_settings->get_distance_factor();
if (distance_factor <= 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/fmod_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ namespace godot {
Ref<FmodFile> load_file_as_music(const String& path);
void unload_file(const String& path);
Ref<FmodSound> create_sound_instance(const String& path);
FMOD_STUDIO_SOUND_INFO get_sound_info(const String& sound_key) const;
FMOD::Sound* create_sound(FMOD_STUDIO_SOUND_INFO& sound_info, FMOD_MODE mode) const;

//CALLBACKS
void add_callback(const Callback& callback);
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
(actualSize) = maxSize; \
}

#define ERROR_CHECK(_result) checkErrors(_result, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__)
#define ERROR_CHECK(_result) godot::checkErrors(_result, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__)

#define FMODCLASS(m_class, m_inherits, m_owned) \
GDCLASS(m_class, m_inherits) \
Expand Down
Loading

0 comments on commit 269a5d8

Please sign in to comment.