distortion.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei 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. cutoff = props->Distortion.LowpassCutoff;
  72. /* Bandwidth value is constant in octaves. */
  73. bandwidth = (cutoff / 2.0f) / (cutoff * 0.67f);
  74. /* Multiply sampling frequency by the amount of oversampling done during
  75. * processing.
  76. */
  77. ALfilterState_setParams(&state->lowpass, ALfilterType_LowPass, 1.0f,
  78. cutoff / (frequency*4.0f), calc_rcpQ_from_bandwidth(cutoff / (frequency*4.0f), bandwidth)
  79. );
  80. cutoff = props->Distortion.EQCenter;
  81. /* Convert bandwidth in Hz to octaves. */
  82. bandwidth = props->Distortion.EQBandwidth / (cutoff * 0.67f);
  83. ALfilterState_setParams(&state->bandpass, ALfilterType_BandPass, 1.0f,
  84. cutoff / (frequency*4.0f), calc_rcpQ_from_bandwidth(cutoff / (frequency*4.0f), bandwidth)
  85. );
  86. ComputeAmbientGains(Device->Dry, Slot->Params.Gain, state->Gain);
  87. }
  88. static ALvoid ALdistortionState_process(ALdistortionState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
  89. {
  90. const ALfloat fc = state->edge_coeff;
  91. ALsizei it, kt;
  92. ALsizei base;
  93. for(base = 0;base < SamplesToDo;)
  94. {
  95. float buffer[2][64 * 4];
  96. ALsizei td = mini(64, SamplesToDo-base);
  97. /* Perform 4x oversampling to avoid aliasing. Oversampling greatly
  98. * improves distortion quality and allows to implement lowpass and
  99. * bandpass filters using high frequencies, at which classic IIR
  100. * filters became unstable.
  101. */
  102. /* Fill oversample buffer using zero stuffing. */
  103. for(it = 0;it < td;it++)
  104. {
  105. /* Multiply the sample by the amount of oversampling to maintain
  106. * the signal's power.
  107. */
  108. buffer[0][it*4 + 0] = SamplesIn[0][it+base] * 4.0f;
  109. buffer[0][it*4 + 1] = 0.0f;
  110. buffer[0][it*4 + 2] = 0.0f;
  111. buffer[0][it*4 + 3] = 0.0f;
  112. }
  113. /* First step, do lowpass filtering of original signal. Additionally
  114. * perform buffer interpolation and lowpass cutoff for oversampling
  115. * (which is fortunately first step of distortion). So combine three
  116. * operations into the one.
  117. */
  118. ALfilterState_process(&state->lowpass, buffer[1], buffer[0], td*4);
  119. /* Second step, do distortion using waveshaper function to emulate
  120. * signal processing during tube overdriving. Three steps of
  121. * waveshaping are intended to modify waveform without boost/clipping/
  122. * attenuation process.
  123. */
  124. for(it = 0;it < td*4;it++)
  125. {
  126. ALfloat smp = buffer[1][it];
  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[0][it] = smp;
  131. }
  132. /* Third step, do bandpass filtering of distorted signal. */
  133. ALfilterState_process(&state->bandpass, buffer[1], buffer[0], td*4);
  134. for(kt = 0;kt < NumChannels;kt++)
  135. {
  136. /* Fourth step, final, do attenuation and perform decimation,
  137. * store only one sample out of 4.
  138. */
  139. ALfloat gain = state->Gain[kt] * state->attenuation;
  140. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  141. continue;
  142. for(it = 0;it < td;it++)
  143. SamplesOut[kt][base+it] += gain * buffer[1][it*4];
  144. }
  145. base += td;
  146. }
  147. }
  148. typedef struct ALdistortionStateFactory {
  149. DERIVE_FROM_TYPE(ALeffectStateFactory);
  150. } ALdistortionStateFactory;
  151. static ALeffectState *ALdistortionStateFactory_create(ALdistortionStateFactory *UNUSED(factory))
  152. {
  153. ALdistortionState *state;
  154. NEW_OBJ0(state, ALdistortionState)();
  155. if(!state) return NULL;
  156. return STATIC_CAST(ALeffectState, state);
  157. }
  158. DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALdistortionStateFactory);
  159. ALeffectStateFactory *ALdistortionStateFactory_getFactory(void)
  160. {
  161. static ALdistortionStateFactory DistortionFactory = { { GET_VTABLE2(ALdistortionStateFactory, ALeffectStateFactory) } };
  162. return STATIC_CAST(ALeffectStateFactory, &DistortionFactory);
  163. }
  164. void ALdistortion_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  165. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  166. void ALdistortion_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  167. {
  168. ALdistortion_setParami(effect, context, param, vals[0]);
  169. }
  170. void ALdistortion_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  171. {
  172. ALeffectProps *props = &effect->Props;
  173. switch(param)
  174. {
  175. case AL_DISTORTION_EDGE:
  176. if(!(val >= AL_DISTORTION_MIN_EDGE && val <= AL_DISTORTION_MAX_EDGE))
  177. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  178. props->Distortion.Edge = val;
  179. break;
  180. case AL_DISTORTION_GAIN:
  181. if(!(val >= AL_DISTORTION_MIN_GAIN && val <= AL_DISTORTION_MAX_GAIN))
  182. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  183. props->Distortion.Gain = val;
  184. break;
  185. case AL_DISTORTION_LOWPASS_CUTOFF:
  186. if(!(val >= AL_DISTORTION_MIN_LOWPASS_CUTOFF && val <= AL_DISTORTION_MAX_LOWPASS_CUTOFF))
  187. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  188. props->Distortion.LowpassCutoff = val;
  189. break;
  190. case AL_DISTORTION_EQCENTER:
  191. if(!(val >= AL_DISTORTION_MIN_EQCENTER && val <= AL_DISTORTION_MAX_EQCENTER))
  192. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  193. props->Distortion.EQCenter = val;
  194. break;
  195. case AL_DISTORTION_EQBANDWIDTH:
  196. if(!(val >= AL_DISTORTION_MIN_EQBANDWIDTH && val <= AL_DISTORTION_MAX_EQBANDWIDTH))
  197. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  198. props->Distortion.EQBandwidth = val;
  199. break;
  200. default:
  201. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  202. }
  203. }
  204. void ALdistortion_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  205. {
  206. ALdistortion_setParamf(effect, context, param, vals[0]);
  207. }
  208. void ALdistortion_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  209. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  210. void ALdistortion_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  211. {
  212. ALdistortion_getParami(effect, context, param, vals);
  213. }
  214. void ALdistortion_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  215. {
  216. const ALeffectProps *props = &effect->Props;
  217. switch(param)
  218. {
  219. case AL_DISTORTION_EDGE:
  220. *val = props->Distortion.Edge;
  221. break;
  222. case AL_DISTORTION_GAIN:
  223. *val = props->Distortion.Gain;
  224. break;
  225. case AL_DISTORTION_LOWPASS_CUTOFF:
  226. *val = props->Distortion.LowpassCutoff;
  227. break;
  228. case AL_DISTORTION_EQCENTER:
  229. *val = props->Distortion.EQCenter;
  230. break;
  231. case AL_DISTORTION_EQBANDWIDTH:
  232. *val = props->Distortion.EQBandwidth;
  233. break;
  234. default:
  235. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  236. }
  237. }
  238. void ALdistortion_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  239. {
  240. ALdistortion_getParamf(effect, context, param, vals);
  241. }
  242. DEFINE_ALEFFECT_VTABLE(ALdistortion);