VirtualBox

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

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

Audio: Forward ported audio mixing buffer changes from 5.1 (as of r115728).

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

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