MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
RtAudioBackend.hpp
Go to the documentation of this file.
1#pragma once
2#include "AudioBackend.hpp"
3
4#ifdef RTAUDIO_BACKEND
5#include "RtAudio.h"
6#endif
7
8namespace MayaFlux::Core {
9
10/**
11 * @brief Converts RtAudio-specific device information to the engine's device model
12 * @param rtInfo Native RtAudio device information structure
13 * @param id System identifier for the device
14 * @param defaultOutputDevice System identifier for the default output device
15 * @param defaultInputDevice System identifier for the default input device
16 * @return Standardized DeviceInfo structure with platform-agnostic representation
17 *
18 * Maps the vendor-specific device information format to the engine's
19 * standardized representation, enabling consistent device handling
20 * across different audio backends.
21 */
22// TODO:: Need to see if this is truly necessary or if DeviceInfo can be more accommodating
24 const RtAudio::DeviceInfo& rtInfo,
25 unsigned int id,
26 unsigned int defaultOutputDevice,
27 unsigned int defaultInputDevice)
28{
29 DeviceInfo info;
30 info.name = rtInfo.name;
31 info.input_channels = rtInfo.inputChannels;
32 info.output_channels = rtInfo.outputChannels;
33 info.duplex_channels = rtInfo.duplexChannels;
34 info.preferred_sample_rate = rtInfo.preferredSampleRate;
35 info.supported_samplerates = rtInfo.sampleRates;
36 info.is_default_output = (id == defaultOutputDevice);
37 info.is_default_input = (id == defaultInputDevice);
38 return info;
39}
40
41/**
42 * @class RtAudioBackend
43 * @brief RtAudio implementation of the audio backend interface
44 *
45 * Provides a concrete implementation of the IAudioBackend interface
46 * using the RtAudio cross-platform audio I/O library. This class
47 * manages the lifecycle of the RtAudio context and serves as a factory
48 * for RtAudio-specific device and stream implementations.
49 */
51public:
52 /**
53 * @brief Initializes the RtAudio backend
54 *
55 * Creates and configures the RtAudio context with default settings.
56 * The backend is ready for device enumeration and stream creation
57 * immediately after construction.
58 */
60
61 /**
62 * @brief Cleans up the RtAudio backend
63 *
64 * Ensures proper release of all RtAudio resources when the backend
65 * is no longer needed.
66 */
67 ~RtAudioBackend() override;
68
69 /**
70 * @brief Creates an RtAudio-specific device manager
71 * @return Unique pointer to an RtAudioDevice implementation
72 *
73 * Instantiates a device manager that uses the RtAudio API to
74 * enumerate and provide information about available audio endpoints.
75 */
76 std::unique_ptr<AudioDevice> create_device_manager() override;
77
78 /**
79 * @brief Creates an RtAudio-specific audio stream
80 * @param output_device_id System identifier for the target audio output device
81 * @param input_device_id System identifier for the target audio input device
82 * @param stream_info Configuration parameters for the audio stream
83 * @param user_data Optional context pointer passed to callbacks
84 * @return Unique pointer to an RtAudioStream implementation
85 *
86 * Establishes a digital audio pipeline between the application and
87 * the specified hardware endpoint using the RtAudio API.
88 */
89 std::unique_ptr<AudioStream> create_stream(
90 unsigned int output_device_id,
91 unsigned int input_device_id,
92 const GlobalStreamInfo& stream_info,
93 void* user_data) override;
94
95 /**
96 * @brief Retrieves the RtAudio library version
97 * @return String representation of the RtAudio version
98 */
99 [[nodiscard]] std::string get_version_string() const override;
100
101 /**
102 * @brief Retrieves the RtAudio API type identifier
103 * @return Integer code representing the active audio API
104 *
105 * Returns a value from the RtAudio::Api enumeration indicating
106 * which underlying audio system is being used (WASAPI, ALSA, etc.).
107 */
108 [[nodiscard]] int get_api_type() const override;
109
110 /**
111 * @brief Provides safe access to the RtAudio context
112 * @return Reference to the underlying RtAudio instance
113 *
114 * Exposes the native RtAudio handle for operations that require
115 * direct access to the RtAudio API.
116 */
117 RtAudio& get_context_reference() { return *m_context; }
118
119 /**
120 * @brief Releases all resources held by the backend
121 *
122 * Performs necessary cleanup operations before backend destruction,
123 * including releasing the RtAudio context and any associated resources.
124 * This method should be called only before application termination to
125 * ensure proper resource deallocation and prevent memory leaks. It is not
126 * intended for general use and should not be called during normal
127 * application operation.
128 * NOTE: The nature of unique_ptr makes manual cleanup redundant. This only exists when it is necessary to
129 * take ownership of the cleanup, for example, when a different backend needs to be used
130 * or when Audio is no longer needed.
131 */
132 void cleanup() override;
133
134private:
135 /** @brief Pointer to the underlying RtAudio context */
136 RtAudio* m_context;
137};
138
139/**
140 * @class RtAudioDevice
141 * @brief RtAudio implementation of the audio device interface
142 *
143 * Provides device enumeration and information services using the
144 * RtAudio API. Maintains cached device lists to improve performance
145 * for repeated device queries.
146 */
148public:
149 /**
150 * @brief Initializes the device manager with an RtAudio context
151 * @param context Pointer to an active RtAudio instance
152 *
153 * Creates a device manager that uses the provided RtAudio context
154 * to enumerate and query audio devices. The device lists are
155 * populated during construction.
156 */
157 RtAudioDevice(RtAudio* context);
158
159 /**
160 * @brief Retrieves information about all available output devices
161 * @return Vector of DeviceInfo structures for output endpoints
162 */
163 [[nodiscard]] std::vector<DeviceInfo> get_output_devices() const override;
164
165 /**
166 * @brief Retrieves information about all available input devices
167 * @return Vector of DeviceInfo structures for input endpoints
168 */
169 [[nodiscard]] std::vector<DeviceInfo> get_input_devices() const override;
170
171 /**
172 * @brief Gets the system's primary output device identifier
173 * @return Device ID for the default output endpoint
174 */
175 [[nodiscard]] unsigned int get_default_output_device() const override;
176
177 /**
178 * @brief Gets the system's primary input device identifier
179 * @return Device ID for the default input endpoint
180 */
181 [[nodiscard]] unsigned int get_default_input_device() const override;
182
183private:
184 /** @brief Pointer to the underlying RtAudio context */
185 RtAudio* m_context;
186
187 /** @brief Cached list of output devices */
188 std::vector<DeviceInfo> m_output_devices;
189
190 /** @brief Cached list of input devices */
191 std::vector<DeviceInfo> m_input_devices;
192
193 /** @brief System identifier for the default output device */
195
196 /** @brief System identifier for the default input device */
198};
199
200/**
201 * @class RtAudioStream
202 * @brief RtAudio implementation of the audio stream interface
203 *
204 * Manages real-time audio data flow between the application and hardware
205 * using the RtAudio API. Handles the configuration, lifecycle, and callback
206 * integration for audio streams.
207 */
209public:
210 /**
211 * @brief Initializes an audio stream with the specified configuration
212 * @param context Pointer to an active RtAudio instance
213 * @param output_device_id System identifier for the target audio output device
214 * @param input_device_id System identifier for the target audio input device
215 * @param streamInfo Configuration parameters for the audio stream
216 * @param userData Optional context pointer passed to callbacks
217 *
218 * Creates an audio stream configuration but does not open the stream.
219 * The stream must be explicitly opened with open() before use.
220 */
222 RtAudio* context,
223 unsigned int output_device_id,
224 unsigned int input_device_id,
225 const GlobalStreamInfo& streamInfo,
226 void* userData);
227
228 /**
229 * @brief Ensures proper cleanup of stream resources
230 *
231 * Automatically closes the stream if it's still open during destruction.
232 */
233 ~RtAudioStream() override;
234
235 /**
236 * @brief Initializes the audio stream and allocates required resources
237 *
238 * Opens the RtAudio stream with the configured parameters, making it
239 * ready for data transfer. Does not start the actual data flow.
240 */
241 void open() override;
242
243 /**
244 * @brief Activates the audio stream and begins data transfer
245 *
246 * Starts the RtAudio stream, initiating the real-time processing
247 * of audio data through the registered callback function.
248 */
249 void start() override;
250
251 /**
252 * @brief Deactivates the audio stream and halts data transfer
253 *
254 * Stops the RtAudio stream, suspending the real-time processing
255 * while maintaining the stream's configuration.
256 */
257 void stop() override;
258
259 /**
260 * @brief Terminates the audio stream and releases all resources
261 *
262 * Closes the RtAudio stream, releasing all allocated resources
263 * and disconnecting from hardware endpoints.
264 */
265 void close() override;
266
267 /**
268 * @brief Checks if the stream is actively processing audio data
269 * @return True if the stream is currently active, false otherwise
270 */
271 bool is_running() const override;
272
273 /**
274 * @brief Checks if the stream is initialized and ready for activation
275 * @return True if the stream is open, false otherwise
276 */
277 bool is_open() const override;
278
279 /**
280 * @brief Sets the function to process audio data
281 * @param processCallback Function that handles audio data processing
282 *
283 * Registers a callback function that will be invoked by the RtAudio
284 * system when new input data is available or output data is needed.
285 */
287 std::function<int(void*, void*, unsigned int)> processCallback) override;
288
289private:
290 /**
291 * @brief Static callback function for the RtAudio API
292 * @param output_buffer Pointer to the output sample buffer
293 * @param input_buffer Pointer to the input sample buffer
294 * @param num_frames Number of frames to process
295 * @param stream_time Current stream time in seconds
296 * @param status Stream status flags
297 * @param user_data Pointer to the RtAudioStream instance
298 * @return 0 on success, non-zero on error
299 *
300 * This static function serves as the entry point for the RtAudio
301 * callback system. It unpacks the parameters and delegates to the
302 * user-provided callback function registered with set_process_callback.
303 */
304 static int rtAudioCallback(
305 void* output_buffer,
306 void* input_buffer,
307 unsigned int num_frames,
308 double stream_time,
309 RtAudioStreamStatus status,
310 void* user_data);
311
312 /** @brief Pointer to the underlying RtAudio context */
313 RtAudio* m_context;
314
315 /** @brief RtAudio-specific stream output configuration parameters */
316 RtAudio::StreamParameters m_out_parameters;
317
318 /** @brief RtAudio-specific stream input configuration parameters */
319 RtAudio::StreamParameters m_in_parameters;
320
321 /** @brief RtAudio-specific stream options */
322 RtAudio::StreamOptions m_options;
323
324 /** @brief Engine stream configuration */
326
327 /** @brief User-provided context pointer for callbacks */
329
330 /** @brief Flag indicating if the stream is currently open */
332
333 /** @brief Flag indicating if the stream is currently running */
335
336 /** @brief User-provided callback function for audio processing */
337 std::function<int(void*, void*, unsigned int)> m_process_callback;
338
339 /** @brief Copy of the stream configuration for reference */
341
342 /**
343 * @brief Configures RtAudio stream options based on GlobalStreamInfo
344 *
345 * Maps the engine's stream configuration parameters to the
346 * corresponding RtAudio-specific options.
347 */
349};
350
351} // namespace MayaFlux::Core
Manages audio endpoint discovery and enumeration.
Manages digital audio data flow between the engine and hardware.
Interface for audio system abstraction layer.
int get_api_type() const override
Retrieves the RtAudio API type identifier.
RtAudio & get_context_reference()
Provides safe access to the RtAudio context.
std::string get_version_string() const override
Retrieves the RtAudio library version.
std::unique_ptr< AudioStream > create_stream(unsigned int output_device_id, unsigned int input_device_id, const GlobalStreamInfo &stream_info, void *user_data) override
Creates an RtAudio-specific audio stream.
RtAudio * m_context
Pointer to the underlying RtAudio context.
void cleanup() override
Releases all resources held by the backend.
RtAudioBackend()
Initializes the RtAudio backend.
std::unique_ptr< AudioDevice > create_device_manager() override
Creates an RtAudio-specific device manager.
~RtAudioBackend() override
Cleans up the RtAudio backend.
RtAudio implementation of the audio backend interface.
std::vector< DeviceInfo > m_output_devices
Cached list of output devices.
unsigned int m_defaultOutputDevice
System identifier for the default output device.
RtAudio * m_context
Pointer to the underlying RtAudio context.
std::vector< DeviceInfo > get_input_devices() const override
Retrieves information about all available input devices.
std::vector< DeviceInfo > m_input_devices
Cached list of input devices.
std::vector< DeviceInfo > get_output_devices() const override
Retrieves information about all available output devices.
unsigned int get_default_output_device() const override
Gets the system's primary output device identifier.
unsigned int get_default_input_device() const override
Gets the system's primary input device identifier.
unsigned int m_defaultInputDevice
System identifier for the default input device.
RtAudio implementation of the audio device interface.
RtAudio::StreamOptions m_options
RtAudio-specific stream options.
GlobalStreamInfo m_stream_info
Copy of the stream configuration for reference.
void set_process_callback(std::function< int(void *, void *, unsigned int)> processCallback) override
Sets the function to process audio data.
void * m_userData
User-provided context pointer for callbacks.
void start() override
Activates the audio stream and begins data transfer.
void stop() override
Deactivates the audio stream and halts data transfer.
void configure_stream_options()
Configures RtAudio stream options based on GlobalStreamInfo.
~RtAudioStream() override
Ensures proper cleanup of stream resources.
bool m_isOpen
Flag indicating if the stream is currently open.
bool is_open() const override
Checks if the stream is initialized and ready for activation.
void open() override
Initializes the audio stream and allocates required resources.
bool m_isRunning
Flag indicating if the stream is currently running.
GlobalStreamInfo stream_info
Engine stream configuration.
RtAudio::StreamParameters m_in_parameters
RtAudio-specific stream input configuration parameters.
RtAudio::StreamParameters m_out_parameters
RtAudio-specific stream output configuration parameters.
void close() override
Terminates the audio stream and releases all resources.
RtAudio * m_context
Pointer to the underlying RtAudio context.
std::function< int(void *, void *, unsigned int)> m_process_callback
User-provided callback function for audio processing.
static int rtAudioCallback(void *output_buffer, void *input_buffer, unsigned int num_frames, double stream_time, RtAudioStreamStatus status, void *user_data)
Static callback function for the RtAudio API.
bool is_running() const override
Checks if the stream is actively processing audio data.
RtAudio implementation of the audio stream interface.
static DeviceInfo convert_device_info(const RtAudio::DeviceInfo &rtInfo, unsigned int id, unsigned int defaultOutputDevice, unsigned int defaultInputDevice)
Converts RtAudio-specific device information to the engine's device model.
std::string name
System identifier for the audio endpoint.
uint32_t duplex_channels
Number of channels supporting simultaneous input and output.
std::vector< uint32_t > supported_samplerates
Collection of all sample rates supported by this device.
bool is_default_output
Indicates if this device is the system's primary output endpoint.
uint32_t input_channels
Number of discrete input channels available for signal capture.
bool is_default_input
Indicates if this device is the system's primary input endpoint.
uint32_t output_channels
Number of discrete output channels available for signal playback.
uint32_t preferred_sample_rate
Optimal sample rate for this device as reported by the system.
Contains digital audio device configuration parameters.
Comprehensive configuration for digital audio stream processing.