2
0

distortion.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 *UNUSED(state))
  39. {
  40. }
  41. static ALboolean ALdistortionState_deviceUpdate(ALdistortionState *UNUSED(state), ALCdevice *UNUSED(device))
  42. {
  43. return AL_TRUE;
  44. }
  45. static ALvoid ALdistortionState_update(ALdistortionState *state, ALCdevice *Device, const ALeffectslot *Slot)
  46. {
  47. ALfloat frequency = (ALfloat)Device->Frequency;
  48. ALfloat bandwidth;
  49. ALfloat cutoff;
  50. ALfloat edge;
  51. /* Store distorted signal attenuation settings */
  52. state->attenuation = Slot->EffectProps.Distortion.Gain;
  53. /* Store waveshaper edge settings */
  54. edge = sinf(Slot->EffectProps.Distortion.Edge * (F_PI_2));
  55. edge = minf(edge, 0.99f);
  56. state->edge_coeff = 2.0f * edge / (1.0f-edge);
  57. /* Lowpass filter */
  58. cutoff = Slot->EffectProps.Distortion.LowpassCutoff;
  59. /* Bandwidth value is constant in octaves */
  60. bandwidth = (cutoff / 2.0f) / (cutoff * 0.67f);
  61. ALfilterState_setParams(&state->lowpass, ALfilterType_LowPass, 1.0f,
  62. cutoff / (frequency*4.0f), calc_rcpQ_from_bandwidth(cutoff / (frequency*4.0f), bandwidth)
  63. );
  64. /* Bandpass filter */
  65. cutoff = Slot->EffectProps.Distortion.EQCenter;
  66. /* Convert bandwidth in Hz to octaves */
  67. bandwidth = Slot->EffectProps.Distortion.EQBandwidth / (cutoff * 0.67f);
  68. ALfilterState_setParams(&state->bandpass, ALfilterType_BandPass, 1.0f,
  69. cutoff / (frequency*4.0f), calc_rcpQ_from_bandwidth(cutoff / (frequency*4.0f), bandwidth)
  70. );
  71. ComputeAmbientGains(Device, Slot->Gain, state->Gain);
  72. }
  73. static ALvoid ALdistortionState_process(ALdistortionState *state, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
  74. {
  75. const ALfloat fc = state->edge_coeff;
  76. ALuint base;
  77. ALuint it;
  78. ALuint ot;
  79. ALuint kt;
  80. for(base = 0;base < SamplesToDo;)
  81. {
  82. float oversample_buffer[64][4];
  83. ALuint td = minu(64, SamplesToDo-base);
  84. /* Perform 4x oversampling to avoid aliasing. */
  85. /* Oversampling greatly improves distortion */
  86. /* quality and allows to implement lowpass and */
  87. /* bandpass filters using high frequencies, at */
  88. /* which classic IIR filters became unstable. */
  89. /* Fill oversample buffer using zero stuffing */
  90. for(it = 0;it < td;it++)
  91. {
  92. oversample_buffer[it][0] = SamplesIn[it+base];
  93. oversample_buffer[it][1] = 0.0f;
  94. oversample_buffer[it][2] = 0.0f;
  95. oversample_buffer[it][3] = 0.0f;
  96. }
  97. /* First step, do lowpass filtering of original signal, */
  98. /* additionally perform buffer interpolation and lowpass */
  99. /* cutoff for oversampling (which is fortunately first */
  100. /* step of distortion). So combine three operations into */
  101. /* the one. */
  102. for(it = 0;it < td;it++)
  103. {
  104. for(ot = 0;ot < 4;ot++)
  105. {
  106. ALfloat smp;
  107. smp = ALfilterState_processSingle(&state->lowpass, oversample_buffer[it][ot]);
  108. /* Restore signal power by multiplying sample by amount of oversampling */
  109. oversample_buffer[it][ot] = smp * 4.0f;
  110. }
  111. }
  112. for(it = 0;it < td;it++)
  113. {
  114. /* Second step, do distortion using waveshaper function */
  115. /* to emulate signal processing during tube overdriving. */
  116. /* Three steps of waveshaping are intended to modify */
  117. /* waveform without boost/clipping/attenuation process. */
  118. for(ot = 0;ot < 4;ot++)
  119. {
  120. ALfloat smp = oversample_buffer[it][ot];
  121. smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp));
  122. smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp)) * -1.0f;
  123. smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp));
  124. /* Third step, do bandpass filtering of distorted signal */
  125. smp = ALfilterState_processSingle(&state->bandpass, smp);
  126. oversample_buffer[it][ot] = smp;
  127. }
  128. }
  129. for(kt = 0;kt < NumChannels;kt++)
  130. {
  131. /* Fourth step, final, do attenuation and perform decimation,
  132. * store only one sample out of 4.
  133. */
  134. ALfloat gain = state->Gain[kt] * state->attenuation;
  135. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  136. continue;
  137. for(it = 0;it < td;it++)
  138. SamplesOut[kt][base+it] += gain * oversample_buffer[it][0];
  139. }
  140. base += td;
  141. }
  142. }
  143. DECLARE_DEFAULT_ALLOCATORS(ALdistortionState)
  144. DEFINE_ALEFFECTSTATE_VTABLE(ALdistortionState);
  145. typedef struct ALdistortionStateFactory {
  146. DERIVE_FROM_TYPE(ALeffectStateFactory);
  147. } ALdistortionStateFactory;
  148. static ALeffectState *ALdistortionStateFactory_create(ALdistortionStateFactory *UNUSED(factory))
  149. {
  150. ALdistortionState *state;
  151. state = ALdistortionState_New(sizeof(*state));
  152. if(!state) return NULL;
  153. SET_VTABLE2(ALdistortionState, ALeffectState, state);
  154. ALfilterState_clear(&state->lowpass);
  155. ALfilterState_clear(&state->bandpass);
  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);