VirtualBox

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

Last change on this file since 61541 was 61352, checked in by vboxsync, 9 years ago

AudioMixBuffer,tstAudioMixBuffer,pdmaudioifs.h: Fixed buffer overflow in AudioMixBufWriteAtEx during testcase run due to incomplete checking. Fixed some coding convesion issues. Made the debug build of the testcsae always use the electric fence heap.

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