libdonut  2.3.2
Application framework for cross-platform game development in C++20
SoundStage.hpp
Go to the documentation of this file.
1 #ifndef DONUT_AUDIO_SOUND_STAGE_HPP
2 #define DONUT_AUDIO_SOUND_STAGE_HPP
3 
4 #include <donut/Time.hpp>
5 #include <donut/UniqueHandle.hpp>
7 #include <donut/math.hpp>
8 
9 namespace donut::audio {
10 
11 class Sound; // Forward declaration, to avoid including Sound.hpp.
12 
24  float volume = 1.0f;
25 
36  float speedOfSound = 343.3f;
37 
43  unsigned maxSimultaneousSounds = 32u;
44 };
45 
56 class SoundStage {
57 public:
61  using SoundInstanceId = unsigned;
62 
77  explicit SoundStage(const SoundStageOptions& options = {});
78 
91  void update(Time<float> deltaTime, const Listener& listener);
92 
112  SoundInstanceId playSound(const Sound& sound, float volume = -1.0f, vec3 position = {0.0f, 0.0f, 0.0f}, vec3 velocity = {0.0f, 0.0f, 0.0f});
113 
128  SoundInstanceId playSoundInBackground(const Sound& sound, float volume = -1.0f);
129 
145  SoundInstanceId createPausedSoundInBackground(const Sound& sound, float volume = -1.0f);
146 
160  [[nodiscard]] bool isSoundPaused(SoundInstanceId id) const noexcept;
161 
174  [[nodiscard]] bool isSoundStopped(SoundInstanceId id) const noexcept;
175 
189 
204 
218 
234  void scheduleSoundStop(SoundInstanceId id, Time<float> timePointInSound);
235 
252  void scheduleSoundPause(SoundInstanceId id, Time<float> timePointInSound);
253 
267  void seekToSoundTime(SoundInstanceId id, Time<float> timePointInSound);
268 
285  void setSoundPosition(SoundInstanceId id, vec3 newPosition);
286 
302  void setSoundVelocity(SoundInstanceId id, vec3 newVelocity);
303 
322  void setSoundPositionAndVelocity(SoundInstanceId id, vec3 newPosition, vec3 newVelocity);
323 
337  void setSoundVolume(SoundInstanceId id, float volume);
338 
353  void fadeSoundVolume(SoundInstanceId id, float targetVolume, float fadeDuration);
354 
371  void setSoundPlaybackSpeed(SoundInstanceId id, float playbackSpeed);
372 
388  void fadeSoundPlaybackSpeed(SoundInstanceId id, float targetPlaybackSpeed, float fadeDuration);
389 
398  void setVolume(float volume);
399 
410  void fadeVolume(float targetVolume, float fadeDuration);
411 
418  void setSpeedOfSound(float speedOfSound);
419 
427  void setMaxSimultaneousSounds(unsigned maxSimultaneousSounds);
428 
429 private:
430  struct EngineDeleter {
431  void operator()(void* handle) const noexcept;
432  };
433 
435  Time<float> time{};
436 };
437 
438 } // namespace donut::audio
439 
440 #endif
Persistent system for playing sound in a simulated 3D arena to the default audio device.
Definition: SoundStage.hpp:56
void scheduleSoundStop(SoundInstanceId id, Time< float > timePointInSound)
Schedule for a specific sound instance to stop playing and remove itself when the playback reaches a ...
void setSoundVolume(SoundInstanceId id, float volume)
Set the volume of a specific sound instance.
bool isSoundStopped(SoundInstanceId id) const noexcept
Check if a specific sound instance has finished playing.
void fadeSoundVolume(SoundInstanceId id, float targetVolume, float fadeDuration)
Fade the volume of a specific sound instance towards a target volume over a given duration.
SoundInstanceId createPausedSoundInBackground(const Sound &sound, float volume=-1.0f)
Create a new sound instance without any panning, but don't start playing it yet.
void pauseSound(SoundInstanceId id)
Pause a specific sound instance.
void fadeVolume(float targetVolume, float fadeDuration)
Fade the global master volume of the sound stage towards a target volume over a given duration.
void scheduleSoundPause(SoundInstanceId id, Time< float > timePointInSound)
Schedule for a specific sound instance to pause itself when the playback reaches a specific time poin...
void update(Time< float > deltaTime, const Listener &listener)
Update the 3D parameters of the sound stage and advance the current time.
void setSoundPosition(SoundInstanceId id, vec3 newPosition)
Set the current 3D position of a specific sound instance.
void resumeSound(SoundInstanceId id)
Unpause and resume a specific sound instance.
void fadeSoundPlaybackSpeed(SoundInstanceId id, float targetPlaybackSpeed, float fadeDuration)
Fade the relative playback speed of a specific sound instance towards a target relative playback spee...
SoundInstanceId playSoundInBackground(const Sound &sound, float volume=-1.0f)
Create a new sound instance without any panning and start playing it.
unsigned SoundInstanceId
Opaque handle to a specific instance of a sound in the sound stage.
Definition: SoundStage.hpp:61
bool isSoundPaused(SoundInstanceId id) const noexcept
Check if a specific sound instance is currently paused.
void setSpeedOfSound(float speedOfSound)
Set the speed of sound in the sound stage.
void setSoundVelocity(SoundInstanceId id, vec3 newVelocity)
Set the current 3D velocity of a specific sound instance.
void stopSound(SoundInstanceId id)
Stop a specific sound instance and remove it.
SoundInstanceId playSound(const Sound &sound, float volume=-1.0f, vec3 position={0.0f, 0.0f, 0.0f}, vec3 velocity={0.0f, 0.0f, 0.0f})
Create a new 3D sound instance and start playing it.
void seekToSoundTime(SoundInstanceId id, Time< float > timePointInSound)
Set the current playback time point for a specific sound instance.
void setSoundPositionAndVelocity(SoundInstanceId id, vec3 newPosition, vec3 newVelocity)
Set both the 3D position and 3D velocity of a specific sound instance at the same time.
void setVolume(float volume)
Set the global master volume of the sound stage.
void setSoundPlaybackSpeed(SoundInstanceId id, float playbackSpeed)
Set the relative playback speed of a specific sound instance.
SoundStage(const SoundStageOptions &options={})
Construct a sound stage.
void setMaxSimultaneousSounds(unsigned maxSimultaneousSounds)
Set the maximum total number of sound instances that can play simultaneously.
Container for a particular sound wave that can be played in a SoundStage.
Definition: Sound.hpp:208
Definition: Error.hpp:8
Current state of the sound listener, i.e.
Definition: Listener.hpp:15
Configuration options for a SoundStage.
Definition: SoundStage.hpp:16
float volume
Global master volume.
Definition: SoundStage.hpp:24
float speedOfSound
The speed of sound in the sound stage.
Definition: SoundStage.hpp:36
unsigned maxSimultaneousSounds
The maximum total number of sound instances that can play simultaneously.
Definition: SoundStage.hpp:43