MayaFlux 0.3.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 Retrieves the backend API type identifier
91 * @return Integer code representing the backend type
92 */
93 [[nodiscard]] virtual int get_api_type() const = 0;
94
95 /**
96 * @brief Releases all resources held by the backend.
97 *
98 * Performs necessary cleanup operations before backend destruction,
99 * including releasing system resources and terminating background processes.
100 * This method should be called only before application termination to
101 * ensure proper resource deallocation and prevent memory leaks. It is not
102 * intended for general use and should not be called during normal
103 * application operation.
104 * NOTE: The nature of unique_ptr makes manual cleanup redundant. This only exists when it is necessary to
105 * take ownership of the cleanup, for example, when a different backend needs to be used
106 * or when Audio is no longer needed.
107 */
108 virtual void cleanup() = 0;
109};
110
111/**
112 * @class AudioDevice
113 * @brief Manages audio endpoint discovery and enumeration
114 *
115 * Provides access to the system's available audio I/O endpoints,
116 * abstracting the platform-specific device enumeration process and
117 * exposing a consistent interface for querying device capabilities.
118 */
120public:
121 /** @brief Virtual destructor for proper cleanup of derived classes */
122 virtual ~AudioDevice() = default;
123
124 /**
125 * @brief Retrieves information about all available output devices
126 * @return Vector of DeviceInfo structures for output endpoints
127 */
128 [[nodiscard]] virtual std::vector<DeviceInfo> get_output_devices() const = 0;
129
130 /**
131 * @brief Retrieves information about all available input devices
132 * @return Vector of DeviceInfo structures for input endpoints
133 */
134 [[nodiscard]] virtual std::vector<DeviceInfo> get_input_devices() const = 0;
135
136 /**
137 * @brief Gets the system's primary output device identifier
138 * @return Device ID for the default output endpoint
139 */
140 [[nodiscard]] virtual unsigned int get_default_output_device() const = 0;
141
142 /**
143 * @brief Gets the system's primary input device identifier
144 * @return Device ID for the default input endpoint
145 */
146 [[nodiscard]] virtual unsigned int get_default_input_device() const = 0;
147};
148
149/**
150 * @class AudioStream
151 * @brief Manages digital audio data flow between the engine and hardware
152 *
153 * Handles the real-time bidirectional transfer of audio sample data between
154 * the processing engine and system audio endpoints. Provides lifecycle management
155 * for audio streams and callback-based processing integration.
156 */
158public:
159 /** @brief Virtual destructor for proper cleanup of derived classes */
160 virtual ~AudioStream() = default;
161
162 /**
163 * @brief Initializes the audio stream and allocates required resources
164 *
165 * Prepares the stream for data transfer by establishing connections
166 * to the hardware and allocating necessary buffers, but does not start
167 * the actual data flow.
168 */
169 virtual void open() = 0;
170
171 /**
172 * @brief Activates the audio stream and begins data transfer
173 *
174 * Initiates the real-time processing of audio data, enabling the flow
175 * of samples between the application and hardware endpoints.
176 */
177 virtual void start() = 0;
178
179 /**
180 * @brief Deactivates the audio stream and halts data transfer
181 *
182 * Suspends the real-time processing of audio data while maintaining
183 * the stream's configuration and resource allocations.
184 */
185 virtual void stop() = 0;
186
187 /**
188 * @brief Terminates the audio stream and releases all resources
189 *
190 * Completely shuts down the audio stream, releasing all allocated
191 * resources and disconnecting from hardware endpoints.
192 */
193 virtual void close() = 0;
194
195 /**
196 * @brief Checks if the stream is actively processing audio data
197 * @return True if the stream is currently active, false otherwise
198 */
199 [[nodiscard]] virtual bool is_running() const = 0;
200
201 /**
202 * @brief Checks if the stream is initialized and ready for activation
203 * @return True if the stream is open, false otherwise
204 */
205 [[nodiscard]] virtual bool is_open() const = 0;
206
207 /**
208 * @brief Sets the function to process audio data
209 * @param processCallback Function that handles audio data processing
210 *
211 * Registers a callback function that will be invoked by the audio system
212 * when new input data is available or output data is needed. The callback
213 * receives pointers to input and output buffers along with the frame count.
214 */
215 virtual void set_process_callback(std::function<int(void*, void*, unsigned int)> processCallback) = 0;
216};
217
218/**
219 * @class AudioBackendFactory
220 * @brief Factory pattern implementation for audio backend instantiation
221 *
222 * Creates concrete implementations of the IAudioBackend interface based on
223 * the requested backend type. Centralizes backend creation logic and enables
224 * runtime selection of appropriate audio subsystem implementations.
225 */
227public:
228 /**
229 * @brief Creates a specific audio backend implementation
230 * @param type Identifier for the requested backend type
231 * @param api_preference Optional preference for a specific audio API
232 * @return Unique pointer to an IAudioBackend implementation
233 *
234 * Instantiates and configures the appropriate backend implementation
235 * based on the specified type, abstracting the details of backend
236 * selection and initialization.
237 */
238 static std::unique_ptr<IAudioBackend> create_backend(
240 std::optional<GlobalStreamInfo::AudioApi> api_preference = std::nullopt);
241};
242}
static std::unique_ptr< IAudioBackend > create_backend(Core::AudioBackendType type, std::optional< GlobalStreamInfo::AudioApi > api_preference=std::nullopt)
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 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.
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 int get_api_type() const =0
Retrieves the backend API type identifier.
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.