autowah.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.,
  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 <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 20hz (no amplitude) to
  31. * 20khz (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[MAX_OUTPUT_CHANNELS];
  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 = (ALfloat)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. attackTime = slot->EffectProps.Autowah.AttackTime * state->Frequency;
  59. releaseTime = slot->EffectProps.Autowah.ReleaseTime * state->Frequency;
  60. state->AttackRate = powf(1.0f/GAIN_SILENCE_THRESHOLD, 1.0f/attackTime);
  61. state->ReleaseRate = powf(GAIN_SILENCE_THRESHOLD/1.0f, 1.0f/releaseTime);
  62. state->PeakGain = slot->EffectProps.Autowah.PeakGain;
  63. state->Resonance = slot->EffectProps.Autowah.Resonance;
  64. ComputeAmbientGains(device, slot->Gain, state->Gain);
  65. }
  66. static ALvoid ALautowahState_process(ALautowahState *state, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[BUFFERSIZE], ALuint NumChannels)
  67. {
  68. ALuint it, kt;
  69. ALuint base;
  70. for(base = 0;base < SamplesToDo;)
  71. {
  72. ALfloat temps[256];
  73. ALuint td = minu(256, SamplesToDo-base);
  74. ALfloat gain = state->GainCtrl;
  75. for(it = 0;it < td;it++)
  76. {
  77. ALfloat smp = SamplesIn[it+base];
  78. ALfloat a[3], b[3];
  79. ALfloat alpha, w0;
  80. ALfloat amplitude;
  81. ALfloat cutoff;
  82. /* Similar to compressor, we get the current amplitude of the
  83. * incoming signal, and attack or release to reach it. */
  84. amplitude = fabsf(smp);
  85. if(amplitude > gain)
  86. gain = minf(gain*state->AttackRate, amplitude);
  87. else if(amplitude < gain)
  88. gain = maxf(gain*state->ReleaseRate, amplitude);
  89. gain = maxf(gain, GAIN_SILENCE_THRESHOLD);
  90. /* FIXME: What range does the filter cover? */
  91. cutoff = lerp(20.0f, 20000.0f, minf(gain/state->PeakGain, 1.0f));
  92. /* The code below is like calling ALfilterState_setParams with
  93. * ALfilterType_LowPass. However, instead of passing a bandwidth,
  94. * we use the resonance property for Q. This also inlines the call.
  95. */
  96. w0 = F_TAU * cutoff / state->Frequency;
  97. /* FIXME: Resonance controls the resonant peak, or Q. How? Not sure
  98. * that Q = resonance*0.1. */
  99. alpha = sinf(w0) / (2.0f * state->Resonance*0.1f);
  100. b[0] = (1.0f - cosf(w0)) / 2.0f;
  101. b[1] = 1.0f - cosf(w0);
  102. b[2] = (1.0f - cosf(w0)) / 2.0f;
  103. a[0] = 1.0f + alpha;
  104. a[1] = -2.0f * cosf(w0);
  105. a[2] = 1.0f - alpha;
  106. state->LowPass.a1 = a[1] / a[0];
  107. state->LowPass.a2 = a[2] / a[0];
  108. state->LowPass.b1 = b[1] / a[0];
  109. state->LowPass.b2 = b[2] / a[0];
  110. state->LowPass.input_gain = b[0] / a[0];
  111. temps[it] = ALfilterState_processSingle(&state->LowPass, smp);
  112. }
  113. state->GainCtrl = gain;
  114. for(kt = 0;kt < NumChannels;kt++)
  115. {
  116. ALfloat gain = state->Gain[kt];
  117. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  118. continue;
  119. for(it = 0;it < td;it++)
  120. SamplesOut[kt][base+it] += gain * temps[it];
  121. }
  122. base += td;
  123. }
  124. }
  125. DECLARE_DEFAULT_ALLOCATORS(ALautowahState)
  126. DEFINE_ALEFFECTSTATE_VTABLE(ALautowahState);
  127. typedef struct ALautowahStateFactory {
  128. DERIVE_FROM_TYPE(ALeffectStateFactory);
  129. } ALautowahStateFactory;
  130. static ALeffectState *ALautowahStateFactory_create(ALautowahStateFactory *UNUSED(factory))
  131. {
  132. ALautowahState *state;
  133. state = ALautowahState_New(sizeof(*state));
  134. if(!state) return NULL;
  135. SET_VTABLE2(ALautowahState, ALeffectState, state);
  136. state->AttackRate = 1.0f;
  137. state->ReleaseRate = 1.0f;
  138. state->Resonance = 2.0f;
  139. state->PeakGain = 1.0f;
  140. state->GainCtrl = 1.0f;
  141. ALfilterState_clear(&state->LowPass);
  142. return STATIC_CAST(ALeffectState, state);
  143. }
  144. DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALautowahStateFactory);
  145. ALeffectStateFactory *ALautowahStateFactory_getFactory(void)
  146. {
  147. static ALautowahStateFactory AutowahFactory = { { GET_VTABLE2(ALautowahStateFactory, ALeffectStateFactory) } };
  148. return STATIC_CAST(ALeffectStateFactory, &AutowahFactory);
  149. }
  150. void ALautowah_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  151. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  152. void ALautowah_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  153. {
  154. ALautowah_setParami(effect, context, param, vals[0]);
  155. }
  156. void ALautowah_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  157. {
  158. ALeffectProps *props = &effect->Props;
  159. switch(param)
  160. {
  161. case AL_AUTOWAH_ATTACK_TIME:
  162. if(!(val >= AL_AUTOWAH_MIN_ATTACK_TIME && val <= AL_AUTOWAH_MAX_ATTACK_TIME))
  163. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  164. props->Autowah.AttackTime = val;
  165. break;
  166. case AL_AUTOWAH_RELEASE_TIME:
  167. if(!(val >= AL_AUTOWAH_MIN_RELEASE_TIME && val <= AL_AUTOWAH_MAX_RELEASE_TIME))
  168. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  169. props->Autowah.ReleaseTime = val;
  170. break;
  171. case AL_AUTOWAH_RESONANCE:
  172. if(!(val >= AL_AUTOWAH_MIN_RESONANCE && val <= AL_AUTOWAH_MAX_RESONANCE))
  173. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  174. props->Autowah.Resonance = val;
  175. break;
  176. case AL_AUTOWAH_PEAK_GAIN:
  177. if(!(val >= AL_AUTOWAH_MIN_PEAK_GAIN && val <= AL_AUTOWAH_MAX_PEAK_GAIN))
  178. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  179. props->Autowah.PeakGain = val;
  180. break;
  181. default:
  182. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  183. }
  184. }
  185. void ALautowah_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  186. {
  187. ALautowah_setParamf(effect, context, param, vals[0]);
  188. }
  189. void ALautowah_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  190. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  191. void ALautowah_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  192. {
  193. ALautowah_getParami(effect, context, param, vals);
  194. }
  195. void ALautowah_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  196. {
  197. const ALeffectProps *props = &effect->Props;
  198. switch(param)
  199. {
  200. case AL_AUTOWAH_ATTACK_TIME:
  201. *val = props->Autowah.AttackTime;
  202. break;
  203. case AL_AUTOWAH_RELEASE_TIME:
  204. *val = props->Autowah.ReleaseTime;
  205. break;
  206. case AL_AUTOWAH_RESONANCE:
  207. *val = props->Autowah.Resonance;
  208. break;
  209. case AL_AUTOWAH_PEAK_GAIN:
  210. *val = props->Autowah.PeakGain;
  211. break;
  212. default:
  213. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  214. }
  215. }
  216. void ALautowah_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  217. {
  218. ALautowah_getParamf(effect, context, param, vals);
  219. }
  220. DEFINE_ALEFFECT_VTABLE(ALautowah);