audio_effect_phaser.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**************************************************************************/
  2. /* audio_effect_phaser.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_phaser.h"
  31. #include "core/math/math_funcs.h"
  32. #include "servers/audio_server.h"
  33. void AudioEffectPhaserInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
  34. float sampling_rate = AudioServer::get_singleton()->get_mix_rate();
  35. float dmin = base->range_min / (sampling_rate / 2.0);
  36. float dmax = base->range_max / (sampling_rate / 2.0);
  37. float increment = Math_TAU * (base->rate / sampling_rate);
  38. for (int i = 0; i < p_frame_count; i++) {
  39. phase += increment;
  40. while (phase >= Math_TAU) {
  41. phase -= Math_TAU;
  42. }
  43. float d = dmin + (dmax - dmin) * ((sin(phase) + 1.f) / 2.f);
  44. //update filter coeffs
  45. for (int j = 0; j < 6; j++) {
  46. allpass[0][j].delay(d);
  47. allpass[1][j].delay(d);
  48. }
  49. //calculate output
  50. float y = allpass[0][0].update(
  51. allpass[0][1].update(
  52. allpass[0][2].update(
  53. allpass[0][3].update(
  54. allpass[0][4].update(
  55. allpass[0][5].update(p_src_frames[i].left + h.left * base->feedback))))));
  56. h.left = y;
  57. p_dst_frames[i].left = p_src_frames[i].left + y * base->depth;
  58. y = allpass[1][0].update(
  59. allpass[1][1].update(
  60. allpass[1][2].update(
  61. allpass[1][3].update(
  62. allpass[1][4].update(
  63. allpass[1][5].update(p_src_frames[i].right + h.right * base->feedback))))));
  64. h.right = y;
  65. p_dst_frames[i].right = p_src_frames[i].right + y * base->depth;
  66. }
  67. }
  68. Ref<AudioEffectInstance> AudioEffectPhaser::instantiate() {
  69. Ref<AudioEffectPhaserInstance> ins;
  70. ins.instantiate();
  71. ins->base = Ref<AudioEffectPhaser>(this);
  72. ins->phase = 0;
  73. ins->h = AudioFrame(0, 0);
  74. return ins;
  75. }
  76. void AudioEffectPhaser::set_range_min_hz(float p_hz) {
  77. range_min = p_hz;
  78. }
  79. float AudioEffectPhaser::get_range_min_hz() const {
  80. return range_min;
  81. }
  82. void AudioEffectPhaser::set_range_max_hz(float p_hz) {
  83. range_max = p_hz;
  84. }
  85. float AudioEffectPhaser::get_range_max_hz() const {
  86. return range_max;
  87. }
  88. void AudioEffectPhaser::set_rate_hz(float p_hz) {
  89. rate = p_hz;
  90. }
  91. float AudioEffectPhaser::get_rate_hz() const {
  92. return rate;
  93. }
  94. void AudioEffectPhaser::set_feedback(float p_fbk) {
  95. feedback = p_fbk;
  96. }
  97. float AudioEffectPhaser::get_feedback() const {
  98. return feedback;
  99. }
  100. void AudioEffectPhaser::set_depth(float p_depth) {
  101. depth = p_depth;
  102. }
  103. float AudioEffectPhaser::get_depth() const {
  104. return depth;
  105. }
  106. void AudioEffectPhaser::_bind_methods() {
  107. ClassDB::bind_method(D_METHOD("set_range_min_hz", "hz"), &AudioEffectPhaser::set_range_min_hz);
  108. ClassDB::bind_method(D_METHOD("get_range_min_hz"), &AudioEffectPhaser::get_range_min_hz);
  109. ClassDB::bind_method(D_METHOD("set_range_max_hz", "hz"), &AudioEffectPhaser::set_range_max_hz);
  110. ClassDB::bind_method(D_METHOD("get_range_max_hz"), &AudioEffectPhaser::get_range_max_hz);
  111. ClassDB::bind_method(D_METHOD("set_rate_hz", "hz"), &AudioEffectPhaser::set_rate_hz);
  112. ClassDB::bind_method(D_METHOD("get_rate_hz"), &AudioEffectPhaser::get_rate_hz);
  113. ClassDB::bind_method(D_METHOD("set_feedback", "fbk"), &AudioEffectPhaser::set_feedback);
  114. ClassDB::bind_method(D_METHOD("get_feedback"), &AudioEffectPhaser::get_feedback);
  115. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &AudioEffectPhaser::set_depth);
  116. ClassDB::bind_method(D_METHOD("get_depth"), &AudioEffectPhaser::get_depth);
  117. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "range_min_hz", PROPERTY_HINT_RANGE, "10,10000,suffix:Hz"), "set_range_min_hz", "get_range_min_hz");
  118. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "range_max_hz", PROPERTY_HINT_RANGE, "10,10000,suffix:Hz"), "set_range_max_hz", "get_range_max_hz");
  119. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rate_hz", PROPERTY_HINT_RANGE, "0.01,20,suffix:Hz"), "set_rate_hz", "get_rate_hz");
  120. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "feedback", PROPERTY_HINT_RANGE, "0.1,0.9,0.1"), "set_feedback", "get_feedback");
  121. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_RANGE, "0.1,4,0.1"), "set_depth", "get_depth");
  122. }
  123. AudioEffectPhaser::AudioEffectPhaser() {
  124. range_min = 440;
  125. range_max = 1600;
  126. rate = 0.5;
  127. feedback = 0.7;
  128. depth = 1;
  129. }