1 | /* $Id: tstAudioMixBuffer.cpp 68132 2017-07-27 08:15:43Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Audio testcase - Mixing buffer.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <iprt/err.h>
|
---|
23 | #include <iprt/initterm.h>
|
---|
24 | #include <iprt/mem.h>
|
---|
25 | #include <iprt/rand.h>
|
---|
26 | #include <iprt/stream.h>
|
---|
27 | #include <iprt/string.h>
|
---|
28 | #include <iprt/test.h>
|
---|
29 |
|
---|
30 |
|
---|
31 | #include "../AudioMixBuffer.h"
|
---|
32 | #include "../DrvAudio.h"
|
---|
33 |
|
---|
34 |
|
---|
35 | /*********************************************************************************************************************************
|
---|
36 | * Structures and Typedefs *
|
---|
37 | *********************************************************************************************************************************/
|
---|
38 |
|
---|
39 | static int tstSingle(RTTEST hTest)
|
---|
40 | {
|
---|
41 | RTTestSubF(hTest, "Single buffer");
|
---|
42 |
|
---|
43 | /* 44100Hz, 2 Channels, S16 */
|
---|
44 | PDMAUDIOPCMPROPS config =
|
---|
45 | {
|
---|
46 | 16, /* Bits */
|
---|
47 | true, /* Signed */
|
---|
48 | 2, /* Channels */
|
---|
49 | 44100, /* Hz */
|
---|
50 | PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(16 /* Bits */, 2 /* Channels */), /* Shift */
|
---|
51 | false /* Swap Endian */
|
---|
52 | };
|
---|
53 |
|
---|
54 | RTTESTI_CHECK(DrvAudioHlpPCMPropsAreValid(&config));
|
---|
55 |
|
---|
56 | uint32_t cBufSize = _1K;
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * General stuff.
|
---|
60 | */
|
---|
61 | PDMAUDIOMIXBUF mb;
|
---|
62 | RTTESTI_CHECK_RC_OK(AudioMixBufInit(&mb, "Single", &config, cBufSize));
|
---|
63 | RTTESTI_CHECK(AudioMixBufSize(&mb) == cBufSize);
|
---|
64 | RTTESTI_CHECK(AUDIOMIXBUF_B2F(&mb, AudioMixBufSizeBytes(&mb)) == cBufSize);
|
---|
65 | RTTESTI_CHECK(AUDIOMIXBUF_F2B(&mb, AudioMixBufSize(&mb)) == AudioMixBufSizeBytes(&mb));
|
---|
66 | RTTESTI_CHECK(AudioMixBufFree(&mb) == cBufSize);
|
---|
67 | RTTESTI_CHECK(AUDIOMIXBUF_F2B(&mb, AudioMixBufFree(&mb)) == AudioMixBufFreeBytes(&mb));
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * Absolute writes.
|
---|
71 | */
|
---|
72 | uint32_t cFramesRead = 0, cFramesWritten = 0, cFramesWrittenAbs = 0;
|
---|
73 | int8_t aFrames8 [2] = { 0x12, 0x34 };
|
---|
74 | int16_t aFrames16[2] = { 0xAA, 0xBB };
|
---|
75 | int32_t aFrames32[2] = { 0xCC, 0xDD };
|
---|
76 |
|
---|
77 | RTTESTI_CHECK_RC_OK(AudioMixBufWriteAt(&mb, 0 /* Offset */, &aFrames8, sizeof(aFrames8), &cFramesWritten));
|
---|
78 | RTTESTI_CHECK(cFramesWritten == 0 /* Frames */);
|
---|
79 | RTTESTI_CHECK(AudioMixBufUsed(&mb) == 0);
|
---|
80 |
|
---|
81 | RTTESTI_CHECK_RC_OK(AudioMixBufWriteAt(&mb, 0 /* Offset */, &aFrames16, sizeof(aFrames16), &cFramesWritten));
|
---|
82 | RTTESTI_CHECK(cFramesWritten == 1 /* Frames */);
|
---|
83 | RTTESTI_CHECK(AudioMixBufUsed(&mb) == 1);
|
---|
84 |
|
---|
85 | RTTESTI_CHECK_RC_OK(AudioMixBufWriteAt(&mb, 2 /* Offset */, &aFrames32, sizeof(aFrames32), &cFramesWritten));
|
---|
86 | RTTESTI_CHECK(cFramesWritten == 2 /* Frames */);
|
---|
87 | RTTESTI_CHECK(AudioMixBufUsed(&mb) == 2);
|
---|
88 |
|
---|
89 | /* Beyond buffer. */
|
---|
90 | RTTESTI_CHECK_RC(AudioMixBufWriteAt(&mb, AudioMixBufSize(&mb) + 1, &aFrames16, sizeof(aFrames16),
|
---|
91 | &cFramesWritten), VERR_BUFFER_OVERFLOW);
|
---|
92 |
|
---|
93 | /* Offset wrap-around: When writing as much (or more) frames the mixing buffer can hold. */
|
---|
94 | uint32_t cbSamples = cBufSize * sizeof(int16_t) * 2 /* Channels */;
|
---|
95 | RTTESTI_CHECK(cbSamples);
|
---|
96 | uint16_t *paSamples = (uint16_t *)RTMemAlloc(cbSamples);
|
---|
97 | RTTESTI_CHECK(paSamples);
|
---|
98 | RTTESTI_CHECK_RC_OK(AudioMixBufWriteAt(&mb, 0 /* Offset */, paSamples, cbSamples, &cFramesWritten));
|
---|
99 | RTTESTI_CHECK(cFramesWritten == cBufSize /* Frames */);
|
---|
100 | RTTESTI_CHECK(AudioMixBufUsed(&mb) == cBufSize);
|
---|
101 | RTTESTI_CHECK(AudioMixBufReadPos(&mb) == 0);
|
---|
102 | RTTESTI_CHECK(AudioMixBufWritePos(&mb) == 0);
|
---|
103 | RTMemFree(paSamples);
|
---|
104 | cbSamples = 0;
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Circular writes.
|
---|
108 | */
|
---|
109 | AudioMixBufReset(&mb);
|
---|
110 |
|
---|
111 | RTTESTI_CHECK_RC_OK(AudioMixBufWriteAt(&mb, 2 /* Offset */, &aFrames32, sizeof(aFrames32), &cFramesWritten));
|
---|
112 | RTTESTI_CHECK(cFramesWritten == 2 /* Frames */);
|
---|
113 | RTTESTI_CHECK(AudioMixBufUsed(&mb) == 2);
|
---|
114 |
|
---|
115 | cFramesWrittenAbs = AudioMixBufUsed(&mb);
|
---|
116 |
|
---|
117 | uint32_t cToWrite = AudioMixBufSize(&mb) - cFramesWrittenAbs - 1; /* -1 as padding plus -2 frames for above. */
|
---|
118 | for (uint32_t i = 0; i < cToWrite; i++)
|
---|
119 | {
|
---|
120 | RTTESTI_CHECK_RC_OK(AudioMixBufWriteCirc(&mb, &aFrames16, sizeof(aFrames16), &cFramesWritten));
|
---|
121 | RTTESTI_CHECK(cFramesWritten == 1);
|
---|
122 | }
|
---|
123 | RTTESTI_CHECK(!AudioMixBufIsEmpty(&mb));
|
---|
124 | RTTESTI_CHECK(AudioMixBufFree(&mb) == 1);
|
---|
125 | RTTESTI_CHECK(AudioMixBufFreeBytes(&mb) == AUDIOMIXBUF_F2B(&mb, 1U));
|
---|
126 | RTTESTI_CHECK(AudioMixBufUsed(&mb) == cToWrite + cFramesWrittenAbs /* + last absolute write */);
|
---|
127 |
|
---|
128 | RTTESTI_CHECK_RC_OK(AudioMixBufWriteCirc(&mb, &aFrames16, sizeof(aFrames16), &cFramesWritten));
|
---|
129 | RTTESTI_CHECK(cFramesWritten == 1);
|
---|
130 | RTTESTI_CHECK(AudioMixBufFree(&mb) == 0);
|
---|
131 | RTTESTI_CHECK(AudioMixBufFreeBytes(&mb) == AUDIOMIXBUF_F2B(&mb, 0U));
|
---|
132 | RTTESTI_CHECK(AudioMixBufUsed(&mb) == cBufSize);
|
---|
133 |
|
---|
134 | /* Circular reads. */
|
---|
135 | uint32_t cToRead = AudioMixBufSize(&mb) - cFramesWrittenAbs - 1;
|
---|
136 | for (uint32_t i = 0; i < cToRead; i++)
|
---|
137 | {
|
---|
138 | RTTESTI_CHECK_RC_OK(AudioMixBufReadCirc(&mb, &aFrames16, sizeof(aFrames16), &cFramesRead));
|
---|
139 | RTTESTI_CHECK(cFramesRead == 1);
|
---|
140 | AudioMixBufFinish(&mb, cFramesRead);
|
---|
141 | }
|
---|
142 | RTTESTI_CHECK(!AudioMixBufIsEmpty(&mb));
|
---|
143 | RTTESTI_CHECK(AudioMixBufFree(&mb) == AudioMixBufSize(&mb) - cFramesWrittenAbs - 1);
|
---|
144 | RTTESTI_CHECK(AudioMixBufFreeBytes(&mb) == AUDIOMIXBUF_F2B(&mb, cBufSize - cFramesWrittenAbs - 1));
|
---|
145 | RTTESTI_CHECK(AudioMixBufUsed(&mb) == cBufSize - cToRead);
|
---|
146 |
|
---|
147 | RTTESTI_CHECK_RC_OK(AudioMixBufReadCirc(&mb, &aFrames16, sizeof(aFrames16), &cFramesRead));
|
---|
148 | RTTESTI_CHECK(cFramesRead == 1);
|
---|
149 | AudioMixBufFinish(&mb, cFramesRead);
|
---|
150 | RTTESTI_CHECK(AudioMixBufFree(&mb) == cBufSize - cFramesWrittenAbs);
|
---|
151 | RTTESTI_CHECK(AudioMixBufFreeBytes(&mb) == AUDIOMIXBUF_F2B(&mb, cBufSize - cFramesWrittenAbs));
|
---|
152 | RTTESTI_CHECK(AudioMixBufUsed(&mb) == cFramesWrittenAbs);
|
---|
153 |
|
---|
154 | AudioMixBufDestroy(&mb);
|
---|
155 |
|
---|
156 | return RTTestSubErrorCount(hTest) ? VERR_GENERAL_FAILURE : VINF_SUCCESS;
|
---|
157 | }
|
---|
158 |
|
---|
159 | static int tstParentChild(RTTEST hTest)
|
---|
160 | {
|
---|
161 | uint32_t cParentBufSize = RTRandU32Ex(_1K /* Min */, _16K /* Max */); /* Enough room for random sizes */
|
---|
162 |
|
---|
163 | /* 44100Hz, 2 Channels, S16 */
|
---|
164 | PDMAUDIOPCMPROPS cfg_p =
|
---|
165 | {
|
---|
166 | 16, /* Bits */
|
---|
167 | true, /* Signed */
|
---|
168 | 2, /* Channels */
|
---|
169 | 44100, /* Hz */
|
---|
170 | PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(16 /* Bits */, 2 /* Channels */), /* Shift */
|
---|
171 | false /* Swap Endian */
|
---|
172 | };
|
---|
173 |
|
---|
174 | RTTESTI_CHECK(DrvAudioHlpPCMPropsAreValid(&cfg_p));
|
---|
175 |
|
---|
176 | PDMAUDIOMIXBUF parent;
|
---|
177 | RTTESTI_CHECK_RC_OK(AudioMixBufInit(&parent, "Parent", &cfg_p, cParentBufSize));
|
---|
178 |
|
---|
179 | /* 22050Hz, 2 Channels, S16 */
|
---|
180 | PDMAUDIOPCMPROPS cfg_c1 = /* Upmixing to parent */
|
---|
181 | {
|
---|
182 | 16, /* Bits */
|
---|
183 | true, /* Signed */
|
---|
184 | 2, /* Channels */
|
---|
185 | 22050, /* Hz */
|
---|
186 | PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(16 /* Bits */, 2 /* Channels */), /* Shift */
|
---|
187 | false /* Swap Endian */
|
---|
188 | };
|
---|
189 |
|
---|
190 | RTTESTI_CHECK(DrvAudioHlpPCMPropsAreValid(&cfg_c1));
|
---|
191 |
|
---|
192 | uint32_t cFrames = 16;
|
---|
193 | uint32_t cChildBufSize = RTRandU32Ex(cFrames /* Min */, 64 /* Max */);
|
---|
194 |
|
---|
195 | PDMAUDIOMIXBUF child1;
|
---|
196 | RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child1, "Child1", &cfg_c1, cChildBufSize));
|
---|
197 | RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child1, &parent));
|
---|
198 |
|
---|
199 | /* 48000Hz, 2 Channels, S16 */
|
---|
200 | PDMAUDIOPCMPROPS cfg_c2 = /* Downmixing to parent */
|
---|
201 | {
|
---|
202 | 16, /* Bits */
|
---|
203 | true, /* Signed */
|
---|
204 | 2, /* Channels */
|
---|
205 | 48000, /* Hz */
|
---|
206 | PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(16 /* Bits */, 2 /* Channels */), /* Shift */
|
---|
207 | false /* Swap Endian */
|
---|
208 | };
|
---|
209 |
|
---|
210 | RTTESTI_CHECK(DrvAudioHlpPCMPropsAreValid(&cfg_c2));
|
---|
211 |
|
---|
212 | PDMAUDIOMIXBUF child2;
|
---|
213 | RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child2, "Child2", &cfg_c2, cChildBufSize));
|
---|
214 | RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child2, &parent));
|
---|
215 |
|
---|
216 | /*
|
---|
217 | * Writing + mixing from child/children -> parent, sequential.
|
---|
218 | */
|
---|
219 | uint32_t cbBuf = _1K;
|
---|
220 | char pvBuf[_1K];
|
---|
221 | int16_t aFrames16[32] = { 0xAA, 0xBB };
|
---|
222 | uint32_t cFramesRead, cFramesWritten, cFramesMixed;
|
---|
223 |
|
---|
224 | uint32_t cFramesChild1 = cFrames;
|
---|
225 | uint32_t cFramesChild2 = cFrames;
|
---|
226 |
|
---|
227 | uint32_t t = RTRandU32() % 32;
|
---|
228 |
|
---|
229 | RTTestPrintf(hTest, RTTESTLVL_DEBUG,
|
---|
230 | "cParentBufSize=%RU32, cChildBufSize=%RU32, %RU32 frames -> %RU32 iterations total\n",
|
---|
231 | cParentBufSize, cChildBufSize, cFrames, t);
|
---|
232 |
|
---|
233 | /*
|
---|
234 | * Using AudioMixBufWriteAt for writing to children.
|
---|
235 | */
|
---|
236 | RTTestSubF(hTest, "2 Children -> Parent (AudioMixBufWriteAt)");
|
---|
237 |
|
---|
238 | uint32_t cChildrenSamplesMixedTotal = 0;
|
---|
239 |
|
---|
240 | for (uint32_t i = 0; i < t; i++)
|
---|
241 | {
|
---|
242 | RTTestPrintf(hTest, RTTESTLVL_DEBUG, "i=%RU32\n", i);
|
---|
243 |
|
---|
244 | uint32_t cChild1Writes = RTRandU32() % 8;
|
---|
245 |
|
---|
246 | for (uint32_t c1 = 0; c1 < cChild1Writes; c1++)
|
---|
247 | {
|
---|
248 | /* Child 1. */
|
---|
249 | RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufWriteAt(&child1, 0, &aFrames16, sizeof(aFrames16), &cFramesWritten));
|
---|
250 | RTTESTI_CHECK_MSG_BREAK(cFramesWritten == cFramesChild1, ("Child1: Expected %RU32 written frames, got %RU32\n", cFramesChild1, cFramesWritten));
|
---|
251 | RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufMixToParent(&child1, cFramesWritten, &cFramesMixed));
|
---|
252 |
|
---|
253 | cChildrenSamplesMixedTotal += cFramesMixed;
|
---|
254 |
|
---|
255 | RTTESTI_CHECK_MSG_BREAK(cFramesWritten == cFramesMixed, ("Child1: Expected %RU32 mixed frames, got %RU32\n", cFramesWritten, cFramesMixed));
|
---|
256 | RTTESTI_CHECK_MSG_BREAK(AudioMixBufUsed(&child1) == 0, ("Child1: Expected %RU32 used frames, got %RU32\n", 0, AudioMixBufUsed(&child1)));
|
---|
257 | }
|
---|
258 |
|
---|
259 | uint32_t cChild2Writes = RTRandU32() % 8;
|
---|
260 |
|
---|
261 | for (uint32_t c2 = 0; c2 < cChild2Writes; c2++)
|
---|
262 | {
|
---|
263 | /* Child 2. */
|
---|
264 | RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufWriteAt(&child2, 0, &aFrames16, sizeof(aFrames16), &cFramesWritten));
|
---|
265 | RTTESTI_CHECK_MSG_BREAK(cFramesWritten == cFramesChild2, ("Child2: Expected %RU32 written frames, got %RU32\n", cFramesChild2, cFramesWritten));
|
---|
266 | RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufMixToParent(&child2, cFramesWritten, &cFramesMixed));
|
---|
267 |
|
---|
268 | cChildrenSamplesMixedTotal += cFramesMixed;
|
---|
269 |
|
---|
270 | RTTESTI_CHECK_MSG_BREAK(cFramesWritten == cFramesMixed, ("Child2: Expected %RU32 mixed frames, got %RU32\n", cFramesWritten, cFramesMixed));
|
---|
271 | RTTESTI_CHECK_MSG_BREAK(AudioMixBufUsed(&child2) == 0, ("Child2: Expected %RU32 used frames, got %RU32\n", 0, AudioMixBufUsed(&child2)));
|
---|
272 | }
|
---|
273 |
|
---|
274 | /*
|
---|
275 | * Read out all frames from the parent buffer and also mark the just-read frames as finished
|
---|
276 | * so that both connected children buffers can keep track of their stuff.
|
---|
277 | */
|
---|
278 | uint32_t cParentSamples = AudioMixBufUsed(&parent);
|
---|
279 | while (cParentSamples)
|
---|
280 | {
|
---|
281 | RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufReadCirc(&parent, pvBuf, cbBuf, &cFramesRead));
|
---|
282 | if (!cFramesRead)
|
---|
283 | break;
|
---|
284 |
|
---|
285 | AudioMixBufFinish(&parent, cFramesRead);
|
---|
286 |
|
---|
287 | RTTESTI_CHECK(cParentSamples >= cFramesRead);
|
---|
288 | cParentSamples -= cFramesRead;
|
---|
289 | }
|
---|
290 |
|
---|
291 | RTTESTI_CHECK(cParentSamples == 0);
|
---|
292 | }
|
---|
293 |
|
---|
294 | RTTESTI_CHECK(AudioMixBufUsed(&parent) == 0);
|
---|
295 | RTTESTI_CHECK(AudioMixBufLive(&child1) == 0);
|
---|
296 | RTTESTI_CHECK(AudioMixBufLive(&child2) == 0);
|
---|
297 |
|
---|
298 | AudioMixBufDestroy(&parent);
|
---|
299 | AudioMixBufDestroy(&child1);
|
---|
300 | AudioMixBufDestroy(&child2);
|
---|
301 |
|
---|
302 | return RTTestSubErrorCount(hTest) ? VERR_GENERAL_FAILURE : VINF_SUCCESS;
|
---|
303 | }
|
---|
304 |
|
---|
305 | /* Test 8-bit sample conversion (8-bit -> internal -> 8-bit). */
|
---|
306 | static int tstConversion8(RTTEST hTest)
|
---|
307 | {
|
---|
308 | unsigned i;
|
---|
309 | uint32_t cBufSize = 256;
|
---|
310 |
|
---|
311 | RTTestSubF(hTest, "Sample conversion (U8)");
|
---|
312 |
|
---|
313 | /* 44100Hz, 1 Channel, U8 */
|
---|
314 | PDMAUDIOPCMPROPS cfg_p =
|
---|
315 | {
|
---|
316 | 8, /* Bits */
|
---|
317 | false, /* Signed */
|
---|
318 | 1, /* Channels */
|
---|
319 | 44100, /* Hz */
|
---|
320 | PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(8 /* Bits */, 1 /* Channels */), /* Shift */
|
---|
321 | false /* Swap Endian */
|
---|
322 | };
|
---|
323 |
|
---|
324 | RTTESTI_CHECK(DrvAudioHlpPCMPropsAreValid(&cfg_p));
|
---|
325 |
|
---|
326 | PDMAUDIOMIXBUF parent;
|
---|
327 | RTTESTI_CHECK_RC_OK(AudioMixBufInit(&parent, "Parent", &cfg_p, cBufSize));
|
---|
328 |
|
---|
329 | /* Child uses half the sample rate; that ensures the mixing engine can't
|
---|
330 | * take shortcuts and performs conversion. Because conversion to double
|
---|
331 | * the sample rate effectively inserts one additional sample between every
|
---|
332 | * two source frames, N source frames will be converted to N * 2 - 1
|
---|
333 | * frames. However, the last source sample will be saved for later
|
---|
334 | * interpolation and not immediately output.
|
---|
335 | */
|
---|
336 |
|
---|
337 | /* 22050Hz, 1 Channel, U8 */
|
---|
338 | PDMAUDIOPCMPROPS cfg_c = /* Upmixing to parent */
|
---|
339 | {
|
---|
340 | 8, /* Bits */
|
---|
341 | false, /* Signed */
|
---|
342 | 1, /* Channels */
|
---|
343 | 22050, /* Hz */
|
---|
344 | PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(8 /* Bits */, 1 /* Channels */), /* Shift */
|
---|
345 | false /* Swap Endian */
|
---|
346 | };
|
---|
347 |
|
---|
348 | RTTESTI_CHECK(DrvAudioHlpPCMPropsAreValid(&cfg_c));
|
---|
349 |
|
---|
350 | PDMAUDIOMIXBUF child;
|
---|
351 | RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child, "Child", &cfg_c, cBufSize));
|
---|
352 | RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child, &parent));
|
---|
353 |
|
---|
354 | /* 8-bit unsigned frames. Often used with SB16 device. */
|
---|
355 | uint8_t aFrames8U[16] = { 0xAA, 0xBB, 0, 1, 43, 125, 126, 127,
|
---|
356 | 128, 129, 130, 131, 132, UINT8_MAX - 1, UINT8_MAX, 0 };
|
---|
357 |
|
---|
358 | /*
|
---|
359 | * Writing + mixing from child -> parent, sequential.
|
---|
360 | */
|
---|
361 | uint32_t cbBuf = 256;
|
---|
362 | char achBuf[256];
|
---|
363 | uint32_t cFramesRead, cFramesWritten, cFramesMixed;
|
---|
364 |
|
---|
365 | uint32_t cFramesChild = 16;
|
---|
366 | uint32_t cFramesParent = cFramesChild * 2 - 2;
|
---|
367 | uint32_t cFramesTotalRead = 0;
|
---|
368 |
|
---|
369 | /**** 8-bit unsigned samples ****/
|
---|
370 | RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Conversion test %uHz %uch 8-bit\n", cfg_c.uHz, cfg_c.cChannels);
|
---|
371 | RTTESTI_CHECK_RC_OK(AudioMixBufWriteCirc(&child, &aFrames8U, sizeof(aFrames8U), &cFramesWritten));
|
---|
372 | RTTESTI_CHECK_MSG(cFramesWritten == cFramesChild, ("Child: Expected %RU32 written frames, got %RU32\n", cFramesChild, cFramesWritten));
|
---|
373 | RTTESTI_CHECK_RC_OK(AudioMixBufMixToParent(&child, cFramesWritten, &cFramesMixed));
|
---|
374 | uint32_t cFrames = AudioMixBufUsed(&parent);
|
---|
375 | RTTESTI_CHECK_MSG(AudioMixBufLive(&child) == cFrames, ("Child: Expected %RU32 mixed frames, got %RU32\n", AudioMixBufLive(&child), cFrames));
|
---|
376 |
|
---|
377 | RTTESTI_CHECK(AudioMixBufUsed(&parent) == AudioMixBufLive(&child));
|
---|
378 |
|
---|
379 | for (;;)
|
---|
380 | {
|
---|
381 | RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufReadCirc(&parent, achBuf, cbBuf, &cFramesRead));
|
---|
382 | if (!cFramesRead)
|
---|
383 | break;
|
---|
384 | cFramesTotalRead += cFramesRead;
|
---|
385 | AudioMixBufFinish(&parent, cFramesRead);
|
---|
386 | }
|
---|
387 |
|
---|
388 | RTTESTI_CHECK_MSG(cFramesTotalRead == cFramesParent, ("Parent: Expected %RU32 mixed frames, got %RU32\n", cFramesParent, cFramesTotalRead));
|
---|
389 |
|
---|
390 | /* Check that the frames came out unharmed. Every other sample is interpolated and we ignore it. */
|
---|
391 | /* NB: This also checks that the default volume setting is 0dB attenuation. */
|
---|
392 | uint8_t *pSrc8 = &aFrames8U[0];
|
---|
393 | uint8_t *pDst8 = (uint8_t *)achBuf;
|
---|
394 |
|
---|
395 | for (i = 0; i < cFramesChild - 1; ++i)
|
---|
396 | {
|
---|
397 | RTTESTI_CHECK_MSG(*pSrc8 == *pDst8, ("index %u: Dst=%d, Src=%d\n", i, *pDst8, *pSrc8));
|
---|
398 | pSrc8 += 1;
|
---|
399 | pDst8 += 2;
|
---|
400 | }
|
---|
401 |
|
---|
402 | RTTESTI_CHECK(AudioMixBufUsed(&parent) == 0);
|
---|
403 | RTTESTI_CHECK(AudioMixBufLive(&child) == 0);
|
---|
404 |
|
---|
405 | AudioMixBufDestroy(&parent);
|
---|
406 | AudioMixBufDestroy(&child);
|
---|
407 |
|
---|
408 | return RTTestSubErrorCount(hTest) ? VERR_GENERAL_FAILURE : VINF_SUCCESS;
|
---|
409 | }
|
---|
410 |
|
---|
411 | /* Test 16-bit sample conversion (16-bit -> internal -> 16-bit). */
|
---|
412 | static int tstConversion16(RTTEST hTest)
|
---|
413 | {
|
---|
414 | unsigned i;
|
---|
415 | uint32_t cBufSize = 256;
|
---|
416 |
|
---|
417 | RTTestSubF(hTest, "Sample conversion (S16)");
|
---|
418 |
|
---|
419 | /* 44100Hz, 1 Channel, S16 */
|
---|
420 | PDMAUDIOPCMPROPS cfg_p =
|
---|
421 | {
|
---|
422 | 16, /* Bits */
|
---|
423 | true, /* Signed */
|
---|
424 | 1, /* Channels */
|
---|
425 | 44100, /* Hz */
|
---|
426 | PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(16 /* Bits */, 1 /* Channels */), /* Shift */
|
---|
427 | false /* Swap Endian */
|
---|
428 | };
|
---|
429 |
|
---|
430 | RTTESTI_CHECK(DrvAudioHlpPCMPropsAreValid(&cfg_p));
|
---|
431 |
|
---|
432 | PDMAUDIOMIXBUF parent;
|
---|
433 | RTTESTI_CHECK_RC_OK(AudioMixBufInit(&parent, "Parent", &cfg_p, cBufSize));
|
---|
434 |
|
---|
435 | /* 22050Hz, 1 Channel, S16 */
|
---|
436 | PDMAUDIOPCMPROPS cfg_c = /* Upmixing to parent */
|
---|
437 | {
|
---|
438 | 16, /* Bits */
|
---|
439 | true, /* Signed */
|
---|
440 | 1, /* Channels */
|
---|
441 | 22050, /* Hz */
|
---|
442 | PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(16 /* Bits */, 1 /* Channels */), /* Shift */
|
---|
443 | false /* Swap Endian */
|
---|
444 | };
|
---|
445 |
|
---|
446 | RTTESTI_CHECK(DrvAudioHlpPCMPropsAreValid(&cfg_c));
|
---|
447 |
|
---|
448 | PDMAUDIOMIXBUF child;
|
---|
449 | RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child, "Child", &cfg_c, cBufSize));
|
---|
450 | RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child, &parent));
|
---|
451 |
|
---|
452 | /* 16-bit signed. More or less exclusively used as output, and usually as input, too. */
|
---|
453 | int16_t aFrames16S[16] = { 0xAA, 0xBB, INT16_MIN, INT16_MIN + 1, INT16_MIN / 2, -3, -2, -1,
|
---|
454 | 0, 1, 2, 3, INT16_MAX / 2, INT16_MAX - 1, INT16_MAX, 0 };
|
---|
455 |
|
---|
456 | /*
|
---|
457 | * Writing + mixing from child -> parent, sequential.
|
---|
458 | */
|
---|
459 | uint32_t cbBuf = 256;
|
---|
460 | char achBuf[256];
|
---|
461 | uint32_t cFramesRead, cFramesWritten, cFramesMixed;
|
---|
462 |
|
---|
463 | uint32_t cFramesChild = 16;
|
---|
464 | uint32_t cFramesParent = cFramesChild * 2 - 2;
|
---|
465 | uint32_t cFramesTotalRead = 0;
|
---|
466 |
|
---|
467 | /**** 16-bit signed samples ****/
|
---|
468 | RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Conversion test %uHz %uch 16-bit\n", cfg_c.uHz, cfg_c.cChannels);
|
---|
469 | RTTESTI_CHECK_RC_OK(AudioMixBufWriteCirc(&child, &aFrames16S, sizeof(aFrames16S), &cFramesWritten));
|
---|
470 | RTTESTI_CHECK_MSG(cFramesWritten == cFramesChild, ("Child: Expected %RU32 written frames, got %RU32\n", cFramesChild, cFramesWritten));
|
---|
471 | RTTESTI_CHECK_RC_OK(AudioMixBufMixToParent(&child, cFramesWritten, &cFramesMixed));
|
---|
472 | uint32_t cFrames = AudioMixBufUsed(&parent);
|
---|
473 | RTTESTI_CHECK_MSG(AudioMixBufLive(&child) == cFrames, ("Child: Expected %RU32 mixed frames, got %RU32\n", AudioMixBufLive(&child), cFrames));
|
---|
474 |
|
---|
475 | RTTESTI_CHECK(AudioMixBufUsed(&parent) == AudioMixBufLive(&child));
|
---|
476 |
|
---|
477 | for (;;)
|
---|
478 | {
|
---|
479 | RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufReadCirc(&parent, achBuf, cbBuf, &cFramesRead));
|
---|
480 | if (!cFramesRead)
|
---|
481 | break;
|
---|
482 | cFramesTotalRead += cFramesRead;
|
---|
483 | AudioMixBufFinish(&parent, cFramesRead);
|
---|
484 | }
|
---|
485 | RTTESTI_CHECK_MSG(cFramesTotalRead == cFramesParent, ("Parent: Expected %RU32 mixed frames, got %RU32\n", cFramesParent, cFramesTotalRead));
|
---|
486 |
|
---|
487 | /* Check that the frames came out unharmed. Every other sample is interpolated and we ignore it. */
|
---|
488 | /* NB: This also checks that the default volume setting is 0dB attenuation. */
|
---|
489 | int16_t *pSrc16 = &aFrames16S[0];
|
---|
490 | int16_t *pDst16 = (int16_t *)achBuf;
|
---|
491 |
|
---|
492 | for (i = 0; i < cFramesChild - 1; ++i)
|
---|
493 | {
|
---|
494 | RTTESTI_CHECK_MSG(*pSrc16 == *pDst16, ("index %u: Dst=%d, Src=%d\n", i, *pDst16, *pSrc16));
|
---|
495 | pSrc16 += 1;
|
---|
496 | pDst16 += 2;
|
---|
497 | }
|
---|
498 |
|
---|
499 | RTTESTI_CHECK(AudioMixBufUsed(&parent) == 0);
|
---|
500 | RTTESTI_CHECK(AudioMixBufLive(&child) == 0);
|
---|
501 |
|
---|
502 | AudioMixBufDestroy(&parent);
|
---|
503 | AudioMixBufDestroy(&child);
|
---|
504 |
|
---|
505 | return RTTestSubErrorCount(hTest) ? VERR_GENERAL_FAILURE : VINF_SUCCESS;
|
---|
506 | }
|
---|
507 |
|
---|
508 | /* Test volume control. */
|
---|
509 | static int tstVolume(RTTEST hTest)
|
---|
510 | {
|
---|
511 | unsigned i;
|
---|
512 | uint32_t cBufSize = 256;
|
---|
513 |
|
---|
514 | RTTestSubF(hTest, "Volume control");
|
---|
515 |
|
---|
516 | /* Same for parent/child. */
|
---|
517 | /* 44100Hz, 2 Channels, S16 */
|
---|
518 | PDMAUDIOPCMPROPS cfg =
|
---|
519 | {
|
---|
520 | 16, /* Bits */
|
---|
521 | true, /* Signed */
|
---|
522 | 2, /* Channels */
|
---|
523 | 44100, /* Hz */
|
---|
524 | PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(16 /* Bits */, 2 /* Channels */), /* Shift */
|
---|
525 | false /* Swap Endian */
|
---|
526 | };
|
---|
527 |
|
---|
528 | RTTESTI_CHECK(DrvAudioHlpPCMPropsAreValid(&cfg));
|
---|
529 |
|
---|
530 | PDMAUDIOVOLUME vol = { false, 0, 0 }; /* Not muted. */
|
---|
531 | PDMAUDIOMIXBUF parent;
|
---|
532 | RTTESTI_CHECK_RC_OK(AudioMixBufInit(&parent, "Parent", &cfg, cBufSize));
|
---|
533 |
|
---|
534 | PDMAUDIOMIXBUF child;
|
---|
535 | RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child, "Child", &cfg, cBufSize));
|
---|
536 | RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child, &parent));
|
---|
537 |
|
---|
538 | /* A few 16-bit signed samples. */
|
---|
539 | int16_t aFrames16S[16] = { INT16_MIN, INT16_MIN + 1, -128, -64, -4, -1, 0, 1,
|
---|
540 | 2, 255, 256, INT16_MAX / 2, INT16_MAX - 2, INT16_MAX - 1, INT16_MAX, 0 };
|
---|
541 |
|
---|
542 | /*
|
---|
543 | * Writing + mixing from child -> parent.
|
---|
544 | */
|
---|
545 | uint32_t cbBuf = 256;
|
---|
546 | char achBuf[256];
|
---|
547 | uint32_t cFramesRead, cFramesWritten, cFramesMixed;
|
---|
548 |
|
---|
549 | uint32_t cFramesChild = 8;
|
---|
550 | uint32_t cFramesParent = cFramesChild;
|
---|
551 | uint32_t cFramesTotalRead;
|
---|
552 | int16_t *pSrc16;
|
---|
553 | int16_t *pDst16;
|
---|
554 |
|
---|
555 | /**** Volume control test ****/
|
---|
556 | RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Volume control test %uHz %uch \n", cfg.uHz, cfg.cChannels);
|
---|
557 |
|
---|
558 | /* 1) Full volume/0dB attenuation (255). */
|
---|
559 | vol.uLeft = vol.uRight = 255;
|
---|
560 | AudioMixBufSetVolume(&child, &vol);
|
---|
561 |
|
---|
562 | RTTESTI_CHECK_RC_OK(AudioMixBufWriteCirc(&child, &aFrames16S, sizeof(aFrames16S), &cFramesWritten));
|
---|
563 | RTTESTI_CHECK_MSG(cFramesWritten == cFramesChild, ("Child: Expected %RU32 written frames, got %RU32\n", cFramesChild, cFramesWritten));
|
---|
564 | RTTESTI_CHECK_RC_OK(AudioMixBufMixToParent(&child, cFramesWritten, &cFramesMixed));
|
---|
565 |
|
---|
566 | cFramesTotalRead = 0;
|
---|
567 | for (;;)
|
---|
568 | {
|
---|
569 | RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufReadCirc(&parent, achBuf, cbBuf, &cFramesRead));
|
---|
570 | if (!cFramesRead)
|
---|
571 | break;
|
---|
572 | cFramesTotalRead += cFramesRead;
|
---|
573 | AudioMixBufFinish(&parent, cFramesRead);
|
---|
574 | }
|
---|
575 | RTTESTI_CHECK_MSG(cFramesTotalRead == cFramesParent, ("Parent: Expected %RU32 mixed frames, got %RU32\n", cFramesParent, cFramesTotalRead));
|
---|
576 |
|
---|
577 | /* Check that at 0dB the frames came out unharmed. */
|
---|
578 | pSrc16 = &aFrames16S[0];
|
---|
579 | pDst16 = (int16_t *)achBuf;
|
---|
580 |
|
---|
581 | for (i = 0; i < cFramesParent * 2 /* stereo */; ++i)
|
---|
582 | {
|
---|
583 | RTTESTI_CHECK_MSG(*pSrc16 == *pDst16, ("index %u: Dst=%d, Src=%d\n", i, *pDst16, *pSrc16));
|
---|
584 | ++pSrc16;
|
---|
585 | ++pDst16;
|
---|
586 | }
|
---|
587 | AudioMixBufReset(&child);
|
---|
588 |
|
---|
589 | /* 2) Half volume/-6dB attenuation (16 steps down). */
|
---|
590 | vol.uLeft = vol.uRight = 255 - 16;
|
---|
591 | AudioMixBufSetVolume(&child, &vol);
|
---|
592 |
|
---|
593 | RTTESTI_CHECK_RC_OK(AudioMixBufWriteCirc(&child, &aFrames16S, sizeof(aFrames16S), &cFramesWritten));
|
---|
594 | RTTESTI_CHECK_MSG(cFramesWritten == cFramesChild, ("Child: Expected %RU32 written frames, got %RU32\n", cFramesChild, cFramesWritten));
|
---|
595 | RTTESTI_CHECK_RC_OK(AudioMixBufMixToParent(&child, cFramesWritten, &cFramesMixed));
|
---|
596 |
|
---|
597 | cFramesTotalRead = 0;
|
---|
598 | for (;;)
|
---|
599 | {
|
---|
600 | RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufReadCirc(&parent, achBuf, cbBuf, &cFramesRead));
|
---|
601 | if (!cFramesRead)
|
---|
602 | break;
|
---|
603 | cFramesTotalRead += cFramesRead;
|
---|
604 | AudioMixBufFinish(&parent, cFramesRead);
|
---|
605 | }
|
---|
606 | RTTESTI_CHECK_MSG(cFramesTotalRead == cFramesParent, ("Parent: Expected %RU32 mixed frames, got %RU32\n", cFramesParent, cFramesTotalRead));
|
---|
607 |
|
---|
608 | /* Check that at -6dB the sample values are halved. */
|
---|
609 | pSrc16 = &aFrames16S[0];
|
---|
610 | pDst16 = (int16_t *)achBuf;
|
---|
611 |
|
---|
612 | for (i = 0; i < cFramesParent * 2 /* stereo */; ++i)
|
---|
613 | {
|
---|
614 | /* Watch out! For negative values, x >> 1 is not the same as x / 2. */
|
---|
615 | RTTESTI_CHECK_MSG(*pSrc16 >> 1 == *pDst16, ("index %u: Dst=%d, Src=%d\n", i, *pDst16, *pSrc16));
|
---|
616 | ++pSrc16;
|
---|
617 | ++pDst16;
|
---|
618 | }
|
---|
619 |
|
---|
620 | AudioMixBufDestroy(&parent);
|
---|
621 | AudioMixBufDestroy(&child);
|
---|
622 |
|
---|
623 | return RTTestSubErrorCount(hTest) ? VERR_GENERAL_FAILURE : VINF_SUCCESS;
|
---|
624 | }
|
---|
625 |
|
---|
626 | int main(int argc, char **argv)
|
---|
627 | {
|
---|
628 | RTR3InitExe(argc, &argv, 0);
|
---|
629 |
|
---|
630 | /*
|
---|
631 | * Initialize IPRT and create the test.
|
---|
632 | */
|
---|
633 | RTTEST hTest;
|
---|
634 | int rc = RTTestInitAndCreate("tstAudioMixBuffer", &hTest);
|
---|
635 | if (rc)
|
---|
636 | return rc;
|
---|
637 | RTTestBanner(hTest);
|
---|
638 |
|
---|
639 | rc = tstSingle(hTest);
|
---|
640 | if (RT_SUCCESS(rc))
|
---|
641 | rc = tstParentChild(hTest);
|
---|
642 | if (RT_SUCCESS(rc))
|
---|
643 | rc = tstConversion8(hTest);
|
---|
644 | if (RT_SUCCESS(rc))
|
---|
645 | rc = tstConversion16(hTest);
|
---|
646 | if (RT_SUCCESS(rc))
|
---|
647 | rc = tstVolume(hTest);
|
---|
648 |
|
---|
649 | /*
|
---|
650 | * Summary
|
---|
651 | */
|
---|
652 | return RTTestSummaryAndDestroy(hTest);
|
---|
653 | }
|
---|