From be920d315f3262bd9cf872ba66aa69b8853df2c9 Mon Sep 17 00:00:00 2001 From: ArtemPopof Date: Wed, 13 Feb 2019 16:11:35 -0500 Subject: [PATCH 01/15] Pickups speed adjusted --- rwengine/src/render/ObjectRenderer.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rwengine/src/render/ObjectRenderer.cpp b/rwengine/src/render/ObjectRenderer.cpp index 358696d21..04ba15a62 100644 --- a/rwengine/src/render/ObjectRenderer.cpp +++ b/rwengine/src/render/ObjectRenderer.cpp @@ -308,9 +308,11 @@ void ObjectRenderer::renderVehicle(VehicleObject* vehicle, void ObjectRenderer::renderPickup(PickupObject* pickup, RenderList& outList) { if (!pickup->isEnabled()) return; + static const auto rotationSpeedCoeff = 3; glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), pickup->getPosition()); - modelMatrix = glm::rotate(modelMatrix, m_world->getGameTime(), + modelMatrix = glm::rotate(modelMatrix, + m_world->getGameTime() * rotationSpeedCoeff, glm::vec3(0.f, 0.f, 1.f)); auto odata = pickup->getModelInfo(); From 8bd8e17921c347738a3a459649d167d10b78a083 Mon Sep 17 00:00:00 2001 From: ArtemPopof Date: Fri, 15 Feb 2019 15:52:19 -0500 Subject: [PATCH 02/15] Environment for OpenAL EFX added reverb effect added --- rwengine/CMakeLists.txt | 8 ++++ rwengine/src/audio/EffectSlot.cpp | 39 ++++++++++++++++ rwengine/src/audio/EffectSlot.hpp | 62 +++++++++++++++++++++++++ rwengine/src/audio/OpenAlExtensions.cpp | 25 ++++++++++ rwengine/src/audio/OpenAlExtensions.hpp | 26 +++++++++++ rwengine/src/audio/ReverbEffect.cpp | 44 ++++++++++++++++++ rwengine/src/audio/ReverbEffect.h | 11 +++++ rwengine/src/audio/ReverbEffect.hpp | 22 +++++++++ rwengine/src/audio/Sound.cpp | 5 ++ rwengine/src/audio/Sound.hpp | 3 ++ rwengine/src/audio/SoundBuffer.cpp | 11 +++++ rwengine/src/audio/SoundBuffer.hpp | 6 +++ rwengine/src/audio/SoundEffect.cpp | 22 +++++++++ rwengine/src/audio/SoundEffect.hpp | 38 +++++++++++++++ rwengine/src/audio/SoundManager.cpp | 18 ++++++- rwengine/src/audio/SoundManager.hpp | 3 ++ 16 files changed, 342 insertions(+), 1 deletion(-) create mode 100644 rwengine/src/audio/EffectSlot.cpp create mode 100644 rwengine/src/audio/EffectSlot.hpp create mode 100644 rwengine/src/audio/OpenAlExtensions.cpp create mode 100644 rwengine/src/audio/OpenAlExtensions.hpp create mode 100644 rwengine/src/audio/ReverbEffect.cpp create mode 100644 rwengine/src/audio/ReverbEffect.h create mode 100644 rwengine/src/audio/ReverbEffect.hpp create mode 100644 rwengine/src/audio/SoundEffect.cpp create mode 100644 rwengine/src/audio/SoundEffect.hpp diff --git a/rwengine/CMakeLists.txt b/rwengine/CMakeLists.txt index cddfe5f9e..b2a22c53a 100644 --- a/rwengine/CMakeLists.txt +++ b/rwengine/CMakeLists.txt @@ -24,6 +24,14 @@ set(RWENGINE_SOURCES src/audio/SoundManager.hpp src/audio/SoundSource.cpp src/audio/SoundSource.hpp + src/audio/EffectSlot.hpp + src/audio/EffectSlot.cpp + src/audio/SoundEffect.cpp + src/audio/SoundEffect.hpp + src/audio/ReverbEffect.cpp + src/audio/ReverbEffect.hpp + src/audio/OpenAlExtensions.hpp + src/audio/OpenAlExtensions.cpp src/core/Logger.cpp src/core/Logger.hpp diff --git a/rwengine/src/audio/EffectSlot.cpp b/rwengine/src/audio/EffectSlot.cpp new file mode 100644 index 000000000..bbc2698c9 --- /dev/null +++ b/rwengine/src/audio/EffectSlot.cpp @@ -0,0 +1,39 @@ +#include "EffectSlot.hpp" +#include "SoundEffect.hpp" +#include "OpenAlExtensions.hpp" + +EffectSlot::EffectSlot() { + alGenAuxiliaryEffectSlots(1, &slotId); + + effect = nullptr; + gain = 1.0f; + slotNumber = 0; + created = alGetError() == AL_NO_ERROR; +} + +EffectSlot::~EffectSlot() { + alDeleteAuxiliaryEffectSlots(1, &slotId); +} + +bool EffectSlot::attachEffect(std::shared_ptr effect) { + alAuxiliaryEffectSloti(slotId, AL_EFFECTSLOT_EFFECT, effect->getId()); + + if (alGetError() != AL_NO_ERROR) { + return false; + } + + this->effect = std::move(effect); + + return true; +} + +bool EffectSlot::detachEffect() { + alAuxiliaryEffectSloti(slotId, AL_EFFECTSLOT_EFFECT, 0); + this->effect = nullptr; + + return alGetError() == AL_NO_ERROR; +} + +void EffectSlot::setGain(float gain) { + alAuxiliaryEffectSlotf(slotId, AL_EFFECTSLOT_GAIN, this->gain = gain); +} diff --git a/rwengine/src/audio/EffectSlot.hpp b/rwengine/src/audio/EffectSlot.hpp new file mode 100644 index 000000000..d9fe0b7e5 --- /dev/null +++ b/rwengine/src/audio/EffectSlot.hpp @@ -0,0 +1,62 @@ +#ifndef EFFECTSLOT_H +#define EFFECTSLOT_H + +#include + +#include + +class SoundEffect; + +/** + * Effect slot. + * + * Many sound sources can be attached to one slot. + * Different effects can be binded to this (e.g reverb, delay). + */ +class EffectSlot { +public: + EffectSlot(); + ~EffectSlot(); + + /** + * Attach effect to this slot. + * + * @param sound effect (e.g reverb, delay) + * @return true if effect attached successfully, false otherwise + */ + bool attachEffect(std::shared_ptr effect); + + /** + * Detach current effect from this slot. + * @return true if effect detached successfully, false otherwise + */ + bool detachEffect(); + + void setGain(float gain); + + ALuint getSlotId() const { + return slotId; + } + + int getSlotNumber() const { + return slotNumber; + } + +private: + /** + * Effect binded to this slot + */ + std::shared_ptr effect; + + ALuint slotId; + /** + * This is flag of successfull slot creation + */ + bool created; + float gain; + + /// OpenAL aux slot + int slotNumber; +}; + +#endif // EFFECTSLOT_H diff --git a/rwengine/src/audio/OpenAlExtensions.cpp b/rwengine/src/audio/OpenAlExtensions.cpp new file mode 100644 index 000000000..ead6089bc --- /dev/null +++ b/rwengine/src/audio/OpenAlExtensions.cpp @@ -0,0 +1,25 @@ +#include "OpenAlExtensions.hpp" + +LPALGENEFFECTS alGenEffects = nullptr; +LPALDELETEEFFECTS alDeleteEffects = nullptr; +LPALEFFECTI alEffecti = nullptr; +LPALEFFECTF alEffectf = nullptr; + +// Auxiliary slot object +LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots = nullptr; +LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots = nullptr; +LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti = nullptr; +LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf = nullptr; + +void initEfxFunctionPointers() { + alGenEffects = reinterpret_cast(alGetProcAddress("alGenEffects")); + alDeleteEffects = reinterpret_cast(alGetProcAddress("alDeleteEffects")); + alEffecti = reinterpret_cast(alGetProcAddress("alEffecti")); + alEffectf = reinterpret_cast(alGetProcAddress("alEffectf")); + + // aux slot + alGenAuxiliaryEffectSlots = reinterpret_cast(alGetProcAddress("alGenAuxiliaryEffectSlots")); + alDeleteAuxiliaryEffectSlots = reinterpret_cast(alGetProcAddress("alDeleteAuxiliaryEffectSlots")); + alAuxiliaryEffectSloti = reinterpret_cast(alGetProcAddress("alAuxiliaryEffectSloti")); + alAuxiliaryEffectSlotf = reinterpret_cast(alGetProcAddress("alAuxiliaryEffectSlotf")); +} diff --git a/rwengine/src/audio/OpenAlExtensions.hpp b/rwengine/src/audio/OpenAlExtensions.hpp new file mode 100644 index 000000000..ee700f3ad --- /dev/null +++ b/rwengine/src/audio/OpenAlExtensions.hpp @@ -0,0 +1,26 @@ +#ifndef OPENALEXTENSIONS_HPP +#define OPENALEXTENSIONS_HPP + +#include + +/** + * Functions to access OpenAL EFX extension + */ + +extern LPALGENEFFECTS alGenEffects; +extern LPALDELETEEFFECTS alDeleteEffects; +extern LPALEFFECTI alEffecti; +extern LPALEFFECTF alEffectf; + +// Aux slot +extern LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots; +extern LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots; +extern LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti; +extern LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf; + +/** + * Initialize function pointers + */ +void initEfxFunctionPointers(); + +#endif // OPENALEXTENSIONS_HPP diff --git a/rwengine/src/audio/ReverbEffect.cpp b/rwengine/src/audio/ReverbEffect.cpp new file mode 100644 index 000000000..e69fe7351 --- /dev/null +++ b/rwengine/src/audio/ReverbEffect.cpp @@ -0,0 +1,44 @@ +#include "ReverbEffect.hpp" +#include "OpenAlExtensions.hpp" +#include + +ReverbEffect::ReverbEffect() : SoundEffect (AL_EFFECT_REVERB) { + +} + + +void ReverbEffect::setDensity(float d) { + alEffectf(id, AL_REVERB_DENSITY, d); +} + +void ReverbEffect::setDiffusion(float d) { + alEffectf(id, AL_REVERB_DIFFUSION, d); +} + +void ReverbEffect::setGain(float g) { + alEffectf(id, AL_REVERB_GAIN, g); +} + +void ReverbEffect::setGainHf(float g) { + alEffectf(id, AL_REVERB_GAINHF, g); +} + +void ReverbEffect::setDecayTime(float t) { + alEffectf(id, AL_REVERB_DECAY_TIME, t); +} + +void ReverbEffect :: setLateReverbGain (float g) { + alEffectf(id, AL_REVERB_LATE_REVERB_GAIN, g); +} + +void ReverbEffect :: setLateReverbDelay (float t) { + alEffectf(id, AL_REVERB_LATE_REVERB_DELAY, t); +} + +void ReverbEffect :: setAirAbsorptionGainHf (float g) { + alEffectf(id, AL_REVERB_AIR_ABSORPTION_GAINHF, g); +} + +void ReverbEffect :: setDecayHfLimit (bool flag) { + alEffecti(id, AL_REVERB_DECAY_HFLIMIT, flag ? AL_TRUE : AL_FALSE); +} diff --git a/rwengine/src/audio/ReverbEffect.h b/rwengine/src/audio/ReverbEffect.h new file mode 100644 index 000000000..209068795 --- /dev/null +++ b/rwengine/src/audio/ReverbEffect.h @@ -0,0 +1,11 @@ +#ifndef REVERBEFFECT_H +#define REVERBEFFECT_H + + +class ReverbEffect +{ +public: + ReverbEffect(); +}; + +#endif // REVERBEFFECT_H diff --git a/rwengine/src/audio/ReverbEffect.hpp b/rwengine/src/audio/ReverbEffect.hpp new file mode 100644 index 000000000..d7fb6cd19 --- /dev/null +++ b/rwengine/src/audio/ReverbEffect.hpp @@ -0,0 +1,22 @@ +#ifndef REVERBEFFECT_H +#define REVERBEFFECT_H + +#include "SoundEffect.hpp" + +class ReverbEffect : public SoundEffect { + +public: + ReverbEffect(); + + void setDensity ( float density = 1.0f ); + void setDiffusion ( float diffusion = 1.0f ); + void setGain ( float gain = 0.32f ); + void setGainHf ( float gainHf = 0.89f ); + void setDecayTime ( float decayTime = 1.49f ); + void setLateReverbGain ( float lateReverbGain = 1.26f ); + void setLateReverbDelay ( float lateReverbDelay = 0.011f ); + void setAirAbsorptionGainHf ( float absorptionGainHf = 0.994f ); + void setDecayHfLimit ( bool decayHfLimit = true ); +}; + +#endif // REVERBEFFECT_H diff --git a/rwengine/src/audio/Sound.cpp b/rwengine/src/audio/Sound.cpp index df934cd78..ebea663f6 100644 --- a/rwengine/src/audio/Sound.cpp +++ b/rwengine/src/audio/Sound.cpp @@ -46,6 +46,11 @@ void Sound::setMaxDistance(float maxDist) { buffer->setMaxDistance(maxDist); } +void Sound::attachToEffectSlot(const std::shared_ptr effectSlot) +{ + buffer->attachToEffectSlot(effectSlot); +} + size_t Sound::getScriptObjectID() const { return id; } diff --git a/rwengine/src/audio/Sound.hpp b/rwengine/src/audio/Sound.hpp index fbb29122b..72b5b8ab0 100644 --- a/rwengine/src/audio/Sound.hpp +++ b/rwengine/src/audio/Sound.hpp @@ -6,6 +6,7 @@ #include class SoundSource; +class EffectSlot; struct SoundBuffer; /// Wrapper for SoundBuffer and SoundSource. @@ -42,6 +43,8 @@ struct Sound { void setMaxDistance(float maxDist); + void attachToEffectSlot(const std::shared_ptr effectSlot); + size_t getScriptObjectID() const; }; #endif diff --git a/rwengine/src/audio/SoundBuffer.cpp b/rwengine/src/audio/SoundBuffer.cpp index 9d367eb83..583e5ec62 100644 --- a/rwengine/src/audio/SoundBuffer.cpp +++ b/rwengine/src/audio/SoundBuffer.cpp @@ -1,9 +1,12 @@ #include "audio/SoundBuffer.hpp" #include +#include #include "audio/alCheck.hpp" #include "audio/SoundSource.hpp" +#include "audio/SoundEffect.hpp" +#include "audio/EffectSlot.hpp" SoundBuffer::SoundBuffer() { alCheck(alGenSources(1, &source)); @@ -83,3 +86,11 @@ void SoundBuffer::setGain(float gain) { void SoundBuffer::setMaxDistance(float maxDist) { alCheck(alSourcef(source, AL_MAX_DISTANCE, maxDist)); } + +void SoundBuffer::attachToEffectSlot(const std::shared_ptr slot) { + alCheck(alSource3i(source, AL_AUXILIARY_SEND_FILTER, slot->getSlotId(), slot->getSlotNumber(), AL_FILTER_NULL)); +} + +void SoundBuffer::detachFromEffectSlot(const std::shared_ptr slot) { + alCheck(alSource3i (source, AL_AUXILIARY_SEND_FILTER, AL_EFFECTSLOT_NULL, slot->getSlotNumber(), AL_FILTER_NULL)); +} diff --git a/rwengine/src/audio/SoundBuffer.hpp b/rwengine/src/audio/SoundBuffer.hpp index 2a5f6949e..5c063cc49 100644 --- a/rwengine/src/audio/SoundBuffer.hpp +++ b/rwengine/src/audio/SoundBuffer.hpp @@ -4,6 +4,9 @@ #include #include +#include + +class EffectSlot; class SoundSource; /// OpenAL tool for playing @@ -27,6 +30,9 @@ struct SoundBuffer { void setGain(float gain); void setMaxDistance(float maxDist); + void attachToEffectSlot(const std::shared_ptr effectSlot); + void detachFromEffectSlot(const std::shared_ptr effectSlot); + ALuint source; ALuint buffer; }; diff --git a/rwengine/src/audio/SoundEffect.cpp b/rwengine/src/audio/SoundEffect.cpp new file mode 100644 index 000000000..9f5b04648 --- /dev/null +++ b/rwengine/src/audio/SoundEffect.cpp @@ -0,0 +1,22 @@ +#include "SoundEffect.hpp" +#include "OpenAlExtensions.hpp" + +SoundEffect::SoundEffect(ALint type) { + // reset error + alGetError(); + + alGenEffects(1, &id); + + created = alGetError() == AL_NO_ERROR; + if (!created) { + return; + } + + alEffecti(id, AL_EFFECT_TYPE, type); + + created = alGetError() == AL_NO_ERROR; +} + +SoundEffect::~SoundEffect() { + alDeleteEffects(1, &id); +} diff --git a/rwengine/src/audio/SoundEffect.hpp b/rwengine/src/audio/SoundEffect.hpp new file mode 100644 index 000000000..cc0e105cd --- /dev/null +++ b/rwengine/src/audio/SoundEffect.hpp @@ -0,0 +1,38 @@ +#ifndef SOUNDEFFECT_H +#define SOUNDEFFECT_H + +#include + +/** + * Class to represent any effect. + * + * Any concrete realisation should have own class + * in order to be able to set different effect-specific parameters. + */ +class SoundEffect { +public: + /** + * Create effect + * @param type OpenAl specific effect type. + */ + SoundEffect(ALint type); + ~SoundEffect(); + + ALuint getId() const { + return id; + } + +protected: + /** + * Effect openal id + */ + ALuint id; + +private: + /** + * Effect created successfully if this is true after construction + */ + bool created; +}; + +#endif // SOUNDEFFECT_H diff --git a/rwengine/src/audio/SoundManager.cpp b/rwengine/src/audio/SoundManager.cpp index 328feef79..778803e5c 100644 --- a/rwengine/src/audio/SoundManager.cpp +++ b/rwengine/src/audio/SoundManager.cpp @@ -5,12 +5,15 @@ extern "C" { #include #include #include +#include } #include "audio/alCheck.hpp" #include "audio/Sound.hpp" #include "audio/SoundBuffer.hpp" #include "audio/SoundSource.hpp" +#include "audio/OpenAlExtensions.hpp" +#include "audio/SoundEffect.hpp" #include "engine/GameData.hpp" #include "engine/GameWorld.hpp" #include "render/ViewCamera.hpp" @@ -43,6 +46,7 @@ SoundManager::SoundManager(GameWorld* engine) : _engine(engine) { initializeOpenAL(); initializeAVCodec(); + initializeEFX(); } SoundManager::~SoundManager() { @@ -87,6 +91,18 @@ void SoundManager::initializeAVCodec() { #endif } +bool SoundManager::initializeEFX() { + if (!alcIsExtensionPresent(alDevice, ALC_EXT_EFX_NAME)) { + RW_ERROR("EFX extention is not present"); + return false; + } + + initEfxFunctionPointers(); + + SoundEffect effect(0); + return true; +} + void SoundManager::deinitializeOpenAL() { // Buffers have to been removed before openAL is deinitialized sounds.clear(); @@ -138,7 +154,7 @@ void SoundManager::loadSound(size_t index) { sound->source = std::make_shared(); sound->source->loadSfx(sdt, index); -} + } size_t SoundManager::createSfxInstance(size_t index) { Sound* sound = nullptr; diff --git a/rwengine/src/audio/SoundManager.hpp b/rwengine/src/audio/SoundManager.hpp index 07efda605..d3624ce07 100644 --- a/rwengine/src/audio/SoundManager.hpp +++ b/rwengine/src/audio/SoundManager.hpp @@ -91,6 +91,9 @@ class SoundManager { bool initializeOpenAL(); void initializeAVCodec(); + /// Initialize EFX OpenAL extention + bool initializeEFX(); + void deinitializeOpenAL(); ALCcontext* alContext = nullptr; From cb40cc390290a1d80952895cfb6a35647078fba7 Mon Sep 17 00:00:00 2001 From: Artem Popov Date: Fri, 15 Feb 2019 16:44:19 -0500 Subject: [PATCH 03/15] requested changes --- rwengine/src/render/ObjectRenderer.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rwengine/src/render/ObjectRenderer.cpp b/rwengine/src/render/ObjectRenderer.cpp index 04ba15a62..56db49503 100644 --- a/rwengine/src/render/ObjectRenderer.cpp +++ b/rwengine/src/render/ObjectRenderer.cpp @@ -307,12 +307,11 @@ void ObjectRenderer::renderVehicle(VehicleObject* vehicle, void ObjectRenderer::renderPickup(PickupObject* pickup, RenderList& outList) { if (!pickup->isEnabled()) return; - - static const auto rotationSpeedCoeff = 3; + + static constexpr float kRotationSpeedCoeff = 3.0f; glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), pickup->getPosition()); - modelMatrix = glm::rotate(modelMatrix, - m_world->getGameTime() * rotationSpeedCoeff, + modelMatrix = glm::rotate(modelMatrix, m_world->getGameTime() * rotationSpeedCoeff, glm::vec3(0.f, 0.f, 1.f)); auto odata = pickup->getModelInfo(); From 958b7f7983f7d0088e15560b5329f24cb5b7e127 Mon Sep 17 00:00:00 2001 From: Artem Popov Date: Fri, 15 Feb 2019 16:48:05 -0500 Subject: [PATCH 04/15] Variable name adjusted --- rwengine/src/render/ObjectRenderer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rwengine/src/render/ObjectRenderer.cpp b/rwengine/src/render/ObjectRenderer.cpp index 56db49503..a98f3a6b5 100644 --- a/rwengine/src/render/ObjectRenderer.cpp +++ b/rwengine/src/render/ObjectRenderer.cpp @@ -311,7 +311,7 @@ void ObjectRenderer::renderPickup(PickupObject* pickup, RenderList& outList) { static constexpr float kRotationSpeedCoeff = 3.0f; glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), pickup->getPosition()); - modelMatrix = glm::rotate(modelMatrix, m_world->getGameTime() * rotationSpeedCoeff, + modelMatrix = glm::rotate(modelMatrix, m_world->getGameTime() * kRotationSpeedCoeff, glm::vec3(0.f, 0.f, 1.f)); auto odata = pickup->getModelInfo(); From c464756075f03c5904673c1503791ad3deaa0ed3 Mon Sep 17 00:00:00 2001 From: Artem Popov Date: Sat, 16 Feb 2019 07:03:33 -0500 Subject: [PATCH 05/15] Removed some extra spaces --- rwengine/src/audio/ReverbEffect.hpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/rwengine/src/audio/ReverbEffect.hpp b/rwengine/src/audio/ReverbEffect.hpp index d7fb6cd19..eb375f1a8 100644 --- a/rwengine/src/audio/ReverbEffect.hpp +++ b/rwengine/src/audio/ReverbEffect.hpp @@ -8,15 +8,15 @@ class ReverbEffect : public SoundEffect { public: ReverbEffect(); - void setDensity ( float density = 1.0f ); - void setDiffusion ( float diffusion = 1.0f ); - void setGain ( float gain = 0.32f ); - void setGainHf ( float gainHf = 0.89f ); - void setDecayTime ( float decayTime = 1.49f ); - void setLateReverbGain ( float lateReverbGain = 1.26f ); - void setLateReverbDelay ( float lateReverbDelay = 0.011f ); - void setAirAbsorptionGainHf ( float absorptionGainHf = 0.994f ); - void setDecayHfLimit ( bool decayHfLimit = true ); + void setDensity(float density = 1.0f); + void setDiffusion(float diffusion = 1.0f); + void setGain(float gain = 0.32f); + void setGainHf(float gainHf = 0.89f); + void setDecayTime(float decayTime = 1.49f); + void setLateReverbGain(float lateReverbGain = 1.26f ); + void setLateReverbDelay(float lateReverbDelay = 0.011f); + void setAirAbsorptionGainHf(float absorptionGainHf = 0.994f); + void setDecayHfLimit(bool decayHfLimit = true); }; #endif // REVERBEFFECT_H From 99429f302bba7015b1bc3d6373423234bf8a9f1b Mon Sep 17 00:00:00 2001 From: Artem Popov Date: Sat, 16 Feb 2019 07:27:29 -0500 Subject: [PATCH 06/15] Minor formatting improvements --- rwengine/src/audio/ReverbEffect.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rwengine/src/audio/ReverbEffect.cpp b/rwengine/src/audio/ReverbEffect.cpp index e69fe7351..e48b43736 100644 --- a/rwengine/src/audio/ReverbEffect.cpp +++ b/rwengine/src/audio/ReverbEffect.cpp @@ -27,18 +27,18 @@ void ReverbEffect::setDecayTime(float t) { alEffectf(id, AL_REVERB_DECAY_TIME, t); } -void ReverbEffect :: setLateReverbGain (float g) { +void ReverbEffect::setLateReverbGain(float g) { alEffectf(id, AL_REVERB_LATE_REVERB_GAIN, g); } -void ReverbEffect :: setLateReverbDelay (float t) { +void ReverbEffect::setLateReverbDelay(float t) { alEffectf(id, AL_REVERB_LATE_REVERB_DELAY, t); } -void ReverbEffect :: setAirAbsorptionGainHf (float g) { +void ReverbEffect::setAirAbsorptionGainHf(float g) { alEffectf(id, AL_REVERB_AIR_ABSORPTION_GAINHF, g); } -void ReverbEffect :: setDecayHfLimit (bool flag) { +void ReverbEffect::setDecayHfLimit(bool flag) { alEffecti(id, AL_REVERB_DECAY_HFLIMIT, flag ? AL_TRUE : AL_FALSE); } From 519536de8e0f3a5bd17f563a1c5031520d93a5a3 Mon Sep 17 00:00:00 2001 From: Artem Popov Date: Sat, 16 Feb 2019 07:28:31 -0500 Subject: [PATCH 07/15] Remove duplication --- rwengine/src/audio/ReverbEffect.h | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 rwengine/src/audio/ReverbEffect.h diff --git a/rwengine/src/audio/ReverbEffect.h b/rwengine/src/audio/ReverbEffect.h deleted file mode 100644 index 209068795..000000000 --- a/rwengine/src/audio/ReverbEffect.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef REVERBEFFECT_H -#define REVERBEFFECT_H - - -class ReverbEffect -{ -public: - ReverbEffect(); -}; - -#endif // REVERBEFFECT_H From 8496f6a817bbf49fd27162ff76ec1e939d9e1b6b Mon Sep 17 00:00:00 2001 From: Artem Popov Date: Sat, 16 Feb 2019 07:34:24 -0500 Subject: [PATCH 08/15] Deleted useless line --- rwengine/src/audio/SoundManager.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/rwengine/src/audio/SoundManager.cpp b/rwengine/src/audio/SoundManager.cpp index 778803e5c..2f2e3ba2c 100644 --- a/rwengine/src/audio/SoundManager.cpp +++ b/rwengine/src/audio/SoundManager.cpp @@ -98,8 +98,6 @@ bool SoundManager::initializeEFX() { } initEfxFunctionPointers(); - - SoundEffect effect(0); return true; } From 7e2f208a2a3fa6c101b33837f75b8adb006a0762 Mon Sep 17 00:00:00 2001 From: ArtemPopof Date: Mon, 18 Feb 2019 14:50:50 -0500 Subject: [PATCH 09/15] Minor format and refactor --- rwengine/src/audio/EffectSlot.cpp | 3 --- rwengine/src/audio/EffectSlot.hpp | 4 ++-- rwengine/src/audio/ReverbEffect.hpp | 18 +++++++++--------- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/rwengine/src/audio/EffectSlot.cpp b/rwengine/src/audio/EffectSlot.cpp index bbc2698c9..d851bac24 100644 --- a/rwengine/src/audio/EffectSlot.cpp +++ b/rwengine/src/audio/EffectSlot.cpp @@ -5,9 +5,6 @@ EffectSlot::EffectSlot() { alGenAuxiliaryEffectSlots(1, &slotId); - effect = nullptr; - gain = 1.0f; - slotNumber = 0; created = alGetError() == AL_NO_ERROR; } diff --git a/rwengine/src/audio/EffectSlot.hpp b/rwengine/src/audio/EffectSlot.hpp index d9fe0b7e5..f90f84fbf 100644 --- a/rwengine/src/audio/EffectSlot.hpp +++ b/rwengine/src/audio/EffectSlot.hpp @@ -48,12 +48,12 @@ class EffectSlot { */ std::shared_ptr effect; - ALuint slotId; + ALuint slotId = 0; /** * This is flag of successfull slot creation */ bool created; - float gain; + float gain = 1.0f; /// OpenAL aux slot int slotNumber; diff --git a/rwengine/src/audio/ReverbEffect.hpp b/rwengine/src/audio/ReverbEffect.hpp index d7fb6cd19..eb375f1a8 100644 --- a/rwengine/src/audio/ReverbEffect.hpp +++ b/rwengine/src/audio/ReverbEffect.hpp @@ -8,15 +8,15 @@ class ReverbEffect : public SoundEffect { public: ReverbEffect(); - void setDensity ( float density = 1.0f ); - void setDiffusion ( float diffusion = 1.0f ); - void setGain ( float gain = 0.32f ); - void setGainHf ( float gainHf = 0.89f ); - void setDecayTime ( float decayTime = 1.49f ); - void setLateReverbGain ( float lateReverbGain = 1.26f ); - void setLateReverbDelay ( float lateReverbDelay = 0.011f ); - void setAirAbsorptionGainHf ( float absorptionGainHf = 0.994f ); - void setDecayHfLimit ( bool decayHfLimit = true ); + void setDensity(float density = 1.0f); + void setDiffusion(float diffusion = 1.0f); + void setGain(float gain = 0.32f); + void setGainHf(float gainHf = 0.89f); + void setDecayTime(float decayTime = 1.49f); + void setLateReverbGain(float lateReverbGain = 1.26f ); + void setLateReverbDelay(float lateReverbDelay = 0.011f); + void setAirAbsorptionGainHf(float absorptionGainHf = 0.994f); + void setDecayHfLimit(bool decayHfLimit = true); }; #endif // REVERBEFFECT_H From b673ab6ec9684f553c84b6c20651c4188e4985cb Mon Sep 17 00:00:00 2001 From: ArtemPopof Date: Mon, 18 Feb 2019 15:02:44 -0500 Subject: [PATCH 10/15] Extracted variable assigning from fucntion call --- rwengine/src/audio/EffectSlot.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rwengine/src/audio/EffectSlot.cpp b/rwengine/src/audio/EffectSlot.cpp index d851bac24..18ef435ff 100644 --- a/rwengine/src/audio/EffectSlot.cpp +++ b/rwengine/src/audio/EffectSlot.cpp @@ -32,5 +32,6 @@ bool EffectSlot::detachEffect() { } void EffectSlot::setGain(float gain) { - alAuxiliaryEffectSlotf(slotId, AL_EFFECTSLOT_GAIN, this->gain = gain); + this->gain = gain; + alAuxiliaryEffectSlotf(slotId, AL_EFFECTSLOT_GAIN, gain); } From 1dbabd193f078017f538283733f30d5daa1b3aa3 Mon Sep 17 00:00:00 2001 From: ArtemPopof Date: Mon, 18 Feb 2019 15:36:15 -0500 Subject: [PATCH 11/15] memory cleanup improvements minor refactoring --- rwengine/src/audio/EffectSlot.hpp | 4 ++-- rwengine/src/audio/ReverbEffect.hpp | 6 +++--- rwengine/src/audio/Sound.cpp | 8 ++++++++ rwengine/src/audio/Sound.hpp | 4 +++- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/rwengine/src/audio/EffectSlot.hpp b/rwengine/src/audio/EffectSlot.hpp index f90f84fbf..0f02a50f7 100644 --- a/rwengine/src/audio/EffectSlot.hpp +++ b/rwengine/src/audio/EffectSlot.hpp @@ -48,7 +48,7 @@ class EffectSlot { */ std::shared_ptr effect; - ALuint slotId = 0; + ALuint slotId; /** * This is flag of successfull slot creation */ @@ -56,7 +56,7 @@ class EffectSlot { float gain = 1.0f; /// OpenAL aux slot - int slotNumber; + int slotNumber = 0; }; #endif // EFFECTSLOT_H diff --git a/rwengine/src/audio/ReverbEffect.hpp b/rwengine/src/audio/ReverbEffect.hpp index eb375f1a8..6a1f46b92 100644 --- a/rwengine/src/audio/ReverbEffect.hpp +++ b/rwengine/src/audio/ReverbEffect.hpp @@ -9,10 +9,10 @@ class ReverbEffect : public SoundEffect { ReverbEffect(); void setDensity(float density = 1.0f); - void setDiffusion(float diffusion = 1.0f); - void setGain(float gain = 0.32f); + void setDiffusion(float diffusion = 0.3f); + void setGain(float gain = 0.92f); void setGainHf(float gainHf = 0.89f); - void setDecayTime(float decayTime = 1.49f); + void setDecayTime(float decayTime = 5.49f); void setLateReverbGain(float lateReverbGain = 1.26f ); void setLateReverbDelay(float lateReverbDelay = 0.011f); void setAirAbsorptionGainHf(float absorptionGainHf = 0.994f); diff --git a/rwengine/src/audio/Sound.cpp b/rwengine/src/audio/Sound.cpp index ebea663f6..d88c62977 100644 --- a/rwengine/src/audio/Sound.cpp +++ b/rwengine/src/audio/Sound.cpp @@ -2,6 +2,13 @@ #include "audio/SoundBuffer.hpp" +Sound::~Sound() +{ + if (effectSlot != nullptr) { + buffer->detachFromEffectSlot(effectSlot); + } +} + bool Sound::isPlaying() const { return buffer->isPlaying(); } @@ -48,6 +55,7 @@ void Sound::setMaxDistance(float maxDist) { void Sound::attachToEffectSlot(const std::shared_ptr effectSlot) { + this->effectSlot = effectSlot; buffer->attachToEffectSlot(effectSlot); } diff --git a/rwengine/src/audio/Sound.hpp b/rwengine/src/audio/Sound.hpp index 72b5b8ab0..1e502d5ae 100644 --- a/rwengine/src/audio/Sound.hpp +++ b/rwengine/src/audio/Sound.hpp @@ -19,8 +19,10 @@ struct Sound { std::shared_ptr source; std::unique_ptr buffer; + std::shared_ptr effectSlot; + Sound() = default; - ~Sound() = default; + ~Sound(); bool isPlaying() const; From dd0dd4429688928db9b28674d7e83b0a85de0571 Mon Sep 17 00:00:00 2001 From: ArtemPopof Date: Mon, 18 Feb 2019 15:52:18 -0500 Subject: [PATCH 12/15] made SoundEffect constructor virtual --- rwengine/src/audio/SoundEffect.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rwengine/src/audio/SoundEffect.hpp b/rwengine/src/audio/SoundEffect.hpp index cc0e105cd..8164024f7 100644 --- a/rwengine/src/audio/SoundEffect.hpp +++ b/rwengine/src/audio/SoundEffect.hpp @@ -16,7 +16,7 @@ class SoundEffect { * @param type OpenAl specific effect type. */ SoundEffect(ALint type); - ~SoundEffect(); + virtual ~SoundEffect(); ALuint getId() const { return id; From ce7c45f94c242dcea4985fde31d9120ba5875ccf Mon Sep 17 00:00:00 2001 From: ArtemPopof Date: Mon, 18 Feb 2019 16:07:14 -0500 Subject: [PATCH 13/15] argument pass method improvements --- rwengine/src/audio/Sound.cpp | 4 ++-- rwengine/src/audio/SoundBuffer.cpp | 4 ++-- rwengine/src/audio/SoundBuffer.hpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rwengine/src/audio/Sound.cpp b/rwengine/src/audio/Sound.cpp index d88c62977..d9be16a53 100644 --- a/rwengine/src/audio/Sound.cpp +++ b/rwengine/src/audio/Sound.cpp @@ -55,8 +55,8 @@ void Sound::setMaxDistance(float maxDist) { void Sound::attachToEffectSlot(const std::shared_ptr effectSlot) { - this->effectSlot = effectSlot; - buffer->attachToEffectSlot(effectSlot); + this->effectSlot = std::move(effectSlot); + buffer->attachToEffectSlot(this->effectSlot); } size_t Sound::getScriptObjectID() const { diff --git a/rwengine/src/audio/SoundBuffer.cpp b/rwengine/src/audio/SoundBuffer.cpp index 583e5ec62..88a859369 100644 --- a/rwengine/src/audio/SoundBuffer.cpp +++ b/rwengine/src/audio/SoundBuffer.cpp @@ -87,10 +87,10 @@ void SoundBuffer::setMaxDistance(float maxDist) { alCheck(alSourcef(source, AL_MAX_DISTANCE, maxDist)); } -void SoundBuffer::attachToEffectSlot(const std::shared_ptr slot) { +void SoundBuffer::attachToEffectSlot(const std::shared_ptr &slot) { alCheck(alSource3i(source, AL_AUXILIARY_SEND_FILTER, slot->getSlotId(), slot->getSlotNumber(), AL_FILTER_NULL)); } -void SoundBuffer::detachFromEffectSlot(const std::shared_ptr slot) { +void SoundBuffer::detachFromEffectSlot(const std::shared_ptr &slot) { alCheck(alSource3i (source, AL_AUXILIARY_SEND_FILTER, AL_EFFECTSLOT_NULL, slot->getSlotNumber(), AL_FILTER_NULL)); } diff --git a/rwengine/src/audio/SoundBuffer.hpp b/rwengine/src/audio/SoundBuffer.hpp index 5c063cc49..0763fb869 100644 --- a/rwengine/src/audio/SoundBuffer.hpp +++ b/rwengine/src/audio/SoundBuffer.hpp @@ -30,8 +30,8 @@ struct SoundBuffer { void setGain(float gain); void setMaxDistance(float maxDist); - void attachToEffectSlot(const std::shared_ptr effectSlot); - void detachFromEffectSlot(const std::shared_ptr effectSlot); + void attachToEffectSlot(const std::shared_ptr &effectSlot); + void detachFromEffectSlot(const std::shared_ptr &effectSlot); ALuint source; ALuint buffer; From 1b4cc6103a0d83d0c3595df49dd99b57499ebf09 Mon Sep 17 00:00:00 2001 From: ArtemPopof Date: Wed, 20 Feb 2019 11:26:08 -0500 Subject: [PATCH 14/15] Refactor of EFX classes SoundEffect and SoundSlot is merged SoundManager contains all pre-initialized effects --- rwengine/src/audio/Sound.cpp | 10 +++++----- rwengine/src/audio/Sound.hpp | 8 +++++--- rwengine/src/audio/SoundBuffer.cpp | 9 ++++----- rwengine/src/audio/SoundBuffer.hpp | 6 +++--- rwengine/src/audio/SoundEffect.cpp | 25 +++++++++++++++++++++++++ rwengine/src/audio/SoundEffect.hpp | 26 ++++++++++++++++++++++++++ rwengine/src/audio/SoundManager.cpp | 19 ++++++++++++++++--- rwengine/src/audio/SoundManager.hpp | 10 ++++++++-- rwgame/states/IngameState.cpp | 9 +++++++++ 9 files changed, 101 insertions(+), 21 deletions(-) diff --git a/rwengine/src/audio/Sound.cpp b/rwengine/src/audio/Sound.cpp index d9be16a53..d186fd975 100644 --- a/rwengine/src/audio/Sound.cpp +++ b/rwengine/src/audio/Sound.cpp @@ -4,8 +4,8 @@ Sound::~Sound() { - if (effectSlot != nullptr) { - buffer->detachFromEffectSlot(effectSlot); + if (effect != nullptr) { + buffer->disableEffect(effect); } } @@ -53,10 +53,10 @@ void Sound::setMaxDistance(float maxDist) { buffer->setMaxDistance(maxDist); } -void Sound::attachToEffectSlot(const std::shared_ptr effectSlot) +void Sound::enableEffect(std::shared_ptr effect) { - this->effectSlot = std::move(effectSlot); - buffer->attachToEffectSlot(this->effectSlot); + this->effect = std::move(effect); + buffer->enableEffect(this->effect); } size_t Sound::getScriptObjectID() const { diff --git a/rwengine/src/audio/Sound.hpp b/rwengine/src/audio/Sound.hpp index 1e502d5ae..6eb857cbf 100644 --- a/rwengine/src/audio/Sound.hpp +++ b/rwengine/src/audio/Sound.hpp @@ -1,12 +1,14 @@ #ifndef _RWENGINE_SOUND_HPP_ #define _RWENGINE_SOUND_HPP_ +#include