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