aboutsummaryrefslogtreecommitdiff
path: root/test/audio/raw.h
blob: 51ec067f7e4d0f9ad6305f2bb93a8e768022b257 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <cxxtest/TestSuite.h>

#include "audio/decoders/raw.h"

#include "helper.h"

class RawStreamTestSuite : public CxxTest::TestSuite
{
private:
	template<typename T>
	void readBufferTestTemplate(const int sampleRate, const int time, const bool le, const bool isStereo, const bool makePartition = false) {
		int16 *sine;
		Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, &sine, le, isStereo, makePartition);

		const int totalSamples = sampleRate * time * (isStereo ? 2 : 1);
		int16 *buffer = new int16[totalSamples];
		TS_ASSERT_EQUALS(s->readBuffer(buffer, totalSamples), totalSamples);
		TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * totalSamples), 0);
		TS_ASSERT_EQUALS(s->endOfData(), true);

		delete[] sine;
		delete[] buffer;
		delete s;
	}

public:
	void test_read_buffer_8_bit_signed_mono() {
		readBufferTestTemplate<int8>(11025, 2, false, false);
	}

	void test_read_buffer_8_bit_signed_stereo() {
		readBufferTestTemplate<int8>(11025, 2, false, true);
	}

	void test_read_buffer_8_bit_unsigned_mono() {
		readBufferTestTemplate<uint8>(11025, 2, false, false);
	}

	void test_read_buffer_16_bit_signed_be_mono() {
		readBufferTestTemplate<int16>(11025, 2, false, false);
	}

	void test_read_buffer_16_bit_signed_be_stereo() {
		readBufferTestTemplate<int16>(11025, 2, false, true);
	}

	void test_read_buffer_16_bit_unsigned_be_mono() {
		readBufferTestTemplate<uint16>(11025, 2, false, false);
	}

	void test_read_buffer_16_bit_unsigned_be_stereo() {
		readBufferTestTemplate<uint16>(11025, 2, false, true);
	}

	void test_read_buffer_16_bit_signed_le_mono() {
		readBufferTestTemplate<int16>(11025, 2, true, false);
	}

	void test_read_buffer_16_bit_signed_le_stereo() {
		readBufferTestTemplate<int16>(11025, 2, true, true);
	}

	void test_read_buffer_16_bit_unsigned_le_mono() {
		readBufferTestTemplate<uint16>(11025, 2, true, false);
	}

	void test_read_buffer_16_bit_unsigned_le_stereo() {
		readBufferTestTemplate<uint16>(11025, 2, true, true);
	}

	void test_read_buffer_8_bit_signed_mono_parted() {
		readBufferTestTemplate<int8>(11025, 2, false, false, true);
	}

	void test_read_buffer_8_bit_signed_stereo_parted() {
		readBufferTestTemplate<int8>(11025, 2, false, true, true);
	}

	void test_read_buffer_8_bit_unsigned_mono_parted() {
		readBufferTestTemplate<uint8>(11025, 2, false, false, true);
	}

	void test_read_buffer_16_bit_signed_be_mono_parted() {
		readBufferTestTemplate<int16>(11025, 2, false, false, true);
	}

	void test_read_buffer_16_bit_signed_be_stereo_parted() {
		readBufferTestTemplate<int16>(11025, 2, false, true, true);
	}

	void test_read_buffer_16_bit_unsigned_be_mono_parted() {
		readBufferTestTemplate<uint16>(11025, 2, false, false, true);
	}

	void test_read_buffer_16_bit_unsigned_be_stereo_parted() {
		readBufferTestTemplate<uint16>(11025, 2, false, true, true);
	}

	void test_read_buffer_16_bit_signed_le_mono_parted() {
		readBufferTestTemplate<int16>(11025, 2, true, false, true);
	}

	void test_read_buffer_16_bit_signed_le_stereo_parted() {
		readBufferTestTemplate<int16>(11025, 2, true, true, true);
	}

	void test_read_buffer_16_bit_unsigned_le_mono_parted() {
		readBufferTestTemplate<uint16>(11025, 2, true, false, true);
	}

	void test_read_buffer_16_bit_unsigned_le_stereo_parted() {
		readBufferTestTemplate<uint16>(11025, 2, true, true, true);
	}

private:
	void partialReadTest(const bool makePartition) {
		const int sampleRate = 11025;
		const int time = 4;

		int16 *sine;
		Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, &sine, false, false, makePartition);
		int16 *buffer = new int16[sampleRate * time];

		TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate), sampleRate);
		TS_ASSERT_EQUALS(memcmp(sine, buffer, sampleRate), 0);
		TS_ASSERT_EQUALS(s->endOfData(), false);

		TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * 2), sampleRate * 2);
		TS_ASSERT_EQUALS(memcmp(sine + sampleRate, buffer, sampleRate * 2), 0);
		TS_ASSERT_EQUALS(s->endOfData(), false);

		TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate), sampleRate);
		TS_ASSERT_EQUALS(memcmp(sine + sampleRate * 3, buffer, sampleRate), 0);
		TS_ASSERT_EQUALS(s->endOfData(), true);

		delete[] sine;
		delete[] buffer;
		delete s;
	}
public:
	void test_partial_read() {
		partialReadTest(false);
	}

	void test_partial_read_parted() {
		partialReadTest(true);
	}

private:
	void readAfterEndTest(const bool makePartition) {
		const int sampleRate = 11025;
		const int time = 1;
		Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, 0, false, false);
		int16 *buffer = new int16[sampleRate * time];

		TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time);
		TS_ASSERT_EQUALS(s->endOfData(), true);

		TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), 0);
		TS_ASSERT_EQUALS(s->endOfData(), true);

		delete[] buffer;
		delete s;
	}

public:
	void test_read_after_end() {
		readAfterEndTest(false);
	}

	void test_read_after_end_parted() {
		readAfterEndTest(true);
	}

private:
	void rewindTest(const bool makePartition) {
		const int sampleRate = 11025;
		const int time = 2;
		Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, 0, false, false, makePartition);
		int16 *buffer = new int16[sampleRate * time];

		TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time);
		TS_ASSERT_EQUALS(s->endOfData(), true);

		s->rewind();
		TS_ASSERT_EQUALS(s->endOfData(), false);

		TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time);
		TS_ASSERT_EQUALS(s->endOfData(), true);

		delete[] buffer;
		delete s;
	}
public:
	void test_rewind() {
		rewindTest(false);
	}

	void test_rewind_parted() {
		rewindTest(true);
	}

private:
	void lengthTest(const bool makePartition) {
		int sampleRate = 0;
		const int time = 4;

		Audio::SeekableAudioStream *s = 0;

		// 11025 Hz tests
		sampleRate = 11025;
		s = createSineStream<int8>(sampleRate, time, 0, false, false, makePartition);
		TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
		delete s;

		s = createSineStream<uint16>(sampleRate, time, 0, false, false, makePartition);
		TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
		delete s;

		// 48000 Hz tests
		sampleRate = 48000;
		s = createSineStream<int8>(sampleRate, time, 0, false, false, makePartition);
		TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
		delete s;

		s = createSineStream<uint16>(sampleRate, time, 0, true, false, makePartition);
		TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
		delete s;

		// 11840 Hz tests
		sampleRate = 11840;
		s = createSineStream<int8>(sampleRate, time, 0, false, false, makePartition);
		TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
		delete s;

		s = createSineStream<uint16>(sampleRate, time, 0, false, false, makePartition);
		TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
		delete s;

		// 11111 Hz tests
		sampleRate = 11111;
		s = createSineStream<int8>(sampleRate, time, 0, false, false, makePartition);
		TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
		delete s;

		s = createSineStream<uint16>(sampleRate, time, 0, false, false, makePartition);
		TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
		delete s;

		// 22050 Hz stereo test
		sampleRate = 22050;
		s = createSineStream<int8>(sampleRate, time, 0, false, true, makePartition);
		TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
		delete s;

		s = createSineStream<uint16>(sampleRate, time, 0, true, true, makePartition);
		TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
		delete s;
	}

public:
	void test_length() {
		lengthTest(false);
	}

	void test_length_parted() {
		lengthTest(true);
	}

private:
	void seekTest(const int sampleRate, const int time, const bool isStereo, const bool makePartition) {
		const int totalFrames = sampleRate * time * (isStereo ? 2 : 1);
		int readData = 0, offset = 0;

		int16 *buffer = new int16[totalFrames];
		Audio::SeekableAudioStream *s = 0;
		int16 *sine = 0;

		s = createSineStream<int8>(sampleRate, time, &sine, false, isStereo, makePartition);

		// Seek to 500ms
		const Audio::Timestamp a(0, 1, 2);
		offset = Audio::convertTimeToStreamPos(a, sampleRate, isStereo).totalNumberOfFrames();
		readData = totalFrames - offset;

		TS_ASSERT_EQUALS(s->seek(a), true);
		TS_ASSERT_EQUALS(s->endOfData(), false);
		TS_ASSERT_EQUALS(s->readBuffer(buffer, readData), readData);
		TS_ASSERT_EQUALS(memcmp(buffer, sine + offset, readData * sizeof(int16)), 0);
		TS_ASSERT_EQUALS(s->endOfData(), true);

		// Seek to 3/4 of a second
		const Audio::Timestamp b(0, 3, 4);
		offset = Audio::convertTimeToStreamPos(b, sampleRate, isStereo).totalNumberOfFrames();
		readData = totalFrames - offset;

		TS_ASSERT_EQUALS(s->seek(b), true);
		TS_ASSERT_EQUALS(s->endOfData(), false);
		TS_ASSERT_EQUALS(s->readBuffer(buffer, readData), readData);
		TS_ASSERT_EQUALS(memcmp(buffer, sine + offset, readData * sizeof(int16)), 0);
		TS_ASSERT_EQUALS(s->endOfData(), true);

		// Seek to the start of the stream
		TS_ASSERT_EQUALS(s->seek(0), true);
		TS_ASSERT_EQUALS(s->endOfData(), false);

		// Seek to the mid of the stream
		const Audio::Timestamp c(time * 1000 / 2, 1000);
		offset = Audio::convertTimeToStreamPos(c, sampleRate, isStereo).totalNumberOfFrames();
		readData = totalFrames - offset;

		TS_ASSERT_EQUALS(s->seek(c), true);
		TS_ASSERT_EQUALS(s->endOfData(), false);
		TS_ASSERT_EQUALS(s->readBuffer(buffer, readData), readData);
		TS_ASSERT_EQUALS(memcmp(buffer, sine + offset, readData * sizeof(int16)), 0);
		TS_ASSERT_EQUALS(s->endOfData(), true);

		// Seek to the 1/4th of the last second of the stream
		const Audio::Timestamp d(time - 1, 1, 4);
		offset = Audio::convertTimeToStreamPos(d, sampleRate, isStereo).totalNumberOfFrames();
		readData = totalFrames - offset;

		TS_ASSERT_EQUALS(s->seek(d), true);
		TS_ASSERT_EQUALS(s->endOfData(), false);
		TS_ASSERT_EQUALS(s->readBuffer(buffer, readData), readData);
		TS_ASSERT_EQUALS(memcmp(buffer, sine + offset, readData * sizeof(int16)), 0);
		TS_ASSERT_EQUALS(s->endOfData(), true);

		// Try to seek after the end of the stream
		TS_ASSERT_EQUALS(s->seek(Audio::Timestamp(time, 1, 100000)), false);
		TS_ASSERT_EQUALS(s->endOfData(), true);

		// Try to seek exactly at the end of the stream
		TS_ASSERT_EQUALS(s->seek(Audio::Timestamp(time * 1000, 1000)), true);
		TS_ASSERT_EQUALS(s->endOfData(), true);

		delete[] sine;
		delete s;
		delete[] buffer;
	}

public:
	void test_seek_mono() {
		seekTest(11025, 2, false, false);
	}

	void test_seek_stereo() {
		seekTest(11025, 2, true, false);
	}

	void test_seek_mono_parted() {
		seekTest(11025, 2, false, true);
	}

	void test_seek_stereo_parted() {
		seekTest(11025, 2, true, true);
	}
};