MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Engine.hpp
Go to the documentation of this file.
1#pragma once
2
4
7
9class Random;
10}
11
12namespace MayaFlux::Vruta {
13class EventManager;
14}
15
16namespace MayaFlux::Core {
17
18class WindowManager;
19
20// struct GlobalEngineInfo {
21// GlobalStreamInfo audio;
22// GlobalWindowInfo window;
23// };
24
25/**
26 * @class Engine
27 * @brief Central lifecycle manager and component orchestrator for the MayaFlux processing system
28 *
29 * The Engine serves as the primary entry point and lifecycle coordinator for MayaFlux, acting as:
30 * - **Lifecycle Manager**: Controls initialization, startup, pause/resume, and shutdown sequences
31 * - **Component Initializer**: Creates and configures core system components with proper dependencies
32 * - **Access Router**: Provides centralized access to all major subsystems and managers
33 * - **Reference Holder**: Maintains shared ownership of core components to ensure proper lifetime management
34 *
35 * **Core Responsibilities:**
36 * 1. **System Initialization**: Orchestrates the creation and configuration of all core components
37 * 2. **Lifecycle Control**: Manages the start/stop/pause/resume cycle of the entire processing system
38 * 3. **Component Access**: Provides unified access to subsystems (audio, scheduling, node graph, buffers)
39 * 4. **Resource Management**: Ensures proper construction/destruction order and shared ownership
40 *
41 * **Architecture Philosophy:**
42 * The Engine follows a "batteries included but replaceable" approach:
43 * - Provides sensible defaults and automatic component wiring for ease of use
44 * - Allows advanced users to access individual components directly for custom workflows
45 * - Enables completely custom component instantiation when needed
46 *
47 * **Usage Patterns:**
48 *
49 * *Simple Usage (Recommended):*
50 * ```cpp
51 * Engine engine;
52 * engine.Init(48000, 512, 2, 0); // 48kHz, 512 samples, stereo out
53 * engine.Start();
54 * // Use engine.get_scheduler(), engine.get_node_graph_manager(), etc.
55 * ```
56 *
57 * *Advanced Usage:*
58 * ```cpp
59 * Engine engine;
60 * auto custom_scheduler = std::make_shared<CustomScheduler>();
61 * engine.Init(stream_info);
62 * // Replace default scheduler with custom implementation
63 * engine.get_scheduler() = custom_scheduler;
64 * ```
65 *
66 * *Offline Processing:*
67 * ```cpp
68 * // Engine components can be used without hardware I/O
69 * auto scheduler = engine.get_scheduler();
70 * auto node_graph = engine.get_node_graph_manager();
71 * // Process manually without Start()
72 * ```
73 *
74 * The Engine does not perform direct signal processing or scheduling - it delegates these
75 * responsibilities to specialized subsystems while ensuring they work together coherently.
76 */
77class MAYAFLUX_API Engine {
78public:
79 //-------------------------------------------------------------------------
80 // Initialization and Lifecycle
81 //-------------------------------------------------------------------------
82
83 /**
84 * @brief Constructs a new Engine instance
85 * @param type The type of audio backend to use (default: RtAudio)
86 *
87 * Creates a new Engine instance with the specified audio backend.
88 * The backend type determines the underlying audio API used for device management
89 * and stream processing.
90 */
91 // Engine(Utils::AudioBackendType audio_type = Utils::AudioBackendType::RTAUDIO);
92 Engine();
93
94 /**
95 * @brief Destroys the Engine instance and cleans up resources
96 *
97 * Stops and closes any active streams and frees allocated resources.
98 */
99 ~Engine();
100
101 Engine(const Engine&) = delete;
102 Engine& operator=(const Engine&) = delete;
103
104 /**
105 * @brief Move constructor
106 * @param other Engine instance to move from
107 */
108 Engine(Engine&& other) noexcept;
109
110 /**
111 * @brief Move assignment operator
112 * @param other Engine instance to move from
113 * @return Reference to this Engine
114 */
115 Engine& operator=(Engine&& other) noexcept;
116
117 /**
118 * @brief Initializes all system components and prepares for processing
119 *
120 * Orchestrates the initialization sequence for all core components:
121 * - Creates and configures the task scheduler with the specified sample rate
122 * - Initializes the node graph manager and buffer manager
123 * - Sets up subsystem managers and audio backend
124 * - Establishes proper component interconnections
125 *
126 * This method must be called before Start().
127 */
128 void Init();
129
130 /**
131 * @brief Initializes the processing engine with a custom stream configuration
132 * @param streamInfo Configuration for sample rate, buffer size, and channels
133 *
134 * Configures the processing engine with the specified stream information.
135 * This method must be called before starting the engine.
136 */
137 void Init(const GlobalStreamInfo& streamInfo);
138
139 /**
140 * @brief Initializes the processing engine with custom stream and graphics configurations
141 * @param streamInfo Configuration for sample rate, buffer size, and channels
142 * @param graphics_config Configuration for graphics/windowing backend
143 *
144 * Configures the processing engine with the specified stream and graphics information.
145 * This method must be called before starting the engine.
146 */
147 void Init(const GlobalStreamInfo& streamInfo, const GlobalGraphicsConfig& graphics_config);
148
149 /**
150 * @brief Starts the coordinated processing of all subsystems
151 *
152 * Initiates the processing lifecycle by:
153 * - Starting the audio backend and opening streams
154 * - Beginning task scheduler execution
155 * - Activating node graph processing
156 * - Enabling real-time audio I/O
157 *
158 * Init() must be called first to prepare all components.
159 */
160 void Start();
161
162 /**
163 * @brief Pauses all processing while maintaining system state
164 *
165 * Temporarily halts processing activities:
166 * - Pauses the audio stream
167 * - Suspends task scheduler execution
168 * - Maintains all component state for later resumption
169 */
170 void Pause();
171
172 /**
173 * @brief Resumes processing from paused state
174 *
175 * Restarts all processing activities:
176 * - Resumes the audio stream
177 * - Reactivates task scheduler
178 * - Continues from the exact state when paused
179 */
180 void Resume();
181
182 /**
183 * @brief Stops all processing and performs clean shutdown
184 *
185 * Orchestrates the shutdown sequence:
186 * - Terminates all active tasks and coroutines
187 * - Stops and closes audio streams
188 * - Releases all resources and buffers
189 * - Resets components to uninitialized state
190 */
191 void End();
192
193 /**
194 * @brief Checks if the coordinated processing system is currently active
195 * @return true if all subsystems are running and processing, false otherwise
196 *
197 * This reflects the overall system state - true only when the audio stream
198 * is active, schedulers are running, and the system is processing data.
199 */
200 bool is_running() const;
201
202 //-------------------------------------------------------------------------
203 // Configuration Access
204 //-------------------------------------------------------------------------
205
206 /**
207 * @brief Gets the current stream configuration
208 * @return Reference to the GlobalStreamInfo struct
209 */
210 inline GlobalStreamInfo& get_stream_info() { return m_stream_info; }
211
212 /**
213 * @brief Gets the current graphics configuration
214 * @return Reference to the GlobalGraphicsConfig struct
215 */
216 inline GlobalGraphicsConfig& get_graphics_config() { return m_graphics_config; }
217
218 //-------------------------------------------------------------------------
219 // Component Access - Engine acts as access router to all subsystems
220 //-------------------------------------------------------------------------
221
222 /**
223 * @brief Gets the node graph manager
224 * @return Shared pointer to the NodeGraphManager for node-based processing
225 *
226 * The NodeGraphManager handles the computational graph of processing nodes.
227 * Access through Engine ensures proper initialization and lifetime management.
228 */
229 inline std::shared_ptr<Nodes::NodeGraphManager> get_node_graph_manager() { return m_node_graph_manager; }
230
231 /**
232 * @brief Gets the task scheduler
233 * @return Shared pointer to the TaskScheduler for coroutine-based timing
234 *
235 * The TaskScheduler manages sample-accurate timing and coroutine execution.
236 * Access through Engine ensures proper clock synchronization with audio.
237 */
238 inline std::shared_ptr<Vruta::TaskScheduler> get_scheduler() { return m_scheduler; }
239
240 /**
241 * @brief Gets the buffer manager
242 * @return Shared pointer to the BufferManager for memory management
243 *
244 * The BufferManager handles efficient allocation and reuse of audio buffers.
245 * Access through Engine ensures buffers are sized correctly for the stream.
246 */
247 inline std::shared_ptr<Buffers::BufferManager> get_buffer_manager() { return m_buffer_manager; }
248
249 /**
250 * @brief Gets the window manager
251 * @return Shared pointer to the WindowManager for windowing operations
252 *
253 * The WindowManager handles creation and management of application windows.
254 * Access through Engine ensures proper graphics backend initialization.
255 */
256 inline std::shared_ptr<WindowManager> get_window_manager() { return m_window_manager; }
257
258 /**
259 * @brief Gets the event manager
260 * @return Shared pointer to the EventManager for input/event handling
261 *
262 * The EventManager processes input events (keyboard, mouse, etc.).
263 * Access through Engine ensures events are routed correctly to windows.
264 */
265 inline std::shared_ptr<Vruta::EventManager> get_event_manager() { return m_event_manager; }
266
267 /**
268 * @brief Gets the stochastic signal generator engine
269 * @return Pointer to the Random node for random signal generation
270 *
271 * The Random node provides various stochastic signal sources.
272 * Managed directly by Engine for optimal performance in generator nodes.
273 */
275
276 /**
277 * @brief Gets the subsystem manager for advanced component access
278 * @return Shared pointer to SubsystemManager for subsystem coordination
279 *
280 * The SubsystemManager provides access to specialized subsystems like
281 * audio backends, graphics systems, and custom processing domains.
282 */
283 inline std::shared_ptr<SubsystemManager> get_subsystem_manager() { return m_subsystem_manager; }
284
285 /**
286 * @brief Get typed access to a specific subsystem
287 * @tparam SubsystemType Expected type of the subsystem
288 * @param tokens Token configuration identifying the subsystem
289 * @return Shared pointer to subsystem or nullptr if not found
290 */
291 std::shared_ptr<ISubsystem> get_subsystem(SubsystemType type)
292 {
293 return m_subsystem_manager->get_subsystem(type);
294 }
295
296private:
297 //-------------------------------------------------------------------------
298 // System Components
299 //-------------------------------------------------------------------------
300
301 GlobalStreamInfo m_stream_info {}; ///< Stream configuration
302 GlobalGraphicsConfig m_graphics_config {}; ///< Graphics/windowing configuration
303
304 bool m_is_paused {}; ///< Pause state flag
305 bool m_is_initialized {};
306
307 std::atomic<bool> m_should_shutdown { false };
308
309 //-------------------------------------------------------------------------
310 // Core Components
311 //-------------------------------------------------------------------------
312
313 std::shared_ptr<Vruta::TaskScheduler> m_scheduler; ///< Task scheduler
314 std::shared_ptr<Nodes::NodeGraphManager> m_node_graph_manager; ///< Node graph manager
315 std::shared_ptr<Buffers::BufferManager> m_buffer_manager; ///< Buffer manager
316 std::shared_ptr<SubsystemManager> m_subsystem_manager;
317 std::shared_ptr<WindowManager> m_window_manager; ///< Window manager (Windowing subsystem)
318 std::shared_ptr<Vruta::EventManager> m_event_manager; ///< Event manager (currently only glfw events)
319 std::unique_ptr<Nodes::Generator::Stochastics::Random> m_rng; ///< Stochastic signal generator
320};
321
322} // namespace MayaFlux::Core
GlobalStreamInfo & get_stream_info()
Gets the current stream configuration.
Definition Engine.hpp:210
std::shared_ptr< SubsystemManager > m_subsystem_manager
Definition Engine.hpp:316
std::shared_ptr< WindowManager > get_window_manager()
Gets the window manager.
Definition Engine.hpp:256
std::unique_ptr< Nodes::Generator::Stochastics::Random > m_rng
Stochastic signal generator.
Definition Engine.hpp:319
std::shared_ptr< Nodes::NodeGraphManager > get_node_graph_manager()
Gets the node graph manager.
Definition Engine.hpp:229
std::shared_ptr< Vruta::EventManager > m_event_manager
Event manager (currently only glfw events)
Definition Engine.hpp:318
Engine(const Engine &)=delete
std::shared_ptr< Buffers::BufferManager > m_buffer_manager
Buffer manager.
Definition Engine.hpp:315
std::shared_ptr< ISubsystem > get_subsystem(SubsystemType type)
Get typed access to a specific subsystem.
Definition Engine.hpp:291
std::shared_ptr< SubsystemManager > get_subsystem_manager()
Gets the subsystem manager for advanced component access.
Definition Engine.hpp:283
GlobalGraphicsConfig & get_graphics_config()
Gets the current graphics configuration.
Definition Engine.hpp:216
std::shared_ptr< WindowManager > m_window_manager
Window manager (Windowing subsystem)
Definition Engine.hpp:317
std::shared_ptr< Vruta::EventManager > get_event_manager()
Gets the event manager.
Definition Engine.hpp:265
std::shared_ptr< Vruta::TaskScheduler > m_scheduler
Task scheduler.
Definition Engine.hpp:313
std::shared_ptr< Vruta::TaskScheduler > get_scheduler()
Gets the task scheduler.
Definition Engine.hpp:238
std::shared_ptr< Buffers::BufferManager > get_buffer_manager()
Gets the buffer manager.
Definition Engine.hpp:247
Nodes::Generator::Stochastics::Random * get_random_engine()
Gets the stochastic signal generator engine.
Definition Engine.hpp:274
std::shared_ptr< Nodes::NodeGraphManager > m_node_graph_manager
Node graph manager.
Definition Engine.hpp:314
Engine & operator=(const Engine &)=delete
Central lifecycle manager and component orchestrator for the MayaFlux processing system.
Definition Engine.hpp:77
Computational stochastic signal generator with multiple probability distributions.
void Resume()
Resumes audio processing on the default engine.
Definition Core.cpp:126
void Start()
Starts audio processing on the default engine.
Definition Core.cpp:114
void Pause()
Pauses audio processing on the default engine.
Definition Core.cpp:119
void Init()
Initializes the default engine with default settings.
Definition Core.cpp:84
void End()
Stops and cleans up the default engine.
Definition Core.cpp:133
Comprehensive configuration for digital audio stream processing.