VirtualBox

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

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

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