VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/testcase/tstAudioMixBuffer.cpp@ 73529

Last change on this file since 73529 was 71752, checked in by vboxsync, 7 years ago

DevIchAc97: Alignment fixing.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette