VirtualBox

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

Last change on this file since 64565 was 64565, checked in by vboxsync, 8 years ago

Audio/AudioMixBuffer.cpp: Also take the used samples count into account in AudioMixBufFinish(). Converted some VINF_ return codes to VERR_ ones for easier error handling. Documentation, logging tweaks.

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