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