Unravel Engine C++ Reference
Loading...
Searching...
No Matches
audio_system.cpp
Go to the documentation of this file.
1#include "audio_system.h"
2#include <engine/events.h>
3
7#include <engine/ecs/ecs.h>
9#include <audiopp/logger.h>
10#include <logging/logging.h>
11
12namespace unravel
13{
14
15namespace
16{
17
18void on_create_component(entt::registry& r, entt::entity e)
19{
20 auto& comp = r.get<audio_source_component>(e);
21
22 if(r.all_of<active_component>(e))
23 {
24 comp.on_play_begin();
25 }
26}
27void on_destroy_component(entt::registry& r, entt::entity e)
28{
29 auto& comp = r.get<audio_source_component>(e);
30 comp.on_play_end();
31}
32
33void on_create_active_component(entt::registry& r, entt::entity e)
34{
35 if(auto comp = r.try_get<audio_source_component>(e))
36 {
37 comp->on_play_begin();
38 }
39}
40void on_destroy_active_component(entt::registry& r, entt::entity e)
41{
42 if(auto comp = r.try_get<audio_source_component>(e))
43 {
44 comp->on_play_end();
45 }
46}
47
48
49} // namespace
50
52{
53 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
54
55 auto& ev = ctx.get_cached<events>();
56 ev.on_frame_update.connect(sentinel_, this, &audio_system::on_frame_update);
57
58 ev.on_play_begin.connect(sentinel_, 10, this, &audio_system::on_play_begin);
59 ev.on_play_end.connect(sentinel_, -10, this, &audio_system::on_play_end);
60 ev.on_pause.connect(sentinel_, 10, this, &audio_system::on_pause);
61 ev.on_resume.connect(sentinel_, -10, this, &audio_system::on_resume);
62 ev.on_skip_next_frame.connect(sentinel_, -10, this, &audio_system::on_skip_next_frame);
63
64 audio::set_info_logger(
65 [](const std::string& s)
66 {
67 APPLOG_TRACE(s);
68 });
69 audio::set_error_logger(
70 [](const std::string& s)
71 {
72 APPLOG_ERROR(s);
73 });
74
75 audio::set_trace_logger(
76 [](const std::string& s)
77 {
78 APPLOG_TRACE(s);
79 });
80
81 audio::device::print_devices();
82 device_ = std::make_unique<audio::device>();
83
84 return true;
85}
86
88{
89 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
90
91 return true;
92}
93
94void audio_system::on_play_begin(rtti::context& ctx)
95{
96 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
97
98 auto& ec = ctx.get_cached<ecs>();
99 auto& scn = ec.get_scene();
100 auto& registry = *scn.registry;
101
102 registry.on_construct<audio_source_component>().connect<&on_create_component>();
103 registry.on_destroy<audio_source_component>().connect<&on_destroy_component>();
104
105
106 registry.on_construct<active_component>().connect<&on_create_active_component>();
107 registry.on_destroy<active_component>().connect<&on_destroy_active_component>();
108
109
110 registry.view<audio_source_component, active_component>().each(
111 [&](auto e, auto&& comp, auto&& active)
112 {
113 comp.on_play_begin();
114 });
115}
116
117void audio_system::on_play_end(rtti::context& ctx)
118{
119 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
120
121 auto& ec = ctx.get_cached<ecs>();
122 auto& scn = ec.get_scene();
123 auto& registry = *scn.registry;
124
125 registry.view<audio_source_component, active_component>().each(
126 [&](auto e, auto&& comp, auto&& active)
127 {
128 comp.on_play_end();
129 });
130
131 registry.on_construct<active_component>().disconnect<&on_create_active_component>();
132 registry.on_destroy<active_component>().disconnect<&on_destroy_active_component>();
133
134 registry.on_construct<audio_source_component>().disconnect<&on_create_component>();
135 registry.on_destroy<audio_source_component>().disconnect<&on_destroy_component>();
136}
137
138void audio_system::on_pause(rtti::context& ctx)
139{
140 auto& ec = ctx.get_cached<ecs>();
141 auto& scn = ec.get_scene();
142 auto& registry = *scn.registry;
143
144 registry.view<audio_source_component>().each(
145 [&](auto e, auto&& comp)
146 {
147 comp.pause();
148 });
149}
150
151void audio_system::on_resume(rtti::context& ctx)
152{
153 auto& ec = ctx.get_cached<ecs>();
154 auto& scn = ec.get_scene();
155 auto& registry = *scn.registry;
156
157 registry.view<audio_source_component>().each(
158 [&](auto e, auto&& comp)
159 {
160 comp.resume();
161 });
162}
163
164void audio_system::on_skip_next_frame(rtti::context& ctx)
165{
166 delta_t step(1.0f / 60.0f);
167 on_frame_update(ctx, step);
168}
169
170void audio_system::on_frame_update(rtti::context& ctx, delta_t dt)
171{
172 APP_SCOPE_PERF("Audio/System Update");
173 auto& ev = ctx.get_cached<events>();
174
175 auto& ec = ctx.get_cached<ecs>();
176 auto& scn = ec.get_scene();
177 auto& registry = *scn.registry;
178
179 // update auidio spatial properties from transform
180 registry.view<transform_component, audio_listener_component, active_component>().each(
181 [&](auto e, auto&& transform, auto&& comp, auto&& active)
182 {
183 comp.update(transform.get_transform_global(), dt);
184 });
185
186 registry.view<transform_component, audio_source_component, active_component>().each(
187 [&](auto e, auto&& transform, auto&& comp, auto&& active)
188 {
189 comp.update(transform.get_transform_global(), dt);
190 });
191}
192
193} // namespace unravel
Class that contains core data for audio sources.
auto init(rtti::context &ctx) -> bool
Initializes the audio system with the given context.
auto deinit(rtti::context &ctx) -> bool
Deinitializes the audio system with the given context.
std::chrono::duration< float > delta_t
#define APPLOG_ERROR(...)
Definition logging.h:20
#define APPLOG_TRACE(...)
Definition logging.h:17
bgfx::Transform transform
Definition graphics.h:42
#define APP_SCOPE_PERF(name_literal)
Create a scoped performance timer that records to the timeline profiler. Only accepts string literals...
Definition profiler.h:675
auto get_cached() -> T &
Definition context.hpp:49
Manages the entity-component-system (ECS) operations for the ACE framework.
Definition ecs.h:12
auto get_scene() -> scene &
Gets the current scene.
Definition ecs.cpp:30
hpp::event< void(rtti::context &, delta_t)> on_frame_update
Definition events.h:16