Skip to content

Commit

Permalink
Merge pull request #105 from SirBob01/feat/find-closest-listener
Browse files Browse the repository at this point in the history
Implement ListenerSet::find_closest()
  • Loading branch information
SirBob01 authored May 15, 2023
2 parents ba9a2f5 + bc863ba commit 8eb32c4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
19 changes: 2 additions & 17 deletions src/Sound/Jukebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ namespace Dynamo::Sound {
}

// Apply the filters
ListenerProperties &listener = find_closest_listener(material);
ListenerProperties &listener =
_listeners.find_closest(material.position);
for (const auto &filter : material.filters) {
transformed = filter->apply(transformed,
0,
Expand All @@ -189,22 +190,6 @@ namespace Dynamo::Sound {
chunk.frame += length;
}

ListenerProperties &
Jukebox::find_closest_listener(const DynamicMaterial &material) {
u32 closest_index = 0;
for (u32 i = 0; i < _listeners.size(); i++) {
Vec3 best = _listeners[i].position;
Vec3 curr = _listeners[closest_index].position;

f32 a = (best - material.position).length_squared();
f32 b = (curr - material.position).length_squared();
if (b < a) {
closest_index = i;
}
}
return _listeners[closest_index];
}

const std::vector<Device> Jukebox::get_devices() {
std::vector<Device> devices;
PaError err;
Expand Down
9 changes: 0 additions & 9 deletions src/Sound/Jukebox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,6 @@ namespace Dynamo::Sound {
*/
void process_chunk(Chunk<DynamicMaterial> &chunk);

/**
* @brief Find the closest listener to a sound
*
* @param material
* @return ListenerProperties&
*/
ListenerProperties &
find_closest_listener(const DynamicMaterial &material);

public:
/**
* @brief Construct a new Jukebox object
Expand Down
23 changes: 22 additions & 1 deletion src/Sound/Listener.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "../Types.hpp"
#include "../Math/Quaternion.hpp"
#include "../Math/Vec3.hpp"
#include "../Types.hpp"
#include "../Utils/IdTracker.hpp"
#include "../Utils/SparseSet.hpp"

Expand Down Expand Up @@ -125,5 +125,26 @@ namespace Dynamo::Sound {
inline ListenerProperties &operator[](u32 index) {
return _properties.at(index);
}

/**
* @brief Find the closest listener to a given position.
*
* @param position
* @return ListenerProperties&
*/
ListenerProperties &find_closest(Vec3 position) {
u32 closest_index = 0;
for (u32 i = 0; i < _properties.size(); i++) {
Vec3 best = _properties.at(i).position;
Vec3 curr = _properties.at(closest_index).position;

f32 a = (best - position).length_squared();
f32 b = (curr - position).length_squared();
if (b < a) {
closest_index = i;
}
}
return _properties.at(closest_index);
}
};
} // namespace Dynamo::Sound

0 comments on commit 8eb32c4

Please sign in to comment.