Clean up some messy rounding code
[openal-soft.git] / Alc / effects / dedicated.c
blob52d0ec6dae0e779d0f66df182b43ea5138ef83a0
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2011 by Chris Robinson.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <stdlib.h>
25 #include "alMain.h"
26 #include "alFilter.h"
27 #include "alAuxEffectSlot.h"
28 #include "alError.h"
29 #include "alu.h"
32 typedef struct ALdedicatedState {
33 DERIVE_FROM_TYPE(ALeffectState);
35 ALfloat gains[MAX_OUTPUT_CHANNELS];
36 } ALdedicatedState;
38 static ALvoid ALdedicatedState_Destruct(ALdedicatedState *state);
39 static ALboolean ALdedicatedState_deviceUpdate(ALdedicatedState *state, ALCdevice *device);
40 static ALvoid ALdedicatedState_update(ALdedicatedState *state, const ALCdevice *device, const ALeffectslot *Slot, const ALeffectProps *props);
41 static ALvoid ALdedicatedState_process(ALdedicatedState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels);
42 DECLARE_DEFAULT_ALLOCATORS(ALdedicatedState)
44 DEFINE_ALEFFECTSTATE_VTABLE(ALdedicatedState);
47 static void ALdedicatedState_Construct(ALdedicatedState *state)
49 ALsizei s;
51 ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
52 SET_VTABLE2(ALdedicatedState, ALeffectState, state);
54 for(s = 0;s < MAX_OUTPUT_CHANNELS;s++)
55 state->gains[s] = 0.0f;
58 static ALvoid ALdedicatedState_Destruct(ALdedicatedState *state)
60 ALeffectState_Destruct(STATIC_CAST(ALeffectState,state));
63 static ALboolean ALdedicatedState_deviceUpdate(ALdedicatedState *UNUSED(state), ALCdevice *UNUSED(device))
65 return AL_TRUE;
68 static ALvoid ALdedicatedState_update(ALdedicatedState *state, const ALCdevice *device, const ALeffectslot *Slot, const ALeffectProps *props)
70 ALfloat Gain;
71 ALuint i;
73 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
74 state->gains[i] = 0.0f;
76 Gain = Slot->Params.Gain * props->Dedicated.Gain;
77 if(Slot->Params.EffectType == AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT)
79 int idx;
80 if((idx=GetChannelIdxByName(device->RealOut, LFE)) != -1)
82 STATIC_CAST(ALeffectState,state)->OutBuffer = device->RealOut.Buffer;
83 STATIC_CAST(ALeffectState,state)->OutChannels = device->RealOut.NumChannels;
84 state->gains[idx] = Gain;
87 else if(Slot->Params.EffectType == AL_EFFECT_DEDICATED_DIALOGUE)
89 int idx;
90 /* Dialog goes to the front-center speaker if it exists, otherwise it
91 * plays from the front-center location. */
92 if((idx=GetChannelIdxByName(device->RealOut, FrontCenter)) != -1)
94 STATIC_CAST(ALeffectState,state)->OutBuffer = device->RealOut.Buffer;
95 STATIC_CAST(ALeffectState,state)->OutChannels = device->RealOut.NumChannels;
96 state->gains[idx] = Gain;
98 else
100 ALfloat coeffs[MAX_AMBI_COEFFS];
101 CalcAngleCoeffs(0.0f, 0.0f, 0.0f, coeffs);
103 STATIC_CAST(ALeffectState,state)->OutBuffer = device->Dry.Buffer;
104 STATIC_CAST(ALeffectState,state)->OutChannels = device->Dry.NumChannels;
105 ComputePanningGains(device->Dry, coeffs, Gain, state->gains);
110 static ALvoid ALdedicatedState_process(ALdedicatedState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
112 ALsizei i, c;
114 SamplesIn = ASSUME_ALIGNED(SamplesIn, 16);
115 SamplesOut = ASSUME_ALIGNED(SamplesOut, 16);
116 for(c = 0;c < NumChannels;c++)
118 const ALfloat gain = state->gains[c];
119 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
120 continue;
122 for(i = 0;i < SamplesToDo;i++)
123 SamplesOut[c][i] += SamplesIn[0][i] * gain;
128 typedef struct ALdedicatedStateFactory {
129 DERIVE_FROM_TYPE(ALeffectStateFactory);
130 } ALdedicatedStateFactory;
132 ALeffectState *ALdedicatedStateFactory_create(ALdedicatedStateFactory *UNUSED(factory))
134 ALdedicatedState *state;
136 NEW_OBJ0(state, ALdedicatedState)();
137 if(!state) return NULL;
139 return STATIC_CAST(ALeffectState, state);
142 DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALdedicatedStateFactory);
145 ALeffectStateFactory *ALdedicatedStateFactory_getFactory(void)
147 static ALdedicatedStateFactory DedicatedFactory = { { GET_VTABLE2(ALdedicatedStateFactory, ALeffectStateFactory) } };
149 return STATIC_CAST(ALeffectStateFactory, &DedicatedFactory);
153 void ALdedicated_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
154 { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
155 void ALdedicated_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
157 ALdedicated_setParami(effect, context, param, vals[0]);
159 void ALdedicated_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
161 ALeffectProps *props = &effect->Props;
162 switch(param)
164 case AL_DEDICATED_GAIN:
165 if(!(val >= 0.0f && isfinite(val)))
166 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
167 props->Dedicated.Gain = val;
168 break;
170 default:
171 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
174 void ALdedicated_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
176 ALdedicated_setParamf(effect, context, param, vals[0]);
179 void ALdedicated_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
180 { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
181 void ALdedicated_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
183 ALdedicated_getParami(effect, context, param, vals);
185 void ALdedicated_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
187 const ALeffectProps *props = &effect->Props;
188 switch(param)
190 case AL_DEDICATED_GAIN:
191 *val = props->Dedicated.Gain;
192 break;
194 default:
195 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
198 void ALdedicated_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
200 ALdedicated_getParamf(effect, context, param, vals);
203 DEFINE_ALEFFECT_VTABLE(ALdedicated);