VirtualBox

source: vbox/trunk/include/VBox/vmm/pdmaudioinline.h@ 99396

Last change on this file since 99396 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 51.9 KB
Line 
1/* $Id: pdmaudioinline.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * PDM - Audio Helpers, Inlined Code. (DEV,++)
4 *
5 * This is all inlined because it's too tedious to create a couple libraries to
6 * contain it all (same bad excuse as for intnetinline.h & pdmnetinline.h).
7 */
8
9/*
10 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
11 *
12 * This file is part of VirtualBox base platform packages, as
13 * available from https://www.virtualbox.org.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation, in version 3 of the
18 * License.
19 *
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, see <https://www.gnu.org/licenses>.
27 *
28 * The contents of this file may alternatively be used under the terms
29 * of the Common Development and Distribution License Version 1.0
30 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
31 * in the VirtualBox distribution, in which case the provisions of the
32 * CDDL are applicable instead of those of the GPL.
33 *
34 * You may elect to license modified versions of this file under the
35 * terms and conditions of either the GPL or the CDDL or both.
36 *
37 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
38 */
39
40#ifndef VBOX_INCLUDED_vmm_pdmaudioinline_h
41#define VBOX_INCLUDED_vmm_pdmaudioinline_h
42#ifndef RT_WITHOUT_PRAGMA_ONCE
43# pragma once
44#endif
45
46
47/*********************************************************************************************************************************
48* Header Files *
49*********************************************************************************************************************************/
50#include <VBox/err.h>
51#include <VBox/log.h>
52#include <VBox/vmm/pdmaudioifs.h>
53
54#include <iprt/asm.h>
55#include <iprt/asm-math.h>
56#include <iprt/assert.h>
57#include <iprt/mem.h>
58#include <iprt/string.h>
59
60
61/** @defgroup grp_pdm_audio_inline The PDM Audio Helper APIs
62 * @ingroup grp_pdm
63 * @{
64 */
65
66
67/**
68 * Gets the name of an audio direction enum value.
69 *
70 * @returns Pointer to read-only name string on success, "bad" if passed an
71 * invalid enum value.
72 * @param enmDir The audio direction value to name.
73 */
74DECLINLINE(const char *) PDMAudioDirGetName(PDMAUDIODIR enmDir)
75{
76 switch (enmDir)
77 {
78 case PDMAUDIODIR_INVALID: return "invalid";
79 case PDMAUDIODIR_UNKNOWN: return "unknown";
80 case PDMAUDIODIR_IN: return "input";
81 case PDMAUDIODIR_OUT: return "output";
82 case PDMAUDIODIR_DUPLEX: return "duplex";
83
84 /* no default */
85 case PDMAUDIODIR_END:
86 case PDMAUDIODIR_32BIT_HACK:
87 break;
88 }
89 AssertMsgFailedReturn(("Invalid audio direction %d\n", enmDir), "bad");
90}
91
92/**
93 * Gets the name of an audio mixer control enum value.
94 *
95 * @returns Pointer to read-only name, "bad" if invalid input.
96 * @param enmMixerCtl The audio mixer control value.
97 */
98DECLINLINE(const char *) PDMAudioMixerCtlGetName(PDMAUDIOMIXERCTL enmMixerCtl)
99{
100 switch (enmMixerCtl)
101 {
102 case PDMAUDIOMIXERCTL_INVALID: return "Invalid";
103 case PDMAUDIOMIXERCTL_UNKNOWN: return "Unknown";
104 case PDMAUDIOMIXERCTL_VOLUME_MASTER: return "Master Volume";
105 case PDMAUDIOMIXERCTL_FRONT: return "Front";
106 case PDMAUDIOMIXERCTL_CENTER_LFE: return "Center / LFE";
107 case PDMAUDIOMIXERCTL_REAR: return "Rear";
108 case PDMAUDIOMIXERCTL_LINE_IN: return "Line-In";
109 case PDMAUDIOMIXERCTL_MIC_IN: return "Microphone-In";
110
111 /* no default */
112 case PDMAUDIOMIXERCTL_END:
113 case PDMAUDIOMIXERCTL_32BIT_HACK:
114 break;
115 }
116 AssertMsgFailedReturn(("Invalid mixer control %ld\n", enmMixerCtl), "bad");
117}
118
119/**
120 * Gets the name of a path enum value.
121 *
122 * @returns Pointer to read-only name, "bad" if invalid input.
123 * @param enmPath The path value to name.
124 */
125DECLINLINE(const char *) PDMAudioPathGetName(PDMAUDIOPATH enmPath)
126{
127 switch (enmPath)
128 {
129 case PDMAUDIOPATH_INVALID: return "invalid";
130 case PDMAUDIOPATH_UNKNOWN: return "unknown";
131
132 case PDMAUDIOPATH_OUT_FRONT: return "front";
133 case PDMAUDIOPATH_OUT_CENTER_LFE: return "center-lfe";
134 case PDMAUDIOPATH_OUT_REAR: return "rear";
135
136 case PDMAUDIOPATH_IN_MIC: return "mic";
137 case PDMAUDIOPATH_IN_CD: return "cd";
138 case PDMAUDIOPATH_IN_VIDEO: return "video-in";
139 case PDMAUDIOPATH_IN_AUX: return "aux-in";
140 case PDMAUDIOPATH_IN_LINE: return "line-in";
141 case PDMAUDIOPATH_IN_PHONE: return "phone";
142
143 /* no default */
144 case PDMAUDIOPATH_END:
145 case PDMAUDIOPATH_32BIT_HACK:
146 break;
147 }
148 AssertMsgFailedReturn(("Unknown enmPath=%d\n", enmPath), "bad");
149}
150
151/**
152 * Gets the name of a channel.
153 *
154 * @returns Pointer to read-only name, "bad" if invalid input.
155 * @param enmChannelId The channel ID to name.
156 */
157DECLINLINE(const char *) PDMAudioChannelIdGetName(PDMAUDIOCHANNELID enmChannelId)
158{
159 switch (enmChannelId)
160 {
161 case PDMAUDIOCHANNELID_INVALID: return "invalid";
162 case PDMAUDIOCHANNELID_UNUSED_ZERO: return "unused-zero";
163 case PDMAUDIOCHANNELID_UNUSED_SILENCE: return "unused-silence";
164 case PDMAUDIOCHANNELID_UNKNOWN: return "unknown";
165
166 case PDMAUDIOCHANNELID_FRONT_LEFT: return "FL";
167 case PDMAUDIOCHANNELID_FRONT_RIGHT: return "FR";
168 case PDMAUDIOCHANNELID_FRONT_CENTER: return "FC";
169 case PDMAUDIOCHANNELID_LFE: return "LFE";
170 case PDMAUDIOCHANNELID_REAR_LEFT: return "BL";
171 case PDMAUDIOCHANNELID_REAR_RIGHT: return "BR";
172 case PDMAUDIOCHANNELID_FRONT_LEFT_OF_CENTER: return "FLC";
173 case PDMAUDIOCHANNELID_FRONT_RIGHT_OF_CENTER: return "FRC";
174 case PDMAUDIOCHANNELID_REAR_CENTER: return "BC";
175 case PDMAUDIOCHANNELID_SIDE_LEFT: return "SL";
176 case PDMAUDIOCHANNELID_SIDE_RIGHT: return "SR";
177 case PDMAUDIOCHANNELID_TOP_CENTER: return "TC";
178 case PDMAUDIOCHANNELID_FRONT_LEFT_HEIGHT: return "TFL";
179 case PDMAUDIOCHANNELID_FRONT_CENTER_HEIGHT: return "TFC";
180 case PDMAUDIOCHANNELID_FRONT_RIGHT_HEIGHT: return "TFR";
181 case PDMAUDIOCHANNELID_REAR_LEFT_HEIGHT: return "TBL";
182 case PDMAUDIOCHANNELID_REAR_CENTER_HEIGHT: return "TBC";
183 case PDMAUDIOCHANNELID_REAR_RIGHT_HEIGHT: return "TBR";
184
185 /* no default */
186 case PDMAUDIOCHANNELID_END:
187 case PDMAUDIOCHANNELID_32BIT_HACK:
188 break;
189 }
190 AssertMsgFailedReturn(("Unknown enmChannelId=%d\n", enmChannelId), "bad");
191}
192
193
194/*********************************************************************************************************************************
195* Volume Helpers *
196*********************************************************************************************************************************/
197
198/**
199 * Initializes a PDMAUDIOVOLUME structure to max.
200 *
201 * @param pVol The structure to initialize.
202 */
203DECLINLINE(void) PDMAudioVolumeInitMax(PPDMAUDIOVOLUME pVol)
204{
205 pVol->fMuted = false;
206 for (uintptr_t i = 0; i < RT_ELEMENTS(pVol->auChannels); i++)
207 pVol->auChannels[i] = PDMAUDIO_VOLUME_MAX;
208}
209
210
211/**
212 * Initializes a PDMAUDIOVOLUME structure from a simple stereo setting.
213 *
214 * The additional channels will simply be assigned the higer of the two.
215 *
216 * @param pVol The structure to initialize.
217 * @param fMuted Muted.
218 * @param bLeft The left channel volume.
219 * @param bRight The right channel volume.
220 */
221DECLINLINE(void) PDMAudioVolumeInitFromStereo(PPDMAUDIOVOLUME pVol, bool fMuted, uint8_t bLeft, uint8_t bRight)
222{
223 pVol->fMuted = fMuted;
224 pVol->auChannels[0] = bLeft;
225 pVol->auChannels[1] = bRight;
226
227 uint8_t const bOther = RT_MAX(bLeft, bRight);
228 for (uintptr_t i = 2; i < RT_ELEMENTS(pVol->auChannels); i++)
229 pVol->auChannels[i] = bOther;
230}
231
232
233/**
234 * Combines two volume settings (typically master and sink).
235 *
236 * @param pVol Where to return the combined volume
237 * @param pVol1 The first volume settings to combine.
238 * @param pVol2 The second volume settings.
239 */
240DECLINLINE(void) PDMAudioVolumeCombine(PPDMAUDIOVOLUME pVol, PCPDMAUDIOVOLUME pVol1, PCPDMAUDIOVOLUME pVol2)
241{
242 if (pVol1->fMuted || pVol2->fMuted)
243 {
244 pVol->fMuted = true;
245 for (uintptr_t i = 0; i < RT_ELEMENTS(pVol->auChannels); i++)
246 pVol->auChannels[i] = 0;
247 }
248 else
249 {
250 pVol->fMuted = false;
251 /** @todo Very crude implementation for now -- needs more work! (At least
252 * when used in audioMixerSinkUpdateVolume it was considered as such.) */
253 for (uintptr_t i = 0; i < RT_ELEMENTS(pVol->auChannels); i++)
254 {
255#if 0 /* bird: I think the shift variant should produce the exact same result, w/o two conditionals per iteration. */
256 /* 255 * 255 / 255 = 0xFF (255) */
257 /* 17 * 127 / 255 = 8 */
258 /* 39 * 39 / 255 = 5 */
259 pVol->auChannels[i] = (uint8_t)( (RT_MAX(pVol1->auChannels[i], 1U) * RT_MAX(pVol2->auChannels[i], 1U))
260 / PDMAUDIO_VOLUME_MAX);
261#else
262 /* (((255 + 1) * (255 + 1)) >> 8) - 1 = 0xFF (255) */
263 /* ((( 17 + 1) * (127 + 1)) >> 8) - 1 = 0x8 (8) */
264 /* ((( 39 + 1) * ( 39 + 1)) >> 8) - 1 = 0x5 (5) */
265 pVol->auChannels[i] = (uint8_t)((((1U + pVol1->auChannels[i]) * (1U + pVol2->auChannels[i])) >> 8) - 1U);
266#endif
267 }
268 }
269}
270
271
272/*********************************************************************************************************************************
273* PCM Property Helpers *
274*********************************************************************************************************************************/
275
276/**
277 * Assigns default channel IDs according to the channel count.
278 *
279 * The assignments are taken from the standard speaker channel layouts table
280 * in the wikipedia article on surround sound:
281 * https://en.wikipedia.org/wiki/Surround_sound#Standard_speaker_channels
282 */
283DECLINLINE(void) PDMAudioPropsSetDefaultChannelIds(PPDMAUDIOPCMPROPS pProps)
284{
285 unsigned cChannels = pProps->cChannelsX;
286 switch (cChannels)
287 {
288 case 1:
289 pProps->aidChannels[0] = PDMAUDIOCHANNELID_MONO;
290 break;
291 case 2:
292 pProps->aidChannels[0] = PDMAUDIOCHANNELID_FRONT_LEFT;
293 pProps->aidChannels[1] = PDMAUDIOCHANNELID_FRONT_RIGHT;
294 break;
295 case 3: /* 2.1 */
296 pProps->aidChannels[0] = PDMAUDIOCHANNELID_FRONT_LEFT;
297 pProps->aidChannels[1] = PDMAUDIOCHANNELID_FRONT_RIGHT;
298 pProps->aidChannels[2] = PDMAUDIOCHANNELID_LFE;
299 break;
300 case 4: /* 4.0 */
301 pProps->aidChannels[0] = PDMAUDIOCHANNELID_FRONT_LEFT;
302 pProps->aidChannels[1] = PDMAUDIOCHANNELID_FRONT_RIGHT;
303 pProps->aidChannels[2] = PDMAUDIOCHANNELID_REAR_LEFT;
304 pProps->aidChannels[3] = PDMAUDIOCHANNELID_REAR_RIGHT;
305 break;
306 case 5: /* 4.1 */
307 pProps->aidChannels[0] = PDMAUDIOCHANNELID_FRONT_LEFT;
308 pProps->aidChannels[1] = PDMAUDIOCHANNELID_FRONT_RIGHT;
309 pProps->aidChannels[2] = PDMAUDIOCHANNELID_FRONT_CENTER;
310 pProps->aidChannels[3] = PDMAUDIOCHANNELID_LFE;
311 pProps->aidChannels[4] = PDMAUDIOCHANNELID_REAR_CENTER;
312 break;
313 case 6: /* 5.1 */
314 pProps->aidChannels[0] = PDMAUDIOCHANNELID_FRONT_LEFT;
315 pProps->aidChannels[1] = PDMAUDIOCHANNELID_FRONT_RIGHT;
316 pProps->aidChannels[2] = PDMAUDIOCHANNELID_FRONT_CENTER;
317 pProps->aidChannels[3] = PDMAUDIOCHANNELID_LFE;
318 pProps->aidChannels[4] = PDMAUDIOCHANNELID_REAR_LEFT;
319 pProps->aidChannels[5] = PDMAUDIOCHANNELID_REAR_RIGHT;
320 break;
321 case 7: /* 6.1 */
322 pProps->aidChannels[0] = PDMAUDIOCHANNELID_FRONT_LEFT;
323 pProps->aidChannels[1] = PDMAUDIOCHANNELID_FRONT_RIGHT;
324 pProps->aidChannels[2] = PDMAUDIOCHANNELID_FRONT_CENTER;
325 pProps->aidChannels[3] = PDMAUDIOCHANNELID_LFE;
326 pProps->aidChannels[4] = PDMAUDIOCHANNELID_REAR_LEFT;
327 pProps->aidChannels[5] = PDMAUDIOCHANNELID_REAR_RIGHT;
328 pProps->aidChannels[6] = PDMAUDIOCHANNELID_REAR_CENTER;
329 break;
330 case 8: /* 7.1 */
331 pProps->aidChannels[0] = PDMAUDIOCHANNELID_FRONT_LEFT;
332 pProps->aidChannels[1] = PDMAUDIOCHANNELID_FRONT_RIGHT;
333 pProps->aidChannels[2] = PDMAUDIOCHANNELID_FRONT_CENTER;
334 pProps->aidChannels[3] = PDMAUDIOCHANNELID_LFE;
335 pProps->aidChannels[4] = PDMAUDIOCHANNELID_REAR_LEFT;
336 pProps->aidChannels[5] = PDMAUDIOCHANNELID_REAR_RIGHT;
337 pProps->aidChannels[6] = PDMAUDIOCHANNELID_FRONT_LEFT_OF_CENTER;
338 pProps->aidChannels[7] = PDMAUDIOCHANNELID_FRONT_RIGHT_OF_CENTER;
339 break;
340 case 9: /* 9.0 */
341 pProps->aidChannels[0] = PDMAUDIOCHANNELID_FRONT_LEFT;
342 pProps->aidChannels[1] = PDMAUDIOCHANNELID_FRONT_RIGHT;
343 pProps->aidChannels[2] = PDMAUDIOCHANNELID_FRONT_CENTER;
344 pProps->aidChannels[3] = PDMAUDIOCHANNELID_REAR_LEFT;
345 pProps->aidChannels[4] = PDMAUDIOCHANNELID_REAR_RIGHT;
346 pProps->aidChannels[5] = PDMAUDIOCHANNELID_SIDE_LEFT;
347 pProps->aidChannels[6] = PDMAUDIOCHANNELID_SIDE_RIGHT;
348 pProps->aidChannels[7] = PDMAUDIOCHANNELID_FRONT_LEFT_HEIGHT;
349 pProps->aidChannels[8] = PDMAUDIOCHANNELID_FRONT_RIGHT_HEIGHT;
350 break;
351 case 10: /* 9.1 */
352 pProps->aidChannels[0] = PDMAUDIOCHANNELID_FRONT_LEFT;
353 pProps->aidChannels[1] = PDMAUDIOCHANNELID_FRONT_RIGHT;
354 pProps->aidChannels[2] = PDMAUDIOCHANNELID_FRONT_CENTER;
355 pProps->aidChannels[3] = PDMAUDIOCHANNELID_LFE;
356 pProps->aidChannels[4] = PDMAUDIOCHANNELID_REAR_LEFT;
357 pProps->aidChannels[5] = PDMAUDIOCHANNELID_REAR_RIGHT;
358 pProps->aidChannels[6] = PDMAUDIOCHANNELID_SIDE_LEFT;
359 pProps->aidChannels[7] = PDMAUDIOCHANNELID_SIDE_RIGHT;
360 pProps->aidChannels[8] = PDMAUDIOCHANNELID_FRONT_LEFT_HEIGHT;
361 pProps->aidChannels[9] = PDMAUDIOCHANNELID_FRONT_RIGHT_HEIGHT;
362 break;
363 case 11: /* 11.0 */
364 pProps->aidChannels[0] = PDMAUDIOCHANNELID_FRONT_LEFT;
365 pProps->aidChannels[1] = PDMAUDIOCHANNELID_FRONT_RIGHT;
366 pProps->aidChannels[2] = PDMAUDIOCHANNELID_FRONT_CENTER;
367 pProps->aidChannels[3] = PDMAUDIOCHANNELID_REAR_LEFT;
368 pProps->aidChannels[4] = PDMAUDIOCHANNELID_REAR_RIGHT;
369 pProps->aidChannels[5] = PDMAUDIOCHANNELID_FRONT_LEFT_OF_CENTER;
370 pProps->aidChannels[6] = PDMAUDIOCHANNELID_FRONT_RIGHT_OF_CENTER;
371 pProps->aidChannels[7] = PDMAUDIOCHANNELID_SIDE_LEFT;
372 pProps->aidChannels[8] = PDMAUDIOCHANNELID_SIDE_RIGHT;
373 pProps->aidChannels[9] = PDMAUDIOCHANNELID_FRONT_LEFT_HEIGHT;
374 pProps->aidChannels[10]= PDMAUDIOCHANNELID_FRONT_RIGHT_HEIGHT;
375 break;
376 default:
377 AssertFailed();
378 cChannels = 12;
379 RT_FALL_THROUGH();
380 case 12: /* 11.1 */
381 pProps->aidChannels[0] = PDMAUDIOCHANNELID_FRONT_LEFT;
382 pProps->aidChannels[1] = PDMAUDIOCHANNELID_FRONT_RIGHT;
383 pProps->aidChannels[2] = PDMAUDIOCHANNELID_FRONT_CENTER;
384 pProps->aidChannels[3] = PDMAUDIOCHANNELID_LFE;
385 pProps->aidChannels[4] = PDMAUDIOCHANNELID_REAR_LEFT;
386 pProps->aidChannels[5] = PDMAUDIOCHANNELID_REAR_RIGHT;
387 pProps->aidChannels[6] = PDMAUDIOCHANNELID_FRONT_LEFT_OF_CENTER;
388 pProps->aidChannels[7] = PDMAUDIOCHANNELID_FRONT_RIGHT_OF_CENTER;
389 pProps->aidChannels[8] = PDMAUDIOCHANNELID_SIDE_LEFT;
390 pProps->aidChannels[9] = PDMAUDIOCHANNELID_SIDE_RIGHT;
391 pProps->aidChannels[10]= PDMAUDIOCHANNELID_FRONT_LEFT_HEIGHT;
392 pProps->aidChannels[11]= PDMAUDIOCHANNELID_FRONT_RIGHT_HEIGHT;
393 break;
394 case 0:
395 break;
396 }
397 AssertCompile(RT_ELEMENTS(pProps->aidChannels) >= 12);
398
399 while (cChannels < RT_ELEMENTS(pProps->aidChannels))
400 pProps->aidChannels[cChannels++] = PDMAUDIOCHANNELID_INVALID;
401}
402
403
404/**
405 * Initialize PCM audio properties.
406 */
407DECLINLINE(void) PDMAudioPropsInit(PPDMAUDIOPCMPROPS pProps, uint8_t cbSample, bool fSigned, uint8_t cChannels, uint32_t uHz)
408{
409 pProps->cbFrame = cbSample * cChannels;
410 pProps->cbSampleX = cbSample;
411 pProps->cChannelsX = cChannels;
412 pProps->cShiftX = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(cbSample, cChannels);
413 pProps->fSigned = fSigned;
414 pProps->fSwapEndian = false;
415 pProps->fRaw = false;
416 pProps->uHz = uHz;
417
418 Assert(pProps->cbFrame == (uint32_t)cbSample * cChannels);
419 Assert(pProps->cbSampleX == cbSample);
420 Assert(pProps->cChannelsX == cChannels);
421
422 PDMAudioPropsSetDefaultChannelIds(pProps);
423}
424
425/**
426 * Initialize PCM audio properties, extended version.
427 */
428DECLINLINE(void) PDMAudioPropsInitEx(PPDMAUDIOPCMPROPS pProps, uint8_t cbSample, bool fSigned, uint8_t cChannels, uint32_t uHz,
429 bool fLittleEndian, bool fRaw)
430{
431 Assert(!fRaw || cbSample == sizeof(int64_t));
432 pProps->cbFrame = cbSample * cChannels;
433 pProps->cbSampleX = cbSample;
434 pProps->cChannelsX = cChannels;
435 pProps->cShiftX = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(cbSample, cChannels);
436 pProps->fSigned = fSigned;
437#ifdef RT_LITTLE_ENDIAN
438 pProps->fSwapEndian = !fLittleEndian;
439#else
440 pProps->fSwapEndian = fLittleEndian;
441#endif
442 pProps->fRaw = fRaw;
443 pProps->uHz = uHz;
444
445 Assert(pProps->cbFrame == (uint32_t)cbSample * cChannels);
446 Assert(pProps->cbSampleX == cbSample);
447 Assert(pProps->cChannelsX == cChannels);
448
449 PDMAudioPropsSetDefaultChannelIds(pProps);
450}
451
452/**
453 * Modifies the channel count.
454 *
455 * @note This will reset the channel IDs to defaults.
456 *
457 * @param pProps The PCM properties to update.
458 * @param cChannels The new channel count.
459 */
460DECLINLINE(void) PDMAudioPropsSetChannels(PPDMAUDIOPCMPROPS pProps, uint8_t cChannels)
461{
462 Assert(cChannels > 0); Assert(cChannels < 16);
463 pProps->cChannelsX = cChannels;
464 pProps->cbFrame = pProps->cbSampleX * cChannels;
465 pProps->cShiftX = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pProps->cbSampleX, cChannels);
466
467 PDMAudioPropsSetDefaultChannelIds(pProps);
468}
469
470/**
471 * Modifies the sample size.
472 *
473 * @param pProps The PCM properties to update.
474 * @param cbSample The new sample size (in bytes).
475 */
476DECLINLINE(void) PDMAudioPropsSetSampleSize(PPDMAUDIOPCMPROPS pProps, uint8_t cbSample)
477{
478 Assert(cbSample == 1 || cbSample == 2 || cbSample == 4 || cbSample == 8);
479 pProps->cbSampleX = cbSample;
480 pProps->cbFrame = cbSample * pProps->cChannelsX;
481 pProps->cShiftX = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(cbSample, pProps->cChannelsX);
482}
483
484/**
485 * Gets the bitrate.
486 *
487 * Divide the result by 8 to get the byte rate.
488 *
489 * @returns Bit rate.
490 * @param pProps PCM properties to calculate bitrate for.
491 */
492DECLINLINE(uint32_t) PDMAudioPropsGetBitrate(PCPDMAUDIOPCMPROPS pProps)
493{
494 Assert(pProps->cbFrame == pProps->cbSampleX * pProps->cChannelsX);
495 return pProps->cbFrame * pProps->uHz * 8;
496}
497
498/**
499 * Gets the number of channels.
500 * @returns The channel count.
501 * @param pProps The PCM properties.
502 */
503DECL_FORCE_INLINE(uint8_t) PDMAudioPropsChannels(PCPDMAUDIOPCMPROPS pProps)
504{
505 return pProps->cChannelsX;
506}
507
508/**
509 * Gets the sample size in bytes.
510 * @returns Number of bytes per sample.
511 * @param pProps The PCM properties.
512 */
513DECL_FORCE_INLINE(uint8_t) PDMAudioPropsSampleSize(PCPDMAUDIOPCMPROPS pProps)
514{
515 return pProps->cbSampleX;
516}
517
518/**
519 * Gets the sample size in bits.
520 * @returns Number of bits per sample.
521 * @param pProps The PCM properties.
522 */
523DECLINLINE(uint8_t) PDMAudioPropsSampleBits(PCPDMAUDIOPCMPROPS pProps)
524{
525 return pProps->cbSampleX * 8;
526}
527
528/**
529 * Gets the frame size in bytes.
530 * @returns Number of bytes per frame.
531 * @param pProps The PCM properties.
532 */
533DECL_FORCE_INLINE(uint8_t) PDMAudioPropsFrameSize(PCPDMAUDIOPCMPROPS pProps)
534{
535 return pProps->cbFrame;
536}
537
538/**
539 * Gets the frequency.
540 * @returns Frequency.
541 * @param pProps The PCM properties.
542 */
543DECL_FORCE_INLINE(uint32_t) PDMAudioPropsHz(PCPDMAUDIOPCMPROPS pProps)
544{
545 return pProps->uHz;
546}
547
548/**
549 * Checks if the format is signed or unsigned.
550 * @returns true if signed, false if unsigned.
551 * @param pProps The PCM properties.
552 */
553DECL_FORCE_INLINE(bool) PDMAudioPropsIsSigned(PCPDMAUDIOPCMPROPS pProps)
554{
555 return pProps->fSigned;
556}
557
558/**
559 * Checks if the format is little-endian or not.
560 * @returns true if little-endian (or if 8-bit), false if big-endian.
561 * @param pProps The PCM properties.
562 */
563DECL_FORCE_INLINE(bool) PDMAudioPropsIsLittleEndian(PCPDMAUDIOPCMPROPS pProps)
564{
565#ifdef RT_LITTLE_ENDIAN
566 return !pProps->fSwapEndian || pProps->cbSampleX < 2;
567#else
568 return pProps->fSwapEndian || pProps->cbSampleX < 2;
569#endif
570}
571
572/**
573 * Checks if the format is big-endian or not.
574 * @returns true if big-endian (or if 8-bit), false if little-endian.
575 * @param pProps The PCM properties.
576 */
577DECL_FORCE_INLINE(bool) PDMAudioPropsIsBigEndian(PCPDMAUDIOPCMPROPS pProps)
578{
579#ifdef RT_LITTLE_ENDIAN
580 return pProps->fSwapEndian || pProps->cbSampleX < 2;
581#else
582 return !pProps->fSwapEndian || pProps->cbSampleX < 2;
583#endif
584}
585
586/**
587 * Rounds down the given byte amount to the nearest frame boundrary.
588 *
589 * @returns Rounded byte amount.
590 * @param pProps PCM properties to use.
591 * @param cb The size (in bytes) to round.
592 */
593DECLINLINE(uint32_t) PDMAudioPropsFloorBytesToFrame(PCPDMAUDIOPCMPROPS pProps, uint32_t cb)
594{
595 AssertPtrReturn(pProps, 0);
596 return PDMAUDIOPCMPROPS_F2B(pProps, PDMAUDIOPCMPROPS_B2F(pProps, cb));
597}
598
599/**
600 * Rounds up the given byte amount to the nearest frame boundrary.
601 *
602 * @returns Rounded byte amount.
603 * @param pProps PCM properties to use.
604 * @param cb The size (in bytes) to round.
605 */
606DECLINLINE(uint32_t) PDMAudioPropsRoundUpBytesToFrame(PCPDMAUDIOPCMPROPS pProps, uint32_t cb)
607{
608 AssertPtrReturn(pProps, 0);
609 uint32_t const cbFrame = PDMAudioPropsFrameSize(pProps);
610 AssertReturn(cbFrame, 0);
611 return PDMAUDIOPCMPROPS_F2B(pProps, PDMAUDIOPCMPROPS_B2F(pProps, cb + cbFrame - 1));
612}
613
614/**
615 * Checks if the given size is aligned on a frame boundrary.
616 *
617 * @returns @c true if properly aligned, @c false if not.
618 * @param pProps PCM properties to use.
619 * @param cb The size (in bytes) to check.
620 */
621DECLINLINE(bool) PDMAudioPropsIsSizeAligned(PCPDMAUDIOPCMPROPS pProps, uint32_t cb)
622{
623 AssertPtrReturn(pProps, false);
624 uint32_t const cbFrame = PDMAudioPropsFrameSize(pProps);
625 AssertReturn(cbFrame, false);
626 return cb % cbFrame == 0;
627}
628
629/**
630 * Converts bytes to frames (rounding down of course).
631 *
632 * @returns Number of frames.
633 * @param pProps PCM properties to use.
634 * @param cb The number of bytes to convert.
635 */
636DECLINLINE(uint32_t) PDMAudioPropsBytesToFrames(PCPDMAUDIOPCMPROPS pProps, uint32_t cb)
637{
638 AssertPtrReturn(pProps, 0);
639 return PDMAUDIOPCMPROPS_B2F(pProps, cb);
640}
641
642/**
643 * Converts bytes to milliseconds.
644 *
645 * @return Number milliseconds @a cb takes to play or record.
646 * @param pProps PCM properties to use.
647 * @param cb The number of bytes to convert.
648 *
649 * @note Rounds up the result.
650 */
651DECLINLINE(uint64_t) PDMAudioPropsBytesToMilli(PCPDMAUDIOPCMPROPS pProps, uint32_t cb)
652{
653 AssertPtrReturn(pProps, 0);
654
655 /* Check parameters to prevent division by chainsaw: */
656 uint32_t const uHz = pProps->uHz;
657 if (uHz)
658 {
659 const unsigned cbFrame = PDMAudioPropsFrameSize(pProps);
660 if (cbFrame)
661 {
662 /* Round cb up to closest frame size: */
663 cb = (cb + cbFrame - 1) / cbFrame;
664
665 /* Convert to milliseconds. */
666 return (cb * (uint64_t)RT_MS_1SEC + uHz - 1) / uHz;
667 }
668 }
669 return 0;
670}
671
672/**
673 * Converts bytes to microseconds.
674 *
675 * @return Number microseconds @a cb takes to play or record.
676 * @param pProps PCM properties to use.
677 * @param cb The number of bytes to convert.
678 *
679 * @note Rounds up the result.
680 */
681DECLINLINE(uint64_t) PDMAudioPropsBytesToMicro(PCPDMAUDIOPCMPROPS pProps, uint32_t cb)
682{
683 AssertPtrReturn(pProps, 0);
684
685 /* Check parameters to prevent division by chainsaw: */
686 uint32_t const uHz = pProps->uHz;
687 if (uHz)
688 {
689 const unsigned cbFrame = PDMAudioPropsFrameSize(pProps);
690 if (cbFrame)
691 {
692 /* Round cb up to closest frame size: */
693 cb = (cb + cbFrame - 1) / cbFrame;
694
695 /* Convert to microseconds. */
696 return (cb * (uint64_t)RT_US_1SEC + uHz - 1) / uHz;
697 }
698 }
699 return 0;
700}
701
702/**
703 * Converts bytes to nanoseconds.
704 *
705 * @return Number nanoseconds @a cb takes to play or record.
706 * @param pProps PCM properties to use.
707 * @param cb The number of bytes to convert.
708 *
709 * @note Rounds up the result.
710 */
711DECLINLINE(uint64_t) PDMAudioPropsBytesToNano(PCPDMAUDIOPCMPROPS pProps, uint32_t cb)
712{
713 AssertPtrReturn(pProps, 0);
714
715 /* Check parameters to prevent division by chainsaw: */
716 uint32_t const uHz = pProps->uHz;
717 if (uHz)
718 {
719 const unsigned cbFrame = PDMAudioPropsFrameSize(pProps);
720 if (cbFrame)
721 {
722 /* Round cb up to closest frame size: */
723 cb = (cb + cbFrame - 1) / cbFrame;
724
725 /* Convert to nanoseconds. */
726 return (cb * (uint64_t)RT_NS_1SEC + uHz - 1) / uHz;
727 }
728 }
729 return 0;
730}
731
732/**
733 * Converts bytes to nanoseconds, 64-bit version.
734 *
735 * @return Number nanoseconds @a cb takes to play or record.
736 * @param pProps PCM properties to use.
737 * @param cb The number of bytes to convert (64-bit).
738 *
739 * @note Rounds up the result.
740 */
741DECLINLINE(uint64_t) PDMAudioPropsBytesToNano64(PCPDMAUDIOPCMPROPS pProps, uint64_t cb)
742{
743 AssertPtrReturn(pProps, 0);
744
745 /* Check parameters to prevent division by chainsaw: */
746 uint32_t const uHz = pProps->uHz;
747 if (uHz)
748 {
749 const unsigned cbFrame = PDMAudioPropsFrameSize(pProps);
750 if (cbFrame)
751 {
752 /* Round cb up to closest frame size: */
753 cb = (cb + cbFrame - 1) / cbFrame;
754
755 /* Convert to nanoseconds. */
756 return (cb * RT_NS_1SEC + uHz - 1) / uHz;
757 }
758 }
759 return 0;
760}
761
762/**
763 * Converts frames to bytes.
764 *
765 * @returns Number of bytes.
766 * @param pProps The PCM properties to use.
767 * @param cFrames Number of audio frames to convert.
768 * @sa PDMAUDIOPCMPROPS_F2B
769 */
770DECLINLINE(uint32_t) PDMAudioPropsFramesToBytes(PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames)
771{
772 AssertPtrReturn(pProps, 0);
773 return PDMAUDIOPCMPROPS_F2B(pProps, cFrames);
774}
775
776/**
777 * Converts frames to milliseconds.
778 *
779 * @returns milliseconds.
780 * @param pProps The PCM properties to use.
781 * @param cFrames Number of audio frames to convert.
782 * @note No rounding here, result is floored.
783 */
784DECLINLINE(uint64_t) PDMAudioPropsFramesToMilli(PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames)
785{
786 AssertPtrReturn(pProps, 0);
787
788 /* Check input to prevent division by chainsaw: */
789 uint32_t const uHz = pProps->uHz;
790 if (uHz)
791 return ASMMultU32ByU32DivByU32(cFrames, RT_MS_1SEC, uHz);
792 return 0;
793}
794
795/**
796 * Converts frames to milliseconds, but not returning more than @a cMsMax
797 *
798 * This is a convenience for logging and such.
799 *
800 * @returns milliseconds (32-bit).
801 * @param pProps The PCM properties to use.
802 * @param cFrames Number of audio frames to convert.
803 * @param cMsMax Max return value (32-bit).
804 * @note No rounding here, result is floored.
805 */
806DECLINLINE(uint32_t) PDMAudioPropsFramesToMilliMax(PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames, uint32_t cMsMax)
807{
808 AssertPtrReturn(pProps, 0);
809
810 /* Check input to prevent division by chainsaw: */
811 uint32_t const uHz = pProps->uHz;
812 if (uHz)
813 {
814 uint32_t const cMsResult = ASMMultU32ByU32DivByU32(cFrames, RT_MS_1SEC, uHz);
815 return RT_MIN(cMsResult, cMsMax);
816 }
817 return 0;
818}
819
820/**
821 * Converts frames to microseconds.
822 *
823 * @returns microseconds.
824 * @param pProps The PCM properties to use.
825 * @param cFrames Number of audio frames to convert.
826 * @note No rounding here, result is floored.
827 */
828DECLINLINE(uint64_t) PDMAudioPropsFramesToMicro(PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames)
829{
830 AssertPtrReturn(pProps, 0);
831
832 /* Check input to prevent division by chainsaw: */
833 uint32_t const uHz = pProps->uHz;
834 if (uHz)
835 return ASMMultU32ByU32DivByU32(cFrames, RT_US_1SEC, uHz);
836 return 0;
837}
838
839/**
840 * Converts frames to nanoseconds.
841 *
842 * @returns Nanoseconds.
843 * @param pProps The PCM properties to use.
844 * @param cFrames Number of audio frames to convert.
845 * @note No rounding here, result is floored.
846 */
847DECLINLINE(uint64_t) PDMAudioPropsFramesToNano(PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames)
848{
849 AssertPtrReturn(pProps, 0);
850
851 /* Check input to prevent division by chainsaw: */
852 uint32_t const uHz = pProps->uHz;
853 if (uHz)
854 return ASMMultU32ByU32DivByU32(cFrames, RT_NS_1SEC, uHz);
855 return 0;
856}
857
858/**
859 * Converts frames to NT ticks (100 ns units).
860 *
861 * @returns NT ticks.
862 * @param pProps The PCM properties to use.
863 * @param cFrames Number of audio frames to convert.
864 * @note No rounding here, result is floored.
865 */
866DECLINLINE(uint64_t) PDMAudioPropsFramesToNtTicks(PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames)
867{
868 AssertPtrReturn(pProps, 0);
869
870 /* Check input to prevent division by chainsaw: */
871 uint32_t const uHz = pProps->uHz;
872 if (uHz)
873 return ASMMultU32ByU32DivByU32(cFrames, RT_NS_1SEC / 100, uHz);
874 return 0;
875}
876
877/**
878 * Converts milliseconds to frames.
879 *
880 * @returns Number of frames
881 * @param pProps The PCM properties to use.
882 * @param cMs The number of milliseconds to convert.
883 *
884 * @note The result is rounded rather than floored (hysterical raisins).
885 */
886DECLINLINE(uint32_t) PDMAudioPropsMilliToFrames(PCPDMAUDIOPCMPROPS pProps, uint64_t cMs)
887{
888 AssertPtrReturn(pProps, 0);
889
890 uint32_t const uHz = pProps->uHz;
891 uint32_t cFrames;
892 if (cMs < RT_MS_1SEC)
893 cFrames = 0;
894 else
895 {
896 cFrames = cMs / RT_MS_1SEC * uHz;
897 cMs %= RT_MS_1SEC;
898 }
899 cFrames += (ASMMult2xU32RetU64(uHz, (uint32_t)cMs) + RT_MS_1SEC - 1) / RT_MS_1SEC;
900 return cFrames;
901}
902
903/**
904 * Converts milliseconds to bytes.
905 *
906 * @returns Number of bytes (frame aligned).
907 * @param pProps The PCM properties to use.
908 * @param cMs The number of milliseconds to convert.
909 *
910 * @note The result is rounded rather than floored (hysterical raisins).
911 */
912DECLINLINE(uint32_t) PDMAudioPropsMilliToBytes(PCPDMAUDIOPCMPROPS pProps, uint64_t cMs)
913{
914 return PDMAUDIOPCMPROPS_F2B(pProps, PDMAudioPropsMilliToFrames(pProps, cMs));
915}
916
917/**
918 * Converts nanoseconds to frames.
919 *
920 * @returns Number of frames.
921 * @param pProps The PCM properties to use.
922 * @param cNs The number of nanoseconds to convert.
923 *
924 * @note The result is rounded rather than floored (hysterical raisins).
925 */
926DECLINLINE(uint32_t) PDMAudioPropsNanoToFrames(PCPDMAUDIOPCMPROPS pProps, uint64_t cNs)
927{
928 AssertPtrReturn(pProps, 0);
929
930 uint32_t const uHz = pProps->uHz;
931 uint32_t cFrames;
932 if (cNs < RT_NS_1SEC)
933 cFrames = 0;
934 else
935 {
936 cFrames = cNs / RT_NS_1SEC * uHz;
937 cNs %= RT_NS_1SEC;
938 }
939 cFrames += (ASMMult2xU32RetU64(uHz, (uint32_t)cNs) + RT_NS_1SEC - 1) / RT_NS_1SEC;
940 return cFrames;
941}
942
943/**
944 * Converts nanoseconds to frames, 64-bit return.
945 *
946 * @returns Number of frames (64-bit).
947 * @param pProps The PCM properties to use.
948 * @param cNs The number of nanoseconds to convert.
949 *
950 * @note The result is floored!
951 */
952DECLINLINE(uint64_t) PDMAudioPropsNanoToFrames64(PCPDMAUDIOPCMPROPS pProps, uint64_t cNs)
953{
954 AssertPtrReturn(pProps, 0);
955
956 uint32_t const uHz = pProps->uHz;
957 uint64_t cFrames;
958 if (cNs < RT_NS_1SEC)
959 cFrames = 0;
960 else
961 {
962 cFrames = cNs / RT_NS_1SEC * uHz;
963 cNs %= RT_NS_1SEC;
964 }
965 cFrames += ASMMult2xU32RetU64(uHz, (uint32_t)cNs) / RT_NS_1SEC;
966 return cFrames;
967}
968
969/**
970 * Converts nanoseconds to bytes.
971 *
972 * @returns Number of bytes (frame aligned).
973 * @param pProps The PCM properties to use.
974 * @param cNs The number of nanoseconds to convert.
975 *
976 * @note The result is rounded rather than floored (hysterical raisins).
977 */
978DECLINLINE(uint32_t) PDMAudioPropsNanoToBytes(PCPDMAUDIOPCMPROPS pProps, uint64_t cNs)
979{
980 return PDMAUDIOPCMPROPS_F2B(pProps, PDMAudioPropsNanoToFrames(pProps, cNs));
981}
982
983/**
984 * Converts nanoseconds to bytes, 64-bit version.
985 *
986 * @returns Number of bytes (frame aligned), 64-bit.
987 * @param pProps The PCM properties to use.
988 * @param cNs The number of nanoseconds to convert.
989 *
990 * @note The result is floored.
991 */
992DECLINLINE(uint64_t) PDMAudioPropsNanoToBytes64(PCPDMAUDIOPCMPROPS pProps, uint64_t cNs)
993{
994 return PDMAUDIOPCMPROPS_F2B(pProps, PDMAudioPropsNanoToFrames(pProps, cNs));
995}
996
997/**
998 * Clears a sample buffer by the given amount of audio frames with silence (according to the format
999 * given by the PCM properties).
1000 *
1001 * @param pProps The PCM properties to apply.
1002 * @param pvBuf The buffer to clear.
1003 * @param cbBuf The buffer size in bytes.
1004 * @param cFrames The number of audio frames to clear. Capped at @a cbBuf
1005 * if exceeding the buffer. If the size is an unaligned
1006 * number of frames, the extra bytes may be left
1007 * uninitialized in some configurations.
1008 */
1009DECLINLINE(void) PDMAudioPropsClearBuffer(PCPDMAUDIOPCMPROPS pProps, void *pvBuf, size_t cbBuf, uint32_t cFrames)
1010{
1011 /*
1012 * Validate input
1013 */
1014 AssertPtrReturnVoid(pProps);
1015 Assert(pProps->cbSampleX);
1016 if (!cbBuf || !cFrames)
1017 return;
1018 AssertPtrReturnVoid(pvBuf);
1019
1020 /*
1021 * Decide how much needs clearing.
1022 */
1023 size_t cbToClear = PDMAudioPropsFramesToBytes(pProps, cFrames);
1024 AssertStmt(cbToClear <= cbBuf, cbToClear = cbBuf);
1025
1026 Log2Func(("pProps=%p, pvBuf=%p, cFrames=%RU32, fSigned=%RTbool, cbSample=%RU8\n",
1027 pProps, pvBuf, cFrames, pProps->fSigned, pProps->cbSampleX));
1028
1029 /*
1030 * Do the job.
1031 */
1032 if (pProps->fSigned)
1033 RT_BZERO(pvBuf, cbToClear);
1034 else /* Unsigned formats. */
1035 {
1036 switch (pProps->cbSampleX)
1037 {
1038 case 1: /* 8 bit */
1039 memset(pvBuf, 0x80, cbToClear);
1040 break;
1041
1042 case 2: /* 16 bit */
1043 {
1044 uint16_t *pu16Dst = (uint16_t *)pvBuf;
1045 uint16_t const u16Offset = !pProps->fSwapEndian ? UINT16_C(0x8000) : UINT16_C(0x80);
1046 cbBuf /= sizeof(*pu16Dst);
1047 while (cbBuf-- > 0)
1048 *pu16Dst++ = u16Offset;
1049 break;
1050 }
1051
1052 case 4: /* 32 bit */
1053 ASMMemFill32(pvBuf, cbToClear & ~(size_t)(sizeof(uint32_t) - 1),
1054 !pProps->fSwapEndian ? UINT32_C(0x80000000) : UINT32_C(0x80));
1055 break;
1056
1057 default:
1058 AssertMsgFailed(("Invalid bytes per sample: %RU8\n", pProps->cbSampleX));
1059 }
1060 }
1061}
1062
1063/**
1064 * Checks if the given buffer is silence.
1065 *
1066 * @param pProps The PCM properties to use checking the buffer.
1067 * @param pvBuf The buffer to check.
1068 * @param cbBuf The number of bytes to check (must be frame aligned).
1069 */
1070DECLINLINE(bool) PDMAudioPropsIsBufferSilence(PCPDMAUDIOPCMPROPS pProps, void const *pvBuf, size_t cbBuf)
1071{
1072 /*
1073 * Validate input
1074 */
1075 AssertPtrReturn(pProps, false);
1076 if (!cbBuf)
1077 return false;
1078 AssertPtrReturn(pvBuf, false);
1079
1080 /*
1081 * Do the job.
1082 */
1083 if (pProps->fSigned)
1084 return ASMMemIsZero(pvBuf, cbBuf);
1085
1086 switch (pProps->cbSampleX)
1087 {
1088 case 1: /* 8 bit */
1089 return ASMMemIsAllU8(pvBuf, cbBuf, 0x80);
1090
1091 case 2: /* 16 bit */
1092 {
1093 uint16_t const *pu16 = (uint16_t const *)pvBuf;
1094 uint16_t const u16Offset = !pProps->fSwapEndian ? UINT16_C(0x8000) : UINT16_C(0x80);
1095 cbBuf /= sizeof(*pu16);
1096 while (cbBuf-- > 0)
1097 if (*pu16 != u16Offset)
1098 return false;
1099 return true;
1100 }
1101
1102 case 4: /* 32 bit */
1103 {
1104 uint32_t const *pu32 = (uint32_t const *)pvBuf;
1105 uint32_t const u32Offset = !pProps->fSwapEndian ? UINT32_C(0x80000000) : UINT32_C(0x80);
1106 cbBuf /= sizeof(*pu32);
1107 while (cbBuf-- > 0)
1108 if (*pu32 != u32Offset)
1109 return false;
1110 return true;
1111 }
1112
1113 default:
1114 AssertMsgFailed(("Invalid bytes per sample: %RU8\n", pProps->cbSampleX));
1115 return false;
1116 }
1117}
1118
1119/**
1120 * Compares two sets of PCM properties.
1121 *
1122 * @returns @c true if the same, @c false if not.
1123 * @param pProps1 The first set of properties to compare.
1124 * @param pProps2 The second set of properties to compare.
1125 */
1126DECLINLINE(bool) PDMAudioPropsAreEqual(PCPDMAUDIOPCMPROPS pProps1, PCPDMAUDIOPCMPROPS pProps2)
1127{
1128 uintptr_t idxCh;
1129 AssertPtrReturn(pProps1, false);
1130 AssertPtrReturn(pProps2, false);
1131
1132 if (pProps1 == pProps2) /* If the pointers match, take a shortcut. */
1133 return true;
1134
1135 if (pProps1->uHz != pProps2->uHz)
1136 return false;
1137 if (pProps1->cChannelsX != pProps2->cChannelsX)
1138 return false;
1139 if (pProps1->cbSampleX != pProps2->cbSampleX)
1140 return false;
1141 if (pProps1->fSigned != pProps2->fSigned)
1142 return false;
1143 if (pProps1->fSwapEndian != pProps2->fSwapEndian)
1144 return false;
1145 if (pProps1->fRaw != pProps2->fRaw)
1146 return false;
1147
1148 idxCh = pProps1->cChannelsX;
1149 while (idxCh-- > 0)
1150 if (pProps1->aidChannels[idxCh] != pProps2->aidChannels[idxCh])
1151 return false;
1152
1153 return true;
1154}
1155
1156/**
1157 * Checks whether the given PCM properties are valid or not.
1158 *
1159 * @returns true/false accordingly.
1160 * @param pProps The PCM properties to check.
1161 *
1162 * @remarks This just performs a generic check of value ranges.
1163 *
1164 * @sa PDMAudioStrmCfgIsValid
1165 */
1166DECLINLINE(bool) PDMAudioPropsAreValid(PCPDMAUDIOPCMPROPS pProps)
1167{
1168 AssertPtrReturn(pProps, false);
1169
1170 /* Channels. */
1171 if ( pProps->cChannelsX != 0
1172 && pProps->cChannelsX <= PDMAUDIO_MAX_CHANNELS
1173 /* Sample size. */
1174 && ( pProps->cbSampleX == 1
1175 || pProps->cbSampleX == 2
1176 || pProps->cbSampleX == 4
1177 || (pProps->cbSampleX == 8 && pProps->fRaw))
1178 /* Hertz rate. */
1179 && pProps->uHz >= 1000
1180 && pProps->uHz < 1000000
1181 /* Raw format: Here we only support int64_t as sample size currently, if enabled. */
1182 && ( !pProps->fRaw
1183 || (pProps->fSigned && pProps->cbSampleX == sizeof(int64_t)))
1184 )
1185 {
1186 /* A few more sanity checks to see if the structure has been properly initialized (via PDMAudioPropsInit[Ex]). */
1187 AssertMsgReturn(pProps->cShiftX == PDMAUDIOPCMPROPS_MAKE_SHIFT(pProps),
1188 ("cShift=%u cbSample=%u cChannels=%u\n", pProps->cShiftX, pProps->cbSampleX, pProps->cChannelsX),
1189 false);
1190 AssertMsgReturn(pProps->cbFrame == pProps->cbSampleX * pProps->cChannelsX,
1191 ("cbFrame=%u cbSample=%u cChannels=%u\n", pProps->cbFrame, pProps->cbSampleX, pProps->cChannelsX),
1192 false);
1193
1194 return true;
1195 }
1196
1197 return false;
1198}
1199
1200/**
1201 * Get number of bytes per frame.
1202 *
1203 * @returns Number of bytes per audio frame.
1204 * @param pProps PCM properties to use.
1205 * @sa PDMAUDIOPCMPROPS_F2B
1206 */
1207DECLINLINE(uint32_t) PDMAudioPropsBytesPerFrame(PCPDMAUDIOPCMPROPS pProps)
1208{
1209 return PDMAUDIOPCMPROPS_F2B(pProps, 1 /*cFrames*/);
1210}
1211
1212/**
1213 * Prints PCM properties to the debug log.
1214 *
1215 * @param pProps PCM properties to use.
1216 */
1217DECLINLINE(void) PDMAudioPropsLog(PCPDMAUDIOPCMPROPS pProps)
1218{
1219 AssertPtrReturnVoid(pProps);
1220
1221 Log(("uHz=%RU32, cChannels=%RU8, cBits=%RU8%s",
1222 pProps->uHz, pProps->cChannelsX, pProps->cbSampleX * 8, pProps->fSigned ? "S" : "U"));
1223}
1224
1225/** Max necessary buffer space for PDMAudioPropsToString */
1226#define PDMAUDIOPROPSTOSTRING_MAX sizeof("16ch S64 4294967296Hz swap raw")
1227
1228/**
1229 * Formats the PCM audio properties into a string buffer.
1230 *
1231 * @returns pszDst
1232 * @param pProps PCM properties to use.
1233 * @param pszDst The destination buffer.
1234 * @param cchDst The size of the destination buffer. Recommended to be at
1235 * least PDMAUDIOPROPSTOSTRING_MAX bytes.
1236 */
1237DECLINLINE(char *) PDMAudioPropsToString(PCPDMAUDIOPCMPROPS pProps, char *pszDst, size_t cchDst)
1238{
1239 /* 2ch S64 44100Hz swap raw */
1240 RTStrPrintf(pszDst, cchDst, "%uch %c%u %RU32Hz%s%s",
1241 PDMAudioPropsChannels(pProps), PDMAudioPropsIsSigned(pProps) ? 'S' : 'U', PDMAudioPropsSampleBits(pProps),
1242 PDMAudioPropsHz(pProps), pProps->fSwapEndian ? " swap" : "", pProps->fRaw ? " raw" : "");
1243 return pszDst;
1244}
1245
1246
1247/*********************************************************************************************************************************
1248* Stream Configuration Helpers *
1249*********************************************************************************************************************************/
1250
1251/**
1252 * Initializes a stream configuration from PCM properties.
1253 *
1254 * @returns VBox status code.
1255 * @param pCfg The stream configuration to initialize.
1256 * @param pProps The PCM properties to use.
1257 */
1258DECLINLINE(int) PDMAudioStrmCfgInitWithProps(PPDMAUDIOSTREAMCFG pCfg, PCPDMAUDIOPCMPROPS pProps)
1259{
1260 AssertPtrReturn(pProps, VERR_INVALID_POINTER);
1261 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
1262
1263 RT_ZERO(*pCfg);
1264 pCfg->Backend.cFramesPreBuffering = UINT32_MAX; /* Explicitly set to "undefined". */
1265
1266 memcpy(&pCfg->Props, pProps, sizeof(PDMAUDIOPCMPROPS));
1267
1268 return VINF_SUCCESS;
1269}
1270
1271/**
1272 * Checks whether stream configuration matches the given PCM properties.
1273 *
1274 * @returns @c true if equal, @c false if not.
1275 * @param pCfg The stream configuration.
1276 * @param pProps The PCM properties to match with.
1277 */
1278DECLINLINE(bool) PDMAudioStrmCfgMatchesProps(PCPDMAUDIOSTREAMCFG pCfg, PCPDMAUDIOPCMPROPS pProps)
1279{
1280 AssertPtrReturn(pCfg, false);
1281 return PDMAudioPropsAreEqual(pProps, &pCfg->Props);
1282}
1283
1284/**
1285 * Checks whether two stream configuration matches.
1286 *
1287 * @returns @c true if equal, @c false if not.
1288 * @param pCfg1 The first stream configuration.
1289 * @param pCfg2 The second stream configuration.
1290 */
1291DECLINLINE(bool) PDMAudioStrmCfgEquals(PCPDMAUDIOSTREAMCFG pCfg1, PCPDMAUDIOSTREAMCFG pCfg2)
1292{
1293 if (!pCfg1 || !pCfg2)
1294 return false;
1295 if (pCfg1 == pCfg2)
1296 return pCfg1 != NULL;
1297 if (PDMAudioPropsAreEqual(&pCfg1->Props, &pCfg2->Props))
1298 return pCfg1->enmDir == pCfg2->enmDir
1299 && pCfg1->enmPath == pCfg2->enmPath
1300 && pCfg1->Device.cMsSchedulingHint == pCfg2->Device.cMsSchedulingHint
1301 && pCfg1->Backend.cFramesPeriod == pCfg2->Backend.cFramesPeriod
1302 && pCfg1->Backend.cFramesBufferSize == pCfg2->Backend.cFramesBufferSize
1303 && pCfg1->Backend.cFramesPreBuffering == pCfg2->Backend.cFramesPreBuffering
1304 && strcmp(pCfg1->szName, pCfg2->szName) == 0;
1305 return false;
1306}
1307
1308/**
1309 * Frees an audio stream allocated by PDMAudioStrmCfgDup().
1310 *
1311 * @param pCfg The stream configuration to free.
1312 */
1313DECLINLINE(void) PDMAudioStrmCfgFree(PPDMAUDIOSTREAMCFG pCfg)
1314{
1315 if (pCfg)
1316 RTMemFree(pCfg);
1317}
1318
1319/**
1320 * Checks whether the given stream configuration is valid or not.
1321 *
1322 * @returns true/false accordingly.
1323 * @param pCfg Stream configuration to check.
1324 *
1325 * @remarks This just performs a generic check of value ranges. Further, it
1326 * will assert if the input is invalid.
1327 *
1328 * @sa PDMAudioPropsAreValid
1329 */
1330DECLINLINE(bool) PDMAudioStrmCfgIsValid(PCPDMAUDIOSTREAMCFG pCfg)
1331{
1332 AssertPtrReturn(pCfg, false);
1333 AssertMsgReturn(pCfg->enmDir >= PDMAUDIODIR_UNKNOWN && pCfg->enmDir < PDMAUDIODIR_END, ("%d\n", pCfg->enmDir), false);
1334 return PDMAudioPropsAreValid(&pCfg->Props);
1335}
1336
1337/**
1338 * Copies one stream configuration to another.
1339 *
1340 * @returns VBox status code.
1341 * @param pDstCfg The destination stream configuration.
1342 * @param pSrcCfg The source stream configuration.
1343 */
1344DECLINLINE(int) PDMAudioStrmCfgCopy(PPDMAUDIOSTREAMCFG pDstCfg, PCPDMAUDIOSTREAMCFG pSrcCfg)
1345{
1346 AssertPtrReturn(pDstCfg, VERR_INVALID_POINTER);
1347 AssertPtrReturn(pSrcCfg, VERR_INVALID_POINTER);
1348
1349 /* This used to be VBOX_STRICT only and return VERR_INVALID_PARAMETER, but
1350 that's making release builds work differently from debug & strict builds,
1351 which is a terrible idea: */
1352 Assert(PDMAudioStrmCfgIsValid(pSrcCfg));
1353
1354 memcpy(pDstCfg, pSrcCfg, sizeof(PDMAUDIOSTREAMCFG));
1355
1356 return VINF_SUCCESS;
1357}
1358
1359/**
1360 * Duplicates an audio stream configuration.
1361 *
1362 * @returns Pointer to duplicate on success, NULL on failure. Must be freed
1363 * using PDMAudioStrmCfgFree().
1364 *
1365 * @param pCfg The audio stream configuration to duplicate.
1366 */
1367DECLINLINE(PPDMAUDIOSTREAMCFG) PDMAudioStrmCfgDup(PCPDMAUDIOSTREAMCFG pCfg)
1368{
1369 AssertPtrReturn(pCfg, NULL);
1370
1371 PPDMAUDIOSTREAMCFG pDst = (PPDMAUDIOSTREAMCFG)RTMemAllocZ(sizeof(PDMAUDIOSTREAMCFG));
1372 if (pDst)
1373 {
1374 int rc = PDMAudioStrmCfgCopy(pDst, pCfg);
1375 if (RT_SUCCESS(rc))
1376 return pDst;
1377
1378 PDMAudioStrmCfgFree(pDst);
1379 }
1380 return NULL;
1381}
1382
1383/**
1384 * Logs an audio stream configuration.
1385 *
1386 * @param pCfg The stream configuration to log.
1387 */
1388DECLINLINE(void) PDMAudioStrmCfgLog(PCPDMAUDIOSTREAMCFG pCfg)
1389{
1390 if (pCfg)
1391 LogFunc(("szName=%s enmDir=%RU32 uHz=%RU32 cBits=%RU8%s cChannels=%RU8\n", pCfg->szName, pCfg->enmDir,
1392 pCfg->Props.uHz, pCfg->Props.cbSampleX * 8, pCfg->Props.fSigned ? "S" : "U", pCfg->Props.cChannelsX));
1393}
1394
1395/**
1396 * Converts a stream command enum value to a string.
1397 *
1398 * @returns Pointer to read-only stream command name on success,
1399 * "bad" if invalid command value.
1400 * @param enmCmd The stream command to name.
1401 */
1402DECLINLINE(const char *) PDMAudioStrmCmdGetName(PDMAUDIOSTREAMCMD enmCmd)
1403{
1404 switch (enmCmd)
1405 {
1406 case PDMAUDIOSTREAMCMD_INVALID: return "Invalid";
1407 case PDMAUDIOSTREAMCMD_ENABLE: return "Enable";
1408 case PDMAUDIOSTREAMCMD_DISABLE: return "Disable";
1409 case PDMAUDIOSTREAMCMD_PAUSE: return "Pause";
1410 case PDMAUDIOSTREAMCMD_RESUME: return "Resume";
1411 case PDMAUDIOSTREAMCMD_DRAIN: return "Drain";
1412 case PDMAUDIOSTREAMCMD_END:
1413 case PDMAUDIOSTREAMCMD_32BIT_HACK:
1414 break;
1415 /* no default! */
1416 }
1417 AssertMsgFailedReturn(("Invalid stream command %d\n", enmCmd), "bad");
1418}
1419
1420/** Max necessary buffer space for PDMAudioStrmCfgToString */
1421#define PDMAUDIOSTRMCFGTOSTRING_MAX \
1422 sizeof("'01234567890123456789012345678901234567890123456789012345678901234' unknown 16ch S64 4294967295Hz swap raw, 9999999ms buffer, 9999999ms period, 9999999ms pre-buffer, 4294967295ms sched, center-lfe")
1423
1424/**
1425 * Formats an audio stream configuration.
1426 *
1427 * @param pCfg The stream configuration to stringify.
1428 * @param pszDst The destination buffer.
1429 * @param cbDst The size of the destination buffer. Recommend this be
1430 * at least PDMAUDIOSTRMCFGTOSTRING_MAX bytes.
1431 */
1432DECLINLINE(const char *) PDMAudioStrmCfgToString(PCPDMAUDIOSTREAMCFG pCfg, char *pszDst, size_t cbDst)
1433{
1434 /* 'front' output 2ch 44100Hz raw, 300ms buffer, 75ms period, 150ms pre-buffer, 10ms sched */
1435 RTStrPrintf(pszDst, cbDst,
1436 "'%s' %s %uch %c%u %RU32Hz%s%s, %RU32ms buffer, %RU32ms period, %RU32ms pre-buffer, %RU32ms sched%s%s",
1437 pCfg->szName, PDMAudioDirGetName(pCfg->enmDir), PDMAudioPropsChannels(&pCfg->Props),
1438 PDMAudioPropsIsSigned(&pCfg->Props) ? 'S' : 'U', PDMAudioPropsSampleBits(&pCfg->Props),
1439 PDMAudioPropsHz(&pCfg->Props), pCfg->Props.fSwapEndian ? " swap" : "", pCfg->Props.fRaw ? " raw" : "",
1440 PDMAudioPropsFramesToMilliMax(&pCfg->Props, pCfg->Backend.cFramesBufferSize, 9999999),
1441 PDMAudioPropsFramesToMilliMax(&pCfg->Props, pCfg->Backend.cFramesPeriod, 9999999),
1442 PDMAudioPropsFramesToMilliMax(&pCfg->Props, pCfg->Backend.cFramesPreBuffering, 9999999),
1443 pCfg->Device.cMsSchedulingHint,
1444 pCfg->enmPath == PDMAUDIOPATH_UNKNOWN ? "" : ", ",
1445 pCfg->enmPath == PDMAUDIOPATH_UNKNOWN ? "" : PDMAudioPathGetName(pCfg->enmPath) );
1446 return pszDst;
1447}
1448
1449
1450/*********************************************************************************************************************************
1451* Stream Status Helpers *
1452*********************************************************************************************************************************/
1453
1454/**
1455 * Converts a audio stream state enum value to a string.
1456 *
1457 * @returns Pointer to read-only audio stream state string on success,
1458 * "illegal" if invalid command value.
1459 * @param enmStreamState The state to convert.
1460 */
1461DECLINLINE(const char *) PDMAudioStreamStateGetName(PDMAUDIOSTREAMSTATE enmStreamState)
1462{
1463 switch (enmStreamState)
1464 {
1465 case PDMAUDIOSTREAMSTATE_INVALID: return "invalid";
1466 case PDMAUDIOSTREAMSTATE_NOT_WORKING: return "not-working";
1467 case PDMAUDIOSTREAMSTATE_NEED_REINIT: return "need-reinit";
1468 case PDMAUDIOSTREAMSTATE_INACTIVE: return "inactive";
1469 case PDMAUDIOSTREAMSTATE_ENABLED: return "enabled";
1470 case PDMAUDIOSTREAMSTATE_ENABLED_READABLE: return "enabled-readable";
1471 case PDMAUDIOSTREAMSTATE_ENABLED_WRITABLE: return "enabled-writable";
1472 /* no default: */
1473 case PDMAUDIOSTREAMSTATE_END:
1474 case PDMAUDIOSTREAMSTATE_32BIT_HACK:
1475 break;
1476 }
1477 AssertMsgFailedReturn(("Invalid audio stream state: %d\n", enmStreamState), "illegal");
1478}
1479
1480/**
1481 * Converts a host audio (backend) stream state enum value to a string.
1482 *
1483 * @returns Pointer to read-only host audio stream state string on success,
1484 * "illegal" if invalid command value.
1485 * @param enmHostAudioStreamState The state to convert.
1486 */
1487DECLINLINE(const char *) PDMHostAudioStreamStateGetName(PDMHOSTAUDIOSTREAMSTATE enmHostAudioStreamState)
1488{
1489 switch (enmHostAudioStreamState)
1490 {
1491 case PDMHOSTAUDIOSTREAMSTATE_INVALID: return "invalid";
1492 case PDMHOSTAUDIOSTREAMSTATE_INITIALIZING: return "initializing";
1493 case PDMHOSTAUDIOSTREAMSTATE_NOT_WORKING: return "not-working";
1494 case PDMHOSTAUDIOSTREAMSTATE_OKAY: return "okay";
1495 case PDMHOSTAUDIOSTREAMSTATE_DRAINING: return "draining";
1496 case PDMHOSTAUDIOSTREAMSTATE_INACTIVE: return "inactive";
1497 /* no default: */
1498 case PDMHOSTAUDIOSTREAMSTATE_END:
1499 case PDMHOSTAUDIOSTREAMSTATE_32BIT_HACK:
1500 break;
1501 }
1502 AssertMsgFailedReturn(("Invalid host audio stream state: %d\n", enmHostAudioStreamState), "illegal");
1503}
1504
1505/** @} */
1506
1507#endif /* !VBOX_INCLUDED_vmm_pdmaudioinline_h */
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