VirtualBox

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

Last change on this file since 103914 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 43.0 KB
Line 
1/* $Id: tstAudioMixBuffer.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * Audio testcase - Mixing buffer.
4 */
5
6/*
7 * Copyright (C) 2014-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#include <iprt/errcore.h>
33#include <iprt/initterm.h>
34#include <iprt/mem.h>
35#include <iprt/rand.h>
36#include <iprt/stream.h>
37#include <iprt/string.h>
38#include <iprt/test.h>
39
40#include <VBox/vmm/pdm.h>
41#include <VBox/vmm/pdmaudioinline.h>
42
43#include "../AudioMixBuffer.h"
44#include "../AudioHlp.h"
45
46#define _USE_MATH_DEFINES
47#include <math.h> /* sin, M_PI */
48
49
50/*********************************************************************************************************************************
51* Global Variables *
52*********************************************************************************************************************************/
53#ifdef RT_LITTLE_ENDIAN
54bool const g_fLittleEndian = true;
55#else
56bool const g_fLittleEndian = false;
57#endif
58
59
60static void tstBasics(RTTEST hTest)
61{
62 RTTestSub(hTest, "Basics");
63
64 const PDMAUDIOPCMPROPS Cfg441StereoS16 = PDMAUDIOPCMPROPS_INITIALIZER(
65 /* a_cb: */ 2,
66 /* a_fSigned: */ true,
67 /* a_cChannels: */ 2,
68 /* a_uHz: */ 44100,
69 /* a_fSwapEndian: */ false
70 );
71 const PDMAUDIOPCMPROPS Cfg441StereoU16 = PDMAUDIOPCMPROPS_INITIALIZER(
72 /* a_cb: */ 2,
73 /* a_fSigned: */ false,
74 /* a_cChannels: */ 2,
75 /* a_uHz: */ 44100,
76 /* a_fSwapEndian: */ false
77 );
78 const PDMAUDIOPCMPROPS Cfg441StereoU32 = PDMAUDIOPCMPROPS_INITIALIZER(
79 /* a_cb: */ 4,
80 /* a_fSigned: */ false,
81 /* a_cChannels: */ 2,
82 /* a_uHz: */ 44100,
83 /* a_fSwapEndian: */ false
84 );
85
86 RTTESTI_CHECK(PDMAudioPropsGetBitrate(&Cfg441StereoS16) == 44100*4*8);
87 RTTESTI_CHECK(PDMAudioPropsGetBitrate(&Cfg441StereoU16) == 44100*4*8);
88 RTTESTI_CHECK(PDMAudioPropsGetBitrate(&Cfg441StereoU32) == 44100*8*8);
89
90 RTTESTI_CHECK(AudioHlpPcmPropsAreValidAndSupported(&Cfg441StereoS16));
91 RTTESTI_CHECK(AudioHlpPcmPropsAreValidAndSupported(&Cfg441StereoU16));
92 RTTESTI_CHECK(AudioHlpPcmPropsAreValidAndSupported(&Cfg441StereoU32));
93
94
95 RTTESTI_CHECK_MSG(PDMAUDIOPCMPROPS_F2B(&Cfg441StereoS16, 1) == 4,
96 ("got %x, expected 4\n", PDMAUDIOPCMPROPS_F2B(&Cfg441StereoS16, 1)));
97 RTTESTI_CHECK_MSG(PDMAUDIOPCMPROPS_F2B(&Cfg441StereoU16, 1) == 4,
98 ("got %x, expected 4\n", PDMAUDIOPCMPROPS_F2B(&Cfg441StereoU16, 1)));
99 RTTESTI_CHECK_MSG(PDMAUDIOPCMPROPS_F2B(&Cfg441StereoU32, 1) == 8,
100 ("got %x, expected 4\n", PDMAUDIOPCMPROPS_F2B(&Cfg441StereoU32, 1)));
101
102 RTTESTI_CHECK_MSG(PDMAudioPropsBytesPerFrame(&Cfg441StereoS16) == 4,
103 ("got %x, expected 4\n", PDMAudioPropsBytesPerFrame(&Cfg441StereoS16)));
104 RTTESTI_CHECK_MSG(PDMAudioPropsBytesPerFrame(&Cfg441StereoU16) == 4,
105 ("got %x, expected 4\n", PDMAudioPropsBytesPerFrame(&Cfg441StereoU16)));
106 RTTESTI_CHECK_MSG(PDMAudioPropsBytesPerFrame(&Cfg441StereoU32) == 8,
107 ("got %x, expected 4\n", PDMAudioPropsBytesPerFrame(&Cfg441StereoU32)));
108
109 uint32_t u32;
110 for (uint32_t i = 0; i < 256; i += 8)
111 {
112 RTTESTI_CHECK(PDMAudioPropsIsSizeAligned(&Cfg441StereoU32, i) == true);
113 for (uint32_t j = 1; j < 8; j++)
114 RTTESTI_CHECK(PDMAudioPropsIsSizeAligned(&Cfg441StereoU32, i + j) == false);
115 for (uint32_t j = 0; j < 8; j++)
116 RTTESTI_CHECK(PDMAudioPropsFloorBytesToFrame(&Cfg441StereoU32, i + j) == i);
117 }
118 for (uint32_t i = 0; i < 4096; i += 4)
119 {
120 RTTESTI_CHECK(PDMAudioPropsIsSizeAligned(&Cfg441StereoS16, i) == true);
121 for (uint32_t j = 1; j < 4; j++)
122 RTTESTI_CHECK(PDMAudioPropsIsSizeAligned(&Cfg441StereoS16, i + j) == false);
123 for (uint32_t j = 0; j < 4; j++)
124 RTTESTI_CHECK(PDMAudioPropsFloorBytesToFrame(&Cfg441StereoS16, i + j) == i);
125 }
126
127 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsFramesToBytes(&Cfg441StereoS16, 44100)) == 44100 * 2 * 2,
128 ("cb=%RU32\n", u32));
129 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsFramesToBytes(&Cfg441StereoS16, 2)) == 2 * 2 * 2,
130 ("cb=%RU32\n", u32));
131 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsFramesToBytes(&Cfg441StereoS16, 1)) == 4,
132 ("cb=%RU32\n", u32));
133 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsFramesToBytes(&Cfg441StereoU16, 1)) == 4,
134 ("cb=%RU32\n", u32));
135 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsFramesToBytes(&Cfg441StereoU32, 1)) == 8,
136 ("cb=%RU32\n", u32));
137
138 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsBytesToFrames(&Cfg441StereoS16, 4)) == 1, ("cb=%RU32\n", u32));
139 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsBytesToFrames(&Cfg441StereoU16, 4)) == 1, ("cb=%RU32\n", u32));
140 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsBytesToFrames(&Cfg441StereoU32, 8)) == 1, ("cb=%RU32\n", u32));
141
142 uint64_t u64;
143 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsBytesToNano(&Cfg441StereoS16, 44100 * 2 * 2)) == RT_NS_1SEC,
144 ("ns=%RU64\n", u64));
145 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsBytesToMicro(&Cfg441StereoS16, 44100 * 2 * 2)) == RT_US_1SEC,
146 ("us=%RU64\n", u64));
147 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsBytesToMilli(&Cfg441StereoS16, 44100 * 2 * 2)) == RT_MS_1SEC,
148 ("ms=%RU64\n", u64));
149
150 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsFramesToNano(&Cfg441StereoS16, 44100)) == RT_NS_1SEC, ("ns=%RU64\n", u64));
151 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsFramesToNano(&Cfg441StereoS16, 1)) == 22675, ("ns=%RU64\n", u64));
152 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsFramesToNano(&Cfg441StereoS16, 31)) == 702947, ("ns=%RU64\n", u64));
153 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsFramesToNano(&Cfg441StereoS16, 255)) == 5782312, ("ns=%RU64\n", u64));
154 //RTTESTI_CHECK_MSG((u64 = DrvAudioHlpFramesToMicro(&Cfg441StereoS16, 44100)) == RT_US_1SEC,
155 // ("us=%RU64\n", u64));
156 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsFramesToMilli(&Cfg441StereoS16, 44100)) == RT_MS_1SEC, ("ms=%RU64\n", u64));
157 RTTESTI_CHECK_MSG((u64 = PDMAudioPropsFramesToMilli(&Cfg441StereoS16, 255)) == 5, ("ms=%RU64\n", u64));
158
159 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsNanoToFrames(&Cfg441StereoS16, RT_NS_1SEC)) == 44100, ("cb=%RU32\n", u32));
160 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsNanoToFrames(&Cfg441StereoS16, 215876)) == 10, ("cb=%RU32\n", u32));
161 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsMilliToFrames(&Cfg441StereoS16, RT_MS_1SEC)) == 44100, ("cb=%RU32\n", u32));
162 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsMilliToFrames(&Cfg441StereoU32, 6)) == 265, ("cb=%RU32\n", u32));
163
164 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsNanoToBytes(&Cfg441StereoS16, RT_NS_1SEC)) == 44100*2*2, ("cb=%RU32\n", u32));
165 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsNanoToBytes(&Cfg441StereoS16, 702947)) == 31*2*2, ("cb=%RU32\n", u32));
166 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsMilliToBytes(&Cfg441StereoS16, RT_MS_1SEC)) == 44100*2*2, ("cb=%RU32\n", u32));
167 RTTESTI_CHECK_MSG((u32 = PDMAudioPropsMilliToBytes(&Cfg441StereoS16, 5)) == 884, ("cb=%RU32\n", u32));
168
169 /* DrvAudioHlpClearBuf: */
170 uint8_t *pbPage;
171 int rc = RTTestGuardedAlloc(hTest, HOST_PAGE_SIZE, 0, false /*fHead*/, (void **)&pbPage);
172 RTTESTI_CHECK_RC_OK_RETV(rc);
173
174 memset(pbPage, 0x42, HOST_PAGE_SIZE);
175 PDMAudioPropsClearBuffer(&Cfg441StereoS16, pbPage, HOST_PAGE_SIZE, HOST_PAGE_SIZE / 4);
176 RTTESTI_CHECK(ASMMemIsZero(pbPage, HOST_PAGE_SIZE));
177
178 memset(pbPage, 0x42, HOST_PAGE_SIZE);
179 PDMAudioPropsClearBuffer(&Cfg441StereoU16, pbPage, HOST_PAGE_SIZE, HOST_PAGE_SIZE / 4);
180 for (uint32_t off = 0; off < HOST_PAGE_SIZE; off += 2)
181 RTTESTI_CHECK_MSG(pbPage[off] == 0 && pbPage[off + 1] == 0x80, ("off=%#x: %#x %x\n", off, pbPage[off], pbPage[off + 1]));
182
183 memset(pbPage, 0x42, HOST_PAGE_SIZE);
184 PDMAudioPropsClearBuffer(&Cfg441StereoU32, pbPage, HOST_PAGE_SIZE, HOST_PAGE_SIZE / 8);
185 for (uint32_t off = 0; off < HOST_PAGE_SIZE; off += 4)
186 RTTESTI_CHECK(pbPage[off] == 0 && pbPage[off + 1] == 0 && pbPage[off + 2] == 0 && pbPage[off + 3] == 0x80);
187
188
189 RTTestDisableAssertions(hTest);
190 memset(pbPage, 0x42, HOST_PAGE_SIZE);
191 PDMAudioPropsClearBuffer(&Cfg441StereoS16, pbPage, HOST_PAGE_SIZE, HOST_PAGE_SIZE); /* should adjust down the frame count. */
192 RTTESTI_CHECK(ASMMemIsZero(pbPage, HOST_PAGE_SIZE));
193
194 memset(pbPage, 0x42, HOST_PAGE_SIZE);
195 PDMAudioPropsClearBuffer(&Cfg441StereoU16, pbPage, HOST_PAGE_SIZE, HOST_PAGE_SIZE); /* should adjust down the frame count. */
196 for (uint32_t off = 0; off < HOST_PAGE_SIZE; off += 2)
197 RTTESTI_CHECK_MSG(pbPage[off] == 0 && pbPage[off + 1] == 0x80, ("off=%#x: %#x %x\n", off, pbPage[off], pbPage[off + 1]));
198
199 memset(pbPage, 0x42, HOST_PAGE_SIZE);
200 PDMAudioPropsClearBuffer(&Cfg441StereoU32, pbPage, HOST_PAGE_SIZE, HOST_PAGE_SIZE); /* should adjust down the frame count. */
201 for (uint32_t off = 0; off < HOST_PAGE_SIZE; off += 4)
202 RTTESTI_CHECK(pbPage[off] == 0 && pbPage[off + 1] == 0 && pbPage[off + 2] == 0 && pbPage[off + 3] == 0x80);
203 RTTestRestoreAssertions(hTest);
204
205 RTTestGuardedFree(hTest, pbPage);
206}
207
208
209static void tstSimple(RTTEST hTest)
210{
211 RTTestSub(hTest, "Simple");
212
213 /* 44100Hz, 2 Channels, S16 */
214 PDMAUDIOPCMPROPS config = PDMAUDIOPCMPROPS_INITIALIZER(
215 2, /* Bytes */
216 true, /* Signed */
217 2, /* Channels */
218 44100, /* Hz */
219 false /* Swap Endian */
220 );
221
222 RTTESTI_CHECK(AudioHlpPcmPropsAreValidAndSupported(&config));
223
224 uint32_t cBufSize = _1K;
225
226 /*
227 * General stuff.
228 */
229 AUDIOMIXBUF mb;
230 RTTESTI_CHECK_RC_OK_RETV(AudioMixBufInit(&mb, "Single", &config, cBufSize));
231 RTTESTI_CHECK(AudioMixBufSize(&mb) == cBufSize);
232 RTTESTI_CHECK(AUDIOMIXBUF_B2F(&mb, AudioMixBufSizeBytes(&mb)) == cBufSize);
233 RTTESTI_CHECK(AUDIOMIXBUF_F2B(&mb, AudioMixBufSize(&mb)) == AudioMixBufSizeBytes(&mb));
234 RTTESTI_CHECK(AudioMixBufFree(&mb) == cBufSize);
235 RTTESTI_CHECK(AUDIOMIXBUF_F2B(&mb, AudioMixBufFree(&mb)) == AudioMixBufFreeBytes(&mb));
236
237 AUDIOMIXBUFWRITESTATE WriteState;
238 RTTESTI_CHECK_RC(AudioMixBufInitWriteState(&mb, &WriteState, &config), VINF_SUCCESS);
239
240 AUDIOMIXBUFPEEKSTATE PeekState;
241 RTTESTI_CHECK_RC(AudioMixBufInitPeekState(&mb, &PeekState, &config), VINF_SUCCESS);
242
243 /*
244 * A few writes (used to be the weird absolute writes).
245 */
246 uint32_t cFramesRead = 0, cFramesWritten = 0, cFramesWrittenAbs = 0;
247 int16_t aFrames16[2] = { 0xAA, 0xBB };
248 int32_t aFrames32[2] = { 0xCC, 0xDD };
249
250 RTTESTI_CHECK(AudioMixBufUsed(&mb) == 0);
251
252 AudioMixBufWrite(&mb, &WriteState, &aFrames16, sizeof(aFrames16), 0 /*offDstFrame*/, cBufSize / 4, &cFramesWritten);
253 RTTESTI_CHECK(cFramesWritten == 1 /* Frames */);
254 RTTESTI_CHECK(AudioMixBufUsed(&mb) == 0);
255 AudioMixBufCommit(&mb, cFramesWritten);
256 RTTESTI_CHECK(AudioMixBufUsed(&mb) == 1);
257 RTTESTI_CHECK(AudioMixBufReadPos(&mb) == 0);
258 RTTESTI_CHECK(AudioMixBufWritePos(&mb) == 1);
259
260 AudioMixBufWrite(&mb, &WriteState, &aFrames32, sizeof(aFrames32), 0 /*offDstFrame*/, cBufSize / 4, &cFramesWritten);
261 RTTESTI_CHECK(cFramesWritten == 2 /* Frames */);
262 AudioMixBufCommit(&mb, cFramesWritten);
263 RTTESTI_CHECK(AudioMixBufUsed(&mb) == 3);
264 RTTESTI_CHECK(AudioMixBufReadPos(&mb) == 0);
265 RTTESTI_CHECK(AudioMixBufWritePos(&mb) == 3);
266
267 /* Pretend we read the frames.*/
268 AudioMixBufAdvance(&mb, 3);
269 RTTESTI_CHECK(AudioMixBufUsed(&mb) == 0);
270 RTTESTI_CHECK(AudioMixBufReadPos(&mb) == 3);
271 RTTESTI_CHECK(AudioMixBufWritePos(&mb) == 3);
272
273 /* Fill up the buffer completely and check wraps. */
274
275 uint32_t cbSamples = PDMAudioPropsFramesToBytes(&config, cBufSize);
276 uint16_t *paSamples = (uint16_t *)RTMemAlloc(cbSamples);
277 RTTESTI_CHECK_RETV(paSamples);
278 AudioMixBufWrite(&mb, &WriteState, paSamples, cbSamples, 0 /*offDstFrame*/, cBufSize, &cFramesWritten);
279 RTTESTI_CHECK(cFramesWritten == cBufSize);
280 AudioMixBufCommit(&mb, cFramesWritten);
281 RTTESTI_CHECK(AudioMixBufUsed(&mb) == cBufSize);
282 RTTESTI_CHECK(AudioMixBufReadPos(&mb) == 3);
283 RTTESTI_CHECK(AudioMixBufWritePos(&mb) == 3);
284 RTMemFree(paSamples);
285 cbSamples = 0;
286
287 /*
288 * Writes and reads (used to be circular).
289 */
290 AudioMixBufDrop(&mb);
291
292 cFramesWrittenAbs = AudioMixBufUsed(&mb);
293
294 uint32_t cToWrite = AudioMixBufSize(&mb) - cFramesWrittenAbs - 1; /* -1 as padding plus -2 frames for above. */
295 for (uint32_t i = 0; i < cToWrite; i++)
296 {
297 AudioMixBufWrite(&mb, &WriteState, &aFrames16[0], sizeof(aFrames16), 0 /*offDstFrame*/, 1, &cFramesWritten);
298 RTTESTI_CHECK(cFramesWritten == 1);
299 AudioMixBufCommit(&mb, cFramesWritten);
300 }
301 RTTESTI_CHECK(!AudioMixBufIsEmpty(&mb));
302 RTTESTI_CHECK(AudioMixBufFree(&mb) == 1);
303 RTTESTI_CHECK(AudioMixBufFreeBytes(&mb) == AUDIOMIXBUF_F2B(&mb, 1U));
304 RTTESTI_CHECK(AudioMixBufUsed(&mb) == cToWrite + cFramesWrittenAbs /* + last absolute write */);
305
306 AudioMixBufWrite(&mb, &WriteState, &aFrames16[0], sizeof(aFrames16), 0 /*offDstFrame*/, 1, &cFramesWritten);
307 RTTESTI_CHECK(cFramesWritten == 1);
308 AudioMixBufCommit(&mb, cFramesWritten);
309 RTTESTI_CHECK(AudioMixBufFree(&mb) == 0);
310 RTTESTI_CHECK(AudioMixBufFreeBytes(&mb) == AUDIOMIXBUF_F2B(&mb, 0U));
311 RTTESTI_CHECK(AudioMixBufUsed(&mb) == cBufSize);
312
313 /* Reads. */
314 RTTESTI_CHECK(AudioMixBufReadPos(&mb) == 0);
315 uint32_t cbRead;
316 uint16_t aFrames16Buf[RT_ELEMENTS(aFrames16)];
317 uint32_t cToRead = AudioMixBufSize(&mb) - cFramesWrittenAbs - 1;
318 for (uint32_t i = 0; i < cToRead; i++)
319 {
320 AudioMixBufPeek(&mb, 0 /*offSrcFrame*/, 1, &cFramesRead, &PeekState, aFrames16Buf, sizeof(aFrames16Buf), &cbRead);
321 RTTESTI_CHECK(cFramesRead == 1);
322 RTTESTI_CHECK(cbRead == sizeof(aFrames16Buf));
323 AudioMixBufAdvance(&mb, cFramesRead);
324 RTTESTI_CHECK(AudioMixBufReadPos(&mb) == i + 1);
325 }
326 RTTESTI_CHECK(!AudioMixBufIsEmpty(&mb));
327 RTTESTI_CHECK(AudioMixBufFree(&mb) == AudioMixBufSize(&mb) - cFramesWrittenAbs - 1);
328 RTTESTI_CHECK(AudioMixBufFreeBytes(&mb) == AUDIOMIXBUF_F2B(&mb, cBufSize - cFramesWrittenAbs - 1));
329 RTTESTI_CHECK(AudioMixBufUsed(&mb) == cBufSize - cToRead);
330
331 AudioMixBufPeek(&mb, 0 /*offSrcFrame*/, 1, &cFramesRead, &PeekState, aFrames16Buf, sizeof(aFrames16Buf), &cbRead);
332 RTTESTI_CHECK(cFramesRead == 1);
333 RTTESTI_CHECK(cbRead == sizeof(aFrames16Buf));
334 AudioMixBufAdvance(&mb, cFramesRead);
335 RTTESTI_CHECK(AudioMixBufFree(&mb) == cBufSize - cFramesWrittenAbs);
336 RTTESTI_CHECK(AudioMixBufFreeBytes(&mb) == AUDIOMIXBUF_F2B(&mb, cBufSize - cFramesWrittenAbs));
337 RTTESTI_CHECK(AudioMixBufUsed(&mb) == cFramesWrittenAbs);
338 RTTESTI_CHECK(AudioMixBufReadPos(&mb) == 0);
339
340 AudioMixBufTerm(&mb);
341}
342
343/** @name Eight test samples represented in all basic formats.
344 * @{ */
345static uint8_t const g_au8TestSamples[8] = { 0x1, 0x11, 0x32, 0x7f, 0x80, 0x81, 0xbe, 0xff };
346static int8_t const g_ai8TestSamples[8] = { -127, -111, -78, -1, 0, 1, 62, 127 };
347static uint16_t const g_au16TestSamples[8] = { 0x100, 0x1100, 0x3200, 0x7f00, 0x8000, 0x8100, 0xbe00, 0xff00 };
348static int16_t const g_ai16TestSamples[8] = { -32512, -28416, -19968, -256, 0, 256, 15872, 32512 };
349static uint32_t const g_au32TestSamples[8] = { 0x1000000, 0x11000000, 0x32000000, 0x7f000000, 0x80000000, 0x81000000, 0xbe000000, 0xff000000 };
350static int32_t const g_ai32TestSamples[8] = { -2130706432, -1862270976, -1308622848, -16777216, 0, 16777216, 1040187392, 2130706432 };
351static int64_t const g_ai64TestSamples[8] = { -2130706432, -1862270976, -1308622848, -16777216, 0, 16777216, 1040187392, 2130706432 };
352static struct { void const *apv[2]; uint32_t cb; } g_aTestSamples[] =
353{
354 /* 0/0: */ { { NULL, NULL }, 0 },
355 /* 1/8: */ { { g_au8TestSamples, g_ai8TestSamples }, sizeof( g_au8TestSamples) },
356 /* 2/16: */ { { g_au16TestSamples, g_ai16TestSamples }, sizeof(g_au16TestSamples) },
357 /* 3/24: */ { { NULL, NULL }, 0 },
358 /* 4/32: */ { { g_au32TestSamples, g_ai32TestSamples }, sizeof(g_au32TestSamples) },
359 /* 5: */ { { NULL, NULL }, 0 },
360 /* 6: */ { { NULL, NULL }, 0 },
361 /* 7: */ { { NULL, NULL }, 0 },
362 /* 8:64 */ { { NULL, g_ai64TestSamples }, sizeof(g_ai64TestSamples) }, /* raw */
363};
364/** @} */
365
366/** Fills a buffer with samples from an g_aTestSamples entry. */
367static uint32_t tstFillBuf(PCPDMAUDIOPCMPROPS pCfg, void const *pvTestSamples, uint32_t iTestSample,
368 uint8_t *pbBuf, uint32_t cFrames)
369{
370 uint8_t const cTestSamples = RT_ELEMENTS(g_au8TestSamples);
371
372 cFrames *= PDMAudioPropsChannels(pCfg);
373 switch (PDMAudioPropsSampleSize(pCfg))
374 {
375 case 1:
376 {
377 uint8_t const * const pau8TestSamples = (uint8_t const *)pvTestSamples;
378 uint8_t *pu8Dst = (uint8_t *)pbBuf;
379 while (cFrames-- > 0)
380 {
381 *pu8Dst++ = pau8TestSamples[iTestSample];
382 iTestSample = (iTestSample + 1) % cTestSamples;
383 }
384 break;
385 }
386
387 case 2:
388 {
389 uint16_t const * const pau16TestSamples = (uint16_t const *)pvTestSamples;
390 uint16_t *pu16Dst = (uint16_t *)pbBuf;
391 while (cFrames-- > 0)
392 {
393 *pu16Dst++ = pau16TestSamples[iTestSample];
394 iTestSample = (iTestSample + 1) % cTestSamples;
395 }
396 break;
397 }
398
399 case 4:
400 {
401 uint32_t const * const pau32TestSamples = (uint32_t const *)pvTestSamples;
402 uint32_t *pu32Dst = (uint32_t *)pbBuf;
403 while (cFrames-- > 0)
404 {
405 *pu32Dst++ = pau32TestSamples[iTestSample];
406 iTestSample = (iTestSample + 1) % cTestSamples;
407 }
408 break;
409 }
410
411 case 8:
412 {
413 uint64_t const * const pau64TestSamples = (uint64_t const *)pvTestSamples;
414 uint64_t *pu64Dst = (uint64_t *)pbBuf;
415 while (cFrames-- > 0)
416 {
417 *pu64Dst++ = pau64TestSamples[iTestSample];
418 iTestSample = (iTestSample + 1) % cTestSamples;
419 }
420 break;
421 }
422
423 default:
424 AssertFailedBreak();
425 }
426 return iTestSample;
427}
428
429
430static void tstConversion(RTTEST hTest, uint8_t cSrcBits, bool fSrcSigned, uint8_t cSrcChs,
431 uint8_t cDstBits, bool fDstSigned, uint8_t cDstChs)
432{
433 RTTestSubF(hTest, "Conv %uch %c%u to %uch %c%u", cSrcChs, fSrcSigned ? 'S' : 'U', cSrcBits,
434 cDstChs, fDstSigned ? 'S' : 'U', cDstBits);
435
436 PDMAUDIOPCMPROPS CfgSrc, CfgDst;
437 PDMAudioPropsInitEx(&CfgSrc, cSrcBits / 8, fSrcSigned, cSrcChs, 44100, g_fLittleEndian, cSrcBits == 64 /*fRaw*/);
438 PDMAudioPropsInitEx(&CfgDst, cDstBits / 8, fDstSigned, cDstChs, 44100, g_fLittleEndian, cDstBits == 64 /*fRaw*/);
439
440 void const * const pvSrcTestSamples = g_aTestSamples[cSrcBits / 8].apv[fSrcSigned];
441 void const * const pvDstTestSamples = g_aTestSamples[cDstBits / 8].apv[fDstSigned];
442 uint32_t const cMixBufFrames = RTRandU32Ex(128, 16384);
443 uint32_t const cIterations = RTRandU32Ex(256, 1536);
444 uint32_t const cbSrcBuf = PDMAudioPropsFramesToBytes(&CfgSrc, cMixBufFrames + 64);
445 uint8_t * const pbSrcBuf = (uint8_t *)RTMemAllocZ(cbSrcBuf);
446 uint32_t const cbDstBuf = PDMAudioPropsFramesToBytes(&CfgDst, cMixBufFrames + 64);
447 uint8_t * const pbDstBuf = (uint8_t *)RTMemAllocZ(cbDstBuf);
448 uint8_t * const pbDstExpect = (uint8_t *)RTMemAllocZ(cbDstBuf);
449 RTTESTI_CHECK_RETV(pbSrcBuf);
450 RTTESTI_CHECK_RETV(pbDstBuf);
451 RTTESTI_CHECK_RETV(pbDstExpect);
452
453 AUDIOMIXBUF MixBuf;
454 RTTESTI_CHECK_RC_RETV(AudioMixBufInit(&MixBuf, "FormatOutputConversion", &CfgSrc, cMixBufFrames), VINF_SUCCESS);
455 AUDIOMIXBUFWRITESTATE WriteState;
456 RTTESTI_CHECK_RC_RETV(AudioMixBufInitWriteState(&MixBuf, &WriteState, &CfgSrc), VINF_SUCCESS);
457 AUDIOMIXBUFWRITESTATE WriteStateIgnZero = WriteState; RT_NOREF(WriteStateIgnZero);
458 AUDIOMIXBUFPEEKSTATE PeekState;
459 RTTESTI_CHECK_RC_RETV(AudioMixBufInitPeekState(&MixBuf, &PeekState, &CfgDst), VINF_SUCCESS);
460
461 uint32_t iSrcTestSample = 0;
462 uint32_t iDstTestSample = 0;
463 //RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "cIterations=%u\n", cIterations);
464 for (uint32_t iIteration = 0; iIteration < cIterations; iIteration++)
465 {
466 /* Write some frames to the buffer. */
467 uint32_t const cSrcFramesToWrite = iIteration < 16 ? iIteration + 1
468 : AudioMixBufFree(&MixBuf) ? RTRandU32Ex(1, AudioMixBufFree(&MixBuf)) : 0;
469 if (cSrcFramesToWrite > 0)
470 {
471 uint32_t const cbSrcToWrite = PDMAudioPropsFramesToBytes(&CfgSrc, cSrcFramesToWrite);
472 uint32_t cFrames = RTRandU32();
473 switch (RTRandU32Ex(0, 3))
474 {
475 default:
476 iSrcTestSample = tstFillBuf(&CfgSrc, pvSrcTestSamples, iSrcTestSample, pbSrcBuf, cSrcFramesToWrite);
477 AudioMixBufWrite(&MixBuf, &WriteState, pbSrcBuf, cbSrcToWrite, 0 /*offDstFrame*/, cSrcFramesToWrite, &cFrames);
478 RTTESTI_CHECK(cFrames == cSrcFramesToWrite);
479 break;
480
481 case 1: /* zero & blend */
482 AudioMixBufSilence(&MixBuf, &WriteStateIgnZero, 0 /*offFrame*/, cSrcFramesToWrite);
483 iSrcTestSample = tstFillBuf(&CfgSrc, pvSrcTestSamples, iSrcTestSample, pbSrcBuf, cSrcFramesToWrite);
484 AudioMixBufBlend(&MixBuf, &WriteState, pbSrcBuf, cbSrcToWrite, 0 /*offDstFrame*/, cSrcFramesToWrite, &cFrames);
485 RTTESTI_CHECK(cFrames == cSrcFramesToWrite);
486 break;
487
488 case 2: /* blend same equal data twice */
489 {
490 AUDIOMIXBUFWRITESTATE WriteStateSame = WriteState;
491 iSrcTestSample = tstFillBuf(&CfgSrc, pvSrcTestSamples, iSrcTestSample, pbSrcBuf, cSrcFramesToWrite);
492 AudioMixBufWrite(&MixBuf, &WriteState, pbSrcBuf, cbSrcToWrite, 0 /*offDstFrame*/, cSrcFramesToWrite, &cFrames);
493 RTTESTI_CHECK(cFrames == cSrcFramesToWrite);
494 AudioMixBufBlend(&MixBuf, &WriteStateSame, pbSrcBuf, cbSrcToWrite, 0 /*offDstFrame*/, cSrcFramesToWrite, &cFrames);
495 RTTESTI_CHECK(cFrames == cSrcFramesToWrite);
496 break;
497 }
498 case 3: /* write & blend with zero */
499 {
500 AUDIOMIXBUFWRITESTATE WriteStateSame = WriteState;
501 iSrcTestSample = tstFillBuf(&CfgSrc, pvSrcTestSamples, iSrcTestSample, pbSrcBuf, cSrcFramesToWrite);
502 AudioMixBufWrite(&MixBuf, &WriteState, pbSrcBuf, cbSrcToWrite, 0 /*offDstFrame*/, cSrcFramesToWrite, &cFrames);
503 RTTESTI_CHECK(cFrames == cSrcFramesToWrite);
504 PDMAudioPropsClearBuffer(&CfgSrc, pbSrcBuf, cbSrcToWrite, cSrcFramesToWrite);
505 AudioMixBufBlend(&MixBuf, &WriteStateSame, pbSrcBuf, cbSrcToWrite, 0 /*offDstFrame*/, cSrcFramesToWrite, &cFrames);
506 RTTESTI_CHECK(cFrames == cSrcFramesToWrite);
507 break;
508 }
509 }
510 AudioMixBufCommit(&MixBuf, cSrcFramesToWrite);
511 }
512
513 /* Read some frames back. */
514 uint32_t const cUsed = AudioMixBufUsed(&MixBuf);
515 uint32_t const cDstFramesToRead = iIteration < 16 ? iIteration + 1 : iIteration + 5 >= cIterations ? cUsed
516 : cUsed ? RTRandU32Ex(1, cUsed) : 0;
517 if (cDstFramesToRead > 0)
518 {
519 uint32_t const cbDstToRead = PDMAudioPropsFramesToBytes(&CfgDst, cDstFramesToRead);
520 uint32_t cbRead = RTRandU32();
521 uint32_t cFrames = RTRandU32();
522 RTRandBytes(pbDstBuf, cbDstToRead);
523 AudioMixBufPeek(&MixBuf, 0 /*offSrcFrame*/, (iIteration & 3) != 2 ? cDstFramesToRead : cUsed, &cFrames,
524 &PeekState, pbDstBuf, (iIteration & 3) != 3 ? cbDstToRead : cbDstBuf, &cbRead);
525 RTTESTI_CHECK(cFrames == cDstFramesToRead);
526 RTTESTI_CHECK(cbRead == cbDstToRead);
527 AudioMixBufAdvance(&MixBuf, cFrames);
528
529 /* Verify if we can. */
530 if (PDMAudioPropsChannels(&CfgSrc) == PDMAudioPropsChannels(&CfgDst))
531 {
532 iDstTestSample = tstFillBuf(&CfgDst, pvDstTestSamples, iDstTestSample, pbDstExpect, cFrames);
533 if (memcmp(pbDstExpect, pbDstBuf, cbRead) == 0)
534 { /* likely */ }
535 else
536 {
537 RTTestFailed(hTest,
538 "mismatch: %.*Rhxs\n"
539 "expected: %.*Rhxs\n"
540 "iIteration=%u cDstFramesToRead=%u cbRead=%#x\n",
541 RT_MIN(cbRead, 48), pbDstBuf,
542 RT_MIN(cbRead, 48), pbDstExpect,
543 iIteration, cDstFramesToRead, cbRead);
544 break;
545 }
546 }
547 }
548 }
549
550 AudioMixBufTerm(&MixBuf);
551 RTMemFree(pbSrcBuf);
552 RTMemFree(pbDstBuf);
553 RTMemFree(pbDstExpect);
554}
555
556
557#if 0 /** @todo rewrite to non-parent/child setup */
558static void tstDownsampling(RTTEST hTest, uint32_t uFromHz, uint32_t uToHz)
559{
560 RTTestSubF(hTest, "Downsampling %u to %u Hz (S16)", uFromHz, uToHz);
561
562 struct { int16_t l, r; }
563 aSrcFrames[4096],
564 aDstFrames[4096];
565
566 /* Parent (destination) buffer is xxxHz 2ch S16 */
567 uint32_t const cFramesParent = RTRandU32Ex(16, RT_ELEMENTS(aDstFrames));
568 PDMAUDIOPCMPROPS const CfgDst = PDMAUDIOPCMPROPS_INITIALIZER(2 /*cbSample*/, true /*fSigned*/, 2 /*ch*/, uToHz, false /*fSwap*/);
569 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&CfgDst));
570 AUDIOMIXBUF Parent;
571 RTTESTI_CHECK_RC_OK_RETV(AudioMixBufInit(&Parent, "ParentDownsampling", &CfgDst, cFramesParent));
572
573 /* Child (source) buffer is yyykHz 2ch S16 */
574 PDMAUDIOPCMPROPS const CfgSrc = PDMAUDIOPCMPROPS_INITIALIZER(2 /*cbSample*/, true /*fSigned*/, 2 /*ch*/, uFromHz, false /*fSwap*/);
575 RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&CfgSrc));
576 uint32_t const cFramesChild = RTRandU32Ex(32, RT_ELEMENTS(aSrcFrames));
577 AUDIOMIXBUF Child;
578 RTTESTI_CHECK_RC_OK_RETV(AudioMixBufInit(&Child, "ChildDownsampling", &CfgSrc, cFramesChild));
579 RTTESTI_CHECK_RC_OK_RETV(AudioMixBufLinkTo(&Child, &Parent));
580
581 /*
582 * Test parameters.
583 */
584 uint32_t const cMaxSrcFrames = RT_MIN(cFramesParent * uFromHz / uToHz - 1, cFramesChild);
585 uint32_t const cIterations = RTRandU32Ex(4, 128);
586 RTTestErrContext(hTest, "cFramesParent=%RU32 cFramesChild=%RU32 cMaxSrcFrames=%RU32 cIterations=%RU32",
587 cFramesParent, cFramesChild, cMaxSrcFrames, cIterations);
588 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "cFramesParent=%RU32 cFramesChild=%RU32 cMaxSrcFrames=%RU32 cIterations=%RU32\n",
589 cFramesParent, cFramesChild, cMaxSrcFrames, cIterations);
590
591 /*
592 * We generate a simple "A" sine wave as input.
593 */
594 uint32_t iSrcFrame = 0;
595 uint32_t iDstFrame = 0;
596 double rdFixed = 2.0 * M_PI * 440.0 /* A */ / PDMAudioPropsHz(&CfgSrc); /* Fixed sin() input. */
597 for (uint32_t i = 0; i < cIterations; i++)
598 {
599 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "i=%RU32\n", i);
600
601 /*
602 * Generate source frames and write them.
603 */
604 uint32_t const cSrcFrames = i < cIterations / 2
605 ? RTRandU32Ex(2, cMaxSrcFrames) & ~(uint32_t)1
606 : RTRandU32Ex(1, cMaxSrcFrames - 1) | 1;
607 for (uint32_t j = 0; j < cSrcFrames; j++, iSrcFrame++)
608 aSrcFrames[j].r = aSrcFrames[j].l = 32760 /*Amplitude*/ * sin(rdFixed * iSrcFrame);
609
610 uint32_t cSrcFramesWritten = UINT32_MAX / 2;
611 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufWriteAt(&Child, 0, &aSrcFrames, cSrcFrames * sizeof(aSrcFrames[0]),
612 &cSrcFramesWritten));
613 RTTESTI_CHECK_MSG_BREAK(cSrcFrames == cSrcFramesWritten,
614 ("cSrcFrames=%RU32 vs cSrcFramesWritten=%RU32\n", cSrcFrames, cSrcFramesWritten));
615
616 /*
617 * Mix them.
618 */
619 uint32_t cSrcFramesMixed = UINT32_MAX / 2;
620 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufMixToParent(&Child, cSrcFramesWritten, &cSrcFramesMixed));
621 RTTESTI_CHECK_MSG(AudioMixBufUsed(&Child) == 0, ("%RU32\n", AudioMixBufUsed(&Child)));
622 RTTESTI_CHECK_MSG_BREAK(cSrcFramesWritten == cSrcFramesMixed,
623 ("cSrcFramesWritten=%RU32 cSrcFramesMixed=%RU32\n", cSrcFramesWritten, cSrcFramesMixed));
624 RTTESTI_CHECK_MSG_BREAK(AudioMixBufUsed(&Child) == 0, ("%RU32\n", AudioMixBufUsed(&Child)));
625
626 /*
627 * Read out the parent buffer.
628 */
629 uint32_t cDstFrames = AudioMixBufUsed(&Parent);
630 while (cDstFrames > 0)
631 {
632 uint32_t cFramesRead = UINT32_MAX / 2;
633 RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufAcquireReadBlock(&Parent, aDstFrames, sizeof(aDstFrames), &cFramesRead));
634 RTTESTI_CHECK_MSG(cFramesRead > 0 && cFramesRead <= cDstFrames,
635 ("cFramesRead=%RU32 cDstFrames=%RU32\n", cFramesRead, cDstFrames));
636
637 AudioMixBufReleaseReadBlock(&Parent, cFramesRead);
638 AudioMixBufFinish(&Parent, cFramesRead);
639
640 iDstFrame += cFramesRead;
641 cDstFrames -= cFramesRead;
642 RTTESTI_CHECK(AudioMixBufUsed(&Parent) == cDstFrames);
643 }
644 }
645
646 RTTESTI_CHECK(AudioMixBufUsed(&Parent) == 0);
647 RTTESTI_CHECK(AudioMixBufLive(&Child) == 0);
648 uint32_t const cDstMinExpect = (uint64_t)iSrcFrame * uToHz / uFromHz;
649 uint32_t const cDstMaxExpect = ((uint64_t)iSrcFrame * uToHz + uFromHz - 1) / uFromHz;
650 RTTESTI_CHECK_MSG(iDstFrame == cDstMinExpect || iDstFrame == cDstMaxExpect,
651 ("iSrcFrame=%#x -> %#x,%#x; iDstFrame=%#x\n", iSrcFrame, cDstMinExpect, cDstMaxExpect, iDstFrame));
652
653 AudioMixBufDestroy(&Parent);
654 AudioMixBufDestroy(&Child);
655}
656#endif
657
658
659static void tstNewPeek(RTTEST hTest, uint32_t uFromHz, uint32_t uToHz)
660{
661 RTTestSubF(hTest, "New peek %u to %u Hz (S16)", uFromHz, uToHz);
662
663 struct { int16_t l, r; }
664 aSrcFrames[4096],
665 aDstFrames[4096];
666
667 /* Mix buffer is uFromHz 2ch S16 */
668 uint32_t const cFrames = RTRandU32Ex(16, RT_ELEMENTS(aSrcFrames));
669 PDMAUDIOPCMPROPS const CfgSrc = PDMAUDIOPCMPROPS_INITIALIZER(2 /*cbSample*/, true /*fSigned*/, 2 /*ch*/, uFromHz, false /*fSwap*/);
670 RTTESTI_CHECK(AudioHlpPcmPropsAreValidAndSupported(&CfgSrc));
671 AUDIOMIXBUF MixBuf;
672 RTTESTI_CHECK_RC_OK_RETV(AudioMixBufInit(&MixBuf, "NewPeekMixBuf", &CfgSrc, cFrames));
673
674 /* Write state (source). */
675 AUDIOMIXBUFWRITESTATE WriteState;
676 RTTESTI_CHECK_RC_OK_RETV(AudioMixBufInitWriteState(&MixBuf, &WriteState, &CfgSrc));
677
678 /* Peek state (destination) is uToHz 2ch S16 */
679 PDMAUDIOPCMPROPS const CfgDst = PDMAUDIOPCMPROPS_INITIALIZER(2 /*cbSample*/, true /*fSigned*/, 2 /*ch*/, uToHz, false /*fSwap*/);
680 RTTESTI_CHECK(AudioHlpPcmPropsAreValidAndSupported(&CfgDst));
681 AUDIOMIXBUFPEEKSTATE PeekState;
682 RTTESTI_CHECK_RC_OK_RETV(AudioMixBufInitPeekState(&MixBuf, &PeekState, &CfgDst));
683
684 /*
685 * Test parameters.
686 */
687 uint32_t const cMaxSrcFrames = RT_MIN(cFrames * uFromHz / uToHz - 1, cFrames);
688 uint32_t const cIterations = RTRandU32Ex(64, 1024);
689 RTTestErrContext(hTest, "cFrames=%RU32 cMaxSrcFrames=%RU32 cIterations=%RU32", cFrames, cMaxSrcFrames, cIterations);
690 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "cFrames=%RU32 cMaxSrcFrames=%RU32 cIterations=%RU32\n",
691 cFrames, cMaxSrcFrames, cIterations);
692
693 /*
694 * We generate a simple "A" sine wave as input.
695 */
696 uint32_t iSrcFrame = 0;
697 uint32_t iDstFrame = 0;
698 double rdFixed = 2.0 * M_PI * 440.0 /* A */ / PDMAudioPropsHz(&CfgSrc); /* Fixed sin() input. */
699 for (uint32_t i = 0; i < cIterations; i++)
700 {
701 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "i=%RU32\n", i);
702
703 /*
704 * Generate source frames and write them.
705 */
706 uint32_t const cSrcFrames = i < cIterations / 2
707 ? RTRandU32Ex(2, cMaxSrcFrames) & ~(uint32_t)1
708 : RTRandU32Ex(1, cMaxSrcFrames - 1) | 1;
709 for (uint32_t j = 0; j < cSrcFrames; j++, iSrcFrame++)
710 aSrcFrames[j].r = aSrcFrames[j].l = 32760 /*Amplitude*/ * sin(rdFixed * iSrcFrame);
711
712 uint32_t cSrcFramesWritten = UINT32_MAX / 2;
713 AudioMixBufWrite(&MixBuf, &WriteState, &aSrcFrames[0], cSrcFrames * sizeof(aSrcFrames[0]),
714 0 /*offDstFrame*/, cSrcFrames, &cSrcFramesWritten);
715 RTTESTI_CHECK_MSG_BREAK(cSrcFrames == cSrcFramesWritten,
716 ("cSrcFrames=%RU32 vs cSrcFramesWritten=%RU32 cLiveFrames=%RU32\n",
717 cSrcFrames, cSrcFramesWritten, AudioMixBufUsed(&MixBuf)));
718 AudioMixBufCommit(&MixBuf, cSrcFrames);
719
720 /*
721 * Read out all the frames using the peek function.
722 */
723 uint32_t offSrcFrame = 0;
724 while (offSrcFrame < cSrcFramesWritten)
725 {
726 uint32_t cSrcFramesToRead = cSrcFramesWritten - offSrcFrame;
727 uint32_t cTmp = (uint64_t)cSrcFramesToRead * uToHz / uFromHz;
728 if (cTmp + 32 >= RT_ELEMENTS(aDstFrames))
729 cSrcFramesToRead = ((uint64_t)RT_ELEMENTS(aDstFrames) - 32) * uFromHz / uToHz; /* kludge */
730
731 uint32_t cSrcFramesPeeked = UINT32_MAX / 4;
732 uint32_t cbDstPeeked = UINT32_MAX / 2;
733 RTRandBytes(aDstFrames, sizeof(aDstFrames));
734 AudioMixBufPeek(&MixBuf, offSrcFrame, cSrcFramesToRead, &cSrcFramesPeeked,
735 &PeekState, aDstFrames, sizeof(aDstFrames), &cbDstPeeked);
736 uint32_t cDstFramesPeeked = PDMAudioPropsBytesToFrames(&CfgDst, cbDstPeeked);
737 RTTESTI_CHECK(cbDstPeeked > 0 || cSrcFramesPeeked > 0);
738
739 if (uFromHz == uToHz)
740 {
741 for (uint32_t iDst = 0; iDst < cDstFramesPeeked; iDst++)
742 if (memcmp(&aDstFrames[iDst], &aSrcFrames[offSrcFrame + iDst], sizeof(aSrcFrames[0])) != 0)
743 RTTestFailed(hTest, "Frame #%u differs: %#x / %#x, expected %#x / %#x\n", iDstFrame + iDst,
744 aDstFrames[iDst].l, aDstFrames[iDst].r,
745 aSrcFrames[iDst + offSrcFrame].l, aSrcFrames[iDst + offSrcFrame].r);
746 }
747
748 offSrcFrame += cSrcFramesPeeked;
749 iDstFrame += cDstFramesPeeked;
750 }
751
752 /*
753 * Then advance.
754 */
755 AudioMixBufAdvance(&MixBuf, cSrcFrames);
756 RTTESTI_CHECK(AudioMixBufUsed(&MixBuf) == 0);
757 }
758
759 /** @todo this is a bit lax... */
760 uint32_t const cDstMinExpect = ((uint64_t)iSrcFrame * uToHz - uFromHz - 1) / uFromHz;
761 uint32_t const cDstMaxExpect = ((uint64_t)iSrcFrame * uToHz + uFromHz - 1) / uFromHz;
762 RTTESTI_CHECK_MSG(iDstFrame >= cDstMinExpect && iDstFrame <= cDstMaxExpect,
763 ("iSrcFrame=%#x -> %#x..%#x; iDstFrame=%#x (delta %d)\n",
764 iSrcFrame, cDstMinExpect, cDstMaxExpect, iDstFrame, (cDstMinExpect + cDstMaxExpect) / 2 - iDstFrame));
765
766 AudioMixBufTerm(&MixBuf);
767}
768
769/* Test volume control. */
770static void tstVolume(RTTEST hTest)
771{
772 RTTestSub(hTest, "Volume control (44.1kHz S16 2ch)");
773 uint32_t const cBufSize = 256;
774
775 /*
776 * Configure a mixbuf where we read and write 44.1kHz S16 2ch.
777 */
778 PDMAUDIOPCMPROPS const Cfg = PDMAUDIOPCMPROPS_INITIALIZER(
779 2, /* Bytes */
780 true, /* Signed */
781 2, /* Channels */
782 44100, /* Hz */
783 false /* Swap Endian */
784 );
785 AUDIOMIXBUF MixBuf;
786 RTTESTI_CHECK_RC_RETV(AudioMixBufInit(&MixBuf, "Volume", &Cfg, cBufSize), VINF_SUCCESS);
787
788 AUDIOMIXBUFWRITESTATE WriteState;
789 RTTESTI_CHECK_RC_RETV(AudioMixBufInitWriteState(&MixBuf, &WriteState, &Cfg), VINF_SUCCESS);
790
791 AUDIOMIXBUFPEEKSTATE PeekState;
792 RTTESTI_CHECK_RC_RETV(AudioMixBufInitPeekState(&MixBuf, &PeekState, &Cfg), VINF_SUCCESS);
793
794 /*
795 * A few 16-bit signed test samples.
796 */
797 static int16_t const s_aFrames16S[16] =
798 {
799 INT16_MIN, INT16_MIN + 1, -128, -64, -4, -1, 0, 1,
800 2, 255, 256, INT16_MAX / 2, INT16_MAX - 2, INT16_MAX - 1, INT16_MAX, 0,
801 };
802
803 /*
804 * 1) Full volume/0dB attenuation (255).
805 */
806 PDMAUDIOVOLUME Vol = PDMAUDIOVOLUME_INITIALIZER_MAX;
807 AudioMixBufSetVolume(&MixBuf, &Vol);
808
809 /* Write all the test frames to the mixer buffer: */
810 uint32_t cFramesWritten;
811 AudioMixBufWrite(&MixBuf, &WriteState, &s_aFrames16S[0], sizeof(s_aFrames16S), 0 /*offDstFrame*/, cBufSize, &cFramesWritten);
812 RTTESTI_CHECK(cFramesWritten == RT_ELEMENTS(s_aFrames16S) / 2);
813 AudioMixBufCommit(&MixBuf, cFramesWritten);
814
815 /* Read them back. We should get them back just like we wrote them. */
816 uint16_t au16Buf[cBufSize * 2];
817 uint32_t cFramesPeeked;
818 uint32_t cbPeeked;
819 AudioMixBufPeek(&MixBuf, 0 /*offSrcFrame*/, cFramesWritten, &cFramesPeeked, &PeekState, au16Buf, sizeof(au16Buf), &cbPeeked);
820 RTTESTI_CHECK(cFramesPeeked == cFramesWritten);
821 RTTESTI_CHECK(cbPeeked == PDMAudioPropsFramesToBytes(&Cfg, cFramesPeeked));
822 AudioMixBufAdvance(&MixBuf, cFramesPeeked);
823
824 /* Check that at 0dB the frames came out unharmed. */
825 if (memcmp(au16Buf, s_aFrames16S, sizeof(s_aFrames16S)) != 0)
826 RTTestFailed(hTest,
827 "0dB test failed\n"
828 "mismatch: %.*Rhxs\n"
829 "expected: %.*Rhxs\n",
830 sizeof(s_aFrames16S), au16Buf, sizeof(s_aFrames16S), s_aFrames16S);
831
832 /*
833 * 2) Half volume/-6dB attenuation (16 steps down).
834 */
835 PDMAudioVolumeInitFromStereo(&Vol, false, 255 - 16, 255 - 16);
836 AudioMixBufSetVolume(&MixBuf, &Vol);
837
838 /* Write all the test frames to the mixer buffer: */
839 AudioMixBufWrite(&MixBuf, &WriteState, &s_aFrames16S[0], sizeof(s_aFrames16S), 0 /*offDstFrame*/, cBufSize, &cFramesWritten);
840 RTTESTI_CHECK(cFramesWritten == RT_ELEMENTS(s_aFrames16S) / 2);
841 AudioMixBufCommit(&MixBuf, cFramesWritten);
842
843 /* Read them back. We should get them back just like we wrote them. */
844 AudioMixBufPeek(&MixBuf, 0 /*offSrcFrame*/, cFramesWritten, &cFramesPeeked, &PeekState, au16Buf, sizeof(au16Buf), &cbPeeked);
845 RTTESTI_CHECK(cFramesPeeked == cFramesWritten);
846 RTTESTI_CHECK(cbPeeked == PDMAudioPropsFramesToBytes(&Cfg, cFramesPeeked));
847 AudioMixBufAdvance(&MixBuf, cFramesPeeked);
848
849 /* Check that at -6dB the sample values are halved. */
850 int16_t ai16Expect[sizeof(s_aFrames16S) / 2];
851 memcpy(ai16Expect, s_aFrames16S, sizeof(ai16Expect));
852 for (uintptr_t i = 0; i < RT_ELEMENTS(ai16Expect); i++)
853 ai16Expect[i] >>= 1; /* /= 2 - not the same for signed numbers; */
854 if (memcmp(au16Buf, ai16Expect, sizeof(ai16Expect)) != 0)
855 RTTestFailed(hTest,
856 "-6dB test failed\n"
857 "mismatch: %.*Rhxs\n"
858 "expected: %.*Rhxs\n"
859 "wrote: %.*Rhxs\n",
860 sizeof(ai16Expect), au16Buf, sizeof(ai16Expect), ai16Expect, sizeof(s_aFrames16S), s_aFrames16S);
861
862 AudioMixBufTerm(&MixBuf);
863}
864
865
866int main(int argc, char **argv)
867{
868 RTR3InitExe(argc, &argv, 0);
869
870 /*
871 * Initialize IPRT and create the test.
872 */
873 RTTEST hTest;
874 int rc = RTTestInitAndCreate("tstAudioMixBuffer", &hTest);
875 if (rc)
876 return rc;
877 RTTestBanner(hTest);
878
879 tstBasics(hTest);
880 tstSimple(hTest);
881
882 /* Run tstConversion for all combinations we have test data. */
883 for (unsigned iSrc = 0; iSrc < RT_ELEMENTS(g_aTestSamples); iSrc++)
884 {
885 for (unsigned iSrcSigned = 0; iSrcSigned < RT_ELEMENTS(g_aTestSamples[0].apv); iSrcSigned++)
886 if (g_aTestSamples[iSrc].apv[iSrcSigned])
887 for (unsigned cSrcChs = 1; cSrcChs <= 2; cSrcChs++)
888 for (unsigned iDst = 0; iDst < RT_ELEMENTS(g_aTestSamples); iDst++)
889 for (unsigned iDstSigned = 0; iDstSigned < RT_ELEMENTS(g_aTestSamples[0].apv); iDstSigned++)
890 if (g_aTestSamples[iDst].apv[iDstSigned])
891 for (unsigned cDstChs = 1; cDstChs <= 2; cDstChs++)
892 tstConversion(hTest, iSrc * 8, iSrcSigned == 1, cSrcChs,
893 /*->*/ iDst * 8, iDstSigned == 1, cDstChs);
894 }
895
896#if 0 /** @todo rewrite to non-parent/child setup */
897 tstDownsampling(hTest, 44100, 22050);
898 tstDownsampling(hTest, 48000, 44100);
899 tstDownsampling(hTest, 48000, 22050);
900 tstDownsampling(hTest, 48000, 11000);
901#endif
902 tstNewPeek(hTest, 48000, 48000);
903 tstNewPeek(hTest, 48000, 11000);
904 tstNewPeek(hTest, 48000, 44100);
905 tstNewPeek(hTest, 44100, 22050);
906 tstNewPeek(hTest, 44100, 11000);
907 //tstNewPeek(hTest, 11000, 48000);
908 //tstNewPeek(hTest, 22050, 44100);
909
910 tstVolume(hTest);
911
912 /*
913 * Summary
914 */
915 return RTTestSummaryAndDestroy(hTest);
916}
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