Wayverb
attenuator.h
1 #pragma once
2 
3 #include "raytracer/cl/structs.h"
4 
5 #include "core/attenuator/hrtf.h"
6 #include "core/attenuator/microphone.h"
7 #include "core/attenuator/null.h"
8 #include "core/conversions.h"
9 
10 #include "utilities/map_to_vector.h"
11 #include "utilities/mapping_iterator_adapter.h"
12 
13 #include "glm/glm.hpp"
14 
15 namespace wayverb {
16 namespace raytracer {
17 
18 template <size_t channels>
19 auto attenuate(const core::attenuator::null& null,
20  const glm::vec3& position,
21  const impulse<channels>& i) {
22  return make_attenuated_impulse(i.volume, i.distance);
23 }
24 
27 template <size_t channels>
28 auto attenuate(const core::attenuator::microphone& mic,
29  const glm::vec3& position,
30  const impulse<channels>& i) {
31  const auto dir = core::to_vec3{}(i.position) - position;
32  const auto att = attenuation(mic, dir);
33  return make_attenuated_impulse(i.volume * att, i.distance);
34 }
35 
39 template <size_t channels>
40 auto attenuate(const core::attenuator::hrtf& hrtf,
41  const glm::vec3& position,
42  const impulse<channels>& i) {
43  const auto adjusted_listener_position = get_ear_position(hrtf, position);
44 
45  const auto impulse_position = core::to_vec3{}(i.position);
46 
47  const auto dir = impulse_position - adjusted_listener_position;
48  const auto att = attenuation(hrtf, dir);
49 
50  return make_attenuated_impulse(
51  i.volume * att,
52  glm::distance(impulse_position, adjusted_listener_position));
53 }
54 
55 template <typename Method>
56 struct attenuate_mapper final {
57  Method method;
58  glm::vec3 position;
59 
60  template <typename T>
61  auto operator()(const T& t) const {
62  return attenuate(method, position, t);
63  }
64 };
65 
66 template <typename Method>
67 auto make_attenuate_mapper(Method method, const glm::vec3& position) {
68  return attenuate_mapper<Method>{std::move(method), position};
69 }
70 
71 template <typename It, typename Method>
72 auto make_attenuator_iterator(It it, Method method, const glm::vec3& position) {
73  return util::make_mapping_iterator_adapter(
74  std::move(it), make_attenuate_mapper(std::move(method), position));
75 }
76 
77 } // namespace raytracer
78 } // namespace wayverb
Definition: pressure.h:22
Definition: dir.h:9
Definition: capsule_base.h:9
Definition: attenuator.h:56