Debian Codegraph @

servers/audio/audio_stream.h

raw symbols SCIP
1 /**************************************************************************/
2 /* audio_stream.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 "core/io/resource.h"
34 #include "scene/property_list_helper.h"
35 #include "servers/audio/audio_server.h"
36
37 #include "core/object/gdvirtual.gen.inc"
38 #include "core/variant/native_ptr.h"
39 #include "core/variant/typed_array.h"
40
41 class AudioStream;
42
43 class AudioSamplePlayback : public RefCounted {
44 GDCLASS(AudioSamplePlayback, RefCounted);
45
46 public:
47 Ref<AudioStream> stream;
48 Ref<AudioStreamPlayback> stream_playback;
49
50 float offset = 0.0f;
51 float pitch_scale = 1.0;
52 Vector<AudioFrame> volume_vector;
53 StringName bus;
54 };
55
56 class AudioSample : public RefCounted {
57 GDCLASS(AudioSample, RefCounted)
58
59 public:
60 enum LoopMode {
61 LOOP_DISABLED,
62 LOOP_FORWARD,
63 LOOP_PINGPONG,
64 LOOP_BACKWARD,
65 };
66
67 Ref<AudioStream> stream;
68 Vector<AudioFrame> data;
69 int num_channels = 1;
70 int sample_rate = 44100;
71 LoopMode loop_mode = LOOP_DISABLED;
72 int loop_begin = 0;
73 int loop_end = 0;
74 };
75
76 ///////////
77
78 class AudioStreamPlayback : public RefCounted {
79 GDCLASS(AudioStreamPlayback, RefCounted);
80
81 protected:
82 static void _bind_methods();
83 PackedVector2Array _mix_audio_bind(float p_rate_scale, int p_frames);
84 GDVIRTUAL1_REQUIRED(_start, double)
85 GDVIRTUAL0_REQUIRED(_stop)
86 GDVIRTUAL0RC_REQUIRED(bool, _is_playing)
87 GDVIRTUAL0RC(int, _get_loop_count)
88 GDVIRTUAL0RC_REQUIRED(double, _get_playback_position)
89 GDVIRTUAL1(_seek, double)
90 GDVIRTUAL3R_REQUIRED(int, _mix, GDExtensionPtr<AudioFrame>, float, int)
91 GDVIRTUAL0(_tag_used_streams)
92 GDVIRTUAL2(_set_parameter, const StringName &, const Variant &)
93 GDVIRTUAL1RC(Variant, _get_parameter, const StringName &)
94
95 public:
96 virtual void start(double p_from_pos = 0.0);
97 virtual void stop();
98 virtual bool is_playing() const;
99
100 virtual int get_loop_count() const; //times it looped
101
102 virtual double get_playback_position() const;
103 virtual void seek(double p_time);
104
105 virtual void tag_used_streams();
106
107 virtual void set_parameter(const StringName &p_name, const Variant &p_value);
108 virtual Variant get_parameter(const StringName &p_name) const;
109
110 virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
111
112 virtual void set_is_sample(bool p_is_sample) {}
113 virtual bool get_is_sample() const { return false; }
114 virtual Ref<AudioSamplePlayback> get_sample_playback() const;
115 virtual void set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {}
116
117 AudioStreamPlayback();
118 ~AudioStreamPlayback();
119
120 Vector<AudioFrame> mix_audio(float p_rate_scale, int p_frames);
121 void start_playback(double p_from_pos = 0.0);
122 void stop_playback();
123 void seek_playback(double p_time);
124 };
125
126 class AudioStreamPlaybackResampled : public AudioStreamPlayback {
127 GDCLASS(AudioStreamPlaybackResampled, AudioStreamPlayback);
128
129 enum {
130 FP_BITS = 16, //fixed point used for resampling
131 FP_LEN = (1 << FP_BITS),
132 FP_MASK = FP_LEN - 1,
133 INTERNAL_BUFFER_LEN = 128, // 128 warrants 3ms positional jitter at much at 44100hz
134 CUBIC_INTERP_HISTORY = 4
135 };
136
137 AudioFrame internal_buffer[INTERNAL_BUFFER_LEN + CUBIC_INTERP_HISTORY];
138 unsigned int internal_buffer_end = -1;
139 uint64_t mix_offset = 0;
140
141 protected:
142 void begin_resample();
143 // Returns the number of frames that were mixed.
144 virtual int _mix_internal(AudioFrame *p_buffer, int p_frames);
145 virtual float get_stream_sampling_rate();
146
147 GDVIRTUAL2R_REQUIRED(int, _mix_resampled, GDExtensionPtr<AudioFrame>, int)
148 GDVIRTUAL0RC_REQUIRED(float, _get_stream_sampling_rate)
149
150 static void _bind_methods();
151
152 public:
153 virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
154
155 AudioStreamPlaybackResampled() { mix_offset = 0; }
156 };
157
158 class AudioStream : public Resource {
159 GDCLASS(AudioStream, Resource);
160 OBJ_SAVE_TYPE(AudioStream); // Saves derived classes with common type so they can be interchanged.
161
162 enum {
163 MAX_TAGGED_OFFSETS = 8
164 };
165
166 uint64_t tagged_frame = 0;
167 uint64_t offset_count = 0;
168 float tagged_offsets[MAX_TAGGED_OFFSETS];
169
170 protected:
171 static void _bind_methods();
172
173 GDVIRTUAL0RC_REQUIRED(Ref<AudioStreamPlayback>, _instantiate_playback)
174 GDVIRTUAL0RC(String, _get_stream_name)
175 GDVIRTUAL0RC(double, _get_length)
176 GDVIRTUAL0RC(bool, _is_monophonic)
177 GDVIRTUAL0RC(double, _get_bpm)
178 GDVIRTUAL0RC(bool, _has_loop)
179 GDVIRTUAL0RC(int, _get_bar_beats)
180 GDVIRTUAL0RC(int, _get_beat_count)
181 GDVIRTUAL0RC(Dictionary, _get_tags);
182 GDVIRTUAL0RC(TypedArray<Dictionary>, _get_parameter_list)
183
184 public:
185 virtual Ref<AudioStreamPlayback> instantiate_playback();
186 virtual String get_stream_name() const;
187
188 virtual double get_bpm() const;
189 virtual bool has_loop() const;
190 virtual int get_bar_beats() const;
191 virtual int get_beat_count() const;
192 virtual Dictionary get_tags() const;
193
194 virtual double get_length() const;
195 virtual bool is_monophonic() const;
196
197 void tag_used(float p_offset);
198 uint64_t get_tagged_frame() const;
199 uint32_t get_tagged_frame_count() const;
200 float get_tagged_frame_offset(int p_index) const;
201
202 struct Parameter {
203 PropertyInfo property;
204 Variant default_value;
205 Parameter(const PropertyInfo &p_info = PropertyInfo(), const Variant &p_default_value = Variant()) {
206 property = p_info;
207 default_value = p_default_value;
208 }
209 };
210
211 virtual void get_parameter_list(List<Parameter> *r_parameters);
212
213 virtual bool can_be_sampled() const { return false; }
214 virtual Ref<AudioSample> generate_sample() const;
215
216 virtual bool is_meta_stream() const { return false; }
217 };
218
219 // Microphone
220
221 class AudioStreamPlaybackMicrophone;
222
223 class AudioStreamMicrophone : public AudioStream {
224 GDCLASS(AudioStreamMicrophone, AudioStream);
225 friend class AudioStreamPlaybackMicrophone;
226
227 HashSet<AudioStreamPlaybackMicrophone *> playbacks;
228
229 public:
230 virtual Ref<AudioStreamPlayback> instantiate_playback() override;
231 virtual String get_stream_name() const override;
232
233 virtual double get_length() const override; //if supported, otherwise return 0
234
235 virtual bool is_monophonic() const override;
236 };
237
238 class AudioStreamPlaybackMicrophone : public AudioStreamPlaybackResampled {
239 GDCLASS(AudioStreamPlaybackMicrophone, AudioStreamPlaybackResampled);
240 friend class AudioStreamMicrophone;
241
242 bool active = false;
243 unsigned int input_ofs = 0;
244
245 Ref<AudioStreamMicrophone> microphone;
246
247 protected:
248 virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override;
249 virtual float get_stream_sampling_rate() override;
250 virtual double get_playback_position() const override;
251
252 public:
253 virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
254
255 virtual void start(double p_from_pos = 0.0) override;
256 virtual void stop() override;
257 virtual bool is_playing() const override;
258
259 virtual int get_loop_count() const override; //times it looped
260
261 virtual void seek(double p_time) override;
262
263 virtual void tag_used_streams() override;
264
265 ~AudioStreamPlaybackMicrophone();
266 AudioStreamPlaybackMicrophone();
267 };
268
269 //
270
271 class AudioStreamPlaybackRandomizer;
272
273 class AudioStreamRandomizer : public AudioStream {
274 GDCLASS(AudioStreamRandomizer, AudioStream);
275
276 public:
277 enum PlaybackMode {
278 PLAYBACK_RANDOM_NO_REPEATS,
279 PLAYBACK_RANDOM,
280 PLAYBACK_SEQUENTIAL,
281 };
282
283 private:
284 friend class AudioStreamPlaybackRandomizer;
285
286 struct PoolEntry {
287 Ref<AudioStream> stream;
288 float weight = 1.0;
289 };
290
291 static inline PropertyListHelper base_property_helper;
292 PropertyListHelper property_helper;
293
294 HashSet<AudioStreamPlaybackRandomizer *> playbacks;
295 Vector<PoolEntry> audio_stream_pool;
296 float random_pitch_scale = 1.0f;
297 float random_volume_offset_db = 0.0f;
298
299 Ref<AudioStreamPlayback> instance_playback_random();
300 Ref<AudioStreamPlayback> instance_playback_no_repeats();
301 Ref<AudioStreamPlayback> instance_playback_sequential();
302
303 Ref<AudioStream> last_playback = nullptr;
304 PlaybackMode playback_mode = PLAYBACK_RANDOM_NO_REPEATS;
305
306 protected:
307 static void _bind_methods();
308
309 bool _set(const StringName &p_name, const Variant &p_value) { return property_helper.property_set_value(p_name, p_value); }
310 bool _get(const StringName &p_name, Variant &r_ret) const { return property_helper.property_get_value(p_name, r_ret); }
311 void _get_property_list(List<PropertyInfo> *p_list) const { property_helper.get_property_list(p_list); }
312 bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); }
313 bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
314
315 public:
316 void add_stream(int p_index, Ref<AudioStream> p_stream, float p_weight = 1.0);
317 void move_stream(int p_index_from, int p_index_to);
318 void remove_stream(int p_index);
319
320 void set_stream(int p_index, Ref<AudioStream> p_stream);
321 Ref<AudioStream> get_stream(int p_index) const;
322 void set_stream_probability_weight(int p_index, float p_weight);
323 float get_stream_probability_weight(int p_index) const;
324
325 void set_streams_count(int p_count);
326 int get_streams_count() const;
327
328 void set_random_pitch_semitones(float p_pitch_semitones);
329 float get_random_pitch_semitones() const;
330
331 void set_random_pitch(float p_pitch_scale);
332 float get_random_pitch() const;
333
334 void set_random_volume_offset_db(float p_volume_offset_db);
335 float get_random_volume_offset_db() const;
336
337 void set_playback_mode(PlaybackMode p_playback_mode);
338 PlaybackMode get_playback_mode() const;
339
340 virtual Ref<AudioStreamPlayback> instantiate_playback() override;
341 virtual String get_stream_name() const override;
342
343 virtual double get_length() const override; //if supported, otherwise return 0
344 virtual bool is_monophonic() const override;
345
346 virtual bool is_meta_stream() const override { return true; }
347
348 AudioStreamRandomizer();
349 };
350
351 class AudioStreamPlaybackRandomizer : public AudioStreamPlayback {
352 GDCLASS(AudioStreamPlaybackRandomizer, AudioStreamPlayback);
353 friend class AudioStreamRandomizer;
354
355 Ref<AudioStreamRandomizer> randomizer;
356 Ref<AudioStreamPlayback> playback;
357 Ref<AudioStreamPlayback> playing;
358
359 float pitch_scale;
360 float volume_scale;
361
362 public:
363 virtual void start(double p_from_pos = 0.0) override;
364 virtual void stop() override;
365 virtual bool is_playing() const override;
366
367 virtual int get_loop_count() const override; //times it looped
368
369 virtual double get_playback_position() const override;
370 virtual void seek(double p_time) override;
371
372 virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
373
374 virtual void tag_used_streams() override;
375
376 ~AudioStreamPlaybackRandomizer();
377 };
378
379 VARIANT_ENUM_CAST(AudioStreamRandomizer::PlaybackMode);