alcModulator.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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, k; \
  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. for(k = 0;k < MAXCHANNELS;k++) \
  86. SamplesOut[i][k] += state->Gain[k] * samp; \
  87. } \
  88. state->index = index; \
  89. }
  90. DECL_TEMPLATE(Sin)
  91. DECL_TEMPLATE(Saw)
  92. DECL_TEMPLATE(Square)
  93. #undef DECL_TEMPLATE
  94. static ALvoid ModulatorDestroy(ALeffectState *effect)
  95. {
  96. ALmodulatorState *state = (ALmodulatorState*)effect;
  97. free(state);
  98. }
  99. static ALboolean ModulatorDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
  100. {
  101. return AL_TRUE;
  102. (void)effect;
  103. (void)Device;
  104. }
  105. static ALvoid ModulatorUpdate(ALeffectState *effect, ALCdevice *Device, const ALeffectslot *Slot)
  106. {
  107. ALmodulatorState *state = (ALmodulatorState*)effect;
  108. ALfloat gain, cw, a = 0.0f;
  109. ALuint index;
  110. if(Slot->effect.Modulator.Waveform == AL_RING_MODULATOR_SINUSOID)
  111. state->Waveform = SINUSOID;
  112. else if(Slot->effect.Modulator.Waveform == AL_RING_MODULATOR_SAWTOOTH)
  113. state->Waveform = SAWTOOTH;
  114. else if(Slot->effect.Modulator.Waveform == AL_RING_MODULATOR_SQUARE)
  115. state->Waveform = SQUARE;
  116. state->step = fastf2u(Slot->effect.Modulator.Frequency*WAVEFORM_FRACONE /
  117. Device->Frequency);
  118. if(state->step == 0) state->step = 1;
  119. cw = aluCos(F_PI*2.0f * Slot->effect.Modulator.HighPassCutoff /
  120. Device->Frequency);
  121. a = (2.0f-cw) - aluSqrt(aluPow(2.0f-cw, 2.0f) - 1.0f);
  122. state->iirFilter.coeff = a;
  123. gain = aluSqrt(1.0f/Device->NumChan);
  124. gain *= Slot->Gain;
  125. for(index = 0;index < MAXCHANNELS;index++)
  126. state->Gain[index] = 0.0f;
  127. for(index = 0;index < Device->NumChan;index++)
  128. {
  129. enum Channel chan = Device->Speaker2Chan[index];
  130. state->Gain[chan] = gain;
  131. }
  132. }
  133. static ALvoid ModulatorProcess(ALeffectState *effect, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])
  134. {
  135. ALmodulatorState *state = (ALmodulatorState*)effect;
  136. switch(state->Waveform)
  137. {
  138. case SINUSOID:
  139. ProcessSin(state, SamplesToDo, SamplesIn, SamplesOut);
  140. break;
  141. case SAWTOOTH:
  142. ProcessSaw(state, SamplesToDo, SamplesIn, SamplesOut);
  143. break;
  144. case SQUARE:
  145. ProcessSquare(state, SamplesToDo, SamplesIn, SamplesOut);
  146. break;
  147. }
  148. }
  149. ALeffectState *ModulatorCreate(void)
  150. {
  151. ALmodulatorState *state;
  152. state = malloc(sizeof(*state));
  153. if(!state)
  154. return NULL;
  155. state->state.Destroy = ModulatorDestroy;
  156. state->state.DeviceUpdate = ModulatorDeviceUpdate;
  157. state->state.Update = ModulatorUpdate;
  158. state->state.Process = ModulatorProcess;
  159. state->index = 0;
  160. state->step = 1;
  161. state->iirFilter.coeff = 0.0f;
  162. state->iirFilter.history[0] = 0.0f;
  163. return &state->state;
  164. }