1 | /**************************************************************************/ |
2 | /* audio_stream_player_3d.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_STREAM_PLAYER_3D_H |
32 | #define AUDIO_STREAM_PLAYER_3D_H |
33 | |
34 | #include "core/os/mutex.h" |
35 | #include "scene/3d/area_3d.h" |
36 | #include "scene/3d/node_3d.h" |
37 | #include "scene/3d/velocity_tracker_3d.h" |
38 | #include "servers/audio/audio_filter_sw.h" |
39 | #include "servers/audio/audio_stream.h" |
40 | #include "servers/audio_server.h" |
41 | |
42 | class Camera3D; |
43 | class AudioStreamPlayer3D : public Node3D { |
44 | GDCLASS(AudioStreamPlayer3D, Node3D); |
45 | |
46 | public: |
47 | enum AttenuationModel { |
48 | ATTENUATION_INVERSE_DISTANCE, |
49 | ATTENUATION_INVERSE_SQUARE_DISTANCE, |
50 | ATTENUATION_LOGARITHMIC, |
51 | ATTENUATION_DISABLED, |
52 | }; |
53 | |
54 | enum DopplerTracking { |
55 | DOPPLER_TRACKING_DISABLED, |
56 | DOPPLER_TRACKING_IDLE_STEP, |
57 | DOPPLER_TRACKING_PHYSICS_STEP |
58 | }; |
59 | |
60 | private: |
61 | enum { |
62 | MAX_OUTPUTS = 8, |
63 | MAX_INTERSECT_AREAS = 32 |
64 | |
65 | }; |
66 | |
67 | Vector<Ref<AudioStreamPlayback>> stream_playbacks; |
68 | Ref<AudioStream> stream; |
69 | |
70 | SafeFlag active{ false }; |
71 | SafeNumeric<float> setplay{ -1.0 }; |
72 | Ref<AudioStreamPlayback> setplayback; |
73 | |
74 | AttenuationModel attenuation_model = ATTENUATION_INVERSE_DISTANCE; |
75 | float volume_db = 0.0; |
76 | float unit_size = 10.0; |
77 | float max_db = 3.0; |
78 | float pitch_scale = 1.0; |
79 | // Internally used to take doppler tracking into account. |
80 | float actual_pitch_scale = 1.0; |
81 | bool autoplay = false; |
82 | StringName bus = SNAME("Master" ); |
83 | int max_polyphony = 1; |
84 | |
85 | uint64_t last_mix_count = -1; |
86 | bool force_update_panning = false; |
87 | |
88 | static void _calc_output_vol(const Vector3 &source_dir, real_t tightness, Vector<AudioFrame> &output); |
89 | |
90 | void _calc_reverb_vol(Area3D *area, Vector3 listener_area_pos, Vector<AudioFrame> direct_path_vol, Vector<AudioFrame> &reverb_vol); |
91 | |
92 | static void _listener_changed_cb(void *self) { reinterpret_cast<AudioStreamPlayer3D *>(self)->force_update_panning = true; } |
93 | |
94 | void _set_playing(bool p_enable); |
95 | bool _is_active() const; |
96 | StringName _get_actual_bus(); |
97 | Area3D *_get_overriding_area(); |
98 | Vector<AudioFrame> _update_panning(); |
99 | |
100 | void _bus_layout_changed(); |
101 | |
102 | uint32_t area_mask = 1; |
103 | |
104 | bool emission_angle_enabled = false; |
105 | float emission_angle = 45.0; |
106 | float emission_angle_filter_attenuation_db = -12.0; |
107 | float attenuation_filter_cutoff_hz = 5000.0; |
108 | float attenuation_filter_db = -24.0; |
109 | |
110 | float linear_attenuation = 0; |
111 | |
112 | float max_distance = 0.0; |
113 | |
114 | Ref<VelocityTracker3D> velocity_tracker; |
115 | |
116 | DopplerTracking doppler_tracking = DOPPLER_TRACKING_DISABLED; |
117 | |
118 | float _get_attenuation_db(float p_distance) const; |
119 | |
120 | float panning_strength = 1.0f; |
121 | float cached_global_panning_strength = 0.5f; |
122 | |
123 | protected: |
124 | void _validate_property(PropertyInfo &p_property) const; |
125 | void _notification(int p_what); |
126 | static void _bind_methods(); |
127 | |
128 | public: |
129 | void set_stream(Ref<AudioStream> p_stream); |
130 | Ref<AudioStream> get_stream() const; |
131 | |
132 | void set_volume_db(float p_volume); |
133 | float get_volume_db() const; |
134 | |
135 | void set_unit_size(float p_volume); |
136 | float get_unit_size() const; |
137 | |
138 | void set_max_db(float p_boost); |
139 | float get_max_db() const; |
140 | |
141 | void set_pitch_scale(float p_pitch_scale); |
142 | float get_pitch_scale() const; |
143 | |
144 | void play(float p_from_pos = 0.0); |
145 | void seek(float p_seconds); |
146 | void stop(); |
147 | bool is_playing() const; |
148 | float get_playback_position(); |
149 | |
150 | void set_bus(const StringName &p_bus); |
151 | StringName get_bus() const; |
152 | |
153 | void set_max_polyphony(int p_max_polyphony); |
154 | int get_max_polyphony() const; |
155 | |
156 | void set_autoplay(bool p_enable); |
157 | bool is_autoplay_enabled(); |
158 | |
159 | void set_max_distance(float p_metres); |
160 | float get_max_distance() const; |
161 | |
162 | void set_area_mask(uint32_t p_mask); |
163 | uint32_t get_area_mask() const; |
164 | |
165 | void set_emission_angle_enabled(bool p_enable); |
166 | bool is_emission_angle_enabled() const; |
167 | |
168 | void set_emission_angle(float p_angle); |
169 | float get_emission_angle() const; |
170 | |
171 | void set_emission_angle_filter_attenuation_db(float p_angle_attenuation_db); |
172 | float get_emission_angle_filter_attenuation_db() const; |
173 | |
174 | void set_attenuation_filter_cutoff_hz(float p_hz); |
175 | float get_attenuation_filter_cutoff_hz() const; |
176 | |
177 | void set_attenuation_filter_db(float p_db); |
178 | float get_attenuation_filter_db() const; |
179 | |
180 | void set_attenuation_model(AttenuationModel p_model); |
181 | AttenuationModel get_attenuation_model() const; |
182 | |
183 | void set_doppler_tracking(DopplerTracking p_tracking); |
184 | DopplerTracking get_doppler_tracking() const; |
185 | |
186 | void set_stream_paused(bool p_pause); |
187 | bool get_stream_paused() const; |
188 | |
189 | void set_panning_strength(float p_panning_strength); |
190 | float get_panning_strength() const; |
191 | |
192 | bool has_stream_playback(); |
193 | Ref<AudioStreamPlayback> get_stream_playback(); |
194 | |
195 | AudioStreamPlayer3D(); |
196 | ~AudioStreamPlayer3D(); |
197 | }; |
198 | |
199 | VARIANT_ENUM_CAST(AudioStreamPlayer3D::AttenuationModel) |
200 | VARIANT_ENUM_CAST(AudioStreamPlayer3D::DopplerTracking) |
201 | |
202 | #endif // AUDIO_STREAM_PLAYER_3D_H |
203 | |