audio_effect_eq.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**************************************************************************/
  2. /* audio_effect_eq.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_eq.h"
  31. #include "servers/audio_server.h"
  32. void AudioEffectEQInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
  33. int band_count = bands[0].size();
  34. EQ::BandProcess *proc_l = bands[0].ptrw();
  35. EQ::BandProcess *proc_r = bands[1].ptrw();
  36. float *bgain = gains.ptrw();
  37. for (int i = 0; i < band_count; i++) {
  38. bgain[i] = Math::db_to_linear(base->gain[i]);
  39. }
  40. for (int i = 0; i < p_frame_count; i++) {
  41. AudioFrame src = p_src_frames[i];
  42. AudioFrame dst = AudioFrame(0, 0);
  43. for (int j = 0; j < band_count; j++) {
  44. float l = src.l;
  45. float r = src.r;
  46. proc_l[j].process_one(l);
  47. proc_r[j].process_one(r);
  48. dst.l += l * bgain[j];
  49. dst.r += r * bgain[j];
  50. }
  51. p_dst_frames[i] = dst;
  52. }
  53. }
  54. Ref<AudioEffectInstance> AudioEffectEQ::instantiate() {
  55. Ref<AudioEffectEQInstance> ins;
  56. ins.instantiate();
  57. ins->base = Ref<AudioEffectEQ>(this);
  58. ins->gains.resize(eq.get_band_count());
  59. for (int i = 0; i < 2; i++) {
  60. ins->bands[i].resize(eq.get_band_count());
  61. for (int j = 0; j < ins->bands[i].size(); j++) {
  62. ins->bands[i].write[j] = eq.get_band_processor(j);
  63. }
  64. }
  65. return ins;
  66. }
  67. void AudioEffectEQ::set_band_gain_db(int p_band, float p_volume) {
  68. ERR_FAIL_INDEX(p_band, gain.size());
  69. gain.write[p_band] = p_volume;
  70. }
  71. float AudioEffectEQ::get_band_gain_db(int p_band) const {
  72. ERR_FAIL_INDEX_V(p_band, gain.size(), 0);
  73. return gain[p_band];
  74. }
  75. int AudioEffectEQ::get_band_count() const {
  76. return gain.size();
  77. }
  78. bool AudioEffectEQ::_set(const StringName &p_name, const Variant &p_value) {
  79. HashMap<StringName, int>::ConstIterator E = prop_band_map.find(p_name);
  80. if (E) {
  81. set_band_gain_db(E->value, p_value);
  82. return true;
  83. }
  84. return false;
  85. }
  86. bool AudioEffectEQ::_get(const StringName &p_name, Variant &r_ret) const {
  87. HashMap<StringName, int>::ConstIterator E = prop_band_map.find(p_name);
  88. if (E) {
  89. r_ret = get_band_gain_db(E->value);
  90. return true;
  91. }
  92. return false;
  93. }
  94. void AudioEffectEQ::_get_property_list(List<PropertyInfo> *p_list) const {
  95. for (int i = 0; i < band_names.size(); i++) {
  96. p_list->push_back(PropertyInfo(Variant::FLOAT, band_names[i], PROPERTY_HINT_RANGE, "-60,24,0.1"));
  97. }
  98. }
  99. void AudioEffectEQ::_bind_methods() {
  100. ClassDB::bind_method(D_METHOD("set_band_gain_db", "band_idx", "volume_db"), &AudioEffectEQ::set_band_gain_db);
  101. ClassDB::bind_method(D_METHOD("get_band_gain_db", "band_idx"), &AudioEffectEQ::get_band_gain_db);
  102. ClassDB::bind_method(D_METHOD("get_band_count"), &AudioEffectEQ::get_band_count);
  103. }
  104. AudioEffectEQ::AudioEffectEQ(EQ::Preset p_preset) {
  105. eq.set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
  106. eq.set_preset_band_mode(p_preset);
  107. gain.resize(eq.get_band_count());
  108. for (int i = 0; i < gain.size(); i++) {
  109. gain.write[i] = 0.0;
  110. String band_name = "band_db/" + itos(eq.get_band_frequency(i)) + "_hz";
  111. prop_band_map[band_name] = i;
  112. band_names.push_back(band_name);
  113. }
  114. }