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