Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
SoundManager.h
Go to the documentation of this file.
1/*
2 This source file is part of Rigs of Rods
3 Copyright 2005-2012 Pierre-Michel Ricordel
4 Copyright 2007-2012 Thomas Fischer
5 Copyright 2013-2020 Petr Ohlidal
6
7 For more information, see http://www.rigsofrods.org/
8
9 Rigs of Rods is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License version 3, as
11 published by the Free Software Foundation.
12
13 Rigs of Rods is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#ifdef USE_OPENAL
23
24#pragma once
25
26#include "Actor.h"
27#include "Application.h"
28#include "Collisions.h"
29#include "GameContext.h"
30#include "Sound.h"
31#include "SoundScriptManager.h"
32
33#include <OgreVector3.h>
34#include <OgreString.h>
35
36#ifdef __APPLE__
37 #include <OpenAL/al.h>
38 #include <OpenAL/alc.h>
39 #include <OpenAL/alext.h>
40 #include <OpenAL/efx-presets.h>
41#else
42 #include <AL/al.h>
43 #include <AL/alc.h>
44 #include <AL/alext.h>
45 #include <AL/efx-presets.h>
46#endif // __APPLE__
47
48namespace RoR {
49
52
54{
55 friend class Sound;
56
57public:
60
64 void CleanUp();
65
70 SoundPtr createSound(Ogre::String filename, Ogre::String resource_group_name = "");
71
75 Ogre::Vector3 GetListenerPosition() const { return m_listener_position; }
76
81
87 void Update(const float dt);
88
92 void UpdateGlobalDopplerFactor() const;
93
98 void UpdateEfxSpecificProperties(const float dt);
99
105 void UpdateSourceSpecificDopplerFactor(const int hardware_index) const;
106
114 void SetListener(Ogre::Vector3 position, Ogre::Vector3 direction, Ogre::Vector3 up, Ogre::Vector3 velocity);
115
119 void pauseAllSounds();
120
124 void resumeAllSounds();
125
130 void setMasterVolume(float v);
131
132 bool isDisabled() { return audio_device == 0; }
133
137 float GetSpeedOfSound() const { return alGetFloat(AL_SPEED_OF_SOUND); }
138
144 void SetSpeedOfSound(const float speed_of_sound) const { alSpeedOfSound(speed_of_sound); }
145
149 float GetDopplerFactor() const { return alGetFloat(AL_DOPPLER_FACTOR); }
150
155 void SetDopplerFactor(const float doppler_factor) const { alDopplerFactor(doppler_factor); }
156
163 void SetDopplerFactor(const ALuint hardware_source, const float doppler_factor) const;
164
169 void SetAirAbsorptionFactor(const float air_absorption_factor) { m_air_absorption_factor = air_absorption_factor; }
170
175
182
187 const std::map<std::string, EFXEAXREVERBPROPERTIES>& getEfxPropertiesMap() const { return m_efx_properties_map; }
188
195 const EFXEAXREVERBPROPERTIES* GetEfxProperties(const std::string& efx_preset_name) const;
196
202 const EFXEAXREVERBPROPERTIES* GetReverbPresetAt(Ogre::Vector3 position) const;
203
204 static const float MAX_DISTANCE;
205 static const float ROLLOFF_FACTOR;
206 static const float REFERENCE_DISTANCE;
207 static const unsigned int MAX_HARDWARE_SOURCES = 32;
208 static const unsigned int MAX_AUDIO_BUFFERS = 8192;
209
210private:
215 void UpdateAlListener();
216
221
222 void recomputeAllSources();
223
232 void recomputeSource(int source_index, int reason, float vfl, Ogre::Vector3 *vvec);
233
239 ALuint getHardwareSource(int hardware_index) { return hardware_sources[hardware_index]; };
240
246 void assign(int source_index, int hardware_index);
247
252 void retire(int source_index);
253
254 bool loadWAVFile(Ogre::String filename, ALuint buffer, Ogre::String resource_group_name = "");
255
256 // active audio sources (hardware sources)
260 ALuint hardware_sources[MAX_HARDWARE_SOURCES]; // this buffer contains valid AL handles up to m_hardware_sources_num
261
262 // audio sources
265 // helper for calculating the most audible sources
267
268 // audio buffers: Array of AL buffers and filenames
272
274 Ogre::Vector3 m_listener_position = Ogre::Vector3::ZERO;
275 Ogre::Vector3 m_listener_direction = Ogre::Vector3::ZERO;
276 Ogre::Vector3 m_listener_up = Ogre::Vector3::ZERO;
277 Ogre::Vector3 m_listener_velocity = Ogre::Vector3::ZERO;
278
279 ALCdevice* audio_device = nullptr;
280 ALCcontext* sound_context = nullptr;
281
282 // OpenAL EFX stuff
283 bool m_efx_is_available = false;
284 ALuint m_listener_slot = 0;
290 const EFXEAXREVERBPROPERTIES* m_listener_efx_reverb_properties = nullptr;
291 std::map<std::string, EFXEAXREVERBPROPERTIES> m_efx_properties_map;
292 std::map<ALuint, ALuint> m_efx_effect_id_map; //<! maps from auxiliary effect slot id to effect id
293 LPALGENEFFECTS alGenEffects = nullptr;
294 LPALDELETEEFFECTS alDeleteEffects = nullptr;
295 LPALISEFFECT alIsEffect = nullptr;
296 LPALEFFECTI alEffecti = nullptr;
297 LPALEFFECTF alEffectf = nullptr;
298 LPALEFFECTFV alEffectfv = nullptr;
299 LPALGENFILTERS alGenFilters = nullptr;
300 LPALDELETEFILTERS alDeleteFilters = nullptr;
301 LPALISFILTER alIsFilter = nullptr;
302 LPALFILTERI alFilteri = nullptr;
303 LPALFILTERF alFilterf = nullptr;
304 LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots = nullptr;
305 LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots = nullptr;
306 LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot = nullptr;
307 LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti = nullptr;
308 LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf = nullptr;
309 LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv = nullptr;
310
316 ALuint CreateAlEffect(const EFXEAXREVERBPROPERTIES* efx_properties) const;
317
322 void DeleteAlEffect(const ALuint efx_effect_id) const;
323
329
337 void UpdateListenerEffectSlot(const float dt);
338
346 void SmoothlyUpdateAlAuxiliaryEffectSlot(const float dt, const ALuint slot_id, const EFXEAXREVERBPROPERTIES* target_efx_properties);
347
354 std::tuple<Ogre::Vector3, float, float> ComputeEarlyReflectionsProperties() const;
355
360 void UpdateSourceFilters(const int hardware_index) const;
361
368 bool IsHardwareSourceObstructed(const int hardware_index) const;
369
375 void UpdateObstructionFilter(const int hardware_index, const bool enable_obstruction_filter) const;
376
386 bool UpdateOcclusionFilter(const int hardware_index, const ALuint effect_slot_id, const EFXEAXREVERBPROPERTIES* reference_efx_reverb_properties) const;
387
391 void UpdateDirectedSounds() const;
392
403 const ALuint source,
404 const Ogre::Vector3& cone_direction,
405 const float cone_inner_angle,
406 const float cone_outer_angle,
407 const float cone_outer_gain,
408 const float cone_outer_gain_hf
409 ) const;
410};
411
413
414} // namespace RoR
415
416#endif // USE_OPENAL
Central state/object manager and communications hub.
Game state manager and message-queue provider.
void SetDopplerFactor(const float doppler_factor) const
Updates the global Doppler factor in OpenAL with the provided value.
static const float MAX_DISTANCE
Ogre::Vector3 GetListenerPosition() const
Returns the position vector of the listener.
void UpdateConeProperties(const ALuint source, const Ogre::Vector3 &cone_direction, const float cone_inner_angle, const float cone_outer_angle, const float cone_outer_gain, const float cone_outer_gain_hf) const
Updates the Cone properties for the hardware source.
void Update(const float dt)
Does the per-frame update of sounds and listener environment.
LPALISFILTER alIsFilter
std::map< ALuint, ALuint > m_efx_effect_id_map
LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti
void recomputeSource(int source_index, int reason, float vfl, Ogre::Vector3 *vvec)
Computes audibility of an audio source and retires it if it is inaudible.
int getNumHardwareSources()
Returns the number of currently used hardware sources.
void PrepopulateEfxPropertiesMap()
Helper function that fills the m_efx_properties_map with presets provided by OpenAL's efx-presets....
ALuint audio_buffers[MAX_AUDIO_BUFFERS]
ALuint m_efx_outdoor_obstruction_lowpass_filter_id
float m_air_absorption_factor
SoundPtr audio_sources[MAX_AUDIO_BUFFERS]
LPALEFFECTI alEffecti
void UpdateGlobalDopplerFactor() const
Updates the global Doppler factor based on CVar settings and the state of the physics simulation.
void pauseAllSounds()
Unlike the name suggests, this sets the listener's gain to 0, essentially muting all sounds.
bool loadWAVFile(Ogre::String filename, ALuint buffer, Ogre::String resource_group_name="")
bool UpdateOcclusionFilter(const int hardware_index, const ALuint effect_slot_id, const EFXEAXREVERBPROPERTIES *reference_efx_reverb_properties) const
Applies an occlusion filter to the provided source if certain conditions apply.
void UpdateListenerEnvironment()
Determines several properties of the environment of the listener and updates OpenAL to use them.
static const unsigned int MAX_AUDIO_BUFFERS
ALuint m_efx_occlusion_wet_path_lowpass_filter_id
ALuint getHardwareSource(int hardware_index)
Returns the AL handle for the hardware source with the provided index.
LPALDELETEFILTERS alDeleteFilters
SoundPtr createSound(Ogre::String filename, Ogre::String resource_group_name="")
LPALFILTERF alFilterf
LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv
ALuint CreateAlEffect(const EFXEAXREVERBPROPERTIES *efx_properties) const
Creates an OpenAL effect based on the parameters of an efx/eax reverb preset.
Ogre::String audio_buffer_file_name[MAX_AUDIO_BUFFERS]
int hardware_sources_in_use_count
void assign(int source_index, int hardware_index)
Adds an audio source to hardware source.
Ogre::Vector3 m_listener_direction
EfxReverbEngine m_efx_reverb_engine
std::map< std::string, EFXEAXREVERBPROPERTIES > m_efx_properties_map
const EFXEAXREVERBPROPERTIES * m_listener_efx_reverb_properties
int hardware_sources_map[MAX_HARDWARE_SOURCES]
maps from the index of a hardware source to the index of the audio source currently assigned to the c...
void SetAirAbsorptionFactor(const float air_absorption_factor)
Sets the air absorptions factor for the direct path of all sounds.
void UpdateObstructionFilter(const int hardware_index, const bool enable_obstruction_filter) const
Applies an obstruction filter to the provided hardware source.
ALuint hardware_sources[MAX_HARDWARE_SOURCES]
void setMasterVolume(float v)
Updates both CVar audio_master_volume and the listener's gain to the provided value.
void UpdateEfxSpecificProperties(const float dt)
Updates properties of OpenAL facilities that are only available with EFX.
void UpdateSourceFilters(const int hardware_index) const
Helper function to call several other functions to update source filters.
static const float REFERENCE_DISTANCE
Ogre::Vector3 m_listener_position
int hardware_sources_num
total number of allocated hardware sources (<= MAX_HARDWARE_SOURCES)
LPALGENEFFECTS alGenEffects
void SetSpeedOfSound(const float speed_of_sound) const
Updates the speed of sound in OpenAL with the provided value.
static const float ROLLOFF_FACTOR
LPALEFFECTFV alEffectfv
std::pair< int, float > audio_sources_most_audible[MAX_AUDIO_BUFFERS]
bool ListenerIsUnderwater() const
void DeleteAlEffect(const ALuint efx_effect_id) const
Deletes an OpenAL effect.
void UpdateDirectedSounds() const
Updates AL Cones for sources of directed sound emissions (exhausts, turboprops and turbojets).
ALuint m_efx_occlusion_wet_path_send_id
ALCdevice * audio_device
Ogre::Vector3 m_listener_up
LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots
void resumeAllSounds()
Unlike the name suggests, this sets the listener's gain to the value of the CVar audio_master_volume.
void UpdateSourceSpecificDopplerFactor(const int hardware_index) const
Identifies the actor to which the sound corresponding to a hardware source belongs and updates the Do...
LPALFILTERI alFilteri
LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots
LPALEFFECTF alEffectf
const std::map< std::string, EFXEAXREVERBPROPERTIES > & getEfxPropertiesMap() const
Returns currently registered EFX presets.
Ogre::Vector3 m_listener_velocity
bool IsHardwareSourceObstructed(const int hardware_index) const
Performs various checks against the environment of the listener to determine whether the sound belong...
float GetSpeedOfSound() const
LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf
void SetListener(Ogre::Vector3 position, Ogre::Vector3 direction, Ogre::Vector3 up, Ogre::Vector3 velocity)
Sets position and speed of the listener.
float GetAirAbsorptionFactor() const
LPALDELETEEFFECTS alDeleteEffects
LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot
LPALISEFFECT alIsEffect
std::tuple< Ogre::Vector3, float, float > ComputeEarlyReflectionsProperties() const
Detects surfaces close to the listener and calculates a user-relative (as opposed to listener-relativ...
const EFXEAXREVERBPROPERTIES * GetReverbPresetAt(Ogre::Vector3 position) const
Determines which reverb preset corresponds to the provided position and returns its properties.
static const unsigned int MAX_HARDWARE_SOURCES
ALCcontext * sound_context
void retire(int source_index)
Stops and the removes an audio source from hardware source.
void UpdateListenerEffectSlot(const float dt)
Dynamically adjusts some parameters of the currently active reverb preset based on the current enviro...
float GetDopplerFactor() const
void SmoothlyUpdateAlAuxiliaryEffectSlot(const float dt, const ALuint slot_id, const EFXEAXREVERBPROPERTIES *target_efx_properties)
This performs a smooth update of the efx properties of an OpenAL Auxiliary Effect slot using linear i...
LPALGENFILTERS alGenFilters
void CleanUp()
Cleans up various objects that should be reset when returning from a terrain to the main menu.
void UpdateAlListener()
Updates the listener's position, orientation and velocity vectors in OpenAL.
const EFXEAXREVERBPROPERTIES * GetEfxProperties(const std::string &efx_preset_name) const
Returns a pointer to properties of an EFX preset stored in the EFX properties map.
EfxReverbEngine