Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Tightness parameter to AudioStreamPlayer3D #42358

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/classes/AudioStreamPlayer3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
<member name="stream_paused" type="bool" setter="set_stream_paused" getter="get_stream_paused" default="false">
If [code]true[/code], the playback is paused. You can resume it by setting [code]stream_paused[/code] to [code]false[/code].
</member>
<member name="tightness" type="float" setter="set_tightness" getter="get_tightness" default="4.0">
How much the sound will "enclose" the listener (more undirected / playing from speakers not facing the source). Lower values enclose more.
</member>
<member name="unit_db" type="float" setter="set_unit_db" getter="get_unit_db" default="0.0">
Base sound level unaffected by dampening, in dB.
</member>
Expand Down
14 changes: 13 additions & 1 deletion scene/3d/audio_stream_player_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ void AudioStreamPlayer3D::_notification(int p_what) {

//TODO: The lower the second parameter (tightness) the more the sound will "enclose" the listener (more undirected / playing from
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like "TODO:" can now be deleted from the comment.

// speakers not facing the source) - this could be made distance dependent.
_calc_output_vol(local_pos.normalized(), 4.0, output);
_calc_output_vol(local_pos.normalized(), tightness, output);

unsigned int cc = AudioServer::get_singleton()->get_channel_count();
for (unsigned int k = 0; k < cc; k++) {
Expand Down Expand Up @@ -674,6 +674,13 @@ float AudioStreamPlayer3D::get_max_db() const {
return max_db;
}

void AudioStreamPlayer3D::set_tightness(float p_tightness) {
tightness = p_tightness;
}
float AudioStreamPlayer3D::get_tightness() const {
return tightness;
}

void AudioStreamPlayer3D::set_pitch_scale(float p_pitch_scale) {
ERR_FAIL_COND(p_pitch_scale <= 0.0);
pitch_scale = p_pitch_scale;
Expand Down Expand Up @@ -912,6 +919,9 @@ void AudioStreamPlayer3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_max_db", "max_db"), &AudioStreamPlayer3D::set_max_db);
ClassDB::bind_method(D_METHOD("get_max_db"), &AudioStreamPlayer3D::get_max_db);

ClassDB::bind_method(D_METHOD("set_tightness", "tightness"), &AudioStreamPlayer3D::set_tightness);
ClassDB::bind_method(D_METHOD("get_tightness"), &AudioStreamPlayer3D::get_tightness);

ClassDB::bind_method(D_METHOD("set_pitch_scale", "pitch_scale"), &AudioStreamPlayer3D::set_pitch_scale);
ClassDB::bind_method(D_METHOD("get_pitch_scale"), &AudioStreamPlayer3D::get_pitch_scale);

Expand Down Expand Up @@ -971,6 +981,7 @@ void AudioStreamPlayer3D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "unit_db", PROPERTY_HINT_RANGE, "-80,80"), "set_unit_db", "get_unit_db");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "unit_size", PROPERTY_HINT_RANGE, "0.1,100,0.1"), "set_unit_size", "get_unit_size");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_db", PROPERTY_HINT_RANGE, "-24,6"), "set_max_db", "get_max_db");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "tightness", PROPERTY_HINT_RANGE, "0,8"), "set_tightness", "get_tightness");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pitch_scale", PROPERTY_HINT_RANGE, "0.01,4,0.01,or_greater"), "set_pitch_scale", "get_pitch_scale");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_playing", "is_playing");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "is_autoplay_enabled");
Expand Down Expand Up @@ -1010,6 +1021,7 @@ AudioStreamPlayer3D::AudioStreamPlayer3D() {
attenuation_model = ATTENUATION_INVERSE_DISTANCE;
max_db = 3;
pitch_scale = 1.0;
tightness = 4.0;
autoplay = false;
setseek = -1;
active = false;
Expand Down
4 changes: 4 additions & 0 deletions scene/3d/audio_stream_player_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class AudioStreamPlayer3D : public Node3D {
float unit_size;
float max_db;
float pitch_scale;
float tightness;
bool autoplay;
bool stream_paused;
bool stream_paused_fade_in;
Expand Down Expand Up @@ -161,6 +162,9 @@ class AudioStreamPlayer3D : public Node3D {
void set_pitch_scale(float p_pitch_scale);
float get_pitch_scale() const;

void set_tightness(float p_tightness);
float get_tightness() const;

void play(float p_from_pos = 0.0);
void seek(float p_seconds);
void stop();
Expand Down