AuroraOpenALSoft/OpenAL32/alAuxEffectSlot.c

729 lines
21 KiB
C
Raw Normal View History

/**
* OpenAL cross platform audio library
* Copyright (C) 1999-2007 by authors.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* Or go to http://www.gnu.org/copyleft/lgpl.html
*/
#include "config.h"
2008-01-16 22:09:04 +00:00
#include <stdlib.h>
Implement AL_EFFECT_REVERB Here is a quick description of how the reverb effect works: +--->---+*(4) | V new sample +-----+---+---+ | |extra|ltr|ref| <- +*(1) +-----+---+---+ (3,5)*| |*(2) +-->| V out sample 1) Apply master reverb gain to incoming sample and place it at the head of the buffer. The master reverb gainhf was already applied when the source was initially mixed. 2) Copy the delayed reflection sample to an output sample and apply the reflection gain. 3) Apply the late reverb gain to the late reverb sample 4) Copy the end of the buffer, applying a decay gain and the decay hf ratio, and add to the late reverb. 5) Copy the late reverb sample, adding to the output sample. Then the head and sampling points are shifted forward, and done again for each new sample. The extra buffer length is determined by the Reverb Density property. A value of 0 gives a length of 0.1 seconds (long, with fairly distinct echos) , and 1 gives 0.075 seconds (short, indistinct echos). The decay gain is calculated such that after a number of loops to satisfy the Decay Time, a sample will be 1/32768th as powerful (virtually insignificant to the resulting output, and only getting further reduced). It is calculated as: DecayGain = pow(1.0f/32768.0f, 1.0/(DecayTime/ExtraLength)); Things to note: Reverb Diffusion is not currently handled, nor is Decay HF Limit. Decay HF Ratios above 1 probably give incorrect results. Also, this method likely sucks, but it's the best I can come up with before release. :)
2008-01-19 05:25:40 +00:00
#include <math.h>
2008-01-16 22:09:04 +00:00
#include "AL/al.h"
#include "AL/alc.h"
#include "alMain.h"
#include "alAuxEffectSlot.h"
#include "alThunk.h"
#include "alError.h"
#include "alListener.h"
#include "alSource.h"
#include "almalloc.h"
extern inline void LockEffectSlotsRead(ALCcontext *context);
extern inline void UnlockEffectSlotsRead(ALCcontext *context);
extern inline void LockEffectSlotsWrite(ALCcontext *context);
extern inline void UnlockEffectSlotsWrite(ALCcontext *context);
2013-11-04 21:44:46 +00:00
extern inline struct ALeffectslot *LookupEffectSlot(ALCcontext *context, ALuint id);
extern inline struct ALeffectslot *RemoveEffectSlot(ALCcontext *context, ALuint id);
static UIntMap EffectStateFactoryMap;
2013-05-29 05:27:07 +00:00
static inline ALeffectStateFactory *getFactoryByType(ALenum type)
{
ALeffectStateFactory* (*getFactory)(void) = LookupUIntMapKey(&EffectStateFactoryMap, type);
if(getFactory != NULL)
return getFactory();
return NULL;
}
static void ALeffectState_IncRef(ALeffectState *state);
static void ALeffectState_DecRef(ALeffectState *state);
#define DO_UPDATEPROPS() do { \
if(!ATOMIC_LOAD(&context->DeferUpdates, almemory_order_acquire)) \
UpdateEffectSlotProps(slot); \
else \
ATOMIC_FLAG_CLEAR(&slot->PropsClean, almemory_order_release); \
} while(0)
2010-05-12 09:20:14 +00:00
AL_API ALvoid AL_APIENTRY alGenAuxiliaryEffectSlots(ALsizei n, ALuint *effectslots)
{
2013-10-07 18:30:11 +00:00
ALCcontext *context;
ALeffectslot **tmpslots = NULL;
2013-10-07 18:30:11 +00:00
ALsizei cur;
ALenum err;
2013-10-07 18:30:11 +00:00
context = GetContextRef();
if(!context) return;
2013-10-07 18:30:11 +00:00
if(!(n >= 0))
SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
tmpslots = al_malloc(DEF_ALIGN, sizeof(ALeffectslot*)*n);
LockEffectSlotsWrite(context);
2013-10-07 18:30:11 +00:00
for(cur = 0;cur < n;cur++)
{
2013-10-07 18:30:11 +00:00
ALeffectslot *slot = al_calloc(16, sizeof(ALeffectslot));
err = AL_OUT_OF_MEMORY;
if(!slot || (err=InitEffectSlot(slot)) != AL_NO_ERROR)
{
2013-10-07 18:30:11 +00:00
al_free(slot);
UnlockEffectSlotsWrite(context);
2013-10-07 18:30:11 +00:00
alDeleteAuxiliaryEffectSlots(cur, effectslots);
SET_ERROR_AND_GOTO(context, err, done);
}
2013-10-07 18:30:11 +00:00
err = NewThunkEntry(&slot->id);
if(err == AL_NO_ERROR)
err = InsertUIntMapEntryNoLock(&context->EffectSlotMap, slot->id, slot);
if(err != AL_NO_ERROR)
2013-03-24 20:55:41 +00:00
{
2013-10-07 18:30:11 +00:00
FreeThunkEntry(slot->id);
ALeffectState_DecRef(slot->Effect.State);
if(slot->Params.EffectState)
ALeffectState_DecRef(slot->Params.EffectState);
2013-10-07 18:30:11 +00:00
al_free(slot);
UnlockEffectSlotsWrite(context);
2013-10-07 18:30:11 +00:00
alDeleteAuxiliaryEffectSlots(cur, effectslots);
2013-10-07 18:30:11 +00:00
SET_ERROR_AND_GOTO(context, err, done);
2013-03-24 20:55:41 +00:00
}
2013-10-07 18:30:11 +00:00
aluInitEffectPanning(slot);
tmpslots[cur] = slot;
2013-10-07 18:30:11 +00:00
effectslots[cur] = slot->id;
}
if(n > 0)
{
struct ALeffectslotArray *curarray = ATOMIC_LOAD(&context->ActiveAuxSlots, almemory_order_acquire);
struct ALeffectslotArray *newarray = NULL;
ALsizei newcount = curarray->count + n;
ALCdevice *device;
newarray = al_calloc(DEF_ALIGN, FAM_SIZE(struct ALeffectslotArray, slot, newcount));
newarray->count = newcount;
memcpy(newarray->slot, tmpslots, sizeof(ALeffectslot*)*n);
if(curarray)
memcpy(newarray->slot+n, curarray->slot, sizeof(ALeffectslot*)*curarray->count);
newarray = ATOMIC_EXCHANGE_PTR(&context->ActiveAuxSlots, newarray,
almemory_order_acq_rel);
device = context->Device;
while((ATOMIC_LOAD(&device->MixCount, almemory_order_acquire)&1))
althrd_yield();
al_free(newarray);
}
UnlockEffectSlotsWrite(context);
2013-10-07 18:30:11 +00:00
done:
al_free(tmpslots);
2013-10-07 18:30:11 +00:00
ALCcontext_DecRef(context);
}
2011-09-22 07:31:42 +00:00
AL_API ALvoid AL_APIENTRY alDeleteAuxiliaryEffectSlots(ALsizei n, const ALuint *effectslots)
{
2013-10-07 18:30:11 +00:00
ALCcontext *context;
ALeffectslot *slot;
ALsizei i;
2013-10-07 18:30:11 +00:00
context = GetContextRef();
if(!context) return;
LockEffectSlotsWrite(context);
2013-10-07 18:30:11 +00:00
if(!(n >= 0))
SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
for(i = 0;i < n;i++)
{
2013-10-07 18:30:11 +00:00
if((slot=LookupEffectSlot(context, effectslots[i])) == NULL)
SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
if(ReadRef(&slot->ref) != 0)
2013-10-07 18:30:11 +00:00
SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
}
2013-10-07 18:30:11 +00:00
// All effectslots are valid
if(n > 0)
{
struct ALeffectslotArray *curarray = ATOMIC_LOAD(&context->ActiveAuxSlots, almemory_order_acquire);
struct ALeffectslotArray *newarray = NULL;
ALsizei newcount = curarray->count - n;
ALCdevice *device;
ALsizei j, k;
assert(newcount >= 0);
newarray = al_calloc(DEF_ALIGN, FAM_SIZE(struct ALeffectslotArray, slot, newcount));
newarray->count = newcount;
for(i = j = 0;i < newarray->count;)
{
slot = curarray->slot[j++];
for(k = 0;k < n;k++)
{
if(slot->id == effectslots[k])
break;
}
if(k == n)
newarray->slot[i++] = slot;
}
newarray = ATOMIC_EXCHANGE_PTR(&context->ActiveAuxSlots, newarray,
almemory_order_acq_rel);
device = context->Device;
while((ATOMIC_LOAD(&device->MixCount, almemory_order_acquire)&1))
althrd_yield();
al_free(newarray);
}
2013-10-07 18:30:11 +00:00
for(i = 0;i < n;i++)
{
if((slot=RemoveEffectSlot(context, effectslots[i])) == NULL)
continue;
FreeThunkEntry(slot->id);
DeinitEffectSlot(slot);
2013-10-07 18:30:11 +00:00
memset(slot, 0, sizeof(*slot));
al_free(slot);
}
2013-10-07 18:30:11 +00:00
done:
UnlockEffectSlotsWrite(context);
2013-10-07 18:30:11 +00:00
ALCcontext_DecRef(context);
}
AL_API ALboolean AL_APIENTRY alIsAuxiliaryEffectSlot(ALuint effectslot)
{
2013-10-07 18:30:11 +00:00
ALCcontext *context;
ALboolean ret;
2013-10-07 18:30:11 +00:00
context = GetContextRef();
if(!context) return AL_FALSE;
LockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
ret = (LookupEffectSlot(context, effectslot) ? AL_TRUE : AL_FALSE);
UnlockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
ALCcontext_DecRef(context);
2013-10-07 18:30:11 +00:00
return ret;
}
AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint value)
{
2013-10-07 18:30:11 +00:00
ALCdevice *device;
ALCcontext *context;
ALeffectslot *slot;
ALeffect *effect = NULL;
ALenum err;
2013-10-07 18:30:11 +00:00
context = GetContextRef();
if(!context) return;
WriteLock(&context->PropLock);
LockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
if((slot=LookupEffectSlot(context, effectslot)) == NULL)
SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
switch(param)
{
2013-10-07 18:30:11 +00:00
case AL_EFFECTSLOT_EFFECT:
device = context->Device;
LockEffectsRead(device);
2013-10-07 18:30:11 +00:00
effect = (value ? LookupEffect(device, value) : NULL);
if(!(value == 0 || effect != NULL))
{
UnlockEffectsRead(device);
2013-10-07 18:30:11 +00:00
SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
}
2013-10-07 18:30:11 +00:00
err = InitializeEffect(device, slot, effect);
UnlockEffectsRead(device);
2013-10-07 18:30:11 +00:00
if(err != AL_NO_ERROR)
SET_ERROR_AND_GOTO(context, err, done);
break;
2013-10-07 18:30:11 +00:00
case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
if(!(value == AL_TRUE || value == AL_FALSE))
SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
slot->AuxSendAuto = value;
break;
2013-10-07 18:30:11 +00:00
default:
SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
}
DO_UPDATEPROPS();
2013-10-07 18:30:11 +00:00
done:
UnlockEffectSlotsRead(context);
WriteUnlock(&context->PropLock);
2013-10-07 18:30:11 +00:00
ALCcontext_DecRef(context);
}
AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, const ALint *values)
{
2013-10-07 18:30:11 +00:00
ALCcontext *context;
switch(param)
{
2013-10-07 18:30:11 +00:00
case AL_EFFECTSLOT_EFFECT:
case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
alAuxiliaryEffectSloti(effectslot, param, values[0]);
return;
}
2013-10-07 18:30:11 +00:00
context = GetContextRef();
if(!context) return;
LockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
if(LookupEffectSlot(context, effectslot) == NULL)
SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
switch(param)
{
2013-10-07 18:30:11 +00:00
default:
SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
}
2013-10-07 18:30:11 +00:00
done:
UnlockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
ALCcontext_DecRef(context);
}
AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat value)
{
2013-10-07 18:30:11 +00:00
ALCcontext *context;
ALeffectslot *slot;
2013-10-07 18:30:11 +00:00
context = GetContextRef();
if(!context) return;
WriteLock(&context->PropLock);
LockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
if((slot=LookupEffectSlot(context, effectslot)) == NULL)
SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
switch(param)
{
2013-10-07 18:30:11 +00:00
case AL_EFFECTSLOT_GAIN:
if(!(value >= 0.0f && value <= 1.0f))
SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
slot->Gain = value;
break;
2007-12-18 23:47:24 +00:00
2013-10-07 18:30:11 +00:00
default:
SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
}
DO_UPDATEPROPS();
2013-10-07 18:30:11 +00:00
done:
UnlockEffectSlotsRead(context);
WriteUnlock(&context->PropLock);
2013-10-07 18:30:11 +00:00
ALCcontext_DecRef(context);
}
AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, const ALfloat *values)
{
2013-10-07 18:30:11 +00:00
ALCcontext *context;
switch(param)
{
2013-10-07 18:30:11 +00:00
case AL_EFFECTSLOT_GAIN:
alAuxiliaryEffectSlotf(effectslot, param, values[0]);
return;
}
2013-10-07 18:30:11 +00:00
context = GetContextRef();
if(!context) return;
LockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
if(LookupEffectSlot(context, effectslot) == NULL)
SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
switch(param)
{
2013-10-07 18:30:11 +00:00
default:
SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
}
2013-10-07 18:30:11 +00:00
done:
UnlockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
ALCcontext_DecRef(context);
}
AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint *value)
{
2013-10-07 18:30:11 +00:00
ALCcontext *context;
ALeffectslot *slot;
2013-10-07 18:30:11 +00:00
context = GetContextRef();
if(!context) return;
LockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
if((slot=LookupEffectSlot(context, effectslot)) == NULL)
SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
switch(param)
{
2013-10-07 18:30:11 +00:00
case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
*value = slot->AuxSendAuto;
break;
2013-10-07 18:30:11 +00:00
default:
SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
}
2013-10-07 18:30:11 +00:00
done:
UnlockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
ALCcontext_DecRef(context);
}
AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, ALint *values)
{
2013-10-07 18:30:11 +00:00
ALCcontext *context;
switch(param)
{
2013-10-07 18:30:11 +00:00
case AL_EFFECTSLOT_EFFECT:
case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
alGetAuxiliaryEffectSloti(effectslot, param, values);
return;
}
2013-10-07 18:30:11 +00:00
context = GetContextRef();
if(!context) return;
LockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
if(LookupEffectSlot(context, effectslot) == NULL)
SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
switch(param)
{
2013-10-07 18:30:11 +00:00
default:
SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
}
2013-10-07 18:30:11 +00:00
done:
UnlockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
ALCcontext_DecRef(context);
}
AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat *value)
{
2013-10-07 18:30:11 +00:00
ALCcontext *context;
ALeffectslot *slot;
2013-10-07 18:30:11 +00:00
context = GetContextRef();
if(!context) return;
LockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
if((slot=LookupEffectSlot(context, effectslot)) == NULL)
SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
switch(param)
{
2013-10-07 18:30:11 +00:00
case AL_EFFECTSLOT_GAIN:
*value = slot->Gain;
break;
2007-12-18 23:47:24 +00:00
2013-10-07 18:30:11 +00:00
default:
SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
}
2013-10-07 18:30:11 +00:00
done:
UnlockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
ALCcontext_DecRef(context);
}
AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, ALfloat *values)
{
2013-10-07 18:30:11 +00:00
ALCcontext *context;
switch(param)
{
2013-10-07 18:30:11 +00:00
case AL_EFFECTSLOT_GAIN:
alGetAuxiliaryEffectSlotf(effectslot, param, values);
return;
}
2013-10-07 18:30:11 +00:00
context = GetContextRef();
if(!context) return;
LockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
if(LookupEffectSlot(context, effectslot) == NULL)
SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
switch(param)
{
2013-10-07 18:30:11 +00:00
default:
SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
}
2013-10-07 18:30:11 +00:00
done:
UnlockEffectSlotsRead(context);
2013-10-07 18:30:11 +00:00
ALCcontext_DecRef(context);
}
void InitEffectFactoryMap(void)
{
InitUIntMap(&EffectStateFactoryMap, INT_MAX);
InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_NULL, ALnullStateFactory_getFactory);
InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_EAXREVERB, ALreverbStateFactory_getFactory);
InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_REVERB, ALreverbStateFactory_getFactory);
InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_CHORUS, ALchorusStateFactory_getFactory);
2013-10-03 14:55:12 +00:00
InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_COMPRESSOR, ALcompressorStateFactory_getFactory);
InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_DISTORTION, ALdistortionStateFactory_getFactory);
InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_ECHO, ALechoStateFactory_getFactory);
InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_EQUALIZER, ALequalizerStateFactory_getFactory);
InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_FLANGER, ALflangerStateFactory_getFactory);
InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_RING_MODULATOR, ALmodulatorStateFactory_getFactory);
InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_DEDICATED_DIALOGUE, ALdedicatedStateFactory_getFactory);
InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT, ALdedicatedStateFactory_getFactory);
}
void DeinitEffectFactoryMap(void)
{
ResetUIntMap(&EffectStateFactoryMap);
}
ALenum InitializeEffect(ALCdevice *Device, ALeffectslot *EffectSlot, ALeffect *effect)
{
ALenum newtype = (effect ? effect->type : AL_EFFECT_NULL);
struct ALeffectslotProps *props;
ALeffectState *State;
if(newtype != EffectSlot->Effect.Type)
{
ALeffectStateFactory *factory;
2012-09-16 08:35:16 +00:00
FPUCtl oldMode;
factory = getFactoryByType(newtype);
if(!factory)
{
ERR("Failed to find factory for effect type 0x%04x\n", newtype);
return AL_INVALID_ENUM;
}
2013-11-03 00:30:28 +00:00
State = V0(factory,create)();
if(!State) return AL_OUT_OF_MEMORY;
2012-09-16 08:35:16 +00:00
SetMixerFPUMode(&oldMode);
almtx_lock(&Device->BackendLock);
State->OutBuffer = Device->Dry.Buffer;
State->OutChannels = Device->Dry.NumChannels;
2013-11-03 00:30:28 +00:00
if(V(State,deviceUpdate)(Device) == AL_FALSE)
2009-05-31 18:54:49 +00:00
{
almtx_unlock(&Device->BackendLock);
RestoreFPUMode(&oldMode);
ALeffectState_DecRef(State);
return AL_OUT_OF_MEMORY;
2009-05-31 18:54:49 +00:00
}
almtx_unlock(&Device->BackendLock);
2012-09-16 08:35:16 +00:00
RestoreFPUMode(&oldMode);
if(!effect)
{
EffectSlot->Effect.Type = AL_EFFECT_NULL;
memset(&EffectSlot->Effect.Props, 0, sizeof(EffectSlot->Effect.Props));
}
else
{
EffectSlot->Effect.Type = effect->type;
EffectSlot->Effect.Props = effect->Props;
}
ALeffectState_DecRef(EffectSlot->Effect.State);
EffectSlot->Effect.State = State;
}
else if(effect)
EffectSlot->Effect.Props = effect->Props;
/* Remove state references from old effect slot property updates. */
props = ATOMIC_LOAD_SEQ(&EffectSlot->FreeList);
while(props)
{
2017-03-08 11:38:28 +00:00
if(props->State)
ALeffectState_DecRef(props->State);
props->State = NULL;
props = ATOMIC_LOAD(&props->next, almemory_order_relaxed);
}
return AL_NO_ERROR;
Implement AL_EFFECT_REVERB Here is a quick description of how the reverb effect works: +--->---+*(4) | V new sample +-----+---+---+ | |extra|ltr|ref| <- +*(1) +-----+---+---+ (3,5)*| |*(2) +-->| V out sample 1) Apply master reverb gain to incoming sample and place it at the head of the buffer. The master reverb gainhf was already applied when the source was initially mixed. 2) Copy the delayed reflection sample to an output sample and apply the reflection gain. 3) Apply the late reverb gain to the late reverb sample 4) Copy the end of the buffer, applying a decay gain and the decay hf ratio, and add to the late reverb. 5) Copy the late reverb sample, adding to the output sample. Then the head and sampling points are shifted forward, and done again for each new sample. The extra buffer length is determined by the Reverb Density property. A value of 0 gives a length of 0.1 seconds (long, with fairly distinct echos) , and 1 gives 0.075 seconds (short, indistinct echos). The decay gain is calculated such that after a number of loops to satisfy the Decay Time, a sample will be 1/32768th as powerful (virtually insignificant to the resulting output, and only getting further reduced). It is calculated as: DecayGain = pow(1.0f/32768.0f, 1.0/(DecayTime/ExtraLength)); Things to note: Reverb Diffusion is not currently handled, nor is Decay HF Limit. Decay HF Ratios above 1 probably give incorrect results. Also, this method likely sucks, but it's the best I can come up with before release. :)
2008-01-19 05:25:40 +00:00
}
static void ALeffectState_IncRef(ALeffectState *state)
{
uint ref;
ref = IncrementRef(&state->Ref);
TRACEREF("%p increasing refcount to %u\n", state, ref);
}
static void ALeffectState_DecRef(ALeffectState *state)
{
uint ref;
ref = DecrementRef(&state->Ref);
TRACEREF("%p decreasing refcount to %u\n", state, ref);
if(ref == 0) DELETE_OBJ(state);
}
void ALeffectState_Construct(ALeffectState *state)
{
InitRef(&state->Ref, 1);
state->OutBuffer = NULL;
state->OutChannels = 0;
}
void ALeffectState_Destruct(ALeffectState *UNUSED(state))
{
}
ALenum InitEffectSlot(ALeffectslot *slot)
{
ALeffectStateFactory *factory;
slot->Effect.Type = AL_EFFECT_NULL;
factory = getFactoryByType(AL_EFFECT_NULL);
if(!(slot->Effect.State=V0(factory,create)()))
return AL_OUT_OF_MEMORY;
slot->Gain = 1.0;
slot->AuxSendAuto = AL_TRUE;
ATOMIC_FLAG_TEST_AND_SET(&slot->PropsClean, almemory_order_relaxed);
InitRef(&slot->ref, 0);
ATOMIC_INIT(&slot->Update, NULL);
ATOMIC_INIT(&slot->FreeList, NULL);
slot->Params.Gain = 1.0f;
slot->Params.AuxSendAuto = AL_TRUE;
ALeffectState_IncRef(slot->Effect.State);
slot->Params.EffectState = slot->Effect.State;
slot->Params.RoomRolloff = 0.0f;
slot->Params.DecayTime = 0.0f;
slot->Params.DecayHFRatio = 0.0f;
slot->Params.DecayHFLimit = AL_FALSE;
slot->Params.AirAbsorptionGainHF = 1.0f;
return AL_NO_ERROR;
}
void DeinitEffectSlot(ALeffectslot *slot)
{
struct ALeffectslotProps *props;
size_t count = 0;
props = ATOMIC_LOAD_SEQ(&slot->Update);
if(props)
{
2017-03-08 11:38:28 +00:00
if(props->State) ALeffectState_DecRef(props->State);
TRACE("Freed unapplied AuxiliaryEffectSlot update %p\n", props);
al_free(props);
}
props = ATOMIC_LOAD(&slot->FreeList, almemory_order_relaxed);
while(props)
{
2017-03-08 11:38:28 +00:00
struct ALeffectslotProps *next = ATOMIC_LOAD(&props->next, almemory_order_relaxed);
if(props->State) ALeffectState_DecRef(props->State);
al_free(props);
props = next;
++count;
}
TRACE("Freed "SZFMT" AuxiliaryEffectSlot property object%s\n", count, (count==1)?"":"s");
ALeffectState_DecRef(slot->Effect.State);
if(slot->Params.EffectState)
ALeffectState_DecRef(slot->Params.EffectState);
}
void UpdateEffectSlotProps(ALeffectslot *slot)
{
struct ALeffectslotProps *props;
ALeffectState *oldstate;
/* Get an unused property container, or allocate a new one as needed. */
props = ATOMIC_LOAD(&slot->FreeList, almemory_order_relaxed);
if(!props)
props = al_calloc(16, sizeof(*props));
else
{
struct ALeffectslotProps *next;
do {
next = ATOMIC_LOAD(&props->next, almemory_order_relaxed);
} while(ATOMIC_COMPARE_EXCHANGE_PTR_WEAK(&slot->FreeList, &props, next,
almemory_order_seq_cst, almemory_order_acquire) == 0);
}
/* Copy in current property values. */
2017-03-08 11:38:28 +00:00
props->Gain = slot->Gain;
props->AuxSendAuto = slot->AuxSendAuto;
2017-03-08 11:38:28 +00:00
props->Type = slot->Effect.Type;
props->Props = slot->Effect.Props;
/* Swap out any stale effect state object there may be in the container, to
* delete it.
*/
ALeffectState_IncRef(slot->Effect.State);
2017-03-08 11:38:28 +00:00
oldstate = props->State;
props->State = slot->Effect.State;
/* Set the new container for updating internal parameters. */
props = ATOMIC_EXCHANGE_PTR(&slot->Update, props, almemory_order_acq_rel);
if(props)
{
/* If there was an unused update container, put it back in the
* freelist.
*/
2016-12-22 03:58:03 +00:00
ATOMIC_REPLACE_HEAD(struct ALeffectslotProps*, &slot->FreeList, props);
}
if(oldstate)
ALeffectState_DecRef(oldstate);
}
void UpdateAllEffectSlotProps(ALCcontext *context)
{
struct ALeffectslotArray *auxslots;
ALsizei i;
LockEffectSlotsRead(context);
auxslots = ATOMIC_LOAD(&context->ActiveAuxSlots, almemory_order_acquire);
for(i = 0;i < auxslots->count;i++)
{
ALeffectslot *slot = auxslots->slot[i];
2017-03-24 02:32:53 +00:00
if(!ATOMIC_FLAG_TEST_AND_SET(&slot->PropsClean, almemory_order_acq_rel))
UpdateEffectSlotProps(slot);
}
UnlockEffectSlotsRead(context);
}
2008-01-16 00:22:39 +00:00
ALvoid ReleaseALAuxiliaryEffectSlots(ALCcontext *Context)
{
2010-05-12 09:20:14 +00:00
ALsizei pos;
for(pos = 0;pos < Context->EffectSlotMap.size;pos++)
{
ALeffectslot *temp = Context->EffectSlotMap.values[pos];
Context->EffectSlotMap.values[pos] = NULL;
DeinitEffectSlot(temp);
Implement AL_EFFECT_REVERB Here is a quick description of how the reverb effect works: +--->---+*(4) | V new sample +-----+---+---+ | |extra|ltr|ref| <- +*(1) +-----+---+---+ (3,5)*| |*(2) +-->| V out sample 1) Apply master reverb gain to incoming sample and place it at the head of the buffer. The master reverb gainhf was already applied when the source was initially mixed. 2) Copy the delayed reflection sample to an output sample and apply the reflection gain. 3) Apply the late reverb gain to the late reverb sample 4) Copy the end of the buffer, applying a decay gain and the decay hf ratio, and add to the late reverb. 5) Copy the late reverb sample, adding to the output sample. Then the head and sampling points are shifted forward, and done again for each new sample. The extra buffer length is determined by the Reverb Density property. A value of 0 gives a length of 0.1 seconds (long, with fairly distinct echos) , and 1 gives 0.075 seconds (short, indistinct echos). The decay gain is calculated such that after a number of loops to satisfy the Decay Time, a sample will be 1/32768th as powerful (virtually insignificant to the resulting output, and only getting further reduced). It is calculated as: DecayGain = pow(1.0f/32768.0f, 1.0/(DecayTime/ExtraLength)); Things to note: Reverb Diffusion is not currently handled, nor is Decay HF Limit. Decay HF Ratios above 1 probably give incorrect results. Also, this method likely sucks, but it's the best I can come up with before release. :)
2008-01-19 05:25:40 +00:00
FreeThunkEntry(temp->id);
memset(temp, 0, sizeof(ALeffectslot));
2012-09-27 01:00:38 +00:00
al_free(temp);
}
}