distortion.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "alAuxEffectSlot.h"
  25. #include "alError.h"
  26. #include "alu.h"
  27. #include "filters/defs.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. BiquadFilter lowpass;
  34. BiquadFilter bandpass;
  35. ALfloat attenuation;
  36. ALfloat edge_coeff;
  37. ALfloat Buffer[2][BUFFERSIZE];
  38. } ALdistortionState;
  39. static ALvoid ALdistortionState_Destruct(ALdistortionState *state);
  40. static ALboolean ALdistortionState_deviceUpdate(ALdistortionState *state, ALCdevice *device);
  41. static ALvoid ALdistortionState_update(ALdistortionState *state, const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props);
  42. static ALvoid ALdistortionState_process(ALdistortionState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels);
  43. DECLARE_DEFAULT_ALLOCATORS(ALdistortionState)
  44. DEFINE_ALEFFECTSTATE_VTABLE(ALdistortionState);
  45. static void ALdistortionState_Construct(ALdistortionState *state)
  46. {
  47. ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
  48. SET_VTABLE2(ALdistortionState, ALeffectState, state);
  49. }
  50. static ALvoid ALdistortionState_Destruct(ALdistortionState *state)
  51. {
  52. ALeffectState_Destruct(STATIC_CAST(ALeffectState,state));
  53. }
  54. static ALboolean ALdistortionState_deviceUpdate(ALdistortionState *state, ALCdevice *UNUSED(device))
  55. {
  56. BiquadFilter_clear(&state->lowpass);
  57. BiquadFilter_clear(&state->bandpass);
  58. return AL_TRUE;
  59. }
  60. static ALvoid ALdistortionState_update(ALdistortionState *state, const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props)
  61. {
  62. const ALCdevice *device = context->Device;
  63. ALfloat frequency = (ALfloat)device->Frequency;
  64. ALfloat coeffs[MAX_AMBI_COEFFS];
  65. ALfloat bandwidth;
  66. ALfloat cutoff;
  67. ALfloat edge;
  68. /* Store waveshaper edge settings. */
  69. edge = sinf(props->Distortion.Edge * (F_PI_2));
  70. edge = minf(edge, 0.99f);
  71. state->edge_coeff = 2.0f * edge / (1.0f-edge);
  72. cutoff = props->Distortion.LowpassCutoff;
  73. /* Bandwidth value is constant in octaves. */
  74. bandwidth = (cutoff / 2.0f) / (cutoff * 0.67f);
  75. /* Multiply sampling frequency by the amount of oversampling done during
  76. * processing.
  77. */
  78. BiquadFilter_setParams(&state->lowpass, BiquadType_LowPass, 1.0f,
  79. cutoff / (frequency*4.0f), calc_rcpQ_from_bandwidth(cutoff / (frequency*4.0f), bandwidth)
  80. );
  81. cutoff = props->Distortion.EQCenter;
  82. /* Convert bandwidth in Hz to octaves. */
  83. bandwidth = props->Distortion.EQBandwidth / (cutoff * 0.67f);
  84. BiquadFilter_setParams(&state->bandpass, BiquadType_BandPass, 1.0f,
  85. cutoff / (frequency*4.0f), calc_rcpQ_from_bandwidth(cutoff / (frequency*4.0f), bandwidth)
  86. );
  87. CalcAngleCoeffs(0.0f, 0.0f, 0.0f, coeffs);
  88. ComputeDryPanGains(&device->Dry, coeffs, slot->Params.Gain * props->Distortion.Gain,
  89. state->Gain);
  90. }
  91. static ALvoid ALdistortionState_process(ALdistortionState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
  92. {
  93. ALfloat (*restrict buffer)[BUFFERSIZE] = state->Buffer;
  94. const ALfloat fc = state->edge_coeff;
  95. ALsizei base;
  96. ALsizei i, k;
  97. for(base = 0;base < SamplesToDo;)
  98. {
  99. /* Perform 4x oversampling to avoid aliasing. Oversampling greatly
  100. * improves distortion quality and allows to implement lowpass and
  101. * bandpass filters using high frequencies, at which classic IIR
  102. * filters became unstable.
  103. */
  104. ALsizei todo = mini(BUFFERSIZE, (SamplesToDo-base) * 4);
  105. /* Fill oversample buffer using zero stuffing. Multiply the sample by
  106. * the amount of oversampling to maintain the signal's power.
  107. */
  108. for(i = 0;i < todo;i++)
  109. buffer[0][i] = !(i&3) ? SamplesIn[0][(i>>2)+base] * 4.0f : 0.0f;
  110. /* First step, do lowpass filtering of original signal. Additionally
  111. * perform buffer interpolation and lowpass cutoff for oversampling
  112. * (which is fortunately first step of distortion). So combine three
  113. * operations into the one.
  114. */
  115. BiquadFilter_process(&state->lowpass, buffer[1], buffer[0], todo);
  116. /* Second step, do distortion using waveshaper function to emulate
  117. * signal processing during tube overdriving. Three steps of
  118. * waveshaping are intended to modify waveform without boost/clipping/
  119. * attenuation process.
  120. */
  121. for(i = 0;i < todo;i++)
  122. {
  123. ALfloat smp = buffer[1][i];
  124. smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp));
  125. smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp)) * -1.0f;
  126. smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp));
  127. buffer[0][i] = smp;
  128. }
  129. /* Third step, do bandpass filtering of distorted signal. */
  130. BiquadFilter_process(&state->bandpass, buffer[1], buffer[0], todo);
  131. todo >>= 2;
  132. for(k = 0;k < NumChannels;k++)
  133. {
  134. /* Fourth step, final, do attenuation and perform decimation,
  135. * storing only one sample out of four.
  136. */
  137. ALfloat gain = state->Gain[k];
  138. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  139. continue;
  140. for(i = 0;i < todo;i++)
  141. SamplesOut[k][base+i] += gain * buffer[1][i*4];
  142. }
  143. base += todo;
  144. }
  145. }
  146. typedef struct DistortionStateFactory {
  147. DERIVE_FROM_TYPE(EffectStateFactory);
  148. } DistortionStateFactory;
  149. static ALeffectState *DistortionStateFactory_create(DistortionStateFactory *UNUSED(factory))
  150. {
  151. ALdistortionState *state;
  152. NEW_OBJ0(state, ALdistortionState)();
  153. if(!state) return NULL;
  154. return STATIC_CAST(ALeffectState, state);
  155. }
  156. DEFINE_EFFECTSTATEFACTORY_VTABLE(DistortionStateFactory);
  157. EffectStateFactory *DistortionStateFactory_getFactory(void)
  158. {
  159. static DistortionStateFactory DistortionFactory = { { GET_VTABLE2(DistortionStateFactory, EffectStateFactory) } };
  160. return STATIC_CAST(EffectStateFactory, &DistortionFactory);
  161. }
  162. void ALdistortion_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALint UNUSED(val))
  163. { alSetError(context, AL_INVALID_ENUM, "Invalid distortion integer property 0x%04x", param); }
  164. void ALdistortion_setParamiv(ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, const ALint *UNUSED(vals))
  165. { alSetError(context, AL_INVALID_ENUM, "Invalid distortion integer-vector property 0x%04x", param); }
  166. void ALdistortion_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  167. {
  168. ALeffectProps *props = &effect->Props;
  169. switch(param)
  170. {
  171. case AL_DISTORTION_EDGE:
  172. if(!(val >= AL_DISTORTION_MIN_EDGE && val <= AL_DISTORTION_MAX_EDGE))
  173. SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion edge out of range");
  174. props->Distortion.Edge = val;
  175. break;
  176. case AL_DISTORTION_GAIN:
  177. if(!(val >= AL_DISTORTION_MIN_GAIN && val <= AL_DISTORTION_MAX_GAIN))
  178. SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion gain out of range");
  179. props->Distortion.Gain = val;
  180. break;
  181. case AL_DISTORTION_LOWPASS_CUTOFF:
  182. if(!(val >= AL_DISTORTION_MIN_LOWPASS_CUTOFF && val <= AL_DISTORTION_MAX_LOWPASS_CUTOFF))
  183. SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion low-pass cutoff out of range");
  184. props->Distortion.LowpassCutoff = val;
  185. break;
  186. case AL_DISTORTION_EQCENTER:
  187. if(!(val >= AL_DISTORTION_MIN_EQCENTER && val <= AL_DISTORTION_MAX_EQCENTER))
  188. SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion EQ center out of range");
  189. props->Distortion.EQCenter = val;
  190. break;
  191. case AL_DISTORTION_EQBANDWIDTH:
  192. if(!(val >= AL_DISTORTION_MIN_EQBANDWIDTH && val <= AL_DISTORTION_MAX_EQBANDWIDTH))
  193. SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion EQ bandwidth out of range");
  194. props->Distortion.EQBandwidth = val;
  195. break;
  196. default:
  197. alSetError(context, AL_INVALID_ENUM, "Invalid distortion float property 0x%04x",
  198. param);
  199. }
  200. }
  201. void ALdistortion_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  202. { ALdistortion_setParamf(effect, context, param, vals[0]); }
  203. void ALdistortion_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALint *UNUSED(val))
  204. { alSetError(context, AL_INVALID_ENUM, "Invalid distortion integer property 0x%04x", param); }
  205. void ALdistortion_getParamiv(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALint *UNUSED(vals))
  206. { alSetError(context, AL_INVALID_ENUM, "Invalid distortion integer-vector property 0x%04x", param); }
  207. void ALdistortion_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  208. {
  209. const ALeffectProps *props = &effect->Props;
  210. switch(param)
  211. {
  212. case AL_DISTORTION_EDGE:
  213. *val = props->Distortion.Edge;
  214. break;
  215. case AL_DISTORTION_GAIN:
  216. *val = props->Distortion.Gain;
  217. break;
  218. case AL_DISTORTION_LOWPASS_CUTOFF:
  219. *val = props->Distortion.LowpassCutoff;
  220. break;
  221. case AL_DISTORTION_EQCENTER:
  222. *val = props->Distortion.EQCenter;
  223. break;
  224. case AL_DISTORTION_EQBANDWIDTH:
  225. *val = props->Distortion.EQBandwidth;
  226. break;
  227. default:
  228. alSetError(context, AL_INVALID_ENUM, "Invalid distortion float property 0x%04x",
  229. param);
  230. }
  231. }
  232. void ALdistortion_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  233. { ALdistortion_getParamf(effect, context, param, vals); }
  234. DEFINE_ALEFFECT_VTABLE(ALdistortion);