VR-Engage  2.2
Loading...
Searching...
No Matches
soundscape.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file soundscape.h
7//! \brief Defines a soundscape system for managing ambient and periodic sounds
8//!
9//! The DtSoundscape class manages ambient background sounds and periodic sound
10//! effects for creating immersive audio environments. It supports varying
11//! sound volumes, pitches, and positions over time according to configured
12//! parameters loaded from an initialization file.
13
14#pragma once
15
17#include <makAudio/abstractAudio.h>
18
19#include <matrix/vlVector.h>
20
21#include <string>
22#include <vector>
23
24namespace makAudio
25{
26//! Forward declaration of DtSound class from makAudio namespace
27class DtSound;
28} // namespace makAudio
29
30namespace makVre
31{
32//! Forward declaration of initialization table class
33class DtInitTable;
34
35//! \brief A class for managing ambient and periodic sound effects
36//!
37//! DtSoundscape loads and manages a collection of background ambient sounds
38//! and periodic sound effects from a configuration file. Background sounds
39//! play continuously with varying volume and pitch, while periodic sounds
40//! play at random intervals from random positions within a specified range.
41//! This provides a dynamic audio environment for immersive simulations.
42class PLAYERSTATION_DLL DtSoundscape : public makAudio::DtAbstractAudio
43{
44public:
45 //! \brief Constructor that loads a soundscape from a configuration file
46 //! \param filename The path to the soundscape configuration file
47 //!
48 //! Loads background and periodic sound definitions from the specified
49 //! configuration file, preparing them for playback when play() is called.
50 DtSoundscape(std::string filename);
51
52 //! \brief Destructor that cleans up all sound resources
53 //!
54 //! Stops all sounds and releases allocated resources for
55 //! background and periodic sounds.
56 virtual ~DtSoundscape() override;
57
58 //! \brief Starts playing all background and periodic sounds
59 //! \return True if playback started successfully
60 //!
61 //! Plays all background sounds immediately and begins the timer
62 //! for periodic sounds. Also registers this soundscape for updates
63 //! with the audio manager.
64 virtual bool play() override;
65
66 //! \brief Pauses all currently playing sounds
67 //! \return True if all sounds were paused successfully
68 //!
69 //! Pauses all background and periodic sounds and stops updates
70 //! from the audio manager.
71 virtual bool pause() override;
72
73 //! \brief Stops all sounds and resets them
74 //! \return True if all sounds were stopped successfully
75 //!
76 //! Stops all background and periodic sounds and unregisters this
77 //! soundscape from the audio manager.
78 virtual bool stop() override;
79
80 //! \brief Sets the master volume multiplier for all sounds
81 //! \param vol The volume multiplier (gain) to apply
82 //!
83 //! Sets the master volume for all sounds in the soundscape.
84 //! Individual sound volumes will be multiplied by this value.
85 virtual void setGain(float vol) override;
86
87protected:
88 //! \brief Updates all sounds in the soundscape
89 //! \param dt The time elapsed since the last update in seconds
90 //!
91 //! Called by the audio manager to update the state of all background
92 //! and periodic sounds, applying volume and pitch variations and
93 //! triggering periodic sounds as needed.
94 virtual void update(double dt) override;
95
96 //! \brief Loads a background sound configuration from initialization data
97 //! \param data The initialization table containing background sound parameters
98 //!
99 //! Creates and configures a new background sound that plays continuously
100 //! with varying volume and pitch parameters defined in the data table.
102
103 //! \brief Loads a periodic sound configuration from initialization data
104 //! \param data The initialization table containing periodic sound parameters
105 //!
106 //! Creates and configures a new periodic sound that plays at random intervals
107 //! from random positions within a specified range.
109
110 //! \brief A structure for managing a value that varies over time
111 //!
112 //! Handles automatic variation of a floating point value (like volume or pitch)
113 //! within a specified range and over varying durations.
114 struct Varying
115 {
116 //! \brief Current value of the parameter
118
119 //! \brief Time remaining until next value change (seconds)
120 float duration;
121
122 //! \brief Rate of change per second toward target value
123 float rate;
124
125 //! \brief Minimum allowed value
126 float minVal;
127
128 //! \brief Maximum allowed value
129 float maxVal;
130
131 //! \brief Minimum time between value changes (seconds)
132 float minTime;
133
134 //! \brief Maximum time between value changes (seconds)
135 float maxTime;
136
137 //! \brief Resets the variation with a new random target value and duration
138 //!
139 //! Selects a new random target value between minVal and maxVal and
140 //! calculates the rate of change needed to reach that value over a
141 //! random duration between minTime and maxTime.
142 void reset();
143
144 //! \brief Updates the current value based on elapsed time
145 //! \param dt Time elapsed since last update in seconds
146 //!
147 //! Applies the current rate of change to the current value and
148 //! resets if the duration has elapsed.
149 void update(double dt);
150 };
151
152 //! \brief A structure for managing a background ambient sound
153 //!
154 //! Represents a continuously playing ambient sound with varying
155 //! volume and pitch characteristics.
157 {
158 //! \brief Constructor that initializes with parent soundscape reference
159 //! \param soundscape Pointer to the parent soundscape
161 : mySoundscape(soundscape)
162 {
163 }
164
165 //! \brief The sound resource to play
166 makAudio::DtSound* sound;
167
168 //! \brief Volume variation controller
170
171 //! \brief Pitch variation controller
173
174 //! \brief Updates the sound's volume and pitch
175 //! \param dt Time elapsed since last update in seconds
176 //!
177 //! Applies current volume and pitch values to the sound and
178 //! ensures it continues playing.
179 void update(double dt);
180
181 //! \brief Reference to the parent soundscape for gain access
183 };
184
185 //! \brief A structure for managing a periodic sound effect
186 //!
187 //! Represents a sound that plays at random intervals from random
188 //! positions within a configurable range.
189 struct Periodic
190 {
191 //! \brief Constructor that initializes with parent soundscape reference
192 //! \param soundscape Pointer to the parent soundscape
194 : mySoundscape(soundscape)
195 {
196 }
197
198 //! \brief The sound resource to play periodically
199 makAudio::DtSound* sound;
200
201 //! \brief Minimum position coordinates for random placement
202 DtVector minRange;
203
204 //! \brief Maximum position coordinates for random placement
205 DtVector maxRange;
206
207 //! \brief Time remaining until next sound trigger (seconds)
208 float nextTime;
209
210 //! \brief Minimum delay between sound triggers (seconds)
211 float minDelay;
212
213 //! \brief Maximum delay between sound triggers (seconds)
214 float maxDelay;
215
216 //! \brief Minimum pitch for random pitch selection
217 float minPitch;
218
219 //! \brief Maximum pitch for random pitch selection
220 float maxPitch;
221
222 //! \brief Updates the timer and triggers the sound when appropriate
223 //! \param dt Time elapsed since last update in seconds
224 //!
225 //! Decrements the timer and when it reaches zero, plays the sound
226 //! at a random position with random pitch, then resets the timer.
227 void update(double dt);
228
229 //! \brief Reference to the parent soundscape for gain access
231 };
232
233 //! \brief Collection of all background sounds in this soundscape
234 std::vector<Background*> myBackgrounds;
235
236 //! \brief Collection of all periodic sounds in this soundscape
237 std::vector<Periodic*> myPeriodics;
238
239 //! \brief Path to the directory containing sound files
240 std::string mySoundscapePath;
241};
242
243} // namespace makVre
Table-based access to Lua state for configuration data.
Definition initializer.h:38
void loadPeriodic(DtInitTable &data)
Loads a periodic sound configuration from initialization data.
virtual bool play() override
Starts playing all background and periodic sounds.
void loadBackground(DtInitTable &data)
Loads a background sound configuration from initialization data.
virtual bool pause() override
Pauses all currently playing sounds.
virtual bool stop() override
Stops all sounds and resets them.
virtual void setGain(float vol) override
Sets the master volume multiplier for all sounds.
virtual void update(double dt) override
Updates all sounds in the soundscape.
DtSoundscape(std::string filename)
Constructor that loads a soundscape from a configuration file.
std::string mySoundscapePath
Path to the directory containing sound files.
Definition soundscape.h:240
std::vector< Periodic * > myPeriodics
Collection of all periodic sounds in this soundscape.
Definition soundscape.h:237
virtual ~DtSoundscape() override
Destructor that cleans up all sound resources.
std::vector< Background * > myBackgrounds
Collection of all background sounds in this soundscape.
Definition soundscape.h:234
Defines export macros for the VR-Engage Player Station library.
#define PLAYERSTATION_DLL
Definition export.h:24
Component configuration and usage details.
Definition audioUpdater.h:21
Include export definitions for this library.
Definition glsVreMessageUtil.h:49
makAudio::DtSound * sound
The sound resource to play.
Definition soundscape.h:166
DtSoundscape * mySoundscape
Reference to the parent soundscape for gain access.
Definition soundscape.h:182
void update(double dt)
Updates the sound's volume and pitch.
Background(DtSoundscape *soundscape)
Constructor that initializes with parent soundscape reference.
Definition soundscape.h:160
Varying volume
Volume variation controller.
Definition soundscape.h:169
Varying pitch
Pitch variation controller.
Definition soundscape.h:172
float minPitch
Minimum pitch for random pitch selection.
Definition soundscape.h:217
float maxDelay
Maximum delay between sound triggers (seconds)
Definition soundscape.h:214
float maxPitch
Maximum pitch for random pitch selection.
Definition soundscape.h:220
DtSoundscape * mySoundscape
Reference to the parent soundscape for gain access.
Definition soundscape.h:230
float minDelay
Minimum delay between sound triggers (seconds)
Definition soundscape.h:211
Periodic(DtSoundscape *soundscape)
Constructor that initializes with parent soundscape reference.
Definition soundscape.h:193
makAudio::DtSound * sound
The sound resource to play periodically.
Definition soundscape.h:199
DtVector maxRange
Maximum position coordinates for random placement.
Definition soundscape.h:205
void update(double dt)
Updates the timer and triggers the sound when appropriate.
DtVector minRange
Minimum position coordinates for random placement.
Definition soundscape.h:202
float nextTime
Time remaining until next sound trigger (seconds)
Definition soundscape.h:208
A structure for managing a value that varies over time.
Definition soundscape.h:115
void update(double dt)
Updates the current value based on elapsed time.
float rate
Rate of change per second toward target value.
Definition soundscape.h:123
float maxTime
Maximum time between value changes (seconds)
Definition soundscape.h:135
float minTime
Minimum time between value changes (seconds)
Definition soundscape.h:132
float duration
Time remaining until next value change (seconds)
Definition soundscape.h:120
float currentVal
Current value of the parameter.
Definition soundscape.h:117
float maxVal
Maximum allowed value.
Definition soundscape.h:129
void reset()
Resets the variation with a new random target value and duration.
float minVal
Minimum allowed value.
Definition soundscape.h:126