-
Notifications
You must be signed in to change notification settings - Fork 172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EFX environment and reverb effect. Closes #697. #702
base: main
Are you sure you want to change the base?
Changes from 6 commits
be920d3
8bd8e17
cb40cc3
958b7f7
7a22c06
c464756
99429f3
519536d
8496f6a
7e2f208
60ac44a
b673ab6
1dbabd1
dd0dd44
ce7c45f
1b4cc61
960d1a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include "EffectSlot.hpp" | ||
#include "SoundEffect.hpp" | ||
#include "OpenAlExtensions.hpp" | ||
|
||
EffectSlot::EffectSlot() { | ||
alGenAuxiliaryEffectSlots(1, &slotId); | ||
|
||
effect = nullptr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. default member initializer |
||
gain = 1.0f; | ||
slotNumber = 0; | ||
created = alGetError() == AL_NO_ERROR; | ||
} | ||
|
||
EffectSlot::~EffectSlot() { | ||
alDeleteAuxiliaryEffectSlots(1, &slotId); | ||
} | ||
|
||
bool EffectSlot::attachEffect(std::shared_ptr<SoundEffect> 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Split a variable assigning and a function invocation |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#ifndef EFFECTSLOT_H | ||
#define EFFECTSLOT_H | ||
|
||
#include <al.h> | ||
|
||
#include <memory> | ||
|
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have problems with understanding connection between this class and |
||
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<SoundEffect> 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<SoundEffect> effect; | ||
|
||
ALuint slotId; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or you can set members to defaults right here |
||
/** | ||
* This is flag of successfull slot creation | ||
*/ | ||
bool created; | ||
float gain; | ||
|
||
/// OpenAL aux slot | ||
int slotNumber; | ||
}; | ||
|
||
#endif // EFFECTSLOT_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<LPALGENEFFECTS>(alGetProcAddress("alGenEffects")); | ||
alDeleteEffects = reinterpret_cast<LPALDELETEEFFECTS>(alGetProcAddress("alDeleteEffects")); | ||
alEffecti = reinterpret_cast<LPALEFFECTI>(alGetProcAddress("alEffecti")); | ||
alEffectf = reinterpret_cast<LPALEFFECTF>(alGetProcAddress("alEffectf")); | ||
|
||
// aux slot | ||
alGenAuxiliaryEffectSlots = reinterpret_cast<LPALGENAUXILIARYEFFECTSLOTS>(alGetProcAddress("alGenAuxiliaryEffectSlots")); | ||
alDeleteAuxiliaryEffectSlots = reinterpret_cast<LPALDELETEAUXILIARYEFFECTSLOTS>(alGetProcAddress("alDeleteAuxiliaryEffectSlots")); | ||
alAuxiliaryEffectSloti = reinterpret_cast<LPALAUXILIARYEFFECTSLOTI>(alGetProcAddress("alAuxiliaryEffectSloti")); | ||
alAuxiliaryEffectSlotf = reinterpret_cast<LPALAUXILIARYEFFECTSLOTF>(alGetProcAddress("alAuxiliaryEffectSlotf")); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef OPENALEXTENSIONS_HPP | ||
#define OPENALEXTENSIONS_HPP | ||
|
||
#include <efx.h> | ||
|
||
/** | ||
* 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "ReverbEffect.hpp" | ||
#include "OpenAlExtensions.hpp" | ||
#include <efx.h> | ||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something wrong with a formatting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed it |
||
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef REVERBEFFECT_H | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this file is unneeded. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deleted. |
||
#define REVERBEFFECT_H | ||
|
||
|
||
class ReverbEffect | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another ReverbEffect header? |
||
{ | ||
public: | ||
ReverbEffect(); | ||
}; | ||
|
||
#endif // REVERBEFFECT_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,11 @@ void Sound::setMaxDistance(float maxDist) { | |
buffer->setMaxDistance(maxDist); | ||
} | ||
|
||
void Sound::attachToEffectSlot(const std::shared_ptr<EffectSlot> effectSlot) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also |
||
buffer->attachToEffectSlot(effectSlot); | ||
} | ||
|
||
size_t Sound::getScriptObjectID() const { | ||
return id; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
#include <memory> | ||
|
||
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> effectSlot); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should pass a shared_ptr by const reference or by value and use std::move |
||
|
||
size_t getScriptObjectID() const; | ||
}; | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
#include "audio/SoundBuffer.hpp" | ||
|
||
#include <rw/types.hpp> | ||
#include <efx.h> | ||
|
||
#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<EffectSlot> slot) { | ||
alCheck(alSource3i(source, AL_AUXILIARY_SEND_FILTER, slot->getSlotId(), slot->getSlotNumber(), AL_FILTER_NULL)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use const reference here |
||
} | ||
|
||
void SoundBuffer::detachFromEffectSlot(const std::shared_ptr<EffectSlot> slot) { | ||
alCheck(alSource3i (source, AL_AUXILIARY_SEND_FILTER, AL_EFFECTSLOT_NULL, slot->getSlotNumber(), AL_FILTER_NULL)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifndef SOUNDEFFECT_H | ||
#define SOUNDEFFECT_H | ||
|
||
#include <al.h> | ||
|
||
/** | ||
* 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should make the destructor virtual: |
||
|
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using an initialization list