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 | #ifndef AUDIO_STREAM_H |
32 | #define AUDIO_STREAM_H |
33 | |
34 | #include "core/io/image.h" |
35 | #include "core/io/resource.h" |
36 | #include "servers/audio/audio_filter_sw.h" |
37 | #include "servers/audio_server.h" |
38 | |
39 | #include "core/object/gdvirtual.gen.inc" |
40 | #include "core/variant/native_ptr.h" |
41 | |
42 | class AudioStream; |
43 | |
44 | class AudioStreamPlayback : public RefCounted { |
45 | GDCLASS(AudioStreamPlayback, RefCounted); |
46 | |
47 | protected: |
48 | static void _bind_methods(); |
49 | GDVIRTUAL1(_start, double) |
50 | GDVIRTUAL0(_stop) |
51 | GDVIRTUAL0RC(bool, _is_playing) |
52 | GDVIRTUAL0RC(int, _get_loop_count) |
53 | GDVIRTUAL0RC(double, _get_playback_position) |
54 | GDVIRTUAL1(_seek, double) |
55 | GDVIRTUAL3R(int, _mix, GDExtensionPtr<AudioFrame>, float, int) |
56 | GDVIRTUAL0(_tag_used_streams) |
57 | public: |
58 | virtual void start(double p_from_pos = 0.0); |
59 | virtual void stop(); |
60 | virtual bool is_playing() const; |
61 | |
62 | virtual int get_loop_count() const; //times it looped |
63 | |
64 | virtual double get_playback_position() const; |
65 | virtual void seek(double p_time); |
66 | |
67 | virtual void tag_used_streams(); |
68 | |
69 | virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames); |
70 | }; |
71 | |
72 | class AudioStreamPlaybackResampled : public AudioStreamPlayback { |
73 | GDCLASS(AudioStreamPlaybackResampled, AudioStreamPlayback); |
74 | |
75 | enum { |
76 | FP_BITS = 16, //fixed point used for resampling |
77 | FP_LEN = (1 << FP_BITS), |
78 | FP_MASK = FP_LEN - 1, |
79 | INTERNAL_BUFFER_LEN = 128, // 128 warrants 3ms positional jitter at much at 44100hz |
80 | CUBIC_INTERP_HISTORY = 4 |
81 | }; |
82 | |
83 | AudioFrame internal_buffer[INTERNAL_BUFFER_LEN + CUBIC_INTERP_HISTORY]; |
84 | unsigned int internal_buffer_end = -1; |
85 | uint64_t mix_offset = 0; |
86 | |
87 | protected: |
88 | void begin_resample(); |
89 | // Returns the number of frames that were mixed. |
90 | virtual int _mix_internal(AudioFrame *p_buffer, int p_frames); |
91 | virtual float get_stream_sampling_rate(); |
92 | |
93 | GDVIRTUAL2R(int, _mix_resampled, GDExtensionPtr<AudioFrame>, int) |
94 | GDVIRTUAL0RC(float, _get_stream_sampling_rate) |
95 | |
96 | static void _bind_methods(); |
97 | |
98 | public: |
99 | virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override; |
100 | |
101 | AudioStreamPlaybackResampled() { mix_offset = 0; } |
102 | }; |
103 | |
104 | class AudioStream : public Resource { |
105 | GDCLASS(AudioStream, Resource); |
106 | OBJ_SAVE_TYPE(AudioStream); // Saves derived classes with common type so they can be interchanged. |
107 | |
108 | enum { |
109 | MAX_TAGGED_OFFSETS = 8 |
110 | }; |
111 | |
112 | uint64_t tagged_frame = 0; |
113 | uint64_t offset_count = 0; |
114 | float tagged_offsets[MAX_TAGGED_OFFSETS]; |
115 | |
116 | protected: |
117 | static void _bind_methods(); |
118 | |
119 | GDVIRTUAL0RC(Ref<AudioStreamPlayback>, _instantiate_playback) |
120 | GDVIRTUAL0RC(String, _get_stream_name) |
121 | GDVIRTUAL0RC(double, _get_length) |
122 | GDVIRTUAL0RC(bool, _is_monophonic) |
123 | GDVIRTUAL0RC(double, _get_bpm) |
124 | GDVIRTUAL0RC(bool, _has_loop) |
125 | GDVIRTUAL0RC(int, _get_bar_beats) |
126 | GDVIRTUAL0RC(int, _get_beat_count) |
127 | |
128 | public: |
129 | virtual Ref<AudioStreamPlayback> instantiate_playback(); |
130 | virtual String get_stream_name() const; |
131 | |
132 | virtual double get_bpm() const; |
133 | virtual bool has_loop() const; |
134 | virtual int get_bar_beats() const; |
135 | virtual int get_beat_count() const; |
136 | |
137 | virtual double get_length() const; |
138 | virtual bool is_monophonic() const; |
139 | |
140 | void tag_used(float p_offset); |
141 | uint64_t get_tagged_frame() const; |
142 | uint32_t get_tagged_frame_count() const; |
143 | float get_tagged_frame_offset(int p_index) const; |
144 | }; |
145 | |
146 | // Microphone |
147 | |
148 | class AudioStreamPlaybackMicrophone; |
149 | |
150 | class AudioStreamMicrophone : public AudioStream { |
151 | GDCLASS(AudioStreamMicrophone, AudioStream); |
152 | friend class AudioStreamPlaybackMicrophone; |
153 | |
154 | HashSet<AudioStreamPlaybackMicrophone *> playbacks; |
155 | |
156 | protected: |
157 | static void _bind_methods(); |
158 | |
159 | public: |
160 | virtual Ref<AudioStreamPlayback> instantiate_playback() override; |
161 | virtual String get_stream_name() const override; |
162 | |
163 | virtual double get_length() const override; //if supported, otherwise return 0 |
164 | |
165 | virtual bool is_monophonic() const override; |
166 | |
167 | AudioStreamMicrophone(); |
168 | }; |
169 | |
170 | class AudioStreamPlaybackMicrophone : public AudioStreamPlaybackResampled { |
171 | GDCLASS(AudioStreamPlaybackMicrophone, AudioStreamPlaybackResampled); |
172 | friend class AudioStreamMicrophone; |
173 | |
174 | bool active = false; |
175 | unsigned int input_ofs = 0; |
176 | |
177 | Ref<AudioStreamMicrophone> microphone; |
178 | |
179 | protected: |
180 | virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override; |
181 | virtual float get_stream_sampling_rate() override; |
182 | virtual double get_playback_position() const override; |
183 | |
184 | public: |
185 | virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override; |
186 | |
187 | virtual void start(double p_from_pos = 0.0) override; |
188 | virtual void stop() override; |
189 | virtual bool is_playing() const override; |
190 | |
191 | virtual int get_loop_count() const override; //times it looped |
192 | |
193 | virtual void seek(double p_time) override; |
194 | |
195 | virtual void tag_used_streams() override; |
196 | |
197 | ~AudioStreamPlaybackMicrophone(); |
198 | AudioStreamPlaybackMicrophone(); |
199 | }; |
200 | |
201 | // |
202 | |
203 | class AudioStreamPlaybackRandomizer; |
204 | |
205 | class AudioStreamRandomizer : public AudioStream { |
206 | GDCLASS(AudioStreamRandomizer, AudioStream); |
207 | |
208 | public: |
209 | enum PlaybackMode { |
210 | PLAYBACK_RANDOM_NO_REPEATS, |
211 | PLAYBACK_RANDOM, |
212 | PLAYBACK_SEQUENTIAL, |
213 | }; |
214 | |
215 | private: |
216 | friend class AudioStreamPlaybackRandomizer; |
217 | |
218 | struct PoolEntry { |
219 | Ref<AudioStream> stream; |
220 | float weight; |
221 | }; |
222 | |
223 | HashSet<AudioStreamPlaybackRandomizer *> playbacks; |
224 | Vector<PoolEntry> audio_stream_pool; |
225 | float random_pitch_scale = 1.0f; |
226 | float random_volume_offset_db = 0.0f; |
227 | |
228 | Ref<AudioStreamPlayback> instance_playback_random(); |
229 | Ref<AudioStreamPlayback> instance_playback_no_repeats(); |
230 | Ref<AudioStreamPlayback> instance_playback_sequential(); |
231 | |
232 | Ref<AudioStream> last_playback = nullptr; |
233 | PlaybackMode playback_mode = PLAYBACK_RANDOM_NO_REPEATS; |
234 | |
235 | protected: |
236 | static void _bind_methods(); |
237 | |
238 | bool _set(const StringName &p_name, const Variant &p_value); |
239 | bool _get(const StringName &p_name, Variant &r_ret) const; |
240 | void _get_property_list(List<PropertyInfo> *p_list) const; |
241 | |
242 | public: |
243 | void add_stream(int p_index, Ref<AudioStream> p_stream, float p_weight = 1.0); |
244 | void move_stream(int p_index_from, int p_index_to); |
245 | void remove_stream(int p_index); |
246 | |
247 | void set_stream(int p_index, Ref<AudioStream> p_stream); |
248 | Ref<AudioStream> get_stream(int p_index) const; |
249 | void set_stream_probability_weight(int p_index, float p_weight); |
250 | float get_stream_probability_weight(int p_index) const; |
251 | |
252 | void set_streams_count(int p_count); |
253 | int get_streams_count() const; |
254 | |
255 | void set_random_pitch(float p_pitch_scale); |
256 | float get_random_pitch() const; |
257 | |
258 | void set_random_volume_offset_db(float p_volume_offset_db); |
259 | float get_random_volume_offset_db() const; |
260 | |
261 | void set_playback_mode(PlaybackMode p_playback_mode); |
262 | PlaybackMode get_playback_mode() const; |
263 | |
264 | virtual Ref<AudioStreamPlayback> instantiate_playback() override; |
265 | virtual String get_stream_name() const override; |
266 | |
267 | virtual double get_length() const override; //if supported, otherwise return 0 |
268 | virtual bool is_monophonic() const override; |
269 | |
270 | AudioStreamRandomizer(); |
271 | }; |
272 | |
273 | class AudioStreamPlaybackRandomizer : public AudioStreamPlayback { |
274 | GDCLASS(AudioStreamPlaybackRandomizer, AudioStreamPlayback); |
275 | friend class AudioStreamRandomizer; |
276 | |
277 | Ref<AudioStreamRandomizer> randomizer; |
278 | Ref<AudioStreamPlayback> playback; |
279 | Ref<AudioStreamPlayback> playing; |
280 | |
281 | float pitch_scale; |
282 | float volume_scale; |
283 | |
284 | public: |
285 | virtual void start(double p_from_pos = 0.0) override; |
286 | virtual void stop() override; |
287 | virtual bool is_playing() const override; |
288 | |
289 | virtual int get_loop_count() const override; //times it looped |
290 | |
291 | virtual double get_playback_position() const override; |
292 | virtual void seek(double p_time) override; |
293 | |
294 | virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override; |
295 | |
296 | virtual void tag_used_streams() override; |
297 | |
298 | ~AudioStreamPlaybackRandomizer(); |
299 | }; |
300 | |
301 | VARIANT_ENUM_CAST(AudioStreamRandomizer::PlaybackMode); |
302 | |
303 | #endif // AUDIO_STREAM_H |
304 | |