audio_effect_chorus.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. enum {
  51. MAX_DELAY_MS = 50,
  52. MAX_DEPTH_MS = 20,
  53. MAX_WIDTH_MS = 50,
  54. MAX_VOICES = 4,
  55. CYCLES_FRAC = 16,
  56. CYCLES_MASK = (1 << CYCLES_FRAC) - 1,
  57. MAX_CHANNELS = 4,
  58. MS_CUTOFF_MAX = 16000
  59. };
  60. private:
  61. struct Voice {
  62. float delay;
  63. float rate;
  64. float depth;
  65. float level;
  66. float cutoff;
  67. float pan;
  68. Voice() {
  69. delay = 12.0;
  70. rate = 1;
  71. depth = 0;
  72. level = 0;
  73. cutoff = MS_CUTOFF_MAX;
  74. pan = 0;
  75. }
  76. } voice[MAX_VOICES];
  77. int voice_count;
  78. float wet;
  79. float dry;
  80. protected:
  81. void _validate_property(PropertyInfo &p_property) const;
  82. static void _bind_methods();
  83. public:
  84. void set_voice_count(int p_voices);
  85. int get_voice_count() const;
  86. void set_voice_delay_ms(int p_voice, float p_delay_ms);
  87. float get_voice_delay_ms(int p_voice) const;
  88. void set_voice_rate_hz(int p_voice, float p_rate_hz);
  89. float get_voice_rate_hz(int p_voice) const;
  90. void set_voice_depth_ms(int p_voice, float p_depth_ms);
  91. float get_voice_depth_ms(int p_voice) const;
  92. void set_voice_level_db(int p_voice, float p_level_db);
  93. float get_voice_level_db(int p_voice) const;
  94. void set_voice_cutoff_hz(int p_voice, float p_cutoff_hz);
  95. float get_voice_cutoff_hz(int p_voice) const;
  96. void set_voice_pan(int p_voice, float p_pan);
  97. float get_voice_pan(int p_voice) const;
  98. void set_wet(float amount);
  99. float get_wet() const;
  100. void set_dry(float amount);
  101. float get_dry() const;
  102. Ref<AudioEffectInstance> instantiate() override;
  103. AudioEffectChorus();
  104. };