VirtualBox

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

最後變更 在這個檔案從89350是 89339,由 vboxsync 提交於 4 年 前

AudioMixer: Removed AudioMixBufReset and AudioMixBufClear in favor of AudioMixBufDrop. The content of unused buffer space is not relevant, we don't need to clear anything, just logically drop it. bugref:9890

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 45.9 KB
 
1/* $Id: tstAudioMixBuffer.cpp 89339 2021-05-28 09:05:28Z 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.alldomusa.eu.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/pdm.h>
31#include <VBox/vmm/pdmaudioinline.h>
32
33#include "../AudioMixBuffer.h"
34#include "../AudioHlp.h"
35
36#define _USE_MATH_DEFINES
37#include <math.h> /* sin, M_PI */
38
39
40static void tstBasics(RTTEST hTest)
41{
42 RTTestSub(hTest, "Basics");
43
44 const PDMAUDIOPCMPROPS Cfg441StereoS16 = PDMAUDIOPCMPROPS_INITIALIZER(
45 /* a_cb: */ 2,
46 /* a_fSigned: */ true,
47 /* a_cChannels: */ 2,
48 /* a_uHz: */ 44100,
49 /* a_fSwapEndian: */ false
50 );
51 const PDMAUDIOPCMPROPS Cfg441StereoU16 = PDMAUDIOPCMPROPS_INITIALIZER(
52 /* a_cb: */ 2,
53 /* a_fSigned: */ false,
54 /* a_cChannels: */ 2,
55 /* a_uHz: */ 44100,
56 /* a_fSwapEndian: */ false
57 );
58 const PDMAUDIOPCMPROPS Cfg441StereoU32 = PDMAUDIOPCMPROPS_INITIALIZER(
59 /* a_cb: */ 4,
60 /* a_fSigned: */ false,
61 /* a_cChannels: */ 2,
62 /* a_uHz: */ 44100,
63 /* a_fSwapEndian: */ false
64 );
65
66 RTTESTI_CHECK(PDMAudioPropsGetBitrate(&Cfg441StereoS16) == 44100*4*8);
67 RTTESTI_CHECK(PDMAudioPropsGetBitrate(&Cfg441StereoU16) == 44100*4*8);
68 RTTESTI_CHECK(PDMAudioPropsGetBitrate(&Cfg441StereoU32) == 44100*8*8);
69
70 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&Cfg441StereoS16));
71 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&Cfg441StereoU16) == false); /* go figure */
72 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&Cfg441StereoU32) == false); /* go figure */
73
74
75 RTTESTI_CHECK_MSG(PDMAUDIOPCMPROPS_F2B(&Cfg441StereoS16, 1) == 4,
76 ("got %x, expected 4\n", PDMAUDIOPCMPROPS_F2B(&Cfg441StereoS16, 1)));
77 RTTESTI_CHECK_MSG(PDMAUDIOPCMPROPS_F2B(&Cfg441StereoU16, 1) == 4,
78 ("got %x, expected 4\n", PDMAUDIOPCMPROPS_F2B(&Cfg441StereoU16, 1)));
79 RTTESTI_CHECK_MSG(PDMAUDIOPCMPROPS_F2B(&Cfg441StereoU32, 1) == 8,
80 ("got %x, expected 4\n", PDMAUDIOPCMPROPS_F2B(&Cfg441StereoU32, 1)));
81
82 RTTESTI_CHECK_MSG(PDMAudioPropsBytesPerFrame(&Cfg441StereoS16) == 4,
83 ("got %x, expected 4\n", PDMAudioPropsBytesPerFrame(&Cfg441StereoS16)));
84 RTTESTI_CHECK_MSG(PDMAudioPropsBytesPerFrame(&Cfg441StereoU16) == 4,
85 ("got %x, expected 4\n", PDMAudioPropsBytesPerFrame(&Cfg441StereoU16)));
86 RTTESTI_CHECK_MSG(PDMAudioPropsBytesPerFrame(&Cfg441StereoU32) == 8,
87 ("got %x, expected 4\n", PDMAudioPropsBytesPerFrame(&Cfg441StereoU32)));
88
89 uint32_t u32;
90 for (uint32_t i = 0; i < 256; i += 8)
91 {
92 RTTESTI_CHECK(PDMAudioPropsIsSizeAligned(&Cfg441StereoU32, i) == true);
93 for (uint32_t j = 1; j < 8; j++)
94 RTTESTI_CHECK(PDMAudioPropsIsSizeAligned(&Cfg441StereoU32, i + j) == false);
95 for (uint32_t j = 0; j < 8; j++)
96 RTTESTI_CHECK(PDMAudioPropsFloorBytesToFrame(&Cfg441StereoU32, i + j) == i);
97 }
98 for (uint32_t i = 0; i < 4096; i += 4)
99 {
100 RTTESTI_CHECK(PDMAudioPropsIsSizeAligned(&Cfg441StereoS16, i) == true);
101 for (uint32_t j = 1; j < 4; j++)
102 RTTESTI_CHECK(PDMAudioPropsIsSizeAligned(&Cfg441StereoS16, i + j) == false);
103 for (uint32_t j = 0; j < 4; j++)
104 RTTESTI_CHECK(PDMAudioPropsFloorBytesToFrame(&Cfg441StereoS16, i + j) == i);
105 }
106
107 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsFramesToBytes(&Cfg441StereoS16, 44100)) == 44100 * 2 * 2,
108 ("cb=%RU32\n", u32));
109 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsFramesToBytes(&Cfg441StereoS16, 2)) == 2 * 2 * 2,
110 ("cb=%RU32\n", u32));
111 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsFramesToBytes(&Cfg441StereoS16, 1)) == 4,
112 ("cb=%RU32\n", u32));
113 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsFramesToBytes(&Cfg441StereoU16, 1)) == 4,
114 ("cb=%RU32\n", u32));
115 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsFramesToBytes(&Cfg441StereoU32, 1)) == 8,
116 ("cb=%RU32\n", u32));
117
118 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsBytesToFrames(&Cfg441StereoS16, 4)) == 1, ("cb=%RU32\n", u32));
119 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsBytesToFrames(&Cfg441StereoU16, 4)) == 1, ("cb=%RU32\n", u32));
120 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsBytesToFrames(&Cfg441StereoU32, 8)) == 1, ("cb=%RU32\n", u32));
121
122 uint64_t u64;
123 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsBytesToNano(&Cfg441StereoS16, 44100 * 2 * 2)) == RT_NS_1SEC,
124 ("ns=%RU64\n", u64));
125 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsBytesToMicro(&Cfg441StereoS16, 44100 * 2 * 2)) == RT_US_1SEC,
126 ("us=%RU64\n", u64));
127 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsBytesToMilli(&Cfg441StereoS16, 44100 * 2 * 2)) == RT_MS_1SEC,
128 ("ms=%RU64\n", u64));
129
130 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsFramesToNano(&Cfg441StereoS16, 44100)) == RT_NS_1SEC, ("ns=%RU64\n", u64));
131 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsFramesToNano(&Cfg441StereoS16, 1)) == 22675, ("ns=%RU64\n", u64));
132 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsFramesToNano(&Cfg441StereoS16, 31)) == 702947, ("ns=%RU64\n", u64));
133 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsFramesToNano(&Cfg441StereoS16, 255)) == 5782312, ("ns=%RU64\n", u64));
134 //RTTESTI_CHECK_MSG((u64 = DrvAudioHlpFramesToMicro(&Cfg441StereoS16, 44100)) == RT_US_1SEC,
135 // ("us=%RU64\n", u64));
136 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsFramesToMilli(&Cfg441StereoS16, 44100)) == RT_MS_1SEC, ("ms=%RU64\n", u64));
137 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsFramesToMilli(&Cfg441StereoS16, 255)) == 5, ("ms=%RU64\n", u64));
138
139 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsNanoToFrames(&Cfg441StereoS16, RT_NS_1SEC)) == 44100, ("cb=%RU32\n", u32));
140 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsNanoToFrames(&Cfg441StereoS16, 215876)) == 10, ("cb=%RU32\n", u32));
141 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsMilliToFrames(&Cfg441StereoS16, RT_MS_1SEC)) == 44100, ("cb=%RU32\n", u32));
142 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsMilliToFrames(&Cfg441StereoU32, 6)) == 265, ("cb=%RU32\n", u32));
143
144 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsNanoToBytes(&Cfg441StereoS16, RT_NS_1SEC)) == 44100*2*2, ("cb=%RU32\n", u32));
145 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsNanoToBytes(&Cfg441StereoS16, 702947)) == 31*2*2, ("cb=%RU32\n", u32));
146 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsMilliToBytes(&Cfg441StereoS16, RT_MS_1SEC)) == 44100*2*2, ("cb=%RU32\n", u32));
147 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsMilliToBytes(&Cfg441StereoS16, 5)) == 884, ("cb=%RU32\n", u32));
148
149 /* DrvAudioHlpClearBuf: */
150 uint8_t *pbPage;
151 int rc = RTTestGuardedAlloc(hTest, PAGE_SIZE, 0, false /*fHead*/, (void **)&pbPage);
152 RTTESTI_CHECK_RC_OK_RETV(rc);
153
154 memset(pbPage, 0x42, PAGE_SIZE);
155 PDMAudioPropsClearBuffer(&Cfg441StereoS16, pbPage, PAGE_SIZE, PAGE_SIZE / 4);
156 RTTESTI_CHECK(ASMMemIsZero(pbPage, PAGE_SIZE));
157
158 memset(pbPage, 0x42, PAGE_SIZE);
159 PDMAudioPropsClearBuffer(&Cfg441StereoU16, pbPage, PAGE_SIZE, PAGE_SIZE / 4);
160 for (uint32_t off = 0; off < PAGE_SIZE; off += 2)
161 RTTESTI_CHECK_MSG(pbPage[off] == 0 && pbPage[off + 1] == 0x80, ("off=%#x: %#x %x\n", off, pbPage[off], pbPage[off + 1]));
162
163 memset(pbPage, 0x42, PAGE_SIZE);
164 PDMAudioPropsClearBuffer(&Cfg441StereoU32, pbPage, PAGE_SIZE, PAGE_SIZE / 8);
165 for (uint32_t off = 0; off < PAGE_SIZE; off += 4)
166 RTTESTI_CHECK(pbPage[off] == 0 && pbPage[off + 1] == 0 && pbPage[off + 2] == 0 && pbPage[off + 3] == 0x80);
167
168
169 RTTestDisableAssertions(hTest);
170 memset(pbPage, 0x42, PAGE_SIZE);
171 PDMAudioPropsClearBuffer(&Cfg441StereoS16, pbPage, PAGE_SIZE, PAGE_SIZE); /* should adjust down the frame count. */
172 RTTESTI_CHECK(ASMMemIsZero(pbPage, PAGE_SIZE));
173
174 memset(pbPage, 0x42, PAGE_SIZE);
175 PDMAudioPropsClearBuffer(&Cfg441StereoU16, pbPage, PAGE_SIZE, PAGE_SIZE); /* should adjust down the frame count. */
176 for (uint32_t off = 0; off < PAGE_SIZE; off += 2)
177 RTTESTI_CHECK_MSG(pbPage[off] == 0 && pbPage[off + 1] == 0x80, ("off=%#x: %#x %x\n", off, pbPage[off], pbPage[off + 1]));
178
179 memset(pbPage, 0x42, PAGE_SIZE);
180 PDMAudioPropsClearBuffer(&Cfg441StereoU32, pbPage, PAGE_SIZE, PAGE_SIZE); /* should adjust down the frame count. */
181 for (uint32_t off = 0; off < PAGE_SIZE; off += 4)
182 RTTESTI_CHECK(pbPage[off] == 0 && pbPage[off + 1] == 0 && pbPage[off + 2] == 0 && pbPage[off + 3] == 0x80);
183 RTTestRestoreAssertions(hTest);
184
185 RTTestGuardedFree(hTest, pbPage);
186}
187
188
189static void tstSimple(RTTEST hTest)
190{
191 RTTestSub(hTest, "Simple");
192
193 /* 44100Hz, 2 Channels, S16 */
194 PDMAUDIOPCMPROPS config = PDMAUDIOPCMPROPS_INITIALIZER(
195 2, /* Bytes */
196 true, /* Signed */
197 2, /* Channels */
198 44100, /* Hz */
199 false /* Swap Endian */
200 );
201
202 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&config));
203
204 uint32_t cBufSize = _1K;
205
206 /*
207 * General stuff.
208 */
209 AUDIOMIXBUF mb;
210 RTTESTI_CHECK_RC_OK_RETV(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 AUDIOMIXBUFWRITESTATE WriteState;
218 RTTESTI_CHECK_RC(AudioMixBufInitWriteState(&mb, &WriteState, &config), VINF_SUCCESS);
219
220 AUDIOMIXBUFPEEKSTATE PeekState;
221 RTTESTI_CHECK_RC(AudioMixBufInitPeekState(&mb, &PeekState, &config), VINF_SUCCESS);
222
223 /*
224 * A few writes (used to be the weird absolute writes).
225 */
226 uint32_t cFramesRead = 0, cFramesWritten = 0, cFramesWrittenAbs = 0;
227 int16_t aFrames16[2] = { 0xAA, 0xBB };
228 int32_t aFrames32[2] = { 0xCC, 0xDD };
229
230 RTTESTI_CHECK(AudioMixBufUsed(&mb) == 0);
231
232 AudioMixBufWrite(&mb, &WriteState, &aFrames16, sizeof(aFrames16), 0 /*offDstFrame*/, cBufSize / 4, &cFramesWritten);
233 RTTESTI_CHECK(cFramesWritten == 1 /* Frames */);
234 RTTESTI_CHECK(AudioMixBufUsed(&mb) == 0);
235 AudioMixBufCommit(&mb, cFramesWritten);
236 RTTESTI_CHECK(AudioMixBufUsed(&mb) == 1);
237 RTTESTI_CHECK(AudioMixBufReadPos(&mb) == 0);
238 RTTESTI_CHECK(AudioMixBufWritePos(&mb) == 1);
239
240 AudioMixBufWrite(&mb, &WriteState, &aFrames32, sizeof(aFrames32), 0 /*offDstFrame*/, cBufSize / 4, &cFramesWritten);
241 RTTESTI_CHECK(cFramesWritten == 2 /* Frames */);
242 AudioMixBufCommit(&mb, cFramesWritten);
243 RTTESTI_CHECK(AudioMixBufUsed(&mb) == 3);
244 RTTESTI_CHECK(AudioMixBufReadPos(&mb) == 0);
245 RTTESTI_CHECK(AudioMixBufWritePos(&mb) == 3);
246
247 /* Pretend we read the frames.*/
248 AudioMixBufAdvance(&mb, 3);
249 RTTESTI_CHECK(AudioMixBufUsed(&mb) == 0);
250 RTTESTI_CHECK(AudioMixBufReadPos(&mb) == 3);
251 RTTESTI_CHECK(AudioMixBufWritePos(&mb) == 3);
252
253 /* Fill up the buffer completely and check wraps. */
254
255 uint32_t cbSamples = PDMAudioPropsFramesToBytes(&config, cBufSize);
256 uint16_t *paSamples = (uint16_t *)RTMemAlloc(cbSamples);
257 RTTESTI_CHECK_RETV(paSamples);
258 AudioMixBufWrite(&mb, &WriteState, paSamples, cbSamples, 0 /*offDstFrame*/, cBufSize, &cFramesWritten);
259 RTTESTI_CHECK(cFramesWritten == cBufSize);
260 AudioMixBufCommit(&mb, cFramesWritten);
261 RTTESTI_CHECK(AudioMixBufUsed(&mb) == cBufSize);
262 RTTESTI_CHECK(AudioMixBufReadPos(&mb) == 3);
263 RTTESTI_CHECK(AudioMixBufWritePos(&mb) == 3);
264 RTMemFree(paSamples);
265 cbSamples = 0;
266
267 /*
268 * Writes and reads (used to be circular).
269 */
270 AudioMixBufDrop(&mb);
271
272 cFramesWrittenAbs = AudioMixBufUsed(&mb);
273
274 uint32_t cToWrite = AudioMixBufSize(&mb) - cFramesWrittenAbs - 1; /* -1 as padding plus -2 frames for above. */
275 for (uint32_t i = 0; i < cToWrite; i++)
276 {
277 AudioMixBufWrite(&mb, &WriteState, &aFrames16[0], sizeof(aFrames16), 0 /*offDstFrame*/, 1, &cFramesWritten);
278 RTTESTI_CHECK(cFramesWritten == 1);
279 AudioMixBufCommit(&mb, cFramesWritten);
280 }
281 RTTESTI_CHECK(!AudioMixBufIsEmpty(&mb));
282 RTTESTI_CHECK(AudioMixBufFree(&mb) == 1);
283 RTTESTI_CHECK(AudioMixBufFreeBytes(&mb) == AUDIOMIXBUF_F2B(&mb, 1U));
284 RTTESTI_CHECK(AudioMixBufUsed(&mb) == cToWrite + cFramesWrittenAbs /* + last absolute write */);
285
286 AudioMixBufWrite(&mb, &WriteState, &aFrames16[0], sizeof(aFrames16), 0 /*offDstFrame*/, 1, &cFramesWritten);
287 RTTESTI_CHECK(cFramesWritten == 1);
288 AudioMixBufCommit(&mb, cFramesWritten);
289 RTTESTI_CHECK(AudioMixBufFree(&mb) == 0);
290 RTTESTI_CHECK(AudioMixBufFreeBytes(&mb) == AUDIOMIXBUF_F2B(&mb, 0U));
291 RTTESTI_CHECK(AudioMixBufUsed(&mb) == cBufSize);
292
293 /* Reads. */
294 RTTESTI_CHECK(AudioMixBufReadPos(&mb) == 0);
295 uint32_t cbRead;
296 uint16_t aFrames16Buf[RT_ELEMENTS(aFrames16)];
297 uint32_t cToRead = AudioMixBufSize(&mb) - cFramesWrittenAbs - 1;
298 for (uint32_t i = 0; i < cToRead; i++)
299 {
300 AudioMixBufPeek(&mb, 0 /*offSrcFrame*/, 1, &cFramesRead, &PeekState, aFrames16Buf, sizeof(aFrames16Buf), &cbRead);
301 RTTESTI_CHECK(cFramesRead == 1);
302 RTTESTI_CHECK(cbRead == sizeof(aFrames16Buf));
303 AudioMixBufAdvance(&mb, cFramesRead);
304 RTTESTI_CHECK(AudioMixBufReadPos(&mb) == i + 1);
305 }
306 RTTESTI_CHECK(!AudioMixBufIsEmpty(&mb));
307 RTTESTI_CHECK(AudioMixBufFree(&mb) == AudioMixBufSize(&mb) - cFramesWrittenAbs - 1);
308 RTTESTI_CHECK(AudioMixBufFreeBytes(&mb) == AUDIOMIXBUF_F2B(&mb, cBufSize - cFramesWrittenAbs - 1));
309 RTTESTI_CHECK(AudioMixBufUsed(&mb) == cBufSize - cToRead);
310
311 AudioMixBufPeek(&mb, 0 /*offSrcFrame*/, 1, &cFramesRead, &PeekState, aFrames16Buf, sizeof(aFrames16Buf), &cbRead);
312 RTTESTI_CHECK(cFramesRead == 1);
313 RTTESTI_CHECK(cbRead == sizeof(aFrames16Buf));
314 AudioMixBufAdvance(&mb, cFramesRead);
315 RTTESTI_CHECK(AudioMixBufFree(&mb) == cBufSize - cFramesWrittenAbs);
316 RTTESTI_CHECK(AudioMixBufFreeBytes(&mb) == AUDIOMIXBUF_F2B(&mb, cBufSize - cFramesWrittenAbs));
317 RTTESTI_CHECK(AudioMixBufUsed(&mb) == cFramesWrittenAbs);
318 RTTESTI_CHECK(AudioMixBufReadPos(&mb) == 0);
319
320 AudioMixBufDestroy(&mb);
321}
322
323#if 0 /* obsolete */
324static int tstParentChild(RTTEST hTest)
325{
326 RTTestSub(hTest, "2 Children -> Parent (AudioMixBufWriteAt)");
327
328 uint32_t cParentBufSize = RTRandU32Ex(_1K /* Min */, _16K /* Max */); /* Enough room for random sizes */
329
330 /* 44100Hz, 2 Channels, S16 */
331 PDMAUDIOPCMPROPS cfg_p = PDMAUDIOPCMPROPS_INITIALIZER(
332 2, /* Bytes */
333 true, /* Signed */
334 2, /* Channels */
335 44100, /* Hz */
336 false /* Swap Endian */
337 );
338
339 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&cfg_p));
340
341 AUDIOMIXBUF parent;
342 RTTESTI_CHECK_RC_OK(AudioMixBufInit(&parent, "Parent", &cfg_p, cParentBufSize));
343
344 /* 22050Hz, 2 Channels, S16 */
345 PDMAUDIOPCMPROPS cfg_c1 = PDMAUDIOPCMPROPS_INITIALIZER(/* Upmixing to parent */
346 2, /* Bytes */
347 true, /* Signed */
348 2, /* Channels */
349 22050, /* Hz */
350 false /* Swap Endian */
351 );
352
353 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&cfg_c1));
354
355 uint32_t cFrames = 16;
356 uint32_t cChildBufSize = RTRandU32Ex(cFrames /* Min */, 64 /* Max */);
357
358 AUDIOMIXBUF child1;
359 RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child1, "Child1", &cfg_c1, cChildBufSize));
360 RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child1, &parent));
361
362 /* 48000Hz, 2 Channels, S16 */
363 PDMAUDIOPCMPROPS cfg_c2 = PDMAUDIOPCMPROPS_INITIALIZER(/* Downmixing to parent */
364 2, /* Bytes */
365 true, /* Signed */
366 2, /* Channels */
367 48000, /* Hz */
368 false /* Swap Endian */
369 );
370
371 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&cfg_c2));
372
373 AUDIOMIXBUF child2;
374 RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child2, "Child2", &cfg_c2, cChildBufSize));
375 RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child2, &parent));
376
377 /*
378 * Writing + mixing from child/children -> parent, sequential.
379 */
380 uint32_t cbBuf = _1K;
381 char pvBuf[_1K];
382 int16_t aFrames16[32] = { 0xAA, 0xBB };
383 uint32_t cFramesRead, cFramesWritten, cFramesMixed;
384
385 uint32_t cFramesChild1 = cFrames;
386 uint32_t cFramesChild2 = cFrames;
387
388 uint32_t t = RTRandU32() % 32;
389
390 RTTestPrintf(hTest, RTTESTLVL_DEBUG,
391 "cParentBufSize=%RU32, cChildBufSize=%RU32, %RU32 frames -> %RU32 iterations total\n",
392 cParentBufSize, cChildBufSize, cFrames, t);
393
394 /*
395 * Using AudioMixBufWriteAt for writing to children.
396 */
397 uint32_t cChildrenSamplesMixedTotal = 0;
398
399 for (uint32_t i = 0; i < t; i++)
400 {
401 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "i=%RU32\n", i);
402
403 uint32_t cChild1Writes = RTRandU32() % 8;
404
405 for (uint32_t c1 = 0; c1 < cChild1Writes; c1++)
406 {
407 /* Child 1. */
408 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufWriteAt(&child1, 0, &aFrames16, sizeof(aFrames16), &cFramesWritten));
409 RTTESTI_CHECK_MSG_BREAK(cFramesWritten == cFramesChild1, ("Child1: Expected %RU32 written frames, got %RU32\n", cFramesChild1, cFramesWritten));
410 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufMixToParent(&child1, cFramesWritten, &cFramesMixed));
411
412 cChildrenSamplesMixedTotal += cFramesMixed;
413
414 RTTESTI_CHECK_MSG_BREAK(cFramesWritten == cFramesMixed, ("Child1: Expected %RU32 mixed frames, got %RU32\n", cFramesWritten, cFramesMixed));
415 RTTESTI_CHECK_MSG_BREAK(AudioMixBufUsed(&child1) == 0, ("Child1: Expected %RU32 used frames, got %RU32\n", 0, AudioMixBufUsed(&child1)));
416 }
417
418 uint32_t cChild2Writes = RTRandU32() % 8;
419
420 for (uint32_t c2 = 0; c2 < cChild2Writes; c2++)
421 {
422 /* Child 2. */
423 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufWriteAt(&child2, 0, &aFrames16, sizeof(aFrames16), &cFramesWritten));
424 RTTESTI_CHECK_MSG_BREAK(cFramesWritten == cFramesChild2, ("Child2: Expected %RU32 written frames, got %RU32\n", cFramesChild2, cFramesWritten));
425 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufMixToParent(&child2, cFramesWritten, &cFramesMixed));
426
427 cChildrenSamplesMixedTotal += cFramesMixed;
428
429 RTTESTI_CHECK_MSG_BREAK(cFramesWritten == cFramesMixed, ("Child2: Expected %RU32 mixed frames, got %RU32\n", cFramesWritten, cFramesMixed));
430 RTTESTI_CHECK_MSG_BREAK(AudioMixBufUsed(&child2) == 0, ("Child2: Expected %RU32 used frames, got %RU32\n", 0, AudioMixBufUsed(&child2)));
431 }
432
433 /*
434 * Read out all frames from the parent buffer and also mark the just-read frames as finished
435 * so that both connected children buffers can keep track of their stuff.
436 */
437 uint32_t cParentSamples = AudioMixBufUsed(&parent);
438 while (cParentSamples)
439 {
440 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufAcquireReadBlock(&parent, pvBuf, cbBuf, &cFramesRead));
441 if (!cFramesRead)
442 break;
443
444 AudioMixBufReleaseReadBlock(&parent, cFramesRead);
445 AudioMixBufFinish(&parent, cFramesRead);
446
447 RTTESTI_CHECK(cParentSamples >= cFramesRead);
448 cParentSamples -= cFramesRead;
449 }
450
451 RTTESTI_CHECK(cParentSamples == 0);
452 }
453
454 RTTESTI_CHECK(AudioMixBufUsed(&parent) == 0);
455 RTTESTI_CHECK(AudioMixBufLive(&child1) == 0);
456 RTTESTI_CHECK(AudioMixBufLive(&child2) == 0);
457
458 AudioMixBufDestroy(&parent);
459 AudioMixBufDestroy(&child1);
460 AudioMixBufDestroy(&child2);
461
462 return RTTestSubErrorCount(hTest) ? VERR_GENERAL_FAILURE : VINF_SUCCESS;
463}
464#endif
465
466
467#if 0 /** @todo rewrite to non-parent/child setup */
468static void tstDownsampling(RTTEST hTest, uint32_t uFromHz, uint32_t uToHz)
469{
470 RTTestSubF(hTest, "Downsampling %u to %u Hz (S16)", uFromHz, uToHz);
471
472 struct { int16_t l, r; }
473 aSrcFrames[4096],
474 aDstFrames[4096];
475
476 /* Parent (destination) buffer is xxxHz 2ch S16 */
477 uint32_t const cFramesParent = RTRandU32Ex(16, RT_ELEMENTS(aDstFrames));
478 PDMAUDIOPCMPROPS const CfgDst = PDMAUDIOPCMPROPS_INITIALIZER(2 /*cbSample*/, true /*fSigned*/, 2 /*ch*/, uToHz, false /*fSwap*/);
479 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&CfgDst));
480 AUDIOMIXBUF Parent;
481 RTTESTI_CHECK_RC_OK_RETV(AudioMixBufInit(&Parent, "ParentDownsampling", &CfgDst, cFramesParent));
482
483 /* Child (source) buffer is yyykHz 2ch S16 */
484 PDMAUDIOPCMPROPS const CfgSrc = PDMAUDIOPCMPROPS_INITIALIZER(2 /*cbSample*/, true /*fSigned*/, 2 /*ch*/, uFromHz, false /*fSwap*/);
485 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&CfgSrc));
486 uint32_t const cFramesChild = RTRandU32Ex(32, RT_ELEMENTS(aSrcFrames));
487 AUDIOMIXBUF Child;
488 RTTESTI_CHECK_RC_OK_RETV(AudioMixBufInit(&Child, "ChildDownsampling", &CfgSrc, cFramesChild));
489 RTTESTI_CHECK_RC_OK_RETV(AudioMixBufLinkTo(&Child, &Parent));
490
491 /*
492 * Test parameters.
493 */
494 uint32_t const cMaxSrcFrames = RT_MIN(cFramesParent * uFromHz / uToHz - 1, cFramesChild);
495 uint32_t const cIterations = RTRandU32Ex(4, 128);
496 RTTestErrContext(hTest, "cFramesParent=%RU32 cFramesChild=%RU32 cMaxSrcFrames=%RU32 cIterations=%RU32",
497 cFramesParent, cFramesChild, cMaxSrcFrames, cIterations);
498 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "cFramesParent=%RU32 cFramesChild=%RU32 cMaxSrcFrames=%RU32 cIterations=%RU32\n",
499 cFramesParent, cFramesChild, cMaxSrcFrames, cIterations);
500
501 /*
502 * We generate a simple "A" sine wave as input.
503 */
504 uint32_t iSrcFrame = 0;
505 uint32_t iDstFrame = 0;
506 double rdFixed = 2.0 * M_PI * 440.0 /* A */ / PDMAudioPropsHz(&CfgSrc); /* Fixed sin() input. */
507 for (uint32_t i = 0; i < cIterations; i++)
508 {
509 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "i=%RU32\n", i);
510
511 /*
512 * Generate source frames and write them.
513 */
514 uint32_t const cSrcFrames = i < cIterations / 2
515 ? RTRandU32Ex(2, cMaxSrcFrames) & ~(uint32_t)1
516 : RTRandU32Ex(1, cMaxSrcFrames - 1) | 1;
517 for (uint32_t j = 0; j < cSrcFrames; j++, iSrcFrame++)
518 aSrcFrames[j].r = aSrcFrames[j].l = 32760 /*Amplitude*/ * sin(rdFixed * iSrcFrame);
519
520 uint32_t cSrcFramesWritten = UINT32_MAX / 2;
521 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufWriteAt(&Child, 0, &aSrcFrames, cSrcFrames * sizeof(aSrcFrames[0]),
522 &cSrcFramesWritten));
523 RTTESTI_CHECK_MSG_BREAK(cSrcFrames == cSrcFramesWritten,
524 ("cSrcFrames=%RU32 vs cSrcFramesWritten=%RU32\n", cSrcFrames, cSrcFramesWritten));
525
526 /*
527 * Mix them.
528 */
529 uint32_t cSrcFramesMixed = UINT32_MAX / 2;
530 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufMixToParent(&Child, cSrcFramesWritten, &cSrcFramesMixed));
531 RTTESTI_CHECK_MSG(AudioMixBufUsed(&Child) == 0, ("%RU32\n", AudioMixBufUsed(&Child)));
532 RTTESTI_CHECK_MSG_BREAK(cSrcFramesWritten == cSrcFramesMixed,
533 ("cSrcFramesWritten=%RU32 cSrcFramesMixed=%RU32\n", cSrcFramesWritten, cSrcFramesMixed));
534 RTTESTI_CHECK_MSG_BREAK(AudioMixBufUsed(&Child) == 0, ("%RU32\n", AudioMixBufUsed(&Child)));
535
536 /*
537 * Read out the parent buffer.
538 */
539 uint32_t cDstFrames = AudioMixBufUsed(&Parent);
540 while (cDstFrames > 0)
541 {
542 uint32_t cFramesRead = UINT32_MAX / 2;
543 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufAcquireReadBlock(&Parent, aDstFrames, sizeof(aDstFrames), &cFramesRead));
544 RTTESTI_CHECK_MSG(cFramesRead > 0 && cFramesRead <= cDstFrames,
545 ("cFramesRead=%RU32 cDstFrames=%RU32\n", cFramesRead, cDstFrames));
546
547 AudioMixBufReleaseReadBlock(&Parent, cFramesRead);
548 AudioMixBufFinish(&Parent, cFramesRead);
549
550 iDstFrame += cFramesRead;
551 cDstFrames -= cFramesRead;
552 RTTESTI_CHECK(AudioMixBufUsed(&Parent) == cDstFrames);
553 }
554 }
555
556 RTTESTI_CHECK(AudioMixBufUsed(&Parent) == 0);
557 RTTESTI_CHECK(AudioMixBufLive(&Child) == 0);
558 uint32_t const cDstMinExpect = (uint64_t)iSrcFrame * uToHz / uFromHz;
559 uint32_t const cDstMaxExpect = ((uint64_t)iSrcFrame * uToHz + uFromHz - 1) / uFromHz;
560 RTTESTI_CHECK_MSG(iDstFrame == cDstMinExpect || iDstFrame == cDstMaxExpect,
561 ("iSrcFrame=%#x -> %#x,%#x; iDstFrame=%#x\n", iSrcFrame, cDstMinExpect, cDstMaxExpect, iDstFrame));
562
563 AudioMixBufDestroy(&Parent);
564 AudioMixBufDestroy(&Child);
565}
566#endif
567
568
569static void tstNewPeek(RTTEST hTest, uint32_t uFromHz, uint32_t uToHz)
570{
571 RTTestSubF(hTest, "New peek %u to %u Hz (S16)", uFromHz, uToHz);
572
573 struct { int16_t l, r; }
574 aSrcFrames[4096],
575 aDstFrames[4096];
576
577 /* Mix buffer is uFromHz 2ch S16 */
578 uint32_t const cFrames = RTRandU32Ex(16, RT_ELEMENTS(aSrcFrames));
579 PDMAUDIOPCMPROPS const CfgSrc = PDMAUDIOPCMPROPS_INITIALIZER(2 /*cbSample*/, true /*fSigned*/, 2 /*ch*/, uFromHz, false /*fSwap*/);
580 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&CfgSrc));
581 AUDIOMIXBUF MixBuf;
582 RTTESTI_CHECK_RC_OK_RETV(AudioMixBufInit(&MixBuf, "NewPeekMixBuf", &CfgSrc, cFrames));
583
584 /* Write state (source). */
585 AUDIOMIXBUFWRITESTATE WriteState;
586 RTTESTI_CHECK_RC_OK_RETV(AudioMixBufInitWriteState(&MixBuf, &WriteState, &CfgSrc));
587
588 /* Peek state (destination) is uToHz 2ch S16 */
589 PDMAUDIOPCMPROPS const CfgDst = PDMAUDIOPCMPROPS_INITIALIZER(2 /*cbSample*/, true /*fSigned*/, 2 /*ch*/, uToHz, false /*fSwap*/);
590 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&CfgDst));
591 AUDIOMIXBUFPEEKSTATE PeekState;
592 RTTESTI_CHECK_RC_OK_RETV(AudioMixBufInitPeekState(&MixBuf, &PeekState, &CfgDst));
593
594 /*
595 * Test parameters.
596 */
597 uint32_t const cMaxSrcFrames = RT_MIN(cFrames * uFromHz / uToHz - 1, cFrames);
598 uint32_t const cIterations = RTRandU32Ex(64, 1024);
599 RTTestErrContext(hTest, "cFrames=%RU32 cMaxSrcFrames=%RU32 cIterations=%RU32", cFrames, cMaxSrcFrames, cIterations);
600 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "cFrames=%RU32 cMaxSrcFrames=%RU32 cIterations=%RU32\n",
601 cFrames, cMaxSrcFrames, cIterations);
602
603 /*
604 * We generate a simple "A" sine wave as input.
605 */
606 uint32_t iSrcFrame = 0;
607 uint32_t iDstFrame = 0;
608 double rdFixed = 2.0 * M_PI * 440.0 /* A */ / PDMAudioPropsHz(&CfgSrc); /* Fixed sin() input. */
609 for (uint32_t i = 0; i < cIterations; i++)
610 {
611 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "i=%RU32\n", i);
612
613 /*
614 * Generate source frames and write them.
615 */
616 uint32_t const cSrcFrames = i < cIterations / 2
617 ? RTRandU32Ex(2, cMaxSrcFrames) & ~(uint32_t)1
618 : RTRandU32Ex(1, cMaxSrcFrames - 1) | 1;
619 for (uint32_t j = 0; j < cSrcFrames; j++, iSrcFrame++)
620 aSrcFrames[j].r = aSrcFrames[j].l = 32760 /*Amplitude*/ * sin(rdFixed * iSrcFrame);
621
622 uint32_t cSrcFramesWritten = UINT32_MAX / 2;
623 AudioMixBufWrite(&MixBuf, &WriteState, &aSrcFrames[0], cSrcFrames * sizeof(aSrcFrames[0]),
624 0 /*offDstFrame*/, cSrcFrames, &cSrcFramesWritten);
625 RTTESTI_CHECK_MSG_BREAK(cSrcFrames == cSrcFramesWritten,
626 ("cSrcFrames=%RU32 vs cSrcFramesWritten=%RU32 cLiveFrames=%RU32\n",
627 cSrcFrames, cSrcFramesWritten, AudioMixBufLive(&MixBuf)));
628 AudioMixBufCommit(&MixBuf, cSrcFrames);
629
630 /*
631 * Read out all the frames using the peek function.
632 */
633 uint32_t offSrcFrame = 0;
634 while (offSrcFrame < cSrcFramesWritten)
635 {
636 uint32_t cSrcFramesToRead = cSrcFramesWritten - offSrcFrame;
637 uint32_t cTmp = (uint64_t)cSrcFramesToRead * uToHz / uFromHz;
638 if (cTmp + 32 >= RT_ELEMENTS(aDstFrames))
639 cSrcFramesToRead = ((uint64_t)RT_ELEMENTS(aDstFrames) - 32) * uFromHz / uToHz; /* kludge */
640
641 uint32_t cSrcFramesPeeked = UINT32_MAX / 4;
642 uint32_t cbDstPeeked = UINT32_MAX / 2;
643 RTRandBytes(aDstFrames, sizeof(aDstFrames));
644 AudioMixBufPeek(&MixBuf, offSrcFrame, cSrcFramesToRead, &cSrcFramesPeeked,
645 &PeekState, aDstFrames, sizeof(aDstFrames), &cbDstPeeked);
646 uint32_t cDstFramesPeeked = PDMAudioPropsBytesToFrames(&CfgDst, cbDstPeeked);
647 RTTESTI_CHECK(cbDstPeeked > 0 || cSrcFramesPeeked > 0);
648
649 if (uFromHz == uToHz)
650 {
651 for (uint32_t iDst = 0; iDst < cDstFramesPeeked; iDst++)
652 if (memcmp(&aDstFrames[iDst], &aSrcFrames[offSrcFrame + iDst], sizeof(aSrcFrames[0])) != 0)
653 RTTestFailed(hTest, "Frame #%u differs: %#x / %#x, expected %#x / %#x\n", iDstFrame + iDst,
654 aDstFrames[iDst].l, aDstFrames[iDst].r,
655 aSrcFrames[iDst + offSrcFrame].l, aSrcFrames[iDst + offSrcFrame].r);
656 }
657
658 offSrcFrame += cSrcFramesPeeked;
659 iDstFrame += cDstFramesPeeked;
660 }
661
662 /*
663 * Then advance.
664 */
665 AudioMixBufAdvance(&MixBuf, cSrcFrames);
666 RTTESTI_CHECK(AudioMixBufLive(&MixBuf) == 0);
667 }
668
669 /** @todo this is a bit lax... */
670 uint32_t const cDstMinExpect = ((uint64_t)iSrcFrame * uToHz - uFromHz - 1) / uFromHz;
671 uint32_t const cDstMaxExpect = ((uint64_t)iSrcFrame * uToHz + uFromHz - 1) / uFromHz;
672 RTTESTI_CHECK_MSG(iDstFrame >= cDstMinExpect && iDstFrame <= cDstMaxExpect,
673 ("iSrcFrame=%#x -> %#x..%#x; iDstFrame=%#x (delta %d)\n",
674 iSrcFrame, cDstMinExpect, cDstMaxExpect, iDstFrame, (cDstMinExpect + cDstMaxExpect) / 2 - iDstFrame));
675
676 AudioMixBufDestroy(&MixBuf);
677}
678
679
680#if 0 /** @todo rewrite to non-parent/child setup */
681/* Test 8-bit sample conversion (8-bit -> internal -> 8-bit). */
682static int tstConversion8(RTTEST hTest)
683{
684 RTTestSub(hTest, "Convert 22kHz/U8 to 44.1kHz/S16 (mono)");
685 unsigned i;
686 uint32_t cBufSize = 256;
687
688
689 /* 44100Hz, 1 Channel, U8 */
690 PDMAUDIOPCMPROPS cfg_p = PDMAUDIOPCMPROPS_INITIALIZER(
691 1, /* Bytes */
692 false, /* Signed */
693 1, /* Channels */
694 44100, /* Hz */
695 false /* Swap Endian */
696 );
697
698 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&cfg_p));
699
700 AUDIOMIXBUF parent;
701 RTTESTI_CHECK_RC_OK(AudioMixBufInit(&parent, "Parent", &cfg_p, cBufSize));
702
703 /* Child uses half the sample rate; that ensures the mixing engine can't
704 * take shortcuts and performs conversion. Because conversion to double
705 * the sample rate effectively inserts one additional sample between every
706 * two source frames, N source frames will be converted to N * 2 - 1
707 * frames. However, the last source sample will be saved for later
708 * interpolation and not immediately output.
709 */
710
711 /* 22050Hz, 1 Channel, U8 */
712 PDMAUDIOPCMPROPS cfg_c = PDMAUDIOPCMPROPS_INITIALIZER( /* Upmixing to parent */
713 1, /* Bytes */
714 false, /* Signed */
715 1, /* Channels */
716 22050, /* Hz */
717 false /* Swap Endian */
718 );
719
720 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&cfg_c));
721
722 AUDIOMIXBUF child;
723 RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child, "Child", &cfg_c, cBufSize));
724 RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child, &parent));
725
726 /* 8-bit unsigned frames. Often used with SB16 device. */
727 uint8_t aFrames8U[16] = { 0xAA, 0xBB, 0, 1, 43, 125, 126, 127,
728 128, 129, 130, 131, 132, UINT8_MAX - 1, UINT8_MAX, 0 };
729
730 /*
731 * Writing + mixing from child -> parent, sequential.
732 */
733 uint32_t cbBuf = 256;
734 char achBuf[256];
735 uint32_t cFramesRead, cFramesWritten, cFramesMixed;
736
737 uint32_t cFramesChild = 16;
738 uint32_t cFramesParent = cFramesChild * 2 - 2;
739 uint32_t cFramesTotalRead = 0;
740
741 /**** 8-bit unsigned samples ****/
742 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Conversion test %uHz %uch 8-bit\n", cfg_c.uHz, PDMAudioPropsChannels(&cfg_c));
743 RTTESTI_CHECK_RC_OK(AudioMixBufWriteCirc(&child, &aFrames8U, sizeof(aFrames8U), &cFramesWritten));
744 RTTESTI_CHECK_MSG(cFramesWritten == cFramesChild, ("Child: Expected %RU32 written frames, got %RU32\n", cFramesChild, cFramesWritten));
745 RTTESTI_CHECK_RC_OK(AudioMixBufMixToParent(&child, cFramesWritten, &cFramesMixed));
746 uint32_t cFrames = AudioMixBufUsed(&parent);
747 RTTESTI_CHECK_MSG(AudioMixBufLive(&child) == cFrames, ("Child: Expected %RU32 mixed frames, got %RU32\n", AudioMixBufLive(&child), cFrames));
748
749 RTTESTI_CHECK(AudioMixBufUsed(&parent) == AudioMixBufLive(&child));
750
751 for (;;)
752 {
753 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufAcquireReadBlock(&parent, achBuf, cbBuf, &cFramesRead));
754 if (!cFramesRead)
755 break;
756 cFramesTotalRead += cFramesRead;
757 AudioMixBufReleaseReadBlock(&parent, cFramesRead);
758 AudioMixBufFinish(&parent, cFramesRead);
759 }
760
761 RTTESTI_CHECK_MSG(cFramesTotalRead == cFramesParent, ("Parent: Expected %RU32 mixed frames, got %RU32\n", cFramesParent, cFramesTotalRead));
762
763 /* Check that the frames came out unharmed. Every other sample is interpolated and we ignore it. */
764 /* NB: This also checks that the default volume setting is 0dB attenuation. */
765 uint8_t *pSrc8 = &aFrames8U[0];
766 uint8_t *pDst8 = (uint8_t *)achBuf;
767
768 for (i = 0; i < cFramesChild - 1; ++i)
769 {
770 RTTESTI_CHECK_MSG(*pSrc8 == *pDst8, ("index %u: Dst=%d, Src=%d\n", i, *pDst8, *pSrc8));
771 pSrc8 += 1;
772 pDst8 += 2;
773 }
774
775 RTTESTI_CHECK(AudioMixBufUsed(&parent) == 0);
776 RTTESTI_CHECK(AudioMixBufLive(&child) == 0);
777
778 AudioMixBufDestroy(&parent);
779 AudioMixBufDestroy(&child);
780
781 return RTTestSubErrorCount(hTest) ? VERR_GENERAL_FAILURE : VINF_SUCCESS;
782}
783#endif
784
785#if 0 /** @todo rewrite to non-parent/child setup */
786/* Test 16-bit sample conversion (16-bit -> internal -> 16-bit). */
787static int tstConversion16(RTTEST hTest)
788{
789 RTTestSub(hTest, "Convert 22kHz/S16 to 44.1kHz/S16 (mono)");
790 unsigned i;
791 uint32_t cBufSize = 256;
792
793 /* 44100Hz, 1 Channel, S16 */
794 PDMAUDIOPCMPROPS cfg_p = PDMAUDIOPCMPROPS_INITIALIZER(
795 2, /* Bytes */
796 true, /* Signed */
797 1, /* Channels */
798 44100, /* Hz */
799 false /* Swap Endian */
800 );
801
802 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&cfg_p));
803
804 AUDIOMIXBUF parent;
805 RTTESTI_CHECK_RC_OK(AudioMixBufInit(&parent, "Parent", &cfg_p, cBufSize));
806
807 /* 22050Hz, 1 Channel, S16 */
808 PDMAUDIOPCMPROPS cfg_c = PDMAUDIOPCMPROPS_INITIALIZER( /* Upmixing to parent */
809 2, /* Bytes */
810 true, /* Signed */
811 1, /* Channels */
812 22050, /* Hz */
813 false /* Swap Endian */
814 );
815
816 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&cfg_c));
817
818 AUDIOMIXBUF child;
819 RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child, "Child", &cfg_c, cBufSize));
820 RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child, &parent));
821
822 /* 16-bit signed. More or less exclusively used as output, and usually as input, too. */
823 int16_t aFrames16S[16] = { 0xAA, 0xBB, INT16_MIN, INT16_MIN + 1, INT16_MIN / 2, -3, -2, -1,
824 0, 1, 2, 3, INT16_MAX / 2, INT16_MAX - 1, INT16_MAX, 0 };
825
826 /*
827 * Writing + mixing from child -> parent, sequential.
828 */
829 uint32_t cbBuf = 256;
830 char achBuf[256];
831 uint32_t cFramesRead, cFramesWritten, cFramesMixed;
832
833 uint32_t cFramesChild = 16;
834 uint32_t cFramesParent = cFramesChild * 2 - 2;
835 uint32_t cFramesTotalRead = 0;
836
837 /**** 16-bit signed samples ****/
838 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Conversion test %uHz %uch 16-bit\n", cfg_c.uHz, PDMAudioPropsChannels(&cfg_c));
839 RTTESTI_CHECK_RC_OK(AudioMixBufWriteCirc(&child, &aFrames16S, sizeof(aFrames16S), &cFramesWritten));
840 RTTESTI_CHECK_MSG(cFramesWritten == cFramesChild, ("Child: Expected %RU32 written frames, got %RU32\n", cFramesChild, cFramesWritten));
841 RTTESTI_CHECK_RC_OK(AudioMixBufMixToParent(&child, cFramesWritten, &cFramesMixed));
842 uint32_t cFrames = AudioMixBufUsed(&parent);
843 RTTESTI_CHECK_MSG(AudioMixBufLive(&child) == cFrames, ("Child: Expected %RU32 mixed frames, got %RU32\n", AudioMixBufLive(&child), cFrames));
844
845 RTTESTI_CHECK(AudioMixBufUsed(&parent) == AudioMixBufLive(&child));
846
847 for (;;)
848 {
849 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufAcquireReadBlock(&parent, achBuf, cbBuf, &cFramesRead));
850 if (!cFramesRead)
851 break;
852 cFramesTotalRead += cFramesRead;
853 AudioMixBufReleaseReadBlock(&parent, cFramesRead);
854 AudioMixBufFinish(&parent, cFramesRead);
855 }
856 RTTESTI_CHECK_MSG(cFramesTotalRead == cFramesParent, ("Parent: Expected %RU32 mixed frames, got %RU32\n", cFramesParent, cFramesTotalRead));
857
858 /* Check that the frames came out unharmed. Every other sample is interpolated and we ignore it. */
859 /* NB: This also checks that the default volume setting is 0dB attenuation. */
860 int16_t *pSrc16 = &aFrames16S[0];
861 int16_t *pDst16 = (int16_t *)achBuf;
862
863 for (i = 0; i < cFramesChild - 1; ++i)
864 {
865 RTTESTI_CHECK_MSG(*pSrc16 == *pDst16, ("index %u: Dst=%d, Src=%d\n", i, *pDst16, *pSrc16));
866 pSrc16 += 1;
867 pDst16 += 2;
868 }
869
870 RTTESTI_CHECK(AudioMixBufUsed(&parent) == 0);
871 RTTESTI_CHECK(AudioMixBufLive(&child) == 0);
872
873 AudioMixBufDestroy(&parent);
874 AudioMixBufDestroy(&child);
875
876 return RTTestSubErrorCount(hTest) ? VERR_GENERAL_FAILURE : VINF_SUCCESS;
877}
878#endif
879
880#if 0 /** @todo rewrite to non-parent/child setup */
881/* Test volume control. */
882static int tstVolume(RTTEST hTest)
883{
884 RTTestSub(hTest, "Volume control (44.1kHz S16 2ch)");
885 uint32_t cBufSize = 256;
886
887 /* Same for parent/child. */
888 /* 44100Hz, 2 Channels, S16 */
889 PDMAUDIOPCMPROPS cfg = PDMAUDIOPCMPROPS_INITIALIZER(
890 2, /* Bytes */
891 true, /* Signed */
892 2, /* Channels */
893 44100, /* Hz */
894 false /* Swap Endian */
895 );
896
897 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&cfg));
898
899 PDMAUDIOVOLUME vol = { false, 0, 0 }; /* Not muted. */
900 AUDIOMIXBUF parent;
901 RTTESTI_CHECK_RC_OK(AudioMixBufInit(&parent, "Parent", &cfg, cBufSize));
902
903 AUDIOMIXBUF child;
904 RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child, "Child", &cfg, cBufSize));
905 RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child, &parent));
906
907 /* A few 16-bit signed samples. */
908 int16_t aFrames16S[16] = { INT16_MIN, INT16_MIN + 1, -128, -64, -4, -1, 0, 1,
909 2, 255, 256, INT16_MAX / 2, INT16_MAX - 2, INT16_MAX - 1, INT16_MAX, 0 };
910
911 /*
912 * Writing + mixing from child -> parent.
913 */
914 uint32_t cbBuf = 256;
915 char achBuf[256];
916 uint32_t cFramesRead, cFramesWritten, cFramesMixed;
917
918 uint32_t cFramesChild = 8;
919 uint32_t cFramesParent = cFramesChild;
920 uint32_t cFramesTotalRead;
921 int16_t *pSrc16;
922 int16_t *pDst16;
923
924 /**** Volume control test ****/
925 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Volume control test %uHz %uch \n", cfg.uHz, PDMAudioPropsChannels(&cfg));
926
927 /* 1) Full volume/0dB attenuation (255). */
928 vol.uLeft = vol.uRight = 255;
929 AudioMixBufSetVolume(&child, &vol);
930
931 RTTESTI_CHECK_RC_OK(AudioMixBufWriteCirc(&child, &aFrames16S, sizeof(aFrames16S), &cFramesWritten));
932 RTTESTI_CHECK_MSG(cFramesWritten == cFramesChild, ("Child: Expected %RU32 written frames, got %RU32\n", cFramesChild, cFramesWritten));
933 RTTESTI_CHECK_RC_OK(AudioMixBufMixToParent(&child, cFramesWritten, &cFramesMixed));
934
935 cFramesTotalRead = 0;
936 for (;;)
937 {
938 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufAcquireReadBlock(&parent, achBuf, cbBuf, &cFramesRead));
939 if (!cFramesRead)
940 break;
941 cFramesTotalRead += cFramesRead;
942 AudioMixBufReleaseReadBlock(&parent, cFramesRead);
943 AudioMixBufFinish(&parent, cFramesRead);
944 }
945 RTTESTI_CHECK_MSG(cFramesTotalRead == cFramesParent, ("Parent: Expected %RU32 mixed frames, got %RU32\n", cFramesParent, cFramesTotalRead));
946
947 /* Check that at 0dB the frames came out unharmed. */
948 pSrc16 = &aFrames16S[0];
949 pDst16 = (int16_t *)achBuf;
950
951 for (unsigned i = 0; i < cFramesParent * 2 /* stereo */; ++i)
952 {
953 RTTESTI_CHECK_MSG(*pSrc16 == *pDst16, ("index %u: Dst=%d, Src=%d\n", i, *pDst16, *pSrc16));
954 ++pSrc16;
955 ++pDst16;
956 }
957 AudioMixBufReset(&child);
958
959 /* 2) Half volume/-6dB attenuation (16 steps down). */
960 vol.uLeft = vol.uRight = 255 - 16;
961 AudioMixBufSetVolume(&child, &vol);
962
963 RTTESTI_CHECK_RC_OK(AudioMixBufWriteCirc(&child, &aFrames16S, sizeof(aFrames16S), &cFramesWritten));
964 RTTESTI_CHECK_MSG(cFramesWritten == cFramesChild, ("Child: Expected %RU32 written frames, got %RU32\n", cFramesChild, cFramesWritten));
965 RTTESTI_CHECK_RC_OK(AudioMixBufMixToParent(&child, cFramesWritten, &cFramesMixed));
966
967 cFramesTotalRead = 0;
968 for (;;)
969 {
970 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufAcquireReadBlock(&parent, achBuf, cbBuf, &cFramesRead));
971 if (!cFramesRead)
972 break;
973 cFramesTotalRead += cFramesRead;
974 AudioMixBufReleaseReadBlock(&parent, cFramesRead);
975 AudioMixBufFinish(&parent, cFramesRead);
976 }
977 RTTESTI_CHECK_MSG(cFramesTotalRead == cFramesParent, ("Parent: Expected %RU32 mixed frames, got %RU32\n", cFramesParent, cFramesTotalRead));
978
979 /* Check that at -6dB the sample values are halved. */
980 pSrc16 = &aFrames16S[0];
981 pDst16 = (int16_t *)achBuf;
982
983 for (unsigned i = 0; i < cFramesParent * 2 /* stereo */; ++i)
984 {
985 /* Watch out! For negative values, x >> 1 is not the same as x / 2. */
986 RTTESTI_CHECK_MSG(*pSrc16 >> 1 == *pDst16, ("index %u: Dst=%d, Src=%d\n", i, *pDst16, *pSrc16));
987 ++pSrc16;
988 ++pDst16;
989 }
990
991 AudioMixBufDestroy(&parent);
992 AudioMixBufDestroy(&child);
993
994 return RTTestSubErrorCount(hTest) ? VERR_GENERAL_FAILURE : VINF_SUCCESS;
995}
996#endif
997
998int main(int argc, char **argv)
999{
1000 RTR3InitExe(argc, &argv, 0);
1001
1002 /*
1003 * Initialize IPRT and create the test.
1004 */
1005 RTTEST hTest;
1006 int rc = RTTestInitAndCreate("tstAudioMixBuffer", &hTest);
1007 if (rc)
1008 return rc;
1009 RTTestBanner(hTest);
1010
1011 tstBasics(hTest);
1012 tstSimple(hTest);
1013 //tstParentChild(hTest);
1014 //tstConversion8(hTest);
1015 //tstConversion16(hTest);
1016 //tstVolume(hTest);
1017#if 0 /** @todo rewrite to non-parent/child setup */
1018 tstDownsampling(hTest, 44100, 22050);
1019 tstDownsampling(hTest, 48000, 44100);
1020 tstDownsampling(hTest, 48000, 22050);
1021 tstDownsampling(hTest, 48000, 11000);
1022#endif
1023 tstNewPeek(hTest, 48000, 48000);
1024 tstNewPeek(hTest, 48000, 11000);
1025 tstNewPeek(hTest, 48000, 44100);
1026 tstNewPeek(hTest, 44100, 22050);
1027 tstNewPeek(hTest, 44100, 11000);
1028 //tstNewPeek(hTest, 11000, 48000);
1029 //tstNewPeek(hTest, 22050, 44100);
1030
1031 /*
1032 * Summary
1033 */
1034 return RTTestSummaryAndDestroy(hTest);
1035}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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