1 | /* $Id: AudioMixBuffer.h 89382 2021-05-31 00:03:03Z 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 | int32_t ai32Samples[PDMAUDIO_MAX_CHANNELS];
|
---|
53 | } SrcLast;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Resampling function.
|
---|
57 | * @returns Number of destination frames written.
|
---|
58 | */
|
---|
59 | DECLR3CALLBACKMEMBER(uint32_t, pfnResample, (int32_t *pi32Dst, uint32_t cDstFrames,
|
---|
60 | int32_t const *pi32Src, uint32_t cSrcFrames, uint32_t *pcSrcFramesRead,
|
---|
61 | struct AUDIOSTREAMRATE *pRate));
|
---|
62 |
|
---|
63 | } AUDIOSTREAMRATE;
|
---|
64 | /** Pointer to rate processing information of a stream. */
|
---|
65 | typedef AUDIOSTREAMRATE *PAUDIOSTREAMRATE;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Mixing buffer volume parameters.
|
---|
69 | *
|
---|
70 | * The volume values are in fixed point style and must be converted to/from
|
---|
71 | * before using with e.g. PDMAUDIOVOLUME.
|
---|
72 | */
|
---|
73 | typedef struct AUDMIXBUFVOL
|
---|
74 | {
|
---|
75 | /** Set to @c true if this stream is muted, @c false if not. */
|
---|
76 | bool fMuted;
|
---|
77 | /** Set if all (relevant) channels are at max. */
|
---|
78 | bool fAllMax;
|
---|
79 | /** The per-channels values. */
|
---|
80 | uint32_t auChannels[PDMAUDIO_MAX_CHANNELS];
|
---|
81 | } AUDMIXBUFVOL;
|
---|
82 | /** Pointer to mixing buffer volument parameters. */
|
---|
83 | typedef AUDMIXBUFVOL *PAUDMIXBUFVOL;
|
---|
84 |
|
---|
85 |
|
---|
86 | /** Pointer to audio mixing buffer. */
|
---|
87 | typedef struct AUDIOMIXBUF *PAUDIOMIXBUF;
|
---|
88 | /** Pointer to a const audio mixing buffer. */
|
---|
89 | typedef struct AUDIOMIXBUF const *PCAUDIOMIXBUF;
|
---|
90 |
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * State & config for AudioMixBufPeek created by AudioMixBufInitPeekState.
|
---|
94 | */
|
---|
95 | typedef struct AUDIOMIXBUFPEEKSTATE
|
---|
96 | {
|
---|
97 | /** Encodes @a cFrames from @a paSrc to @a pvDst. */
|
---|
98 | DECLR3CALLBACKMEMBER(void, pfnEncode,(void *pvDst, int32_t const *paSrc, uint32_t cFrames, struct AUDIOMIXBUFPEEKSTATE *pState));
|
---|
99 | /** Sample rate conversion state (only used when needed). */
|
---|
100 | AUDIOSTREAMRATE Rate;
|
---|
101 | /** Source (mixer) channels. */
|
---|
102 | uint8_t cSrcChannels;
|
---|
103 | /** Destination channels. */
|
---|
104 | uint8_t cDstChannels;
|
---|
105 | /** Destination frame size. */
|
---|
106 | uint8_t cbDstFrame;
|
---|
107 | /** The destination frame layout described as indexes into the source frame.
|
---|
108 | * This ASSUMES that all channels uses the same sample size, so one sample per
|
---|
109 | * channel if you like.
|
---|
110 | * Negative values are special: -1 for zero, -2 for silence.
|
---|
111 | * @note Blending stereo into mono is not really expressible here. */
|
---|
112 | int8_t aidxChannelMap[PDMAUDIO_MAX_CHANNELS];
|
---|
113 | } AUDIOMIXBUFPEEKSTATE;
|
---|
114 | /** Pointer to peek state & config. */
|
---|
115 | typedef AUDIOMIXBUFPEEKSTATE *PAUDIOMIXBUFPEEKSTATE;
|
---|
116 |
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * State & config for AudioMixBufWrite, AudioMixBufSilence, AudioMixBufBlend and
|
---|
120 | * AudioMixBufBlendGap, created by AudioMixBufInitWriteState.
|
---|
121 | */
|
---|
122 | typedef struct AUDIOMIXBUFWRITESTATE
|
---|
123 | {
|
---|
124 | /** Encodes @a cFrames from @a pvSrc to @a paDst. */
|
---|
125 | DECLR3CALLBACKMEMBER(void, pfnDecode,(int32_t *paDst, const void *pvSrc, uint32_t cFrames, struct AUDIOMIXBUFWRITESTATE *pState));
|
---|
126 | /** Encodes @a cFrames from @a pvSrc blending into @a paDst. */
|
---|
127 | DECLR3CALLBACKMEMBER(void, pfnDecodeBlend,(int32_t *paDst, const void *pvSrc, uint32_t cFrames, struct AUDIOMIXBUFWRITESTATE *pState));
|
---|
128 | /** Sample rate conversion state (only used when needed). */
|
---|
129 | AUDIOSTREAMRATE Rate;
|
---|
130 | /** Destination (mixer) channels. */
|
---|
131 | uint8_t cDstChannels;
|
---|
132 | /** Source hannels. */
|
---|
133 | uint8_t cSrcChannels;
|
---|
134 | /** Source frame size. */
|
---|
135 | uint8_t cbSrcFrame;
|
---|
136 | /** The destination frame layout described as indexes into the source frame.
|
---|
137 | * This ASSUMES that all channels uses the same sample size, so one sample per
|
---|
138 | * channel if you like.
|
---|
139 | * Negative values are special: -1 for zero, -2 for silence.
|
---|
140 | * @note Blending stereo into mono is not really expressible here. */
|
---|
141 | int8_t aidxChannelMap[PDMAUDIO_MAX_CHANNELS];
|
---|
142 | } AUDIOMIXBUFWRITESTATE;
|
---|
143 | /** Pointer to write state & config. */
|
---|
144 | typedef AUDIOMIXBUFWRITESTATE *PAUDIOMIXBUFWRITESTATE;
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Audio mixing buffer.
|
---|
149 | */
|
---|
150 | typedef struct AUDIOMIXBUF
|
---|
151 | {
|
---|
152 | /** Magic value (AUDIOMIXBUF_MAGIC). */
|
---|
153 | uint32_t uMagic;
|
---|
154 | /** Size of the frame buffer (in audio frames). */
|
---|
155 | uint32_t cFrames;
|
---|
156 | /** The frame buffer.
|
---|
157 | * This is a two dimensional array consisting of cFrames rows and
|
---|
158 | * cChannels columns. */
|
---|
159 | int32_t *pi32Samples;
|
---|
160 | /** The number of channels. */
|
---|
161 | uint8_t cChannels;
|
---|
162 | /** The frame size (row size if you like). */
|
---|
163 | uint8_t cbFrame;
|
---|
164 | uint8_t abPadding[2];
|
---|
165 | /** The current read position (in frames). */
|
---|
166 | uint32_t offRead;
|
---|
167 | /** The current write position (in frames). */
|
---|
168 | uint32_t offWrite;
|
---|
169 | /** How much audio frames are currently being used in this buffer.
|
---|
170 | * @note This also is known as the distance in ring buffer terms. */
|
---|
171 | uint32_t cUsed;
|
---|
172 | /** Audio properties for the buffer content - for frequency and channel count.
|
---|
173 | * (This is the guest side PCM properties.) */
|
---|
174 | PDMAUDIOPCMPROPS Props;
|
---|
175 | /** Internal representation of current volume used for mixing. */
|
---|
176 | AUDMIXBUFVOL Volume;
|
---|
177 | /** Name of the buffer. */
|
---|
178 | char *pszName;
|
---|
179 | } AUDIOMIXBUF;
|
---|
180 |
|
---|
181 | /** Magic value for AUDIOMIXBUF (Antonio Lucio Vivaldi). */
|
---|
182 | #define AUDIOMIXBUF_MAGIC UINT32_C(0x16780304)
|
---|
183 | /** Dead mixer buffer magic. */
|
---|
184 | #define AUDIOMIXBUF_MAGIC_DEAD UINT32_C(0x17410728)
|
---|
185 |
|
---|
186 | /** Converts (audio) frames to bytes. */
|
---|
187 | #define AUDIOMIXBUF_F2B(a_pMixBuf, a_cFrames) PDMAUDIOPCMPROPS_F2B(&(a_pMixBuf)->Props, a_cFrames)
|
---|
188 | /** Converts bytes to (audio) frames.
|
---|
189 | * @note Does *not* take the conversion ratio into account. */
|
---|
190 | #define AUDIOMIXBUF_B2F(a_pMixBuf, a_cb) PDMAUDIOPCMPROPS_B2F(&(a_pMixBuf)->Props, a_cb)
|
---|
191 |
|
---|
192 |
|
---|
193 | int AudioMixBufInit(PAUDIOMIXBUF pMixBuf, const char *pszName, PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames);
|
---|
194 | void AudioMixBufTerm(PAUDIOMIXBUF pMixBuf);
|
---|
195 | void AudioMixBufDrop(PAUDIOMIXBUF pMixBuf);
|
---|
196 | void AudioMixBufSetVolume(PAUDIOMIXBUF pMixBuf, PCPDMAUDIOVOLUME pVol);
|
---|
197 |
|
---|
198 | /** @name Mixer buffer getters
|
---|
199 | * @{ */
|
---|
200 | uint32_t AudioMixBufSize(PCAUDIOMIXBUF pMixBuf);
|
---|
201 | uint32_t AudioMixBufSizeBytes(PCAUDIOMIXBUF pMixBuf);
|
---|
202 | uint32_t AudioMixBufUsed(PCAUDIOMIXBUF pMixBuf);
|
---|
203 | uint32_t AudioMixBufUsedBytes(PCAUDIOMIXBUF pMixBuf);
|
---|
204 | uint32_t AudioMixBufFree(PCAUDIOMIXBUF pMixBuf);
|
---|
205 | uint32_t AudioMixBufFreeBytes(PCAUDIOMIXBUF pMixBuf);
|
---|
206 | bool AudioMixBufIsEmpty(PCAUDIOMIXBUF pMixBuf);
|
---|
207 | uint32_t AudioMixBufReadPos(PCAUDIOMIXBUF pMixBuf);
|
---|
208 | uint32_t AudioMixBufWritePos(PCAUDIOMIXBUF pMixBuf);
|
---|
209 | /** @} */
|
---|
210 |
|
---|
211 | /** @name Mixer buffer reading
|
---|
212 | * @{ */
|
---|
213 | int AudioMixBufInitPeekState(PCAUDIOMIXBUF pMixBuf, PAUDIOMIXBUFPEEKSTATE pState, PCPDMAUDIOPCMPROPS pDstProps);
|
---|
214 | void AudioMixBufPeek(PCAUDIOMIXBUF pMixBuf, uint32_t offSrcFrame, uint32_t cMaxSrcFrames, uint32_t *pcSrcFramesPeeked,
|
---|
215 | PAUDIOMIXBUFPEEKSTATE pState, void *pvDst, uint32_t cbDst, uint32_t *pcbDstPeeked);
|
---|
216 | void AudioMixBufAdvance(PAUDIOMIXBUF pMixBuf, uint32_t cFrames);
|
---|
217 | /** @} */
|
---|
218 |
|
---|
219 | /** @name Mixer buffer writing
|
---|
220 | * @{ */
|
---|
221 | int AudioMixBufInitWriteState(PCAUDIOMIXBUF pMixBuf, PAUDIOMIXBUFWRITESTATE pState, PCPDMAUDIOPCMPROPS pSrcProps);
|
---|
222 | void AudioMixBufWrite(PAUDIOMIXBUF pMixBuf, PAUDIOMIXBUFWRITESTATE pState, const void *pvSrcBuf, uint32_t cbSrcBuf,
|
---|
223 | uint32_t offDstFrame, uint32_t cMaxDstFrames, uint32_t *pcDstFramesWritten);
|
---|
224 | void AudioMixBufSilence(PAUDIOMIXBUF pMixBuf, PAUDIOMIXBUFWRITESTATE pState, uint32_t offFrame, uint32_t cFrames);
|
---|
225 | void AudioMixBufBlend(PAUDIOMIXBUF pMixBuf, PAUDIOMIXBUFWRITESTATE pState, const void *pvSrcBuf, uint32_t cbSrcBuf,
|
---|
226 | uint32_t offDstFrame, uint32_t cMaxDstFrames, uint32_t *pcDstFramesBlended);
|
---|
227 | void AudioMixBufBlendGap(PAUDIOMIXBUF pMixBuf, PAUDIOMIXBUFWRITESTATE pState, uint32_t cFrames);
|
---|
228 | void AudioMixBufCommit(PAUDIOMIXBUF pMixBuf, uint32_t cFrames);
|
---|
229 | /** @} */
|
---|
230 |
|
---|
231 | #endif /* !VBOX_INCLUDED_SRC_Audio_AudioMixBuffer_h */
|
---|
232 |
|
---|