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