VirtualBox

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

Last change on this file since 62323 was 61887, checked in by vboxsync, 9 years ago

Audio: Implemented support for master volume controls.

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