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

Implement air absorption #85

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ void SteamAudioPlayer::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_min_attenuation_distance", "p_min_attenuation_distance"), &SteamAudioPlayer::set_min_attenuation_dist);
ClassDB::bind_method(D_METHOD("set_max_reflection_distance", "p_max_reflection_distance"), &SteamAudioPlayer::set_max_reflection_dist);
ClassDB::bind_method(D_METHOD("get_max_reflection_distance"), &SteamAudioPlayer::get_max_reflection_dist);
ClassDB::bind_method(D_METHOD("is_air_absorp_on"), &SteamAudioPlayer::is_air_absorp_on);
ClassDB::bind_method(D_METHOD("set_air_absorp_on", "p_air_absorp_on"), &SteamAudioPlayer::set_air_absorp_on);
ClassDB::bind_method(D_METHOD("set_air_absorption_low", "p_air_absorption_low"), &SteamAudioPlayer::set_air_absorption_low);
ClassDB::bind_method(D_METHOD("get_air_absorption_low"), &SteamAudioPlayer::get_air_absorption_low);
ClassDB::bind_method(D_METHOD("set_air_absorption_mid", "p_air_absorption_mid"), &SteamAudioPlayer::set_air_absorption_mid);
ClassDB::bind_method(D_METHOD("get_air_absorption_mid"), &SteamAudioPlayer::get_air_absorption_mid);
ClassDB::bind_method(D_METHOD("set_air_absorption_high", "p_air_absorption_high"), &SteamAudioPlayer::set_air_absorption_high);
ClassDB::bind_method(D_METHOD("get_air_absorption_high"), &SteamAudioPlayer::get_air_absorption_high);
ClassDB::bind_method(D_METHOD("get_air_absorption_model_type"), &SteamAudioPlayer::get_air_absorption_model_type);
ClassDB::bind_method(D_METHOD("set_air_absorption_model_type", "p_air_absorption_model_type"), &SteamAudioPlayer::set_air_absorption_model_type);
ClassDB::bind_method(D_METHOD("is_occlusion_on"), &SteamAudioPlayer::is_occlusion_on);
ClassDB::bind_method(D_METHOD("set_occlusion_on", "p_occlusion_on"), &SteamAudioPlayer::set_occlusion_on);
ClassDB::bind_method(D_METHOD("is_reflection_on"), &SteamAudioPlayer::is_reflection_on);
Expand All @@ -36,6 +46,13 @@ void SteamAudioPlayer::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "distance_attenuation"), "set_dist_attn_on", "is_dist_attn_on");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_attenuation_distance", PROPERTY_HINT_RANGE, "0.0,100.0,0.1"), "set_min_attenuation_distance", "get_min_attenuation_distance");

ADD_GROUP("Air Absorption", "");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "air_absorption"), "set_air_absorp_on", "is_air_absorp_on");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "air_absorption_low", PROPERTY_HINT_RANGE, "0.0,1.0,0.1"), "set_air_absorption_low", "get_air_absorption_low");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "air_absorption_mid", PROPERTY_HINT_RANGE, "0.0,1.0,0.1"), "set_air_absorption_mid", "get_air_absorption_mid");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "air_absorption_high", PROPERTY_HINT_RANGE, "0.0,1.0,0.1"), "set_air_absorption_high", "get_air_absorption_high");
ADD_PROPERTY(PropertyInfo(Variant::INT, "air_absorption_model", PROPERTY_HINT_ENUM, "Default,Exponential"), "set_air_absorption_model_type", "get_air_absorption_model_type");

ADD_GROUP("Occlusion and Transmission", "");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "occlusion"), "set_occlusion_on", "is_occlusion_on");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "occlusion_radius", PROPERTY_HINT_RANGE, "0.0,20.0,0.1"), "set_occlusion_radius", "get_occlusion_radius");
Expand Down Expand Up @@ -291,6 +308,18 @@ void SteamAudioPlayer::set_max_reflection_dist(float p_max_reflection_dist) { cf

bool SteamAudioPlayer::is_dist_attn_on() { return cfg.is_dist_attn_on; }
void SteamAudioPlayer::set_dist_attn_on(bool p_dist_attn_on) { cfg.is_dist_attn_on = p_dist_attn_on; }

bool SteamAudioPlayer::is_air_absorp_on() { return cfg.is_air_absorp_on; }
void SteamAudioPlayer::set_air_absorp_on(bool p_air_absorp_on) { cfg.is_air_absorp_on = p_air_absorp_on; }
float SteamAudioPlayer::get_air_absorption_low() { return cfg.air_absorption_low; }
void SteamAudioPlayer::set_air_absorption_low(float p_air_absorption_low) { cfg.air_absorption_low = p_air_absorption_low; }
float SteamAudioPlayer::get_air_absorption_mid() { return cfg.air_absorption_mid; }
void SteamAudioPlayer::set_air_absorption_mid(float p_air_absorption_mid) { cfg.air_absorption_mid = p_air_absorption_mid; }
float SteamAudioPlayer::get_air_absorption_high() { return cfg.air_absorption_high; }
void SteamAudioPlayer::set_air_absorption_high(float p_air_absorption_high) { cfg.air_absorption_high = p_air_absorption_high; }
IPLAirAbsorptionModelType SteamAudioPlayer::get_air_absorption_model_type() { return cfg.air_absorption_model_type; }
void SteamAudioPlayer::set_air_absorption_model_type(IPLAirAbsorptionModelType p_air_absorption_model_type) { cfg.air_absorption_model_type = p_air_absorption_model_type; }

bool SteamAudioPlayer::is_reflection_on() { return cfg.is_reflection_on; }
void SteamAudioPlayer::set_reflection_on(bool p_reflection_on) { cfg.is_reflection_on = p_reflection_on; }
bool SteamAudioPlayer::is_occlusion_on() { return cfg.is_occlusion_on; }
Expand Down
19 changes: 18 additions & 1 deletion src/player.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#ifndef STEAM_AUDIO_PLAYER_H
#define STEAM_AUDIO_PLAYER_H

Expand Down Expand Up @@ -28,6 +27,11 @@ class SteamAudioPlayer : public AudioStreamPlayer3D {
1,
10000.0f,
false,
false,
0.9,
0.7,
0.5,
IPLAirAbsorptionModelType::IPL_AIRABSORPTIONTYPE_DEFAULT,
true,
true,
false
Expand Down Expand Up @@ -66,8 +70,21 @@ class SteamAudioPlayer : public AudioStreamPlayer3D {

bool is_dist_attn_on();
void set_dist_attn_on(bool p_dist_attn_on);

bool is_air_absorp_on();
void set_air_absorp_on(bool p_air_absorp_on);
float get_air_absorption_low();
void set_air_absorption_low(float p_air_absorption_low);
float get_air_absorption_mid();
void set_air_absorption_mid(float p_air_absorption_mid);
float get_air_absorption_high();
void set_air_absorption_high(float p_air_absorption_high);
IPLAirAbsorptionModelType get_air_absorption_model_type();
void set_air_absorption_model_type(IPLAirAbsorptionModelType p_air_absorption_model_type);

bool is_reflection_on();
void set_reflection_on(bool p_reflection_on);

bool is_occlusion_on();
void set_occlusion_on(bool p_occlusion_on);

Expand Down
7 changes: 7 additions & 0 deletions src/steam_audio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

using namespace godot;

VARIANT_ENUM_CAST(IPLAirAbsorptionModelType);

class SteamAudio {
public:
typedef enum {
Expand Down Expand Up @@ -48,6 +50,11 @@ struct SteamAudioSourceConfig {
int ambisonics_order;
float max_refl_dist;
bool is_dist_attn_on;
bool is_air_absorp_on;
float air_absorption_low;
float air_absorption_mid;
float air_absorption_high;
IPLAirAbsorptionModelType air_absorption_model_type;
bool is_ambisonics_on;
bool is_occlusion_on;
bool is_reflection_on;
Expand Down
22 changes: 22 additions & 0 deletions src/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ int32_t SteamAudioStreamPlayback::_mix(AudioFrame *buffer, double rate_scale, in
ls->bufs.in.data[1][i] = mixed_frames[i].y;
}

if (ls->cfg.is_air_absorp_on) {
IPLAirAbsorptionModel airAbsorptionModel{};
airAbsorptionModel.type = ls->cfg.air_absorption_model_type;

IPLVector3 sourcePosition = ipl_vec3_from(ls->src.player->get_global_transform().origin);
IPLVector3 listenerPosition = gs->listener_coords.origin;
float airAbsorption[3];
airAbsorption[0] = ls->cfg.air_absorption_low;
airAbsorption[1] = ls->cfg.air_absorption_mid;
airAbsorption[2] = ls->cfg.air_absorption_high;

iplAirAbsorptionCalculate(gs->ctx, sourcePosition, listenerPosition, &airAbsorptionModel, airAbsorption);

ls->direct_outputs.airAbsorption[0] = airAbsorption[0];
ls->direct_outputs.airAbsorption[1] = airAbsorption[1];
ls->direct_outputs.airAbsorption[2] = airAbsorption[2];

ls->direct_outputs.flags = static_cast<IPLDirectEffectFlags>(
ls->direct_outputs.flags |
IPL_DIRECTEFFECTFLAGS_APPLYAIRABSORPTION);
}

if (ls->cfg.is_dist_attn_on) {
ls->direct_outputs.flags = static_cast<IPLDirectEffectFlags>(
ls->direct_outputs.flags |
Expand Down