2010-04-08 08:54:28 -07:00
|
|
|
/**
|
|
|
|
* OpenAL cross platform audio library
|
|
|
|
* Copyright (C) 2009 by Chris Robinson.
|
|
|
|
* 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
|
2014-08-18 14:11:03 +02:00
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2010-04-08 08:54:28 -07:00
|
|
|
* Or go to http://www.gnu.org/copyleft/lgpl.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "alMain.h"
|
|
|
|
#include "alFilter.h"
|
|
|
|
#include "alAuxEffectSlot.h"
|
|
|
|
#include "alError.h"
|
|
|
|
#include "alu.h"
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct ALmodulatorState {
|
2013-05-20 01:32:02 -07:00
|
|
|
DERIVE_FROM_TYPE(ALeffectState);
|
2010-04-08 09:36:20 -07:00
|
|
|
|
2010-04-08 10:37:09 -07:00
|
|
|
enum {
|
|
|
|
SINUSOID,
|
|
|
|
SAWTOOTH,
|
|
|
|
SQUARE
|
|
|
|
} Waveform;
|
|
|
|
|
|
|
|
ALuint index;
|
|
|
|
ALuint step;
|
|
|
|
|
2014-11-07 02:18:24 -08:00
|
|
|
ALfloat Gain[MAX_OUTPUT_CHANNELS];
|
2010-05-12 04:56:03 -07:00
|
|
|
|
2013-05-27 21:24:28 -07:00
|
|
|
ALfilterState Filter;
|
2010-04-08 08:54:28 -07:00
|
|
|
} ALmodulatorState;
|
|
|
|
|
2013-05-18 00:43:09 -07:00
|
|
|
#define WAVEFORM_FRACBITS 24
|
2011-09-23 23:11:35 -07:00
|
|
|
#define WAVEFORM_FRACONE (1<<WAVEFORM_FRACBITS)
|
|
|
|
#define WAVEFORM_FRACMASK (WAVEFORM_FRACONE-1)
|
2010-04-08 10:37:09 -07:00
|
|
|
|
2013-05-28 22:27:07 -07:00
|
|
|
static inline ALfloat Sin(ALuint index)
|
2010-04-08 10:37:09 -07:00
|
|
|
{
|
2013-10-08 16:31:23 -07:00
|
|
|
return sinf(index*(F_2PI/WAVEFORM_FRACONE) - F_PI)*0.5f + 0.5f;
|
2010-04-08 10:37:09 -07:00
|
|
|
}
|
|
|
|
|
2013-05-28 22:27:07 -07:00
|
|
|
static inline ALfloat Saw(ALuint index)
|
2010-04-08 10:37:09 -07:00
|
|
|
{
|
2013-05-18 00:43:09 -07:00
|
|
|
return (ALfloat)index / WAVEFORM_FRACONE;
|
2010-04-08 10:37:09 -07:00
|
|
|
}
|
|
|
|
|
2013-05-28 22:27:07 -07:00
|
|
|
static inline ALfloat Square(ALuint index)
|
2010-04-08 10:37:09 -07:00
|
|
|
{
|
2013-05-18 00:43:09 -07:00
|
|
|
return (ALfloat)((index >> (WAVEFORM_FRACBITS - 1)) & 1);
|
2010-04-08 10:37:09 -07:00
|
|
|
}
|
|
|
|
|
2011-07-01 01:48:04 -07:00
|
|
|
#define DECL_TEMPLATE(func) \
|
2011-07-16 03:35:51 -07:00
|
|
|
static void Process##func(ALmodulatorState *state, ALuint SamplesToDo, \
|
2013-05-22 15:11:39 -07:00
|
|
|
const ALfloat *restrict SamplesIn, \
|
2014-11-07 03:43:33 -08:00
|
|
|
ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels) \
|
2011-07-01 01:48:04 -07:00
|
|
|
{ \
|
|
|
|
const ALuint step = state->step; \
|
|
|
|
ALuint index = state->index; \
|
2013-05-20 04:16:48 -07:00
|
|
|
ALuint base; \
|
2011-07-01 01:48:04 -07:00
|
|
|
\
|
2013-05-20 04:16:48 -07:00
|
|
|
for(base = 0;base < SamplesToDo;) \
|
2011-07-01 01:48:04 -07:00
|
|
|
{ \
|
2014-12-18 09:23:55 -08:00
|
|
|
ALfloat temps[256]; \
|
|
|
|
ALuint td = minu(256, SamplesToDo-base); \
|
2013-05-20 04:16:48 -07:00
|
|
|
ALuint i, k; \
|
2011-07-01 01:48:04 -07:00
|
|
|
\
|
2013-05-20 04:16:48 -07:00
|
|
|
for(i = 0;i < td;i++) \
|
|
|
|
{ \
|
|
|
|
ALfloat samp; \
|
|
|
|
samp = SamplesIn[base+i]; \
|
2013-05-27 21:24:28 -07:00
|
|
|
samp = ALfilterState_processSingle(&state->Filter, samp); \
|
2013-05-20 04:16:48 -07:00
|
|
|
\
|
|
|
|
index += step; \
|
|
|
|
index &= WAVEFORM_FRACMASK; \
|
|
|
|
temps[i] = samp * func(index); \
|
|
|
|
} \
|
2011-07-01 01:48:04 -07:00
|
|
|
\
|
2014-11-07 03:43:33 -08:00
|
|
|
for(k = 0;k < NumChannels;k++) \
|
2013-05-20 04:16:48 -07:00
|
|
|
{ \
|
|
|
|
ALfloat gain = state->Gain[k]; \
|
2014-11-07 03:43:33 -08:00
|
|
|
if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD)) \
|
2013-05-20 04:16:48 -07:00
|
|
|
continue; \
|
|
|
|
\
|
|
|
|
for(i = 0;i < td;i++) \
|
|
|
|
SamplesOut[k][base+i] += gain * temps[i]; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
base += td; \
|
2011-07-01 01:48:04 -07:00
|
|
|
} \
|
|
|
|
state->index = index; \
|
|
|
|
}
|
|
|
|
|
|
|
|
DECL_TEMPLATE(Sin)
|
|
|
|
DECL_TEMPLATE(Saw)
|
|
|
|
DECL_TEMPLATE(Square)
|
|
|
|
|
|
|
|
#undef DECL_TEMPLATE
|
|
|
|
|
|
|
|
|
2013-10-07 08:34:54 -07:00
|
|
|
static ALvoid ALmodulatorState_Destruct(ALmodulatorState *UNUSED(state))
|
2010-04-08 08:54:28 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-07 08:34:54 -07:00
|
|
|
static ALboolean ALmodulatorState_deviceUpdate(ALmodulatorState *UNUSED(state), ALCdevice *UNUSED(device))
|
2010-04-08 08:54:28 -07:00
|
|
|
{
|
|
|
|
return AL_TRUE;
|
|
|
|
}
|
|
|
|
|
2013-05-29 11:17:45 -07:00
|
|
|
static ALvoid ALmodulatorState_update(ALmodulatorState *state, ALCdevice *Device, const ALeffectslot *Slot)
|
2010-04-08 08:54:28 -07:00
|
|
|
{
|
2014-11-04 03:33:35 -08:00
|
|
|
ALfloat cw, a;
|
2010-04-08 09:36:20 -07:00
|
|
|
|
2013-05-25 22:07:31 -07:00
|
|
|
if(Slot->EffectProps.Modulator.Waveform == AL_RING_MODULATOR_SINUSOID)
|
2010-04-08 10:37:09 -07:00
|
|
|
state->Waveform = SINUSOID;
|
2013-05-25 22:07:31 -07:00
|
|
|
else if(Slot->EffectProps.Modulator.Waveform == AL_RING_MODULATOR_SAWTOOTH)
|
2010-04-08 10:37:09 -07:00
|
|
|
state->Waveform = SAWTOOTH;
|
2013-05-25 22:07:31 -07:00
|
|
|
else if(Slot->EffectProps.Modulator.Waveform == AL_RING_MODULATOR_SQUARE)
|
2010-04-08 10:37:09 -07:00
|
|
|
state->Waveform = SQUARE;
|
|
|
|
|
2013-05-25 22:07:31 -07:00
|
|
|
state->step = fastf2u(Slot->EffectProps.Modulator.Frequency*WAVEFORM_FRACONE /
|
2011-09-29 06:55:00 -07:00
|
|
|
Device->Frequency);
|
2011-09-23 23:11:35 -07:00
|
|
|
if(state->step == 0) state->step = 1;
|
2010-04-08 10:37:09 -07:00
|
|
|
|
2013-05-27 21:24:28 -07:00
|
|
|
/* Custom filter coeffs, which match the old version instead of a low-shelf. */
|
2013-10-08 16:31:23 -07:00
|
|
|
cw = cosf(F_2PI * Slot->EffectProps.Modulator.HighPassCutoff / Device->Frequency);
|
2012-06-29 02:12:36 -07:00
|
|
|
a = (2.0f-cw) - sqrtf(powf(2.0f-cw, 2.0f) - 1.0f);
|
2013-05-27 21:24:28 -07:00
|
|
|
|
|
|
|
state->Filter.b[0] = a;
|
|
|
|
state->Filter.b[1] = -a;
|
|
|
|
state->Filter.b[2] = 0.0f;
|
|
|
|
state->Filter.a[0] = 1.0f;
|
|
|
|
state->Filter.a[1] = -a;
|
|
|
|
state->Filter.a[2] = 0.0f;
|
2011-07-16 03:35:51 -07:00
|
|
|
|
2014-11-04 03:33:35 -08:00
|
|
|
ComputeAmbientGains(Device, Slot->Gain, state->Gain);
|
2010-04-08 08:54:28 -07:00
|
|
|
}
|
|
|
|
|
2014-11-07 03:43:33 -08:00
|
|
|
static ALvoid ALmodulatorState_process(ALmodulatorState *state, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
|
2010-04-08 08:54:28 -07:00
|
|
|
{
|
2010-04-08 10:37:09 -07:00
|
|
|
switch(state->Waveform)
|
2010-04-08 08:54:28 -07:00
|
|
|
{
|
2011-07-01 01:48:04 -07:00
|
|
|
case SINUSOID:
|
2014-11-07 03:43:33 -08:00
|
|
|
ProcessSin(state, SamplesToDo, SamplesIn, SamplesOut, NumChannels);
|
2011-07-01 01:48:04 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SAWTOOTH:
|
2014-11-07 03:43:33 -08:00
|
|
|
ProcessSaw(state, SamplesToDo, SamplesIn, SamplesOut, NumChannels);
|
2011-07-01 01:48:04 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SQUARE:
|
2014-11-07 03:43:33 -08:00
|
|
|
ProcessSquare(state, SamplesToDo, SamplesIn, SamplesOut, NumChannels);
|
2011-07-01 01:48:04 -07:00
|
|
|
break;
|
2010-04-08 08:54:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-21 23:56:18 -07:00
|
|
|
DECLARE_DEFAULT_ALLOCATORS(ALmodulatorState)
|
2013-05-21 12:47:18 -07:00
|
|
|
|
2013-05-21 04:18:02 -07:00
|
|
|
DEFINE_ALEFFECTSTATE_VTABLE(ALmodulatorState);
|
|
|
|
|
2013-05-21 12:47:18 -07:00
|
|
|
|
2013-10-07 14:49:36 -07:00
|
|
|
typedef struct ALmodulatorStateFactory {
|
|
|
|
DERIVE_FROM_TYPE(ALeffectStateFactory);
|
|
|
|
} ALmodulatorStateFactory;
|
|
|
|
|
2013-10-07 08:34:54 -07:00
|
|
|
static ALeffectState *ALmodulatorStateFactory_create(ALmodulatorStateFactory *UNUSED(factory))
|
2010-04-08 08:54:28 -07:00
|
|
|
{
|
|
|
|
ALmodulatorState *state;
|
|
|
|
|
2014-03-21 23:56:18 -07:00
|
|
|
state = ALmodulatorState_New(sizeof(*state));
|
2013-05-21 04:18:02 -07:00
|
|
|
if(!state) return NULL;
|
|
|
|
SET_VTABLE2(ALmodulatorState, ALeffectState, state);
|
2010-04-08 08:54:28 -07:00
|
|
|
|
2011-09-23 23:11:35 -07:00
|
|
|
state->index = 0;
|
|
|
|
state->step = 1;
|
2010-04-08 10:37:09 -07:00
|
|
|
|
2013-05-27 21:24:28 -07:00
|
|
|
ALfilterState_clear(&state->Filter);
|
2010-04-08 09:36:20 -07:00
|
|
|
|
2013-05-21 02:30:11 -07:00
|
|
|
return STATIC_CAST(ALeffectState, state);
|
2010-04-08 08:54:28 -07:00
|
|
|
}
|
2013-03-13 21:53:42 -07:00
|
|
|
|
2013-05-21 12:47:18 -07:00
|
|
|
DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALmodulatorStateFactory);
|
|
|
|
|
|
|
|
ALeffectStateFactory *ALmodulatorStateFactory_getFactory(void)
|
|
|
|
{
|
2013-10-07 14:49:36 -07:00
|
|
|
static ALmodulatorStateFactory ModulatorFactory = { { GET_VTABLE2(ALmodulatorStateFactory, ALeffectStateFactory) } };
|
|
|
|
|
2013-05-21 12:47:18 -07:00
|
|
|
return STATIC_CAST(ALeffectStateFactory, &ModulatorFactory);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-29 11:17:45 -07:00
|
|
|
void ALmodulator_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
|
2013-03-13 21:53:42 -07:00
|
|
|
{
|
2013-05-25 22:07:31 -07:00
|
|
|
ALeffectProps *props = &effect->Props;
|
2013-03-13 21:53:42 -07:00
|
|
|
switch(param)
|
|
|
|
{
|
|
|
|
case AL_RING_MODULATOR_FREQUENCY:
|
2013-05-26 08:54:34 -07:00
|
|
|
if(!(val >= AL_RING_MODULATOR_MIN_FREQUENCY && val <= AL_RING_MODULATOR_MAX_FREQUENCY))
|
|
|
|
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
|
|
|
|
props->Modulator.Frequency = val;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_RING_MODULATOR_HIGHPASS_CUTOFF:
|
2013-05-26 08:54:34 -07:00
|
|
|
if(!(val >= AL_RING_MODULATOR_MIN_HIGHPASS_CUTOFF && val <= AL_RING_MODULATOR_MAX_HIGHPASS_CUTOFF))
|
|
|
|
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
|
|
|
|
props->Modulator.HighPassCutoff = val;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-05-26 08:54:34 -07:00
|
|
|
SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
|
2013-03-13 21:53:42 -07:00
|
|
|
}
|
|
|
|
}
|
2013-05-29 11:17:45 -07:00
|
|
|
void ALmodulator_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
|
2013-03-13 21:53:42 -07:00
|
|
|
{
|
2013-05-29 11:17:45 -07:00
|
|
|
ALmodulator_setParamf(effect, context, param, vals[0]);
|
2013-03-13 21:53:42 -07:00
|
|
|
}
|
2013-05-29 11:17:45 -07:00
|
|
|
void ALmodulator_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
|
2013-03-13 21:53:42 -07:00
|
|
|
{
|
2013-05-25 22:07:31 -07:00
|
|
|
ALeffectProps *props = &effect->Props;
|
2013-03-13 21:53:42 -07:00
|
|
|
switch(param)
|
|
|
|
{
|
|
|
|
case AL_RING_MODULATOR_FREQUENCY:
|
|
|
|
case AL_RING_MODULATOR_HIGHPASS_CUTOFF:
|
2013-05-29 11:17:45 -07:00
|
|
|
ALmodulator_setParamf(effect, context, param, (ALfloat)val);
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_RING_MODULATOR_WAVEFORM:
|
2013-05-26 08:54:34 -07:00
|
|
|
if(!(val >= AL_RING_MODULATOR_MIN_WAVEFORM && val <= AL_RING_MODULATOR_MAX_WAVEFORM))
|
|
|
|
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
|
|
|
|
props->Modulator.Waveform = val;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-05-26 08:54:34 -07:00
|
|
|
SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
|
2013-03-13 21:53:42 -07:00
|
|
|
}
|
|
|
|
}
|
2013-05-29 11:17:45 -07:00
|
|
|
void ALmodulator_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
|
2013-03-13 21:53:42 -07:00
|
|
|
{
|
2013-05-29 11:17:45 -07:00
|
|
|
ALmodulator_setParami(effect, context, param, vals[0]);
|
2013-03-13 21:53:42 -07:00
|
|
|
}
|
|
|
|
|
2013-10-07 12:56:41 -07:00
|
|
|
void ALmodulator_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
|
2013-03-13 21:53:42 -07:00
|
|
|
{
|
2013-05-25 22:07:31 -07:00
|
|
|
const ALeffectProps *props = &effect->Props;
|
2013-03-13 21:53:42 -07:00
|
|
|
switch(param)
|
|
|
|
{
|
|
|
|
case AL_RING_MODULATOR_FREQUENCY:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = (ALint)props->Modulator.Frequency;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
case AL_RING_MODULATOR_HIGHPASS_CUTOFF:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = (ALint)props->Modulator.HighPassCutoff;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
case AL_RING_MODULATOR_WAVEFORM:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = props->Modulator.Waveform;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-05-26 08:54:34 -07:00
|
|
|
SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
|
2013-03-13 21:53:42 -07:00
|
|
|
}
|
|
|
|
}
|
2013-10-07 12:56:41 -07:00
|
|
|
void ALmodulator_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
|
2013-03-13 21:53:42 -07:00
|
|
|
{
|
2013-05-29 11:17:45 -07:00
|
|
|
ALmodulator_getParami(effect, context, param, vals);
|
2013-03-13 21:53:42 -07:00
|
|
|
}
|
2013-10-07 12:56:41 -07:00
|
|
|
void ALmodulator_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
|
2013-03-13 21:53:42 -07:00
|
|
|
{
|
2013-10-07 12:56:41 -07:00
|
|
|
const ALeffectProps *props = &effect->Props;
|
2013-03-13 21:53:42 -07:00
|
|
|
switch(param)
|
|
|
|
{
|
|
|
|
case AL_RING_MODULATOR_FREQUENCY:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = props->Modulator.Frequency;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
case AL_RING_MODULATOR_HIGHPASS_CUTOFF:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = props->Modulator.HighPassCutoff;
|
2013-03-13 21:53:42 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-05-26 08:54:34 -07:00
|
|
|
SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
|
2013-03-13 21:53:42 -07:00
|
|
|
}
|
|
|
|
}
|
2013-10-07 12:56:41 -07:00
|
|
|
void ALmodulator_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
|
2013-03-13 21:53:42 -07:00
|
|
|
{
|
2013-05-29 11:17:45 -07:00
|
|
|
ALmodulator_getParamf(effect, context, param, vals);
|
2013-03-13 21:53:42 -07:00
|
|
|
}
|
2013-05-24 23:26:59 -07:00
|
|
|
|
|
|
|
DEFINE_ALEFFECT_VTABLE(ALmodulator);
|