Skip to content

Commit

Permalink
hunt down PannerNode issue introduced much earlier, producing incorre…
Browse files Browse the repository at this point in the history
…ctly HRTF

spatialized audio (forward vec was incorrectly set on the AudioListener)
  • Loading branch information
ddiakopoulos committed Jan 5, 2018
1 parent 0dd4fed commit d513807
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 27 deletions.
4 changes: 3 additions & 1 deletion examples/src/Spatialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ struct SpatializationApp : public LabSoundExampleApp
trainNode->setLooping(true);
context->listener()->setPosition(0, 0, 0);
panner->setVelocity(4, 0, 0);



const int seconds = 10;
float halfTime = seconds * 0.5f;
for (float i = 0; i < seconds; i += 0.01f)
Expand All @@ -45,6 +46,7 @@ struct SpatializationApp : public LabSoundExampleApp
// Put position a +up && +front, because if it goes right through the
// listener at (0, 0, 0) it abruptly switches from left to right.
panner->setPosition(x, 0.1f, 0.1f);

std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
Expand Down
12 changes: 6 additions & 6 deletions include/LabSound/core/AudioListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace lab
class AudioListener
{
// Position / Orientation
FloatPoint3D m_position = {0, 0, 0};
FloatPoint3D m_orientation {0, 0, 0};
FloatPoint3D m_upVector = {0, 1, 0};
FloatPoint3D m_position {0, 0, 0};
FloatPoint3D m_orientation {0, 0, -1}; // forward vector
FloatPoint3D m_upVector {-1, 1, 0};

FloatPoint3D m_velocity = {0, 0, 0};

Expand All @@ -29,7 +29,7 @@ namespace lab

// Position
void setPosition(float x, float y, float z) { setPosition(FloatPoint3D(x, y, z)); }
void setPosition(const FloatPoint3D &position) { m_position = position; }
void setPosition(const FloatPoint3D & position) { m_position = position; }
const FloatPoint3D & position() const { return m_position; }

// Orientation
Expand All @@ -38,8 +38,8 @@ namespace lab
setOrientation(FloatPoint3D(x, y, z));
setUpVector(FloatPoint3D(upX, upY, upZ));
}
void setOrientation(const FloatPoint3D &orientation) { m_orientation = orientation; }
const FloatPoint3D& orientation() const { return m_orientation; }
void setOrientation(const FloatPoint3D & orientation) { m_orientation = orientation; }
const FloatPoint3D & orientation() const { return m_orientation; }

// Up-vector
void setUpVector(const FloatPoint3D &upVector) { m_upVector = upVector; }
Expand Down
14 changes: 7 additions & 7 deletions include/LabSound/core/PannerNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ class PannerNode : public AudioNode

// Position
FloatPoint3D position() const { return m_position; }
void setPosition(float x, float y, float z) { m_position = FloatPoint3D(x, y, z); }
void setPosition(float x, float y, float z) { m_position = { x, y, z }; }

// Orientation
FloatPoint3D orientation() const { return m_position; }
void setOrientation(float x, float y, float z) { m_orientation = FloatPoint3D(x, y, z); }
FloatPoint3D orientation() const { return m_orientation; }
void setOrientation(float x, float y, float z) { m_orientation = { x, y, z }; }

// Velocity
FloatPoint3D velocity() const { return m_velocity; }
void setVelocity(float x, float y, float z) { m_velocity = FloatPoint3D(x, y, z); }
void setVelocity(float x, float y, float z) { m_velocity = { x, y, z }; }

// Distance parameters
unsigned short distanceModel();
Expand Down Expand Up @@ -115,9 +115,9 @@ class PannerNode : public AudioNode

PanningMode m_panningModel;

FloatPoint3D m_position;
FloatPoint3D m_orientation;
FloatPoint3D m_velocity;
FloatPoint3D m_position{ 0, 0, 0 };
FloatPoint3D m_orientation{ 0, 0, 0 };
FloatPoint3D m_velocity{ 0, 0, 0 };

std::shared_ptr<AudioParam> m_distanceGain;
std::shared_ptr<AudioParam> m_coneGain;
Expand Down
16 changes: 3 additions & 13 deletions src/core/PannerNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ PannerNode::PannerNode(float sampleRate, const std::string & searchPath) : Audio
m_params.push_back(m_distanceGain);
m_params.push_back(m_coneGain);

m_position = FloatPoint3D(0, 0, 0);
m_orientation = FloatPoint3D(0, 0, 0);
m_velocity = FloatPoint3D(0, 0, 0);

// Node-specific default mixing rules.
m_channelCount = 2;
m_channelCountMode = ChannelCountMode::ClampedMax;
Expand Down Expand Up @@ -181,9 +177,7 @@ void PannerNode::reset(ContextRenderLock&)

std::shared_ptr<AudioListener> PannerNode::listener(ContextRenderLock& r)
{
if (!r.context())
return nullptr;

if (!r.context()) return nullptr;
return r.context()->listener();
}

Expand Down Expand Up @@ -235,7 +229,7 @@ void PannerNode::getAzimuthElevation(ContextRenderLock& r, double* outAzimuth, d

// Calculate the source-listener vector
FloatPoint3D listenerPosition = listener(r)->position();
FloatPoint3D sourceListener = m_position - listenerPosition;
FloatPoint3D sourceListener = normalize(m_position - listenerPosition);

if (is_zero(sourceListener))
{
Expand All @@ -245,19 +239,15 @@ void PannerNode::getAzimuthElevation(ContextRenderLock& r, double* outAzimuth, d
return;
}

sourceListener = normalize(sourceListener);

// Align axes
FloatPoint3D listenerFront = normalize(listener(r)->orientation());
FloatPoint3D listenerUp = listener(r)->upVector();
FloatPoint3D listenerRight = normalize(cross(listenerFront, listenerUp));

FloatPoint3D up = cross(listenerRight, listenerFront);

float upProjection = dot(sourceListener, up);

FloatPoint3D projectedSource = sourceListener - upProjection * up;
projectedSource = normalize(projectedSource);
FloatPoint3D projectedSource = normalize(sourceListener - upProjection * up);

azimuth = 180.0 * acos(dot(projectedSource, listenerRight)) / piDouble;
fixNANs(azimuth); // avoid illegal values
Expand Down

0 comments on commit d513807

Please sign in to comment.