SAL
A C++ library for spatial audio.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
propagationline.h
Go to the documentation of this file.
1 /*
2  propagationline.h
3  Spatial Audio Library (SAL)
4  Copyright (c) 2011, Enzo De Sena
5  All rights reserved.
6 
7  Authors: Enzo De Sena, enzodesena@gmail.com
8 
9  */
10 
11 #ifndef SAL_PROPAGATIONLINE_H
12 #define SAL_PROPAGATIONLINE_H
13 
14 #include "delayfilter.h"
15 #include "iirfilter.h"
16 #include "point.h"
17 #include "firfilter.h"
18 #include "salconstants.h"
19 #include "salutilities.h"
20 
21 namespace sal {
22 
23 
24 
31 public:
32 
45  const sal::Time sampling_frequency,
46  const sal::Length max_distance = 100.0,
47  const sal::InterpolationType = sal::InterpolationType::kRounding,
48  const bool air_filters_active = false,
49  const bool allow_attenuation_larger_than_one = false,
50  const sal::Length reference_distance = kOneSampleDistance) noexcept;
51 
53  sal::Sample attenuation() const noexcept;
54 
55  sal::Length distance() const noexcept;
56 
58  void SetAttenuation(const sal::Sample,
59  const sal::Time ramp_time = 0.0) noexcept;
60 
61  inline sal::Time current_latency() const noexcept { return current_latency_; }
62 
63  inline sal::Time target_latency() const noexcept {
64  return latency_smoother_.target_value();
65  }
66 
67  void SetAirFiltersActive(const bool) noexcept;
68 
69  void Write(const sal::Sample &sample) noexcept;
70 
71  void Write(const Sample* samples, const Int num_samples) noexcept;
72 
74  inline sal::Sample Read() const noexcept {
75  if (interpolation_type_ == sal::InterpolationType::kLinear) {
76  return delay_filter_.FractionalReadAt(current_latency_) * current_attenuation_;
77  } else {
78  return delay_filter_.ReadAt(mcl::RoundToInt(current_latency_)) * current_attenuation_;
79  }
80  }
81 
85  void Read(const Int num_samples, Sample* output_data) const noexcept;
86 
87  void Tick() noexcept;
88 
90  void Tick(const Int num_samples) noexcept;
91 
98  void SetDistance(const sal::Length distance,
99  const sal::Time ramp_time = 0.0) noexcept;
100 
102  void Reset() noexcept;
103 
105 
106  static bool Test();
107 private:
108  sal::Time sampling_frequency_;
109  DelayFilter delay_filter_;
110  sal::Length reference_distance_;
112  bool allow_gain_;
113  sal::Sample current_attenuation_;
114  sal::Time current_latency_;
115  bool air_filters_active_;
116  mcl::FirFilter air_filter_;
117  sal::InterpolationType interpolation_type_;
118  RampSmoother attenuation_smoother_;
119  RampSmoother latency_smoother_;
120 
121  void Update() noexcept;
122  sal::Time ComputeLatency(const sal::Length) noexcept;
123  sal::Sample ComputeAttenuation(const sal::Length) noexcept;
124 
125  static std::vector<sal::Sample> GetAirFilter(sal::Length distance) noexcept;
126  static Sample SanitiseAttenuation(const sal::Sample attenuation);
127 };
128 
129 
130 } // namespace sal
131 
132 #endif