dedicated.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint 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. CalcXYZCoeffs(0.0f, 0.0f, -1.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, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
  92. {
  93. const ALfloat *gains = state->gains;
  94. ALuint i, c;
  95. for(c = 0;c < NumChannels;c++)
  96. {
  97. if(!(fabsf(gains[c]) > GAIN_SILENCE_THRESHOLD))
  98. continue;
  99. for(i = 0;i < SamplesToDo;i++)
  100. SamplesOut[c][i] += SamplesIn[0][i] * gains[c];
  101. }
  102. }
  103. typedef struct ALdedicatedStateFactory {
  104. DERIVE_FROM_TYPE(ALeffectStateFactory);
  105. } ALdedicatedStateFactory;
  106. ALeffectState *ALdedicatedStateFactory_create(ALdedicatedStateFactory *UNUSED(factory))
  107. {
  108. ALdedicatedState *state;
  109. NEW_OBJ0(state, ALdedicatedState)();
  110. if(!state) return NULL;
  111. return STATIC_CAST(ALeffectState, state);
  112. }
  113. DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALdedicatedStateFactory);
  114. ALeffectStateFactory *ALdedicatedStateFactory_getFactory(void)
  115. {
  116. static ALdedicatedStateFactory DedicatedFactory = { { GET_VTABLE2(ALdedicatedStateFactory, ALeffectStateFactory) } };
  117. return STATIC_CAST(ALeffectStateFactory, &DedicatedFactory);
  118. }
  119. void ALdedicated_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  120. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  121. void ALdedicated_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  122. {
  123. ALdedicated_setParami(effect, context, param, vals[0]);
  124. }
  125. void ALdedicated_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  126. {
  127. ALeffectProps *props = &effect->Props;
  128. switch(param)
  129. {
  130. case AL_DEDICATED_GAIN:
  131. if(!(val >= 0.0f && isfinite(val)))
  132. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  133. props->Dedicated.Gain = val;
  134. break;
  135. default:
  136. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  137. }
  138. }
  139. void ALdedicated_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  140. {
  141. ALdedicated_setParamf(effect, context, param, vals[0]);
  142. }
  143. void ALdedicated_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  144. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  145. void ALdedicated_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  146. {
  147. ALdedicated_getParami(effect, context, param, vals);
  148. }
  149. void ALdedicated_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  150. {
  151. const ALeffectProps *props = &effect->Props;
  152. switch(param)
  153. {
  154. case AL_DEDICATED_GAIN:
  155. *val = props->Dedicated.Gain;
  156. break;
  157. default:
  158. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  159. }
  160. }
  161. void ALdedicated_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  162. {
  163. ALdedicated_getParamf(effect, context, param, vals);
  164. }
  165. DEFINE_ALEFFECT_VTABLE(ALdedicated);