dedicated.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 *UNUSED(state))
  32. {
  33. }
  34. static ALboolean ALdedicatedState_deviceUpdate(ALdedicatedState *UNUSED(state), ALCdevice *UNUSED(device))
  35. {
  36. return AL_TRUE;
  37. }
  38. static ALvoid ALdedicatedState_update(ALdedicatedState *state, ALCdevice *device, const ALeffectslot *Slot)
  39. {
  40. ALfloat Gain;
  41. ALuint i;
  42. for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
  43. state->gains[i] = 0.0f;
  44. Gain = Slot->Gain * Slot->EffectProps.Dedicated.Gain;
  45. if(Slot->EffectType == AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT)
  46. {
  47. int idx;
  48. if((idx=GetChannelIdxByName(device, LFE)) != -1)
  49. state->gains[idx] = Gain;
  50. }
  51. else if(Slot->EffectType == AL_EFFECT_DEDICATED_DIALOGUE)
  52. {
  53. int idx;
  54. /* Dialog goes to the front-center speaker if it exists, otherwise it
  55. * plays from the front-center location. */
  56. if((idx=GetChannelIdxByName(device, FrontCenter)) != -1)
  57. state->gains[idx] = Gain;
  58. else
  59. {
  60. static const ALfloat front_dir[3] = { 0.0f, 0.0f, -1.0f };
  61. ComputeDirectionalGains(device, front_dir, Gain, state->gains);
  62. }
  63. }
  64. }
  65. static ALvoid ALdedicatedState_process(ALdedicatedState *state, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
  66. {
  67. const ALfloat *gains = state->gains;
  68. ALuint i, c;
  69. for(c = 0;c < NumChannels;c++)
  70. {
  71. if(!(fabsf(gains[c]) > GAIN_SILENCE_THRESHOLD))
  72. continue;
  73. for(i = 0;i < SamplesToDo;i++)
  74. SamplesOut[c][i] = SamplesIn[i] * gains[c];
  75. }
  76. }
  77. DECLARE_DEFAULT_ALLOCATORS(ALdedicatedState)
  78. DEFINE_ALEFFECTSTATE_VTABLE(ALdedicatedState);
  79. typedef struct ALdedicatedStateFactory {
  80. DERIVE_FROM_TYPE(ALeffectStateFactory);
  81. } ALdedicatedStateFactory;
  82. ALeffectState *ALdedicatedStateFactory_create(ALdedicatedStateFactory *UNUSED(factory))
  83. {
  84. ALdedicatedState *state;
  85. ALsizei s;
  86. state = ALdedicatedState_New(sizeof(*state));
  87. if(!state) return NULL;
  88. SET_VTABLE2(ALdedicatedState, ALeffectState, state);
  89. for(s = 0;s < MAX_OUTPUT_CHANNELS;s++)
  90. state->gains[s] = 0.0f;
  91. return STATIC_CAST(ALeffectState, state);
  92. }
  93. DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALdedicatedStateFactory);
  94. ALeffectStateFactory *ALdedicatedStateFactory_getFactory(void)
  95. {
  96. static ALdedicatedStateFactory DedicatedFactory = { { GET_VTABLE2(ALdedicatedStateFactory, ALeffectStateFactory) } };
  97. return STATIC_CAST(ALeffectStateFactory, &DedicatedFactory);
  98. }
  99. void ALdedicated_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  100. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  101. void ALdedicated_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  102. {
  103. ALdedicated_setParami(effect, context, param, vals[0]);
  104. }
  105. void ALdedicated_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  106. {
  107. ALeffectProps *props = &effect->Props;
  108. switch(param)
  109. {
  110. case AL_DEDICATED_GAIN:
  111. if(!(val >= 0.0f && isfinite(val)))
  112. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  113. props->Dedicated.Gain = val;
  114. break;
  115. default:
  116. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  117. }
  118. }
  119. void ALdedicated_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  120. {
  121. ALdedicated_setParamf(effect, context, param, vals[0]);
  122. }
  123. void ALdedicated_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  124. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  125. void ALdedicated_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  126. {
  127. ALdedicated_getParami(effect, context, param, vals);
  128. }
  129. void ALdedicated_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  130. {
  131. const ALeffectProps *props = &effect->Props;
  132. switch(param)
  133. {
  134. case AL_DEDICATED_GAIN:
  135. *val = props->Dedicated.Gain;
  136. break;
  137. default:
  138. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  139. }
  140. }
  141. void ALdedicated_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  142. {
  143. ALdedicated_getParamf(effect, context, param, vals);
  144. }
  145. DEFINE_ALEFFECT_VTABLE(ALdedicated);