2
0

audio_effect_chorus.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**************************************************************************/
  2. /* audio_effect_chorus.h */
  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. #pragma once
  31. #include "servers/audio/audio_effect.h"
  32. class AudioEffectChorus;
  33. class AudioEffectChorusInstance : public AudioEffectInstance {
  34. GDCLASS(AudioEffectChorusInstance, AudioEffectInstance);
  35. friend class AudioEffectChorus;
  36. Ref<AudioEffectChorus> base;
  37. Vector<AudioFrame> audio_buffer;
  38. unsigned int buffer_pos;
  39. unsigned int buffer_mask;
  40. AudioFrame filter_h[4];
  41. uint64_t cycles[4];
  42. void _process_chunk(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count);
  43. public:
  44. virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) override;
  45. };
  46. class AudioEffectChorus : public AudioEffect {
  47. GDCLASS(AudioEffectChorus, AudioEffect);
  48. friend class AudioEffectChorusInstance;
  49. public:
  50. static constexpr int32_t MAX_DELAY_MS = 50;
  51. static constexpr int32_t MAX_DEPTH_MS = 20;
  52. static constexpr int32_t MAX_WIDTH_MS = 50;
  53. static constexpr int32_t MAX_VOICES = 4;
  54. static constexpr int32_t CYCLES_FRAC = 16;
  55. static constexpr int32_t CYCLES_MASK = (1 << CYCLES_FRAC) - 1;
  56. static constexpr int32_t MAX_CHANNELS = 4;
  57. static constexpr int32_t MS_CUTOFF_MAX = 16000;
  58. private:
  59. struct Voice {
  60. float delay;
  61. float rate;
  62. float depth;
  63. float level;
  64. float cutoff;
  65. float pan;
  66. Voice() {
  67. delay = 12.0;
  68. rate = 1;
  69. depth = 0;
  70. level = 0;
  71. cutoff = MS_CUTOFF_MAX;
  72. pan = 0;
  73. }
  74. } voice[MAX_VOICES];
  75. int voice_count;
  76. float wet;
  77. float dry;
  78. protected:
  79. void _validate_property(PropertyInfo &p_property) const;
  80. static void _bind_methods();
  81. public:
  82. void set_voice_count(int p_voices);
  83. int get_voice_count() const;
  84. void set_voice_delay_ms(int p_voice, float p_delay_ms);
  85. float get_voice_delay_ms(int p_voice) const;
  86. void set_voice_rate_hz(int p_voice, float p_rate_hz);
  87. float get_voice_rate_hz(int p_voice) const;
  88. void set_voice_depth_ms(int p_voice, float p_depth_ms);
  89. float get_voice_depth_ms(int p_voice) const;
  90. void set_voice_level_db(int p_voice, float p_level_db);
  91. float get_voice_level_db(int p_voice) const;
  92. void set_voice_cutoff_hz(int p_voice, float p_cutoff_hz);
  93. float get_voice_cutoff_hz(int p_voice) const;
  94. void set_voice_pan(int p_voice, float p_pan);
  95. float get_voice_pan(int p_voice) const;
  96. void set_wet(float amount);
  97. float get_wet() const;
  98. void set_dry(float amount);
  99. float get_dry() const;
  100. Ref<AudioEffectInstance> instantiate() override;
  101. AudioEffectChorus();
  102. };