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_source.cpp
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#include "utility/sound/audio_source.hpp"
23#include "utility/sound/audio_manager.hpp"
24
26 AudioManager &manager)
27 : _sourceID(sourceID)
28 , _manager(manager)
29{
30}
31
33{
34 _manager.destroyAudioSource(_sourceID);
35}
36
38{
39 _isPlaying = true;
40 _manager.submitCommand({ AudioCommandType::Play, _sourceID });
41}
42
44{
45 _isPlaying = false;
46 _manager.submitCommand({ AudioCommandType::Pause, _sourceID });
47}
48
50{
51 _isPlaying = false;
52 _manager.submitCommand({ AudioCommandType::Stop, _sourceID });
53}
54
55void utility::sound::AudioSource::setBuffer(std::shared_ptr<AudioBuffer> buffer)
56{
57 _buffer = std::move(buffer);
58
59 AudioCommand command { AudioCommandType::SetBuffer, _sourceID, {} };
60 command.data.bufferID = _buffer->bufferID();
61 _manager.submitCommand(command);
62}
63
65{
66 _looping = loop;
67 AudioCommand command { AudioCommandType::SetLooping, _sourceID, {} };
68 command.data.looping = loop;
69 _manager.submitCommand(command);
70}
71
73{
74 _pitch = pitch;
75 AudioCommand command { AudioCommandType::SetPitch, _sourceID, {} };
76 command.data.pitch = pitch;
77 _manager.submitCommand(command);
78}
79
81{
82 _gain = gain;
83 AudioCommand command { AudioCommandType::SetGain, _sourceID, {} };
84 command.data.gain = gain;
85 _manager.submitCommand(command);
86}
87
89{
90 return _sourceID;
91}
Central manager responsible for OpenAL initialization and audio resource management.
void setPitch(float pitch)
Sets the playback pitch.
void stop()
Stops audio playback.
void setGain(float gain)
Sets the playback gain.
void setBuffer(std::shared_ptr< AudioBuffer > buffer)
Assigns an audio buffer to this source.
void play()
Starts or resumes audio playback.
void pause()
Pauses audio playback.
uint32_t sourceID() const
Returns the unique identifier of this audio source.
~AudioSource()
Destroys the audio source.
AudioSource(uint32_t sourceID, AudioManager &manager)
Creates a new audio source.
void setLooping(bool loop)
Enables or disables looping playback.
Represents a command submitted to the audio thread.
CommandData data
Command-specific payload.
float gain
Gain multiplier applied to the source.
float pitch
Playback pitch multiplier.
ALuint bufferID
OpenAL buffer identifier.
bool looping
Indicates whether playback should loop.