audio_effect_stereo_enhance.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**************************************************************************/
  2. /* audio_effect_stereo_enhance.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_stereo_enhance.h"
  31. #include "servers/audio_server.h"
  32. void AudioEffectStereoEnhanceInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
  33. float intensity = base->pan_pullout;
  34. bool surround_mode = base->surround > 0;
  35. float surround_amount = base->surround;
  36. unsigned int delay_frames = (base->time_pullout / 1000.0) * AudioServer::get_singleton()->get_mix_rate();
  37. for (int i = 0; i < p_frame_count; i++) {
  38. float left = p_src_frames[i].left;
  39. float right = p_src_frames[i].right;
  40. float center = (left + right) / 2.0f;
  41. left = (center + (left - center) * intensity);
  42. right = (center + (right - center) * intensity);
  43. if (surround_mode) {
  44. float val = (left + right) / 2.0;
  45. delay_ringbuff[ringbuff_pos & ringbuff_mask] = val;
  46. float out = delay_ringbuff[(ringbuff_pos - delay_frames) & ringbuff_mask] * surround_amount;
  47. left += out;
  48. right += -out;
  49. } else {
  50. float val = right;
  51. delay_ringbuff[ringbuff_pos & ringbuff_mask] = val;
  52. // The right channel is delayed.
  53. right = delay_ringbuff[(ringbuff_pos - delay_frames) & ringbuff_mask];
  54. }
  55. p_dst_frames[i].left = left;
  56. p_dst_frames[i].right = right;
  57. ringbuff_pos++;
  58. }
  59. }
  60. AudioEffectStereoEnhanceInstance::~AudioEffectStereoEnhanceInstance() {
  61. memdelete_arr(delay_ringbuff);
  62. }
  63. Ref<AudioEffectInstance> AudioEffectStereoEnhance::instantiate() {
  64. Ref<AudioEffectStereoEnhanceInstance> ins;
  65. ins.instantiate();
  66. ins->base = Ref<AudioEffectStereoEnhance>(this);
  67. float ring_buffer_max_size = AudioEffectStereoEnhanceInstance::MAX_DELAY_MS + 2;
  68. ring_buffer_max_size /= 1000.0; //convert to seconds
  69. ring_buffer_max_size *= AudioServer::get_singleton()->get_mix_rate();
  70. int ringbuff_size = (int)ring_buffer_max_size;
  71. int bits = 0;
  72. while (ringbuff_size > 0) {
  73. bits++;
  74. ringbuff_size /= 2;
  75. }
  76. ringbuff_size = 1 << bits;
  77. ins->ringbuff_mask = ringbuff_size - 1;
  78. ins->ringbuff_pos = 0;
  79. ins->delay_ringbuff = memnew_arr(float, ringbuff_size);
  80. return ins;
  81. }
  82. void AudioEffectStereoEnhance::set_pan_pullout(float p_amount) {
  83. pan_pullout = p_amount;
  84. }
  85. float AudioEffectStereoEnhance::get_pan_pullout() const {
  86. return pan_pullout;
  87. }
  88. void AudioEffectStereoEnhance::set_time_pullout(float p_amount) {
  89. time_pullout = p_amount;
  90. }
  91. float AudioEffectStereoEnhance::get_time_pullout() const {
  92. return time_pullout;
  93. }
  94. void AudioEffectStereoEnhance::set_surround(float p_amount) {
  95. surround = p_amount;
  96. }
  97. float AudioEffectStereoEnhance::get_surround() const {
  98. return surround;
  99. }
  100. void AudioEffectStereoEnhance::_bind_methods() {
  101. ClassDB::bind_method(D_METHOD("set_pan_pullout", "amount"), &AudioEffectStereoEnhance::set_pan_pullout);
  102. ClassDB::bind_method(D_METHOD("get_pan_pullout"), &AudioEffectStereoEnhance::get_pan_pullout);
  103. ClassDB::bind_method(D_METHOD("set_time_pullout", "amount"), &AudioEffectStereoEnhance::set_time_pullout);
  104. ClassDB::bind_method(D_METHOD("get_time_pullout"), &AudioEffectStereoEnhance::get_time_pullout);
  105. ClassDB::bind_method(D_METHOD("set_surround", "amount"), &AudioEffectStereoEnhance::set_surround);
  106. ClassDB::bind_method(D_METHOD("get_surround"), &AudioEffectStereoEnhance::get_surround);
  107. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pan_pullout", PROPERTY_HINT_RANGE, "0,4,0.01"), "set_pan_pullout", "get_pan_pullout");
  108. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_pullout_ms", PROPERTY_HINT_RANGE, "0,50,0.01,suffix:ms"), "set_time_pullout", "get_time_pullout");
  109. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "surround", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_surround", "get_surround");
  110. }
  111. AudioEffectStereoEnhance::AudioEffectStereoEnhance() {}