MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
AudioBackend.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Core {
6class AudioDevice;
7class AudioStream;
8
9/**
10 * @struct DeviceInfo
11 * @brief Contains digital audio device configuration parameters
12 *
13 * Encapsulates the technical specifications and capabilities of an audio I/O endpoint,
14 * including channel counts, sample rate capabilities, and system identification.
15 */
16struct DeviceInfo {
17 /** @brief System identifier for the audio endpoint */
18 std::string name;
19
20 /** @brief Number of discrete input channels available for signal capture */
21 uint32_t input_channels = 0;
22
23 /** @brief Number of discrete output channels available for signal playback */
24 uint32_t output_channels = 0;
25
26 /** @brief Number of channels supporting simultaneous input and output */
27 uint32_t duplex_channels = 0;
28
29 /** @brief Optimal sample rate for this device as reported by the system */
31
32 /** @brief Collection of all sample rates supported by this device */
33 std::vector<uint32_t> supported_samplerates;
34
35 /** @brief Indicates if this device is the system's primary output endpoint */
36 bool is_default_output = false;
37
38 /** @brief Indicates if this device is the system's primary input endpoint */
39 bool is_default_input = false;
40};
41
42/**
43 * @class IAudioBackend
44 * @brief Interface for audio system abstraction layer
45 *
46 * Defines the contract for platform-specific audio subsystem implementations,
47 * providing hardware-agnostic access to digital audio I/O capabilities.
48 * Implementations handle the translation between the engine's signal processing
49 * domain and the system's audio infrastructure.
50 */
52public:
53 /** @brief Virtual destructor for proper cleanup of derived classes */
54 virtual ~IAudioBackend() = default;
55
56 /**
57 * @brief Creates a device manager for audio endpoint discovery
58 * @return Unique pointer to an AudioDevice implementation
59 *
60 * Instantiates a platform-specific device manager that can enumerate
61 * and provide information about available audio endpoints.
62 */
63 virtual std::unique_ptr<AudioDevice> create_device_manager() = 0;
64
65 /**
66 * @brief Creates an audio stream for a specific device
67 * @param output_device_id System identifier for the target audio output device
68 * @param input_device_id System identifier for the target audio input device
69 * @param stream_info Configuration parameters for the audio stream
70 * @param user_data Optional context pointer passed to callbacks
71 * @return Unique pointer to an AudioStream implementation
72 *
73 * Establishes a digital audio pipeline between the application and
74 * the specified hardware endpoint with the requested configuration.
75 */
76 virtual std::unique_ptr<AudioStream> create_stream(
77 unsigned int output_device_id,
78 unsigned int input_device_id,
79 GlobalStreamInfo& stream_info,
80 void* user_data)
81 = 0;
82
83 /**
84 * @brief Retrieves the backend implementation version
85 * @return String representation of the backend version
86 */
87 [[nodiscard]] virtual std::string get_version_string() const = 0;
88
89 /**
90 * @brief Releases all resources held by the backend.
91 *
92 * Performs necessary cleanup operations before backend destruction,
93 * including releasing system resources and terminating background processes.
94 * This method should be called only before application termination to
95 * ensure proper resource deallocation and prevent memory leaks. It is not
96 * intended for general use and should not be called during normal
97 * application operation.
98 * NOTE: The nature of unique_ptr makes manual cleanup redundant. This only exists when it is necessary to
99 * take ownership of the cleanup, for example, when a different backend needs to be used
100 * or when Audio is no longer needed.
101 */
102 virtual void cleanup() = 0;
103};
104
105/**
106 * @class AudioDevice
107 * @brief Manages audio endpoint discovery and enumeration
108 *
109 * Provides access to the system's available audio I/O endpoints,
110 * abstracting the platform-specific device enumeration process and
111 * exposing a consistent interface for querying device capabilities.
112 */
114public:
115 /** @brief Virtual destructor for proper cleanup of derived classes */
116 virtual ~AudioDevice() = default;
117
118 /**
119 * @brief Retrieves information about all available output devices
120 * @return Vector of DeviceInfo structures for output endpoints
121 */
122 [[nodiscard]] virtual std::vector<DeviceInfo> get_output_devices() const = 0;
123
124 /**
125 * @brief Retrieves information about all available input devices
126 * @return Vector of DeviceInfo structures for input endpoints
127 */
128 [[nodiscard]] virtual std::vector<DeviceInfo> get_input_devices() const = 0;
129
130 /**
131 * @brief Gets the system's primary output device identifier
132 * @return Device ID for the default output endpoint
133 */
134 [[nodiscard]] virtual unsigned int get_default_output_device() const = 0;
135
136 /**
137 * @brief Gets the system's primary input device identifier
138 * @return Device ID for the default input endpoint
139 */
140 [[nodiscard]] virtual unsigned int get_default_input_device() const = 0;
141};
142
143/**
144 * @class AudioStream
145 * @brief Manages digital audio data flow between the engine and hardware
146 *
147 * Handles the real-time bidirectional transfer of audio sample data between
148 * the processing engine and system audio endpoints. Provides lifecycle management
149 * for audio streams and callback-based processing integration.
150 */
152public:
153 /** @brief Virtual destructor for proper cleanup of derived classes */
154 virtual ~AudioStream() = default;
155
156 /**
157 * @brief Initializes the audio stream and allocates required resources
158 *
159 * Prepares the stream for data transfer by establishing connections
160 * to the hardware and allocating necessary buffers, but does not start
161 * the actual data flow.
162 */
163 virtual void open() = 0;
164
165 /**
166 * @brief Activates the audio stream and begins data transfer
167 *
168 * Initiates the real-time processing of audio data, enabling the flow
169 * of samples between the application and hardware endpoints.
170 */
171 virtual void start() = 0;
172
173 /**
174 * @brief Deactivates the audio stream and halts data transfer
175 *
176 * Suspends the real-time processing of audio data while maintaining
177 * the stream's configuration and resource allocations.
178 */
179 virtual void stop() = 0;
180
181 /**
182 * @brief Temporarily suspends audio processing without releasing resources
183 *
184 * Pauses the flow of audio data while keeping the stream's configuration
185 * intact, allowing for quick resumption without the overhead of reopening.
186 */
187 virtual void pause() = 0;
188
189 /**
190 * @brief Resumes audio processing after a pause
191 *
192 * Restarts the flow of audio data after a pause, maintaining the existing
193 * stream configuration and resource allocations.
194 */
195 virtual void resume() = 0;
196
197 /**
198 * @brief Terminates the audio stream and releases all resources
199 *
200 * Completely shuts down the audio stream, releasing all allocated
201 * resources and disconnecting from hardware endpoints.
202 */
203 virtual void close() = 0;
204
205 /**
206 * @brief Checks if the stream is actively processing audio data
207 * @return True if the stream is currently active, false otherwise
208 */
209 [[nodiscard]] virtual bool is_running() const = 0;
210
211 /**
212 * @brief Checks if the stream is initialized and ready for activation
213 * @return True if the stream is open, false otherwise
214 */
215 [[nodiscard]] virtual bool is_open() const = 0;
216
217 /**
218 * @brief Sets the function to process audio data
219 * @param processCallback Function that handles audio data processing
220 *
221 * Registers a callback function that will be invoked by the audio system
222 * when new input data is available or output data is needed. The callback
223 * receives pointers to input and output buffers along with the frame count.
224 */
225 virtual void set_process_callback(std::function<int(void*, void*, unsigned int)> processCallback) = 0;
226};
227
228/**
229 * @class AudioBackendFactory
230 * @brief Factory pattern implementation for audio backend instantiation
231 *
232 * Creates concrete implementations of the IAudioBackend interface based on
233 * the requested backend type. Centralizes backend creation logic and enables
234 * runtime selection of appropriate audio subsystem implementations.
235 */
237public:
238 /**
239 * @brief Creates a specific audio backend implementation
240 * @param type Identifier for the requested backend type
241 * @param api_preference Optional preference for a specific audio API
242 * @return Unique pointer to an IAudioBackend implementation
243 *
244 * Instantiates and configures the appropriate backend implementation
245 * based on the specified type, abstracting the details of backend
246 * selection and initialization.
247 */
248 static std::unique_ptr<IAudioBackend> create_backend(
250};
251}
static std::unique_ptr< IAudioBackend > create_backend(Core::AudioBackendType type)
Creates a specific audio backend implementation.
Factory pattern implementation for audio backend instantiation.
virtual std::vector< DeviceInfo > get_output_devices() const =0
Retrieves information about all available output devices.
virtual unsigned int get_default_output_device() const =0
Gets the system's primary output device identifier.
virtual ~AudioDevice()=default
Virtual destructor for proper cleanup of derived classes.
virtual unsigned int get_default_input_device() const =0
Gets the system's primary input device identifier.
virtual std::vector< DeviceInfo > get_input_devices() const =0
Retrieves information about all available input devices.
Manages audio endpoint discovery and enumeration.
virtual void pause()=0
Temporarily suspends audio processing without releasing resources.
virtual void start()=0
Activates the audio stream and begins data transfer.
virtual bool is_running() const =0
Checks if the stream is actively processing audio data.
virtual void close()=0
Terminates the audio stream and releases all resources.
virtual ~AudioStream()=default
Virtual destructor for proper cleanup of derived classes.
virtual void open()=0
Initializes the audio stream and allocates required resources.
virtual void set_process_callback(std::function< int(void *, void *, unsigned int)> processCallback)=0
Sets the function to process audio data.
virtual void stop()=0
Deactivates the audio stream and halts data transfer.
virtual bool is_open() const =0
Checks if the stream is initialized and ready for activation.
virtual void resume()=0
Resumes audio processing after a pause.
Manages digital audio data flow between the engine and hardware.
virtual std::unique_ptr< AudioDevice > create_device_manager()=0
Creates a device manager for audio endpoint discovery.
virtual std::string get_version_string() const =0
Retrieves the backend implementation version.
virtual std::unique_ptr< AudioStream > create_stream(unsigned int output_device_id, unsigned int input_device_id, GlobalStreamInfo &stream_info, void *user_data)=0
Creates an audio stream for a specific device.
virtual ~IAudioBackend()=default
Virtual destructor for proper cleanup of derived classes.
virtual void cleanup()=0
Releases all resources held by the backend.
Interface for audio system abstraction layer.
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.