1 | /* $Id: AudioMixBuffer.h 89343 2021-05-28 10:33:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Audio Mixing bufer convert audio samples to/from different rates / formats.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef VBOX_INCLUDED_SRC_Audio_AudioMixBuffer_h
|
---|
19 | #define VBOX_INCLUDED_SRC_Audio_AudioMixBuffer_h
|
---|
20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #include <iprt/cdefs.h>
|
---|
25 | #include <VBox/vmm/pdmaudioifs.h>
|
---|
26 |
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Rate processing information of a source & destination audio stream.
|
---|
30 | *
|
---|
31 | * This is needed because both streams can differ regarding their rates and
|
---|
32 | * therefore need to be treated accordingly.
|
---|
33 | */
|
---|
34 | typedef struct AUDIOSTREAMRATE
|
---|
35 | {
|
---|
36 | /** Current (absolute) offset in the output (destination) stream.
|
---|
37 | * @todo r=bird: Please reveal which unit these members are given in. */
|
---|
38 | uint64_t offDst;
|
---|
39 | /** Increment for moving offDst for the destination stream.
|
---|
40 | * This is needed because the source <-> destination rate might be different. */
|
---|
41 | uint64_t uDstInc;
|
---|
42 | /** Current (absolute) offset in the input stream. */
|
---|
43 | uint32_t offSrc;
|
---|
44 | /** Set if no conversion is necessary. */
|
---|
45 | bool fNoConversionNeeded;
|
---|
46 | bool afPadding[3];
|
---|
47 |
|
---|
48 | /** Last processed frame of the input stream.
|
---|
49 | * Needed for interpolation. */
|
---|
50 | union
|
---|
51 | {
|
---|
52 | int64_t ai64Samples[2];
|
---|
53 | PDMAUDIOFRAME Frame;
|
---|
54 | } SrcLast;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Resampling function.
|
---|
58 | * @returns Number of destination frames written.
|
---|
59 | */
|
---|
60 | DECLR3CALLBACKMEMBER(uint32_t, pfnResample, (int64_t *pi64Dst, uint32_t cDstFrames,
|
---|
61 | int64_t const *pi64Src, uint32_t cSrcFrames, uint32_t *pcSrcFramesRead,
|
---|
62 | struct AUDIOSTREAMRATE *pRate));
|
---|
63 |
|
---|
64 | } AUDIOSTREAMRATE;
|
---|
65 | /** Pointer to rate processing information of a stream. */
|
---|
66 | typedef AUDIOSTREAMRATE *PAUDIOSTREAMRATE;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Mixing buffer volume parameters.
|
---|
70 | *
|
---|
71 | * The volume values are in fixed point style and must be converted to/from
|
---|
72 | * before using with e.g. PDMAUDIOVOLUME.
|
---|
73 | */
|
---|
74 | typedef struct AUDMIXBUFVOL
|
---|
75 | {
|
---|
76 | /** Set to @c true if this stream is muted, @c false if not. */
|
---|
77 | bool fMuted;
|
---|
78 | /** Left volume to apply during conversion.
|
---|
79 | * Pass 0 to convert the original values. May not apply to all conversion functions. */
|
---|
80 | uint32_t uLeft;
|
---|
81 | /** Right volume to apply during conversion.
|
---|
82 | * Pass 0 to convert the original values. May not apply to all conversion functions. */
|
---|
83 | uint32_t uRight;
|
---|
84 | } AUDMIXBUFVOL;
|
---|
85 | /** Pointer to mixing buffer volument parameters. */
|
---|
86 | typedef AUDMIXBUFVOL *PAUDMIXBUFVOL;
|
---|
87 |
|
---|
88 |
|
---|
89 | /** Pointer to audio mixing buffer. */
|
---|
90 | typedef struct AUDIOMIXBUF *PAUDIOMIXBUF;
|
---|
91 | /** Pointer to a const audio mixing buffer. */
|
---|
92 | typedef struct AUDIOMIXBUF const *PCAUDIOMIXBUF;
|
---|
93 |
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * State & config for AudioMixBufPeek created by AudioMixBufInitPeekState.
|
---|
97 | */
|
---|
98 | typedef struct AUDIOMIXBUFPEEKSTATE
|
---|
99 | {
|
---|
100 | /** Encodes @a cFrames from @a paSrc to @a pvDst. */
|
---|
101 | DECLR3CALLBACKMEMBER(void, pfnEncode,(void *pvDst, int64_t const *paSrc, uint32_t cFrames, struct AUDIOMIXBUFPEEKSTATE *pState));
|
---|
102 | /** Sample rate conversion state (only used when needed). */
|
---|
103 | AUDIOSTREAMRATE Rate;
|
---|
104 | /** Source (mixer) channels. */
|
---|
105 | uint8_t cSrcChannels;
|
---|
106 | /** Destination channels. */
|
---|
107 | uint8_t cDstChannels;
|
---|
108 | /** Destination frame size. */
|
---|
109 | uint8_t cbDstFrame;
|
---|
110 | } AUDIOMIXBUFPEEKSTATE;
|
---|
111 | /** Pointer to peek state & config. */
|
---|
112 | typedef AUDIOMIXBUFPEEKSTATE *PAUDIOMIXBUFPEEKSTATE;
|
---|
113 |
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * State & config for AudioMixBufWrite, AudioMixBufSilence, AudioMixBufBlend and
|
---|
117 | * AudioMixBufBlendGap, created by AudioMixBufInitWriteState.
|
---|
118 | */
|
---|
119 | typedef struct AUDIOMIXBUFWRITESTATE
|
---|
120 | {
|
---|
121 | /** Encodes @a cFrames from @a pvSrc to @a paDst. */
|
---|
122 | DECLR3CALLBACKMEMBER(void, pfnDecode,(int64_t *paDst, const void *pvSrc, uint32_t cFrames, struct AUDIOMIXBUFWRITESTATE *pState));
|
---|
123 | /** Encodes @a cFrames from @a pvSrc blending into @a paDst. */
|
---|
124 | DECLR3CALLBACKMEMBER(void, pfnDecodeBlend,(int64_t *paDst, const void *pvSrc, uint32_t cFrames, struct AUDIOMIXBUFWRITESTATE *pState));
|
---|
125 | /** Sample rate conversion state (only used when needed). */
|
---|
126 | AUDIOSTREAMRATE Rate;
|
---|
127 | /** Destination (mixer) channels. */
|
---|
128 | uint8_t cDstChannels;
|
---|
129 | /** Source hannels. */
|
---|
130 | uint8_t cSrcChannels;
|
---|
131 | /** Source frame size. */
|
---|
132 | uint8_t cbSrcFrame;
|
---|
133 | } AUDIOMIXBUFWRITESTATE;
|
---|
134 | /** Pointer to write state & config. */
|
---|
135 | typedef AUDIOMIXBUFWRITESTATE *PAUDIOMIXBUFWRITESTATE;
|
---|
136 |
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Audio mixing buffer.
|
---|
140 | */
|
---|
141 | typedef struct AUDIOMIXBUF
|
---|
142 | {
|
---|
143 | /** Magic value (AUDIOMIXBUF_MAGIC). */
|
---|
144 | uint32_t uMagic;
|
---|
145 | /** Size of the frame buffer (in audio frames). */
|
---|
146 | uint32_t cFrames;
|
---|
147 | /** Frame buffer. */
|
---|
148 | PPDMAUDIOFRAME pFrames;
|
---|
149 | /** The current read position (in frames). */
|
---|
150 | uint32_t offRead;
|
---|
151 | /** The current write position (in frames). */
|
---|
152 | uint32_t offWrite;
|
---|
153 | /** Total frames already mixed down to the parent buffer (if any).
|
---|
154 | *
|
---|
155 | * Always starting at the parent's offRead position.
|
---|
156 | * @note Count always is specified in parent frames, as the sample count can
|
---|
157 | * differ between parent and child. */
|
---|
158 | uint32_t cMixed;
|
---|
159 | /** How much audio frames are currently being used in this buffer.
|
---|
160 | * @note This also is known as the distance in ring buffer terms. */
|
---|
161 | uint32_t cUsed;
|
---|
162 | /** Audio properties for the buffer content - for frequency and channel count.
|
---|
163 | * (This is the guest side PCM properties.) */
|
---|
164 | PDMAUDIOPCMPROPS Props;
|
---|
165 | /** Internal representation of current volume used for mixing. */
|
---|
166 | AUDMIXBUFVOL Volume;
|
---|
167 | /** Name of the buffer. */
|
---|
168 | char *pszName;
|
---|
169 | } AUDIOMIXBUF;
|
---|
170 |
|
---|
171 | /** Magic value for AUDIOMIXBUF (Antonio Lucio Vivaldi). */
|
---|
172 | #define AUDIOMIXBUF_MAGIC UINT32_C(0x16780304)
|
---|
173 | /** Dead mixer buffer magic. */
|
---|
174 | #define AUDIOMIXBUF_MAGIC_DEAD UINT32_C(0x17410728)
|
---|
175 |
|
---|
176 | /** Converts (audio) frames to bytes. */
|
---|
177 | #define AUDIOMIXBUF_F2B(a_pMixBuf, a_cFrames) PDMAUDIOPCMPROPS_F2B(&(a_pMixBuf)->Props, a_cFrames)
|
---|
178 | /** Converts bytes to (audio) frames.
|
---|
179 | * @note Does *not* take the conversion ratio into account. */
|
---|
180 | #define AUDIOMIXBUF_B2F(a_pMixBuf, a_cb) PDMAUDIOPCMPROPS_B2F(&(a_pMixBuf)->Props, a_cb)
|
---|
181 |
|
---|
182 |
|
---|
183 | int AudioMixBufInit(PAUDIOMIXBUF pMixBuf, const char *pszName, PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames);
|
---|
184 | void AudioMixBufDestroy(PAUDIOMIXBUF pMixBuf);
|
---|
185 | void AudioMixBufDrop(PAUDIOMIXBUF pMixBuf);
|
---|
186 |
|
---|
187 | uint32_t AudioMixBufSize(PAUDIOMIXBUF pMixBuf);
|
---|
188 | uint32_t AudioMixBufSizeBytes(PAUDIOMIXBUF pMixBuf);
|
---|
189 | uint32_t AudioMixBufFree(PAUDIOMIXBUF pMixBuf);
|
---|
190 | uint32_t AudioMixBufFreeBytes(PAUDIOMIXBUF pMixBuf);
|
---|
191 | bool AudioMixBufIsEmpty(PCAUDIOMIXBUF pMixBuf);
|
---|
192 | uint32_t AudioMixBufLive(PAUDIOMIXBUF pMixBuf);
|
---|
193 | uint32_t AudioMixBufUsed(PAUDIOMIXBUF pMixBuf);
|
---|
194 | uint32_t AudioMixBufUsedBytes(PAUDIOMIXBUF pMixBuf);
|
---|
195 | uint32_t AudioMixBufReadPos(PAUDIOMIXBUF pMixBuf);
|
---|
196 | uint32_t AudioMixBufWritePos(PAUDIOMIXBUF pMixBuf);
|
---|
197 |
|
---|
198 | void AudioMixBufSetVolume(PAUDIOMIXBUF pMixBuf, PCPDMAUDIOVOLUME pVol);
|
---|
199 |
|
---|
200 | int AudioMixBufInitPeekState(PCAUDIOMIXBUF pMixBuf, PAUDIOMIXBUFPEEKSTATE pState, PCPDMAUDIOPCMPROPS pDstProps);
|
---|
201 | void AudioMixBufPeek(PCAUDIOMIXBUF pMixBuf, uint32_t offSrcFrame, uint32_t cMaxSrcFrames, uint32_t *pcSrcFramesPeeked,
|
---|
202 | PAUDIOMIXBUFPEEKSTATE pState, void *pvDst, uint32_t cbDst, uint32_t *pcbDstPeeked);
|
---|
203 | void AudioMixBufAdvance(PAUDIOMIXBUF pMixBuf, uint32_t cFrames);
|
---|
204 |
|
---|
205 | int AudioMixBufInitWriteState(PCAUDIOMIXBUF pMixBuf, PAUDIOMIXBUFWRITESTATE pState, PCPDMAUDIOPCMPROPS pSrcProps);
|
---|
206 | void AudioMixBufWrite(PAUDIOMIXBUF pMixBuf, PAUDIOMIXBUFWRITESTATE pState, const void *pvSrcBuf, uint32_t cbSrcBuf,
|
---|
207 | uint32_t offDstFrame, uint32_t cMaxDstFrames, uint32_t *pcDstFramesWritten);
|
---|
208 | void AudioMixBufSilence(PAUDIOMIXBUF pMixBuf, PAUDIOMIXBUFWRITESTATE pState, uint32_t offFrame, uint32_t cFrames);
|
---|
209 | void AudioMixBufBlend(PAUDIOMIXBUF pMixBuf, PAUDIOMIXBUFWRITESTATE pState, const void *pvSrcBuf, uint32_t cbSrcBuf,
|
---|
210 | uint32_t offDstFrame, uint32_t cMaxDstFrames, uint32_t *pcDstFramesBlended);
|
---|
211 | void AudioMixBufBlendGap(PAUDIOMIXBUF pMixBuf, PAUDIOMIXBUFWRITESTATE pState, uint32_t cFrames);
|
---|
212 | void AudioMixBufCommit(PAUDIOMIXBUF pMixBuf, uint32_t cFrames);
|
---|
213 |
|
---|
214 | #ifdef DEBUG
|
---|
215 | void AudioMixBufDbgPrint(PAUDIOMIXBUF pMixBuf);
|
---|
216 | void AudioMixBufDbgPrintChain(PAUDIOMIXBUF pMixBuf);
|
---|
217 | #endif
|
---|
218 |
|
---|
219 | #endif /* !VBOX_INCLUDED_SRC_Audio_AudioMixBuffer_h */
|
---|
220 |
|
---|