SAL
A C++ library for spatial audio.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
binauralmic.h
Go to the documentation of this file.
1 /*
2  binauralmic.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_BINAURALMIC_H
12 #define SAL_BINAURALMIC_H
13 
14 #include <map>
15 #include <vector>
16 #include "microphone.h"
17 #include "saltypes.h"
18 #include "array.h"
19 #include "salconstants.h"
20 #include "firfilter.h"
21 
22 namespace sal {
23 
24 enum class HeadRefOrientation {
25  standard, // Head facing positive x-axis; positive z-axis passing through jaw and then scalp
26  y_z // Head facing positive y-axis; positive z-axis passing through jaw and then scalp
27 };
28 
29 class BinauralMicInstance;
30 
31 class BinauralMic : public StereoMicrophone {
32 public:
37  BinauralMic(const mcl::Point& position,
38  const mcl::Quaternion orientation,
39  const Int update_length,
40  const HeadRefOrientation reference_orientation = HeadRefOrientation::standard);
41 
42  void SetUpdateLength(Int update_length) noexcept { update_length_ = update_length; }
43 
45  void SetBypass(bool bypass) noexcept;
46 
47  virtual void Reset() noexcept;
48 
49  bool IsCoincident() const noexcept { return true; }
50 
51  Int num_channels() const noexcept { return 2; }
52 
53  virtual ~BinauralMic() {}
54 
55 
56  virtual void AddPlaneWaveRelative(const Sample* signal,
57  const Int num_samples,
58  const mcl::Point& point,
59  const Int wave_id,
60  Buffer& output_buffer) noexcept;
61 
62 private:
63 
68  virtual Signal GetBrir(const Ear ear, const mcl::Point& point) noexcept = 0;
69 
70  void CreateInstanceIfNotExist(const Int wave_id) noexcept;
71 
72  std::map<UInt, BinauralMicInstance> instances_;
73 
75  Int update_length_;
76 
78  bool bypass_;
79 
80  friend class BinauralMicInstance;
81 
82 protected:
83 
85 };
86 
87 
88 
90 public:
91  DatabaseBinauralMic(const mcl::Point& position,
92  const mcl::Quaternion orientation,
93  const Int update_length,
94  const HeadRefOrientation reference_orientation = HeadRefOrientation::standard);
95 
100  void FilterAll(mcl::DigitalFilter* filter);
101 
102  virtual ~DatabaseBinauralMic() {}
103 protected:
104  // Database
105  std::vector<std::vector<Signal> > hrtf_database_right_;
106  std::vector<std::vector<Signal> > hrtf_database_left_;
107 };
108 
109 
110 
111 
113 private:
114  BinauralMicInstance(BinauralMic* base_mic, sal::Int update_length,
115  const HeadRefOrientation reference_orientation = HeadRefOrientation::standard) :
116  previous_point_(mcl::Point(NAN, NAN, NAN)),
117  base_mic_(base_mic),
118  filter_left_(mcl::FirFilter::GainFilter(1.0)),
119  filter_right_(mcl::FirFilter::GainFilter(1.0)),
120  update_length_(update_length),
121  reference_orientation_(reference_orientation) {}
122 
123  void AddPlaneWaveRelative(const Sample* input_data,
124  const Int num_samples,
125  const mcl::Point& point,
126  Buffer& output_buffer) noexcept;
127 
128  void UpdateFilter(const mcl::Point& point) noexcept;
129 
136  mcl::Point previous_point_;
137 
138  BinauralMic* base_mic_;
139  mcl::FirFilter filter_left_;
140  mcl::FirFilter filter_right_;
141  sal::Int update_length_;
142  HeadRefOrientation reference_orientation_;
143 
144  friend class BinauralMic;
145 };
146 
147 
148 } // namespace sal
149 
150 #endif