alcModulator.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 Scale;
  39. FILTER iirFilter;
  40. ALfloat history[1];
  41. } ALmodulatorState;
  42. #define WAVEFORM_FRACBITS 16
  43. #define WAVEFORM_FRACMASK ((1<<WAVEFORM_FRACBITS)-1)
  44. static __inline ALfloat sin_func(ALuint index)
  45. {
  46. return sin(index / (double)(1<<WAVEFORM_FRACBITS) * M_PI * 2.0f);
  47. }
  48. static __inline ALfloat saw_func(ALuint index)
  49. {
  50. return index*2.0f/(1<<WAVEFORM_FRACBITS) - 1.0f;
  51. }
  52. static __inline ALfloat square_func(ALuint index)
  53. {
  54. return ((index>>(WAVEFORM_FRACBITS-1))&1) ? -1.0f : 1.0f;
  55. }
  56. static __inline ALfloat hpFilter1P(FILTER *iir, ALuint offset, ALfloat input)
  57. {
  58. ALfloat *history = &iir->history[offset];
  59. ALfloat a = iir->coeff;
  60. ALfloat output = input;
  61. output = output + (history[0]-output)*a;
  62. history[0] = output;
  63. return input - output;
  64. }
  65. static ALvoid ModulatorDestroy(ALeffectState *effect)
  66. {
  67. ALmodulatorState *state = (ALmodulatorState*)effect;
  68. free(state);
  69. }
  70. static ALboolean ModulatorDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
  71. {
  72. ALmodulatorState *state = (ALmodulatorState*)effect;
  73. state->Scale = aluSqrt(Device->NumChan / 8.0f);
  74. return AL_TRUE;
  75. }
  76. static ALvoid ModulatorUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffect *Effect)
  77. {
  78. ALmodulatorState *state = (ALmodulatorState*)effect;
  79. ALfloat cw, a = 0.0f;
  80. if(Effect->Modulator.Waveform == AL_RING_MODULATOR_SINUSOID)
  81. state->Waveform = SINUSOID;
  82. else if(Effect->Modulator.Waveform == AL_RING_MODULATOR_SAWTOOTH)
  83. state->Waveform = SAWTOOTH;
  84. else if(Effect->Modulator.Waveform == AL_RING_MODULATOR_SQUARE)
  85. state->Waveform = SQUARE;
  86. state->step = Effect->Modulator.Frequency*(1<<WAVEFORM_FRACBITS) /
  87. Context->Device->Frequency;
  88. if(!state->step)
  89. state->step = 1;
  90. cw = cos(2.0*M_PI * Effect->Modulator.HighPassCutoff / Context->Device->Frequency);
  91. a = (2.0f-cw) - aluSqrt(aluPow(2.0f-cw, 2.0f) - 1.0f);
  92. state->iirFilter.coeff = a;
  93. }
  94. static ALvoid ModulatorProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
  95. {
  96. ALmodulatorState *state = (ALmodulatorState*)effect;
  97. const ALfloat gain = Slot->Gain * state->Scale;
  98. const ALuint step = state->step;
  99. ALuint index = state->index;
  100. ALfloat samp;
  101. ALuint i;
  102. switch(state->Waveform)
  103. {
  104. case SINUSOID:
  105. for(i = 0;i < SamplesToDo;i++)
  106. {
  107. #define FILTER_OUT(func) do { \
  108. samp = SamplesIn[i]; \
  109. \
  110. index += step; \
  111. index &= WAVEFORM_FRACMASK; \
  112. samp *= func(index); \
  113. \
  114. samp = hpFilter1P(&state->iirFilter, 0, samp); \
  115. \
  116. /* Apply slot gain */ \
  117. samp *= gain; \
  118. \
  119. SamplesOut[i][FRONT_LEFT] += samp; \
  120. SamplesOut[i][FRONT_RIGHT] += samp; \
  121. SamplesOut[i][FRONT_CENTER] += samp; \
  122. SamplesOut[i][SIDE_LEFT] += samp; \
  123. SamplesOut[i][SIDE_RIGHT] += samp; \
  124. SamplesOut[i][BACK_LEFT] += samp; \
  125. SamplesOut[i][BACK_RIGHT] += samp; \
  126. SamplesOut[i][BACK_CENTER] += samp; \
  127. } while(0)
  128. FILTER_OUT(sin_func);
  129. }
  130. break;
  131. case SAWTOOTH:
  132. for(i = 0;i < SamplesToDo;i++)
  133. {
  134. FILTER_OUT(saw_func);
  135. }
  136. break;
  137. case SQUARE:
  138. for(i = 0;i < SamplesToDo;i++)
  139. {
  140. FILTER_OUT(square_func);
  141. #undef FILTER_OUT
  142. }
  143. break;
  144. }
  145. state->index = index;
  146. }
  147. ALeffectState *ModulatorCreate(void)
  148. {
  149. ALmodulatorState *state;
  150. state = malloc(sizeof(*state));
  151. if(!state)
  152. return NULL;
  153. state->state.Destroy = ModulatorDestroy;
  154. state->state.DeviceUpdate = ModulatorDeviceUpdate;
  155. state->state.Update = ModulatorUpdate;
  156. state->state.Process = ModulatorProcess;
  157. state->index = 0.0f;
  158. state->step = 1.0f;
  159. state->Scale = 1.0f;
  160. state->iirFilter.coeff = 0.0f;
  161. state->iirFilter.history[0] = 0.0f;
  162. return &state->state;
  163. }