summaryrefslogtreecommitdiff
path: root/scene/3d/audio_stream_player_3d.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/3d/audio_stream_player_3d.cpp')
-rw-r--r--scene/3d/audio_stream_player_3d.cpp120
1 files changed, 76 insertions, 44 deletions
diff --git a/scene/3d/audio_stream_player_3d.cpp b/scene/3d/audio_stream_player_3d.cpp
index afd87deca6..8bc48914d5 100644
--- a/scene/3d/audio_stream_player_3d.cpp
+++ b/scene/3d/audio_stream_player_3d.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
+/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -32,11 +32,13 @@
#include "core/engine.h"
#include "scene/3d/area.h"
#include "scene/3d/camera.h"
+#include "scene/3d/listener.h"
#include "scene/main/viewport.h"
+
void AudioStreamPlayer3D::_mix_audio() {
if (!stream_playback.is_valid() || !active ||
- (stream_paused && !stream_paused_fade_out)) {
+ (stream_paused && !stream_fade_out)) {
return;
}
@@ -51,7 +53,7 @@ void AudioStreamPlayer3D::_mix_audio() {
AudioFrame *buffer = mix_buffer.ptrw();
int buffer_size = mix_buffer.size();
- if (stream_paused_fade_out) {
+ if (stream_fade_out) {
// Short fadeout ramp
buffer_size = MIN(buffer_size, 128);
}
@@ -107,10 +109,10 @@ void AudioStreamPlayer3D::_mix_audio() {
int buffers = AudioServer::get_singleton()->get_channel_count();
for (int k = 0; k < buffers; k++) {
- AudioFrame target_volume = stream_paused_fade_out ? AudioFrame(0.f, 0.f) : current.vol[k];
- AudioFrame vol_prev = stream_paused_fade_in ? AudioFrame(0.f, 0.f) : prev_outputs[i].vol[k];
+ AudioFrame target_volume = stream_fade_out ? AudioFrame(0.f, 0.f) : current.vol[k];
+ AudioFrame vol_prev = stream_fade_in ? AudioFrame(0.f, 0.f) : prev_outputs[i].vol[k];
AudioFrame vol_inc = (target_volume - vol_prev) / float(buffer_size);
- AudioFrame vol = stream_paused_fade_in ? AudioFrame(0.f, 0.f) : current.vol[k];
+ AudioFrame vol = stream_fade_in ? AudioFrame(0.f, 0.f) : current.vol[k];
if (!AudioServer::get_singleton()->thread_has_channel_mix_buffer(current.bus_index, k))
continue; //may have been deleted, will be updated on process
@@ -196,9 +198,15 @@ void AudioStreamPlayer3D::_mix_audio() {
active = false;
}
+ if (stream_stop) {
+ active = false;
+ set_physics_process_internal(false);
+ setplay = -1;
+ }
+
output_ready = false;
- stream_paused_fade_in = false;
- stream_paused_fade_out = false;
+ stream_fade_in = false;
+ stream_fade_out = false;
}
float AudioStreamPlayer3D::_get_attenuation_db(float p_distance) const {
@@ -206,16 +214,17 @@ float AudioStreamPlayer3D::_get_attenuation_db(float p_distance) const {
float att = 0;
switch (attenuation_model) {
case ATTENUATION_INVERSE_DISTANCE: {
- att = Math::linear2db(1.0 / ((p_distance / unit_size) + 000001));
+ att = Math::linear2db(1.0 / ((p_distance / unit_size) + CMP_EPSILON));
} break;
case ATTENUATION_INVERSE_SQUARE_DISTANCE: {
float d = (p_distance / unit_size);
d *= d;
- att = Math::linear2db(1.0 / (d + 0.00001));
+ att = Math::linear2db(1.0 / (d + CMP_EPSILON));
} break;
case ATTENUATION_LOGARITHMIC: {
- att = -20 * Math::log(p_distance / unit_size + 000001);
+ att = -20 * Math::log(p_distance / unit_size + CMP_EPSILON);
} break;
+ case ATTENUATION_DISABLED: break;
default: {
ERR_PRINT("Unknown attenuation type");
break;
@@ -323,16 +332,25 @@ void AudioStreamPlayer3D::_notification(int p_what) {
if (!vp->is_audio_listener())
continue;
- Vector3 local_pos = camera->get_global_transform().orthonormalized().affine_inverse().xform(global_pos);
+ bool listener_is_camera = true;
+ Spatial *listener_node = camera;
+
+ Listener *listener = vp->get_listener();
+ if (listener) {
+ listener_node = listener;
+ listener_is_camera = false;
+ }
+
+ Vector3 local_pos = listener_node->get_global_transform().orthonormalized().affine_inverse().xform(global_pos);
float dist = local_pos.length();
Vector3 area_sound_pos;
- Vector3 cam_area_pos;
+ Vector3 listener_area_pos;
if (area && area->is_using_reverb_bus() && area->get_reverb_uniformity() > 0) {
- area_sound_pos = space_state->get_closest_point_to_object_volume(area->get_rid(), camera->get_global_transform().origin);
- cam_area_pos = camera->get_global_transform().affine_inverse().xform(area_sound_pos);
+ area_sound_pos = space_state->get_closest_point_to_object_volume(area->get_rid(), listener_node->get_global_transform().origin);
+ listener_area_pos = listener_node->get_global_transform().affine_inverse().xform(area_sound_pos);
}
if (max_distance > 0) {
@@ -340,10 +358,10 @@ void AudioStreamPlayer3D::_notification(int p_what) {
float total_max = max_distance;
if (area && area->is_using_reverb_bus() && area->get_reverb_uniformity() > 0) {
- total_max = MAX(total_max, cam_area_pos.length());
+ total_max = MAX(total_max, listener_area_pos.length());
}
if (total_max > max_distance) {
- continue; //can't hear this sound in this camera
+ continue; //can't hear this sound in this listener
}
}
@@ -360,8 +378,8 @@ void AudioStreamPlayer3D::_notification(int p_what) {
float db_att = (1.0 - MIN(1.0, multiplier)) * attenuation_filter_db;
if (emission_angle_enabled) {
- Vector3 camtopos = global_pos - camera->get_global_transform().origin;
- float c = camtopos.normalized().dot(get_global_transform().basis.get_axis(2).normalized()); //it's z negative
+ Vector3 listenertopos = global_pos - listener_node->get_global_transform().origin;
+ float c = listenertopos.normalized().dot(get_global_transform().basis.get_axis(2).normalized()); //it's z negative
float angle = Math::rad2deg(Math::acos(c));
if (angle > emission_angle)
db_att -= -emission_angle_filter_attenuation_db;
@@ -381,8 +399,8 @@ void AudioStreamPlayer3D::_notification(int p_what) {
output.vol[0].l = 1.0 - c;
output.vol[0].r = c;
} else {
- Vector3 camtopos = global_pos - camera->get_global_transform().origin;
- float c = camtopos.normalized().dot(get_global_transform().basis.get_axis(2).normalized()); //it's z negative
+ Vector3 listenertopos = global_pos - listener_node->get_global_transform().origin;
+ float c = listenertopos.normalized().dot(get_global_transform().basis.get_axis(2).normalized()); //it's z negative
float angle = Math::rad2deg(Math::acos(c));
float av = angle * (flat_pos.x < 0 ? -1 : 1) / 180.0;
@@ -405,20 +423,20 @@ void AudioStreamPlayer3D::_notification(int p_what) {
if (cc >= 3) {
// Side pair
- float sl = Math::abs(1.0 - Math::abs(-0.4 - av));
- float sr = Math::abs(1.0 - Math::abs(0.4 - av));
+ float sleft = Math::abs(1.0 - Math::abs(-0.4 - av));
+ float sright = Math::abs(1.0 - Math::abs(0.4 - av));
- output.vol[2].l = sl;
- output.vol[2].r = sr;
+ output.vol[2].l = sleft;
+ output.vol[2].r = sright;
}
if (cc >= 4) {
// Rear pair
- float rl = Math::abs(1.0 - Math::abs(-0.2 - av));
- float rr = Math::abs(1.0 - Math::abs(0.2 - av));
+ float rleft = Math::abs(1.0 - Math::abs(-0.2 - av));
+ float rright = Math::abs(1.0 - Math::abs(0.2 - av));
- output.vol[3].l = rl;
- output.vol[3].r = rr;
+ output.vol[3].l = rleft;
+ output.vol[3].r = rright;
}
}
@@ -448,7 +466,7 @@ void AudioStreamPlayer3D::_notification(int p_what) {
if (uniformity > 0.0) {
- float distance = cam_area_pos.length();
+ float distance = listener_area_pos.length();
float attenuation = Math::db2linear(_get_attenuation_db(distance));
//float dist_att_db = -20 * Math::log(dist + 0.00001); //logarithmic attenuation, like in real life
@@ -458,7 +476,7 @@ void AudioStreamPlayer3D::_notification(int p_what) {
if (attenuation < 1.0) {
//pan the uniform sound
- Vector3 rev_pos = cam_area_pos;
+ Vector3 rev_pos = listener_area_pos;
rev_pos.y = 0;
rev_pos.normalize();
@@ -517,9 +535,13 @@ void AudioStreamPlayer3D::_notification(int p_what) {
if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
- Vector3 camera_velocity = camera->get_doppler_tracked_velocity();
+ Vector3 listener_velocity;
- Vector3 local_velocity = camera->get_global_transform().orthonormalized().basis.xform_inv(linear_velocity - camera_velocity);
+ if (listener_is_camera) {
+ listener_velocity = camera->get_doppler_tracked_velocity();
+ }
+
+ Vector3 local_velocity = listener_node->get_global_transform().orthonormalized().basis.xform_inv(linear_velocity - listener_velocity);
if (local_velocity == Vector3()) {
output.pitch_scale = 1.0;
@@ -641,6 +663,7 @@ float AudioStreamPlayer3D::get_pitch_scale() const {
void AudioStreamPlayer3D::play(float p_from_pos) {
if (stream_playback.is_valid()) {
+ stream_stop = false;
active = true;
setplay = p_from_pos;
output_ready = false;
@@ -658,9 +681,8 @@ void AudioStreamPlayer3D::seek(float p_seconds) {
void AudioStreamPlayer3D::stop() {
if (stream_playback.is_valid()) {
- active = false;
- set_physics_process_internal(false);
- setplay = -1;
+ stream_stop = true;
+ stream_fade_out = true;
}
}
@@ -810,7 +832,7 @@ float AudioStreamPlayer3D::get_attenuation_filter_db() const {
}
void AudioStreamPlayer3D::set_attenuation_model(AttenuationModel p_model) {
- ERR_FAIL_INDEX(p_model, 3);
+ ERR_FAIL_INDEX((int)p_model, 4);
attenuation_model = p_model;
}
@@ -839,7 +861,9 @@ void AudioStreamPlayer3D::set_doppler_tracking(DopplerTracking p_tracking) {
if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
set_notify_transform(true);
velocity_tracker->set_track_physics_step(doppler_tracking == DOPPLER_TRACKING_PHYSICS_STEP);
- velocity_tracker->reset(get_global_transform().origin);
+ if (is_inside_tree()) {
+ velocity_tracker->reset(get_global_transform().origin);
+ }
} else {
set_notify_transform(false);
}
@@ -854,8 +878,8 @@ void AudioStreamPlayer3D::set_stream_paused(bool p_pause) {
if (p_pause != stream_paused) {
stream_paused = p_pause;
- stream_paused_fade_in = stream_paused ? false : true;
- stream_paused_fade_out = stream_paused ? true : false;
+ stream_fade_in = stream_paused ? false : true;
+ stream_fade_out = stream_paused ? true : false;
}
}
@@ -864,6 +888,10 @@ bool AudioStreamPlayer3D::get_stream_paused() const {
return stream_paused;
}
+Ref<AudioStreamPlayback> AudioStreamPlayer3D::get_stream_playback() {
+ return stream_playback;
+}
+
void AudioStreamPlayer3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer3D::set_stream);
@@ -930,10 +958,12 @@ void AudioStreamPlayer3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_stream_paused", "pause"), &AudioStreamPlayer3D::set_stream_paused);
ClassDB::bind_method(D_METHOD("get_stream_paused"), &AudioStreamPlayer3D::get_stream_paused);
+ ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer3D::get_stream_playback);
+
ClassDB::bind_method(D_METHOD("_bus_layout_changed"), &AudioStreamPlayer3D::_bus_layout_changed);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "attenuation_model", PROPERTY_HINT_ENUM, "Inverse,InverseSquare,Log"), "set_attenuation_model", "get_attenuation_model");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "attenuation_model", PROPERTY_HINT_ENUM, "Inverse,InverseSquare,Log,Disabled"), "set_attenuation_model", "get_attenuation_model");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "unit_db", PROPERTY_HINT_RANGE, "-80,80"), "set_unit_db", "get_unit_db");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "unit_size", PROPERTY_HINT_RANGE, "0.1,100,0.1"), "set_unit_size", "get_unit_size");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_db", PROPERTY_HINT_RANGE, "-24,6"), "set_max_db", "get_max_db");
@@ -958,6 +988,7 @@ void AudioStreamPlayer3D::_bind_methods() {
BIND_ENUM_CONSTANT(ATTENUATION_INVERSE_DISTANCE);
BIND_ENUM_CONSTANT(ATTENUATION_INVERSE_SQUARE_DISTANCE);
BIND_ENUM_CONSTANT(ATTENUATION_LOGARITHMIC);
+ BIND_ENUM_CONSTANT(ATTENUATION_DISABLED);
BIND_ENUM_CONSTANT(OUT_OF_RANGE_MIX);
BIND_ENUM_CONSTANT(OUT_OF_RANGE_PAUSE);
@@ -993,8 +1024,9 @@ AudioStreamPlayer3D::AudioStreamPlayer3D() {
out_of_range_mode = OUT_OF_RANGE_MIX;
doppler_tracking = DOPPLER_TRACKING_DISABLED;
stream_paused = false;
- stream_paused_fade_in = false;
- stream_paused_fade_out = false;
+ stream_fade_in = false;
+ stream_fade_out = false;
+ stream_stop = false;
velocity_tracker.instance();
AudioServer::get_singleton()->connect("bus_layout_changed", this, "_bus_layout_changed");