Ardour  9.0-pre0-582-g084a23a80d
libs/audiographer/tests/utils.h
Go to the documentation of this file.
1 #ifndef AUDIOGRAPHER_TESTS_UTILS_H
2 #define AUDIOGRAPHER_TESTS_UTILS_H
3 
4 // Includes we want almost always
5 
6 #include <memory>
7 
8 #include <cppunit/extensions/HelperMacros.h>
9 
10 // includes used in this file
11 
12 #include "audiographer/sink.h"
13 #include "audiographer/exception.h"
14 
15 #include <vector>
16 #include <list>
17 #include <cstring>
18 #include <cstdlib>
19 #include <ctime>
20 
22 
23 struct TestUtils
24 {
25  template<typename T>
26  static bool array_equals (T const * a, T const * b, samplecnt_t samples)
27  {
28  for (samplecnt_t i = 0; i < samples; ++i) {
29  if (a[i] != b[i]) {
30  return false;
31  }
32  }
33  return true;
34  }
35 
36  template<typename T>
37  static bool array_filled (T const * array, samplecnt_t samples)
38  {
39  for (samplecnt_t i = 0; i < samples; ++i) {
40  if (array[i] == static_cast<T> (0.0)) {
41  return false;
42  }
43  }
44  return true;
45  }
46 
48  static float * init_random_data (samplecnt_t samples, float range = 1.0)
49  {
50  unsigned int const granularity = 4096;
51  float * data = new float[samples];
52  srand (std::time (NULL));
53 
54  for (samplecnt_t i = 0; i < samples; ++i) {
55  do {
56  int biased_int = (rand() % granularity) - (granularity / 2);
57  data[i] = (range * biased_int) / granularity;
58  } while (data[i] == 0.0 || data[i] == 1.0 || data[i] == -1.0);
59  }
60  return data;
61  }
62 };
63 
64 template<typename T>
65 class VectorSink : public AudioGrapher::Sink<T>
66 {
67  public:
68  virtual void process (AudioGrapher::ProcessContext<T> const & c)
69  {
70  data.resize (c.samples());
71  memcpy (&data[0], c.data(), c.samples() * sizeof(T));
72  }
73 
76 
77  std::vector<T> const & get_data() const { return data; }
78  T const * get_array() const { return &data[0]; }
79  void reset() { data.clear(); }
80 
81  protected:
82  std::vector<T> data;
83 
84 };
85 
86 template<typename T>
88 {
89  public:
92  {
93  std::vector<T> & data (VectorSink<T>::data);
94  data.resize (total_samples + c.samples());
95  memcpy (&data[total_samples], c.data(), c.samples() * sizeof(T));
96  total_samples += c.samples();
97  }
99 
100  void reset ()
101  {
102  total_samples = 0;
104  }
105 
106  private:
108 };
109 
110 
111 template<typename T>
113 {
114  public:
116  {
117  throw AudioGrapher::Exception(*this, "ThrowingSink threw!");
118  }
120 };
121 
122 template<typename T>
124 {
125  public:
127  {
128  contexts.push_back (c);
129  }
131 
132  typedef std::list<AudioGrapher::ProcessContext<T> > ContextList;
134 
135 };
136 
137 #endif // AUDIOGRAPHER_TESTS_UTILS_H
void process(AudioGrapher::ProcessContext< T > const &c)
T const * data() const
data points to the array of data to process
samplecnt_t const & samples() const
samples tells how many samples the array pointed by data contains
virtual void process(ProcessContext< T > const &context)=0
std::list< AudioGrapher::ProcessContext< T > > ContextList
void process(AudioGrapher::ProcessContext< T > const &c)
void process(AudioGrapher::ProcessContext< T > const &)
void process(AudioGrapher::ProcessContext< T > &c)
std::vector< T > const & get_data() const
virtual void process(AudioGrapher::ProcessContext< T > const &c)
T const * get_array() const
Temporal::samplecnt_t samplecnt_t
static bool array_filled(T const *array, samplecnt_t samples)
static float * init_random_data(samplecnt_t samples, float range=1.0)
Generate random data, all samples guaranteed not to be 0.0, 1.0 or -1.0.
static bool array_equals(T const *a, T const *b, samplecnt_t samples)