Fix export, which has been broken since the boost::signals2 changes. Also update...
[ardour.git] / libs / audiographer / tests / general / sample_format_converter_test.cc
1 #include "tests/utils.h"
2
3 #include "audiographer/general/sample_format_converter.h"
4
5 using namespace AudioGrapher;
6
7 class SampleFormatConverterTest : public CppUnit::TestFixture
8 {
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 ();
19
20   public:
21         void setUp()
22         {
23                 frames = 128;
24                 random_data = TestUtils::init_random_data(frames, 1.0);
25         }
26
27         void tearDown()
28         {
29                 delete [] random_data;
30         }
31
32         void testInit()
33         {
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);
38                 
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);
45                 
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);
51                 
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);
56         }
57
58         void testFrameCount()
59         {
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>());
62                 
63                 converter->init (frames, D_Tri, 32);
64                 converter->add_output (sink);
65                 nframes_t frames_output = 0;
66                 
67                 {
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);
72                 }
73                 
74                 {
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);
79                 }
80                 
81                 {
82                 ProcessContext<float> pc(random_data, frames + 1, 1);
83                 CPPUNIT_ASSERT_THROW(converter->process (pc), Exception);
84                 }
85         }
86
87         void testFloat()
88         {
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;
92                 
93                 converter->init(frames, D_Tri, 32);
94                 converter->add_output (sink);
95                 
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));
102                 
103                 // Make sure a few samples are < -1.0 and > 1.0
104                 random_data[10] = -1.5;
105                 random_data[20] = 1.5;
106                 
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));
112                 
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);
117                 }
118         }
119
120         void testInt32()
121         {
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;
125                 
126                 converter->init(frames, D_Tri, 32);
127                 converter->add_output (sink);
128                 
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));
134         }
135         
136         void testInt24()
137         {
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;
141                 
142                 converter->init(frames, D_Tri, 24);
143                 converter->add_output (sink);
144                 
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));
150         }
151         
152         void testInt16()
153         {
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;
157                 
158                 converter->init(frames, D_Tri, 16);
159                 converter->add_output (sink);
160                 
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));
166         }
167         
168         void testUint8()
169         {
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;
173                 
174                 converter->init(frames, D_Tri, 8);
175                 converter->add_output (sink);
176                 
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));
182         }
183         
184         void testChannelCount()
185         {
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;
189                 
190                 converter->init(frames, D_Tri, 32);
191                 converter->add_output (sink);
192                 
193                 ProcessContext<float> pc(random_data, 4, 1);
194                 CPPUNIT_ASSERT_THROW (converter->process (pc), Exception);
195                 
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()));
201         }
202
203   private:
204
205         float * random_data;
206         nframes_t frames;
207 };
208
209 CPPUNIT_TEST_SUITE_REGISTRATION (SampleFormatConverterTest);
210