2 #include "audiographer/sample_format_converter.h"
4 using namespace AudioGrapher;
6 class SampleFormatConverterTest : public CppUnit::TestFixture
8 CPPUNIT_TEST_SUITE (SampleFormatConverterTest);
9 CPPUNIT_TEST (testInit);
10 CPPUNIT_TEST (testFrameCount);
11 CPPUNIT_TEST (testFloat);
12 CPPUNIT_TEST (testInt32);
13 CPPUNIT_TEST (testInt24);
14 CPPUNIT_TEST (testInt16);
15 CPPUNIT_TEST (testUint8);
16 CPPUNIT_TEST (testChannelCount);
17 CPPUNIT_TEST_SUITE_END ();
23 random_data = TestUtils::init_random_data(frames, 1.0);
28 delete [] random_data;
33 boost::shared_ptr<SampleFormatConverter<float> > f_converter (new SampleFormatConverter<float>(1));
34 f_converter->init (frames, D_Tri, 32); // Doesn't throw
35 CPPUNIT_ASSERT_THROW (f_converter->init (frames, D_Tri, 24), Exception);
36 CPPUNIT_ASSERT_THROW (f_converter->init (frames, D_Tri, 48), Exception);
38 boost::shared_ptr<SampleFormatConverter<int32_t> > i_converter (new SampleFormatConverter<int32_t>(1));
39 i_converter->init (frames, D_Tri, 32); // Doesn't throw
40 i_converter->init (frames, D_Tri, 24); // Doesn't throw
41 CPPUNIT_ASSERT_THROW (i_converter->init (frames, D_Tri, 8), Exception);
42 CPPUNIT_ASSERT_THROW (i_converter->init (frames, D_Tri, 16), Exception);
43 CPPUNIT_ASSERT_THROW (i_converter->init (frames, D_Tri, 48), Exception);
45 boost::shared_ptr<SampleFormatConverter<int16_t> > i16_converter (new SampleFormatConverter<int16_t>(1));
46 i16_converter->init (frames, D_Tri, 16); // Doesn't throw
47 CPPUNIT_ASSERT_THROW (i16_converter->init (frames, D_Tri, 8), Exception);
48 CPPUNIT_ASSERT_THROW (i16_converter->init (frames, D_Tri, 32), Exception);
49 CPPUNIT_ASSERT_THROW (i16_converter->init (frames, D_Tri, 48), Exception);
51 boost::shared_ptr<SampleFormatConverter<uint8_t> > ui_converter (new SampleFormatConverter<uint8_t>(1));
52 ui_converter->init (frames, D_Tri, 8); // Doesn't throw
53 CPPUNIT_ASSERT_THROW (ui_converter->init (frames, D_Tri, 4), Exception);
54 CPPUNIT_ASSERT_THROW (ui_converter->init (frames, D_Tri, 16), Exception);
59 boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
60 boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
62 converter->init (frames, D_Tri, 32);
63 converter->add_output (sink);
64 nframes_t frames_output = 0;
67 ProcessContext<float> pc(random_data, frames / 2, 1);
68 converter->process (pc);
69 frames_output = sink->get_data().size();
70 CPPUNIT_ASSERT_EQUAL (frames / 2, frames_output);
74 ProcessContext<float> pc(random_data, frames, 1);
75 converter->process (pc);
76 frames_output = sink->get_data().size();
77 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
81 ProcessContext<float> pc(random_data, frames + 1, 1);
82 CPPUNIT_ASSERT_THROW(converter->process (pc), Exception);
88 boost::shared_ptr<SampleFormatConverter<float> > converter (new SampleFormatConverter<float>(1));
89 boost::shared_ptr<VectorSink<float> > sink (new VectorSink<float>());
90 nframes_t frames_output = 0;
92 converter->init(frames, D_Tri, 32);
93 converter->add_output (sink);
95 converter->set_clip_floats (false);
96 ProcessContext<float> const pc(random_data, frames, 1);
97 converter->process (pc);
98 frames_output = sink->get_data().size();
99 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
100 CPPUNIT_ASSERT (TestUtils::array_equals(sink->get_array(), random_data, frames));
102 // Make sure a few samples are < -1.0 and > 1.0
103 random_data[10] = -1.5;
104 random_data[20] = 1.5;
106 converter->set_clip_floats (true);
107 converter->process (pc);
108 frames_output = sink->get_data().size();
109 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
110 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
112 for (nframes_t i = 0; i < frames; ++i) {
113 // fp comparison needs a bit of tolerance, 1.01 << 1.5
114 CPPUNIT_ASSERT(sink->get_data()[i] < 1.01);
115 CPPUNIT_ASSERT(sink->get_data()[i] > -1.01);
121 boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
122 boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
123 nframes_t frames_output = 0;
125 converter->init(frames, D_Tri, 32);
126 converter->add_output (sink);
128 ProcessContext<float> pc(random_data, frames, 1);
129 converter->process (pc);
130 frames_output = sink->get_data().size();
131 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
132 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
137 boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
138 boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
139 nframes_t frames_output = 0;
141 converter->init(frames, D_Tri, 24);
142 converter->add_output (sink);
144 ProcessContext<float> pc(random_data, frames, 1);
145 converter->process (pc);
146 frames_output = sink->get_data().size();
147 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
148 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
153 boost::shared_ptr<SampleFormatConverter<int16_t> > converter (new SampleFormatConverter<int16_t>(1));
154 boost::shared_ptr<VectorSink<int16_t> > sink (new VectorSink<int16_t>());
155 nframes_t frames_output = 0;
157 converter->init(frames, D_Tri, 16);
158 converter->add_output (sink);
160 ProcessContext<float> pc(random_data, frames, 1);
161 converter->process (pc);
162 frames_output = sink->get_data().size();
163 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
164 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
169 boost::shared_ptr<SampleFormatConverter<uint8_t> > converter (new SampleFormatConverter<uint8_t>(1));
170 boost::shared_ptr<VectorSink<uint8_t> > sink (new VectorSink<uint8_t>());
171 nframes_t frames_output = 0;
173 converter->init(frames, D_Tri, 8);
174 converter->add_output (sink);
176 ProcessContext<float> pc(random_data, frames, 1);
177 converter->process (pc);
178 frames_output = sink->get_data().size();
179 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
180 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
183 void testChannelCount()
185 boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(3));
186 boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
187 nframes_t frames_output = 0;
189 converter->init(frames, D_Tri, 32);
190 converter->add_output (sink);
192 ProcessContext<float> pc(random_data, 4, 1);
193 CPPUNIT_ASSERT_THROW (converter->process (pc), Exception);
195 pc.frames() = frames - (frames % 3);
196 converter->process (pc);
197 frames_output = sink->get_data().size();
198 CPPUNIT_ASSERT_EQUAL (pc.frames(), frames_output);
199 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), pc.frames()));
208 CPPUNIT_TEST_SUITE_REGISTRATION (SampleFormatConverterTest);