1 #ifndef AUDIOGRAPHER_TESTS_UTILS_H
2 #define AUDIOGRAPHER_TESTS_UTILS_H
4 // Includes we want almost always
6 #include <cppunit/extensions/HelperMacros.h>
7 #include <boost/shared_ptr.hpp>
9 // includes used in this file
11 #include "audiographer/sink.h"
12 #include "audiographer/exception.h"
20 using AudioGrapher::framecnt_t;
25 static bool array_equals (T const * a, T const * b, framecnt_t frames)
27 for (framecnt_t i = 0; i < frames; ++i) {
36 static bool array_filled (T const * array, framecnt_t frames)
38 for (framecnt_t i = 0; i < frames; ++i) {
39 if (array[i] == static_cast<T> (0.0)) {
46 /// Generate random data, all samples guaranteed not to be 0.0, 1.0 or -1.0
47 static float * init_random_data (framecnt_t frames, float range = 1.0)
49 unsigned int const granularity = 4096;
50 float * data = new float[frames];
51 srand (std::time (NULL));
53 for (framecnt_t i = 0; i < frames; ++i) {
55 int biased_int = (rand() % granularity) - (granularity / 2);
56 data[i] = (range * biased_int) / granularity;
57 } while (data[i] == 0.0 || data[i] == 1.0 || data[i] == -1.0);
64 class VectorSink : public AudioGrapher::Sink<T>
67 virtual void process (AudioGrapher::ProcessContext<T> const & c)
69 data.resize (c.frames());
70 memcpy (&data[0], c.data(), c.frames() * sizeof(T));
73 void process (AudioGrapher::ProcessContext<T> & c) { AudioGrapher::Sink<T>::process (c); }
74 using AudioGrapher::Sink<T>::process;
76 std::vector<T> const & get_data() const { return data; }
77 T const * get_array() const { return &data[0]; }
78 void reset() { data.clear(); }
86 class AppendingVectorSink : public VectorSink<T>
89 void process (AudioGrapher::ProcessContext<T> const & c)
91 std::vector<T> & data (VectorSink<T>::data);
92 data.resize (total_frames + c.frames());
93 memcpy (&data[total_frames], c.data(), c.frames() * sizeof(T));
94 total_frames += c.frames();
96 using AudioGrapher::Sink<T>::process;
101 VectorSink<T>::reset();
105 framecnt_t total_frames;
110 class ThrowingSink : public AudioGrapher::Sink<T>
113 void process (AudioGrapher::ProcessContext<T> const &)
115 throw AudioGrapher::Exception(*this, "ThrowingSink threw!");
117 using AudioGrapher::Sink<T>::process;
121 class ProcessContextGrabber : public AudioGrapher::Sink<T>
124 void process (AudioGrapher::ProcessContext<T> const & c)
126 contexts.push_back (c);
128 using AudioGrapher::Sink<T>::process;
130 typedef std::list<AudioGrapher::ProcessContext<T> > ContextList;
131 ContextList contexts;
135 #endif // AUDIOGRAPHER_TESTS_UTILS_H