VirtualBox

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

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

Audio: Renamed DrvAudioCommon.* to AudioHlp.*, changing functions from DrvAudioHlp to AudioHlp. bugref:9890

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