Essentia  2.1-beta6-dev
types.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 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_TYPES_H
21 #define ESSENTIA_TYPES_H
22 
23 #include <map>
24 #include <vector>
25 #include <cctype>
26 #include <cassert>
27 #include <sstream>
28 #include <typeinfo>
29 #include <string.h>
30 #include "config.h"
31 #include "debugging.h"
32 #include "streamutil.h"
33 #include <unsupported/Eigen/CXX11/Tensor>
34 
35 
36 // fixed-size int types
37 
38 #ifndef OS_WIN32
39 
40 #include <inttypes.h>
41 
42 typedef uint16_t uint16;
43 typedef uint32_t uint32;
44 typedef uint64_t uint64;
45 typedef int16_t sint16;
46 typedef int32_t sint32;
47 typedef int64_t sint64;
48 
49 typedef unsigned int uint;
50 
51 #else // OS_WIN32
52 
53 typedef unsigned __int16 uint16;
54 typedef unsigned __int32 uint32;
55 typedef unsigned __int64 uint64;
56 typedef __int16 sint16;
57 typedef __int32 sint32;
58 typedef __int64 sint64;
59 
60 #endif // OS_WIN32
61 
62 
63 
64 namespace essentia {
65 
69 typedef float Real;
70 
71 
77 class EssentiaException : public std::exception {
78 
79  public:
80  EssentiaException(const char* msg) : exception(), _msg(msg) {}
81  EssentiaException(const std::string& msg) : exception(), _msg(msg) {}
82  EssentiaException(const std::ostringstream& msg) : exception(), _msg(msg.str()) {}
83 
84  template <typename T, typename U>
85  EssentiaException(const T& a, const U& b) : exception() {
86  std::ostringstream oss; oss << a << b; _msg = oss.str();
87  }
88 
89  template <typename T, typename U, typename V>
90  EssentiaException(const T& a, const U& b, const V& c) : exception() {
91  std::ostringstream oss; oss << a << b << c; _msg = oss.str();
92  }
93 
94  template <typename T, typename U, typename V, typename W>
95  EssentiaException(const T& a, const U& b, const V& c, const W& d) : exception() {
96  std::ostringstream oss; oss << a << b << c << d; _msg = oss.str();
97  }
98 
99  virtual ~EssentiaException() throw() {}
100  virtual const char* what() const throw() { return _msg.c_str(); }
101 
102  protected:
103  std::string _msg;
104 
105 };
106 
107 
111 inline bool case_insensitive_char_cmp(char a, char b) {
112  return std::tolower(a) < std::tolower(b);
113 }
114 
119  : public std::binary_function<const std::string&, const std::string&, bool> {
120  bool operator()(const std::string& str1, const std::string& str2) const {
121  return std::lexicographical_compare(str1.begin(), str1.end(),
122  str2.begin(), str2.end(),
124  }
125 };
126 
127 
128 template <class T>
129 class OrderedMap : public std::vector<std::pair<std::string, T*> > {
130  public:
131  typedef typename std::vector<std::pair<std::string, T*> > BaseClass;
132 
133  int size() const { return (int)BaseClass::size(); }
134 
135  const std::pair<std::string, T*>& operator[](uint idx) const {
136  return BaseClass::operator[](idx);
137  }
138 
139  std::pair<std::string, T*>& operator[](uint idx) {
140  return BaseClass::operator[](idx);
141  }
142 
143  const T* operator[](const char* str) const {
144  const uint size = this->size();
145  for (uint i=0; i<size; i++) {
146  if (charptr_cmp((*this)[i].first.c_str(), str) == 0) {
147  return (*this)[i].second;
148  }
149  }
150 
151  throw EssentiaException("Value not found: '", str, "'\nAvailable keys: ", keys());
152  }
153 
154  T* operator[](const char* str) {
155  return const_cast<T*>(const_cast<const OrderedMap<T>*>(this)->operator[](str));
156  }
157 
158  const T* operator[](const std::string& str) const {
159  return operator[](str.c_str());
160  }
161 
162  T* operator[](const std::string& str) {
163  return operator[](str.c_str());
164  }
165 
166  std::vector<std::string> keys() const {
167  std::vector<std::string> result(this->size());
168  for (int i=0; i<this->size(); i++) {
169  result[i] = this->at(i).first;
170  }
171  return result;
172  }
173 
174  void insert(const std::string& key, T* value) {
175  this->push_back(std::make_pair(key, value));
176  }
177 };
178 
179 
180 
189 template <typename KeyType, typename ValueType, typename Compare = std::less<KeyType> >
190 class EssentiaMap : public std::map<KeyType, ValueType, Compare> {
191 
192  public:
193  typedef std::map<KeyType, ValueType, Compare> BaseClass;
194 
198  ValueType& operator[](const KeyType& key) {
199  typename BaseClass::iterator it = this->find(key);
200  if (it == BaseClass::end()) {
201  throw EssentiaException("Value not found: '", key, "'\nAvailable keys: ", keys());
202  }
203  return it->second;
204  }
205 
211  const ValueType& operator[](const KeyType& key) const {
212  typename BaseClass::const_iterator it = this->find(key);
213  if (it == BaseClass::end()) {
214  throw EssentiaException("Value not found: '", key, "'\nAvailable keys: ", keys());
215  }
216  return it->second;
217  }
218 
219  std::pair<typename BaseClass::iterator, bool> insert(const KeyType& key, const ValueType& value) {
220  return BaseClass::insert(std::make_pair(key, value));
221  }
222 
223  std::vector<std::string> keys() const {
224  std::vector<std::string> result;
225  result.reserve(BaseClass::size());
226  std::ostringstream stream;
227  typename BaseClass::const_iterator it = this->begin();
228  for (; it != this->end(); ++it) {
229  stream.str("");
230  stream << it->first;
231  result.push_back(stream.str());
232  }
233  return result;
234  }
235 
236 };
237 
238 
243 
244 
245 
246 
250 #if SAFE_TYPE_COMPARISONS
251 
252 // comparison of the type is done using the name() method, because type_info
253 // are not shared between different linking units.
254 inline bool sameType(const std::type_info& t1, const std::type_info& t2) {
255  return strcmp(t1.name(), t2.name()) == 0;
256 }
257 
258 #else // SAFE_TYPE_COMPARISONS
259 
260 inline bool sameType(const std::type_info& t1, const std::type_info& t2) {
261  return t1 == t2;
262 }
263 
264 #endif // SAFE_TYPE_COMPARISONS
265 
266 
267 // defined in src/base/essentia.cpp
268 std::string nameOfType(const std::type_info& type);
269 
274 class TypeProxy {
275  protected:
276  std::string _name;
277 
278  public:
280  TypeProxy(const std::string& name) : _name(name) {}
281 
282  virtual ~TypeProxy() {}
283 
284  const std::string& name() const { return _name; }
285  void setName(const std::string& name) { _name = name; }
286 
287  inline void checkType(const std::type_info& received,
288  const std::type_info& expected) const {
289  if (!sameType(received, expected)) {
290  std::ostringstream msg;
291  msg << "Error when checking types. Expected: " << nameOfType(expected)
292  << ", received: " << nameOfType(received);
293  throw EssentiaException(msg);
294  }
295  }
296 
297  template <typename Type>
298  void checkType() const {
299  checkType(typeid(Type), typeInfo());
300  }
301 
302  void checkSameTypeAs(const TypeProxy& obj) const {
303  checkType(obj.typeInfo(), typeInfo());
304  }
305 
306  void checkVectorSameTypeAs(const TypeProxy& obj) const {
308  }
309 
310  virtual const std::type_info& typeInfo() const = 0;
311  virtual const std::type_info& vectorTypeInfo() const = 0;
312 };
313 
317 inline std::string nameOfType(const TypeProxy& tproxy) {
318  return nameOfType(tproxy.typeInfo());
319 }
320 
324 inline bool sameType(const TypeProxy& lhs, const TypeProxy& rhs) {
325  return sameType(lhs.typeInfo(), rhs.typeInfo());
326 }
327 
332 #define USE_TYPE_INFO(TokenType) \
333  virtual const std::type_info& typeInfo() const { \
334  return typeid(TokenType); \
335  } \
336  virtual const std::type_info& vectorTypeInfo() const { \
337  return typeid(std::vector<TokenType>); \
338  }
339 
340 
344 typedef int ReaderID;
345 
350 
351 template <typename T>
352 class Tuple2 {
353  public:
354  T first;
356 
357  const T& left() const { return first; }
358  const T& right() const { return second; }
359  const T& x() const { return first; }
360  const T& y() const { return second; }
361 
362  T& left() { return first; }
363  T& right() { return second; }
364  T& x() { return first; }
365  T& y() { return second; }
366 };
367 
372 
376 #define TENSORRANK 4
377 
383 template<typename T>
384 using Tensor = Eigen::Tensor<T, TENSORRANK, Eigen::RowMajor>;
385 
389 template<typename T>
390 using TensorMap = Eigen::TensorMap<Tensor<T>, 0>;
391 
395 using TensorScalar = Eigen::Tensor<Real, 0, Eigen::RowMajor>;
396 
400 using Tensor1D = Eigen::Tensor<Real, 1, Eigen::RowMajor>;
401 
405 using Tensor2D = Eigen::Tensor<Real, 2, Eigen::RowMajor>;
406 
410 using Tensor3D = Eigen::Tensor<Real, 3, Eigen::RowMajor>;
411 
412 
413 namespace streaming {
414 
420 class BufferInfo {
421  public:
422  int size;
424 
425  BufferInfo(int size = 0, int contiguous = 0) :
426  size(size), maxContiguousElements(contiguous) {}
427 };
428 
429 namespace BufferUsage {
430 
441 };
442 
443 } // namespace BufferUsage
444 
445 } // namespace streaming
446 } // namespace essentia
447 
448 #endif // ESSENTIA_TYPES_H
Definition: types.h:77
EssentiaException(const T &a, const U &b)
Definition: types.h:85
EssentiaException(const std::ostringstream &msg)
Definition: types.h:82
std::string _msg
Definition: types.h:103
EssentiaException(const std::string &msg)
Definition: types.h:81
EssentiaException(const T &a, const U &b, const V &c, const W &d)
Definition: types.h:95
EssentiaException(const char *msg)
Definition: types.h:80
virtual ~EssentiaException()
Definition: types.h:99
EssentiaException(const T &a, const U &b, const V &c)
Definition: types.h:90
virtual const char * what() const
Definition: types.h:100
Definition: types.h:190
const ValueType & operator[](const KeyType &key) const
Definition: types.h:211
std::pair< typename BaseClass::iterator, bool > insert(const KeyType &key, const ValueType &value)
Definition: types.h:219
std::vector< std::string > keys() const
Definition: types.h:223
std::map< KeyType, ValueType, Compare > BaseClass
Definition: types.h:193
ValueType & operator[](const KeyType &key)
Definition: types.h:198
Definition: types.h:129
T * operator[](const char *str)
Definition: types.h:154
std::pair< std::string, T * > & operator[](uint idx)
Definition: types.h:139
std::vector< std::string > keys() const
Definition: types.h:166
const T * operator[](const std::string &str) const
Definition: types.h:158
const T * operator[](const char *str) const
Definition: types.h:143
T * operator[](const std::string &str)
Definition: types.h:162
void insert(const std::string &key, T *value)
Definition: types.h:174
const std::pair< std::string, T * > & operator[](uint idx) const
Definition: types.h:135
std::vector< std::pair< std::string, T * > > BaseClass
Definition: types.h:131
int size() const
Definition: types.h:133
Definition: types.h:352
T second
Definition: types.h:355
const T & y() const
Definition: types.h:360
const T & right() const
Definition: types.h:358
T & right()
Definition: types.h:363
T & y()
Definition: types.h:365
T & left()
Definition: types.h:362
T first
Definition: types.h:354
const T & x() const
Definition: types.h:359
const T & left() const
Definition: types.h:357
T & x()
Definition: types.h:364
Definition: types.h:274
void checkVectorSameTypeAs(const TypeProxy &obj) const
Definition: types.h:306
void checkSameTypeAs(const TypeProxy &obj) const
Definition: types.h:302
void checkType(const std::type_info &received, const std::type_info &expected) const
Definition: types.h:287
virtual ~TypeProxy()
Definition: types.h:282
virtual const std::type_info & vectorTypeInfo() const =0
virtual const std::type_info & typeInfo() const =0
TypeProxy(const std::string &name)
Definition: types.h:280
void setName(const std::string &name)
Definition: types.h:285
TypeProxy()
Definition: types.h:279
std::string _name
Definition: types.h:276
const std::string & name() const
Definition: types.h:284
void checkType() const
Definition: types.h:298
Definition: types.h:420
int maxContiguousElements
Definition: types.h:423
int size
Definition: types.h:422
BufferInfo(int size=0, int contiguous=0)
Definition: types.h:425
BufferUsageType
Definition: types.h:436
@ forMultipleFrames
Definition: types.h:438
@ forLargeAudioStream
Definition: types.h:440
@ forAudioStream
Definition: types.h:439
@ forSingleFrames
Definition: types.h:437
Definition: algorithm.h:28
Real AudioSample
Definition: types.h:349
bool sameType(const std::type_info &t1, const std::type_info &t2)
Definition: types.h:260
Eigen::TensorMap< Tensor< T >, 0 > TensorMap
Definition: types.h:390
Eigen::Tensor< Real, 1, Eigen::RowMajor > Tensor1D
Definition: types.h:400
Eigen::Tensor< Real, 2, Eigen::RowMajor > Tensor2D
Definition: types.h:405
Eigen::Tensor< Real, 3, Eigen::RowMajor > Tensor3D
Definition: types.h:410
std::string nameOfType(const std::type_info &type)
int ReaderID
Definition: types.h:344
Tuple2< Real > StereoSample
Definition: types.h:371
bool case_insensitive_char_cmp(char a, char b)
Definition: types.h:111
EssentiaMap< std::string, std::string, string_cmp > DescriptionMap
Definition: types.h:242
float Real
Definition: types.h:69
Eigen::Tensor< Real, 0, Eigen::RowMajor > TensorScalar
Definition: types.h:395
Eigen::Tensor< T, 4, Eigen::RowMajor > Tensor
Definition: types.h:384
Definition: types.h:119
bool operator()(const std::string &str1, const std::string &str2) const
Definition: types.h:120
int64_t sint64
Definition: types.h:47
int32_t sint32
Definition: types.h:46
unsigned int uint
Definition: types.h:49
int16_t sint16
Definition: types.h:45
uint64_t uint64
Definition: types.h:44
uint16_t uint16
Definition: types.h:42
uint32_t uint32
Definition: types.h:43