Support dynamically changing 3d sound source position
This commit is contained in:
parent
28fbe8daf8
commit
4490123a64
2 changed files with 28 additions and 7 deletions
|
|
@ -14,7 +14,13 @@ namespace psemek::audio
|
|||
{
|
||||
void set_position(geom::point<float, 3> const & position);
|
||||
|
||||
virtual std::shared_ptr<effect> make_effect(geom::point<float, 3> const & position, float distance_factor);
|
||||
struct mono_effect
|
||||
: effect
|
||||
{
|
||||
virtual void set_position(geom::point<float, 3> const & position) = 0;
|
||||
};
|
||||
|
||||
virtual std::shared_ptr<mono_effect> make_effect(geom::point<float, 3> const & position, float distance_factor);
|
||||
|
||||
private:
|
||||
geom::point<float, 3> position_;
|
||||
|
|
|
|||
|
|
@ -10,10 +10,10 @@ namespace psemek::audio
|
|||
namespace
|
||||
{
|
||||
|
||||
struct mono_impl
|
||||
: effect
|
||||
struct mono_effect_impl
|
||||
: listener_3d_mono::mono_effect
|
||||
{
|
||||
mono_impl(geom::point<float, 3> const & source_position, geom::point<float, 3> & target_position, std::mutex & target_position_mutex, float distance_factor)
|
||||
mono_effect_impl(geom::point<float, 3> const & source_position, geom::point<float, 3> & target_position, std::mutex & target_position_mutex, float distance_factor)
|
||||
: source_position_(source_position)
|
||||
, target_position_(target_position)
|
||||
, cached_target_position_(target_position)
|
||||
|
|
@ -37,15 +37,30 @@ namespace psemek::audio
|
|||
cached_target_position_ = target_position_;
|
||||
}
|
||||
|
||||
float distance = geom::distance(cached_target_position_, source_position_) / distance_factor_;
|
||||
{
|
||||
std::unique_lock lock{source_position_mutex_, std::try_to_lock};
|
||||
if (lock.owns_lock())
|
||||
cached_source_position_ = source_position_;
|
||||
}
|
||||
|
||||
float distance = geom::distance(cached_target_position_, cached_source_position_) / distance_factor_;
|
||||
float factor = std::min(1.f, 1.f / (1.f + distance * distance));
|
||||
|
||||
for (std::size_t i = 0; i < count; ++i)
|
||||
data[i] = pack(unpack(data[i]) * factor);
|
||||
}
|
||||
|
||||
void set_position(geom::point<float, 3> const & position) override
|
||||
{
|
||||
std::lock_guard lock{source_position_mutex_};
|
||||
source_position_ = position;
|
||||
}
|
||||
|
||||
private:
|
||||
std::mutex source_position_mutex_;
|
||||
geom::point<float, 3> source_position_;
|
||||
geom::point<float, 3> cached_source_position_;
|
||||
|
||||
geom::point<float, 3> & target_position_;
|
||||
geom::point<float, 3> cached_target_position_;
|
||||
std::mutex & target_position_mutex_;
|
||||
|
|
@ -60,9 +75,9 @@ namespace psemek::audio
|
|||
position_ = position;
|
||||
}
|
||||
|
||||
std::shared_ptr<effect> listener_3d_mono::make_effect(const geom::point<float, 3> &position, float distance_factor)
|
||||
std::shared_ptr<listener_3d_mono::mono_effect> listener_3d_mono::make_effect(const geom::point<float, 3> &position, float distance_factor)
|
||||
{
|
||||
return std::make_shared<mono_impl>(position, position_, position_mutex_, distance_factor);
|
||||
return std::make_shared<mono_effect_impl>(position, position_, position_mutex_, distance_factor);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue