autowah.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2013 by Anis A. Hireche, Nasca Octavian Paul
  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 <stdlib.h>
  21. #include "config.h"
  22. #include "alu.h"
  23. #include "alFilter.h"
  24. #include "alError.h"
  25. #include "alMain.h"
  26. #include "alAuxEffectSlot.h"
  27. /* Auto-wah is simply a low-pass filter with a cutoff frequency that shifts up
  28. * or down depending on the input signal, and a resonant peak at the cutoff.
  29. *
  30. * Currently, we assume a cutoff frequency range of 500hz (no amplitude) to
  31. * 3khz (peak gain). Peak gain is assumed to be in normalized scale.
  32. */
  33. typedef struct ALautowahState {
  34. DERIVE_FROM_TYPE(ALeffectState);
  35. /* Effect gains for each channel */
  36. ALfloat Gain[MaxChannels];
  37. /* Effect parameters */
  38. ALfloat AttackRate;
  39. ALfloat ReleaseRate;
  40. ALfloat Resonance;
  41. ALfloat PeakGain;
  42. ALfloat GainCtrl;
  43. ALfloat Frequency;
  44. /* Samples processing */
  45. ALfilterState LowPass;
  46. } ALautowahState;
  47. static ALvoid ALautowahState_Destruct(ALautowahState *UNUSED(state))
  48. {
  49. }
  50. static ALboolean ALautowahState_deviceUpdate(ALautowahState *state, ALCdevice *device)
  51. {
  52. state->Frequency = device->Frequency;
  53. return AL_TRUE;
  54. }
  55. static ALvoid ALautowahState_update(ALautowahState *state, ALCdevice *device, const ALeffectslot *slot)
  56. {
  57. ALfloat attackTime, releaseTime;
  58. ALfloat gain;
  59. attackTime = slot->EffectProps.Autowah.AttackTime * state->Frequency;
  60. releaseTime = slot->EffectProps.Autowah.ReleaseTime * state->Frequency;
  61. state->AttackRate = 1.0f / attackTime;
  62. state->ReleaseRate = 1.0f / releaseTime;
  63. state->PeakGain = slot->EffectProps.Autowah.PeakGain;
  64. state->Resonance = slot->EffectProps.Autowah.Resonance;
  65. gain = sqrtf(1.0f / device->NumChan) * slot->Gain;
  66. SetGains(device, gain, state->Gain);
  67. }
  68. static ALvoid ALautowahState_process(ALautowahState *state, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[BUFFERSIZE])
  69. {
  70. ALuint it, kt;
  71. ALuint base;
  72. for(base = 0;base < SamplesToDo;)
  73. {
  74. ALfloat temps[64];
  75. ALuint td = minu(SamplesToDo-base, 64);
  76. ALfloat gain = state->GainCtrl;
  77. for(it = 0;it < td;it++)
  78. {
  79. ALfloat smp = SamplesIn[it+base];
  80. ALfloat alpha, w0;
  81. ALfloat amplitude;
  82. ALfloat cutoff;
  83. /* Similar to compressor, we get the current amplitude of the
  84. * incoming signal, and attack or release to reach it. */
  85. amplitude = fabs(smp);
  86. if(amplitude > gain)
  87. gain = minf(gain+state->AttackRate, amplitude);
  88. else if(amplitude < gain)
  89. gain = maxf(gain-state->ReleaseRate, amplitude);
  90. gain = maxf(gain, GAIN_SILENCE_THRESHOLD);
  91. /* FIXME: What range does the filter cover? */
  92. cutoff = lerp(1000.0f, (ALfloat)LOWPASSFREQREF, minf(gain/state->PeakGain, 1.0f));
  93. /* The code below is like calling ALfilterState_setParams with
  94. * ALfilterType_LowPass. However, instead of passing a bandwidth,
  95. * we use the resonance property for Q. This also inlines the call.
  96. */
  97. w0 = F_2PI * cutoff / state->Frequency;
  98. /* FIXME: Resonance controls the resonant peak, or Q. How? Not sure
  99. * that Q = resonance*0.1. */
  100. alpha = sinf(w0) / (2.0f * state->Resonance*0.1f);
  101. state->LowPass.b[0] = (1.0f - cosf(w0)) / 2.0f;
  102. state->LowPass.b[1] = 1.0f - cosf(w0);
  103. state->LowPass.b[2] = (1.0f - cosf(w0)) / 2.0f;
  104. state->LowPass.a[0] = 1.0f + alpha;
  105. state->LowPass.a[1] = -2.0f * cosf(w0);
  106. state->LowPass.a[2] = 1.0f - alpha;
  107. state->LowPass.b[2] /= state->LowPass.a[0];
  108. state->LowPass.b[1] /= state->LowPass.a[0];
  109. state->LowPass.b[0] /= state->LowPass.a[0];
  110. state->LowPass.a[2] /= state->LowPass.a[0];
  111. state->LowPass.a[1] /= state->LowPass.a[0];
  112. state->LowPass.a[0] /= state->LowPass.a[0];
  113. temps[it] = ALfilterState_processSingle(&state->LowPass, smp);
  114. }
  115. state->GainCtrl = gain;
  116. for(kt = 0;kt < MaxChannels;kt++)
  117. {
  118. ALfloat gain = state->Gain[kt];
  119. if(!(gain > GAIN_SILENCE_THRESHOLD))
  120. continue;
  121. for(it = 0;it < td;it++)
  122. SamplesOut[kt][base+it] += gain * temps[it];
  123. }
  124. base += td;
  125. }
  126. }
  127. static void ALautowahState_Delete(ALautowahState *state)
  128. {
  129. free(state);
  130. }
  131. DEFINE_ALEFFECTSTATE_VTABLE(ALautowahState);
  132. typedef struct ALautowahStateFactory {
  133. DERIVE_FROM_TYPE(ALeffectStateFactory);
  134. } ALautowahStateFactory;
  135. static ALeffectState *ALautowahStateFactory_create(ALautowahStateFactory *UNUSED(factory))
  136. {
  137. ALautowahState *state;
  138. state = malloc(sizeof(*state));
  139. if(!state) return NULL;
  140. SET_VTABLE2(ALautowahState, ALeffectState, state);
  141. state->AttackRate = 0.0f;
  142. state->ReleaseRate = 0.0f;
  143. state->Resonance = 0.0f;
  144. state->PeakGain = 1.0f;
  145. state->GainCtrl = 1.0f;
  146. ALfilterState_clear(&state->LowPass);
  147. return STATIC_CAST(ALeffectState, state);
  148. }
  149. DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALautowahStateFactory);
  150. ALeffectStateFactory *ALautowahStateFactory_getFactory(void)
  151. {
  152. static ALautowahStateFactory AutowahFactory = { { GET_VTABLE2(ALautowahStateFactory, ALeffectStateFactory) } };
  153. return STATIC_CAST(ALeffectStateFactory, &AutowahFactory);
  154. }
  155. void ALautowah_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  156. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  157. void ALautowah_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  158. {
  159. ALautowah_setParami(effect, context, param, vals[0]);
  160. }
  161. void ALautowah_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  162. {
  163. ALeffectProps *props = &effect->Props;
  164. switch(param)
  165. {
  166. case AL_AUTOWAH_ATTACK_TIME:
  167. if(!(val >= AL_AUTOWAH_MIN_ATTACK_TIME && val <= AL_AUTOWAH_MAX_ATTACK_TIME))
  168. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  169. props->Autowah.AttackTime = val;
  170. break;
  171. case AL_AUTOWAH_RELEASE_TIME:
  172. if(!(val >= AL_AUTOWAH_MIN_RELEASE_TIME && val <= AL_AUTOWAH_MAX_RELEASE_TIME))
  173. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  174. props->Autowah.ReleaseTime = val;
  175. break;
  176. case AL_AUTOWAH_RESONANCE:
  177. if(!(val >= AL_AUTOWAH_MIN_RESONANCE && val <= AL_AUTOWAH_MAX_RESONANCE))
  178. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  179. props->Autowah.Resonance = val;
  180. break;
  181. case AL_AUTOWAH_PEAK_GAIN:
  182. if(!(val >= AL_AUTOWAH_MIN_PEAK_GAIN && val <= AL_AUTOWAH_MAX_PEAK_GAIN))
  183. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  184. props->Autowah.PeakGain = val;
  185. break;
  186. default:
  187. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  188. }
  189. }
  190. void ALautowah_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  191. {
  192. ALautowah_setParamf(effect, context, param, vals[0]);
  193. }
  194. void ALautowah_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  195. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  196. void ALautowah_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  197. {
  198. ALautowah_getParami(effect, context, param, vals);
  199. }
  200. void ALautowah_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  201. {
  202. const ALeffectProps *props = &effect->Props;
  203. switch(param)
  204. {
  205. case AL_AUTOWAH_ATTACK_TIME:
  206. *val = props->Autowah.AttackTime;
  207. break;
  208. case AL_AUTOWAH_RELEASE_TIME:
  209. *val = props->Autowah.ReleaseTime;
  210. break;
  211. case AL_AUTOWAH_RESONANCE:
  212. *val = props->Autowah.Resonance;
  213. break;
  214. case AL_AUTOWAH_PEAK_GAIN:
  215. *val = props->Autowah.PeakGain;
  216. break;
  217. default:
  218. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  219. }
  220. }
  221. void ALautowah_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  222. {
  223. ALautowah_getParamf(effect, context, param, vals);
  224. }
  225. DEFINE_ALEFFECT_VTABLE(ALautowah);