VR-Engage  2.2
Loading...
Searching...
No Matches
engineAudioUpdater.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file engineAudioUpdater.h
7//! \brief Defines the DtEngineAudioUpdater component for simulating vehicle engine sounds.
8//!
9//! This component provides dynamic audio simulation for vehicle engines, modulating sound pitch
10//! and volume based on engine RPM or speed values from state attributes.
11//!
12//! \ingroup vreCommonComponents
13
14#pragma once
15
18
19#include <vector>
20
21//! \brief Component configuration and usage details
22//!
23//! The DtEngineAudioUpdater plays audio sounds for the engaged entity based on the value
24//! of a floating point state attribute (typically RPM or speed). The state attribute value
25//! modulates the pitch and gain of the sound to create realistic engine effects.
26//!
27//! Features:
28//! - Uses state attributes (RPM or speed) to control sound pitch and volume
29//! - Applies smoothing to prevent jitter in pitch changes
30//! - Controls multiple engine sounds with different characteristics
31//! - Positions sounds at the entity's location for 3D audio effects
32//! - Supports engine on/off state via a boolean attribute
33//!
34//! Entities need to have a boolean state attribute to represent the engine being on/off.
35//! The default attribute name is "engine-on" but can be changed in the configuration.
36//! If this attribute is false, engine audio sounds will not play.
37//!
38//! \par Example Configuration:
39//! \code
40//! ["engineAudio"] = {
41//! componentType = "DtEngineAudioUpdater";
42//! priority = 10;
43//! audioPath = "$(DATA_DIR)\\audio\\sounds\\";
44//! engineOnAttribute = "engine-on"; -- can also use "engineOnName" for compatibility
45//! sources = {
46//! {
47//! audioFile = "tankEngine.wav", -- can also use "audio" for compatibility
48//! rpmAttribute = "rpm", -- can also use "stateAttribute" for compatibility
49//! rpmMax = 3100, -- can also use "maxValue" for compatibility
50//! rpmSmoothing = 0.999, -- can also use "lowpassAlpha" (1.0 - smoothing) for compatibility
51//! minPitch = 0.1, -- minimum pitch scaling (default: 0.1)
52//! maxPitch = 1.0, -- maximum pitch scaling (default: 1.0)
53//! gain = 0.75, -- volume multiplier (default: 1.0)
54//! decibels = 70.0 -- sound level in decibels (default: 80.0)
55//! },
56//! {
57//! audioFile = "tank.wav",
58//! rpmAttribute = "speed",
59//! rpmMax = 45,
60//! rpmSmoothing = 0.925,
61//! decibels = 80.0
62//! -- gain defaults to 1.0
63//! }
64//! };
65//! };
66//! \endcode
67
68namespace makAudio
69{
70//! \brief Forward declaration of the audio sound class
71class DtSound;
72} // namespace makAudio
73
75{
76//! \ingroup RoleConfigParams
77//! \defgroup EngineAudioUpdaterRoleParams Engine Audio Updater Role Parameters
78//! \roleInherits{PlayerComponentRoleParams}
79//! Configuration Options for engine audio updater role.
80//! @{
81
82//! \vreRoleParam{audioPath,string,"$(SHARED_DATA_DIR)/Audio/",engineAudioUpdater,Base path for locating audio files.}
83constexpr const char* audioPath = "audioPath";
84
85//! \vreRoleParam{engineOnAttribute,string,"engine-on",engineAudioUpdater,Name of the boolean state attribute that controls engine on/off state.}
86constexpr const char* engineOnAttribute = "engineOnAttribute";
87
88//! \vreRoleParam{engineOnName,string,"engine-on",engineAudioUpdater,Legacy name for the engine on/off attribute (use engineOnAttribute instead).}
89constexpr const char* engineOnName = "engineOnName";
90
91//! \vreRoleParam{sources,table,"",engineAudioUpdater,Table of engine audio sources with RPM-based modulation settings.}
92constexpr const char* sources = "sources";
93
94//! @}
95} // namespace EngineAudioUpdaterConfig
96
97namespace makVre
98{
99//! \brief Type identifier for DtEngineAudioUpdater component
100constexpr const char* DtEngineAudioUpdaterType = "DtEngineAudioUpdater";
101
102//! \brief Component for simulating vehicle engine sounds
103//!
104//! The DtEngineAudioUpdater provides realistic audio simulation for vehicle engines.
105//! It dynamically modulates sound pitch and volume based on engine RPM or speed values,
106//! applies smoothing to prevent jitter, and positions sounds in 3D space based on
107//! the entity's location.
108//!
109//! The component supports multiple engine sounds with different characteristics
110//! and can tie sound playback to an engine on/off state attribute.
112{
113public:
114 //! \brief Constructor for DtEngineAudioUpdater
115 //!
116 //! Initializes the component with the name "DtEngineAudioUpdater".
118
119 //! \brief Virtual destructor for DtEngineAudioUpdater
120 //!
121 //! Ensures proper cleanup of audio resources when the component is destroyed.
122 virtual ~DtEngineAudioUpdater() override;
123
124 //! \brief Initializes the component with configuration from Lua
125 //!
126 //! Sets up audio sources based on the provided configuration. Each source is configured
127 //! with its associated RPM attribute, maximum RPM value, pitch range, smoothing factor,
128 //! gain, and decibel level.
129 //!
130 //! \param player Pointer to the player station this component belongs to
131 //! \param config Configuration table containing initialization parameters, especially the sources table
132 //! \return True if initialization succeeds, false otherwise
133 virtual bool initialize(DtPlayerStation* player, DtInitTable& config) override;
134
135 //! \brief Updates the audio playback based on RPM state attributes
136 //!
137 //! Called each frame to manage audio playback. For each configured sound:
138 //! - Reads the associated RPM attribute value
139 //! - Normalizes the value based on the maximum RPM
140 //! - Applies smoothing to reduce jitter
141 //! - Calculates and sets pitch and gain values
142 //! - Updates the sound position to match the entity's location
143 //!
144 //! \param dt Delta time in seconds since the last tick
145 virtual void tick(double dt) override;
146
147 //! \brief Shuts down the component
148 //!
149 //! Cleans up all audio resources by stopping and deleting sounds.
150 virtual void shutdown() override;
151
152 //! \brief Returns the type identifier for this component
153 //! \return The type string for DtEngineAudioUpdater
154 virtual const char* type() const override;
155
156protected:
157 //! \brief Name of the state attribute that indicates if the engine is on
158 //!
159 //! This attribute controls whether engine sounds are played at all.
160 //! Default is "engine-on" but can be changed via configuration.
162
163 //! \brief Structure for managing engine sound resources and their parameters
165 {
166 //! \brief Name of the state attribute providing RPM/speed values
167 //!
168 //! This attribute's value is used to modulate sound pitch and gain.
170
171 //! \brief Pointer to the audio sound resource
172 makAudio::DtSound* myDtSound;
173
174 //! \brief Maximum expected RPM/speed value
175 //!
176 //! Used to normalize the RPM value to a 0.0-1.0 range.
177 float myRpmMax;
178
179 //! \brief Maximum pitch scaling factor
180 //!
181 //! Applied when the normalized RPM value is 1.0 (maximum).
183
184 //! \brief Minimum pitch scaling factor
185 //!
186 //! Applied when the normalized RPM value is 0.0 (idle).
188
189 //! \brief Previously calculated normalized RPM value
190 //!
191 //! Used for smoothing between frames to reduce jitter.
193
194 //! \brief Smoothing factor for RPM changes
195 //!
196 //! Value between 0.0 (no smoothing) and 0.99 (maximum smoothing).
197 //! Higher values result in slower response to RPM changes.
199 };
200
201 //! \brief Collection of engine sounds managed by this component
202 std::vector<EngineSound> myEngineSounds;
203};
204} // namespace makVre
virtual const char * type() const override
Returns the type identifier for this component.
virtual bool initialize(DtPlayerStation *player, DtInitTable &config) override
Initializes the component with configuration from Lua.
std::vector< EngineSound > myEngineSounds
Collection of engine sounds managed by this component.
Definition engineAudioUpdater.h:202
virtual void tick(double dt) override
Updates the audio playback based on RPM state attributes.
virtual ~DtEngineAudioUpdater() override
Virtual destructor for DtEngineAudioUpdater.
DtEngineAudioUpdater()
Constructor for DtEngineAudioUpdater.
virtual void shutdown() override
Shuts down the component.
std::string myEngineOnAttributeName
Name of the state attribute that indicates if the engine is on.
Definition engineAudioUpdater.h:161
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 * sources
Definition engineAudioUpdater.h:92
constexpr const char * audioPath
Definition engineAudioUpdater.h:83
constexpr const char * engineOnAttribute
Definition engineAudioUpdater.h:86
constexpr const char * engineOnName
Definition engineAudioUpdater.h:89
Defines export/import macros for the vreCommonComponents library.
#define VRECOMMONCOMPONENTS_DLL
DLL export/import macro for the vreCommonComponents library.
Definition export.h:28
Definition engineAudioUpdater.h:75
Component configuration and usage details.
Definition audioUpdater.h:21
Include export definitions for this library.
Definition glsVreMessageUtil.h:49
constexpr const char * DtEngineAudioUpdaterType
Type identifier for DtEngineAudioUpdater component.
Definition engineAudioUpdater.h:100
Defines the DtPlayerComponent base class for VR-Engage role components.
Structure for managing engine sound resources and their parameters.
Definition engineAudioUpdater.h:165
std::string myRpmAttributeName
Name of the state attribute providing RPM/speed values.
Definition engineAudioUpdater.h:169
makAudio::DtSound * myDtSound
Pointer to the audio sound resource.
Definition engineAudioUpdater.h:172
double mySmoothing
Smoothing factor for RPM changes.
Definition engineAudioUpdater.h:198
float myMaxPitch
Maximum pitch scaling factor.
Definition engineAudioUpdater.h:182
float myMinPitch
Minimum pitch scaling factor.
Definition engineAudioUpdater.h:187
float myPreviousNormalizedRpm
Previously calculated normalized RPM value.
Definition engineAudioUpdater.h:192
float myRpmMax
Maximum expected RPM/speed value.
Definition engineAudioUpdater.h:177