VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/AudioMixBuffer.cpp@ 68712

Last change on this file since 68712 was 68271, checked in by vboxsync, 7 years ago

Audio: Renamed PDMAUDIOSTRMRATE -> PDMAUDIOSTREAMRATE. No actual code changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 69.2 KB
Line 
1/* $Id: AudioMixBuffer.cpp 68271 2017-08-03 08:19:14Z vboxsync $ */
2/** @file
3 * VBox audio: Audio mixing buffer for converting reading/writing audio data.
4 */
5
6/*
7 * Copyright (C) 2014-2017 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#define LOG_GROUP LOG_GROUP_AUDIO_MIXER_BUFFER
18#include <VBox/log.h>
19
20#if 0
21/*
22 * AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA enables dumping the raw PCM data
23 * to a file on the host. Be sure to adjust AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA_PATH
24 * to your needs before using this!
25 */
26# define AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA
27# ifdef RT_OS_WINDOWS
28# define AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA_PATH "c:\\temp\\"
29# else
30# define AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA_PATH "/tmp/"
31# endif
32/* Warning: Enabling this will generate *huge* logs! */
33//# define AUDIOMIXBUF_DEBUG_MACROS
34#endif
35
36#include <iprt/asm-math.h>
37#include <iprt/assert.h>
38#ifdef AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA
39# include <iprt/file.h>
40#endif
41#include <iprt/mem.h>
42#include <iprt/string.h> /* For RT_BZERO. */
43
44#ifdef VBOX_AUDIO_TESTCASE
45# define LOG_ENABLED
46# include <iprt/stream.h>
47#endif
48#include <VBox/err.h>
49
50#include "AudioMixBuffer.h"
51
52#ifndef VBOX_AUDIO_TESTCASE
53# ifdef DEBUG
54# define AUDMIXBUF_LOG(x) LogFlowFunc(x)
55# else
56# define AUDMIXBUF_LOG(x) do {} while (0)
57# endif
58#else /* VBOX_AUDIO_TESTCASE */
59# define AUDMIXBUF_LOG(x) RTPrintf x
60#endif
61
62#ifdef DEBUG
63DECLINLINE(void) audioMixBufDbgPrintInternal(PPDMAUDIOMIXBUF pMixBuf, const char *pszFunc);
64DECL_FORCE_INLINE(bool) audioMixBufDbgValidate(PPDMAUDIOMIXBUF pMixBuf);
65#endif
66
67/*
68 * Soft Volume Control
69 *
70 * The external code supplies an 8-bit volume (attenuation) value in the
71 * 0 .. 255 range. This represents 0 to -96dB attenuation where an input
72 * value of 0 corresponds to -96dB and 255 corresponds to 0dB (unchanged).
73 *
74 * Each step thus corresponds to 96 / 256 or 0.375dB. Every 6dB (16 steps)
75 * represents doubling the sample value.
76 *
77 * For internal use, the volume control needs to be converted to a 16-bit
78 * (sort of) exponential value between 1 and 65536. This is used with fixed
79 * point arithmetic such that 65536 means 1.0 and 1 means 1/65536.
80 *
81 * For actual volume calculation, 33.31 fixed point is used. Maximum (or
82 * unattenuated) volume is represented as 0x40000000; conveniently, this
83 * value fits into a uint32_t.
84 *
85 * To enable fast processing, the maximum volume must be a power of two
86 * and must not have a sign when converted to int32_t. While 0x80000000
87 * violates these constraints, 0x40000000 does not.
88 */
89
90
91/** Logarithmic/exponential volume conversion table. */
92static uint32_t s_aVolumeConv[256] = {
93 1, 1, 1, 1, 1, 1, 1, 1, /* 7 */
94 1, 2, 2, 2, 2, 2, 2, 2, /* 15 */
95 2, 2, 2, 2, 2, 3, 3, 3, /* 23 */
96 3, 3, 3, 3, 4, 4, 4, 4, /* 31 */
97 4, 4, 5, 5, 5, 5, 5, 6, /* 39 */
98 6, 6, 6, 7, 7, 7, 8, 8, /* 47 */
99 8, 9, 9, 10, 10, 10, 11, 11, /* 55 */
100 12, 12, 13, 13, 14, 15, 15, 16, /* 63 */
101 17, 17, 18, 19, 20, 21, 22, 23, /* 71 */
102 24, 25, 26, 27, 28, 29, 31, 32, /* 79 */
103 33, 35, 36, 38, 40, 41, 43, 45, /* 87 */
104 47, 49, 52, 54, 56, 59, 61, 64, /* 95 */
105 67, 70, 73, 76, 79, 83, 87, 91, /* 103 */
106 95, 99, 103, 108, 112, 117, 123, 128, /* 111 */
107 134, 140, 146, 152, 159, 166, 173, 181, /* 119 */
108 189, 197, 206, 215, 225, 235, 245, 256, /* 127 */
109 267, 279, 292, 304, 318, 332, 347, 362, /* 135 */
110 378, 395, 412, 431, 450, 470, 490, 512, /* 143 */
111 535, 558, 583, 609, 636, 664, 693, 724, /* 151 */
112 756, 790, 825, 861, 899, 939, 981, 1024, /* 159 */
113 1069, 1117, 1166, 1218, 1272, 1328, 1387, 1448, /* 167 */
114 1512, 1579, 1649, 1722, 1798, 1878, 1961, 2048, /* 175 */
115 2139, 2233, 2332, 2435, 2543, 2656, 2774, 2896, /* 183 */
116 3025, 3158, 3298, 3444, 3597, 3756, 3922, 4096, /* 191 */
117 4277, 4467, 4664, 4871, 5087, 5312, 5547, 5793, /* 199 */
118 6049, 6317, 6597, 6889, 7194, 7512, 7845, 8192, /* 207 */
119 8555, 8933, 9329, 9742, 10173, 10624, 11094, 11585, /* 215 */
120 12098, 12634, 13193, 13777, 14387, 15024, 15689, 16384, /* 223 */
121 17109, 17867, 18658, 19484, 20347, 21247, 22188, 23170, /* 231 */
122 24196, 25268, 26386, 27554, 28774, 30048, 31379, 32768, /* 239 */
123 34219, 35734, 37316, 38968, 40693, 42495, 44376, 46341, /* 247 */
124 48393, 50535, 52773, 55109, 57549, 60097, 62757, 65536, /* 255 */
125};
126
127/* Bit shift for fixed point conversion. */
128#define AUDIOMIXBUF_VOL_SHIFT 30
129
130/* Internal representation of 0dB volume (1.0 in fixed point). */
131#define AUDIOMIXBUF_VOL_0DB (1 << AUDIOMIXBUF_VOL_SHIFT)
132
133AssertCompile(AUDIOMIXBUF_VOL_0DB <= 0x40000000); /* Must always hold. */
134AssertCompile(AUDIOMIXBUF_VOL_0DB == 0x40000000); /* For now -- when only attenuation is used. */
135
136
137/**
138 * Peeks for audio frames without any conversion done.
139 * This will get the raw frame data out of a mixing buffer.
140 *
141 * @return IPRT status code or VINF_AUDIO_MORE_DATA_AVAILABLE if more data is available to read.
142 *
143 * @param pMixBuf Mixing buffer to acquire audio frames from.
144 * @param cFramesToRead Number of audio frames to read.
145 * @param paFrameBuf Buffer where to store the returned audio frames.
146 * @param cFrameBuf Size (in frames) of the buffer to store audio frames into.
147 * @param pcFramesRead Returns number of read audio frames. Optional.
148 *
149 * @remark This function is not thread safe!
150 */
151int AudioMixBufPeek(PPDMAUDIOMIXBUF pMixBuf, uint32_t cFramesToRead,
152 PPDMAUDIOFRAME paFrameBuf, uint32_t cFrameBuf, uint32_t *pcFramesRead)
153{
154 AssertPtrReturn(pMixBuf, VERR_INVALID_POINTER);
155 AssertPtrReturn(paFrameBuf, VERR_INVALID_POINTER);
156 AssertReturn(cFrameBuf, VERR_INVALID_PARAMETER);
157 /* pcRead is optional. */
158
159 int rc;
160
161 if (!cFramesToRead)
162 {
163 if (pcFramesRead)
164 *pcFramesRead = 0;
165 return VINF_SUCCESS;
166 }
167
168 uint32_t cRead;
169 if (pMixBuf->offRead + cFramesToRead > pMixBuf->cFrames)
170 {
171 cRead = pMixBuf->cFrames - pMixBuf->offRead;
172 rc = VINF_AUDIO_MORE_DATA_AVAILABLE;
173 }
174 else
175 {
176 cRead = cFramesToRead;
177 rc = VINF_SUCCESS;
178 }
179
180 if (cRead > cFrameBuf)
181 {
182 cRead = cFrameBuf;
183 rc = VINF_AUDIO_MORE_DATA_AVAILABLE;
184 }
185
186 if (cRead)
187 {
188 memcpy(paFrameBuf, &pMixBuf->pFrames[pMixBuf->offRead], sizeof(PDMAUDIOFRAME) * cRead);
189
190 pMixBuf->offRead = (pMixBuf->offRead + cRead) % pMixBuf->cFrames;
191 Assert(pMixBuf->offRead <= pMixBuf->cFrames);
192 pMixBuf->cUsed -= RT_MIN(cRead, pMixBuf->cUsed);
193 }
194
195 if (pcFramesRead)
196 *pcFramesRead = cRead;
197
198 return rc;
199}
200
201/**
202 * Returns a mutable pointer to the mixing buffer's audio frame buffer for writing raw
203 * audio frames.
204 *
205 * @return IPRT status code. VINF_TRY_AGAIN for getting next pointer at beginning (circular).
206 * @param pMixBuf Mixing buffer to acquire audio frames from.
207 * @param cFrames Number of requested audio frames to write.
208 * @param ppvFrames Returns a mutable pointer to the buffer's audio frame data.
209 * @param pcFramesToWrite Number of available audio frames to write.
210 *
211 * @remark This function is not thread safe!
212 */
213int AudioMixBufPeekMutable(PPDMAUDIOMIXBUF pMixBuf, uint32_t cFrames,
214 PPDMAUDIOFRAME *ppvFrames, uint32_t *pcFramesToWrite)
215{
216 AssertPtrReturn(pMixBuf, VERR_INVALID_POINTER);
217 AssertPtrReturn(ppvFrames, VERR_INVALID_POINTER);
218 AssertPtrReturn(pcFramesToWrite, VERR_INVALID_POINTER);
219
220 int rc;
221
222 if (!cFrames)
223 {
224 *pcFramesToWrite = 0;
225 return VINF_SUCCESS;
226 }
227
228 uint32_t cFramesToWrite;
229 if (pMixBuf->offWrite + cFrames > pMixBuf->cFrames)
230 {
231 cFramesToWrite = pMixBuf->cFrames - pMixBuf->offWrite;
232 rc = VINF_TRY_AGAIN;
233 }
234 else
235 {
236 cFramesToWrite = cFrames;
237 rc = VINF_SUCCESS;
238 }
239
240 *ppvFrames = &pMixBuf->pFrames[pMixBuf->offWrite];
241 AssertPtr(ppvFrames);
242
243 pMixBuf->offWrite = (pMixBuf->offWrite + cFramesToWrite) % pMixBuf->cFrames;
244 Assert(pMixBuf->offWrite <= pMixBuf->cFrames);
245 pMixBuf->cUsed += RT_MIN(cFramesToWrite, pMixBuf->cUsed);
246
247 *pcFramesToWrite = cFramesToWrite;
248
249 return rc;
250}
251
252/**
253 * Clears the entire frame buffer.
254 *
255 * @param pMixBuf Mixing buffer to clear.
256 *
257 */
258void AudioMixBufClear(PPDMAUDIOMIXBUF pMixBuf)
259{
260 AssertPtrReturnVoid(pMixBuf);
261
262 if (pMixBuf->cFrames)
263 RT_BZERO(pMixBuf->pFrames, pMixBuf->cFrames * sizeof(PDMAUDIOFRAME));
264}
265
266/**
267 * Clears (zeroes) the buffer by a certain amount of (used) frames and
268 * keeps track to eventually assigned children buffers.
269 *
270 * @param pMixBuf Mixing buffer to clear.
271 * @param cFramesToClear Number of audio frames to clear.
272 */
273void AudioMixBufFinish(PPDMAUDIOMIXBUF pMixBuf, uint32_t cFramesToClear)
274{
275 AUDMIXBUF_LOG(("cFramesToClear=%RU32\n", cFramesToClear));
276 AUDMIXBUF_LOG(("%s: offRead=%RU32, cUsed=%RU32\n",
277 pMixBuf->pszName, pMixBuf->offRead, pMixBuf->cUsed));
278
279 PPDMAUDIOMIXBUF pIter;
280 RTListForEach(&pMixBuf->lstChildren, pIter, PDMAUDIOMIXBUF, Node)
281 {
282 AUDMIXBUF_LOG(("\t%s: cMixed=%RU32 -> %RU32\n",
283 pIter->pszName, pIter->cMixed, pIter->cMixed - cFramesToClear));
284
285 pIter->cMixed -= RT_MIN(pIter->cMixed, cFramesToClear);
286 /* Note: Do not increment pIter->cUsed here, as this gets done when reading from that buffer using AudioMixBufReadXXX. */
287 }
288
289 Assert(cFramesToClear <= pMixBuf->cFrames);
290
291 uint32_t cClearOff;
292 uint32_t cClearLen;
293
294 /* Clear end of buffer (wrap around). */
295 if (cFramesToClear > pMixBuf->offRead)
296 {
297 cClearOff = pMixBuf->cFrames - (cFramesToClear - pMixBuf->offRead);
298 cClearLen = pMixBuf->cFrames - cClearOff;
299
300 AUDMIXBUF_LOG(("Clearing1: %RU32 - %RU32\n", cClearOff, cClearOff + cClearLen));
301
302 RT_BZERO(pMixBuf->pFrames + cClearOff, cClearLen * sizeof(PDMAUDIOFRAME));
303
304 Assert(cFramesToClear >= cClearLen);
305 cFramesToClear -= cClearLen;
306 }
307
308 /* Clear beginning of buffer. */
309 if ( cFramesToClear
310 && pMixBuf->offRead)
311 {
312 Assert(pMixBuf->offRead >= cFramesToClear);
313
314 cClearOff = pMixBuf->offRead - cFramesToClear;
315 cClearLen = cFramesToClear;
316
317 Assert(cClearOff + cClearLen <= pMixBuf->cFrames);
318
319 AUDMIXBUF_LOG(("Clearing2: %RU32 - %RU32\n", cClearOff, cClearOff + cClearLen));
320
321 RT_BZERO(pMixBuf->pFrames + cClearOff, cClearLen * sizeof(PDMAUDIOFRAME));
322 }
323}
324
325/**
326 * Destroys (uninitializes) a mixing buffer.
327 *
328 * @param pMixBuf Mixing buffer to destroy.
329 */
330void AudioMixBufDestroy(PPDMAUDIOMIXBUF pMixBuf)
331{
332 if (!pMixBuf)
333 return;
334
335 AudioMixBufUnlink(pMixBuf);
336
337 if (pMixBuf->pszName)
338 {
339 AUDMIXBUF_LOG(("%s\n", pMixBuf->pszName));
340
341 RTStrFree(pMixBuf->pszName);
342 pMixBuf->pszName = NULL;
343 }
344
345 if (pMixBuf->pRate)
346 {
347 RTMemFree(pMixBuf->pRate);
348 pMixBuf->pRate = NULL;
349 }
350
351 if (pMixBuf->pFrames)
352 {
353 Assert(pMixBuf->cFrames);
354
355 RTMemFree(pMixBuf->pFrames);
356 pMixBuf->pFrames = NULL;
357 }
358
359 pMixBuf->cFrames = 0;
360}
361
362/**
363 * Returns the size (in audio frames) of free audio buffer space.
364 *
365 * @return uint32_t Size (in audio frames) of free audio buffer space.
366 * @param pMixBuf Mixing buffer to return free size for.
367 */
368uint32_t AudioMixBufFree(PPDMAUDIOMIXBUF pMixBuf)
369{
370 AssertPtrReturn(pMixBuf, 0);
371
372 uint32_t cFrames, cFramesFree;
373 if (pMixBuf->pParent)
374 {
375 /*
376 * As a linked child buffer we want to know how many frames
377 * already have been consumed by the parent.
378 */
379 cFrames = pMixBuf->pParent->cFrames;
380
381 Assert(pMixBuf->cMixed <= cFrames);
382 cFramesFree = cFrames - pMixBuf->cMixed;
383 }
384 else /* As a parent. */
385 {
386 cFrames = pMixBuf->cFrames;
387 Assert(cFrames >= pMixBuf->cUsed);
388 cFramesFree = pMixBuf->cFrames - pMixBuf->cUsed;
389 }
390
391 AUDMIXBUF_LOG(("%s: %RU32 of %RU32\n", pMixBuf->pszName, cFramesFree, cFrames));
392 return cFramesFree;
393}
394
395/**
396 * Returns the size (in bytes) of free audio buffer space.
397 *
398 * @return uint32_t Size (in bytes) of free audio buffer space.
399 * @param pMixBuf Mixing buffer to return free size for.
400 */
401uint32_t AudioMixBufFreeBytes(PPDMAUDIOMIXBUF pMixBuf)
402{
403 return AUDIOMIXBUF_F2B(pMixBuf, AudioMixBufFree(pMixBuf));
404}
405
406/**
407 * Allocates the internal audio frame buffer.
408 *
409 * @return IPRT status code.
410 * @param pMixBuf Mixing buffer to allocate frame buffer for.
411 * @param cFrames Number of audio frames to allocate.
412 */
413static int audioMixBufAlloc(PPDMAUDIOMIXBUF pMixBuf, uint32_t cFrames)
414{
415 AssertPtrReturn(pMixBuf, VERR_INVALID_POINTER);
416 AssertReturn(cFrames, VERR_INVALID_PARAMETER);
417
418 AUDMIXBUF_LOG(("%s: cFrames=%RU32\n", pMixBuf->pszName, cFrames));
419
420 size_t cbFrames = cFrames * sizeof(PDMAUDIOFRAME);
421 pMixBuf->pFrames = (PPDMAUDIOFRAME)RTMemAllocZ(cbFrames);
422 if (pMixBuf->pFrames)
423 {
424 pMixBuf->cFrames = cFrames;
425 return VINF_SUCCESS;
426 }
427 return VERR_NO_MEMORY;
428}
429
430#ifdef AUDIOMIXBUF_DEBUG_MACROS
431# define AUDMIXBUF_MACRO_LOG(x) AUDMIXBUF_LOG(x)
432#elif defined(VBOX_AUDIO_TESTCASE_VERBOSE) /* Warning: VBOX_AUDIO_TESTCASE_VERBOSE will generate huge logs! */
433# define AUDMIXBUF_MACRO_LOG(x) RTPrintf x
434#else
435# define AUDMIXBUF_MACRO_LOG(x) do {} while (0)
436#endif
437
438/**
439 * Macro for generating the conversion routines from/to different formats.
440 * Be careful what to pass in/out, as most of the macros are optimized for speed and
441 * thus don't do any bounds checking!
442 *
443 * Note: Currently does not handle any endianness conversion yet!
444 */
445#define AUDMIXBUF_CONVERT(_aName, _aType, _aMin, _aMax, _aSigned, _aShift) \
446 /* Clips a specific output value to a single sample value. */ \
447 DECLCALLBACK(int64_t) audioMixBufClipFrom##_aName(_aType aVal) \
448 { \
449 /* left shifting of signed values is not defined, therefore the intermediate uint64_t cast */ \
450 if (_aSigned) \
451 return (int64_t) (((uint64_t) ((int64_t) aVal )) << (32 - _aShift)); \
452 return (int64_t) (((uint64_t) ((int64_t) aVal - ((_aMax >> 1) + 1))) << (32 - _aShift)); \
453 } \
454 \
455 /* Clips a single sample value to a specific output value. */ \
456 DECLCALLBACK(_aType) audioMixBufClipTo##_aName(int64_t iVal) \
457 { \
458 if (iVal >= 0x7fffffff) \
459 return _aMax; \
460 if (iVal < -INT64_C(0x80000000)) \
461 return _aMin; \
462 \
463 if (_aSigned) \
464 return (_aType) (iVal >> (32 - _aShift)); \
465 return ((_aType) ((iVal >> (32 - _aShift)) + ((_aMax >> 1) + 1))); \
466 } \
467 \
468 DECLCALLBACK(uint32_t) audioMixBufConvFrom##_aName##Stereo(PPDMAUDIOFRAME paDst, const void *pvSrc, uint32_t cbSrc, \
469 PCPDMAUDMIXBUFCONVOPTS pOpts) \
470 { \
471 _aType const *pSrc = (_aType const *)pvSrc; \
472 uint32_t cFrames = RT_MIN(pOpts->cFrames, cbSrc / sizeof(_aType)); \
473 AUDMIXBUF_MACRO_LOG(("cFrames=%RU32, BpS=%zu, lVol=%RU32, rVol=%RU32\n", \
474 pOpts->cFrames, sizeof(_aType), pOpts->From.Volume.uLeft, pOpts->From.Volume.uRight)); \
475 for (uint32_t i = 0; i < cFrames; i++) \
476 { \
477 paDst->i64LSample = ASMMult2xS32RetS64((int32_t)audioMixBufClipFrom##_aName(*pSrc++), pOpts->From.Volume.uLeft ) >> AUDIOMIXBUF_VOL_SHIFT; \
478 paDst->i64RSample = ASMMult2xS32RetS64((int32_t)audioMixBufClipFrom##_aName(*pSrc++), pOpts->From.Volume.uRight) >> AUDIOMIXBUF_VOL_SHIFT; \
479 paDst++; \
480 } \
481 \
482 return cFrames; \
483 } \
484 \
485 DECLCALLBACK(uint32_t) audioMixBufConvFrom##_aName##Mono(PPDMAUDIOFRAME paDst, const void *pvSrc, uint32_t cbSrc, \
486 PCPDMAUDMIXBUFCONVOPTS pOpts) \
487 { \
488 _aType const *pSrc = (_aType const *)pvSrc; \
489 const uint32_t cFrames = RT_MIN(pOpts->cFrames, cbSrc / sizeof(_aType)); \
490 AUDMIXBUF_MACRO_LOG(("cFrames=%RU32, BpS=%zu, lVol=%RU32, rVol=%RU32\n", \
491 cFrames, sizeof(_aType), pOpts->From.Volume.uLeft, pOpts->From.Volume.uRight)); \
492 for (uint32_t i = 0; i < cFrames; i++) \
493 { \
494 paDst->i64LSample = ASMMult2xS32RetS64((int32_t)audioMixBufClipFrom##_aName(*pSrc), pOpts->From.Volume.uLeft) >> AUDIOMIXBUF_VOL_SHIFT; \
495 paDst->i64RSample = ASMMult2xS32RetS64((int32_t)audioMixBufClipFrom##_aName(*pSrc), pOpts->From.Volume.uRight) >> AUDIOMIXBUF_VOL_SHIFT; \
496 pSrc++; \
497 paDst++; \
498 } \
499 \
500 return cFrames; \
501 } \
502 \
503 DECLCALLBACK(void) audioMixBufConvTo##_aName##Stereo(void *pvDst, PCPDMAUDIOFRAME paSrc, PCPDMAUDMIXBUFCONVOPTS pOpts) \
504 { \
505 PCPDMAUDIOFRAME pSrc = paSrc; \
506 _aType *pDst = (_aType *)pvDst; \
507 _aType l, r; \
508 uint32_t cFrames = pOpts->cFrames; \
509 while (cFrames--) \
510 { \
511 AUDMIXBUF_MACRO_LOG(("%p: l=%RI64, r=%RI64\n", pSrc, pSrc->i64LSample, pSrc->i64RSample)); \
512 l = audioMixBufClipTo##_aName(pSrc->i64LSample); \
513 r = audioMixBufClipTo##_aName(pSrc->i64RSample); \
514 AUDMIXBUF_MACRO_LOG(("\t-> l=%RI16, r=%RI16\n", l, r)); \
515 *pDst++ = l; \
516 *pDst++ = r; \
517 pSrc++; \
518 } \
519 } \
520 \
521 DECLCALLBACK(void) audioMixBufConvTo##_aName##Mono(void *pvDst, PCPDMAUDIOFRAME paSrc, PCPDMAUDMIXBUFCONVOPTS pOpts) \
522 { \
523 PCPDMAUDIOFRAME pSrc = paSrc; \
524 _aType *pDst = (_aType *)pvDst; \
525 uint32_t cFrames = pOpts->cFrames; \
526 while (cFrames--) \
527 { \
528 *pDst++ = audioMixBufClipTo##_aName((pSrc->i64LSample + pSrc->i64RSample) / 2); \
529 pSrc++; \
530 } \
531 }
532
533/* audioMixBufConvXXXS8: 8 bit, signed. */
534AUDMIXBUF_CONVERT(S8 /* Name */, int8_t, INT8_MIN /* Min */, INT8_MAX /* Max */, true /* fSigned */, 8 /* cShift */)
535/* audioMixBufConvXXXU8: 8 bit, unsigned. */
536AUDMIXBUF_CONVERT(U8 /* Name */, uint8_t, 0 /* Min */, UINT8_MAX /* Max */, false /* fSigned */, 8 /* cShift */)
537/* audioMixBufConvXXXS16: 16 bit, signed. */
538AUDMIXBUF_CONVERT(S16 /* Name */, int16_t, INT16_MIN /* Min */, INT16_MAX /* Max */, true /* fSigned */, 16 /* cShift */)
539/* audioMixBufConvXXXU16: 16 bit, unsigned. */
540AUDMIXBUF_CONVERT(U16 /* Name */, uint16_t, 0 /* Min */, UINT16_MAX /* Max */, false /* fSigned */, 16 /* cShift */)
541/* audioMixBufConvXXXS32: 32 bit, signed. */
542AUDMIXBUF_CONVERT(S32 /* Name */, int32_t, INT32_MIN /* Min */, INT32_MAX /* Max */, true /* fSigned */, 32 /* cShift */)
543/* audioMixBufConvXXXU32: 32 bit, unsigned. */
544AUDMIXBUF_CONVERT(U32 /* Name */, uint32_t, 0 /* Min */, UINT32_MAX /* Max */, false /* fSigned */, 32 /* cShift */)
545
546#undef AUDMIXBUF_CONVERT
547
548#define AUDMIXBUF_MIXOP(_aName, _aOp) \
549 static void audioMixBufOp##_aName(PPDMAUDIOFRAME paDst, uint32_t cDstFrames, \
550 PPDMAUDIOFRAME paSrc, uint32_t cSrcFrames, \
551 PPDMAUDIOSTREAMRATE pRate, \
552 uint32_t *pcDstWritten, uint32_t *pcSrcRead) \
553 { \
554 AUDMIXBUF_MACRO_LOG(("cSrcFrames=%RU32, cDstFrames=%RU32\n", cSrcFrames, cDstFrames)); \
555 AUDMIXBUF_MACRO_LOG(("Rate: srcOffset=%RU32, dstOffset=%RU32, dstInc=%RU32\n", \
556 pRate->srcOffset, \
557 (uint32_t)(pRate->dstOffset >> 32), (uint32_t)(pRate->dstInc >> 32))); \
558 \
559 if (pRate->dstInc == (UINT64_C(1) + UINT32_MAX)) /* No conversion needed? */ \
560 { \
561 uint32_t cFrames = RT_MIN(cSrcFrames, cDstFrames); \
562 AUDMIXBUF_MACRO_LOG(("cFrames=%RU32\n", cFrames)); \
563 for (uint32_t i = 0; i < cFrames; i++) \
564 { \
565 paDst[i].i64LSample _aOp paSrc[i].i64LSample; \
566 paDst[i].i64RSample _aOp paSrc[i].i64RSample; \
567 } \
568 \
569 if (pcDstWritten) \
570 *pcDstWritten = cFrames; \
571 if (pcSrcRead) \
572 *pcSrcRead = cFrames; \
573 return; \
574 } \
575 \
576 PPDMAUDIOFRAME paSrcStart = paSrc; \
577 PPDMAUDIOFRAME paSrcEnd = paSrc + cSrcFrames; \
578 PPDMAUDIOFRAME paDstStart = paDst; \
579 PPDMAUDIOFRAME paDstEnd = paDst + cDstFrames; \
580 PDMAUDIOFRAME frameCur = { 0 }; \
581 PDMAUDIOFRAME frameOut; \
582 PDMAUDIOFRAME frameLast = pRate->srcFrameLast; \
583 \
584 while (paDst < paDstEnd) \
585 { \
586 Assert(paSrc <= paSrcEnd); \
587 Assert(paDst <= paDstEnd); \
588 if (paSrc >= paSrcEnd) \
589 break; \
590 \
591 while (pRate->srcOffset <= (pRate->dstOffset >> 32)) \
592 { \
593 Assert(paSrc <= paSrcEnd); \
594 frameLast = *paSrc++; \
595 pRate->srcOffset++; \
596 if (paSrc == paSrcEnd) \
597 break; \
598 } \
599 \
600 Assert(paSrc <= paSrcEnd); \
601 if (paSrc == paSrcEnd) \
602 break; \
603 \
604 frameCur = *paSrc; \
605 \
606 /* Interpolate. */ \
607 int64_t iDstOffInt = pRate->dstOffset & UINT32_MAX; \
608 \
609 frameOut.i64LSample = (frameLast.i64LSample * ((int64_t) (INT64_C(1) << 32) - iDstOffInt) + frameCur.i64LSample * iDstOffInt) >> 32; \
610 frameOut.i64RSample = (frameLast.i64RSample * ((int64_t) (INT64_C(1) << 32) - iDstOffInt) + frameCur.i64RSample * iDstOffInt) >> 32; \
611 \
612 paDst->i64LSample _aOp frameOut.i64LSample; \
613 paDst->i64RSample _aOp frameOut.i64RSample; \
614 \
615 AUDMIXBUF_MACRO_LOG(("\tiDstOffInt=%RI64, l=%RI64, r=%RI64 (cur l=%RI64, r=%RI64)\n", \
616 iDstOffInt, \
617 paDst->i64LSample >> 32, paDst->i64RSample >> 32, \
618 frameCur.i64LSample >> 32, frameCur.i64RSample >> 32)); \
619 \
620 paDst++; \
621 pRate->dstOffset += pRate->dstInc; \
622 \
623 AUDMIXBUF_MACRO_LOG(("\t\tpRate->dstOffset=%RU32\n", pRate->dstOffset >> 32)); \
624 \
625 } \
626 \
627 AUDMIXBUF_MACRO_LOG(("%zu source frames -> %zu dest frames\n", paSrc - paSrcStart, paDst - paDstStart)); \
628 \
629 pRate->srcFrameLast = frameLast; \
630 \
631 AUDMIXBUF_MACRO_LOG(("pRate->srcSampleLast l=%RI64, r=%RI64\n", \
632 pRate->srcFrameLast.i64LSample, pRate->srcFrameLast.i64RSample)); \
633 \
634 if (pcDstWritten) \
635 *pcDstWritten = paDst - paDstStart; \
636 if (pcSrcRead) \
637 *pcSrcRead = paSrc - paSrcStart; \
638 }
639
640/* audioMixBufOpAssign: Assigns values from source buffer to destination bufffer, overwriting the destination. */
641AUDMIXBUF_MIXOP(Assign /* Name */, = /* Operation */)
642#if 0 /* unused */
643/* audioMixBufOpBlend: Blends together the values from both, the source and the destination buffer. */
644AUDMIXBUF_MIXOP(Blend /* Name */, += /* Operation */)
645#endif
646
647#undef AUDMIXBUF_MIXOP
648#undef AUDMIXBUF_MACRO_LOG
649
650/** Dummy conversion used when the source is muted. */
651static DECLCALLBACK(uint32_t)
652audioMixBufConvFromSilence(PPDMAUDIOFRAME paDst, const void *pvSrc, uint32_t cbSrc, PCPDMAUDMIXBUFCONVOPTS pOpts)
653{
654 RT_NOREF(cbSrc, pvSrc);
655
656 /* Internally zero always corresponds to silence. */
657 RT_BZERO(paDst, pOpts->cFrames * sizeof(paDst[0]));
658 return pOpts->cFrames;
659}
660
661/**
662 * Looks up the matching conversion (macro) routine for converting
663 * audio frames from a source format.
664 *
665 ** @todo Speed up the lookup by binding it to the actual stream state.
666 *
667 * @return PAUDMIXBUF_FN_CONVFROM Function pointer to conversion macro if found, NULL if not supported.
668 * @param enmFmt Audio format to lookup conversion macro for.
669 */
670static PFNPDMAUDIOMIXBUFCONVFROM audioMixBufConvFromLookup(PDMAUDIOMIXBUFFMT enmFmt)
671{
672 if (AUDMIXBUF_FMT_SIGNED(enmFmt))
673 {
674 if (AUDMIXBUF_FMT_CHANNELS(enmFmt) == 2)
675 {
676 switch (AUDMIXBUF_FMT_BITS_PER_SAMPLE(enmFmt))
677 {
678 case 8: return audioMixBufConvFromS8Stereo;
679 case 16: return audioMixBufConvFromS16Stereo;
680 case 32: return audioMixBufConvFromS32Stereo;
681 default: return NULL;
682 }
683 }
684 else
685 {
686 switch (AUDMIXBUF_FMT_BITS_PER_SAMPLE(enmFmt))
687 {
688 case 8: return audioMixBufConvFromS8Mono;
689 case 16: return audioMixBufConvFromS16Mono;
690 case 32: return audioMixBufConvFromS32Mono;
691 default: return NULL;
692 }
693 }
694 }
695 else /* Unsigned */
696 {
697 if (AUDMIXBUF_FMT_CHANNELS(enmFmt) == 2)
698 {
699 switch (AUDMIXBUF_FMT_BITS_PER_SAMPLE(enmFmt))
700 {
701 case 8: return audioMixBufConvFromU8Stereo;
702 case 16: return audioMixBufConvFromU16Stereo;
703 case 32: return audioMixBufConvFromU32Stereo;
704 default: return NULL;
705 }
706 }
707 else
708 {
709 switch (AUDMIXBUF_FMT_BITS_PER_SAMPLE(enmFmt))
710 {
711 case 8: return audioMixBufConvFromU8Mono;
712 case 16: return audioMixBufConvFromU16Mono;
713 case 32: return audioMixBufConvFromU32Mono;
714 default: return NULL;
715 }
716 }
717 }
718 /* not reached */
719}
720
721/**
722 * Looks up the matching conversion (macro) routine for converting
723 * audio frames to a destination format.
724 *
725 ** @todo Speed up the lookup by binding it to the actual stream state.
726 *
727 * @return PAUDMIXBUF_FN_CONVTO Function pointer to conversion macro if found, NULL if not supported.
728 * @param enmFmt Audio format to lookup conversion macro for.
729 */
730static PFNPDMAUDIOMIXBUFCONVTO audioMixBufConvToLookup(PDMAUDIOMIXBUFFMT enmFmt)
731{
732 if (AUDMIXBUF_FMT_SIGNED(enmFmt))
733 {
734 if (AUDMIXBUF_FMT_CHANNELS(enmFmt) == 2)
735 {
736 switch (AUDMIXBUF_FMT_BITS_PER_SAMPLE(enmFmt))
737 {
738 case 8: return audioMixBufConvToS8Stereo;
739 case 16: return audioMixBufConvToS16Stereo;
740 case 32: return audioMixBufConvToS32Stereo;
741 default: return NULL;
742 }
743 }
744 else
745 {
746 switch (AUDMIXBUF_FMT_BITS_PER_SAMPLE(enmFmt))
747 {
748 case 8: return audioMixBufConvToS8Mono;
749 case 16: return audioMixBufConvToS16Mono;
750 case 32: return audioMixBufConvToS32Mono;
751 default: return NULL;
752 }
753 }
754 }
755 else /* Unsigned */
756 {
757 if (AUDMIXBUF_FMT_CHANNELS(enmFmt) == 2)
758 {
759 switch (AUDMIXBUF_FMT_BITS_PER_SAMPLE(enmFmt))
760 {
761 case 8: return audioMixBufConvToU8Stereo;
762 case 16: return audioMixBufConvToU16Stereo;
763 case 32: return audioMixBufConvToU32Stereo;
764 default: return NULL;
765 }
766 }
767 else
768 {
769 switch (AUDMIXBUF_FMT_BITS_PER_SAMPLE(enmFmt))
770 {
771 case 8: return audioMixBufConvToU8Mono;
772 case 16: return audioMixBufConvToU16Mono;
773 case 32: return audioMixBufConvToU32Mono;
774 default: return NULL;
775 }
776 }
777 }
778 /* not reached */
779}
780
781/**
782 * Converts a PDM audio volume to an internal mixing buffer volume.
783 *
784 * @returns IPRT status code.
785 * @param pVolDst Where to store the converted mixing buffer volume.
786 * @param pVolSrc Volume to convert.
787 */
788static int audioMixBufConvVol(PPDMAUDMIXBUFVOL pVolDst, PPDMAUDIOVOLUME pVolSrc)
789{
790 if (!pVolSrc->fMuted) /* Only change/convert the volume value if we're not muted. */
791 {
792 uint8_t uVolL = pVolSrc->uLeft & 0xFF;
793 uint8_t uVolR = pVolSrc->uRight & 0xFF;
794
795 /** @todo Ensure that the input is in the correct range/initialized! */
796 pVolDst->uLeft = s_aVolumeConv[uVolL] * (AUDIOMIXBUF_VOL_0DB >> 16);
797 pVolDst->uRight = s_aVolumeConv[uVolR] * (AUDIOMIXBUF_VOL_0DB >> 16);
798 }
799
800 pVolDst->fMuted = pVolSrc->fMuted;
801
802 return VINF_SUCCESS;
803}
804
805/**
806 * Initializes a mixing buffer.
807 *
808 * @return IPRT status code.
809 * @param pMixBuf Mixing buffer to initialize.
810 * @param pszName Name of mixing buffer for easier identification. Optional.
811 * @param pProps PCM audio properties to use for the mixing buffer.
812 * @param cFrames Maximum number of audio frames the mixing buffer can hold.
813 */
814int AudioMixBufInit(PPDMAUDIOMIXBUF pMixBuf, const char *pszName, PPDMAUDIOPCMPROPS pProps, uint32_t cFrames)
815{
816 AssertPtrReturn(pMixBuf, VERR_INVALID_POINTER);
817 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
818 AssertPtrReturn(pProps, VERR_INVALID_POINTER);
819
820 pMixBuf->pParent = NULL;
821
822 RTListInit(&pMixBuf->lstChildren);
823 pMixBuf->cChildren = 0;
824
825 pMixBuf->pFrames = NULL;
826 pMixBuf->cFrames = 0;
827
828 pMixBuf->offRead = 0;
829 pMixBuf->offWrite = 0;
830 pMixBuf->cMixed = 0;
831 pMixBuf->cUsed = 0;
832
833 /* Set initial volume to max. */
834 pMixBuf->Volume.fMuted = false;
835 pMixBuf->Volume.uLeft = AUDIOMIXBUF_VOL_0DB;
836 pMixBuf->Volume.uRight = AUDIOMIXBUF_VOL_0DB;
837
838 /* Prevent division by zero.
839 * Do a 1:1 conversion according to AUDIOMIXBUF_S2B_RATIO. */
840 pMixBuf->iFreqRatio = 1 << 20;
841
842 pMixBuf->pRate = NULL;
843
844 pMixBuf->AudioFmt = AUDMIXBUF_AUDIO_FMT_MAKE(pProps->uHz,
845 pProps->cChannels,
846 pProps->cBits,
847 pProps->fSigned);
848
849 pMixBuf->pfnConvFrom = audioMixBufConvFromLookup(pMixBuf->AudioFmt);
850 pMixBuf->pfnConvTo = audioMixBufConvToLookup(pMixBuf->AudioFmt);
851
852 pMixBuf->cShift = pProps->cShift;
853 pMixBuf->pszName = RTStrDup(pszName);
854 if (!pMixBuf->pszName)
855 return VERR_NO_MEMORY;
856
857 AUDMIXBUF_LOG(("%s: uHz=%RU32, cChan=%RU8, cBits=%RU8, fSigned=%RTbool\n",
858 pMixBuf->pszName,
859 AUDMIXBUF_FMT_SAMPLE_FREQ(pMixBuf->AudioFmt),
860 AUDMIXBUF_FMT_CHANNELS(pMixBuf->AudioFmt),
861 AUDMIXBUF_FMT_BITS_PER_SAMPLE(pMixBuf->AudioFmt),
862 RT_BOOL(AUDMIXBUF_FMT_SIGNED(pMixBuf->AudioFmt))));
863
864 return audioMixBufAlloc(pMixBuf, cFrames);
865}
866
867/**
868 * Returns @c true if there are any audio frames available for processing,
869 * @c false if not.
870 *
871 * @return bool @c true if there are any audio frames available for processing, @c false if not.
872 * @param pMixBuf Mixing buffer to return value for.
873 */
874bool AudioMixBufIsEmpty(PPDMAUDIOMIXBUF pMixBuf)
875{
876 AssertPtrReturn(pMixBuf, true);
877
878 if (pMixBuf->pParent)
879 return (pMixBuf->cMixed == 0);
880 return (pMixBuf->cUsed == 0);
881}
882
883/**
884 * Calculates the frequency (sample rate) ratio of mixing buffer A in relation to mixing buffer B.
885 *
886 * @returns Calculated frequency ratio.
887 * @param pMixBufA First mixing buffer.
888 * @param pMixBufB Second mixing buffer.
889 */
890static int64_t audioMixBufCalcFreqRatio(PPDMAUDIOMIXBUF pMixBufA, PPDMAUDIOMIXBUF pMixBufB)
891{
892 int64_t iRatio = ((int64_t)AUDMIXBUF_FMT_SAMPLE_FREQ(pMixBufA->AudioFmt) << 32)
893 / AUDMIXBUF_FMT_SAMPLE_FREQ(pMixBufB->AudioFmt);
894
895 if (iRatio == 0) /* Catch division by zero. */
896 iRatio = 1 << 20; /* Do a 1:1 conversion instead. */
897
898 return iRatio;
899}
900
901/**
902 * Links an audio mixing buffer to a parent mixing buffer. A parent mixing
903 * buffer can have multiple children mixing buffers [1:N], whereas a child only can
904 * have one parent mixing buffer [N:1].
905 *
906 * The mixing direction always goes from the child/children buffer(s) to the
907 * parent buffer.
908 *
909 * For guest audio output the host backend owns the parent mixing buffer, the
910 * device emulation owns the child/children.
911 *
912 * The audio format of each mixing buffer can vary; the internal mixing code
913 * then will automatically do the (needed) conversion.
914 *
915 * @return IPRT status code.
916 * @param pMixBuf Mixing buffer to link parent to.
917 * @param pParent Parent mixing buffer to use for linking.
918 *
919 * @remark Circular linking is not allowed.
920 */
921int AudioMixBufLinkTo(PPDMAUDIOMIXBUF pMixBuf, PPDMAUDIOMIXBUF pParent)
922{
923 AssertPtrReturn(pMixBuf, VERR_INVALID_POINTER);
924 AssertPtrReturn(pParent, VERR_INVALID_POINTER);
925
926 AssertMsgReturn(AUDMIXBUF_FMT_SAMPLE_FREQ(pParent->AudioFmt),
927 ("Parent frame frequency (Hz) not set\n"), VERR_INVALID_PARAMETER);
928 AssertMsgReturn(AUDMIXBUF_FMT_SAMPLE_FREQ(pMixBuf->AudioFmt),
929 ("Buffer sample frequency (Hz) not set\n"), VERR_INVALID_PARAMETER);
930 AssertMsgReturn(pMixBuf != pParent,
931 ("Circular linking not allowed\n"), VERR_INVALID_PARAMETER);
932
933 if (pMixBuf->pParent) /* Already linked? */
934 {
935 AUDMIXBUF_LOG(("%s: Already linked to parent '%s'\n",
936 pMixBuf->pszName, pMixBuf->pParent->pszName));
937 return VERR_ACCESS_DENIED;
938 }
939
940 RTListAppend(&pParent->lstChildren, &pMixBuf->Node);
941 pParent->cChildren++;
942
943 /* Set the parent. */
944 pMixBuf->pParent = pParent;
945
946 /* Calculate the frequency ratios. */
947 pMixBuf->iFreqRatio = audioMixBufCalcFreqRatio(pParent, pMixBuf);
948
949 int rc = VINF_SUCCESS;
950#if 0
951 uint32_t cFrames = (uint32_t)RT_MIN( ((uint64_t)pParent->cFrames << 32)
952 / pMixBuf->iFreqRatio, _64K /* 64K frames max. */);
953 if (!cFrames)
954 cFrames = pParent->cFrames;
955
956 int rc = VINF_SUCCESS;
957
958 if (cFrames != pMixBuf->cFrames)
959 {
960 AUDMIXBUF_LOG(("%s: Reallocating frames %RU32 -> %RU32\n",
961 pMixBuf->pszName, pMixBuf->cFrames, cFrames));
962
963 uint32_t cbSamples = cFrames * sizeof(PDMAUDIOSAMPLE);
964 Assert(cbSamples);
965 pMixBuf->pSamples = (PPDMAUDIOSAMPLE)RTMemRealloc(pMixBuf->pSamples, cbSamples);
966 if (!pMixBuf->pSamples)
967 rc = VERR_NO_MEMORY;
968
969 if (RT_SUCCESS(rc))
970 {
971 pMixBuf->cFrames = cFrames;
972
973 /* Make sure to zero the reallocated buffer so that it can be
974 * used properly when blending with another buffer later. */
975 RT_BZERO(pMixBuf->pSamples, cbSamples);
976 }
977 }
978#endif
979
980 if (RT_SUCCESS(rc))
981 {
982 if (!pMixBuf->pRate)
983 {
984 /* Create rate conversion. */
985 pMixBuf->pRate = (PPDMAUDIOSTREAMRATE)RTMemAllocZ(sizeof(PDMAUDIOSTREAMRATE));
986 if (!pMixBuf->pRate)
987 return VERR_NO_MEMORY;
988 }
989 else
990 RT_BZERO(pMixBuf->pRate, sizeof(PDMAUDIOSTREAMRATE));
991
992 pMixBuf->pRate->dstInc = ((uint64_t)AUDMIXBUF_FMT_SAMPLE_FREQ(pMixBuf->AudioFmt) << 32)
993 / AUDMIXBUF_FMT_SAMPLE_FREQ(pParent->AudioFmt);
994
995 AUDMIXBUF_LOG(("uThisHz=%RU32, uParentHz=%RU32, iFreqRatio=0x%RX64 (%RI64), uRateInc=0x%RX64 (%RU64), cFrames=%RU32 (%RU32 parent)\n",
996 AUDMIXBUF_FMT_SAMPLE_FREQ(pMixBuf->AudioFmt),
997 AUDMIXBUF_FMT_SAMPLE_FREQ(pParent->AudioFmt),
998 pMixBuf->iFreqRatio, pMixBuf->iFreqRatio,
999 pMixBuf->pRate->dstInc, pMixBuf->pRate->dstInc,
1000 pMixBuf->cFrames,
1001 pParent->cFrames));
1002 AUDMIXBUF_LOG(("%s (%RU32Hz) -> %s (%RU32Hz)\n",
1003 pMixBuf->pszName, AUDMIXBUF_FMT_SAMPLE_FREQ(pMixBuf->AudioFmt),
1004 pMixBuf->pParent->pszName, AUDMIXBUF_FMT_SAMPLE_FREQ(pParent->AudioFmt)));
1005 }
1006
1007 return rc;
1008}
1009
1010/**
1011 * Returns number of available live frames, that is, frames that
1012 * have been written into the mixing buffer but not have been processed yet.
1013 *
1014 * For a parent buffer, this simply returns the currently used number of frames
1015 * in the buffer.
1016 *
1017 * For a child buffer, this returns the number of frames which have been mixed
1018 * to the parent and were not processed by the parent yet.
1019 *
1020 * @return uint32_t Number of live frames available.
1021 * @param pMixBuf Mixing buffer to return value for.
1022 */
1023uint32_t AudioMixBufLive(PPDMAUDIOMIXBUF pMixBuf)
1024{
1025 AssertPtrReturn(pMixBuf, 0);
1026
1027#ifdef RT_STRICT
1028 uint32_t cFrames;
1029#endif
1030 uint32_t cAvail;
1031 if (pMixBuf->pParent) /* Is this a child buffer? */
1032 {
1033#ifdef RT_STRICT
1034 /* Use the frame count from the parent, as
1035 * pMixBuf->cMixed specifies the frame count
1036 * in parent frames. */
1037 cFrames = pMixBuf->pParent->cFrames;
1038#endif
1039 cAvail = pMixBuf->cMixed;
1040 }
1041 else
1042 {
1043#ifdef RT_STRICT
1044 cFrames = pMixBuf->cFrames;
1045#endif
1046 cAvail = pMixBuf->cUsed;
1047 }
1048
1049 Assert(cAvail <= cFrames);
1050 return cAvail;
1051}
1052
1053/**
1054 * Mixes audio frames from a source mixing buffer to a destination mixing buffer.
1055 *
1056 * @return IPRT status code.
1057 * VERR_BUFFER_UNDERFLOW if the source did not have enough audio data.
1058 * VERR_BUFFER_OVERFLOW if the destination did not have enough space to store the converted source audio data.
1059 *
1060 * @param pDst Destination mixing buffer.
1061 * @param pSrc Source mixing buffer.
1062 * @param cSrcOff Offset of source audio frames to mix.
1063 * @param cSrcFrames Number of source audio frames to mix.
1064 * @param pcSrcMixed Number of source audio frames successfully mixed. Optional.
1065 */
1066static int audioMixBufMixTo(PPDMAUDIOMIXBUF pDst, PPDMAUDIOMIXBUF pSrc, uint32_t cSrcOff, uint32_t cSrcFrames,
1067 uint32_t *pcSrcMixed)
1068{
1069 AssertPtrReturn(pDst, VERR_INVALID_POINTER);
1070 AssertPtrReturn(pSrc, VERR_INVALID_POINTER);
1071 /* pcSrcMixed is optional. */
1072
1073 AssertMsgReturn(pDst == pSrc->pParent, ("Source buffer '%s' is not a child of destination '%s'\n",
1074 pSrc->pszName, pDst->pszName), VERR_INVALID_PARAMETER);
1075 uint32_t cReadTotal = 0;
1076 uint32_t cWrittenTotal = 0;
1077
1078 Assert(pSrc->cMixed <= pDst->cFrames);
1079
1080 Assert(pSrc->cUsed >= pDst->cMixed);
1081 Assert(pDst->cUsed <= pDst->cFrames);
1082
1083 uint32_t offSrcRead = cSrcOff;
1084
1085 uint32_t offDstWrite = pDst->offWrite;
1086 uint32_t cDstMixed = pSrc->cMixed;
1087
1088 uint32_t cSrcAvail = RT_MIN(cSrcFrames, pSrc->cUsed);
1089 uint32_t cDstAvail = pDst->cFrames - pDst->cUsed; /** @todo Use pDst->cMixed later? */
1090
1091 AUDMIXBUF_LOG(("%s (%RU32 available) -> %s (%RU32 available)\n",
1092 pSrc->pszName, cSrcAvail, pDst->pszName, cDstAvail));
1093#ifdef DEBUG
1094 audioMixBufDbgPrintInternal(pDst, __FUNCTION__);
1095#endif
1096
1097 if (!cSrcAvail)
1098 return VERR_BUFFER_UNDERFLOW;
1099
1100 if (!cDstAvail)
1101 return VERR_BUFFER_OVERFLOW;
1102
1103 uint32_t cSrcToRead = 0;
1104 uint32_t cSrcRead;
1105
1106 uint32_t cDstToWrite;
1107 uint32_t cDstWritten;
1108
1109 int rc = VINF_SUCCESS;
1110
1111 while (cSrcAvail && cDstAvail)
1112 {
1113 cSrcToRead = RT_MIN(cSrcAvail, pSrc->cFrames - offSrcRead);
1114 cDstToWrite = RT_MIN(cDstAvail, pDst->cFrames - offDstWrite);
1115
1116 AUDMIXBUF_LOG(("\tSource: %RU32 @ %RU32 -> reading %RU32\n", cSrcAvail, offSrcRead, cSrcToRead));
1117 AUDMIXBUF_LOG(("\tDest : %RU32 @ %RU32 -> writing %RU32\n", cDstAvail, offDstWrite, cDstToWrite));
1118
1119 if ( !cDstToWrite
1120 || !cSrcToRead)
1121 {
1122 break;
1123 }
1124
1125 cDstWritten = cSrcRead = 0;
1126
1127 Assert(offSrcRead < pSrc->cFrames);
1128 Assert(offSrcRead + cSrcToRead <= pSrc->cFrames);
1129
1130 Assert(offDstWrite < pDst->cFrames);
1131 Assert(offDstWrite + cDstToWrite <= pDst->cFrames);
1132
1133 audioMixBufOpAssign(pDst->pFrames + offDstWrite, cDstToWrite,
1134 pSrc->pFrames + offSrcRead, cSrcToRead,
1135 pSrc->pRate, &cDstWritten, &cSrcRead);
1136
1137 cReadTotal += cSrcRead;
1138 cWrittenTotal += cDstWritten;
1139
1140 offSrcRead = (offSrcRead + cSrcRead) % pSrc->cFrames;
1141 offDstWrite = (offDstWrite + cDstWritten) % pDst->cFrames;
1142
1143 cDstMixed += cDstWritten;
1144
1145 Assert(cSrcAvail >= cSrcRead);
1146 cSrcAvail -= cSrcRead;
1147
1148 Assert(cDstAvail >= cDstWritten);
1149 cDstAvail -= cDstWritten;
1150
1151 AUDMIXBUF_LOG(("\t%RU32 read (%RU32 left @ %RU32), %RU32 written (%RU32 left @ %RU32)\n",
1152 cSrcRead, cSrcAvail, offSrcRead,
1153 cDstWritten, cDstAvail, offDstWrite));
1154 }
1155
1156 pSrc->offRead = offSrcRead;
1157 Assert(pSrc->cUsed >= cReadTotal);
1158 pSrc->cUsed -= RT_MIN(pSrc->cUsed, cReadTotal);
1159
1160 /* Note: Always count in parent frames, as the rate can differ! */
1161 pSrc->cMixed = RT_MIN(cDstMixed, pDst->cFrames);
1162
1163 pDst->offWrite = offDstWrite;
1164 Assert(pDst->offWrite <= pDst->cFrames);
1165 Assert((pDst->cUsed + cWrittenTotal) <= pDst->cFrames);
1166 pDst->cUsed += cWrittenTotal;
1167
1168 /* If there are more used frames than fitting in the destination buffer,
1169 * adjust the values accordingly.
1170 *
1171 * This can happen if this routine has been called too often without
1172 * actually processing the destination buffer in between. */
1173 if (pDst->cUsed > pDst->cFrames)
1174 {
1175 LogFunc(("%s: Warning: Destination buffer used %RU32 / %RU32 frames\n", pDst->pszName, pDst->cUsed, pDst->cFrames));
1176 pDst->offWrite = 0;
1177 pDst->cUsed = pDst->cFrames;
1178
1179 rc = VERR_BUFFER_OVERFLOW;
1180 }
1181
1182#ifdef DEBUG
1183 audioMixBufDbgValidate(pSrc);
1184 audioMixBufDbgValidate(pDst);
1185
1186 Assert(pSrc->cMixed <= pDst->cFrames);
1187#endif
1188
1189#ifdef AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA
1190 uint32_t offRead = pDst->offRead;
1191
1192 uint32_t cLeft = cWrittenTotal;
1193 while (cLeft)
1194 {
1195 uint8_t auBuf[256];
1196 RT_ZERO(auBuf);
1197
1198 Assert(sizeof(auBuf) >= 4);
1199 Assert(sizeof(auBuf) % 4 == 0);
1200
1201 uint32_t cToRead = RT_MIN(AUDIOMIXBUF_B2F(pDst, sizeof(auBuf)), RT_MIN(cLeft, pDst->cFrames - offRead));
1202 Assert(cToRead <= pDst->cUsed);
1203
1204 PDMAUDMIXBUFCONVOPTS convOpts;
1205 RT_ZERO(convOpts);
1206 convOpts.cFrames = cToRead;
1207
1208 pDst->pfnConvTo(auBuf, pDst->pFrames + offRead, &convOpts);
1209
1210 RTFILE fh;
1211 int rc2 = RTFileOpen(&fh, AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA_PATH "mixbuf_mixto.pcm",
1212 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
1213 if (RT_SUCCESS(rc2))
1214 {
1215 RTFileWrite(fh, auBuf, AUDIOMIXBUF_F2B(pDst, cToRead), NULL);
1216 RTFileClose(fh);
1217 }
1218
1219 offRead = (offRead + cToRead) % pDst->cFrames;
1220 cLeft -= cToRead;
1221 }
1222#endif /* AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA */
1223
1224#ifdef DEBUG
1225 audioMixBufDbgPrintInternal(pDst, __FUNCTION__);
1226#endif
1227
1228 if (pcSrcMixed)
1229 *pcSrcMixed = cReadTotal;
1230
1231 AUDMIXBUF_LOG(("cReadTotal=%RU32, cWrittenTotal=%RU32, cSrcMixed=%RU32, cDstUsed=%RU32, rc=%Rrc\n",
1232 cReadTotal, cWrittenTotal, pSrc->cMixed, pDst->cUsed, rc));
1233 return rc;
1234}
1235
1236/**
1237 * Mixes audio frames down to the parent mixing buffer, extended version.
1238 *
1239 * @return IPRT status code. See audioMixBufMixTo() for a more detailed explanation.
1240 * @param pMixBuf Source mixing buffer to mix to its parent.
1241 * @param cSrcOffset Offset (in frames) of source mixing buffer.
1242 * @param cSrcFrames Number of source audio frames to mix to its parent.
1243 * @param pcSrcMixed Number of source audio frames successfully mixed. Optional.
1244 */
1245int AudioMixBufMixToParentEx(PPDMAUDIOMIXBUF pMixBuf, uint32_t cSrcOffset, uint32_t cSrcFrames, uint32_t *pcSrcMixed)
1246{
1247 AssertMsgReturn(VALID_PTR(pMixBuf->pParent),
1248 ("Buffer is not linked to a parent buffer\n"),
1249 VERR_INVALID_PARAMETER);
1250
1251 return audioMixBufMixTo(pMixBuf->pParent, pMixBuf, cSrcOffset, cSrcFrames, pcSrcMixed);
1252}
1253
1254/**
1255 * Mixes audio frames down to the parent mixing buffer.
1256 *
1257 * @return IPRT status code. See audioMixBufMixTo() for a more detailed explanation.
1258 * @param pMixBuf Source mixing buffer to mix to its parent.
1259 * @param cSrcFrames Number of source audio frames to mix to its parent.
1260 * @param pcSrcMixed Number of source audio frames successfully mixed. Optional.
1261 */
1262int AudioMixBufMixToParent(PPDMAUDIOMIXBUF pMixBuf, uint32_t cSrcFrames, uint32_t *pcSrcMixed)
1263{
1264 return audioMixBufMixTo(pMixBuf->pParent, pMixBuf, pMixBuf->offRead, cSrcFrames, pcSrcMixed);
1265}
1266
1267#ifdef DEBUG
1268/**
1269 * Prints a single mixing buffer.
1270 * Internal helper function for debugging. Do not use directly.
1271 *
1272 * @return IPRT status code.
1273 * @param pMixBuf Mixing buffer to print.
1274 * @param pszFunc Function name to log this for.
1275 * @param fIsParent Whether this is a parent buffer or not.
1276 * @param uIdtLvl Indention level to use.
1277 */
1278DECL_FORCE_INLINE(void) audioMixBufDbgPrintSingle(PPDMAUDIOMIXBUF pMixBuf, const char *pszFunc, bool fIsParent, uint16_t uIdtLvl)
1279{
1280 Log(("%s: %*s[%s] %s: offRead=%RU32, offWrite=%RU32, cMixed=%RU32 -> %RU32/%RU32\n",
1281 pszFunc, uIdtLvl * 4, "", fIsParent ? "PARENT" : "CHILD",
1282 pMixBuf->pszName, pMixBuf->offRead, pMixBuf->offWrite, pMixBuf->cMixed, pMixBuf->cUsed, pMixBuf->cFrames));
1283}
1284
1285/**
1286 * Validates a single mixing buffer.
1287 *
1288 * @return @true if the buffer state is valid or @false if not.
1289 * @param pMixBuf Mixing buffer to validate.
1290 */
1291DECL_FORCE_INLINE(bool) audioMixBufDbgValidate(PPDMAUDIOMIXBUF pMixBuf)
1292{
1293 //const uint32_t offReadEnd = (pMixBuf->offRead + pMixBuf->cUsed) % pMixBuf->cFrames;
1294 //const uint32_t offWriteEnd = (pMixBuf->offWrite + (pMixBuf->cFrames - pMixBuf->cUsed)) % pMixBuf->cFrames;
1295
1296 bool fValid = true;
1297
1298 AssertStmt(pMixBuf->offRead <= pMixBuf->cFrames, fValid = false);
1299 AssertStmt(pMixBuf->offWrite <= pMixBuf->cFrames, fValid = false);
1300 AssertStmt(pMixBuf->cUsed <= pMixBuf->cFrames, fValid = false);
1301
1302 if (pMixBuf->offWrite > pMixBuf->offRead)
1303 {
1304 if (pMixBuf->offWrite - pMixBuf->offRead != pMixBuf->cUsed)
1305 fValid = false;
1306 }
1307 else if (pMixBuf->offWrite < pMixBuf->offRead)
1308 {
1309 if (pMixBuf->offWrite + pMixBuf->cFrames - pMixBuf->offRead != pMixBuf->cUsed)
1310 fValid = false;
1311 }
1312
1313 if (!fValid)
1314 {
1315 audioMixBufDbgPrintInternal(pMixBuf, __FUNCTION__);
1316 AssertFailed();
1317 }
1318
1319 return fValid;
1320}
1321
1322/**
1323 * Internal helper function for audioMixBufPrintChain().
1324 * Do not use directly.
1325 *
1326 * @return IPRT status code.
1327 * @param pMixBuf Mixing buffer to print.
1328 * @param pszFunc Function name to print the chain for.
1329 * @param uIdtLvl Indention level to use.
1330 * @param pcChildren Pointer to children counter.
1331 */
1332DECL_FORCE_INLINE(void) audioMixBufDbgPrintChainHelper(PPDMAUDIOMIXBUF pMixBuf, const char *pszFunc, uint16_t uIdtLvl,
1333 size_t *pcChildren)
1334{
1335 PPDMAUDIOMIXBUF pIter;
1336 RTListForEach(&pMixBuf->lstChildren, pIter, PDMAUDIOMIXBUF, Node)
1337 {
1338 audioMixBufDbgPrintSingle(pIter, pszFunc, false /* ifIsParent */, uIdtLvl + 1);
1339 *pcChildren++;
1340 }
1341}
1342
1343DECL_FORCE_INLINE(void) audioMixBufDbgPrintChainInternal(PPDMAUDIOMIXBUF pMixBuf, const char *pszFunc)
1344{
1345 PPDMAUDIOMIXBUF pParent = pMixBuf->pParent;
1346 while (pParent)
1347 {
1348 if (!pParent->pParent)
1349 break;
1350
1351 pParent = pParent->pParent;
1352 }
1353
1354 if (!pParent)
1355 pParent = pMixBuf;
1356
1357 audioMixBufDbgPrintSingle(pParent, pszFunc, true /* fIsParent */, 0 /* uIdtLvl */);
1358
1359 /* Recursively iterate children. */
1360 size_t cChildren = 0;
1361 audioMixBufDbgPrintChainHelper(pParent, pszFunc, 0 /* uIdtLvl */, &cChildren);
1362
1363 Log(("%s: Children: %zu\n", pszFunc, cChildren));
1364}
1365
1366/**
1367 * Prints statistics and status of the full chain of a mixing buffer to the logger,
1368 * starting from the top root mixing buffer.
1369 * For debug versions only.
1370 *
1371 * @return IPRT status code.
1372 * @param pMixBuf Mixing buffer to print.
1373 */
1374void AudioMixBufDbgPrintChain(PPDMAUDIOMIXBUF pMixBuf)
1375{
1376 audioMixBufDbgPrintChainInternal(pMixBuf, __FUNCTION__);
1377}
1378
1379DECL_FORCE_INLINE(void) audioMixBufDbgPrintInternal(PPDMAUDIOMIXBUF pMixBuf, const char *pszFunc)
1380{
1381 PPDMAUDIOMIXBUF pParent = pMixBuf;
1382 if (pMixBuf->pParent)
1383 pParent = pMixBuf->pParent;
1384
1385 audioMixBufDbgPrintSingle(pMixBuf, pszFunc, pParent == pMixBuf /* fIsParent */, 0 /* iIdtLevel */);
1386
1387 PPDMAUDIOMIXBUF pIter;
1388 RTListForEach(&pMixBuf->lstChildren, pIter, PDMAUDIOMIXBUF, Node)
1389 {
1390 if (pIter == pMixBuf)
1391 continue;
1392 audioMixBufDbgPrintSingle(pIter, pszFunc, false /* fIsParent */, 1 /* iIdtLevel */);
1393 }
1394}
1395
1396/**
1397 * Prints statistics and status of a mixing buffer to the logger.
1398 * For debug versions only.
1399 *
1400 * @return IPRT status code.
1401 * @param pMixBuf Mixing buffer to print.
1402 */
1403void AudioMixBufDbgPrint(PPDMAUDIOMIXBUF pMixBuf)
1404{
1405 audioMixBufDbgPrintInternal(pMixBuf, __FUNCTION__);
1406}
1407#endif /* DEBUG */
1408
1409/**
1410 * Returns the total number of frames used.
1411 *
1412 * @return uint32_t
1413 * @param pMixBuf
1414 */
1415uint32_t AudioMixBufUsed(PPDMAUDIOMIXBUF pMixBuf)
1416{
1417 AssertPtrReturn(pMixBuf, 0);
1418 return pMixBuf->cUsed;
1419}
1420
1421/**
1422 * Reads audio frames at a specific offset.
1423 *
1424 * @return IPRT status code.
1425 * @param pMixBuf Mixing buffer to read audio frames from.
1426 * @param offFrames Offset (in audio frames) to start reading from.
1427 * @param pvBuf Pointer to buffer to write output to.
1428 * @param cbBuf Size (in bytes) of buffer to write to.
1429 * @param pcbRead Size (in bytes) of data read. Optional.
1430 */
1431int AudioMixBufReadAt(PPDMAUDIOMIXBUF pMixBuf,
1432 uint32_t offFrames,
1433 void *pvBuf, uint32_t cbBuf,
1434 uint32_t *pcbRead)
1435{
1436 return AudioMixBufReadAtEx(pMixBuf, pMixBuf->AudioFmt,
1437 offFrames, pvBuf, cbBuf, pcbRead);
1438}
1439
1440/**
1441 * Reads audio frames at a specific offset.
1442 * If the audio format of the mixing buffer and the requested audio format do
1443 * not match the output will be converted accordingly.
1444 *
1445 * @return IPRT status code.
1446 * @param pMixBuf Mixing buffer to read audio frames from.
1447 * @param enmFmt Audio format to use for output.
1448 * @param offFrames Offset (in audio frames) to start reading from.
1449 * @param pvBuf Pointer to buffer to write output to.
1450 * @param cbBuf Size (in bytes) of buffer to write to.
1451 * @param pcbRead Size (in bytes) of data read. Optional.
1452 */
1453int AudioMixBufReadAtEx(PPDMAUDIOMIXBUF pMixBuf, PDMAUDIOMIXBUFFMT enmFmt,
1454 uint32_t offFrames,
1455 void *pvBuf, uint32_t cbBuf,
1456 uint32_t *pcbRead)
1457{
1458 AssertPtrReturn(pMixBuf, VERR_INVALID_POINTER);
1459 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1460 /* pcbRead is optional. */
1461
1462 uint32_t cDstFrames = pMixBuf->cFrames;
1463 uint32_t cLive = pMixBuf->cUsed;
1464
1465 uint32_t cDead = cDstFrames - cLive;
1466 uint32_t cToProcess = (uint32_t)AUDIOMIXBUF_F2F_RATIO(pMixBuf, cDead);
1467 cToProcess = RT_MIN(cToProcess, AUDIOMIXBUF_B2F(pMixBuf, cbBuf));
1468
1469 AUDMIXBUF_LOG(("%s: offFrames=%RU32, cLive=%RU32, cDead=%RU32, cToProcess=%RU32\n",
1470 pMixBuf->pszName, offFrames, cLive, cDead, cToProcess));
1471
1472 int rc;
1473 if (cToProcess)
1474 {
1475 PFNPDMAUDIOMIXBUFCONVTO pfnConvTo = NULL;
1476 if (pMixBuf->AudioFmt != enmFmt)
1477 pfnConvTo = audioMixBufConvToLookup(enmFmt);
1478 else
1479 pfnConvTo = pMixBuf->pfnConvTo;
1480
1481 if (pfnConvTo)
1482 {
1483 PDMAUDMIXBUFCONVOPTS convOpts;
1484 RT_ZERO(convOpts);
1485 /* Note: No volume handling/conversion done in the conversion-to macros (yet). */
1486
1487 convOpts.cFrames = cToProcess;
1488
1489 pfnConvTo(pvBuf, pMixBuf->pFrames + offFrames, &convOpts);
1490
1491#ifdef DEBUG
1492 AudioMixBufDbgPrint(pMixBuf);
1493#endif
1494 rc = VINF_SUCCESS;
1495 }
1496 else
1497 {
1498 AssertFailed();
1499 rc = VERR_NOT_SUPPORTED;
1500 }
1501 }
1502 else
1503 rc = VINF_SUCCESS;
1504
1505 if (RT_SUCCESS(rc))
1506 {
1507 if (pcbRead)
1508 *pcbRead = AUDIOMIXBUF_F2B(pMixBuf, cToProcess);
1509 }
1510
1511 AUDMIXBUF_LOG(("cbRead=%RU32, rc=%Rrc\n", AUDIOMIXBUF_F2B(pMixBuf, cToProcess), rc));
1512 return rc;
1513}
1514
1515/**
1516 * Reads audio frames. The audio format of the mixing buffer will be used.
1517 *
1518 * @return IPRT status code.
1519 * @param pMixBuf Mixing buffer to read audio frames from.
1520 * @param pvBuf Pointer to buffer to write output to.
1521 * @param cbBuf Size (in bytes) of buffer to write to.
1522 * @param pcRead Number of audio frames read. Optional.
1523 */
1524int AudioMixBufReadCirc(PPDMAUDIOMIXBUF pMixBuf, void *pvBuf, uint32_t cbBuf, uint32_t *pcRead)
1525{
1526 return AudioMixBufReadCircEx(pMixBuf, pMixBuf->AudioFmt, pvBuf, cbBuf, pcRead);
1527}
1528
1529/**
1530 * Reads audio frames in a specific audio format.
1531 * If the audio format of the mixing buffer and the requested audio format do
1532 * not match the output will be converted accordingly.
1533 *
1534 * @return IPRT status code.
1535 * @param pMixBuf Mixing buffer to read audio frames from.
1536 * @param enmFmt Audio format to use for output.
1537 * @param pvBuf Pointer to buffer to write output to.
1538 * @param cbBuf Size (in bytes) of buffer to write to.
1539 * @param pcRead Number of audio frames read. Optional.
1540 */
1541int AudioMixBufReadCircEx(PPDMAUDIOMIXBUF pMixBuf, PDMAUDIOMIXBUFFMT enmFmt, void *pvBuf, uint32_t cbBuf, uint32_t *pcRead)
1542{
1543 AssertPtrReturn(pMixBuf, VERR_INVALID_POINTER);
1544 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1545 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1546 /* pcRead is optional. */
1547
1548 /* Make sure that we at least have space for a full audio frame. */
1549 AssertReturn(AUDIOMIXBUF_B2F(pMixBuf, cbBuf), VERR_INVALID_PARAMETER);
1550
1551 uint32_t cToRead = RT_MIN(pMixBuf->cUsed, AUDIOMIXBUF_B2F(pMixBuf, cbBuf));
1552
1553 AUDMIXBUF_LOG(("%s: cbBuf=%RU32 (%RU32 frames), cToRead=%RU32, fmtSrc=0x%x, fmtDst=0x%x\n",
1554 pMixBuf->pszName, cbBuf, AUDIOMIXBUF_B2F(pMixBuf, cbBuf), cToRead, pMixBuf->AudioFmt, enmFmt));
1555
1556 if (!cToRead)
1557 {
1558#ifdef DEBUG
1559 audioMixBufDbgPrintInternal(pMixBuf, __FUNCTION__);
1560#endif
1561 if (pcRead)
1562 *pcRead = 0;
1563 return VINF_SUCCESS;
1564 }
1565
1566 PFNPDMAUDIOMIXBUFCONVTO pfnConvTo = NULL;
1567 if (pMixBuf->AudioFmt != enmFmt)
1568 pfnConvTo = audioMixBufConvToLookup(enmFmt);
1569 else
1570 pfnConvTo = pMixBuf->pfnConvTo;
1571
1572 if (!pfnConvTo) /* Audio format not supported. */
1573 {
1574 AssertFailed();
1575 return VERR_NOT_SUPPORTED;
1576 }
1577
1578 cToRead = RT_MIN(cToRead, pMixBuf->cFrames - pMixBuf->offRead);
1579 if (cToRead)
1580 {
1581 PDMAUDMIXBUFCONVOPTS convOpts;
1582 RT_ZERO(convOpts);
1583 convOpts.cFrames = cToRead;
1584
1585 AUDMIXBUF_LOG(("cToRead=%RU32\n", cToRead));
1586
1587 pfnConvTo(pvBuf, pMixBuf->pFrames + pMixBuf->offRead, &convOpts);
1588
1589#ifdef AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA
1590 RTFILE fh;
1591 int rc2 = RTFileOpen(&fh, AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA_PATH "mixbuf_readcirc.pcm",
1592 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
1593 if (RT_SUCCESS(rc2))
1594 {
1595 RTFileWrite(fh, pvBuf, AUDIOMIXBUF_F2B(pMixBuf, cToRead), NULL);
1596 RTFileClose(fh);
1597 }
1598#endif
1599 pMixBuf->offRead = (pMixBuf->offRead + cToRead) % pMixBuf->cFrames;
1600 Assert(pMixBuf->cUsed >= cToRead);
1601 pMixBuf->cUsed -= cToRead;
1602 }
1603
1604 if (pcRead)
1605 *pcRead = cToRead;
1606
1607#ifdef DEBUG
1608 audioMixBufDbgValidate(pMixBuf);
1609#endif
1610
1611 AUDMIXBUF_LOG(("cRead=%RU32 (%RU32 bytes)\n", cToRead, AUDIOMIXBUF_F2B(pMixBuf, cToRead)));
1612 return VINF_SUCCESS;
1613}
1614
1615/**
1616 * Returns the current read position of a mixing buffer.
1617 *
1618 * @returns IPRT status code.
1619 * @param pMixBuf Mixing buffer to return position for.
1620 */
1621uint32_t AudioMixBufReadPos(PPDMAUDIOMIXBUF pMixBuf)
1622{
1623 AssertPtrReturn(pMixBuf, 0);
1624
1625 return pMixBuf->offRead;
1626}
1627
1628/**
1629 * Resets a mixing buffer.
1630 *
1631 * @param pMixBuf Mixing buffer to reset.
1632 */
1633void AudioMixBufReset(PPDMAUDIOMIXBUF pMixBuf)
1634{
1635 AssertPtrReturnVoid(pMixBuf);
1636
1637 AUDMIXBUF_LOG(("%s\n", pMixBuf->pszName));
1638
1639 pMixBuf->offRead = 0;
1640 pMixBuf->offWrite = 0;
1641 pMixBuf->cMixed = 0;
1642 pMixBuf->cUsed = 0;
1643
1644 AudioMixBufClear(pMixBuf);
1645}
1646
1647/**
1648 * Sets the overall (master) volume.
1649 *
1650 * @param pMixBuf Mixing buffer to set volume for.
1651 * @param pVol Pointer to volume structure to set.
1652 */
1653void AudioMixBufSetVolume(PPDMAUDIOMIXBUF pMixBuf, PPDMAUDIOVOLUME pVol)
1654{
1655 AssertPtrReturnVoid(pMixBuf);
1656 AssertPtrReturnVoid(pVol);
1657
1658 LogFlowFunc(("%s: lVol=%RU8, rVol=%RU8, fMuted=%RTbool\n", pMixBuf->pszName, pVol->uLeft, pVol->uRight, pVol->fMuted));
1659
1660 int rc2 = audioMixBufConvVol(&pMixBuf->Volume /* Dest */, pVol /* Source */);
1661 AssertRC(rc2);
1662}
1663
1664/**
1665 * Returns the maximum amount of audio frames this buffer can hold.
1666 *
1667 * @return uint32_t Size (in audio frames) the mixing buffer can hold.
1668 * @param pMixBuf Mixing buffer to retrieve maximum for.
1669 */
1670uint32_t AudioMixBufSize(PPDMAUDIOMIXBUF pMixBuf)
1671{
1672 AssertPtrReturn(pMixBuf, 0);
1673 return pMixBuf->cFrames;
1674}
1675
1676/**
1677 * Returns the maximum amount of bytes this buffer can hold.
1678 *
1679 * @return uint32_t Size (in bytes) the mixing buffer can hold.
1680 * @param pMixBuf Mixing buffer to retrieve maximum for.
1681 */
1682uint32_t AudioMixBufSizeBytes(PPDMAUDIOMIXBUF pMixBuf)
1683{
1684 AssertPtrReturn(pMixBuf, 0);
1685 return AUDIOMIXBUF_F2B(pMixBuf, pMixBuf->cFrames);
1686}
1687
1688/**
1689 * Unlinks a mixing buffer from its parent, if any.
1690 *
1691 * @return IPRT status code.
1692 * @param pMixBuf Mixing buffer to unlink from parent.
1693 */
1694void AudioMixBufUnlink(PPDMAUDIOMIXBUF pMixBuf)
1695{
1696 if (!pMixBuf || !pMixBuf->pszName)
1697 return;
1698
1699 AUDMIXBUF_LOG(("%s\n", pMixBuf->pszName));
1700
1701 if (pMixBuf->pParent) /* IS this a children buffer? */
1702 {
1703 AUDMIXBUF_LOG(("%s: Unlinking from parent \"%s\"\n",
1704 pMixBuf->pszName, pMixBuf->pParent->pszName));
1705
1706 RTListNodeRemove(&pMixBuf->Node);
1707
1708 /* Decrease the paren't children count. */
1709 Assert(pMixBuf->pParent->cChildren);
1710 pMixBuf->pParent->cChildren--;
1711
1712 /* Make sure to reset the parent mixing buffer each time it gets linked
1713 * to a new child. */
1714 AudioMixBufReset(pMixBuf->pParent);
1715 pMixBuf->pParent = NULL;
1716 }
1717
1718 PPDMAUDIOMIXBUF pChild, pChildNext;
1719 RTListForEachSafe(&pMixBuf->lstChildren, pChild, pChildNext, PDMAUDIOMIXBUF, Node)
1720 {
1721 AUDMIXBUF_LOG(("\tUnlinking \"%s\"\n", pChild->pszName));
1722
1723 AudioMixBufReset(pChild);
1724
1725 Assert(pChild->pParent == pMixBuf);
1726 pChild->pParent = NULL;
1727
1728 RTListNodeRemove(&pChild->Node);
1729
1730 /* Decrease the children count. */
1731 Assert(pMixBuf->cChildren);
1732 pMixBuf->cChildren--;
1733 }
1734
1735 Assert(RTListIsEmpty(&pMixBuf->lstChildren));
1736 Assert(pMixBuf->cChildren == 0);
1737
1738 AudioMixBufReset(pMixBuf);
1739
1740 if (pMixBuf->pRate)
1741 {
1742 pMixBuf->pRate->dstOffset = pMixBuf->pRate->srcOffset = 0;
1743 pMixBuf->pRate->dstInc = 0;
1744 }
1745
1746 pMixBuf->iFreqRatio = 1; /* Prevent division by zero. */
1747}
1748
1749/**
1750 * Writes audio frames at a specific offset.
1751 * The sample format being written must match the format of the mixing buffer.
1752 *
1753 * @return IPRT status code.
1754 * @param pMixBuf Pointer to mixing buffer to write to.
1755 * @param offFrames Offset (in frames) starting to write at.
1756 * @param pvBuf Pointer to audio buffer to be written.
1757 * @param cbBuf Size (in bytes) of audio buffer.
1758 * @param pcWritten Returns number of audio frames written. Optional.
1759 */
1760int AudioMixBufWriteAt(PPDMAUDIOMIXBUF pMixBuf, uint32_t offFrames, const void *pvBuf, uint32_t cbBuf, uint32_t *pcWritten)
1761{
1762 return AudioMixBufWriteAtEx(pMixBuf, pMixBuf->AudioFmt, offFrames, pvBuf, cbBuf, pcWritten);
1763}
1764
1765/**
1766 * Writes audio frames at a specific offset.
1767 *
1768 * Note that this operation also modifies the current read and write position
1769 * to \a offFrames + written frames on success.
1770 *
1771 * The audio sample format to be written can be different from the audio format
1772 * the mixing buffer operates on.
1773 *
1774 * @return IPRT status code.
1775 * @param pMixBuf Pointer to mixing buffer to write to.
1776 * @param enmFmt Audio format supplied in the buffer.
1777 * @param offFrames Offset (in frames) starting to write at.
1778 * @param pvBuf Pointer to audio buffer to be written.
1779 * @param cbBuf Size (in bytes) of audio buffer.
1780 * @param pcWritten Returns number of audio frames written. Optional.
1781 */
1782int AudioMixBufWriteAtEx(PPDMAUDIOMIXBUF pMixBuf, PDMAUDIOMIXBUFFMT enmFmt,
1783 uint32_t offFrames, const void *pvBuf, uint32_t cbBuf,
1784 uint32_t *pcWritten)
1785{
1786 AssertPtrReturn(pMixBuf, VERR_INVALID_POINTER);
1787 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1788 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1789 /* pcbWritten is optional. */
1790
1791 if (offFrames >= pMixBuf->cFrames)
1792 {
1793 if (pcWritten)
1794 *pcWritten = 0;
1795 return VERR_BUFFER_OVERFLOW;
1796 }
1797
1798 /*
1799 * Adjust cToWrite so we don't overflow our buffers.
1800 */
1801 uint32_t cToWrite = RT_MIN(AUDIOMIXBUF_B2F(pMixBuf, cbBuf), pMixBuf->cFrames - offFrames);
1802
1803#ifdef AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA
1804 /*
1805 * Now that we know how much we'll be converting we can log it.
1806 */
1807 RTFILE hFile;
1808 int rc2 = RTFileOpen(&hFile, AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA_PATH "mixbuf_writeat.pcm",
1809 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
1810 if (RT_SUCCESS(rc2))
1811 {
1812 RTFileWrite(hFile, pvBuf, AUDIOMIXBUF_F2B(pMixBuf, cToWrite), NULL);
1813 RTFileClose(hFile);
1814 }
1815#endif
1816
1817 /*
1818 * Pick the conversion function and do the conversion.
1819 */
1820 PFNPDMAUDIOMIXBUFCONVFROM pfnConvFrom = NULL;
1821 if (!pMixBuf->Volume.fMuted)
1822 {
1823 if (pMixBuf->AudioFmt != enmFmt)
1824 pfnConvFrom = audioMixBufConvFromLookup(enmFmt);
1825 else
1826 pfnConvFrom = pMixBuf->pfnConvFrom;
1827 }
1828 else
1829 pfnConvFrom = &audioMixBufConvFromSilence;
1830
1831 int rc = VINF_SUCCESS;
1832
1833 uint32_t cWritten;
1834 if ( pfnConvFrom
1835 && cToWrite)
1836 {
1837 PDMAUDMIXBUFCONVOPTS convOpts;
1838
1839 convOpts.cFrames = cToWrite;
1840 convOpts.From.Volume.fMuted = pMixBuf->Volume.fMuted;
1841 convOpts.From.Volume.uLeft = pMixBuf->Volume.uLeft;
1842 convOpts.From.Volume.uRight = pMixBuf->Volume.uRight;
1843
1844 cWritten = pfnConvFrom(pMixBuf->pFrames + offFrames, pvBuf, AUDIOMIXBUF_F2B(pMixBuf, cToWrite), &convOpts);
1845 }
1846 else
1847 {
1848 cWritten = 0;
1849 if (!pfnConvFrom)
1850 {
1851 AssertFailed();
1852 rc = VERR_NOT_SUPPORTED;
1853 }
1854 }
1855
1856 AUDMIXBUF_LOG(("%s: offFrames=%RU32, cbBuf=%RU32, cToWrite=%RU32 (%zu bytes), cWritten=%RU32 (%zu bytes), rc=%Rrc\n",
1857 pMixBuf->pszName, offFrames, cbBuf,
1858 cToWrite, AUDIOMIXBUF_F2B(pMixBuf, cToWrite),
1859 cWritten, AUDIOMIXBUF_F2B(pMixBuf, cWritten), rc));
1860
1861 if (RT_SUCCESS(rc))
1862 {
1863 pMixBuf->offRead = offFrames % pMixBuf->cFrames;
1864 pMixBuf->offWrite = (offFrames + cWritten) % pMixBuf->cFrames;
1865 pMixBuf->cUsed = cWritten;
1866 pMixBuf->cMixed = 0;
1867
1868#ifdef DEBUG
1869 audioMixBufDbgValidate(pMixBuf);
1870#endif
1871 if (pcWritten)
1872 *pcWritten = cWritten;
1873 }
1874 else
1875 AUDMIXBUF_LOG(("%s: Failed with %Rrc\n", pMixBuf->pszName, rc));
1876
1877 return rc;
1878}
1879
1880/**
1881 * Writes audio frames.
1882 *
1883 * The sample format being written must match the format of the mixing buffer.
1884 *
1885 * @return IPRT status code, or VERR_BUFFER_OVERFLOW if frames which not have
1886 * been processed yet have been overwritten (due to cyclic buffer).
1887 * @param pMixBuf Pointer to mixing buffer to write to.
1888 * @param pvBuf Pointer to audio buffer to be written.
1889 * @param cbBuf Size (in bytes) of audio buffer.
1890 * @param pcWritten Returns number of audio frames written. Optional.
1891 */
1892int AudioMixBufWriteCirc(PPDMAUDIOMIXBUF pMixBuf,
1893 const void *pvBuf, uint32_t cbBuf,
1894 uint32_t *pcWritten)
1895{
1896 return AudioMixBufWriteCircEx(pMixBuf, pMixBuf->AudioFmt, pvBuf, cbBuf, pcWritten);
1897}
1898
1899/**
1900 * Writes audio frames of a specific format.
1901 * This function might write less data at once than requested.
1902 *
1903 * @return IPRT status code, or VERR_BUFFER_OVERFLOW no space is available for writing anymore.
1904 * @param pMixBuf Pointer to mixing buffer to write to.
1905 * @param enmFmt Audio format supplied in the buffer.
1906 * @param pvBuf Pointer to audio buffer to be written.
1907 * @param cbBuf Size (in bytes) of audio buffer.
1908 * @param pcWritten Returns number of audio frames written. Optional.
1909 */
1910int AudioMixBufWriteCircEx(PPDMAUDIOMIXBUF pMixBuf, PDMAUDIOMIXBUFFMT enmFmt,
1911 const void *pvBuf, uint32_t cbBuf, uint32_t *pcWritten)
1912{
1913 AssertPtrReturn(pMixBuf, VERR_INVALID_POINTER);
1914 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1915 /* pcbWritten is optional. */
1916
1917 if (!cbBuf)
1918 {
1919 if (pcWritten)
1920 *pcWritten = 0;
1921 return VINF_SUCCESS;
1922 }
1923
1924 /* Make sure that we at least write a full audio frame. */
1925 AssertReturn(AUDIOMIXBUF_B2F(pMixBuf, cbBuf), VERR_INVALID_PARAMETER);
1926
1927 Assert(pMixBuf->cFrames);
1928 AssertPtr(pMixBuf->pFrames);
1929
1930 PFNPDMAUDIOMIXBUFCONVFROM pfnConvFrom = NULL;
1931 if (!pMixBuf->Volume.fMuted)
1932 {
1933 if (pMixBuf->AudioFmt != enmFmt)
1934 pfnConvFrom = audioMixBufConvFromLookup(enmFmt);
1935 else
1936 pfnConvFrom = pMixBuf->pfnConvFrom;
1937 }
1938 else
1939 pfnConvFrom = &audioMixBufConvFromSilence;
1940
1941 if (!pfnConvFrom)
1942 {
1943 AssertFailed();
1944 return VERR_NOT_SUPPORTED;
1945 }
1946
1947 int rc = VINF_SUCCESS;
1948
1949 uint32_t cWritten = 0;
1950
1951 uint32_t cFree = pMixBuf->cFrames - pMixBuf->cUsed;
1952 if (cFree)
1953 {
1954 if ((pMixBuf->cFrames - pMixBuf->offWrite) == 0)
1955 pMixBuf->offWrite = 0;
1956
1957 uint32_t cToWrite = RT_MIN(AUDIOMIXBUF_B2F(pMixBuf, cbBuf), RT_MIN(pMixBuf->cFrames - pMixBuf->offWrite, cFree));
1958 Assert(cToWrite);
1959
1960 PDMAUDMIXBUFCONVOPTS convOpts;
1961 RT_ZERO(convOpts);
1962
1963 convOpts.From.Volume.fMuted = pMixBuf->Volume.fMuted;
1964 convOpts.From.Volume.uLeft = pMixBuf->Volume.uLeft;
1965 convOpts.From.Volume.uRight = pMixBuf->Volume.uRight;
1966
1967 convOpts.cFrames = cToWrite;
1968
1969 cWritten = pfnConvFrom(pMixBuf->pFrames + pMixBuf->offWrite,
1970 pvBuf, AUDIOMIXBUF_F2B(pMixBuf, cToWrite), &convOpts);
1971 Assert(cWritten == cToWrite);
1972
1973#ifdef AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA
1974 RTFILE fh;
1975 RTFileOpen(&fh, AUDIOMIXBUF_DEBUG_DUMP_PCM_DATA_PATH "mixbuf_writecirc_ex.pcm",
1976 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
1977 RTFileWrite(fh, pvBuf, AUDIOMIXBUF_F2B(pMixBuf, cToWrite), NULL);
1978 RTFileClose(fh);
1979#endif
1980 pMixBuf->cUsed += cWritten;
1981 Assert(pMixBuf->cUsed <= pMixBuf->cFrames);
1982
1983 pMixBuf->offWrite = (pMixBuf->offWrite + cWritten) % pMixBuf->cFrames;
1984 Assert(pMixBuf->offWrite <= pMixBuf->cFrames);
1985 }
1986 else
1987 rc = VERR_BUFFER_OVERFLOW;
1988
1989#ifdef DEBUG
1990 audioMixBufDbgPrintInternal(pMixBuf, __FUNCTION__);
1991 audioMixBufDbgValidate(pMixBuf);
1992#endif
1993
1994 if (pcWritten)
1995 *pcWritten = cWritten;
1996
1997 AUDMIXBUF_LOG(("%s: enmFmt=0x%x, cbBuf=%RU32 (%RU32 frames), cWritten=%RU32, rc=%Rrc\n",
1998 pMixBuf->pszName, enmFmt, cbBuf, AUDIOMIXBUF_B2F(pMixBuf, cbBuf), cWritten, rc));
1999 return rc;
2000}
2001
2002/**
2003 * Returns the current write position of a mixing buffer.
2004 *
2005 * @returns IPRT status code.
2006 * @param pMixBuf Mixing buffer to return position for.
2007 */
2008uint32_t AudioMixBufWritePos(PPDMAUDIOMIXBUF pMixBuf)
2009{
2010 AssertPtrReturn(pMixBuf, 0);
2011
2012 return pMixBuf->offWrite;
2013}
2014
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