2010-03-15 15:11:48 -04:00
|
|
|
#include "tests/utils.h"
|
|
|
|
|
|
|
|
#include "audiographer/general/sr_converter.h"
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
using namespace AudioGrapher;
|
|
|
|
|
|
|
|
class SampleRateConverterTest : public CppUnit::TestFixture
|
|
|
|
{
|
|
|
|
CPPUNIT_TEST_SUITE (SampleRateConverterTest);
|
|
|
|
CPPUNIT_TEST (testNoConversion);
|
|
|
|
CPPUNIT_TEST (testUpsampleLength);
|
|
|
|
CPPUNIT_TEST (testDownsampleLength);
|
2009-12-27 17:09:40 -05:00
|
|
|
CPPUNIT_TEST (testRespectsEndOfInput);
|
2009-12-27 09:46:23 -05:00
|
|
|
CPPUNIT_TEST_SUITE_END ();
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp()
|
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
samples = 128;
|
|
|
|
random_data = TestUtils::init_random_data(samples);
|
2009-12-27 09:46:23 -05:00
|
|
|
sink.reset (new AppendingVectorSink<float>());
|
2009-12-27 17:09:40 -05:00
|
|
|
grabber.reset (new ProcessContextGrabber<float>());
|
2009-12-27 09:46:23 -05:00
|
|
|
converter.reset (new SampleRateConverter (1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
{
|
|
|
|
delete [] random_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void testNoConversion()
|
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
assert (samples % 2 == 0);
|
|
|
|
samplecnt_t const half_samples = samples / 2;
|
|
|
|
samplecnt_t samples_output = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->init (44100, 44100);
|
|
|
|
converter->add_output (sink);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (random_data, half_samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->process (c);
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c2 (&random_data[half_samples], half_samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
c2.set_flag (ProcessContext<float>::EndOfInput);
|
|
|
|
converter->process (c2);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (samples, samples_output);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, sink->get_array(), samples));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void testUpsampleLength()
|
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
assert (samples % 2 == 0);
|
|
|
|
samplecnt_t const half_samples = samples / 2;
|
|
|
|
samplecnt_t samples_output = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->init (44100, 88200);
|
2017-09-18 12:39:17 -04:00
|
|
|
converter->allocate_buffers (half_samples);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->add_output (sink);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (random_data, half_samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->process (c);
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c2 (&random_data[half_samples], half_samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
c2.set_flag (ProcessContext<float>::EndOfInput);
|
|
|
|
converter->process (c2);
|
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
samplecnt_t tolerance = 3;
|
|
|
|
CPPUNIT_ASSERT (2 * samples - tolerance < samples_output && samples_output < 2 * samples + tolerance);
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void testDownsampleLength()
|
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
assert (samples % 2 == 0);
|
|
|
|
samplecnt_t const half_samples = samples / 2;
|
|
|
|
samplecnt_t samples_output = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->init (88200, 44100);
|
2017-09-18 12:39:17 -04:00
|
|
|
converter->allocate_buffers (half_samples);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->add_output (sink);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (random_data, half_samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->process (c);
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c2 (&random_data[half_samples], half_samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
c2.set_flag (ProcessContext<float>::EndOfInput);
|
|
|
|
converter->process (c2);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
samplecnt_t tolerance = 3;
|
|
|
|
CPPUNIT_ASSERT (half_samples - tolerance < samples_output && samples_output < half_samples + tolerance);
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 17:09:40 -05:00
|
|
|
void testRespectsEndOfInput()
|
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
assert (samples % 2 == 0);
|
|
|
|
samplecnt_t const half_samples = samples / 2;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 17:09:40 -05:00
|
|
|
converter->init (44100, 48000);
|
2017-09-18 12:39:17 -04:00
|
|
|
converter->allocate_buffers (half_samples);
|
2009-12-27 17:09:40 -05:00
|
|
|
converter->add_output (grabber);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (random_data, half_samples, 1);
|
2009-12-27 17:09:40 -05:00
|
|
|
converter->process (c);
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c2 (&random_data[half_samples], half_samples / 2, 1);
|
2009-12-27 17:09:40 -05:00
|
|
|
c2.set_flag (ProcessContext<float>::EndOfInput);
|
|
|
|
converter->process (c2);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 17:09:40 -05:00
|
|
|
for (std::list<ProcessContext<float> >::iterator it = grabber->contexts.begin(); it != grabber->contexts.end(); ++it) {
|
|
|
|
std::list<ProcessContext<float> >::iterator next = it; ++next;
|
|
|
|
if (next == grabber->contexts.end()) {
|
|
|
|
CPPUNIT_ASSERT (it->has_flag (ProcessContext<float>::EndOfInput));
|
|
|
|
} else {
|
|
|
|
CPPUNIT_ASSERT (!it->has_flag (ProcessContext<float>::EndOfInput));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
boost::shared_ptr<SampleRateConverter > converter;
|
|
|
|
boost::shared_ptr<AppendingVectorSink<float> > sink;
|
2009-12-27 17:09:40 -05:00
|
|
|
boost::shared_ptr<ProcessContextGrabber<float> > grabber;
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
float * random_data;
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples;
|
2009-12-27 09:46:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION (SampleRateConverterTest);
|
|
|
|
|