Essentia  2.1-beta5-dev
parameter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2016 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Essentia
5  *
6  * Essentia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef ESSENTIA_PARAMETER_H
21 #define ESSENTIA_PARAMETER_H
22 
23 #include <map>
24 #include <vector>
25 #include "types.h"
26 #include "utils/tnt/tnt_array2d.h"
27 
28 namespace essentia {
29 
30 class ESSENTIA_API Parameter {
31 
32  public:
33 
34  enum ParamType {
36 
40  INT,
42 
48 
52 
54 
59 
60  MATRIX_REAL
61  };
62 
63  private:
64 
66 
67  std::string _str;
69  bool _boolean;
70  std::vector<Parameter*> _vec;
71  std::map<std::string, Parameter*> _map;
74 
75  public:
76 
77  // Constructor for just declaring type (not providing a value)
78  Parameter(ParamType tp) : _type(tp), _configured(false) {}
79 
80  // Constructor for simple parameters
81  #define SPECIALIZE_CTOR(valueType, paramType, mName) \
82  Parameter (const valueType& x) : _type(paramType), _##mName(x), _configured(true) {}
83 
84  SPECIALIZE_CTOR(std::string, STRING, str);
85  SPECIALIZE_CTOR(Real, REAL, real);
86  SPECIALIZE_CTOR(bool, BOOL, boolean);
87  SPECIALIZE_CTOR(int, INT, real);
88  SPECIALIZE_CTOR(double, REAL, real);
89  SPECIALIZE_CTOR(uint, INT, real);
90  SPECIALIZE_CTOR(StereoSample, STEREOSAMPLE, ssamp);
91 
92  Parameter(const char* x) : _type(STRING), _str(x), _configured(true) {}
93 
94  // Constructor for vector parameters
95  #define SPECIALIZE_VECTOR_CTOR(valueType, paramType) \
96  Parameter(const std::vector<valueType>& v) : _type(paramType), _configured(true) {\
97  _vec.resize(v.size()); \
98  for (int i=0; i<int(v.size()); ++i) { _vec[i] = new Parameter(v[i]); } \
99  }
100 
102  SPECIALIZE_VECTOR_CTOR(std::string, VECTOR_STRING);
103  SPECIALIZE_VECTOR_CTOR(bool, VECTOR_BOOL);
104  SPECIALIZE_VECTOR_CTOR(int, VECTOR_INT);
105  SPECIALIZE_VECTOR_CTOR(StereoSample, VECTOR_STEREOSAMPLE);
106  SPECIALIZE_VECTOR_CTOR(std::vector<Real>, VECTOR_VECTOR_REAL);
107  SPECIALIZE_VECTOR_CTOR(std::vector<std::string>, VECTOR_VECTOR_STRING);
108  SPECIALIZE_VECTOR_CTOR(std::vector<StereoSample>, VECTOR_VECTOR_STEREOSAMPLE);
110 
111  // Constructor for map parameters
112  #define SPECIALIZE_MAP_CTOR(valueType, paramType) \
113  Parameter(const std::map<std::string, valueType>& m) : _type(paramType), _configured(true) { \
114  for (std::map<std::string, valueType>::const_iterator i = m.begin(); \
115  i != m.end(); \
116  ++i) { _map[(*i).first] = new Parameter((*i).second); } \
117  }
118 
119  SPECIALIZE_MAP_CTOR(std::vector<std::string>, MAP_VECTOR_STRING);
120  SPECIALIZE_MAP_CTOR(std::vector<Real>, MAP_VECTOR_REAL);
121  SPECIALIZE_MAP_CTOR(std::vector<int>, MAP_VECTOR_INT);
123 
124  // Constructor for TNT::Array2D aka MATRIX parameters
125  #define SPECIALIZE_MATRIX_CTOR(valueType, innerType) \
126  Parameter(const TNT::Array2D<valueType>& mat) : _type(MATRIX_##innerType), _configured(true) { \
127  _vec.resize(mat.dim1()); \
128  for (int i=0; i<mat.dim1(); ++i) { \
129  _vec[i] = new Parameter(VECTOR_##innerType); \
130  _vec[i]->_configured = true; \
131  _vec[i]->_vec.resize(mat.dim2()); \
132  for (int j=0; j<mat.dim2(); ++j) { \
133  _vec[i]->_vec[j] = new Parameter(mat[i][j]); \
134  } \
135  } \
136  }
137 
139 
140  Parameter(const Parameter& p);
141 
142  // also define ctor with a ptr, which allows a nice trick: we can now construct
143  // a Parameter from a map<string, Parameter*> which do not necessarily have the
144  // same type
145  Parameter(const Parameter* p);
146  ~Parameter();
147 
148  void clear();
149 
150  Parameter& operator=(const Parameter& p);
151  bool operator==(const Parameter& p) const;
152  bool operator!=(const Parameter& p) const;
153  ParamType type() const { return _type; }
154  bool isConfigured() const { return _configured; }
155 
156 
157 
158  std::string toString(int precision = 12) const;
159  std::string toLower() const;
160 
161  #define TO(fname, valueType, paramType, mName) \
162  valueType to##fname() const { \
163  if (!_configured) \
164  throw EssentiaException("Parameter: parameter has not been configured yet (ParamType=", _type, ")"); \
165  if (_type != paramType) \
166  throw EssentiaException("Parameter: parameter is not a " #valueType ", it is a ", _type); \
167  \
168  return (valueType)_##mName; \
169  }
170 
171  TO(Bool, bool, BOOL, boolean)
172  TO(Double, double, REAL, real)
173  TO(Float, float, REAL, real)
174  TO(StereoSample, StereoSample, STEREOSAMPLE, ssamp)
175 
176  int toInt() const {
177  if (!_configured)
178  throw EssentiaException("Parameter: parameter has not been configured yet (ParamType=", _type, ")");
179  if (_type != INT && _type != REAL)
180  throw EssentiaException("Parameter: parameter is not an int nor a Real, it is a ", _type);
181 
182  return (int)_real;
183  }
184 
185  // special case for toReal because it can return an int or a real
186  Real toReal() const {
187  if (!_configured)
188  throw EssentiaException("Parameter: parameter has not been configured yet (ParamType=", _type, ")");
189  if (_type != INT && _type != REAL)
190  throw EssentiaException("Parameter: parameter is not an int nor a Real, it is a ", _type);
191 
192  return _real;
193  }
194 
195  #define TOVECTOR(fname, valueType, paramType) \
196  std::vector<valueType > toVector##fname() const { \
197  if (!_configured) \
198  throw EssentiaException("Parameter: parameter has not been configured yet (ParamType=", _type, ")"); \
199  if (_type != paramType) \
200  throw EssentiaException("Parameter: parameter is not of type: ", paramType); \
201  \
202  std::vector<valueType > result(_vec.size()); \
203  for (int i=0; i<(int)_vec.size(); ++i) { \
204  result[i] = _vec[i]->to##fname(); \
205  } \
206  return result; \
207  }
208 
209  TOVECTOR(Real, Real, VECTOR_REAL)
210  TOVECTOR(String, std::string, VECTOR_STRING)
211  TOVECTOR(Int, int, VECTOR_INT)
212  TOVECTOR(Bool, bool, VECTOR_BOOL)
213  TOVECTOR(StereoSample, StereoSample, VECTOR_STEREOSAMPLE)
214  TOVECTOR(VectorReal, std::vector<Real>, VECTOR_VECTOR_REAL)
215  TOVECTOR(VectorString, std::vector<std::string>, VECTOR_VECTOR_STRING)
216  TOVECTOR(VectorStereoSample, std::vector<StereoSample>, VECTOR_VECTOR_STEREOSAMPLE)
217  TOVECTOR(MatrixReal, TNT::Array2D<Real>, VECTOR_MATRIX_REAL)
218 // TOVECTOR(MatrixInt, TNT::Array2D<int>, VECTOR_MATRIX_INT)
219 
220  #define TOMAP(fname, valueType, paramType) \
221  std::map<std::string, valueType > toMap##fname() const { \
222  if (!_configured) \
223  throw EssentiaException("Parameter: parameter has not been configured yet (ParamType=", _type, ")"); \
224  if (_type != paramType) \
225  throw EssentiaException("Parameter: parameter is not of type: ", paramType); \
226  \
227  std::map<std::string, valueType > result; \
228  \
229  for (std::map<std::string, Parameter*>::const_iterator i = _map.begin(); \
230  i != _map.end(); \
231  ++i) { \
232  result[i->first] = i->second->to##fname(); \
233  } \
234  \
235  return result; \
236  }
237 
238  TOMAP(VectorReal, std::vector<Real>, MAP_VECTOR_REAL)
239  TOMAP(VectorString, std::vector<std::string>, MAP_VECTOR_STRING)
240  TOMAP(VectorInt, std::vector<int>, MAP_VECTOR_INT)
241  TOMAP(Real, Real, MAP_REAL)
242 // TOMAP(String, std::string, MAP_STRING)
243 // TOMAP(Int, int, MAP_INT)
244 // TOMAP(Bool, bool, MAP_BOOL)
245 // TOMAP(StereoSample, StereoSample, MAP_STEREOSAMPLE)
246 
247  #define TOMATRIX(fname, valueType, paramType) \
248  TNT::Array2D<valueType> toMatrix##fname() const { \
249  if (!_configured) \
250  throw EssentiaException("Parameter: parameter has not been configured yet (ParamType=", _type, ")");\
251  if (_type != paramType) \
252  throw EssentiaException("Parameter: parameter is not of type: ", paramType);\
253  TNT::Array2D<valueType> result(_vec.size(), _vec[0]->_vec.size()); \
254  \
255  for (int i=0; i<result.dim1(); ++i) { \
256  for (int j=0; j<result.dim2(); ++j) { \
257  result[i][j] = _vec[i]->_vec[j]->to##fname(); \
258  } \
259  } \
260  return result; \
261  }
262 
263  TOMATRIX(Real, Real, MATRIX_REAL)
264 // TOMATRIX(String, std::string, MATRIX_STRING)
265 // TOMATRIX(Int, int, MATRIX_INT)
266 // TOMATRIX(Bool, bool, MATRIX_BOOL)
267 
268 };
269 
276 class ParameterMap : public EssentiaMap<std::string, Parameter, string_cmp> {
277 
278  protected:
280 
281  public:
282  void add(const std::string& name, const Parameter& value);
283 
284  const Parameter& operator[](const std::string& key) const;
285  Parameter& operator[](const std::string& key);
286 
287 };
288 
289 std::ostream& operator<<(std::ostream& out, const Parameter& p);
290 std::ostream& operator<<(std::ostream& out, const ParameterMap& m);
291 std::ostream& operator<<(std::ostream& out, const Parameter::ParamType& t);
292 
293 } // namespace essentia
294 
295 #endif // ESSENTIA_PARAMETER_H
Real toReal() const
Definition: parameter.h:186
Definition: parameter.h:35
Definition: parameter.h:276
Definition: parameter.h:38
Definition: tnt_array1d.h:35
std::map< std::string, Parameter * > _map
Definition: parameter.h:71
Definition: parameter.h:55
std::string _str
Definition: parameter.h:67
bool _configured
Definition: parameter.h:73
ParamType type() const
Definition: parameter.h:153
Definition: types.h:189
Parameter(ParamType tp)
Definition: parameter.h:78
Parameter(const char *x)
Definition: parameter.h:92
EssentiaMap< std::string, Parameter, string_cmp > ParameterMapBase
Definition: parameter.h:279
std::vector< Parameter * > _vec
Definition: parameter.h:70
#define TO(fname, valueType, paramType, mName)
Definition: parameter.h:161
Definition: parameter.h:44
Definition: parameter.h:41
bool _boolean
Definition: parameter.h:69
Definition: parameter.h:45
ParamType
Definition: parameter.h:34
float Real
Definition: types.h:68
Definition: parameter.h:49
std::ostream & operator<<(std::ostream &out, const Parameter &p)
Definition: parameter.h:40
Definition: parameter.h:37
Definition: parameter.h:43
std::string toLower(const std::string &str)
Definition: parameter.h:58
StereoSample _ssamp
Definition: parameter.h:72
Definition: parameter.h:30
Definition: algorithm.h:28
Definition: parameter.h:46
#define TOVECTOR(fname, valueType, paramType)
Definition: parameter.h:195
Definition: types.h:76
#define SPECIALIZE_CTOR(valueType, paramType, mName)
Definition: parameter.h:81
unsigned int uint
Definition: types.h:48
Definition: parameter.h:39
Definition: parameter.h:57
bool isConfigured() const
Definition: parameter.h:154
ParamType _type
Definition: parameter.h:65
#define SPECIALIZE_VECTOR_CTOR(valueType, paramType)
Definition: parameter.h:95
#define SPECIALIZE_MAP_CTOR(valueType, paramType)
Definition: parameter.h:112
#define TOMAP(fname, valueType, paramType)
Definition: parameter.h:220
Definition: parameter.h:56
Real _real
Definition: parameter.h:68
Definition: parameter.h:53
#define SPECIALIZE_MATRIX_CTOR(valueType, innerType)
Definition: parameter.h:125
#define TOMATRIX(fname, valueType, paramType)
Definition: parameter.h:247