libdonut  2.3.2
Application framework for cross-platform game development in C++20
Sound.hpp
Go to the documentation of this file.
1 #ifndef DONUT_AUDIO_SOUND_HPP
2 #define DONUT_AUDIO_SOUND_HPP
3 
4 #include <donut/Filesystem.hpp>
5 #include <donut/UniqueHandle.hpp>
6 
7 #include <limits> // std::numeric_limits
8 
9 namespace donut::audio {
10 
14 enum class SoundAttenuationModel : unsigned {
20 
42 
60 
82 };
83 
87 struct SoundOptions {
97 
106  float volume = 1.0f;
107 
121  float minDistance = 1.0f;
122 
137  float maxDistance = std::numeric_limits<float>::max();
138 
155  float rolloffFactor = 1.0f;
156 
165  float dopplerFactor = 1.0f;
166 
175  bool useDistanceDelay = false;
176 
184  bool listenerRelative = false;
185 
189  bool looping = false;
190 
198  bool singleInstance = false;
199 };
200 
208 class Sound {
209 public:
230  Sound(const Filesystem& filesystem, const char* filepath, const SoundOptions& options = {});
231 
242  [[nodiscard]] void* get() const noexcept {
243  return buffer.get();
244  }
245 
246 private:
247  struct SourceDeleter {
248  void operator()(void* handle) const noexcept;
249  };
250 
251  using Source = UniqueHandle<void*, SourceDeleter>;
252 
253  Source buffer{};
254 };
255 
256 } // namespace donut::audio
257 
258 #endif
Persistent system for managing the virtual filesystem.
Definition: Filesystem.hpp:185
constexpr Handle get() const noexcept
Get the value of the underlying resource handle.
Definition: UniqueHandle.hpp:152
Container for a particular sound wave that can be played in a SoundStage.
Definition: Sound.hpp:208
void * get() const noexcept
Get an opaque handle to the internal representation of the sound.
Definition: Sound.hpp:242
Sound(const Filesystem &filesystem, const char *filepath, const SoundOptions &options={})
Load a sound from a virtual file.
Definition: Error.hpp:8
SoundAttenuationModel
Distance attenuation/falloff model for 3D positional audio.
Definition: Sound.hpp:14
@ INVERSE_DISTANCE
Attenuate the amplitude of the sound by the inverse distance between the sound instance and the liste...
@ EXPONENTIAL_DISTANCE
Attenuate the amplitude of the sound by the exponential distance between the sound instance and the l...
@ LINEAR_DISTANCE
Attenuate the amplitude of the sound by the linear distance between the sound instance and the listen...
@ NO_ATTENUATION
No distance attenuation; sound has the same volume regardless of distance between the sound instance ...
Configuration options for a Sound.
Definition: Sound.hpp:87
SoundAttenuationModel attenuationModel
Which distance attenuation/falloff model to use for 3D positional audio when playing this sound.
Definition: Sound.hpp:96
float volume
The default volume of instances of this sound, which is used if no volume override is specified when ...
Definition: Sound.hpp:106
float minDistance
The minimum distance of the range where the distance between the sound instance and listener changes ...
Definition: Sound.hpp:121
float rolloffFactor
The rolloff factor to use in the attenuation/falloff calculation for this sound.
Definition: Sound.hpp:155
bool looping
Play this sound on repeat instead of just playing it once.
Definition: Sound.hpp:189
float dopplerFactor
Strength of the doppler effect for this sound.
Definition: Sound.hpp:165
bool useDistanceDelay
Simulate the delay due to the speed of sound between the sound being played and the sound being heard...
Definition: Sound.hpp:175
float maxDistance
The maximum distance of the range where the distance between the sound instance and listener changes ...
Definition: Sound.hpp:137
bool singleInstance
Override any instances of this sound that are already playing when a new instance is played.
Definition: Sound.hpp:198
bool listenerRelative
Don't take the listener's sound stage position into account when playing this sound.
Definition: Sound.hpp:184