2010-03-15 15:11:48 -04:00
|
|
|
#include "tests/utils.h"
|
|
|
|
|
|
|
|
#include "audiographer/general/sample_format_converter.h"
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
using namespace AudioGrapher;
|
|
|
|
|
|
|
|
class SampleFormatConverterTest : public CppUnit::TestFixture
|
|
|
|
{
|
|
|
|
CPPUNIT_TEST_SUITE (SampleFormatConverterTest);
|
|
|
|
CPPUNIT_TEST (testInit);
|
|
|
|
CPPUNIT_TEST (testFrameCount);
|
|
|
|
CPPUNIT_TEST (testFloat);
|
|
|
|
CPPUNIT_TEST (testInt32);
|
|
|
|
CPPUNIT_TEST (testInt24);
|
|
|
|
CPPUNIT_TEST (testInt16);
|
|
|
|
CPPUNIT_TEST (testUint8);
|
|
|
|
CPPUNIT_TEST (testChannelCount);
|
|
|
|
CPPUNIT_TEST_SUITE_END ();
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp()
|
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
samples = 128;
|
|
|
|
random_data = TestUtils::init_random_data(samples, 1.0);
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
{
|
|
|
|
delete [] random_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void testInit()
|
|
|
|
{
|
2014-01-08 13:33:17 -05:00
|
|
|
// Float never uses dithering and should always use full 32 bits of data
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<SampleFormatConverter<float> > f_converter (new SampleFormatConverter<float>(1));
|
2017-09-18 12:39:17 -04:00
|
|
|
f_converter->init (samples, D_Tri, 32); // Doesn't throw
|
|
|
|
CPPUNIT_ASSERT_THROW (f_converter->init (samples, D_Tri, 24), Exception);
|
|
|
|
CPPUNIT_ASSERT_THROW (f_converter->init (samples, D_Tri, 48), Exception);
|
2014-01-08 13:33:17 -05:00
|
|
|
|
|
|
|
/* Test that too large data widths throw.
|
|
|
|
We are fine with unnecessarily narrow data widths */
|
|
|
|
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<SampleFormatConverter<int32_t> > i_converter (new SampleFormatConverter<int32_t>(1));
|
2017-09-18 12:39:17 -04:00
|
|
|
i_converter->init (samples, D_Tri, 32); // Doesn't throw
|
|
|
|
i_converter->init (samples, D_Tri, 24); // Doesn't throw
|
|
|
|
i_converter->init (samples, D_Tri, 8); // Doesn't throw
|
|
|
|
i_converter->init (samples, D_Tri, 16); // Doesn't throw
|
|
|
|
CPPUNIT_ASSERT_THROW (i_converter->init (samples, D_Tri, 48), Exception);
|
2014-01-08 13:33:17 -05:00
|
|
|
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<SampleFormatConverter<int16_t> > i16_converter (new SampleFormatConverter<int16_t>(1));
|
2017-09-18 12:39:17 -04:00
|
|
|
i16_converter->init (samples, D_Tri, 16); // Doesn't throw
|
|
|
|
i16_converter->init (samples, D_Tri, 8); // Doesn't throw
|
|
|
|
CPPUNIT_ASSERT_THROW (i16_converter->init (samples, D_Tri, 32), Exception);
|
|
|
|
CPPUNIT_ASSERT_THROW (i16_converter->init (samples, D_Tri, 48), Exception);
|
2014-01-08 13:33:17 -05:00
|
|
|
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<SampleFormatConverter<uint8_t> > ui_converter (new SampleFormatConverter<uint8_t>(1));
|
2017-09-18 12:39:17 -04:00
|
|
|
ui_converter->init (samples, D_Tri, 8); // Doesn't throw
|
|
|
|
ui_converter->init (samples, D_Tri, 4); // Doesn't throw
|
|
|
|
CPPUNIT_ASSERT_THROW (ui_converter->init (samples, D_Tri, 16), Exception);
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void testFrameCount()
|
|
|
|
{
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
|
|
|
|
std::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
converter->init (samples, D_Tri, 32);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->add_output (sink);
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_output = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> pc(random_data, samples / 2, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->process (pc);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (samples / 2, samples_output);
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> pc(random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->process (pc);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (samples, samples_output);
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> pc(random_data, samples + 1, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
CPPUNIT_ASSERT_THROW(converter->process (pc), Exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testFloat()
|
|
|
|
{
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<SampleFormatConverter<float> > converter (new SampleFormatConverter<float>(1));
|
|
|
|
std::shared_ptr<VectorSink<float> > sink (new VectorSink<float>());
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_output = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
converter->init(samples, D_Tri, 32);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->add_output (sink);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->set_clip_floats (false);
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> const pc(random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->process (pc);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (samples, samples_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals(sink->get_array(), random_data, samples));
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
// Make sure a few samples are < -1.0 and > 1.0
|
|
|
|
random_data[10] = -1.5;
|
|
|
|
random_data[20] = 1.5;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->set_clip_floats (true);
|
|
|
|
converter->process (pc);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (samples, samples_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), samples));
|
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
|
|
|
// fp comparison needs a bit of tolerance, 1.01 << 1.5
|
|
|
|
CPPUNIT_ASSERT(sink->get_data()[i] < 1.01);
|
|
|
|
CPPUNIT_ASSERT(sink->get_data()[i] > -1.01);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testInt32()
|
|
|
|
{
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
|
|
|
|
std::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_output = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
converter->init(samples, D_Tri, 32);
|
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> pc(random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->process (pc);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (samples, samples_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), samples));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
void testInt24()
|
|
|
|
{
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
|
|
|
|
std::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_output = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
converter->init(samples, D_Tri, 24);
|
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> pc(random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->process (pc);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (samples, samples_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), samples));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
void testInt16()
|
|
|
|
{
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<SampleFormatConverter<int16_t> > converter (new SampleFormatConverter<int16_t>(1));
|
|
|
|
std::shared_ptr<VectorSink<int16_t> > sink (new VectorSink<int16_t>());
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_output = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
converter->init(samples, D_Tri, 16);
|
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> pc(random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->process (pc);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (samples, samples_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), samples));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
void testUint8()
|
|
|
|
{
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<SampleFormatConverter<uint8_t> > converter (new SampleFormatConverter<uint8_t>(1));
|
|
|
|
std::shared_ptr<VectorSink<uint8_t> > sink (new VectorSink<uint8_t>());
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_output = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
converter->init(samples, D_Tri, 8);
|
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> pc(random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->process (pc);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (samples, samples_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), samples));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
void testChannelCount()
|
|
|
|
{
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(3));
|
|
|
|
std::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_output = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
converter->init(samples, D_Tri, 32);
|
2009-12-27 09:46:23 -05:00
|
|
|
converter->add_output (sink);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
ProcessContext<float> pc(random_data, 4, 1);
|
|
|
|
CPPUNIT_ASSERT_THROW (converter->process (pc), Exception);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t new_sample_count = samples - (samples % 3);
|
|
|
|
converter->process (ProcessContext<float> (pc.data(), new_sample_count, 3));
|
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (new_sample_count, samples_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), pc.samples()));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
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 (SampleFormatConverterTest);
|
|
|
|
|