2009-12-27 09:46:23 -05:00
|
|
|
#ifndef AUDIOGRAPHER_TESTS_UTILS_H
|
|
|
|
#define AUDIOGRAPHER_TESTS_UTILS_H
|
|
|
|
|
|
|
|
// Includes we want almost always
|
|
|
|
|
2023-02-16 12:59:41 -05:00
|
|
|
#include <memory>
|
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
|
|
|
// includes used in this file
|
|
|
|
|
|
|
|
#include "audiographer/sink.h"
|
|
|
|
#include "audiographer/exception.h"
|
|
|
|
|
|
|
|
#include <vector>
|
2009-12-27 17:09:40 -05:00
|
|
|
#include <list>
|
2009-12-27 09:46:23 -05:00
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <ctime>
|
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
using AudioGrapher::samplecnt_t;
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
struct TestUtils
|
|
|
|
{
|
|
|
|
template<typename T>
|
2017-09-18 12:39:17 -04:00
|
|
|
static bool array_equals (T const * a, T const * b, samplecnt_t samples)
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
for (samplecnt_t i = 0; i < samples; ++i) {
|
2009-12-27 09:46:23 -05:00
|
|
|
if (a[i] != b[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2017-09-18 12:39:17 -04:00
|
|
|
static bool array_filled (T const * array, samplecnt_t samples)
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
for (samplecnt_t i = 0; i < samples; ++i) {
|
2009-12-27 09:46:23 -05:00
|
|
|
if (array[i] == static_cast<T> (0.0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate random data, all samples guaranteed not to be 0.0, 1.0 or -1.0
|
2017-09-18 12:39:17 -04:00
|
|
|
static float * init_random_data (samplecnt_t samples, float range = 1.0)
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
|
|
|
unsigned int const granularity = 4096;
|
2017-09-18 12:39:17 -04:00
|
|
|
float * data = new float[samples];
|
2009-12-27 09:46:23 -05:00
|
|
|
srand (std::time (NULL));
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
for (samplecnt_t i = 0; i < samples; ++i) {
|
2009-12-27 09:46:23 -05:00
|
|
|
do {
|
|
|
|
int biased_int = (rand() % granularity) - (granularity / 2);
|
|
|
|
data[i] = (range * biased_int) / granularity;
|
|
|
|
} while (data[i] == 0.0 || data[i] == 1.0 || data[i] == -1.0);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class VectorSink : public AudioGrapher::Sink<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void process (AudioGrapher::ProcessContext<T> const & c)
|
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
data.resize (c.samples());
|
|
|
|
memcpy (&data[0], c.data(), c.samples() * sizeof(T));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void process (AudioGrapher::ProcessContext<T> & c) { AudioGrapher::Sink<T>::process (c); }
|
|
|
|
using AudioGrapher::Sink<T>::process;
|
|
|
|
|
|
|
|
std::vector<T> const & get_data() const { return data; }
|
|
|
|
T const * get_array() const { return &data[0]; }
|
|
|
|
void reset() { data.clear(); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::vector<T> data;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class AppendingVectorSink : public VectorSink<T>
|
|
|
|
{
|
|
|
|
public:
|
2015-09-05 13:14:16 -04:00
|
|
|
AppendingVectorSink() { reset (); }
|
2009-12-27 09:46:23 -05:00
|
|
|
void process (AudioGrapher::ProcessContext<T> const & c)
|
|
|
|
{
|
|
|
|
std::vector<T> & data (VectorSink<T>::data);
|
2017-09-18 12:39:17 -04:00
|
|
|
data.resize (total_samples + c.samples());
|
|
|
|
memcpy (&data[total_samples], c.data(), c.samples() * sizeof(T));
|
|
|
|
total_samples += c.samples();
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
|
|
|
using AudioGrapher::Sink<T>::process;
|
|
|
|
|
|
|
|
void reset ()
|
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
total_samples = 0;
|
2009-12-27 09:46:23 -05:00
|
|
|
VectorSink<T>::reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t total_samples;
|
2009-12-27 09:46:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class ThrowingSink : public AudioGrapher::Sink<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void process (AudioGrapher::ProcessContext<T> const &)
|
|
|
|
{
|
|
|
|
throw AudioGrapher::Exception(*this, "ThrowingSink threw!");
|
|
|
|
}
|
|
|
|
using AudioGrapher::Sink<T>::process;
|
|
|
|
};
|
|
|
|
|
2009-12-27 17:09:40 -05:00
|
|
|
template<typename T>
|
|
|
|
class ProcessContextGrabber : public AudioGrapher::Sink<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void process (AudioGrapher::ProcessContext<T> const & c)
|
|
|
|
{
|
|
|
|
contexts.push_back (c);
|
|
|
|
}
|
|
|
|
using AudioGrapher::Sink<T>::process;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 17:09:40 -05:00
|
|
|
typedef std::list<AudioGrapher::ProcessContext<T> > ContextList;
|
|
|
|
ContextList contexts;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 17:09:40 -05:00
|
|
|
};
|
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
#endif // AUDIOGRAPHER_TESTS_UTILS_H
|