summaryrefslogtreecommitdiff
path: root/libs/audiographer/tests/general/sample_format_converter_test.cc
blob: 734389a427a40dc677619101b930fee907187e2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "tests/utils.h"

#include "audiographer/general/sample_format_converter.h"

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()
	{
		frames = 128;
		random_data = TestUtils::init_random_data(frames, 1.0);
	}

	void tearDown()
	{
		delete [] random_data;
	}

	void testInit()
	{
		// Float never uses dithering and should always use full 32 bits of data
		boost::shared_ptr<SampleFormatConverter<float> > f_converter (new SampleFormatConverter<float>(1));
		f_converter->init (frames, D_Tri, 32); // Doesn't throw
		CPPUNIT_ASSERT_THROW (f_converter->init (frames, D_Tri, 24), Exception);
		CPPUNIT_ASSERT_THROW (f_converter->init (frames, D_Tri, 48), Exception);

		/* Test that too large data widths throw.
		   We are fine with unnecessarily narrow data widths */

		boost::shared_ptr<SampleFormatConverter<int32_t> > i_converter (new SampleFormatConverter<int32_t>(1));
		i_converter->init (frames, D_Tri, 32); // Doesn't throw
		i_converter->init (frames, D_Tri, 24); // Doesn't throw
		i_converter->init (frames, D_Tri, 8); // Doesn't throw
		i_converter->init (frames, D_Tri, 16); // Doesn't throw
		CPPUNIT_ASSERT_THROW (i_converter->init (frames, D_Tri, 48), Exception);

		boost::shared_ptr<SampleFormatConverter<int16_t> > i16_converter (new SampleFormatConverter<int16_t>(1));
		i16_converter->init (frames, D_Tri, 16); // Doesn't throw
		i16_converter->init (frames, D_Tri, 8); // Doesn't throw
		CPPUNIT_ASSERT_THROW (i16_converter->init (frames, D_Tri, 32), Exception);
		CPPUNIT_ASSERT_THROW (i16_converter->init (frames, D_Tri, 48), Exception);

		boost::shared_ptr<SampleFormatConverter<uint8_t> > ui_converter (new SampleFormatConverter<uint8_t>(1));
		ui_converter->init (frames, D_Tri, 8); // Doesn't throw
		ui_converter->init (frames, D_Tri, 4); // Doesn't throw
		CPPUNIT_ASSERT_THROW (ui_converter->init (frames, D_Tri, 16), Exception);
	}

	void testFrameCount()
	{
		boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
		boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());

		converter->init (frames, D_Tri, 32);
		converter->add_output (sink);
		framecnt_t frames_output = 0;

		{
		ProcessContext<float> pc(random_data, frames / 2, 1);
		converter->process (pc);
		frames_output = sink->get_data().size();
		CPPUNIT_ASSERT_EQUAL (frames / 2, frames_output);
		}

		{
		ProcessContext<float> pc(random_data, frames, 1);
		converter->process (pc);
		frames_output = sink->get_data().size();
		CPPUNIT_ASSERT_EQUAL (frames, frames_output);
		}

		{
		ProcessContext<float> pc(random_data, frames + 1, 1);
		CPPUNIT_ASSERT_THROW(converter->process (pc), Exception);
		}
	}

	void testFloat()
	{
		boost::shared_ptr<SampleFormatConverter<float> > converter (new SampleFormatConverter<float>(1));
		boost::shared_ptr<VectorSink<float> > sink (new VectorSink<float>());
		framecnt_t frames_output = 0;

		converter->init(frames, D_Tri, 32);
		converter->add_output (sink);

		converter->set_clip_floats (false);
		ProcessContext<float> const pc(random_data, frames, 1);
		converter->process (pc);
		frames_output = sink->get_data().size();
		CPPUNIT_ASSERT_EQUAL (frames, frames_output);
		CPPUNIT_ASSERT (TestUtils::array_equals(sink->get_array(), random_data, frames));

		// Make sure a few samples are < -1.0 and > 1.0
		random_data[10] = -1.5;
		random_data[20] = 1.5;

		converter->set_clip_floats (true);
		converter->process (pc);
		frames_output = sink->get_data().size();
		CPPUNIT_ASSERT_EQUAL (frames, frames_output);
		CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));

		for (framecnt_t i = 0; i < frames; ++i) {
			// 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()
	{
		boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
		boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
		framecnt_t frames_output = 0;

		converter->init(frames, D_Tri, 32);
		converter->add_output (sink);

		ProcessContext<float> pc(random_data, frames, 1);
		converter->process (pc);
		frames_output = sink->get_data().size();
		CPPUNIT_ASSERT_EQUAL (frames, frames_output);
		CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
	}

	void testInt24()
	{
		boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
		boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
		framecnt_t frames_output = 0;

		converter->init(frames, D_Tri, 24);
		converter->add_output (sink);

		ProcessContext<float> pc(random_data, frames, 1);
		converter->process (pc);
		frames_output = sink->get_data().size();
		CPPUNIT_ASSERT_EQUAL (frames, frames_output);
		CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
	}

	void testInt16()
	{
		boost::shared_ptr<SampleFormatConverter<int16_t> > converter (new SampleFormatConverter<int16_t>(1));
		boost::shared_ptr<VectorSink<int16_t> > sink (new VectorSink<int16_t>());
		framecnt_t frames_output = 0;

		converter->init(frames, D_Tri, 16);
		converter->add_output (sink);

		ProcessContext<float> pc(random_data, frames, 1);
		converter->process (pc);
		frames_output = sink->get_data().size();
		CPPUNIT_ASSERT_EQUAL (frames, frames_output);
		CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
	}

	void testUint8()
	{
		boost::shared_ptr<SampleFormatConverter<uint8_t> > converter (new SampleFormatConverter<uint8_t>(1));
		boost::shared_ptr<VectorSink<uint8_t> > sink (new VectorSink<uint8_t>());
		framecnt_t frames_output = 0;

		converter->init(frames, D_Tri, 8);
		converter->add_output (sink);

		ProcessContext<float> pc(random_data, frames, 1);
		converter->process (pc);
		frames_output = sink->get_data().size();
		CPPUNIT_ASSERT_EQUAL (frames, frames_output);
		CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
	}

	void testChannelCount()
	{
		boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(3));
		boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
		framecnt_t frames_output = 0;

		converter->init(frames, D_Tri, 32);
		converter->add_output (sink);

		ProcessContext<float> pc(random_data, 4, 1);
		CPPUNIT_ASSERT_THROW (converter->process (pc), Exception);

		framecnt_t new_frame_count = frames - (frames % 3);
		converter->process (ProcessContext<float> (pc.data(), new_frame_count, 3));
		frames_output = sink->get_data().size();
		CPPUNIT_ASSERT_EQUAL (new_frame_count, frames_output);
		CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), pc.frames()));
	}

  private:

	float * random_data;
	framecnt_t frames;
};

CPPUNIT_TEST_SUITE_REGISTRATION (SampleFormatConverterTest);