VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/AudioMixBuffer.h@ 88391

Last change on this file since 88391 was 88356, checked in by vboxsync, 4 years ago

Audio: Trimmed down PDMAUDIOSTREAM a lot by moving non-essential stuff into an wrapper structure in DrvAudio. This allows for the mixing buffers and other stuff to move (back?) into AudioMixBuffer.h. Also started specifying away to skip the mixing in DrvAudio as only DevSB16 really needs this (goal is to reduce number of copies and bufferings). bugref:9890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1/* $Id: AudioMixBuffer.h 88356 2021-04-04 22:45:13Z 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 */
34typedef 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 /** Explicit alignment padding. */
45 uint32_t u32AlignmentPadding;
46 /** Last processed frame of the input stream.
47 * Needed for interpolation. */
48 PDMAUDIOFRAME SrcFrameLast;
49} AUDIOSTREAMRATE;
50/** Pointer to rate processing information of a stream. */
51typedef AUDIOSTREAMRATE *PAUDIOSTREAMRATE;
52
53/**
54 * Mixing buffer volume parameters.
55 *
56 * The volume values are in fixed point style and must be converted to/from
57 * before using with e.g. PDMAUDIOVOLUME.
58 */
59typedef struct AUDMIXBUFVOL
60{
61 /** Set to @c true if this stream is muted, @c false if not. */
62 bool fMuted;
63 /** Left volume to apply during conversion.
64 * Pass 0 to convert the original values. May not apply to all conversion functions. */
65 uint32_t uLeft;
66 /** Right volume to apply during conversion.
67 * Pass 0 to convert the original values. May not apply to all conversion functions. */
68 uint32_t uRight;
69} AUDMIXBUFVOL;
70/** Pointer to mixing buffer volument parameters. */
71typedef AUDMIXBUFVOL *PAUDMIXBUFVOL;
72
73/*
74 * Frame conversion parameters for the audioMixBufConvFromXXX / audioMixBufConvToXXX functions.
75 */
76typedef struct AUDMIXBUFCONVOPTS
77{
78 /** Number of audio frames to convert. */
79 uint32_t cFrames;
80 union
81 {
82 struct
83 {
84 /** Volume to use for conversion. */
85 AUDMIXBUFVOL Volume;
86 } From;
87 } RT_UNION_NM(u);
88} AUDMIXBUFCONVOPTS;
89/** Pointer to conversion parameters for the audio mixer. */
90typedef AUDMIXBUFCONVOPTS *PAUDMIXBUFCONVOPTS;
91/** Pointer to const conversion parameters for the audio mixer. */
92typedef AUDMIXBUFCONVOPTS const *PCAUDMIXBUFCONVOPTS;
93
94/**
95 * @note All internal handling is done in audio frames, not in bytes!
96 * @todo r=bird: What does this note actually apply to?
97 */
98typedef uint32_t AUDIOMIXBUFFMT;
99typedef AUDIOMIXBUFFMT *PAUDIOMIXBUFFMT;
100
101/**
102 * Convertion-from function used by the audio buffer mixer.
103 *
104 * @returns Number of audio frames returned.
105 * @param paDst Where to return the converted frames.
106 * @param pvSrc The source frame bytes.
107 * @param cbSrc Number of bytes to convert.
108 * @param pOpts Conversion options.
109 * @todo r=bird: The @a paDst size is presumable given in @a pOpts->cFrames?
110 */
111typedef DECLCALLBACKTYPE(uint32_t, FNAUDIOMIXBUFCONVFROM,(PPDMAUDIOFRAME paDst, const void *pvSrc, uint32_t cbSrc,
112 PCAUDMIXBUFCONVOPTS pOpts));
113/** Pointer to a convertion-from function used by the audio buffer mixer. */
114typedef FNAUDIOMIXBUFCONVFROM *PFNAUDIOMIXBUFCONVFROM;
115
116/**
117 * Convertion-to function used by the audio buffer mixer.
118 *
119 * @param pvDst Output buffer.
120 * @param paSrc The input frames.
121 * @param pOpts Conversion options.
122 * @todo r=bird: The @a paSrc size is presumable given in @a pOpts->cFrames and
123 * this implicitly gives the pvDst size too, right?
124 */
125typedef DECLCALLBACKTYPE(void, FNAUDIOMIXBUFCONVTO,(void *pvDst, PCPDMAUDIOFRAME paSrc, PCAUDMIXBUFCONVOPTS pOpts));
126/** Pointer to a convertion-to function used by the audio buffer mixer. */
127typedef FNAUDIOMIXBUFCONVTO *PFNAUDIOMIXBUFCONVTO;
128
129/** Pointer to audio mixing buffer. */
130typedef struct AUDIOMIXBUF *PAUDIOMIXBUF;
131
132/**
133 * Audio mixing buffer.
134 */
135typedef struct AUDIOMIXBUF
136{
137 /** Magic value (AUDIOMIXBUF_MAGIC). */
138 uint32_t uMagic;
139 uint8_t abPadding[4];
140 /* ???Undocumented??? */
141 RTLISTNODE Node;
142 /** Name of the buffer. */
143 char *pszName;
144 /** Frame buffer. */
145 PPDMAUDIOFRAME pFrames;
146 /** Size of the frame buffer (in audio frames). */
147 uint32_t cFrames;
148 /** The current read position (in frames). */
149 uint32_t offRead;
150 /** The current write position (in frames). */
151 uint32_t offWrite;
152 /** Total frames already mixed down to the parent buffer (if any).
153 *
154 * Always starting at the parent's offRead position.
155 * @note Count always is specified in parent frames, as the sample count can
156 * differ between parent and child. */
157 uint32_t cMixed;
158 /** How much audio frames are currently being used in this buffer.
159 * @note This also is known as the distance in ring buffer terms. */
160 uint32_t cUsed;
161 /** Number of children mix buffers kept in lstChildren. */
162 uint32_t cChildren;
163 /** List of children mix buffers to keep in sync with (if being a parent buffer). */
164 RTLISTANCHOR lstChildren;
165 /** Pointer to parent buffer (if any). */
166 PAUDIOMIXBUF pParent;
167 /** Intermediate structure for buffer conversion tasks. */
168 PAUDIOSTREAMRATE pRate;
169 /** Internal representation of current volume used for mixing. */
170 AUDMIXBUFVOL Volume;
171 /** This buffer's audio format.
172 * @todo r=bird: This seems to be a value created by AUDMIXBUF_AUDIO_FMT_MAKE(),
173 * which is not define here. Does this structure really belong here at
174 * all? */
175 AUDIOMIXBUFFMT uAudioFmt;
176 /** Audio input properties.
177 * @note There is only one set of audio properties here because we have one
178 * mixer buffer for the guest side and a separate one for the host side.
179 * @todo r=bird: Why exactly do we need to use separate mixer buffers?
180 * Couldn't we just have different conversion fuctions and save the
181 * extra copying? */
182 PDMAUDIOPCMPROPS Props;
183 /** Standard conversion-to function for set uAudioFmt. */
184 PFNAUDIOMIXBUFCONVTO pfnConvTo;
185 /** Standard conversion-from function for set uAudioFmt. */
186 PFNAUDIOMIXBUFCONVFROM pfnConvFrom;
187
188 /** Ratio of the associated parent stream's frequency by this stream's
189 * frequency (1<<32), represented as a signed 64 bit integer.
190 *
191 * For example, if the parent stream has a frequency of 44 khZ, and this
192 * stream has a frequency of 11 kHz, the ration then would be
193 * (44/11 * (1 << 32)).
194 *
195 * Currently this does not get changed once assigned. */
196 int64_t iFreqRatio;
197} AUDIOMIXBUF;
198
199/** Magic value for AUDIOMIXBUF (Antonio Lucio Vivaldi). */
200#define AUDIOMIXBUF_MAGIC UINT32_C(0x16780304)
201/** Dead mixer buffer magic. */
202#define AUDIOMIXBUF_MAGIC_DEAD UINT32_C(0x17410728)
203
204
205/** Constructs 32 bit value for given frequency, number of channels, bits per sample and signed bit.
206 * @note This currently matches 1:1 the VRDE encoding -- this might change in the future, so better don't rely on this fact! */
207#define AUDMIXBUF_AUDIO_FMT_MAKE(freq, c, bps, s) ((((s) & 0x1) << 28) + (((bps) & 0xFF) << 20) + (((c) & 0xF) << 16) + ((freq) & 0xFFFF))
208
209/** Decodes frequency (Hz). */
210#define AUDMIXBUF_FMT_SAMPLE_FREQ(a) ((a) & 0xFFFF)
211/** Decodes number of channels. */
212#define AUDMIXBUF_FMT_CHANNELS(a) (((a) >> 16) & 0xF)
213/** Decodes signed bit. */
214#define AUDMIXBUF_FMT_SIGNED(a) (((a) >> 28) & 0x1)
215/** Decodes number of bits per sample. */
216#define AUDMIXBUF_FMT_BITS_PER_SAMPLE(a) (((a) >> 20) & 0xFF)
217/** Decodes number of bytes per sample. */
218#define AUDMIXBUF_FMT_BYTES_PER_SAMPLE(a) ((AUDMIXBUF_AUDIO_FMT_BITS_PER_SAMPLE(a) + 7) / 8)
219
220/** Converts (audio) frames to bytes. */
221#define AUDIOMIXBUF_F2B(a_pMixBuf, a_cFrames) PDMAUDIOPCMPROPS_F2B(&(a_pMixBuf)->Props, a_cFrames)
222/** Converts bytes to (audio) frames.
223 * @note Does *not* take the conversion ratio into account. */
224#define AUDIOMIXBUF_B2F(a_pMixBuf, a_cb) PDMAUDIOPCMPROPS_B2F(&(a_pMixBuf)->Props, a_cb)
225
226/** Converts frames to bytes, respecting the conversion ratio to
227 * a linked buffer. */
228#define AUDIOMIXBUF_F2B_RATIO(a_pMixBuf, a_cFrames) AUDIOMIXBUF_F2B(a_pMixBuf, AUDIOMIXBUF_F2F_RATIO(a_pMixBuf, a_cFrames))
229/** Converts number of frames according to the buffer's ratio.
230 * @todo r=bird: Why the *signed* cast? */
231#define AUDIOMIXBUF_F2F_RATIO(a_pMixBuf, a_cFrames) (((int64_t)(a_cFrames) << 32) / (a_pMixBuf)->iFreqRatio)
232
233
234int AudioMixBufInit(PAUDIOMIXBUF pMixBuf, const char *pszName, PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames);
235void AudioMixBufDestroy(PAUDIOMIXBUF pMixBuf);
236void AudioMixBufClear(PAUDIOMIXBUF pMixBuf);
237void AudioMixBufFinish(PAUDIOMIXBUF pMixBuf, uint32_t cFramesToClear);
238uint32_t AudioMixBufFree(PAUDIOMIXBUF pMixBuf);
239uint32_t AudioMixBufFreeBytes(PAUDIOMIXBUF pMixBuf);
240bool AudioMixBufIsEmpty(PAUDIOMIXBUF pMixBuf);
241int AudioMixBufLinkTo(PAUDIOMIXBUF pMixBuf, PAUDIOMIXBUF pParent);
242uint32_t AudioMixBufLive(PAUDIOMIXBUF pMixBuf);
243int AudioMixBufMixToParent(PAUDIOMIXBUF pMixBuf, uint32_t cSrcFrames, uint32_t *pcSrcMixed);
244int AudioMixBufMixToParentEx(PAUDIOMIXBUF pMixBuf, uint32_t cSrcOffset, uint32_t cSrcFrames, uint32_t *pcSrcMixed);
245int AudioMixBufPeekMutable(PAUDIOMIXBUF pMixBuf, uint32_t cFramesToRead, PPDMAUDIOFRAME *ppvSamples, uint32_t *pcFramesRead);
246uint32_t AudioMixBufUsed(PAUDIOMIXBUF pMixBuf);
247uint32_t AudioMixBufUsedBytes(PAUDIOMIXBUF pMixBuf);
248int AudioMixBufReadAt(PAUDIOMIXBUF pMixBuf, uint32_t offFrames, void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead);
249int AudioMixBufReadAtEx(PAUDIOMIXBUF pMixBuf, PCPDMAUDIOPCMPROPS pDstProps, uint32_t offFrames,
250 void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead);
251int AudioMixBufAcquireReadBlock(PAUDIOMIXBUF pMixBuf, void *pvBuf, uint32_t cbBuf, uint32_t *pcAcquiredFrames);
252int AudioMixBufAcquireReadBlockEx(PAUDIOMIXBUF pMixBuf, PCPDMAUDIOPCMPROPS pDstProps,
253 void *pvBuf, uint32_t cbBuf, uint32_t *pcAcquiredFrames);
254void AudioMixBufReleaseReadBlock(PAUDIOMIXBUF pMixBuf, uint32_t cFrames);
255uint32_t AudioMixBufReadPos(PAUDIOMIXBUF pMixBuf);
256void AudioMixBufReset(PAUDIOMIXBUF pMixBuf);
257void AudioMixBufSetVolume(PAUDIOMIXBUF pMixBuf, PPDMAUDIOVOLUME pVol);
258uint32_t AudioMixBufSize(PAUDIOMIXBUF pMixBuf);
259uint32_t AudioMixBufSizeBytes(PAUDIOMIXBUF pMixBuf);
260void AudioMixBufUnlink(PAUDIOMIXBUF pMixBuf);
261int AudioMixBufWriteAt(PAUDIOMIXBUF pMixBuf, uint32_t offSamples, const void *pvBuf, uint32_t cbBuf, uint32_t *pcWritten);
262int AudioMixBufWriteAtEx(PAUDIOMIXBUF pMixBuf, PCPDMAUDIOPCMPROPS pSrcProps, uint32_t offFrames,
263 const void *pvBuf, uint32_t cbBuf, uint32_t *pcWritten);
264int AudioMixBufWriteCirc(PAUDIOMIXBUF pMixBuf, const void *pvBuf, uint32_t cbBuf, uint32_t *pcWritten);
265int AudioMixBufWriteCircEx(PAUDIOMIXBUF pMixBuf, PCPDMAUDIOPCMPROPS pSrcProps,
266 const void *pvBuf,uint32_t cbBuf, uint32_t *pcWritten);
267uint32_t AudioMixBufWritePos(PAUDIOMIXBUF pMixBuf);
268
269#ifdef DEBUG
270void AudioMixBufDbgPrint(PAUDIOMIXBUF pMixBuf);
271void AudioMixBufDbgPrintChain(PAUDIOMIXBUF pMixBuf);
272#endif
273
274#endif /* !VBOX_INCLUDED_SRC_Audio_AudioMixBuffer_h */
275
Note: See TracBrowser for help on using the repository browser.

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