dedicated.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.
  8. *
  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.
  13. *
  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
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include "alMain.h"
  23. #include "alFilter.h"
  24. #include "alAuxEffectSlot.h"
  25. #include "alError.h"
  26. #include "alu.h"
  27. typedef struct ALdedicatedState {
  28. DERIVE_FROM_TYPE(ALeffectState);
  29. ALfloat gains[MAX_OUTPUT_CHANNELS];
  30. } ALdedicatedState;
  31. static ALvoid ALdedicatedState_Destruct(ALdedicatedState *state);
  32. static ALboolean ALdedicatedState_deviceUpdate(ALdedicatedState *state, ALCdevice *device);
  33. static ALvoid ALdedicatedState_update(ALdedicatedState *state, const ALCdevice *device, const ALeffectslot *Slot, const ALeffectProps *props);
  34. static ALvoid ALdedicatedState_process(ALdedicatedState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels);
  35. DECLARE_DEFAULT_ALLOCATORS(ALdedicatedState)
  36. DEFINE_ALEFFECTSTATE_VTABLE(ALdedicatedState);
  37. static void ALdedicatedState_Construct(ALdedicatedState *state)
  38. {
  39. ALsizei s;
  40. ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
  41. SET_VTABLE2(ALdedicatedState, ALeffectState, state);
  42. for(s = 0;s < MAX_OUTPUT_CHANNELS;s++)
  43. state->gains[s] = 0.0f;
  44. }
  45. static ALvoid ALdedicatedState_Destruct(ALdedicatedState *state)
  46. {
  47. ALeffectState_Destruct(STATIC_CAST(ALeffectState,state));
  48. }
  49. static ALboolean ALdedicatedState_deviceUpdate(ALdedicatedState *UNUSED(state), ALCdevice *UNUSED(device))
  50. {
  51. return AL_TRUE;
  52. }
  53. static ALvoid ALdedicatedState_update(ALdedicatedState *state, const ALCdevice *device, const ALeffectslot *Slot, const ALeffectProps *props)
  54. {
  55. ALfloat Gain;
  56. ALuint i;
  57. for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
  58. state->gains[i] = 0.0f;
  59. Gain = Slot->Params.Gain * props->Dedicated.Gain;
  60. if(Slot->Params.EffectType == AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT)
  61. {
  62. int idx;
  63. if((idx=GetChannelIdxByName(device->RealOut, LFE)) != -1)
  64. {
  65. STATIC_CAST(ALeffectState,state)->OutBuffer = device->RealOut.Buffer;
  66. STATIC_CAST(ALeffectState,state)->OutChannels = device->RealOut.NumChannels;
  67. state->gains[idx] = Gain;
  68. }
  69. }
  70. else if(Slot->Params.EffectType == AL_EFFECT_DEDICATED_DIALOGUE)
  71. {
  72. int idx;
  73. /* Dialog goes to the front-center speaker if it exists, otherwise it
  74. * plays from the front-center location. */
  75. if((idx=GetChannelIdxByName(device->RealOut, FrontCenter)) != -1)
  76. {
  77. STATIC_CAST(ALeffectState,state)->OutBuffer = device->RealOut.Buffer;
  78. STATIC_CAST(ALeffectState,state)->OutChannels = device->RealOut.NumChannels;
  79. state->gains[idx] = Gain;
  80. }
  81. else
  82. {
  83. ALfloat coeffs[MAX_AMBI_COEFFS];
  84. CalcAngleCoeffs(0.0f, 0.0f, 0.0f, coeffs);
  85. STATIC_CAST(ALeffectState,state)->OutBuffer = device->Dry.Buffer;
  86. STATIC_CAST(ALeffectState,state)->OutChannels = device->Dry.NumChannels;
  87. ComputePanningGains(device->Dry, coeffs, Gain, state->gains);
  88. }
  89. }
  90. }
  91. static ALvoid ALdedicatedState_process(ALdedicatedState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
  92. {
  93. ALsizei i, c;
  94. SamplesIn = ASSUME_ALIGNED(SamplesIn, 16);
  95. SamplesOut = ASSUME_ALIGNED(SamplesOut, 16);
  96. for(c = 0;c < NumChannels;c++)
  97. {
  98. const ALfloat gain = state->gains[c];
  99. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  100. continue;
  101. for(i = 0;i < SamplesToDo;i++)
  102. SamplesOut[c][i] += SamplesIn[0][i] * gain;
  103. }
  104. }
  105. typedef struct ALdedicatedStateFactory {
  106. DERIVE_FROM_TYPE(ALeffectStateFactory);
  107. } ALdedicatedStateFactory;
  108. ALeffectState *ALdedicatedStateFactory_create(ALdedicatedStateFactory *UNUSED(factory))
  109. {
  110. ALdedicatedState *state;
  111. NEW_OBJ0(state, ALdedicatedState)();
  112. if(!state) return NULL;
  113. return STATIC_CAST(ALeffectState, state);
  114. }
  115. DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALdedicatedStateFactory);
  116. ALeffectStateFactory *ALdedicatedStateFactory_getFactory(void)
  117. {
  118. static ALdedicatedStateFactory DedicatedFactory = { { GET_VTABLE2(ALdedicatedStateFactory, ALeffectStateFactory) } };
  119. return STATIC_CAST(ALeffectStateFactory, &DedicatedFactory);
  120. }
  121. void ALdedicated_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  122. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  123. void ALdedicated_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  124. {
  125. ALdedicated_setParami(effect, context, param, vals[0]);
  126. }
  127. void ALdedicated_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  128. {
  129. ALeffectProps *props = &effect->Props;
  130. switch(param)
  131. {
  132. case AL_DEDICATED_GAIN:
  133. if(!(val >= 0.0f && isfinite(val)))
  134. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  135. props->Dedicated.Gain = val;
  136. break;
  137. default:
  138. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  139. }
  140. }
  141. void ALdedicated_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  142. {
  143. ALdedicated_setParamf(effect, context, param, vals[0]);
  144. }
  145. void ALdedicated_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  146. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  147. void ALdedicated_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  148. {
  149. ALdedicated_getParami(effect, context, param, vals);
  150. }
  151. void ALdedicated_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  152. {
  153. const ALeffectProps *props = &effect->Props;
  154. switch(param)
  155. {
  156. case AL_DEDICATED_GAIN:
  157. *val = props->Dedicated.Gain;
  158. break;
  159. default:
  160. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  161. }
  162. }
  163. void ALdedicated_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  164. {
  165. ALdedicated_getParamf(effect, context, param, vals);
  166. }
  167. DEFINE_ALEFFECT_VTABLE(ALdedicated);