Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/audio_stream_player_2d.cpp
9903 views
1
/**************************************************************************/
2
/* audio_stream_player_2d.cpp */
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
#include "audio_stream_player_2d.h"
32
#include "audio_stream_player_2d.compat.inc"
33
34
#include "core/config/project_settings.h"
35
#include "scene/2d/audio_listener_2d.h"
36
#include "scene/audio/audio_stream_player_internal.h"
37
#include "scene/main/viewport.h"
38
#include "scene/resources/world_2d.h"
39
#include "servers/audio/audio_stream.h"
40
#include "servers/audio_server.h"
41
42
#ifndef PHYSICS_2D_DISABLED
43
#include "scene/2d/physics/area_2d.h"
44
#endif // PHYSICS_2D_DISABLED
45
46
void AudioStreamPlayer2D::_notification(int p_what) {
47
internal->notification(p_what);
48
49
switch (p_what) {
50
case NOTIFICATION_ENTER_TREE: {
51
AudioServer::get_singleton()->add_listener_changed_callback(_listener_changed_cb, this);
52
} break;
53
54
case NOTIFICATION_EXIT_TREE: {
55
AudioServer::get_singleton()->remove_listener_changed_callback(_listener_changed_cb, this);
56
} break;
57
58
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
59
// Update anything related to position first, if possible of course.
60
if (setplay.get() > 0 || (internal->active.is_set() && last_mix_count != AudioServer::get_singleton()->get_mix_count()) || force_update_panning) {
61
force_update_panning = false;
62
_update_panning();
63
}
64
65
if (setplayback.is_valid() && setplay.get() >= 0) {
66
internal->active.set();
67
AudioServer::get_singleton()->start_playback_stream(setplayback, _get_actual_bus(), volume_vector, setplay.get(), internal->pitch_scale);
68
setplayback.unref();
69
setplay.set(-1);
70
}
71
72
if (!internal->stream_playbacks.is_empty() && internal->active.is_set()) {
73
internal->process();
74
}
75
internal->ensure_playback_limit();
76
} break;
77
}
78
}
79
80
// Interacts with PhysicsServer2D, so can only be called during _physics_process.
81
StringName AudioStreamPlayer2D::_get_actual_bus() {
82
#ifndef PHYSICS_2D_DISABLED
83
Vector2 global_pos = get_global_position();
84
85
//check if any area is diverting sound into a bus
86
Ref<World2D> world_2d = get_world_2d();
87
ERR_FAIL_COND_V(world_2d.is_null(), SceneStringName(Master));
88
89
PhysicsDirectSpaceState2D *space_state = PhysicsServer2D::get_singleton()->space_get_direct_state(world_2d->get_space());
90
ERR_FAIL_NULL_V(space_state, SceneStringName(Master));
91
PhysicsDirectSpaceState2D::ShapeResult sr[MAX_INTERSECT_AREAS];
92
93
PhysicsDirectSpaceState2D::PointParameters point_params;
94
point_params.position = global_pos;
95
point_params.collision_mask = area_mask;
96
point_params.collide_with_bodies = false;
97
point_params.collide_with_areas = true;
98
99
int areas = space_state->intersect_point(point_params, sr, MAX_INTERSECT_AREAS);
100
for (int i = 0; i < areas; i++) {
101
Area2D *area2d = Object::cast_to<Area2D>(sr[i].collider);
102
if (!area2d) {
103
continue;
104
}
105
106
if (!area2d->is_overriding_audio_bus()) {
107
continue;
108
}
109
110
return area2d->get_audio_bus_name();
111
}
112
#endif // PHYSICS_2D_DISABLED
113
114
return internal->bus;
115
}
116
117
// Interacts with PhysicsServer2D, so can only be called during _physics_process
118
void AudioStreamPlayer2D::_update_panning() {
119
if (!internal->active.is_set() || internal->stream.is_null()) {
120
return;
121
}
122
123
Ref<World2D> world_2d = get_world_2d();
124
ERR_FAIL_COND(world_2d.is_null());
125
126
Vector2 global_pos = get_global_position();
127
128
HashSet<Viewport *> viewports = world_2d->get_viewports();
129
130
volume_vector.resize(4);
131
volume_vector.write[0] = AudioFrame(0, 0);
132
volume_vector.write[1] = AudioFrame(0, 0);
133
volume_vector.write[2] = AudioFrame(0, 0);
134
volume_vector.write[3] = AudioFrame(0, 0);
135
136
StringName actual_bus = _get_actual_bus();
137
138
for (Viewport *vp : viewports) {
139
if (!vp->is_audio_listener_2d()) {
140
continue;
141
}
142
//compute matrix to convert to screen
143
Vector2 screen_size = vp->get_visible_rect().size;
144
Vector2 listener_in_global;
145
Vector2 relative_to_listener;
146
147
//screen in global is used for attenuation
148
AudioListener2D *listener = vp->get_audio_listener_2d();
149
Transform2D full_canvas_transform = vp->get_global_canvas_transform() * vp->get_canvas_transform();
150
if (listener) {
151
listener_in_global = listener->get_global_position();
152
relative_to_listener = (global_pos - listener_in_global).rotated(-listener->get_global_rotation());
153
relative_to_listener *= full_canvas_transform.get_scale(); // Default listener scales with canvas size, do the same here.
154
} else {
155
listener_in_global = full_canvas_transform.affine_inverse().xform(screen_size * 0.5);
156
relative_to_listener = full_canvas_transform.xform(global_pos) - screen_size * 0.5;
157
}
158
159
float dist = global_pos.distance_to(listener_in_global); // Distance to listener, or screen if none.
160
161
if (dist > max_distance) {
162
continue; // Can't hear this sound in this viewport.
163
}
164
165
float multiplier = Math::pow(1.0f - dist / max_distance, attenuation);
166
multiplier *= Math::db_to_linear(internal->volume_db); // Also apply player volume!
167
168
float pan = relative_to_listener.x / screen_size.x;
169
// Don't let the panning effect extend (too far) beyond the screen.
170
pan = CLAMP(pan, -1, 1);
171
172
// Bake in a constant factor here to allow the project setting defaults for 2d and 3d to be normalized to 1.0.
173
pan *= panning_strength * cached_global_panning_strength * 0.5f;
174
175
pan = CLAMP(pan + 0.5, 0.0, 1.0);
176
177
float l = 1.0 - pan;
178
float r = pan;
179
180
const AudioFrame &prev_sample = volume_vector[0];
181
AudioFrame new_sample = AudioFrame(l, r) * multiplier;
182
183
volume_vector.write[0] = AudioFrame(MAX(prev_sample[0], new_sample[0]), MAX(prev_sample[1], new_sample[1]));
184
}
185
186
for (const Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {
187
AudioServer::get_singleton()->set_playback_bus_exclusive(playback, actual_bus, volume_vector);
188
}
189
190
for (const Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {
191
AudioServer::get_singleton()->set_playback_pitch_scale(playback, internal->pitch_scale);
192
if (playback->get_is_sample() && playback->get_sample_playback().is_valid()) {
193
Ref<AudioSamplePlayback> sample_playback = playback->get_sample_playback();
194
AudioServer::get_singleton()->update_sample_playback_pitch_scale(sample_playback, internal->pitch_scale);
195
}
196
}
197
198
last_mix_count = AudioServer::get_singleton()->get_mix_count();
199
}
200
201
void AudioStreamPlayer2D::set_stream(Ref<AudioStream> p_stream) {
202
internal->set_stream(p_stream);
203
}
204
205
Ref<AudioStream> AudioStreamPlayer2D::get_stream() const {
206
return internal->stream;
207
}
208
209
void AudioStreamPlayer2D::set_volume_db(float p_volume) {
210
ERR_FAIL_COND_MSG(Math::is_nan(p_volume), "Volume can't be set to NaN.");
211
internal->volume_db = p_volume;
212
}
213
214
float AudioStreamPlayer2D::get_volume_db() const {
215
return internal->volume_db;
216
}
217
218
void AudioStreamPlayer2D::set_volume_linear(float p_volume) {
219
set_volume_db(Math::linear_to_db(p_volume));
220
}
221
222
float AudioStreamPlayer2D::get_volume_linear() const {
223
return Math::db_to_linear(get_volume_db());
224
}
225
226
void AudioStreamPlayer2D::set_pitch_scale(float p_pitch_scale) {
227
internal->set_pitch_scale(p_pitch_scale);
228
}
229
230
float AudioStreamPlayer2D::get_pitch_scale() const {
231
return internal->pitch_scale;
232
}
233
234
void AudioStreamPlayer2D::play(float p_from_pos) {
235
Ref<AudioStreamPlayback> stream_playback = internal->play_basic();
236
if (stream_playback.is_null()) {
237
return;
238
}
239
setplayback = stream_playback;
240
setplay.set(p_from_pos);
241
242
// Sample handling.
243
if (stream_playback->get_is_sample() && stream_playback->get_sample_playback().is_valid()) {
244
Ref<AudioSamplePlayback> sample_playback = stream_playback->get_sample_playback();
245
sample_playback->offset = p_from_pos;
246
sample_playback->bus = _get_actual_bus();
247
248
AudioServer::get_singleton()->start_sample_playback(sample_playback);
249
}
250
}
251
252
void AudioStreamPlayer2D::seek(float p_seconds) {
253
internal->seek(p_seconds);
254
}
255
256
void AudioStreamPlayer2D::stop() {
257
setplay.set(-1);
258
internal->stop_basic();
259
}
260
261
bool AudioStreamPlayer2D::is_playing() const {
262
if (setplay.get() >= 0) {
263
return true; // play() has been called this frame, but no playback exists just yet.
264
}
265
return internal->is_playing();
266
}
267
268
float AudioStreamPlayer2D::get_playback_position() {
269
if (setplay.get() >= 0) {
270
return setplay.get(); // play() has been called this frame, but no playback exists just yet.
271
}
272
return internal->get_playback_position();
273
}
274
275
void AudioStreamPlayer2D::set_bus(const StringName &p_bus) {
276
internal->bus = p_bus; // This will be pushed to the audio server during the next physics timestep, which is fast enough.
277
}
278
279
StringName AudioStreamPlayer2D::get_bus() const {
280
return internal->get_bus();
281
}
282
283
void AudioStreamPlayer2D::set_autoplay(bool p_enable) {
284
internal->autoplay = p_enable;
285
}
286
287
bool AudioStreamPlayer2D::is_autoplay_enabled() const {
288
return internal->autoplay;
289
}
290
291
void AudioStreamPlayer2D::_set_playing(bool p_enable) {
292
internal->set_playing(p_enable);
293
}
294
295
void AudioStreamPlayer2D::_validate_property(PropertyInfo &p_property) const {
296
internal->validate_property(p_property);
297
}
298
299
void AudioStreamPlayer2D::set_max_distance(float p_pixels) {
300
ERR_FAIL_COND(p_pixels <= 0.0);
301
max_distance = p_pixels;
302
}
303
304
float AudioStreamPlayer2D::get_max_distance() const {
305
return max_distance;
306
}
307
308
void AudioStreamPlayer2D::set_attenuation(float p_curve) {
309
attenuation = p_curve;
310
}
311
312
float AudioStreamPlayer2D::get_attenuation() const {
313
return attenuation;
314
}
315
316
void AudioStreamPlayer2D::set_area_mask(uint32_t p_mask) {
317
area_mask = p_mask;
318
}
319
320
uint32_t AudioStreamPlayer2D::get_area_mask() const {
321
return area_mask;
322
}
323
324
void AudioStreamPlayer2D::set_stream_paused(bool p_pause) {
325
internal->set_stream_paused(p_pause);
326
}
327
328
bool AudioStreamPlayer2D::get_stream_paused() const {
329
return internal->get_stream_paused();
330
}
331
332
bool AudioStreamPlayer2D::has_stream_playback() {
333
return internal->has_stream_playback();
334
}
335
336
Ref<AudioStreamPlayback> AudioStreamPlayer2D::get_stream_playback() {
337
return internal->get_stream_playback();
338
}
339
340
void AudioStreamPlayer2D::set_max_polyphony(int p_max_polyphony) {
341
internal->set_max_polyphony(p_max_polyphony);
342
}
343
344
int AudioStreamPlayer2D::get_max_polyphony() const {
345
return internal->max_polyphony;
346
}
347
348
void AudioStreamPlayer2D::set_panning_strength(float p_panning_strength) {
349
ERR_FAIL_COND_MSG(p_panning_strength < 0, "Panning strength must be a positive number.");
350
panning_strength = p_panning_strength;
351
}
352
353
float AudioStreamPlayer2D::get_panning_strength() const {
354
return panning_strength;
355
}
356
357
AudioServer::PlaybackType AudioStreamPlayer2D::get_playback_type() const {
358
return internal->get_playback_type();
359
}
360
361
void AudioStreamPlayer2D::set_playback_type(AudioServer::PlaybackType p_playback_type) {
362
internal->set_playback_type(p_playback_type);
363
}
364
365
bool AudioStreamPlayer2D::_set(const StringName &p_name, const Variant &p_value) {
366
return internal->set(p_name, p_value);
367
}
368
369
bool AudioStreamPlayer2D::_get(const StringName &p_name, Variant &r_ret) const {
370
return internal->get(p_name, r_ret);
371
}
372
373
void AudioStreamPlayer2D::_get_property_list(List<PropertyInfo> *p_list) const {
374
internal->get_property_list(p_list);
375
}
376
377
void AudioStreamPlayer2D::_bind_methods() {
378
ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer2D::set_stream);
379
ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer2D::get_stream);
380
381
ClassDB::bind_method(D_METHOD("set_volume_db", "volume_db"), &AudioStreamPlayer2D::set_volume_db);
382
ClassDB::bind_method(D_METHOD("get_volume_db"), &AudioStreamPlayer2D::get_volume_db);
383
384
ClassDB::bind_method(D_METHOD("set_volume_linear", "volume_linear"), &AudioStreamPlayer2D::set_volume_linear);
385
ClassDB::bind_method(D_METHOD("get_volume_linear"), &AudioStreamPlayer2D::get_volume_linear);
386
387
ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer2D::set_pitch_scale);
388
ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer2D::get_pitch_scale);
389
390
ClassDB::bind_method(D_METHOD("play", "from_position"), &AudioStreamPlayer2D::play, DEFVAL(0.0));
391
ClassDB::bind_method(D_METHOD("seek", "to_position"), &AudioStreamPlayer2D::seek);
392
ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlayer2D::stop);
393
394
ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlayer2D::is_playing);
395
ClassDB::bind_method(D_METHOD("get_playback_position"), &AudioStreamPlayer2D::get_playback_position);
396
397
ClassDB::bind_method(D_METHOD("set_bus", "bus"), &AudioStreamPlayer2D::set_bus);
398
ClassDB::bind_method(D_METHOD("get_bus"), &AudioStreamPlayer2D::get_bus);
399
400
ClassDB::bind_method(D_METHOD("set_autoplay", "enable"), &AudioStreamPlayer2D::set_autoplay);
401
ClassDB::bind_method(D_METHOD("is_autoplay_enabled"), &AudioStreamPlayer2D::is_autoplay_enabled);
402
403
ClassDB::bind_method(D_METHOD("set_playing", "enable"), &AudioStreamPlayer2D::_set_playing);
404
405
ClassDB::bind_method(D_METHOD("set_max_distance", "pixels"), &AudioStreamPlayer2D::set_max_distance);
406
ClassDB::bind_method(D_METHOD("get_max_distance"), &AudioStreamPlayer2D::get_max_distance);
407
408
ClassDB::bind_method(D_METHOD("set_attenuation", "curve"), &AudioStreamPlayer2D::set_attenuation);
409
ClassDB::bind_method(D_METHOD("get_attenuation"), &AudioStreamPlayer2D::get_attenuation);
410
411
ClassDB::bind_method(D_METHOD("set_area_mask", "mask"), &AudioStreamPlayer2D::set_area_mask);
412
ClassDB::bind_method(D_METHOD("get_area_mask"), &AudioStreamPlayer2D::get_area_mask);
413
414
ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer2D::set_stream_paused);
415
ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer2D::get_stream_paused);
416
417
ClassDB::bind_method(D_METHOD("set_max_polyphony", "max_polyphony"), &AudioStreamPlayer2D::set_max_polyphony);
418
ClassDB::bind_method(D_METHOD("get_max_polyphony"), &AudioStreamPlayer2D::get_max_polyphony);
419
420
ClassDB::bind_method(D_METHOD("set_panning_strength", "panning_strength"), &AudioStreamPlayer2D::set_panning_strength);
421
ClassDB::bind_method(D_METHOD("get_panning_strength"), &AudioStreamPlayer2D::get_panning_strength);
422
423
ClassDB::bind_method(D_METHOD("has_stream_playback"), &AudioStreamPlayer2D::has_stream_playback);
424
ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer2D::get_stream_playback);
425
426
ClassDB::bind_method(D_METHOD("set_playback_type", "playback_type"), &AudioStreamPlayer2D::set_playback_type);
427
ClassDB::bind_method(D_METHOD("get_playback_type"), &AudioStreamPlayer2D::get_playback_type);
428
429
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
430
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_db", PROPERTY_HINT_RANGE, "-80,24,suffix:dB"), "set_volume_db", "get_volume_db");
431
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volume_linear", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_volume_linear", "get_volume_linear");
432
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");
433
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_ONESHOT, "", PROPERTY_USAGE_EDITOR), "set_playing", "is_playing");
434
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
435
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stream_paused", PROPERTY_HINT_NONE, ""), "set_stream_paused", "get_stream_paused");
436
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_distance", PROPERTY_HINT_RANGE, "1,4096,1,or_greater,exp,suffix:px"), "set_max_distance", "get_max_distance");
437
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "attenuation", PROPERTY_HINT_EXP_EASING, "attenuation"), "set_attenuation", "get_attenuation");
438
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_polyphony", PROPERTY_HINT_NONE, ""), "set_max_polyphony", "get_max_polyphony");
439
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "panning_strength", PROPERTY_HINT_RANGE, "0,3,0.01,or_greater"), "set_panning_strength", "get_panning_strength");
440
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bus", PROPERTY_HINT_ENUM, ""), "set_bus", "get_bus");
441
ADD_PROPERTY(PropertyInfo(Variant::INT, "area_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_area_mask", "get_area_mask");
442
ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_type", PROPERTY_HINT_ENUM, "Default,Stream,Sample"), "set_playback_type", "get_playback_type");
443
444
ADD_SIGNAL(MethodInfo("finished"));
445
}
446
447
AudioStreamPlayer2D::AudioStreamPlayer2D() {
448
internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer2D::play), callable_mp(this, &AudioStreamPlayer2D::stop), true));
449
cached_global_panning_strength = GLOBAL_GET_CACHED(float, "audio/general/2d_panning_strength");
450
set_hide_clip_children(true);
451
}
452
453
AudioStreamPlayer2D::~AudioStreamPlayer2D() {
454
memdelete(internal);
455
}
456
457