distortion.c 10 KB

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