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) 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 | |
31 | #ifndef AUDIO_EFFECT_SPECTRUM_ANALYZER_H |
32 | #define AUDIO_EFFECT_SPECTRUM_ANALYZER_H |
33 | |
34 | #include "servers/audio/audio_effect.h" |
35 | |
36 | class AudioEffectSpectrumAnalyzer; |
37 | |
38 | class AudioEffectSpectrumAnalyzerInstance : public AudioEffectInstance { |
39 | GDCLASS(AudioEffectSpectrumAnalyzerInstance, AudioEffectInstance); |
40 | |
41 | public: |
42 | enum MagnitudeMode { |
43 | MAGNITUDE_AVERAGE, |
44 | MAGNITUDE_MAX, |
45 | }; |
46 | |
47 | private: |
48 | friend class AudioEffectSpectrumAnalyzer; |
49 | Ref<AudioEffectSpectrumAnalyzer> base; |
50 | |
51 | Vector<Vector<AudioFrame>> fft_history; |
52 | Vector<float> temporal_fft; |
53 | int temporal_fft_pos; |
54 | int fft_size; |
55 | int fft_count; |
56 | int fft_pos; |
57 | float mix_rate; |
58 | uint64_t last_fft_time; |
59 | |
60 | protected: |
61 | static void _bind_methods(); |
62 | |
63 | public: |
64 | virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) override; |
65 | Vector2 get_magnitude_for_frequency_range(float p_begin, float p_end, MagnitudeMode p_mode = MAGNITUDE_MAX) const; |
66 | }; |
67 | |
68 | VARIANT_ENUM_CAST(AudioEffectSpectrumAnalyzerInstance::MagnitudeMode) |
69 | |
70 | class AudioEffectSpectrumAnalyzer : public AudioEffect { |
71 | GDCLASS(AudioEffectSpectrumAnalyzer, AudioEffect); |
72 | |
73 | public: |
74 | enum FFTSize { |
75 | FFT_SIZE_256, |
76 | FFT_SIZE_512, |
77 | FFT_SIZE_1024, |
78 | FFT_SIZE_2048, |
79 | FFT_SIZE_4096, |
80 | FFT_SIZE_MAX |
81 | }; |
82 | |
83 | public: |
84 | friend class AudioEffectSpectrumAnalyzerInstance; |
85 | float buffer_length; |
86 | float tapback_pos; |
87 | FFTSize fft_size; |
88 | |
89 | protected: |
90 | static void _bind_methods(); |
91 | |
92 | public: |
93 | Ref<AudioEffectInstance> instantiate() override; |
94 | void set_buffer_length(float p_seconds); |
95 | float get_buffer_length() const; |
96 | void set_tap_back_pos(float p_seconds); |
97 | float get_tap_back_pos() const; |
98 | |
99 | void set_fft_size(FFTSize); |
100 | FFTSize get_fft_size() const; |
101 | |
102 | AudioEffectSpectrumAnalyzer(); |
103 | }; |
104 | |
105 | VARIANT_ENUM_CAST(AudioEffectSpectrumAnalyzer::FFTSize); |
106 | |
107 | #endif // AUDIO_EFFECT_SPECTRUM_ANALYZER_H |
108 | |