audio_effect_phaser.cpp 6.1 KB

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