phonon: Source:get/setDirectivity;

This commit is contained in:
bjorn 2021-02-16 02:54:24 -07:00 committed by Bjorn
parent 66e574baac
commit ac96556a13
4 changed files with 59 additions and 5 deletions

View File

@ -157,6 +157,42 @@ static int l_lovrSourceSetAbsorption(lua_State* L) {
return 0;
}
static int l_lovrSourceGetDirectivity(lua_State* L) {
Source* source = luax_checktype(L, 1, Source);
float weight, power;
lovrSourceGetDirectivity(source, &weight, &power);
if (weight == 0.f && power == 0.f) {
lua_pushnil(L);
return 1;
}
lua_pushnumber(L, weight);
lua_pushnumber(L, power);
return 2;
}
static int l_lovrSourceSetDirectivity(lua_State* L) {
Source* source = luax_checktype(L, 1, Source);
float weight = 0.f;
float power = 0.f;
switch (lua_type(L, 2)) {
case LUA_TNONE:
case LUA_TNIL:
break;
case LUA_TBOOLEAN:
if (lua_toboolean(L, 2)) {
weight = .5f;
power = 1.f;
}
break;
default:
weight = luax_checkfloat(L, 2);
power = luax_checkfloat(L, 3);
break;
}
lovrSourceSetDirectivity(source, weight, power);
return 0;
}
static int l_lovrSourceGetFalloff(lua_State* L) {
Source* source = luax_checktype(L, 1, Source);
float falloff = lovrSourceGetFalloff(source);
@ -170,11 +206,10 @@ static int l_lovrSourceGetFalloff(lua_State* L) {
static int l_lovrSourceSetFalloff(lua_State* L) {
Source* source = luax_checktype(L, 1, Source);
float falloff;
float falloff = 0.f;
switch (lua_type(L, 2)) {
case LUA_TNONE:
case LUA_TNIL:
falloff = 0.f;
break;
case LUA_TBOOLEAN:
falloff = lua_toboolean(L, 2) ? 1.f : 0.f;
@ -205,6 +240,8 @@ const luaL_Reg lovrSource[] = {
{ "setPose", l_lovrSourceSetPose },
{ "getAbsorption", l_lovrSourceGetAbsorption },
{ "setAbsorption", l_lovrSourceSetAbsorption },
{ "getDirectivity", l_lovrSourceGetDirectivity },
{ "setDirectivity", l_lovrSourceSetDirectivity },
{ "getFalloff", l_lovrSourceGetFalloff },
{ "setFalloff", l_lovrSourceSetFalloff },
{ NULL, NULL }

View File

@ -35,6 +35,8 @@ struct Source {
float position[4];
float orientation[4];
float absorption[3];
float dipoleWeight;
float dipolePower;
float falloff;
bool playing;
bool looping;
@ -493,6 +495,16 @@ void lovrSourceSetAbsorption(Source* source, float absorption[3]) {
memcpy(source->absorption, absorption, sizeof(source->absorption));
}
void lovrSourceGetDirectivity(Source* source, float* weight, float* power) {
*weight = source->dipoleWeight;
*power = source->dipolePower;
}
void lovrSourceSetDirectivity(Source* source, float weight, float power) {
source->dipoleWeight = weight;
source->dipolePower = power;
}
float lovrSourceGetFalloff(Source* source) {
return source->falloff;
}

View File

@ -58,6 +58,8 @@ void lovrSourceGetPose(Source* source, float position[4], float orientation[4]);
void lovrSourceSetPose(Source* source, float position[4], float orientation[4]);
void lovrSourceGetAbsorption(Source* source, float absorption[3]);
void lovrSourceSetAbsorption(Source* source, float absorption[3]);
void lovrSourceGetDirectivity(Source* source, float* weight, float* power);
void lovrSourceSetDirectivity(Source* source, float weight, float power);
float lovrSourceGetFalloff(Source* source);
void lovrSourceSetFalloff(Source* source, float falloff);
intptr_t* lovrSourceGetSpatializerMemoField(Source* source);

View File

@ -131,6 +131,9 @@ uint32_t phonon_apply(Source* source, const float* input, float* output, uint32_
float absorption[3];
lovrSourceGetAbsorption(source, absorption);
float dipoleWeight;
float dipolePower;
lovrSourceGetDirectivity(source, &dipoleWeight, &dipolePower);
float falloff = lovrSourceGetFalloff(source);
IPLSource iplSource = {
@ -138,8 +141,8 @@ uint32_t phonon_apply(Source* source, const float* input, float* output, uint32_
.ahead = (IPLVector3) { forward[0], forward[1], forward[2] },
.up = (IPLVector3) { up[0], up[1], up[2] },
.right = (IPLVector3) { right[0], right[1], right[2] },
.directivity.dipoleWeight = 0.f,
.directivity.dipolePower = 0.f,
.directivity.dipoleWeight = dipoleWeight,
.directivity.dipolePower = dipolePower,
.distanceAttenuationModel.type = IPL_DISTANCEATTENUATION_INVERSEDISTANCE,
.distanceAttenuationModel.minDistance = falloff,
.airAbsorptionModel.type = IPL_AIRABSORPTION_EXPONENTIAL,
@ -152,8 +155,8 @@ uint32_t phonon_apply(Source* source, const float* input, float* output, uint32_
IPLDirectSoundEffectOptions options = {
.applyDistanceAttenuation = falloff > 0.f ? IPL_TRUE : IPL_FALSE,
.applyDirectivity = IPL_TRUE,
.applyAirAbsorption = (absorption[0] > 0.f || absorption[1] > 0.f || absorption[2] > 0.f) ? IPL_TRUE : IPL_FALSE,
.applyDirectivity = dipoleWeight > 0.f ? IPL_TRUE : IPL_FALSE,
.directOcclusionMode = occlusionMode
};