Debian Codegraph @

modules/interactive_music/audio_stream_interactive.h

raw symbols SCIP
1 /**************************************************************************/
2 /* audio_stream_interactive.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 #pragma once
32
33 #include "servers/audio/audio_stream.h"
34
35 class AudioStreamPlaybackInteractive;
36
37 class AudioStreamInteractive : public AudioStream {
38 GDCLASS(AudioStreamInteractive, AudioStream)
39 OBJ_SAVE_TYPE(AudioStream)
40 public:
41 enum TransitionFromTime {
42 TRANSITION_FROM_TIME_IMMEDIATE,
43 TRANSITION_FROM_TIME_NEXT_BEAT,
44 TRANSITION_FROM_TIME_NEXT_BAR,
45 TRANSITION_FROM_TIME_END,
46 TRANSITION_FROM_TIME_MAX
47 };
48
49 enum TransitionToTime {
50 TRANSITION_TO_TIME_SAME_POSITION,
51 TRANSITION_TO_TIME_START,
52 TRANSITION_TO_TIME_PREVIOUS_POSITION,
53 TRANSITION_TO_TIME_MAX,
54 };
55
56 enum FadeMode {
57 FADE_DISABLED,
58 FADE_IN,
59 FADE_OUT,
60 FADE_CROSS,
61 FADE_AUTOMATIC,
62 FADE_MAX
63 };
64
65 enum AutoAdvanceMode {
66 AUTO_ADVANCE_DISABLED,
67 AUTO_ADVANCE_ENABLED,
68 AUTO_ADVANCE_RETURN_TO_HOLD,
69 };
70
71 enum {
72 CLIP_ANY = -1
73 };
74
75 private:
76 friend class AudioStreamPlaybackInteractive;
77 int sample_rate = 44100;
78 bool stereo = true;
79 int initial_clip = 0;
80
81 double time = 0;
82
83 enum {
84 MAX_CLIPS = 63, // Because we use bitmasks for transition matching.
85 MAX_TRANSITIONS = 63,
86 };
87
88 struct Clip {
89 StringName name;
90 Ref<AudioStream> stream;
91
92 AutoAdvanceMode auto_advance = AUTO_ADVANCE_DISABLED;
93 int auto_advance_next_clip = 0;
94 };
95
96 Clip clips[MAX_CLIPS];
97
98 struct Transition {
99 TransitionFromTime from_time = TRANSITION_FROM_TIME_NEXT_BEAT;
100 TransitionToTime to_time = TRANSITION_TO_TIME_START;
101 FadeMode fade_mode = FADE_AUTOMATIC;
102 float fade_beats = 1;
103 bool use_filler_clip = false;
104 int filler_clip = 0;
105 bool hold_previous = false;
106 };
107
108 struct TransitionKey {
109 uint32_t from_clip;
110 uint32_t to_clip;
111 bool operator==(const TransitionKey &p_key) const {
112 return from_clip == p_key.from_clip && to_clip == p_key.to_clip;
113 }
114 TransitionKey(uint32_t p_from_clip = 0, uint32_t p_to_clip = 0) {
115 from_clip = p_from_clip;
116 to_clip = p_to_clip;
117 }
118 };
119
120 struct TransitionKeyHasher {
121 static _FORCE_INLINE_ uint32_t hash(const TransitionKey &p_key) {
122 uint32_t h = hash_murmur3_one_32(p_key.from_clip);
123 return hash_murmur3_one_32(p_key.to_clip, h);
124 }
125 };
126
127 HashMap<TransitionKey, Transition, TransitionKeyHasher> transition_map;
128
129 uint64_t version = 1; // Used to stop playback instances for incompatibility.
130 int clip_count = 0;
131
132 HashSet<AudioStreamPlaybackInteractive *> playbacks;
133
134 #ifdef TOOLS_ENABLED
135
136 mutable String stream_name_cache;
137 String _get_streams_hint() const;
138 PackedStringArray _get_linked_undo_properties(const String &p_property, const Variant &p_new_value) const;
139 void _inspector_array_swap_clip(uint32_t p_item_a, uint32_t p_item_b);
140
141 #endif
142
143 void _set_transitions(const Dictionary &p_transitions);
144 Dictionary _get_transitions() const;
145
146 public:
147 // CLIPS
148 void set_clip_count(int p_count);
149 int get_clip_count() const;
150
151 void set_initial_clip(int p_clip);
152 int get_initial_clip() const;
153
154 void set_clip_name(int p_clip, const StringName &p_name);
155 StringName get_clip_name(int p_clip) const;
156
157 void set_clip_stream(int p_clip, const Ref<AudioStream> &p_stream);
158 Ref<AudioStream> get_clip_stream(int p_clip) const;
159
160 void set_clip_auto_advance(int p_clip, AutoAdvanceMode p_mode);
161 AutoAdvanceMode get_clip_auto_advance(int p_clip) const;
162
163 void set_clip_auto_advance_next_clip(int p_clip, int p_index);
164 int get_clip_auto_advance_next_clip(int p_clip) const;
165
166 // TRANSITIONS
167
168 void add_transition(int p_from_clip, int p_to_clip, TransitionFromTime p_from_time, TransitionToTime p_to_time, FadeMode p_fade_mode, float p_fade_beats, bool p_use_filler_flip = false, int p_filler_clip = -1, bool p_hold_previous = false);
169 TransitionFromTime get_transition_from_time(int p_from_clip, int p_to_clip) const;
170 TransitionToTime get_transition_to_time(int p_from_clip, int p_to_clip) const;
171 FadeMode get_transition_fade_mode(int p_from_clip, int p_to_clip) const;
172 float get_transition_fade_beats(int p_from_clip, int p_to_clip) const;
173 bool is_transition_using_filler_clip(int p_from_clip, int p_to_clip) const;
174 int get_transition_filler_clip(int p_from_clip, int p_to_clip) const;
175 bool is_transition_holding_previous(int p_from_clip, int p_to_clip) const;
176
177 bool has_transition(int p_from_clip, int p_to_clip) const;
178 void erase_transition(int p_from_clip, int p_to_clip);
179
180 PackedInt32Array get_transition_list() const;
181
182 virtual Ref<AudioStreamPlayback> instantiate_playback() override;
183 virtual String get_stream_name() const override;
184 virtual double get_length() const override { return 0; }
185 virtual bool is_meta_stream() const override { return true; }
186
187 AudioStreamInteractive();
188
189 protected:
190 virtual void get_parameter_list(List<Parameter> *r_parameters) override;
191
192 static void _bind_methods();
193 void _validate_property(PropertyInfo &r_property) const;
194 };
195
196 VARIANT_ENUM_CAST(AudioStreamInteractive::TransitionFromTime)
197 VARIANT_ENUM_CAST(AudioStreamInteractive::TransitionToTime)
198 VARIANT_ENUM_CAST(AudioStreamInteractive::AutoAdvanceMode)
199 VARIANT_ENUM_CAST(AudioStreamInteractive::FadeMode)
200
201 class AudioStreamPlaybackInteractive : public AudioStreamPlayback {
202 GDCLASS(AudioStreamPlaybackInteractive, AudioStreamPlayback)
203 friend class AudioStreamInteractive;
204
205 private:
206 Ref<AudioStreamInteractive> stream;
207 uint64_t version = 0;
208
209 enum {
210 BUFFER_SIZE = 1024
211 };
212
213 AudioFrame mix_buffer[BUFFER_SIZE];
214 AudioFrame temp_buffer[BUFFER_SIZE];
215
216 struct State {
217 Ref<AudioStream> stream;
218 Ref<AudioStreamPlayback> playback;
219 bool active = false;
220 double fade_wait = 0; // Time to wait until fade kicks-in
221 double fade_volume = 1.0;
222 double fade_speed = 0; // Fade speed, negative or positive
223 int auto_advance = -1;
224 bool first_mix = true;
225 double previous_position = 0;
226
227 void reset_fade() {
228 fade_wait = 0;
229 fade_volume = 1.0;
230 fade_speed = 0;
231 }
232 };
233
234 State states[AudioStreamInteractive::MAX_CLIPS];
235 int playback_current = -1;
236
237 bool active = false;
238 int return_memory = -1;
239
240 void _mix_internal(int p_frames);
241 void _mix_internal_state(int p_state_idx, int p_frames);
242
243 void _queue(int p_to_clip_index, bool p_is_auto_advance);
244
245 int switch_request = -1;
246
247 protected:
248 static void _bind_methods();
249
250 public:
251 virtual void start(double p_from_pos = 0.0) override;
252 virtual void stop() override;
253 virtual bool is_playing() const override;
254 virtual int get_loop_count() const override; // times it looped
255 virtual double get_playback_position() const override;
256 virtual void seek(double p_time) override;
257 virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
258
259 virtual void tag_used_streams() override;
260
261 void switch_to_clip_by_name(const StringName &p_name);
262 void switch_to_clip(int p_index);
263 int get_current_clip_index() const;
264
265 virtual void set_parameter(const StringName &p_name, const Variant &p_value) override;
266 virtual Variant get_parameter(const StringName &p_name) const override;
267
268 AudioStreamPlaybackInteractive();
269 ~AudioStreamPlaybackInteractive();
270 };