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
|
#include <cxxtest/TestSuite.h>
#include "audio/decoders/raw.h"
#include "audio/audiostream.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) {
int16 *sine;
Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, &sine, le, isStereo);
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);
}
private:
void partialReadTest() {
const int sampleRate = 11025;
const int time = 4;
int16 *sine;
Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, &sine, false, false);
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();
}
private:
void readAfterEndTest() {
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();
}
private:
void rewindTest() {
const int sampleRate = 11025;
const int time = 2;
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);
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();
}
private:
void lengthTest() {
int sampleRate = 0;
const int time = 4;
Audio::SeekableAudioStream *s = 0;
// 11025 Hz tests
sampleRate = 11025;
s = createSineStream<int8>(sampleRate, time, 0, false, false);
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
delete s;
s = createSineStream<uint16>(sampleRate, time, 0, false, false);
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
delete s;
// 48000 Hz tests
sampleRate = 48000;
s = createSineStream<int8>(sampleRate, time, 0, false, false);
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
delete s;
s = createSineStream<uint16>(sampleRate, time, 0, true, false);
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
delete s;
// 11840 Hz tests
sampleRate = 11840;
s = createSineStream<int8>(sampleRate, time, 0, false, false);
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
delete s;
s = createSineStream<uint16>(sampleRate, time, 0, false, false);
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
delete s;
// 11111 Hz tests
sampleRate = 11111;
s = createSineStream<int8>(sampleRate, time, 0, false, false);
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
delete s;
s = createSineStream<uint16>(sampleRate, time, 0, false, false);
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
delete s;
// 22050 Hz stereo test
sampleRate = 22050;
s = createSineStream<int8>(sampleRate, time, 0, false, true);
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
delete s;
s = createSineStream<uint16>(sampleRate, time, 0, true, true);
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
delete s;
}
public:
void test_length() {
lengthTest();
}
private:
void seekTest(const int sampleRate, const int time, const bool isStereo) {
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);
// 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);
}
void test_seek_stereo() {
seekTest(11025, 2, true);
}
};
|