diff options
Diffstat (limited to 'scene/3d/audio_stream_player_3d.cpp')
-rw-r--r-- | scene/3d/audio_stream_player_3d.cpp | 115 |
1 files changed, 33 insertions, 82 deletions
diff --git a/scene/3d/audio_stream_player_3d.cpp b/scene/3d/audio_stream_player_3d.cpp index 8a00d67e12..6e4db8f382 100644 --- a/scene/3d/audio_stream_player_3d.cpp +++ b/scene/3d/audio_stream_player_3d.cpp @@ -138,7 +138,6 @@ void AudioStreamPlayer3D::_calc_output_vol(const Vector3 &source_dir, real_t tig } void AudioStreamPlayer3D::_mix_audio() { - if (!stream_playback.is_valid() || !active || (stream_paused && !stream_paused_fade_out)) { return; @@ -162,7 +161,6 @@ void AudioStreamPlayer3D::_mix_audio() { // Mix if we're not paused or we're fading out if ((output_count > 0 || out_of_range_mode == OUT_OF_RANGE_MIX)) { - float output_pitch_scale = 0.0; if (output_count) { //used for doppler, not realistic but good enough @@ -179,7 +177,6 @@ void AudioStreamPlayer3D::_mix_audio() { //write all outputs for (int i = 0; i < output_count; i++) { - Output current = outputs[i]; //see if current output exists, to keep volume ramp @@ -216,8 +213,9 @@ void AudioStreamPlayer3D::_mix_audio() { AudioFrame vol_inc = (target_volume - vol_prev) / float(buffer_size); AudioFrame vol = vol_prev; - if (!AudioServer::get_singleton()->thread_has_channel_mix_buffer(current.bus_index, k)) + if (!AudioServer::get_singleton()->thread_has_channel_mix_buffer(current.bus_index, k)) { continue; //may have been deleted, will be updated on process + } AudioFrame *target = AudioServer::get_singleton()->thread_get_channel_mix_buffer(current.bus_index, k); current.filter.set_mode(AudioFilterSW::HIGHSHELF); @@ -228,7 +226,6 @@ void AudioStreamPlayer3D::_mix_audio() { current.filter.set_gain(current.filter_gain); if (interpolate_filter) { - current.filter_process[k * 2 + 0] = prev_outputs[i].filter_process[k * 2 + 0]; current.filter_process[k * 2 + 1] = prev_outputs[i].filter_process[k * 2 + 1]; @@ -238,7 +235,6 @@ void AudioStreamPlayer3D::_mix_audio() { current.filter_process[k * 2 + 0].update_coeffs(buffer_size); current.filter_process[k * 2 + 1].update_coeffs(buffer_size); for (int j = 0; j < buffer_size; j++) { - AudioFrame f = buffer[j] * vol; current.filter_process[k * 2 + 0].process_one_interp(f.l); current.filter_process[k * 2 + 1].process_one_interp(f.r); @@ -253,7 +249,6 @@ void AudioStreamPlayer3D::_mix_audio() { current.filter_process[k * 2 + 0].update_coeffs(); current.filter_process[k * 2 + 1].update_coeffs(); for (int j = 0; j < buffer_size; j++) { - AudioFrame f = buffer[j] * vol; current.filter_process[k * 2 + 0].process_one(f.l); current.filter_process[k * 2 + 1].process_one(f.r); @@ -264,9 +259,9 @@ void AudioStreamPlayer3D::_mix_audio() { } if (current.reverb_bus_index >= 0) { - - if (!AudioServer::get_singleton()->thread_has_channel_mix_buffer(current.reverb_bus_index, k)) + if (!AudioServer::get_singleton()->thread_has_channel_mix_buffer(current.reverb_bus_index, k)) { continue; //may have been deleted, will be updated on process + } AudioFrame *rtarget = AudioServer::get_singleton()->thread_get_channel_mix_buffer(current.reverb_bus_index, k); @@ -275,15 +270,12 @@ void AudioStreamPlayer3D::_mix_audio() { AudioFrame rvol = prev_outputs[i].reverb_vol[k]; for (int j = 0; j < buffer_size; j++) { - rtarget[j] += buffer[j] * rvol; rvol += rvol_inc; } } else { - AudioFrame rvol = current.reverb_vol[k]; for (int j = 0; j < buffer_size; j++) { - rtarget[j] += buffer[j] * rvol; } } @@ -306,7 +298,6 @@ void AudioStreamPlayer3D::_mix_audio() { } float AudioStreamPlayer3D::_get_attenuation_db(float p_distance) const { - float att = 0; switch (attenuation_model) { case ATTENUATION_INVERSE_DISTANCE: { @@ -340,9 +331,7 @@ void _update_sound() { } void AudioStreamPlayer3D::_notification(int p_what) { - if (p_what == NOTIFICATION_ENTER_TREE) { - velocity_tracker->reset(get_global_transform().origin); AudioServer::get_singleton()->add_callback(_mix_audios, this); if (autoplay && !Engine::get_singleton()->is_editor_hint()) { @@ -351,7 +340,6 @@ void AudioStreamPlayer3D::_notification(int p_what) { } if (p_what == NOTIFICATION_EXIT_TREE) { - AudioServer::get_singleton()->remove_callback(_mix_audios, this); } @@ -367,18 +355,15 @@ void AudioStreamPlayer3D::_notification(int p_what) { } if (p_what == NOTIFICATION_TRANSFORM_CHANGED) { - if (doppler_tracking != DOPPLER_TRACKING_DISABLED) { velocity_tracker->update_position(get_global_transform().origin); } } if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) { - //update anything related to position first, if possible of course if (!output_ready) { - Vector3 linear_velocity; //compute linear velocity for doppler @@ -405,15 +390,18 @@ void AudioStreamPlayer3D::_notification(int p_what) { Area3D *area = nullptr; for (int i = 0; i < areas; i++) { - if (!sr[i].collider) + if (!sr[i].collider) { continue; + } Area3D *tarea = Object::cast_to<Area3D>(sr[i].collider); - if (!tarea) + if (!tarea) { continue; + } - if (!tarea->is_overriding_audio_bus() && !tarea->is_using_reverb_bus()) + if (!tarea->is_overriding_audio_bus() && !tarea->is_using_reverb_bus()) { continue; + } area = tarea; break; @@ -423,11 +411,11 @@ void AudioStreamPlayer3D::_notification(int p_what) { world_3d->get_camera_list(&cameras); for (List<Camera3D *>::Element *E = cameras.front(); E; E = E->next()) { - Camera3D *camera = E->get(); Viewport *vp = camera->get_viewport(); - if (!vp->is_audio_listener()) + if (!vp->is_audio_listener()) { continue; + } bool listener_is_camera = true; Node3D *listener_node = camera; @@ -451,7 +439,6 @@ void AudioStreamPlayer3D::_notification(int p_what) { } if (max_distance > 0) { - float total_max = max_distance; if (area && area->is_using_reverb_bus() && area->get_reverb_uniformity() > 0) { @@ -478,8 +465,9 @@ void AudioStreamPlayer3D::_notification(int p_what) { 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) + if (angle > emission_angle) { db_att -= -emission_angle_filter_attenuation_db; + } } output.filter_gain = Math::db2linear(db_att); @@ -497,7 +485,6 @@ void AudioStreamPlayer3D::_notification(int p_what) { int vol_index_max = AudioServer::get_singleton()->get_speaker_mode() + 1; if (area) { - if (area->is_overriding_audio_bus()) { //override audio bus StringName bus_name = area->get_audio_bus(); @@ -505,7 +492,6 @@ void AudioStreamPlayer3D::_notification(int p_what) { } if (area->is_using_reverb_bus()) { - filled_reverb = true; StringName bus_name = area->get_reverb_bus(); output.reverb_bus_index = AudioServer::get_singleton()->thread_find_bus_index(bus_name); @@ -514,7 +500,6 @@ void AudioStreamPlayer3D::_notification(int p_what) { float area_send = area->get_reverb_amount(); if (uniformity > 0.0) { - float distance = listener_area_pos.length(); float attenuation = Math::db2linear(_get_attenuation_db(distance)); @@ -556,26 +541,21 @@ void AudioStreamPlayer3D::_notification(int p_what) { } for (int i = 0; i < vol_index_max; i++) { - output.reverb_vol[i] = output.reverb_vol[i].lerp(center_frame, attenuation); } } else { for (int i = 0; i < vol_index_max; i++) { - output.reverb_vol[i] = center_frame; } } for (int i = 0; i < vol_index_max; i++) { - output.reverb_vol[i] = output.vol[i].lerp(output.reverb_vol[i] * attenuation, uniformity); output.reverb_vol[i] *= area_send; } } else { - for (int i = 0; i < vol_index_max; i++) { - output.reverb_vol[i] = output.vol[i] * area_send; } } @@ -583,7 +563,6 @@ void AudioStreamPlayer3D::_notification(int p_what) { } if (doppler_tracking != DOPPLER_TRACKING_DISABLED) { - Vector3 listener_velocity; if (listener_is_camera) { @@ -608,17 +587,16 @@ void AudioStreamPlayer3D::_notification(int p_what) { } if (!filled_reverb) { - for (int i = 0; i < vol_index_max; i++) { - output.reverb_vol[i] = AudioFrame(0, 0); } } outputs[new_output_count] = output; new_output_count++; - if (new_output_count == MAX_OUTPUTS) + if (new_output_count == MAX_OUTPUTS) { break; + } } output_count = new_output_count; @@ -645,7 +623,6 @@ void AudioStreamPlayer3D::_notification(int p_what) { } void AudioStreamPlayer3D::set_stream(Ref<AudioStream> p_stream) { - AudioServer::get_singleton()->lock(); mix_buffer.resize(AudioServer::get_singleton()->thread_get_mix_buffer_size()); @@ -670,34 +647,30 @@ void AudioStreamPlayer3D::set_stream(Ref<AudioStream> p_stream) { } Ref<AudioStream> AudioStreamPlayer3D::get_stream() const { - return stream; } void AudioStreamPlayer3D::set_unit_db(float p_volume) { - unit_db = p_volume; } -float AudioStreamPlayer3D::get_unit_db() const { +float AudioStreamPlayer3D::get_unit_db() const { return unit_db; } void AudioStreamPlayer3D::set_unit_size(float p_volume) { - unit_size = p_volume; } -float AudioStreamPlayer3D::get_unit_size() const { +float AudioStreamPlayer3D::get_unit_size() const { return unit_size; } void AudioStreamPlayer3D::set_max_db(float p_boost) { - max_db = p_boost; } -float AudioStreamPlayer3D::get_max_db() const { +float AudioStreamPlayer3D::get_max_db() const { return max_db; } @@ -705,12 +678,12 @@ void AudioStreamPlayer3D::set_pitch_scale(float p_pitch_scale) { ERR_FAIL_COND(p_pitch_scale <= 0.0); pitch_scale = p_pitch_scale; } + float AudioStreamPlayer3D::get_pitch_scale() const { return pitch_scale; } void AudioStreamPlayer3D::play(float p_from_pos) { - if (!is_playing()) { // Reset the prev_output_count if the stream is stopped prev_output_count = 0; @@ -725,14 +698,12 @@ void AudioStreamPlayer3D::play(float p_from_pos) { } void AudioStreamPlayer3D::seek(float p_seconds) { - if (stream_playback.is_valid()) { setseek = p_seconds; } } void AudioStreamPlayer3D::stop() { - if (stream_playback.is_valid()) { active = false; set_physics_process_internal(false); @@ -741,7 +712,6 @@ void AudioStreamPlayer3D::stop() { } bool AudioStreamPlayer3D::is_playing() const { - if (stream_playback.is_valid()) { return active; // && stream_playback->is_playing(); } @@ -750,7 +720,6 @@ bool AudioStreamPlayer3D::is_playing() const { } float AudioStreamPlayer3D::get_playback_position() { - if (stream_playback.is_valid()) { return stream_playback->get_playback_position(); } @@ -759,14 +728,13 @@ float AudioStreamPlayer3D::get_playback_position() { } void AudioStreamPlayer3D::set_bus(const StringName &p_bus) { - //if audio is active, must lock this AudioServer::get_singleton()->lock(); bus = p_bus; AudioServer::get_singleton()->unlock(); } -StringName AudioStreamPlayer3D::get_bus() const { +StringName AudioStreamPlayer3D::get_bus() const { for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) { if (AudioServer::get_singleton()->get_bus_name(i) == bus) { return bus; @@ -776,34 +744,32 @@ StringName AudioStreamPlayer3D::get_bus() const { } void AudioStreamPlayer3D::set_autoplay(bool p_enable) { - autoplay = p_enable; } -bool AudioStreamPlayer3D::is_autoplay_enabled() { +bool AudioStreamPlayer3D::is_autoplay_enabled() { return autoplay; } void AudioStreamPlayer3D::_set_playing(bool p_enable) { - - if (p_enable) + if (p_enable) { play(); - else + } else { stop(); + } } -bool AudioStreamPlayer3D::_is_active() const { +bool AudioStreamPlayer3D::_is_active() const { return active; } void AudioStreamPlayer3D::_validate_property(PropertyInfo &property) const { - if (property.name == "bus") { - String options; for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) { - if (i > 0) + if (i > 0) { options += ","; + } String name = AudioServer::get_singleton()->get_bus_name(i); options += name; } @@ -813,28 +779,23 @@ void AudioStreamPlayer3D::_validate_property(PropertyInfo &property) const { } void AudioStreamPlayer3D::_bus_layout_changed() { - _change_notify(); } void AudioStreamPlayer3D::set_max_distance(float p_metres) { - ERR_FAIL_COND(p_metres < 0.0); max_distance = p_metres; } float AudioStreamPlayer3D::get_max_distance() const { - return max_distance; } void AudioStreamPlayer3D::set_area_mask(uint32_t p_mask) { - area_mask = p_mask; } uint32_t AudioStreamPlayer3D::get_area_mask() const { - return area_mask; } @@ -859,30 +820,26 @@ float AudioStreamPlayer3D::get_emission_angle() const { } void AudioStreamPlayer3D::set_emission_angle_filter_attenuation_db(float p_angle_attenuation_db) { - emission_angle_filter_attenuation_db = p_angle_attenuation_db; } float AudioStreamPlayer3D::get_emission_angle_filter_attenuation_db() const { - return emission_angle_filter_attenuation_db; } void AudioStreamPlayer3D::set_attenuation_filter_cutoff_hz(float p_hz) { - attenuation_filter_cutoff_hz = p_hz; } -float AudioStreamPlayer3D::get_attenuation_filter_cutoff_hz() const { +float AudioStreamPlayer3D::get_attenuation_filter_cutoff_hz() const { return attenuation_filter_cutoff_hz; } void AudioStreamPlayer3D::set_attenuation_filter_db(float p_db) { - attenuation_filter_db = p_db; } -float AudioStreamPlayer3D::get_attenuation_filter_db() const { +float AudioStreamPlayer3D::get_attenuation_filter_db() const { return attenuation_filter_db; } @@ -896,20 +853,18 @@ AudioStreamPlayer3D::AttenuationModel AudioStreamPlayer3D::get_attenuation_model } void AudioStreamPlayer3D::set_out_of_range_mode(OutOfRangeMode p_mode) { - ERR_FAIL_INDEX((int)p_mode, 2); out_of_range_mode = p_mode; } AudioStreamPlayer3D::OutOfRangeMode AudioStreamPlayer3D::get_out_of_range_mode() const { - return out_of_range_mode; } void AudioStreamPlayer3D::set_doppler_tracking(DopplerTracking p_tracking) { - - if (doppler_tracking == p_tracking) + if (doppler_tracking == p_tracking) { return; + } doppler_tracking = p_tracking; @@ -925,12 +880,10 @@ void AudioStreamPlayer3D::set_doppler_tracking(DopplerTracking p_tracking) { } AudioStreamPlayer3D::DopplerTracking AudioStreamPlayer3D::get_doppler_tracking() const { - return doppler_tracking; } void AudioStreamPlayer3D::set_stream_paused(bool p_pause) { - if (p_pause != stream_paused) { stream_paused = p_pause; stream_paused_fade_in = !stream_paused; @@ -939,7 +892,6 @@ void AudioStreamPlayer3D::set_stream_paused(bool p_pause) { } bool AudioStreamPlayer3D::get_stream_paused() const { - return stream_paused; } @@ -948,7 +900,6 @@ Ref<AudioStreamPlayback> AudioStreamPlayer3D::get_stream_playback() { } void AudioStreamPlayer3D::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_stream", "stream"), &AudioStreamPlayer3D::set_stream); ClassDB::bind_method(D_METHOD("get_stream"), &AudioStreamPlayer3D::get_stream); @@ -1054,7 +1005,6 @@ void AudioStreamPlayer3D::_bind_methods() { } AudioStreamPlayer3D::AudioStreamPlayer3D() { - unit_db = 0; unit_size = 1; attenuation_model = ATTENUATION_INVERSE_DISTANCE; @@ -1084,5 +1034,6 @@ AudioStreamPlayer3D::AudioStreamPlayer3D() { AudioServer::get_singleton()->connect("bus_layout_changed", callable_mp(this, &AudioStreamPlayer3D::_bus_layout_changed)); set_disable_scale(true); } + AudioStreamPlayer3D::~AudioStreamPlayer3D() { } |