utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
audio_manager.hpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22#pragma once
23
24#include <atomic>
25#include <memory>
26#include <unordered_map>
27#include <thread>
28#include <mutex>
29#include <deque>
30
31#include <AL/al.h>
32#include <AL/alc.h>
33
34#include "utility/sound/audio_source.hpp"
35
36#include "utility/math/vector.hpp"
37
40
41namespace utility::sound
42{
43
53 enum class AudioCommandType {
54 Play,
55 Stop,
56 Pause,
57 SetPosition,
58 SetGain,
59 SetPitch,
60 SetLooping,
61 CreateSource,
62 SetBuffer,
63 DestroySource
64 };
65
88
92 float gain;
93
97 float pitch;
98
103
107 ALuint bufferID;
108 };
109
123 AudioCommandType type;
124
128 uint32_t sourceID;
129
139 };
140
162 protected utility::logging::Loggable<AudioManager,
163 utility::logging::DefaultLogger>
164 {
165 public:
176 AudioManager();
177
185
194 void submitCommand(const AudioCommand &command);
195
206 std::unique_ptr<AudioSource>
207 createAudioSource(std::shared_ptr<AudioBuffer> buffer);
208
218 void destroyAudioSource(uint32_t sourceID);
219
223 void stop();
224
230 bool isRunning() const;
231
232 private:
238 void threadLoop();
239
243 void processCommands();
244
250 void executeCommand(const AudioCommand &command);
251
255 ALCdevice *_device = nullptr;
256
260 ALCcontext *_context = nullptr;
261
265 std::unordered_map<uint32_t, ALuint> _sources;
266
270 std::deque<AudioCommand> _commandQueue;
271
275 std::mutex _commandQueueMutex;
276
280 std::thread _audioThread;
281
285 std::mutex _sourcesMutex;
286
290 uint32_t _nextSourceID = 1;
291
298 std::atomic<bool> _running { true };
299
300 using Loggable::getLogger;
301 };
302} // namespace utility::sound
Mixin base class providing logging capabilities to derived classes.
Definition loggable.hpp:55
3D vector class inheriting from glm::vec3.
Definition vector.hpp:83
Central manager responsible for OpenAL initialization and audio resource management.
bool isRunning() const
Indicates whether the audio manager is running.
AudioManager()
Creates and initializes the audio subsystem.
void stop()
Requests termination of the audio thread.
~AudioManager()
Stops the audio thread and releases OpenAL resources.
void submitCommand(const AudioCommand &command)
Submits an audio command for asynchronous execution.
std::unique_ptr< AudioSource > createAudioSource(std::shared_ptr< AudioBuffer > buffer)
Creates a new audio source.
void destroyAudioSource(uint32_t sourceID)
Destroys an existing audio source.
Platform-specific default logger alias.
Mixin class to provide logging capabilities to derived classes.
Represents a command submitted to the audio thread.
CommandData data
Command-specific payload.
uint32_t sourceID
Identifier of the target audio source.
AudioCommandType type
Type of operation to execute.
Payload associated with an AudioCommand.
float gain
Gain multiplier applied to the source.
math::Vector3F position
New world-space position of the audio source.
float pitch
Playback pitch multiplier.
ALuint bufferID
OpenAL buffer identifier.
bool looping
Indicates whether playback should loop.