VirtualBox

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

Last change on this file since 61608 was 61523, checked in by vboxsync, 9 years ago

Audio: Update.

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

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