distortion.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2013 by Mike Gorchak
  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 "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 ALdistortionState {
  29. DERIVE_FROM_TYPE(ALeffectState);
  30. /* Effect gains for each channel */
  31. ALfloat Gain[MAX_OUTPUT_CHANNELS];
  32. /* Effect parameters */
  33. ALfilterState lowpass;
  34. ALfilterState bandpass;
  35. ALfloat attenuation;
  36. ALfloat edge_coeff;
  37. } ALdistortionState;
  38. static ALvoid ALdistortionState_Destruct(ALdistortionState *state);
  39. static ALboolean ALdistortionState_deviceUpdate(ALdistortionState *state, ALCdevice *device);
  40. static ALvoid ALdistortionState_update(ALdistortionState *state, const ALCdevice *Device, const ALeffectslot *Slot, const ALeffectProps *props);
  41. static ALvoid ALdistortionState_process(ALdistortionState *state, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels);
  42. DECLARE_DEFAULT_ALLOCATORS(ALdistortionState)
  43. DEFINE_ALEFFECTSTATE_VTABLE(ALdistortionState);
  44. static void ALdistortionState_Construct(ALdistortionState *state)
  45. {
  46. ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
  47. SET_VTABLE2(ALdistortionState, ALeffectState, state);
  48. ALfilterState_clear(&state->lowpass);
  49. ALfilterState_clear(&state->bandpass);
  50. }
  51. static ALvoid ALdistortionState_Destruct(ALdistortionState *state)
  52. {
  53. ALeffectState_Destruct(STATIC_CAST(ALeffectState,state));
  54. }
  55. static ALboolean ALdistortionState_deviceUpdate(ALdistortionState *UNUSED(state), ALCdevice *UNUSED(device))
  56. {
  57. return AL_TRUE;
  58. }
  59. static ALvoid ALdistortionState_update(ALdistortionState *state, const ALCdevice *Device, const ALeffectslot *Slot, const ALeffectProps *props)
  60. {
  61. ALfloat frequency = (ALfloat)Device->Frequency;
  62. ALfloat bandwidth;
  63. ALfloat cutoff;
  64. ALfloat edge;
  65. /* Store distorted signal attenuation settings */
  66. state->attenuation = props->Distortion.Gain;
  67. /* Store waveshaper edge settings */
  68. edge = sinf(props->Distortion.Edge * (F_PI_2));
  69. edge = minf(edge, 0.99f);
  70. state->edge_coeff = 2.0f * edge / (1.0f-edge);
  71. /* Lowpass filter */
  72. cutoff = props->Distortion.LowpassCutoff;
  73. /* Bandwidth value is constant in octaves */
  74. bandwidth = (cutoff / 2.0f) / (cutoff * 0.67f);
  75. ALfilterState_setParams(&state->lowpass, ALfilterType_LowPass, 1.0f,
  76. cutoff / (frequency*4.0f), calc_rcpQ_from_bandwidth(cutoff / (frequency*4.0f), bandwidth)
  77. );
  78. /* Bandpass filter */
  79. cutoff = props->Distortion.EQCenter;
  80. /* Convert bandwidth in Hz to octaves */
  81. bandwidth = props->Distortion.EQBandwidth / (cutoff * 0.67f);
  82. ALfilterState_setParams(&state->bandpass, ALfilterType_BandPass, 1.0f,
  83. cutoff / (frequency*4.0f), calc_rcpQ_from_bandwidth(cutoff / (frequency*4.0f), bandwidth)
  84. );
  85. ComputeAmbientGains(Device->Dry, Slot->Params.Gain, state->Gain);
  86. }
  87. static ALvoid ALdistortionState_process(ALdistortionState *state, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
  88. {
  89. const ALfloat fc = state->edge_coeff;
  90. ALuint base;
  91. ALuint it;
  92. ALuint ot;
  93. ALuint kt;
  94. for(base = 0;base < SamplesToDo;)
  95. {
  96. float buffer[2][64 * 4];
  97. ALuint td = minu(64, SamplesToDo-base);
  98. /* Perform 4x oversampling to avoid aliasing. */
  99. /* Oversampling greatly improves distortion */
  100. /* quality and allows to implement lowpass and */
  101. /* bandpass filters using high frequencies, at */
  102. /* which classic IIR filters became unstable. */
  103. /* Fill oversample buffer using zero stuffing */
  104. for(it = 0;it < td;it++)
  105. {
  106. buffer[0][it*4 + 0] = SamplesIn[0][it+base];
  107. buffer[0][it*4 + 1] = 0.0f;
  108. buffer[0][it*4 + 2] = 0.0f;
  109. buffer[0][it*4 + 3] = 0.0f;
  110. }
  111. /* First step, do lowpass filtering of original signal, */
  112. /* additionally perform buffer interpolation and lowpass */
  113. /* cutoff for oversampling (which is fortunately first */
  114. /* step of distortion). So combine three operations into */
  115. /* the one. */
  116. ALfilterState_process(&state->lowpass, buffer[1], buffer[0], td*4);
  117. /* Second step, do distortion using waveshaper function */
  118. /* to emulate signal processing during tube overdriving. */
  119. /* Three steps of waveshaping are intended to modify */
  120. /* waveform without boost/clipping/attenuation process. */
  121. for(it = 0;it < td;it++)
  122. {
  123. for(ot = 0;ot < 4;ot++)
  124. {
  125. /* Restore signal power by multiplying sample by amount of oversampling */
  126. ALfloat smp = buffer[1][it*4 + ot] * 4.0f;
  127. smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp));
  128. smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp)) * -1.0f;
  129. smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp));
  130. buffer[1][it*4 + ot] = smp;
  131. }
  132. }
  133. /* Third step, do bandpass filtering of distorted signal */
  134. ALfilterState_process(&state->bandpass, buffer[0], buffer[1], td*4);
  135. for(kt = 0;kt < NumChannels;kt++)
  136. {
  137. /* Fourth step, final, do attenuation and perform decimation,
  138. * store only one sample out of 4.
  139. */
  140. ALfloat gain = state->Gain[kt] * state->attenuation;
  141. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  142. continue;
  143. for(it = 0;it < td;it++)
  144. SamplesOut[kt][base+it] += gain * buffer[0][it*4];
  145. }
  146. base += td;
  147. }
  148. }
  149. typedef struct ALdistortionStateFactory {
  150. DERIVE_FROM_TYPE(ALeffectStateFactory);
  151. } ALdistortionStateFactory;
  152. static ALeffectState *ALdistortionStateFactory_create(ALdistortionStateFactory *UNUSED(factory))
  153. {
  154. ALdistortionState *state;
  155. NEW_OBJ0(state, ALdistortionState)();
  156. if(!state) return NULL;
  157. return STATIC_CAST(ALeffectState, state);
  158. }
  159. DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALdistortionStateFactory);
  160. ALeffectStateFactory *ALdistortionStateFactory_getFactory(void)
  161. {
  162. static ALdistortionStateFactory DistortionFactory = { { GET_VTABLE2(ALdistortionStateFactory, ALeffectStateFactory) } };
  163. return STATIC_CAST(ALeffectStateFactory, &DistortionFactory);
  164. }
  165. void ALdistortion_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  166. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  167. void ALdistortion_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  168. {
  169. ALdistortion_setParami(effect, context, param, vals[0]);
  170. }
  171. void ALdistortion_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  172. {
  173. ALeffectProps *props = &effect->Props;
  174. switch(param)
  175. {
  176. case AL_DISTORTION_EDGE:
  177. if(!(val >= AL_DISTORTION_MIN_EDGE && val <= AL_DISTORTION_MAX_EDGE))
  178. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  179. props->Distortion.Edge = val;
  180. break;
  181. case AL_DISTORTION_GAIN:
  182. if(!(val >= AL_DISTORTION_MIN_GAIN && val <= AL_DISTORTION_MAX_GAIN))
  183. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  184. props->Distortion.Gain = val;
  185. break;
  186. case AL_DISTORTION_LOWPASS_CUTOFF:
  187. if(!(val >= AL_DISTORTION_MIN_LOWPASS_CUTOFF && val <= AL_DISTORTION_MAX_LOWPASS_CUTOFF))
  188. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  189. props->Distortion.LowpassCutoff = val;
  190. break;
  191. case AL_DISTORTION_EQCENTER:
  192. if(!(val >= AL_DISTORTION_MIN_EQCENTER && val <= AL_DISTORTION_MAX_EQCENTER))
  193. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  194. props->Distortion.EQCenter = val;
  195. break;
  196. case AL_DISTORTION_EQBANDWIDTH:
  197. if(!(val >= AL_DISTORTION_MIN_EQBANDWIDTH && val <= AL_DISTORTION_MAX_EQBANDWIDTH))
  198. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  199. props->Distortion.EQBandwidth = val;
  200. break;
  201. default:
  202. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  203. }
  204. }
  205. void ALdistortion_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  206. {
  207. ALdistortion_setParamf(effect, context, param, vals[0]);
  208. }
  209. void ALdistortion_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  210. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  211. void ALdistortion_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  212. {
  213. ALdistortion_getParami(effect, context, param, vals);
  214. }
  215. void ALdistortion_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  216. {
  217. const ALeffectProps *props = &effect->Props;
  218. switch(param)
  219. {
  220. case AL_DISTORTION_EDGE:
  221. *val = props->Distortion.Edge;
  222. break;
  223. case AL_DISTORTION_GAIN:
  224. *val = props->Distortion.Gain;
  225. break;
  226. case AL_DISTORTION_LOWPASS_CUTOFF:
  227. *val = props->Distortion.LowpassCutoff;
  228. break;
  229. case AL_DISTORTION_EQCENTER:
  230. *val = props->Distortion.EQCenter;
  231. break;
  232. case AL_DISTORTION_EQBANDWIDTH:
  233. *val = props->Distortion.EQBandwidth;
  234. break;
  235. default:
  236. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  237. }
  238. }
  239. void ALdistortion_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  240. {
  241. ALdistortion_getParamf(effect, context, param, vals);
  242. }
  243. DEFINE_ALEFFECT_VTABLE(ALdistortion);