1 #include "tests/utils.h" 2 3 #include "audiographer/general/sr_converter.h" 4 5 using namespace AudioGrapher; 6 7 class SampleRateConverterTest : public CppUnit::TestFixture 8 { 9 CPPUNIT_TEST_SUITE (SampleRateConverterTest); 10 CPPUNIT_TEST (testNoConversion); 11 CPPUNIT_TEST (testUpsampleLength); 12 CPPUNIT_TEST (testDownsampleLength); 13 CPPUNIT_TEST (testRespectsEndOfInput); 14 CPPUNIT_TEST_SUITE_END (); 15 16 public: setUp()17 void setUp() 18 { 19 samples = 128; 20 random_data = TestUtils::init_random_data(samples); 21 sink.reset (new AppendingVectorSink<float>()); 22 grabber.reset (new ProcessContextGrabber<float>()); 23 converter.reset (new SampleRateConverter (1)); 24 } 25 tearDown()26 void tearDown() 27 { 28 delete [] random_data; 29 } 30 testNoConversion()31 void testNoConversion() 32 { 33 assert (samples % 2 == 0); 34 samplecnt_t const half_samples = samples / 2; 35 samplecnt_t samples_output = 0; 36 37 converter->init (44100, 44100); 38 converter->add_output (sink); 39 40 ProcessContext<float> c (random_data, half_samples, 1); 41 converter->process (c); 42 ProcessContext<float> c2 (&random_data[half_samples], half_samples, 1); 43 c2.set_flag (ProcessContext<float>::EndOfInput); 44 converter->process (c2); 45 46 samples_output = sink->get_data().size(); 47 CPPUNIT_ASSERT_EQUAL (samples, samples_output); 48 49 CPPUNIT_ASSERT (TestUtils::array_equals (random_data, sink->get_array(), samples)); 50 } 51 testUpsampleLength()52 void testUpsampleLength() 53 { 54 assert (samples % 2 == 0); 55 samplecnt_t const half_samples = samples / 2; 56 samplecnt_t samples_output = 0; 57 58 converter->init (44100, 88200); 59 converter->allocate_buffers (half_samples); 60 converter->add_output (sink); 61 62 ProcessContext<float> c (random_data, half_samples, 1); 63 converter->process (c); 64 ProcessContext<float> c2 (&random_data[half_samples], half_samples, 1); 65 c2.set_flag (ProcessContext<float>::EndOfInput); 66 converter->process (c2); 67 68 samples_output = sink->get_data().size(); 69 samplecnt_t tolerance = 3; 70 CPPUNIT_ASSERT (2 * samples - tolerance < samples_output && samples_output < 2 * samples + tolerance); 71 } 72 testDownsampleLength()73 void testDownsampleLength() 74 { 75 assert (samples % 2 == 0); 76 samplecnt_t const half_samples = samples / 2; 77 samplecnt_t samples_output = 0; 78 79 converter->init (88200, 44100); 80 converter->allocate_buffers (half_samples); 81 converter->add_output (sink); 82 83 ProcessContext<float> c (random_data, half_samples, 1); 84 converter->process (c); 85 ProcessContext<float> c2 (&random_data[half_samples], half_samples, 1); 86 c2.set_flag (ProcessContext<float>::EndOfInput); 87 converter->process (c2); 88 89 samples_output = sink->get_data().size(); 90 samplecnt_t tolerance = 3; 91 CPPUNIT_ASSERT (half_samples - tolerance < samples_output && samples_output < half_samples + tolerance); 92 } 93 testRespectsEndOfInput()94 void testRespectsEndOfInput() 95 { 96 assert (samples % 2 == 0); 97 samplecnt_t const half_samples = samples / 2; 98 99 converter->init (44100, 48000); 100 converter->allocate_buffers (half_samples); 101 converter->add_output (grabber); 102 103 ProcessContext<float> c (random_data, half_samples, 1); 104 converter->process (c); 105 ProcessContext<float> c2 (&random_data[half_samples], half_samples / 2, 1); 106 c2.set_flag (ProcessContext<float>::EndOfInput); 107 converter->process (c2); 108 109 for (std::list<ProcessContext<float> >::iterator it = grabber->contexts.begin(); it != grabber->contexts.end(); ++it) { 110 std::list<ProcessContext<float> >::iterator next = it; ++next; 111 if (next == grabber->contexts.end()) { 112 CPPUNIT_ASSERT (it->has_flag (ProcessContext<float>::EndOfInput)); 113 } else { 114 CPPUNIT_ASSERT (!it->has_flag (ProcessContext<float>::EndOfInput)); 115 } 116 } 117 } 118 119 120 private: 121 boost::shared_ptr<SampleRateConverter > converter; 122 boost::shared_ptr<AppendingVectorSink<float> > sink; 123 boost::shared_ptr<ProcessContextGrabber<float> > grabber; 124 125 float * random_data; 126 samplecnt_t samples; 127 }; 128 129 CPPUNIT_TEST_SUITE_REGISTRATION (SampleRateConverterTest); 130 131