VR-Engage  2.2
Loading...
Searching...
No Matches
audioUpdater.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file audioUpdater.h
7//! \brief Defines the DtAudioUpdater component that plays audio sounds based on state attributes.
8//!
9//! This component manages audio playback for entities, allowing sounds to be triggered
10//! by boolean state attributes in the VR environment.
11//!
12//! \ingroup vreCommonComponents
13
14#pragma once
15
17
19
20namespace makAudio
21{
22class DtSound;
23}
24
26{
27//! \ingroup RoleConfigParams
28//! \defgroup AudioUpdaterRoleParams Audio Updater Role Parameters
29//! \roleInherits{PlayerComponentRoleParams}
30//! Configuration Options for audio updater role.
31//! @{
32
33//! \vreRoleParamRequired{audio,string,audioUpdater,"example.wav",Filename of the audio clip for a single source entry inside the sources table (resolved relative to audioPath).}
34constexpr const char* audio = "audio";
35
36//! \vreRoleParam{audioPath,string,"$(SHARED_DATA_DIR)/Audio/",audioUpdater,Base path for locating audio files used by configured sources.}
37constexpr const char* audioPath = "audioPath";
38
39//! \vreRoleParam{decibels,float,80.0,audioUpdater,Default decibel level applied to an audio source if not explicitly specified in its entry.}
40constexpr const char* decibels = "decibels";
41
42//! \vreRoleParam{gain,float,1.0,audioUpdater,Default gain multiplier applied to an audio source if not explicitly specified in its entry.}
43constexpr const char* gain = "gain";
44
45//! \vreRoleParam{is3d,bool,true,audioUpdater,Whether an audio source is spatialized in 3D (position follows the entity) by default.}
46constexpr const char* is3d = "is3d";
47
48//! \vreRoleParam{looping,bool,true,audioUpdater,Whether an audio source should loop continuously when enabled by default.}
49constexpr const char* looping = "looping";
50
51//! \vreRoleParam{sources,table,"",audioUpdater,Table (list) of per-sound source configuration entries defining audio playback behavior.}
52constexpr const char* sources = "sources";
53
54//! \vreRoleParam{stateAttribute,string,"",audioUpdater,Name of the boolean state attribute that enables playback for a source entry (empty string means always enabled).}
55constexpr const char* stateAttribute = "stateAttribute";
56
57//! @}
58} // namespace AudioUpdaterConfig
59
60namespace makVre
61{
62//! \brief Type identifier for DtAudioUpdater component
63constexpr const char* DtAudioUpdaterType = "DtAudioUpdater";
64
65//! \brief Component for playing audio sounds based on entity state attributes
66//!
67//! The DtAudioUpdater plays audio sounds for the engaged entity based on the value
68//! of boolean state attributes. Each configured sound is associated with a state attribute
69//! and will play when that attribute is true. Sounds can be configured to loop, play at
70//! specific loudness levels, and be positioned in 3D space at the entity's location.
71//!
72//! Note that entities that are destroyed will not play sounds, regardless of state attributes.
73//!
74//! Example Lua configuration:
75//! \code
76//! ["sirenAudioUpdater"] = {
77//! componentType = "DtAudioUpdater";
78//! priority = 10;
79//! audioPath = "$(SHARED_DATA_DIR)\\Audio\\sirens\\";
80//! sources = {
81//! { audio = "siren-1-fast-loop.wav"; stateAttribute = "siren-fast"; -- [[ default values:looping = true;
82//! gain = 1.0; decibels = 80.0; is3d = true; ]] }; { audio = "siren-1-slow-loop.wav"; stateAttribute =
83//! "siren-slow"; -- [[ default values:looping = true; gain = 1.0; decibels = 80.0; is3d = true; ]] }; { audio
84//! = "siren-airhorn-single.wav"; stateAttribute = "siren-horn"; looping = false; -- [[ default values:gain
85//! = 1.0; decibels = 80.0; is3d = true;]] }; { audio = "siren-piercer-loop.wav"; stateAttribute =
86//! "siren-piercer"; -- [[ default values:looping = true; gain = 1.0; decibels = 80.0; is3d = true;]] };
87//! };
88//! };
89//! \endcode
91{
92public:
93 //! \brief Constructor for DtAudioUpdater
94 //!
95 //! Initializes the component with the name "DtAudioUpdater".
97
98 //! \brief Virtual destructor for DtAudioUpdater
99 //!
100 //! Ensures proper cleanup of audio resources when the component is destroyed.
101 virtual ~DtAudioUpdater() override;
102
103 //! \brief Initializes the component with configuration from Lua
104 //!
105 //! Sets up audio sources based on the provided configuration. Each source is configured
106 //! with its associated state attribute, looping behavior, gain, decibel level, and 3D positioning.
107 //!
108 //! \param player Pointer to the player station this component belongs to
109 //! \param config Configuration table containing initialization parameters, especially the sources table
110 //! \return True if initialization succeeds, false otherwise
111 virtual bool initialize(DtPlayerStation* player, DtInitTable& config) override;
112
113 //! \brief Updates the audio playback based on state attributes
114 //!
115 //! Called each frame to manage audio playback. For each configured sound, checks its
116 //! associated state attribute and plays/stops the sound accordingly. For 3D sounds,
117 //! updates their position to match the entity's current location.
118 //!
119 //! For non-looping sounds, the component will automatically set the state attribute
120 //! to false when the sound finishes playing.
121 //!
122 //! \param dt Delta time in seconds since the last tick
123 virtual void tick(double dt) override;
124
125 //! \brief Shuts down the component
126 //!
127 //! Cleans up all audio resources by stopping and deleting sounds.
128 virtual void shutdown() override;
129
130 //! \brief Returns the component type identifier
131 //! \return The component type string
132 virtual const char* type() const override;
133
134protected:
135 //! \brief Structure for managing sound resources and their associated state attributes
136 struct Sound
137 {
138 //! \brief Name of the state attribute that controls this sound
139 //!
140 //! When this attribute is true, the sound will play. If empty, the sound is always enabled.
141 std::string myStateVar;
142
143 //! \brief Pointer to the audio source for this sound
144 makAudio::DtSound* mySource;
145
146 //! \brief Flag to track if a non-looping sound has been triggered
147 //!
148 //! Used to prevent repeatedly triggering non-looping sounds that are still playing.
150
151 //! \brief Default constructor initializing member variables
153 : myStateVar("")
154 , mySource(0)
155 , myTriggered(false)
156 {
157 }
158 };
159
160 //! \brief Collection of sounds managed by this component
161 std::vector<Sound> mySounds;
162};
163} // namespace makVre
std::vector< Sound > mySounds
Collection of sounds managed by this component.
Definition audioUpdater.h:161
virtual ~DtAudioUpdater() override
Virtual destructor for DtAudioUpdater.
virtual void tick(double dt) override
Updates the audio playback based on state attributes.
virtual const char * type() const override
Returns the component type identifier.
virtual bool initialize(DtPlayerStation *player, DtInitTable &config) override
Initializes the component with configuration from Lua.
virtual void shutdown() override
Shuts down the component.
DtAudioUpdater()
Constructor for DtAudioUpdater.
Table-based access to Lua state for configuration data.
Definition initializer.h:38
virtual DtPlayerStation & player()
Gets the player station.
DtPlayerComponent()
Constructor.
Class for managing the user interface for engaged roles.
Definition playerStation.h:51
constexpr const char * audio
Definition audioUpdater.h:34
constexpr const char * sources
Definition audioUpdater.h:52
constexpr const char * looping
Definition audioUpdater.h:49
constexpr const char * is3d
Definition audioUpdater.h:46
constexpr const char * audioPath
Definition audioUpdater.h:37
constexpr const char * stateAttribute
Definition audioUpdater.h:55
constexpr const char * gain
Definition audioUpdater.h:43
constexpr const char * decibels
Definition audioUpdater.h:40
Defines export/import macros for the vreCommonComponents library.
#define VRECOMMONCOMPONENTS_DLL
DLL export/import macro for the vreCommonComponents library.
Definition export.h:28
Definition audioUpdater.h:26
Component configuration and usage details.
Definition audioUpdater.h:21
Include export definitions for this library.
Definition glsVreMessageUtil.h:49
constexpr const char * DtAudioUpdaterType
Type identifier for DtAudioUpdater component.
Definition audioUpdater.h:63
Defines the DtPlayerComponent base class for VR-Engage role components.
makAudio::DtSound * mySource
Pointer to the audio source for this sound.
Definition audioUpdater.h:144
bool myTriggered
Flag to track if a non-looping sound has been triggered.
Definition audioUpdater.h:149
std::string myStateVar
Name of the state attribute that controls this sound.
Definition audioUpdater.h:141
Sound()
Default constructor initializing member variables.
Definition audioUpdater.h:152