alcModulator.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2009 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., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <math.h>
  22. #include <stdlib.h>
  23. #include "alMain.h"
  24. #include "alFilter.h"
  25. #include "alAuxEffectSlot.h"
  26. #include "alError.h"
  27. #include "alu.h"
  28. typedef struct ALmodulatorState {
  29. // Must be first in all effects!
  30. ALeffectState state;
  31. enum {
  32. SINUSOID,
  33. SAWTOOTH,
  34. SQUARE
  35. } Waveform;
  36. ALuint index;
  37. ALuint step;
  38. ALfloat Gain[MAXCHANNELS];
  39. FILTER iirFilter;
  40. ALfloat history[1];
  41. } ALmodulatorState;
  42. #define WAVEFORM_FRACBITS 16
  43. #define WAVEFORM_FRACONE (1<<WAVEFORM_FRACBITS)
  44. #define WAVEFORM_FRACMASK (WAVEFORM_FRACONE-1)
  45. static __inline ALfloat Sin(ALuint index)
  46. {
  47. return aluSin(index * (F_PI*2.0f / WAVEFORM_FRACONE));
  48. }
  49. static __inline ALfloat Saw(ALuint index)
  50. {
  51. return index*(2.0f/WAVEFORM_FRACONE) - 1.0f;
  52. }
  53. static __inline ALfloat Square(ALuint index)
  54. {
  55. return ((index>>(WAVEFORM_FRACBITS-1))&1)*2.0f - 1.0f;
  56. }
  57. static __inline ALfloat hpFilter1P(FILTER *iir, ALuint offset, ALfloat input)
  58. {
  59. ALfloat *history = &iir->history[offset];
  60. ALfloat a = iir->coeff;
  61. ALfloat output = input;
  62. output = output + (history[0]-output)*a;
  63. history[0] = output;
  64. return input - output;
  65. }
  66. #define DECL_TEMPLATE(func) \
  67. static void Process##func(ALmodulatorState *state, ALuint SamplesToDo, \
  68. const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS]) \
  69. { \
  70. const ALuint step = state->step; \
  71. ALuint index = state->index; \
  72. ALfloat samp; \
  73. ALuint i; \
  74. \
  75. for(i = 0;i < SamplesToDo;i++) \
  76. { \
  77. samp = SamplesIn[i]; \
  78. \
  79. index += step; \
  80. index &= WAVEFORM_FRACMASK; \
  81. samp *= func(index); \
  82. \
  83. samp = hpFilter1P(&state->iirFilter, 0, samp); \
  84. \
  85. SamplesOut[i][FRONT_LEFT] += state->Gain[FRONT_LEFT] * samp; \
  86. SamplesOut[i][FRONT_RIGHT] += state->Gain[FRONT_RIGHT] * samp; \
  87. SamplesOut[i][FRONT_CENTER] += state->Gain[FRONT_CENTER] * samp; \
  88. SamplesOut[i][SIDE_LEFT] += state->Gain[SIDE_LEFT] * samp; \
  89. SamplesOut[i][SIDE_RIGHT] += state->Gain[SIDE_RIGHT] * samp; \
  90. SamplesOut[i][BACK_LEFT] += state->Gain[BACK_LEFT] * samp; \
  91. SamplesOut[i][BACK_RIGHT] += state->Gain[BACK_RIGHT] * samp; \
  92. SamplesOut[i][BACK_CENTER] += state->Gain[BACK_CENTER] * samp; \
  93. } \
  94. state->index = index; \
  95. }
  96. DECL_TEMPLATE(Sin)
  97. DECL_TEMPLATE(Saw)
  98. DECL_TEMPLATE(Square)
  99. #undef DECL_TEMPLATE
  100. static ALvoid ModulatorDestroy(ALeffectState *effect)
  101. {
  102. ALmodulatorState *state = (ALmodulatorState*)effect;
  103. free(state);
  104. }
  105. static ALboolean ModulatorDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
  106. {
  107. return AL_TRUE;
  108. (void)effect;
  109. (void)Device;
  110. }
  111. static ALvoid ModulatorUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffectslot *Slot)
  112. {
  113. ALmodulatorState *state = (ALmodulatorState*)effect;
  114. ALCdevice *Device = Context->Device;
  115. ALfloat gain, cw, a = 0.0f;
  116. ALuint index;
  117. if(Slot->effect.Modulator.Waveform == AL_RING_MODULATOR_SINUSOID)
  118. state->Waveform = SINUSOID;
  119. else if(Slot->effect.Modulator.Waveform == AL_RING_MODULATOR_SAWTOOTH)
  120. state->Waveform = SAWTOOTH;
  121. else if(Slot->effect.Modulator.Waveform == AL_RING_MODULATOR_SQUARE)
  122. state->Waveform = SQUARE;
  123. state->step = fastf2u(Slot->effect.Modulator.Frequency*WAVEFORM_FRACONE /
  124. Device->Frequency);
  125. if(state->step == 0) state->step = 1;
  126. cw = aluCos(F_PI*2.0f * Slot->effect.Modulator.HighPassCutoff /
  127. Device->Frequency);
  128. a = (2.0f-cw) - aluSqrt(aluPow(2.0f-cw, 2.0f) - 1.0f);
  129. state->iirFilter.coeff = a;
  130. gain = Slot->Gain;
  131. for(index = 0;index < MAXCHANNELS;index++)
  132. state->Gain[index] = 0.0f;
  133. for(index = 0;index < Device->NumChan;index++)
  134. {
  135. enum Channel chan = Device->Speaker2Chan[index];
  136. state->Gain[chan] = gain;
  137. }
  138. }
  139. static ALvoid ModulatorProcess(ALeffectState *effect, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])
  140. {
  141. ALmodulatorState *state = (ALmodulatorState*)effect;
  142. switch(state->Waveform)
  143. {
  144. case SINUSOID:
  145. ProcessSin(state, SamplesToDo, SamplesIn, SamplesOut);
  146. break;
  147. case SAWTOOTH:
  148. ProcessSaw(state, SamplesToDo, SamplesIn, SamplesOut);
  149. break;
  150. case SQUARE:
  151. ProcessSquare(state, SamplesToDo, SamplesIn, SamplesOut);
  152. break;
  153. }
  154. }
  155. ALeffectState *ModulatorCreate(void)
  156. {
  157. ALmodulatorState *state;
  158. state = malloc(sizeof(*state));
  159. if(!state)
  160. return NULL;
  161. state->state.Destroy = ModulatorDestroy;
  162. state->state.DeviceUpdate = ModulatorDeviceUpdate;
  163. state->state.Update = ModulatorUpdate;
  164. state->state.Process = ModulatorProcess;
  165. state->index = 0;
  166. state->step = 1;
  167. state->iirFilter.coeff = 0.0f;
  168. state->iirFilter.history[0] = 0.0f;
  169. return &state->state;
  170. }