audio_effect_distortion.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**************************************************************************/
  2. /* audio_effect_distortion.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "audio_effect_distortion.h"
  31. #include "core/math/math_funcs.h"
  32. #include "servers/audio_server.h"
  33. void AudioEffectDistortionInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
  34. const float *src = (const float *)p_src_frames;
  35. float *dst = (float *)p_dst_frames;
  36. //float lpf_c=expf(-2.0*Math_PI*keep_hf_hz.get()/(mix_rate*(float)OVERSAMPLE));
  37. float lpf_c = expf(-2.0 * Math_PI * base->keep_hf_hz / (AudioServer::get_singleton()->get_mix_rate()));
  38. float lpf_ic = 1.0 - lpf_c;
  39. float drive_f = base->drive;
  40. float pregain_f = Math::db2linear(base->pre_gain);
  41. float postgain_f = Math::db2linear(base->post_gain);
  42. float atan_mult = pow(10, drive_f * drive_f * 3.0) - 1.0 + 0.001;
  43. float atan_div = 1.0 / (atanf(atan_mult) * (1.0 + drive_f * 8));
  44. float lofi_mult = powf(2.0, 2.0 + (1.0 - drive_f) * 14); //goes from 16 to 2 bits
  45. for (int i = 0; i < p_frame_count * 2; i++) {
  46. float out = undenormalise(src[i] * lpf_ic + lpf_c * h[i & 1]);
  47. h[i & 1] = out;
  48. float a = out;
  49. float ha = src[i] - out; //high freqs
  50. a *= pregain_f;
  51. switch (base->mode) {
  52. case AudioEffectDistortion::MODE_CLIP: {
  53. float a_sign = a < 0 ? -1.0f : 1.0f;
  54. a = powf(abs(a), 1.0001 - drive_f) * a_sign;
  55. if (a > 1.0) {
  56. a = 1.0;
  57. } else if (a < (-1.0)) {
  58. a = -1.0;
  59. }
  60. } break;
  61. case AudioEffectDistortion::MODE_ATAN: {
  62. a = atanf(a * atan_mult) * atan_div;
  63. } break;
  64. case AudioEffectDistortion::MODE_LOFI: {
  65. a = floorf(a * lofi_mult + 0.5) / lofi_mult;
  66. } break;
  67. case AudioEffectDistortion::MODE_OVERDRIVE: {
  68. const double x = a * 0.686306;
  69. const double z = 1 + exp(sqrt(fabs(x)) * -0.75);
  70. a = (expf(x) - expf(-x * z)) / (expf(x) + expf(-x));
  71. } break;
  72. case AudioEffectDistortion::MODE_WAVESHAPE: {
  73. float x = a;
  74. float k = 2 * drive_f / (1.00001 - drive_f);
  75. a = (1.0 + k) * x / (1.0 + k * fabsf(x));
  76. } break;
  77. }
  78. dst[i] = a * postgain_f + ha;
  79. }
  80. }
  81. Ref<AudioEffectInstance> AudioEffectDistortion::instance() {
  82. Ref<AudioEffectDistortionInstance> ins;
  83. ins.instance();
  84. ins->base = Ref<AudioEffectDistortion>(this);
  85. ins->h[0] = 0;
  86. ins->h[1] = 0;
  87. return ins;
  88. }
  89. void AudioEffectDistortion::set_mode(Mode p_mode) {
  90. mode = p_mode;
  91. }
  92. AudioEffectDistortion::Mode AudioEffectDistortion::get_mode() const {
  93. return mode;
  94. }
  95. void AudioEffectDistortion::set_pre_gain(float p_pre_gain) {
  96. pre_gain = p_pre_gain;
  97. }
  98. float AudioEffectDistortion::get_pre_gain() const {
  99. return pre_gain;
  100. }
  101. void AudioEffectDistortion::set_keep_hf_hz(float p_keep_hf_hz) {
  102. keep_hf_hz = p_keep_hf_hz;
  103. }
  104. float AudioEffectDistortion::get_keep_hf_hz() const {
  105. return keep_hf_hz;
  106. }
  107. void AudioEffectDistortion::set_drive(float p_drive) {
  108. drive = p_drive;
  109. }
  110. float AudioEffectDistortion::get_drive() const {
  111. return drive;
  112. }
  113. void AudioEffectDistortion::set_post_gain(float p_post_gain) {
  114. post_gain = p_post_gain;
  115. }
  116. float AudioEffectDistortion::get_post_gain() const {
  117. return post_gain;
  118. }
  119. void AudioEffectDistortion::_bind_methods() {
  120. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &AudioEffectDistortion::set_mode);
  121. ClassDB::bind_method(D_METHOD("get_mode"), &AudioEffectDistortion::get_mode);
  122. ClassDB::bind_method(D_METHOD("set_pre_gain", "pre_gain"), &AudioEffectDistortion::set_pre_gain);
  123. ClassDB::bind_method(D_METHOD("get_pre_gain"), &AudioEffectDistortion::get_pre_gain);
  124. ClassDB::bind_method(D_METHOD("set_keep_hf_hz", "keep_hf_hz"), &AudioEffectDistortion::set_keep_hf_hz);
  125. ClassDB::bind_method(D_METHOD("get_keep_hf_hz"), &AudioEffectDistortion::get_keep_hf_hz);
  126. ClassDB::bind_method(D_METHOD("set_drive", "drive"), &AudioEffectDistortion::set_drive);
  127. ClassDB::bind_method(D_METHOD("get_drive"), &AudioEffectDistortion::get_drive);
  128. ClassDB::bind_method(D_METHOD("set_post_gain", "post_gain"), &AudioEffectDistortion::set_post_gain);
  129. ClassDB::bind_method(D_METHOD("get_post_gain"), &AudioEffectDistortion::get_post_gain);
  130. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Clip,ATan,LoFi,Overdrive,WaveShape"), "set_mode", "get_mode");
  131. ADD_PROPERTY(PropertyInfo(Variant::REAL, "pre_gain", PROPERTY_HINT_RANGE, "-60,60,0.01"), "set_pre_gain", "get_pre_gain");
  132. ADD_PROPERTY(PropertyInfo(Variant::REAL, "keep_hf_hz", PROPERTY_HINT_RANGE, "1,20500,1"), "set_keep_hf_hz", "get_keep_hf_hz");
  133. ADD_PROPERTY(PropertyInfo(Variant::REAL, "drive", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_drive", "get_drive");
  134. ADD_PROPERTY(PropertyInfo(Variant::REAL, "post_gain", PROPERTY_HINT_RANGE, "-80,24,0.01"), "set_post_gain", "get_post_gain");
  135. BIND_ENUM_CONSTANT(MODE_CLIP);
  136. BIND_ENUM_CONSTANT(MODE_ATAN);
  137. BIND_ENUM_CONSTANT(MODE_LOFI);
  138. BIND_ENUM_CONSTANT(MODE_OVERDRIVE);
  139. BIND_ENUM_CONSTANT(MODE_WAVESHAPE);
  140. }
  141. AudioEffectDistortion::AudioEffectDistortion() {
  142. mode = MODE_CLIP;
  143. pre_gain = 0;
  144. post_gain = 0;
  145. keep_hf_hz = 16000;
  146. drive = 0;
  147. }