forked from antoniorv6/fmod-example-ecs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundSystem.cpp
More file actions
134 lines (118 loc) · 4.95 KB
/
SoundSystem.cpp
File metadata and controls
134 lines (118 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <SoundSystem.hpp>
#include <Logger.hpp>
#include <Components.hpp>
#include <raymath.h>
void ERRCHECK_FMOD (FMOD_RESULT result, const char * file, int line)
{
if(result != FMOD_OK)
{
SOUND_ERROR("{0} - {1}", result, FMOD_ErrorString(result));
exit(-1);
}
}
#define ERRCHECK(_result) ERRCHECK_FMOD(_result, __FILE__, __LINE__)
SoundSystem::SoundSystem()
{
SOUND_TRACE("Initializing FMOD engine in our game...");
ERRCHECK(FMOD::Studio::System::create(&soundSystem));
ERRCHECK(soundSystem->getCoreSystem(&low_level_system));
ERRCHECK(low_level_system->setSoftwareFormat(0, FMOD_SPEAKERMODE_5POINT1, 0));
ERRCHECK(low_level_system->setOutput(FMOD_OUTPUTTYPE_AUTODETECT));
ERRCHECK(soundSystem->initialize(512, FMOD_STUDIO_INIT_LIVEUPDATE, FMOD_INIT_NORMAL, 0));
SOUND_INFO("Successfully initialized FMOD engine");
}
SoundSystem::~SoundSystem()
{
SOUND_TRACE("Dropping FMOD...");
for(const auto& eventDesc : eventMap)
{
eventDesc.second->releaseAllInstances();
}
for(const auto& instance : eventInstances)
{
instance.second->release();
}
master_bank->unload();
strings_bank->unload();
SOUND_INFO("FMOD dropped");
}
void SoundSystem::InitBanks(const std::string& master_bank_location, const std::string& strings_bank_location)
{
SOUND_TRACE("Initializing FMOD engine in our game...");
ERRCHECK(soundSystem->loadBankFile(master_bank_location.c_str(), FMOD_STUDIO_LOAD_BANK_NORMAL, &master_bank));
ERRCHECK(soundSystem->loadBankFile(strings_bank_location.c_str(), FMOD_STUDIO_LOAD_BANK_NORMAL, &strings_bank));
SOUND_INFO("Successfully loaded FMOD banks");
}
void SoundSystem::AddSoundComponent(entt::entity& ent, entt::registry& reg, const std::string& eventName, std::vector<std::string> floatvars, std::vector<std::string> intvars)
{
std::string eventID = eventName + std::to_string((uint32_t)ent);
SOUND_TRACE("Loading {0} as a component for entity {1}", eventName, ent);
FMOD::Studio::EventInstance * soundInstance = nullptr;
if(eventMap.find(eventName) == eventMap.end())
{
FMOD::Studio::EventDescription * eventDescription = nullptr;
ERRCHECK(soundSystem->getEvent(eventName.c_str(), &eventDescription));
eventMap[eventName] = eventDescription;
ERRCHECK(eventDescription->createInstance(&soundInstance));
}
else
{
ERRCHECK(eventMap[eventName]->createInstance(&soundInstance));
}
if(reg.all_of<PositionComponent>(ent))
{
SOUND_TRACE("Including event into 3D position");
auto poscomp = reg.get<PositionComponent>(ent);
FMOD_3D_ATTRIBUTES l_eventAttributes {FMOD_VECTOR{-poscomp.x, poscomp.y, poscomp.z}, FMOD_VECTOR{0,0,0}, FMOD_VECTOR{0,0,1}, FMOD_VECTOR{0,1,0}};
ERRCHECK(soundInstance->set3DAttributes(&l_eventAttributes));
}
eventInstances[eventID] = soundInstance;
std::map<std::string, float> floatmap;
std::map<std::string, int> intmap;
for(const auto& element : floatvars)
{
floatmap[element] = 0.0f;
}
for(const auto& element : intvars)
{
intmap[element] = 0.0f;
}
reg.emplace<SoundComponent>(ent, eventID, floatmap, intmap, 0, 0, 1);
ERRCHECK(soundInstance->start());
SOUND_TRACE("Entity {0} has registered a new Render Component", ent);
}
void SoundSystem::Update(entt::registry& reg)
{
//Update Listener Position
auto view = reg.view<const ListenerComponent, const PositionComponent, const CameraComponent>();
for(auto [lc, p_comp, c_comp]: view.each())
{
Vector3 normalizedForward = Vector3Normalize((Vector3){p_comp.x-c_comp.tarX, p_comp.y-c_comp.tarY, p_comp.z-c_comp.tarZ});
FMOD_3D_ATTRIBUTES l_listenerAttributes {FMOD_VECTOR{-p_comp.x, p_comp.y, p_comp.z}, FMOD_VECTOR{0,0,0}, FMOD_VECTOR{normalizedForward.x, 0.0f, -normalizedForward.z}, FMOD_VECTOR{0,1,0}};
ERRCHECK(soundSystem->setListenerAttributes(0, &l_listenerAttributes));
}
auto view_sc = reg.view<SoundComponent>();
for(auto [e, sc] : view_sc.each())
{
if(sc.marked_for_play)
{
ERRCHECK(eventInstances[sc.event_id]->start());
sc.marked_for_play = 0;
}
if(sc.marked_for_stop)
{
ERRCHECK(eventInstances[sc.event_id]->stop(FMOD_STUDIO_STOP_IMMEDIATE));
sc.marked_for_stop = 0;
}
if(sc.marked_for_parameter_update)
{
for(const auto& element : sc.floatparameers)
ERRCHECK(eventInstances[sc.event_id]->setParameterByName(element.first.c_str(), element.second));
for(const auto& element : sc.intParameters)
ERRCHECK(eventInstances[sc.event_id]->setParameterByName(element.first.c_str(), element.second));
reg.replace<SoundComponent>(e, sc.event_id, sc.floatparameers, sc.intParameters, 0, sc.marked_for_play, sc.marked_for_stop);
}
}
//Update
soundSystem->update();
}