VISR  0.11.8
Versatile Interactive Scene Renderer
biquad_coefficient.hpp
Go to the documentation of this file.
1 /* Copyright Institute of Sound and Vibration Research - All rights reserved */
2 
3 #ifndef VISR_RBBL_BIQUAD_COEFFICIENT_HPP_INCLUDED
4 #define VISR_RBBL_BIQUAD_COEFFICIENT_HPP_INCLUDED
5 
6 #include <boost/property_tree/ptree_fwd.hpp>
7 
8 #include "export_symbols.hpp"
9 
10 #include <algorithm>
11 #include <array>
12 #include <initializer_list>
13 #include <iosfwd>
14 #include <stdexcept>
15 #include <vector>
16 
17 namespace visr
18 {
19 namespace rbbl
20 {
21 
29 template< typename CoeffType >
30 class VISR_RBBL_LIBRARY_SYMBOL BiquadCoefficient
31 {
32 public:
36  static const std::size_t cNumberOfCoeffs = 5;
37 
42  {
43  mCoeffs.fill( static_cast<CoeffType>(0.0) );
44  mCoeffs[0] = static_cast<CoeffType>(1.0);
45  }
46 
47  BiquadCoefficient( BiquadCoefficient<CoeffType> const & rhs ) = default;
48 
53  static BiquadCoefficient fromJson( boost::property_tree::ptree const & tree );
54 
55  static BiquadCoefficient fromJson( std::basic_istream<char> & stream );
56 
57  static BiquadCoefficient fromXml( boost::property_tree::ptree const & tree );
58 
59  static BiquadCoefficient fromXml( std::basic_istream<char> & stream );
61 
62  BiquadCoefficient( CoeffType b0, CoeffType b1, CoeffType b2,
63  CoeffType a1, CoeffType a2 )
64  {
65  mCoeffs[0] = b0;
66  mCoeffs[1] = b1;
67  mCoeffs[2] = b2;
68  mCoeffs[3] = a1;
69  mCoeffs[4] = a2;
70  }
71 
72  BiquadCoefficient( std::initializer_list< CoeffType > const & coeffs )
73  {
74  if( coeffs.size() != mCoeffs.size() )
75  {
76  throw std::invalid_argument( "BiquadCoefficient: Initialisation vector has wrong number of elements." );
77  }
78  std::copy( coeffs.begin(), coeffs.end(), mCoeffs.begin() );
79  }
80 
81  // BiquadCoefficient( const BiquadCoefficient & rhs ) = default;
82 
84  {
85  mCoeffs = rhs.mCoeffs;
86  return *this;
87  }
88 
89  CoeffType const * data() const { return &mCoeffs[0]; }
90 
91  CoeffType * data() { return &mCoeffs[0]; }
92 
93  CoeffType const & operator[]( std::size_t idx ) const
94  {
95  return mCoeffs[ idx ];
96  }
97 
98  CoeffType & operator[]( std::size_t idx )
99  {
100  return mCoeffs[ idx ];
101  }
102 
103  CoeffType const & at( std::size_t idx ) const
104  {
105  return mCoeffs.at( idx );
106  }
107 
108  CoeffType & at( std::size_t idx )
109  {
110  return mCoeffs.at( idx );
111  }
112 
113 
114  CoeffType const & b0() const { return mCoeffs[0]; }
115 
116  CoeffType const & b1() const { return mCoeffs[1]; }
117 
118  CoeffType const & b2() const { return mCoeffs[2]; }
119 
120  CoeffType const & a1() const { return mCoeffs[3]; }
121 
122  CoeffType const & a2() const { return mCoeffs[4]; }
123 
124 
125  CoeffType& b0() { return mCoeffs[0]; }
126 
127  CoeffType& b1() { return mCoeffs[1]; }
128 
129  CoeffType& b2() { return mCoeffs[2]; }
130 
131  CoeffType& a1() { return mCoeffs[3]; }
132 
133  CoeffType& a2() { return mCoeffs[4]; }
134 
135  void loadJson( boost::property_tree::ptree const & tree );
136 
137  void loadJson( std::basic_istream<char> & );
138 
139  void loadXml( boost::property_tree::ptree const & tree );
140 
141  void loadXml( std::basic_istream<char> & );
142 
147  void writeJson( boost::property_tree::ptree & tree ) const;
148 
149  void writeJson( std::basic_ostream<char> & stream ) const;
150 
151  void writeJson( std::string & str ) const;
152 
153  void writeXml( boost::property_tree::ptree & tree ) const;
154 
155  void writeXml ( std::basic_ostream<char> & stream ) const;
156 
157  void writeXml ( std::string & str ) const;
158 
160 private:
164  std::array<CoeffType,5> mCoeffs;
165 };
166 
167 template< class CoeffType >
168 class VISR_RBBL_LIBRARY_SYMBOL BiquadCoefficientList
169 {
170 public:
171  using Container = typename std::vector< BiquadCoefficient< CoeffType > >;
172  using iterator = typename Container::iterator;
173  using const_iterator = typename Container::const_iterator;
174 
178  BiquadCoefficientList() = default;
179 
180  explicit BiquadCoefficientList( const std::size_t initialSize )
181  : mBiquads( initialSize )
182  {
183  }
184 
188  BiquadCoefficientList( BiquadCoefficientList const & rhs ) = default;
189 
190  BiquadCoefficientList( std::initializer_list<BiquadCoefficient<CoeffType> > const & initList )
191  : mBiquads( initList )
192  {
193  }
194 
195  static BiquadCoefficientList fromJson( boost::property_tree::ptree const & tree );
196 
197  static BiquadCoefficientList fromJson( std::basic_istream<char> & stream );
198 
199  static BiquadCoefficientList fromJson( std::string const & str );
200 
201  static BiquadCoefficientList fromXml( boost::property_tree::ptree const & tree );
202 
203  static BiquadCoefficientList fromXml( std::basic_istream<char> & stream );
204 
205  static BiquadCoefficientList fromXml( std::string const & str );
206 
212  {
213  if( rhs.size() != size() )
214  {
215  std::invalid_argument( "BiquadCoefficientList: Size of argument to be assigned does not match." );
216  }
217  mBiquads = rhs.mBiquads;
218  return *this;
219  }
220 
221  std::size_t size() const { return mBiquads.size(); }
222 
223  void resize( std::size_t newSize )
224  {
225  mBiquads.resize( newSize, BiquadCoefficient<CoeffType>() );
226  }
227 
228  BiquadCoefficient<CoeffType> const & operator[]( std::size_t idx ) const { return mBiquads[idx]; }
229  BiquadCoefficient<CoeffType> & operator[]( std::size_t idx ) { return mBiquads[idx]; }
230 
231  BiquadCoefficient<CoeffType> const & at( std::size_t idx ) const { return mBiquads.at( idx ); }
232  BiquadCoefficient<CoeffType> & at( std::size_t idx ) { return mBiquads.at( idx ); }
233 
234  iterator begin() { return mBiquads.begin(); }
235  iterator end() { return mBiquads.end(); }
236 
237  const_iterator begin() const { return mBiquads.begin(); }
238  const_iterator end() const { return mBiquads.end(); }
239 
247  void loadJson( boost::property_tree::ptree const & tree );
248 
252  void loadJson( std::basic_istream<char> & stream );
253 
257  void loadJson( std::string const & str );
258 
262  void loadXml( boost::property_tree::ptree const & tree );
263 
267  void loadXml( std::basic_istream<char> & stream );
268 
272  void loadXml( std::string const & str );
274 
283  void writeJson( boost::property_tree::ptree & tree ) const;
284 
288  void writeJson( std::basic_ostream<char> & stream ) const;
289 
293  void writeJson( std::string & str ) const;
294 
299  void writeXml( boost::property_tree::ptree & tree ) const;
300 
304  void writeXml( std::basic_ostream<char> & stream ) const;
305 
309  void writeXml( std::string & str ) const;
311 
312 
313 private:
314  Container mBiquads;
315 };
316 
317 template<typename CoeffType >
318 class VISR_RBBL_LIBRARY_SYMBOL BiquadCoefficientMatrix
319 {
320 public:
321  explicit BiquadCoefficientMatrix( std::size_t numberOfFilters, std::size_t numberOfBiquads );
322 
323  ~BiquadCoefficientMatrix();
324 
325  static BiquadCoefficientMatrix fromJson( boost::property_tree::ptree const & tree );
326 
327  static BiquadCoefficientMatrix fromJson( std::basic_istream<char> & stream );
328 
329  static BiquadCoefficientMatrix fromJson( std::string const & str );
330 
331  static BiquadCoefficientMatrix fromXml( boost::property_tree::ptree const & tree );
332 
333  static BiquadCoefficientMatrix fromXml( std::basic_istream<char> & stream );
334 
335  static BiquadCoefficientMatrix fromXml( std::string const & str );
336 
337  std::size_t numberOfFilters() const { return mRows.size(); }
338  std::size_t numberOfSections() const { return mRows.empty() ? 0 : mRows[0].size(); }
339 
340  void resize( std::size_t numberOfFilters, std::size_t numberOfBiquads );
341  BiquadCoefficientList<CoeffType> const & operator[]( std::size_t rowIdx ) const { return mRows[rowIdx]; }
342  BiquadCoefficientList<CoeffType> & operator[]( std::size_t rowIdx ) { return mRows[rowIdx]; }
343 
344  BiquadCoefficient<CoeffType> const & operator()( std::size_t rowIdx, std::size_t colIdx ) const { return mRows[rowIdx][colIdx]; }
345  BiquadCoefficient<CoeffType> & operator()( std::size_t rowIdx, std::size_t colIdx ) { return mRows[rowIdx][colIdx]; }
346 
353  void setFilter( std::size_t filterIdx, BiquadCoefficientList<CoeffType> const & newFilter );
354 
362  void loadJson( boost::property_tree::ptree const & tree );
363 
367  void loadJson( std::basic_istream<char> & stream );
368 
372  void loadJson( std::string const & str );
373 
377  void loadXml( boost::property_tree::ptree const & tree );
378 
382  void loadXml( std::basic_istream<char> & stream );
383 
387  void loadXml( std::string const & str );
389 
398  void writeJson( boost::property_tree::ptree & tree ) const;
399 
403  void writeJson( std::basic_ostream<char> & stream ) const;
404 
408  void writeJson( std::string & str ) const;
409 
414  void writeXml( boost::property_tree::ptree & tree ) const;
415 
419  void writeXml( std::basic_ostream<char> & stream ) const;
420 
424  void writeXml( std::string & str ) const;
426 
427 private:
428  using ContainerType = std::vector< BiquadCoefficientList<CoeffType> >;
429 
430  ContainerType mRows;
431 };
432 
433 } // namespace rbbl
434 } // namespace visr
435 
436 #endif // VISR_RBBL_BIQUAD_COEFFICIENT_HPP_INCLUDED
typename std::vector< BiquadCoefficient< SampleType > > Container
Definition: biquad_coefficient.hpp:171
CoeffType & b2()
Definition: biquad_coefficient.hpp:129
BiquadCoefficientList(const std::size_t initialSize)
Definition: biquad_coefficient.hpp:180
const_iterator begin() const
Definition: biquad_coefficient.hpp:237
typename Container::iterator iterator
Definition: biquad_coefficient.hpp:172
iterator begin()
Definition: biquad_coefficient.hpp:234
CoeffType & a1()
Definition: biquad_coefficient.hpp:131
BiquadCoefficientList(std::initializer_list< BiquadCoefficient< CoeffType > > const &initList)
Definition: biquad_coefficient.hpp:190
BiquadCoefficient()
Definition: biquad_coefficient.hpp:41
CoeffType * data()
Definition: biquad_coefficient.hpp:91
CoeffType const & b2() const
Definition: biquad_coefficient.hpp:118
CoeffType const * data() const
Definition: biquad_coefficient.hpp:89
Definition: biquad_coefficient.hpp:30
CoeffType & a2()
Definition: biquad_coefficient.hpp:133
BiquadCoefficient(CoeffType b0, CoeffType b1, CoeffType b2, CoeffType a1, CoeffType a2)
Definition: biquad_coefficient.hpp:62
CoeffType & at(std::size_t idx)
Definition: biquad_coefficient.hpp:108
BiquadCoefficient< CoeffType > const & operator()(std::size_t rowIdx, std::size_t colIdx) const
Definition: biquad_coefficient.hpp:344
BiquadCoefficient(std::initializer_list< CoeffType > const &coeffs)
Definition: biquad_coefficient.hpp:72
Definition: options.cpp:10
BiquadCoefficient< CoeffType > const & at(std::size_t idx) const
Definition: biquad_coefficient.hpp:231
typename Container::const_iterator const_iterator
Definition: biquad_coefficient.hpp:173
CoeffType const & b0() const
Definition: biquad_coefficient.hpp:114
BiquadCoefficient< CoeffType > & operator()(std::size_t rowIdx, std::size_t colIdx)
Definition: biquad_coefficient.hpp:345
CoeffType & operator[](std::size_t idx)
Definition: biquad_coefficient.hpp:98
std::size_t size() const
Definition: biquad_coefficient.hpp:221
std::size_t numberOfFilters() const
Definition: biquad_coefficient.hpp:337
std::size_t numberOfSections() const
Definition: biquad_coefficient.hpp:338
void resize(std::size_t newSize)
Definition: biquad_coefficient.hpp:223
const_iterator end() const
Definition: biquad_coefficient.hpp:238
CoeffType const & at(std::size_t idx) const
Definition: biquad_coefficient.hpp:103
CoeffType & b1()
Definition: biquad_coefficient.hpp:127
Definition: biquad_coefficient.hpp:168
BiquadCoefficient< CoeffType > & operator[](std::size_t idx)
Definition: biquad_coefficient.hpp:229
BiquadCoefficientList & operator=(BiquadCoefficientList const &rhs)
Definition: biquad_coefficient.hpp:211
CoeffType const & b1() const
Definition: biquad_coefficient.hpp:116
BiquadCoefficient< CoeffType > & at(std::size_t idx)
Definition: biquad_coefficient.hpp:232
CoeffType const & a2() const
Definition: biquad_coefficient.hpp:122
BiquadCoefficient & operator=(BiquadCoefficient const &rhs)
Definition: biquad_coefficient.hpp:83
BiquadCoefficient< CoeffType > const & operator[](std::size_t idx) const
Definition: biquad_coefficient.hpp:228
iterator end()
Definition: biquad_coefficient.hpp:235
CoeffType & b0()
Definition: biquad_coefficient.hpp:125
BiquadCoefficientList< CoeffType > const & operator[](std::size_t rowIdx) const
Definition: biquad_coefficient.hpp:341
CoeffType const & a1() const
Definition: biquad_coefficient.hpp:120
BiquadCoefficientList< CoeffType > & operator[](std::size_t rowIdx)
Definition: biquad_coefficient.hpp:342
CoeffType const & operator[](std::size_t idx) const
Definition: biquad_coefficient.hpp:93