1 #include "tests/utils.h"
3 #include "audiographer/general/sample_format_converter.h"
5 using namespace AudioGrapher;
7 class SampleFormatConverterTest : public CppUnit::TestFixture
9 CPPUNIT_TEST_SUITE (SampleFormatConverterTest);
10 CPPUNIT_TEST (testInit);
11 CPPUNIT_TEST (testFrameCount);
12 CPPUNIT_TEST (testFloat);
13 CPPUNIT_TEST (testInt32);
14 CPPUNIT_TEST (testInt24);
15 CPPUNIT_TEST (testInt16);
16 CPPUNIT_TEST (testUint8);
17 CPPUNIT_TEST (testChannelCount);
18 CPPUNIT_TEST_SUITE_END ();
24 random_data = TestUtils::init_random_data(frames, 1.0);
29 delete [] random_data;
34 boost::shared_ptr<SampleFormatConverter<float> > f_converter (new SampleFormatConverter<float>(1));
35 f_converter->init (frames, D_Tri, 32); // Doesn't throw
36 CPPUNIT_ASSERT_THROW (f_converter->init (frames, D_Tri, 24), Exception);
37 CPPUNIT_ASSERT_THROW (f_converter->init (frames, D_Tri, 48), Exception);
39 boost::shared_ptr<SampleFormatConverter<int32_t> > i_converter (new SampleFormatConverter<int32_t>(1));
40 i_converter->init (frames, D_Tri, 32); // Doesn't throw
41 i_converter->init (frames, D_Tri, 24); // Doesn't throw
42 CPPUNIT_ASSERT_THROW (i_converter->init (frames, D_Tri, 8), Exception);
43 CPPUNIT_ASSERT_THROW (i_converter->init (frames, D_Tri, 16), Exception);
44 CPPUNIT_ASSERT_THROW (i_converter->init (frames, D_Tri, 48), Exception);
46 boost::shared_ptr<SampleFormatConverter<int16_t> > i16_converter (new SampleFormatConverter<int16_t>(1));
47 i16_converter->init (frames, D_Tri, 16); // Doesn't throw
48 CPPUNIT_ASSERT_THROW (i16_converter->init (frames, D_Tri, 8), Exception);
49 CPPUNIT_ASSERT_THROW (i16_converter->init (frames, D_Tri, 32), Exception);
50 CPPUNIT_ASSERT_THROW (i16_converter->init (frames, D_Tri, 48), Exception);
52 boost::shared_ptr<SampleFormatConverter<uint8_t> > ui_converter (new SampleFormatConverter<uint8_t>(1));
53 ui_converter->init (frames, D_Tri, 8); // Doesn't throw
54 CPPUNIT_ASSERT_THROW (ui_converter->init (frames, D_Tri, 4), Exception);
55 CPPUNIT_ASSERT_THROW (ui_converter->init (frames, D_Tri, 16), Exception);
60 boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
61 boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
63 converter->init (frames, D_Tri, 32);
64 converter->add_output (sink);
65 nframes_t frames_output = 0;
68 ProcessContext<float> pc(random_data, frames / 2, 1);
69 converter->process (pc);
70 frames_output = sink->get_data().size();
71 CPPUNIT_ASSERT_EQUAL (frames / 2, frames_output);
75 ProcessContext<float> pc(random_data, frames, 1);
76 converter->process (pc);
77 frames_output = sink->get_data().size();
78 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
82 ProcessContext<float> pc(random_data, frames + 1, 1);
83 CPPUNIT_ASSERT_THROW(converter->process (pc), Exception);
89 boost::shared_ptr<SampleFormatConverter<float> > converter (new SampleFormatConverter<float>(1));
90 boost::shared_ptr<VectorSink<float> > sink (new VectorSink<float>());
91 nframes_t frames_output = 0;
93 converter->init(frames, D_Tri, 32);
94 converter->add_output (sink);
96 converter->set_clip_floats (false);
97 ProcessContext<float> const pc(random_data, frames, 1);
98 converter->process (pc);
99 frames_output = sink->get_data().size();
100 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
101 CPPUNIT_ASSERT (TestUtils::array_equals(sink->get_array(), random_data, frames));
103 // Make sure a few samples are < -1.0 and > 1.0
104 random_data[10] = -1.5;
105 random_data[20] = 1.5;
107 converter->set_clip_floats (true);
108 converter->process (pc);
109 frames_output = sink->get_data().size();
110 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
111 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
113 for (nframes_t i = 0; i < frames; ++i) {
114 // fp comparison needs a bit of tolerance, 1.01 << 1.5
115 CPPUNIT_ASSERT(sink->get_data()[i] < 1.01);
116 CPPUNIT_ASSERT(sink->get_data()[i] > -1.01);
122 boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
123 boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
124 nframes_t frames_output = 0;
126 converter->init(frames, D_Tri, 32);
127 converter->add_output (sink);
129 ProcessContext<float> pc(random_data, frames, 1);
130 converter->process (pc);
131 frames_output = sink->get_data().size();
132 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
133 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
138 boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
139 boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
140 nframes_t frames_output = 0;
142 converter->init(frames, D_Tri, 24);
143 converter->add_output (sink);
145 ProcessContext<float> pc(random_data, frames, 1);
146 converter->process (pc);
147 frames_output = sink->get_data().size();
148 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
149 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
154 boost::shared_ptr<SampleFormatConverter<int16_t> > converter (new SampleFormatConverter<int16_t>(1));
155 boost::shared_ptr<VectorSink<int16_t> > sink (new VectorSink<int16_t>());
156 nframes_t frames_output = 0;
158 converter->init(frames, D_Tri, 16);
159 converter->add_output (sink);
161 ProcessContext<float> pc(random_data, frames, 1);
162 converter->process (pc);
163 frames_output = sink->get_data().size();
164 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
165 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
170 boost::shared_ptr<SampleFormatConverter<uint8_t> > converter (new SampleFormatConverter<uint8_t>(1));
171 boost::shared_ptr<VectorSink<uint8_t> > sink (new VectorSink<uint8_t>());
172 nframes_t frames_output = 0;
174 converter->init(frames, D_Tri, 8);
175 converter->add_output (sink);
177 ProcessContext<float> pc(random_data, frames, 1);
178 converter->process (pc);
179 frames_output = sink->get_data().size();
180 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
181 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
184 void testChannelCount()
186 boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(3));
187 boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
188 nframes_t frames_output = 0;
190 converter->init(frames, D_Tri, 32);
191 converter->add_output (sink);
193 ProcessContext<float> pc(random_data, 4, 1);
194 CPPUNIT_ASSERT_THROW (converter->process (pc), Exception);
196 nframes_t new_frame_count = frames - (frames % 3);
197 converter->process (ProcessContext<float> (pc.data(), new_frame_count, 3));
198 frames_output = sink->get_data().size();
199 CPPUNIT_ASSERT_EQUAL (new_frame_count, frames_output);
200 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), pc.frames()));
209 CPPUNIT_TEST_SUITE_REGISTRATION (SampleFormatConverterTest);