audio_effect_spectrum_analyzer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*************************************************************************/
  2. /* audio_effect_spectrum_analyzer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef AUDIO_EFFECT_SPECTRUM_ANALYZER_H
  31. #define AUDIO_EFFECT_SPECTRUM_ANALYZER_H
  32. #include "servers/audio/audio_effect.h"
  33. class AudioEffectSpectrumAnalyzer;
  34. class AudioEffectSpectrumAnalyzerInstance : public AudioEffectInstance {
  35. GDCLASS(AudioEffectSpectrumAnalyzerInstance, AudioEffectInstance);
  36. public:
  37. enum MagnitudeMode {
  38. MAGNITUDE_AVERAGE,
  39. MAGNITUDE_MAX,
  40. };
  41. private:
  42. friend class AudioEffectSpectrumAnalyzer;
  43. Ref<AudioEffectSpectrumAnalyzer> base;
  44. Vector<Vector<AudioFrame> > fft_history;
  45. Vector<float> temporal_fft;
  46. int temporal_fft_pos;
  47. int fft_size;
  48. int fft_count;
  49. int fft_pos;
  50. float mix_rate;
  51. uint64_t last_fft_time;
  52. protected:
  53. static void _bind_methods();
  54. public:
  55. virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count);
  56. Vector2 get_magnitude_for_frequency_range(float p_begin, float p_end, MagnitudeMode p_mode = MAGNITUDE_MAX) const;
  57. };
  58. VARIANT_ENUM_CAST(AudioEffectSpectrumAnalyzerInstance::MagnitudeMode)
  59. class AudioEffectSpectrumAnalyzer : public AudioEffect {
  60. GDCLASS(AudioEffectSpectrumAnalyzer, AudioEffect);
  61. public:
  62. enum FFT_Size {
  63. FFT_SIZE_256,
  64. FFT_SIZE_512,
  65. FFT_SIZE_1024,
  66. FFT_SIZE_2048,
  67. FFT_SIZE_4096,
  68. FFT_SIZE_MAX
  69. };
  70. public:
  71. friend class AudioEffectSpectrumAnalyzerInstance;
  72. float buffer_length;
  73. float tapback_pos;
  74. FFT_Size fft_size;
  75. protected:
  76. static void _bind_methods();
  77. public:
  78. Ref<AudioEffectInstance> instance();
  79. void set_buffer_length(float p_seconds);
  80. float get_buffer_length() const;
  81. void set_tap_back_pos(float p_seconds);
  82. float get_tap_back_pos() const;
  83. void set_fft_size(FFT_Size);
  84. FFT_Size get_fft_size() const;
  85. AudioEffectSpectrumAnalyzer();
  86. };
  87. VARIANT_ENUM_CAST(AudioEffectSpectrumAnalyzer::FFT_Size);
  88. #endif // AUDIO_EFFECT_SPECTRUM_ANALYZER_H