VirtualBox

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

Last change on this file since 87993 was 87993, checked in by vboxsync, 4 years ago

Audio: Switched DrvAudioHlpFramesToMilli parameters. bugref:9890

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