VirtualBox

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

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

DrvHostAudioWasApi: Early WASAPI backend. bugref:9890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 36.3 KB
Line 
1/* $Id: pdmaudioinline.h 88626 2021-04-21 09:58:08Z 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-2020 Oracle Corporation
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.virtualbox.org. This file is free software;
14 * you can redistribute it and/or modify it under the terms of the GNU
15 * General Public License (GPL) as published by the Free Software
16 * Foundation, in version 2 as it comes in the "COPYING" file of the
17 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 *
20 * The contents of this file may alternatively be used under the terms
21 * of the Common Development and Distribution License Version 1.0
22 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
23 * VirtualBox OSE distribution, in which case the provisions of the
24 * CDDL are applicable instead of those of the GPL.
25 *
26 * You may elect to license modified versions of this file under the
27 * terms and conditions of either the GPL or the CDDL or both.
28 */
29
30#ifndef VBOX_INCLUDED_vmm_pdmaudioinline_h
31#define VBOX_INCLUDED_vmm_pdmaudioinline_h
32#ifndef RT_WITHOUT_PRAGMA_ONCE
33# pragma once
34#endif
35
36
37/*********************************************************************************************************************************
38* Header Files *
39*********************************************************************************************************************************/
40#include <VBox/err.h>
41#include <VBox/log.h>
42#include <VBox/vmm/pdmaudioifs.h>
43
44#include <iprt/asm.h>
45#include <iprt/asm-math.h>
46#include <iprt/assert.h>
47#include <iprt/mem.h>
48#include <iprt/string.h>
49
50
51/** @defgroup grp_pdm_audio_inline The PDM Audio Helper APIs
52 * @ingroup grp_pdm
53 * @{
54 */
55
56/* Fix later: */
57DECLINLINE(bool) PDMAudioPropsAreValid(PCPDMAUDIOPCMPROPS pProps);
58DECLINLINE(bool) PDMAudioPropsAreEqual(PCPDMAUDIOPCMPROPS pProps1, PCPDMAUDIOPCMPROPS pProps2);
59
60
61
62/**
63 * Gets the name of an audio direction enum value.
64 *
65 * @returns Pointer to read-only name string on success, "bad" if
66 * passed an invalid enum value.
67 * @param enmDir The audio direction value to name.
68 */
69DECLINLINE(const char *) PDMAudioDirGetName(PDMAUDIODIR enmDir)
70{
71 switch (enmDir)
72 {
73 case PDMAUDIODIR_UNKNOWN: return "Unknown";
74 case PDMAUDIODIR_IN: return "Input";
75 case PDMAUDIODIR_OUT: return "Output";
76 case PDMAUDIODIR_DUPLEX: return "Duplex";
77
78 /* no default */
79 case PDMAUDIODIR_END:
80 case PDMAUDIODIR_INVALID:
81 case PDMAUDIODIR_32BIT_HACK:
82 break;
83 }
84 AssertMsgFailedReturn(("Invalid audio direction %d\n", enmDir), "bad");
85}
86
87/**
88 * Gets the name of an audio mixer control enum value.
89 *
90 * @returns Pointer to read-only name, "bad" if invalid input.
91 * @param enmMixerCtl The audio mixer control value.
92 */
93DECLINLINE(const char *) PDMAudioMixerCtlGetName(PDMAUDIOMIXERCTL enmMixerCtl)
94{
95 switch (enmMixerCtl)
96 {
97 case PDMAUDIOMIXERCTL_UNKNOWN: return "Unknown";
98 case PDMAUDIOMIXERCTL_VOLUME_MASTER: return "Master Volume";
99 case PDMAUDIOMIXERCTL_FRONT: return "Front";
100 case PDMAUDIOMIXERCTL_CENTER_LFE: return "Center / LFE";
101 case PDMAUDIOMIXERCTL_REAR: return "Rear";
102 case PDMAUDIOMIXERCTL_LINE_IN: return "Line-In";
103 case PDMAUDIOMIXERCTL_MIC_IN: return "Microphone-In";
104 /* no default */
105 case PDMAUDIOMIXERCTL_END:
106 case PDMAUDIOMIXERCTL_INVALID:
107 case PDMAUDIOMIXERCTL_32BIT_HACK:
108 break;
109 }
110 AssertMsgFailedReturn(("Invalid mixer control %ld\n", enmMixerCtl), "bad");
111}
112
113/**
114 * Gets the name of a playback destination enum value.
115 *
116 * @returns Pointer to read-only name, "bad" if invalid input.
117 * @param enmPlaybackDst The playback destination value.
118 */
119DECLINLINE(const char *) PDMAudioPlaybackDstGetName(PDMAUDIOPLAYBACKDST enmPlaybackDst)
120{
121 switch (enmPlaybackDst)
122 {
123 case PDMAUDIOPLAYBACKDST_UNKNOWN: return "Unknown";
124 case PDMAUDIOPLAYBACKDST_FRONT: return "Front";
125 case PDMAUDIOPLAYBACKDST_CENTER_LFE: return "Center / LFE";
126 case PDMAUDIOPLAYBACKDST_REAR: return "Rear";
127 /* no default */
128 case PDMAUDIOPLAYBACKDST_INVALID:
129 case PDMAUDIOPLAYBACKDST_END:
130 case PDMAUDIOPLAYBACKDST_32BIT_HACK:
131 break;
132 }
133 AssertMsgFailedReturn(("Invalid playback destination %ld\n", enmPlaybackDst), "bad");
134}
135
136/**
137 * Gets the name of a recording source enum value.
138 *
139 * @returns Pointer to read-only name, "bad" if invalid input.
140 * @param enmRecSrc The recording source value.
141 */
142DECLINLINE(const char *) PDMAudioRecSrcGetName(PDMAUDIORECSRC enmRecSrc)
143{
144 switch (enmRecSrc)
145 {
146 case PDMAUDIORECSRC_UNKNOWN: return "Unknown";
147 case PDMAUDIORECSRC_MIC: return "Microphone In";
148 case PDMAUDIORECSRC_CD: return "CD";
149 case PDMAUDIORECSRC_VIDEO: return "Video";
150 case PDMAUDIORECSRC_AUX: return "AUX";
151 case PDMAUDIORECSRC_LINE: return "Line In";
152 case PDMAUDIORECSRC_PHONE: return "Phone";
153 /* no default */
154 case PDMAUDIORECSRC_END:
155 case PDMAUDIORECSRC_32BIT_HACK:
156 break;
157 }
158 AssertMsgFailedReturn(("Invalid recording source %ld\n", enmRecSrc), "bad");
159}
160
161/**
162 * Checks whether the audio format is signed.
163 *
164 * @returns @c true for signed format, @c false for unsigned.
165 * @param enmFmt The audio format.
166 */
167DECLINLINE(bool) PDMAudioFormatIsSigned(PDMAUDIOFMT enmFmt)
168{
169 switch (enmFmt)
170 {
171 case PDMAUDIOFMT_S8:
172 case PDMAUDIOFMT_S16:
173 case PDMAUDIOFMT_S32:
174 return true;
175
176 case PDMAUDIOFMT_U8:
177 case PDMAUDIOFMT_U16:
178 case PDMAUDIOFMT_U32:
179 return false;
180
181 /* no default */
182 case PDMAUDIOFMT_INVALID:
183 case PDMAUDIOFMT_END:
184 case PDMAUDIOFMT_32BIT_HACK:
185 break;
186 }
187 AssertMsgFailedReturn(("Bogus audio format %ld\n", enmFmt), false);
188}
189
190/**
191 * Gets the encoding width in bits of the give audio format.
192 *
193 * @returns Bit count. 0 if invalid input.
194 * @param enmFmt The audio format.
195 */
196DECLINLINE(uint8_t) PDMAudioFormatGetBits(PDMAUDIOFMT enmFmt)
197{
198 switch (enmFmt)
199 {
200 case PDMAUDIOFMT_S8:
201 case PDMAUDIOFMT_U8:
202 return 8;
203
204 case PDMAUDIOFMT_U16:
205 case PDMAUDIOFMT_S16:
206 return 16;
207
208 case PDMAUDIOFMT_U32:
209 case PDMAUDIOFMT_S32:
210 return 32;
211
212 /* no default */
213 case PDMAUDIOFMT_INVALID:
214 case PDMAUDIOFMT_END:
215 case PDMAUDIOFMT_32BIT_HACK:
216 break;
217 }
218 AssertMsgFailedReturn(("Bogus audio format %ld\n", enmFmt), 0);
219}
220
221/**
222 * Gets the name of an audio format enum value.
223 *
224 * @returns Pointer to read-only name on success, returns "bad" on if
225 * invalid enum value.
226 * @param enmFmt The audio format to name.
227 */
228DECLINLINE(const char *) PDMAudioFormatGetName(PDMAUDIOFMT enmFmt)
229{
230 switch (enmFmt)
231 {
232 case PDMAUDIOFMT_U8: return "U8";
233 case PDMAUDIOFMT_U16: return "U16";
234 case PDMAUDIOFMT_U32: return "U32";
235 case PDMAUDIOFMT_S8: return "S8";
236 case PDMAUDIOFMT_S16: return "S16";
237 case PDMAUDIOFMT_S32: return "S32";
238 /* no default */
239 case PDMAUDIOFMT_INVALID:
240 case PDMAUDIOFMT_END:
241 case PDMAUDIOFMT_32BIT_HACK:
242 break;
243 }
244 AssertMsgFailedReturn(("Bogus audio format %d\n", enmFmt), "bad");
245}
246
247/**
248 * Initializes a stream configuration from PCM properties.
249 *
250 * @returns VBox status code.
251 * @param pCfg The stream configuration to initialize.
252 * @param pProps The PCM properties to use.
253 */
254DECLINLINE(int) PDMAudioStrmCfgInitWithProps(PPDMAUDIOSTREAMCFG pCfg, PCPDMAUDIOPCMPROPS pProps)
255{
256 AssertPtrReturn(pProps, VERR_INVALID_POINTER);
257 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
258
259 RT_ZERO(*pCfg);
260 pCfg->Backend.cFramesPreBuffering = UINT32_MAX; /* Explicitly set to "undefined". */
261
262 memcpy(&pCfg->Props, pProps, sizeof(PDMAUDIOPCMPROPS));
263
264 return VINF_SUCCESS;
265}
266
267/**
268 * Checks whether stream configuration matches the given PCM properties.
269 *
270 * @returns @c true if equal, @c false if not.
271 * @param pCfg The stream configuration.
272 * @param pProps The PCM properties to match with.
273 */
274DECLINLINE(bool) PDMAudioStrmCfgMatchesProps(PCPDMAUDIOSTREAMCFG pCfg, PCPDMAUDIOPCMPROPS pProps)
275{
276 AssertPtrReturn(pCfg, false);
277 return PDMAudioPropsAreEqual(pProps, &pCfg->Props);
278}
279
280/**
281 * Checks whether two stream configuration matches.
282 *
283 * @returns @c true if equal, @c false if not.
284 * @param pCfg1 The first stream configuration.
285 * @param pCfg2 The second stream configuration.
286 */
287DECLINLINE(bool) PDMAudioStrmCfgEquals(PCPDMAUDIOSTREAMCFG pCfg1, PCPDMAUDIOSTREAMCFG pCfg2)
288{
289 if (!pCfg1 || !pCfg2)
290 return false;
291 if (pCfg1 == pCfg2)
292 return pCfg1 != NULL;
293 if (PDMAudioPropsAreEqual(&pCfg1->Props, &pCfg2->Props))
294 return pCfg1->enmDir == pCfg2->enmDir
295 && pCfg1->u.enmDst == pCfg2->u.enmDst
296 && pCfg1->enmLayout == pCfg2->enmLayout
297 && pCfg1->Device.cMsSchedulingHint == pCfg2->Device.cMsSchedulingHint
298 && pCfg1->Backend.cFramesPeriod == pCfg2->Backend.cFramesPeriod
299 && pCfg1->Backend.cFramesBufferSize == pCfg2->Backend.cFramesBufferSize
300 && pCfg1->Backend.cFramesPreBuffering == pCfg2->Backend.cFramesPreBuffering
301 && strcmp(pCfg1->szName, pCfg2->szName) == 0;
302 return false;
303}
304
305/**
306 * Frees an audio stream allocated by PDMAudioStrmCfgDup().
307 *
308 * @param pCfg The stream configuration to free.
309 */
310DECLINLINE(void) PDMAudioStrmCfgFree(PPDMAUDIOSTREAMCFG pCfg)
311{
312 if (pCfg)
313 RTMemFree(pCfg);
314}
315
316/**
317 * Checks whether the given stream configuration is valid or not.
318 *
319 * @returns true/false accordingly.
320 * @param pCfg Stream configuration to check.
321 *
322 * @remarks This just performs a generic check of value ranges. Further, it
323 * will assert if the input is invalid.
324 *
325 * @sa PDMAudioPropsAreValid
326 */
327DECLINLINE(bool) PDMAudioStrmCfgIsValid(PCPDMAUDIOSTREAMCFG pCfg)
328{
329 AssertPtrReturn(pCfg, false);
330 AssertMsgReturn(pCfg->enmDir >= PDMAUDIODIR_UNKNOWN && pCfg->enmDir < PDMAUDIODIR_END,
331 ("%d\n", pCfg->enmDir), false);
332 AssertMsgReturn(pCfg->enmLayout >= PDMAUDIOSTREAMLAYOUT_UNKNOWN && pCfg->enmLayout < PDMAUDIOSTREAMLAYOUT_END,
333 ("%d\n", pCfg->enmLayout), false);
334 return PDMAudioPropsAreValid(&pCfg->Props);
335}
336
337/**
338 * Copies one stream configuration to another.
339 *
340 * @returns VBox status code.
341 * @param pDstCfg The destination stream configuration.
342 * @param pSrcCfg The source stream configuration.
343 */
344DECLINLINE(int) PDMAudioStrmCfgCopy(PPDMAUDIOSTREAMCFG pDstCfg, PCPDMAUDIOSTREAMCFG pSrcCfg)
345{
346 AssertPtrReturn(pDstCfg, VERR_INVALID_POINTER);
347 AssertPtrReturn(pSrcCfg, VERR_INVALID_POINTER);
348
349 /* This used to be VBOX_STRICT only and return VERR_INVALID_PARAMETER, but
350 that's making release builds work differently from debug & strict builds,
351 which is a terrible idea: */
352 Assert(PDMAudioStrmCfgIsValid(pSrcCfg));
353
354 memcpy(pDstCfg, pSrcCfg, sizeof(PDMAUDIOSTREAMCFG));
355
356 return VINF_SUCCESS;
357}
358
359/**
360 * Duplicates an audio stream configuration.
361 *
362 * @returns Pointer to duplicate on success, NULL on failure. Must be freed
363 * using PDMAudioStrmCfgFree().
364 *
365 * @param pCfg The audio stream configuration to duplicate.
366 */
367DECLINLINE(PPDMAUDIOSTREAMCFG) PDMAudioStrmCfgDup(PCPDMAUDIOSTREAMCFG pCfg)
368{
369 AssertPtrReturn(pCfg, NULL);
370
371 PPDMAUDIOSTREAMCFG pDst = (PPDMAUDIOSTREAMCFG)RTMemAllocZ(sizeof(PDMAUDIOSTREAMCFG));
372 if (pDst)
373 {
374 int rc = PDMAudioStrmCfgCopy(pDst, pCfg);
375 if (RT_SUCCESS(rc))
376 return pDst;
377
378 PDMAudioStrmCfgFree(pDst);
379 }
380 return NULL;
381}
382
383/**
384 * Logs an audio stream configuration.
385 *
386 * @param pCfg The stream configuration to log.
387 */
388DECLINLINE(void) PDMAudioStrmCfgLog(PCPDMAUDIOSTREAMCFG pCfg)
389{
390 if (pCfg)
391 LogFunc(("szName=%s enmDir=%RU32 uHz=%RU32 cBits=%RU8%s cChannels=%RU8\n", pCfg->szName, pCfg->enmDir,
392 pCfg->Props.uHz, pCfg->Props.cbSampleX * 8, pCfg->Props.fSigned ? "S" : "U", pCfg->Props.cChannelsX));
393}
394
395/**
396 * Converts a stream command enum value to a string.
397 *
398 * @returns Pointer to read-only stream command name on success,
399 * "bad" if invalid command value.
400 * @param enmCmd The stream command to name.
401 */
402DECLINLINE(const char *) PDMAudioStrmCmdGetName(PDMAUDIOSTREAMCMD enmCmd)
403{
404 switch (enmCmd)
405 {
406 case PDMAUDIOSTREAMCMD_INVALID: return "Invalid";
407 case PDMAUDIOSTREAMCMD_ENABLE: return "Enable";
408 case PDMAUDIOSTREAMCMD_DISABLE: return "Disable";
409 case PDMAUDIOSTREAMCMD_PAUSE: return "Pause";
410 case PDMAUDIOSTREAMCMD_RESUME: return "Resume";
411 case PDMAUDIOSTREAMCMD_DRAIN: return "Drain";
412 case PDMAUDIOSTREAMCMD_END:
413 case PDMAUDIOSTREAMCMD_32BIT_HACK:
414 break;
415 /* no default! */
416 }
417 AssertMsgFailedReturn(("Invalid stream command %d\n", enmCmd), "bad");
418}
419
420/**
421 * Checks if the stream status is one that can be read from.
422 *
423 * @returns @c true if ready to be read from, @c false if not.
424 * @param fStatus Stream status to evaluate, PDMAUDIOSTREAMSTS_FLAGS_XXX.
425 */
426DECLINLINE(bool) PDMAudioStrmStatusCanRead(PDMAUDIOSTREAMSTS fStatus)
427{
428 AssertReturn(fStatus & PDMAUDIOSTREAMSTS_VALID_MASK, false);
429 /*
430 return fStatus & PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED
431 && fStatus & PDMAUDIOSTREAMSTS_FLAGS_ENABLED
432 && !(fStatus & PDMAUDIOSTREAMSTS_FLAGS_PAUSED)
433 && !(fStatus & PDMAUDIOSTREAMSTS_FLAGS_PENDING_REINIT);*/
434 return (fStatus & ( PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED
435 | PDMAUDIOSTREAMSTS_FLAGS_ENABLED
436 | PDMAUDIOSTREAMSTS_FLAGS_PAUSED
437 | PDMAUDIOSTREAMSTS_FLAGS_PENDING_REINIT ))
438 == ( PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED
439 | PDMAUDIOSTREAMSTS_FLAGS_ENABLED);
440}
441
442/**
443 * Checks if the stream status is one that can be written to.
444 *
445 * @returns @c true if ready to be written to, @c false if not.
446 * @param fStatus Stream status to evaluate, PDMAUDIOSTREAMSTS_FLAGS_XXX.
447 */
448DECLINLINE(bool) PDMAudioStrmStatusCanWrite(PDMAUDIOSTREAMSTS fStatus)
449{
450 AssertReturn(fStatus & PDMAUDIOSTREAMSTS_VALID_MASK, false);
451 /*
452 return fStatus & PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED
453 && fStatus & PDMAUDIOSTREAMSTS_FLAGS_ENABLED
454 && !(fStatus & PDMAUDIOSTREAMSTS_FLAGS_PAUSED)
455 && !(fStatus & PDMAUDIOSTREAMSTS_FLAGS_PENDING_DISABLE)
456 && !(fStatus & PDMAUDIOSTREAMSTS_FLAGS_PENDING_REINIT);*/
457 return (fStatus & ( PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED
458 | PDMAUDIOSTREAMSTS_FLAGS_ENABLED
459 | PDMAUDIOSTREAMSTS_FLAGS_PAUSED
460 | PDMAUDIOSTREAMSTS_FLAGS_PENDING_DISABLE
461 | PDMAUDIOSTREAMSTS_FLAGS_PENDING_REINIT))
462 == ( PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED
463 | PDMAUDIOSTREAMSTS_FLAGS_ENABLED);
464}
465
466/**
467 * Checks if the stream status is a read-to-operate one.
468 *
469 * @returns @c true if ready to operate, @c false if not.
470 * @param fStatus Stream status to evaluate, PDMAUDIOSTREAMSTS_FLAGS_XXX.
471 */
472DECLINLINE(bool) PDMAudioStrmStatusIsReady(PDMAUDIOSTREAMSTS fStatus)
473{
474 AssertReturn(fStatus & PDMAUDIOSTREAMSTS_VALID_MASK, false);
475 /*
476 return fStatus & PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED
477 && fStatus & PDMAUDIOSTREAMSTS_FLAGS_ENABLED
478 && !(fStatus & PDMAUDIOSTREAMSTS_FLAGS_PENDING_REINIT);*/
479 return (fStatus & ( PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED
480 | PDMAUDIOSTREAMSTS_FLAGS_ENABLED
481 | PDMAUDIOSTREAMSTS_FLAGS_PENDING_REINIT))
482 == ( PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED
483 | PDMAUDIOSTREAMSTS_FLAGS_ENABLED);
484}
485
486
487/*********************************************************************************************************************************
488* PCM Property Helpers *
489*********************************************************************************************************************************/
490
491/**
492 * Initialize PCM audio properties.
493 */
494DECLINLINE(void) PDMAudioPropsInit(PPDMAUDIOPCMPROPS pProps, uint8_t cbSample, bool fSigned, uint8_t cChannels, uint32_t uHz)
495{
496 pProps->cbFrame = cbSample * cChannels;
497 pProps->cbSampleX = cbSample;
498 pProps->cChannelsX = cChannels;
499 pProps->cShiftX = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(cbSample, cChannels);
500 pProps->fSigned = fSigned;
501 pProps->fSwapEndian = false;
502 pProps->fRaw = false;
503 pProps->uHz = uHz;
504
505 Assert(pProps->cbFrame == (uint32_t)cbSample * cChannels);
506 Assert(pProps->cbSampleX == cbSample);
507 Assert(pProps->cChannelsX == cChannels);
508}
509
510/**
511 * Initialize PCM audio properties, extended version.
512 */
513DECLINLINE(void) PDMAudioPropsInitEx(PPDMAUDIOPCMPROPS pProps, uint8_t cbSample, bool fSigned, uint8_t cChannels, uint32_t uHz,
514 bool fLittleEndian, bool fRaw)
515{
516 Assert(!fRaw || cbSample == sizeof(int64_t));
517 pProps->cbFrame = cbSample * cChannels;
518 pProps->cbSampleX = cbSample;
519 pProps->cChannelsX = cChannels;
520 pProps->cShiftX = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(cbSample, cChannels);
521 pProps->fSigned = fSigned;
522#ifdef RT_LITTLE_ENDIAN
523 pProps->fSwapEndian = !fLittleEndian;
524#else
525 pProps->fSwapEndian = fLittleEndian;
526#endif
527 pProps->fRaw = fRaw;
528 pProps->uHz = uHz;
529
530 Assert(pProps->cbFrame == (uint32_t)cbSample * cChannels);
531 Assert(pProps->cbSampleX == cbSample);
532 Assert(pProps->cChannelsX == cChannels);
533}
534
535/**
536 * Modifies the channel count.
537 *
538 * @param pProps The PCM properties to update.
539 * @param cChannels The new channel count.
540 */
541DECLINLINE(void) PDMAudioPropsSetChannels(PPDMAUDIOPCMPROPS pProps, uint8_t cChannels)
542{
543 Assert(cChannels > 0); Assert(cChannels < 16);
544 pProps->cChannelsX = cChannels;
545 pProps->cbFrame = pProps->cbSampleX * cChannels;
546 pProps->cShiftX = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pProps->cbSampleX, cChannels);
547}
548
549/**
550 * Modifies the sample size.
551 *
552 * @param pProps The PCM properties to update.
553 * @param cbSample The new sample size (in bytes).
554 */
555DECLINLINE(void) PDMAudioPropsSetSampleSize(PPDMAUDIOPCMPROPS pProps, uint8_t cbSample)
556{
557 Assert(cbSample == 1 || cbSample == 2 || cbSample == 4 || cbSample == 8);
558 pProps->cbSampleX = cbSample;
559 pProps->cbFrame = cbSample * pProps->cChannelsX;
560 pProps->cShiftX = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(cbSample, pProps->cChannelsX);
561}
562
563/**
564 * Gets the bitrate.
565 *
566 * Divide the result by 8 to get the byte rate.
567 *
568 * @returns Bit rate.
569 * @param pProps PCM properties to calculate bitrate for.
570 */
571DECLINLINE(uint32_t) PDMAudioPropsGetBitrate(PCPDMAUDIOPCMPROPS pProps)
572{
573 Assert(pProps->cbFrame == pProps->cbSampleX * pProps->cChannelsX);
574 return pProps->cbFrame * pProps->uHz * 8;
575}
576
577/**
578 * Gets the number of channels.
579 * @returns The channel count.
580 * @param pProps The PCM properties.
581 */
582DECL_FORCE_INLINE(uint8_t) PDMAudioPropsChannels(PCPDMAUDIOPCMPROPS pProps)
583{
584 return pProps->cChannelsX;
585}
586
587/**
588 * Gets the sample size in bytes.
589 * @returns Number of bytes per sample.
590 * @param pProps The PCM properties.
591 */
592DECL_FORCE_INLINE(uint8_t) PDMAudioPropsSampleSize(PCPDMAUDIOPCMPROPS pProps)
593{
594 return pProps->cbSampleX;
595}
596
597/**
598 * Gets the sample size in bits.
599 * @returns Number of bits per sample.
600 * @param pProps The PCM properties.
601 */
602DECLINLINE(uint8_t) PDMAudioPropsSampleBits(PCPDMAUDIOPCMPROPS pProps)
603{
604 return pProps->cbSampleX * 8;
605}
606
607/**
608 * Gets the frame size in bytes.
609 * @returns Number of bytes per frame.
610 * @param pProps The PCM properties.
611 */
612DECL_FORCE_INLINE(uint8_t) PDMAudioPropsFrameSize(PCPDMAUDIOPCMPROPS pProps)
613{
614 return pProps->cbFrame;
615}
616
617/**
618 * Gets the frequency.
619 * @returns Frequency.
620 * @param pProps The PCM properties.
621 */
622DECL_FORCE_INLINE(uint32_t) PDMAudioPropsHz(PCPDMAUDIOPCMPROPS pProps)
623{
624 return pProps->uHz;
625}
626
627/**
628 * Checks if the format is signed or unsigned.
629 * @returns true if signed, false if unsigned.
630 * @param pProps The PCM properties.
631 */
632DECL_FORCE_INLINE(bool) PDMAudioPropsIsSigned(PCPDMAUDIOPCMPROPS pProps)
633{
634 return pProps->fSigned;
635}
636
637/**
638 * Checks if the format is little-endian or not.
639 * @returns true if little-endian (or if 8-bit), false if big-endian.
640 * @param pProps The PCM properties.
641 */
642DECL_FORCE_INLINE(bool) PDMAudioPropsIsLittleEndian(PCPDMAUDIOPCMPROPS pProps)
643{
644#ifdef RT_LITTLE_ENDIAN
645 return !pProps->fSwapEndian || pProps->cbSampleX < 2;
646#else
647 return pProps->fSwapEndian || pProps->cbSampleX < 2;
648#endif
649}
650
651/**
652 * Checks if the format is big-endian or not.
653 * @returns true if big-endian (or if 8-bit), false if little-endian.
654 * @param pProps The PCM properties.
655 */
656DECL_FORCE_INLINE(bool) PDMAudioPropsIsBigEndian(PCPDMAUDIOPCMPROPS pProps)
657{
658#ifdef RT_LITTLE_ENDIAN
659 return pProps->fSwapEndian || pProps->cbSampleX < 2;
660#else
661 return !pProps->fSwapEndian || pProps->cbSampleX < 2;
662#endif
663}
664
665/**
666 * Rounds down the given byte amount to the nearest frame boundrary.
667 *
668 * @returns Rounded byte amount.
669 * @param pProps PCM properties to use.
670 * @param cb The size (in bytes) to round.
671 */
672DECLINLINE(uint32_t) PDMAudioPropsFloorBytesToFrame(PCPDMAUDIOPCMPROPS pProps, uint32_t cb)
673{
674 AssertPtrReturn(pProps, 0);
675 return PDMAUDIOPCMPROPS_F2B(pProps, PDMAUDIOPCMPROPS_B2F(pProps, cb));
676}
677
678/**
679 * Rounds up the given byte amount to the nearest frame boundrary.
680 *
681 * @returns Rounded byte amount.
682 * @param pProps PCM properties to use.
683 * @param cb The size (in bytes) to round.
684 */
685DECLINLINE(uint32_t) PDMAudioPropsRoundUpBytesToFrame(PCPDMAUDIOPCMPROPS pProps, uint32_t cb)
686{
687 AssertPtrReturn(pProps, 0);
688 uint32_t const cbFrame = PDMAUDIOPCMPROPS_F2B(pProps, 1 /* Frame */);
689 AssertReturn(cbFrame, 0);
690 return PDMAUDIOPCMPROPS_F2B(pProps, PDMAUDIOPCMPROPS_B2F(pProps, cb + cbFrame - 1));
691}
692
693/**
694 * Checks if the given size is aligned on a frame boundrary.
695 *
696 * @returns @c true if properly aligned, @c false if not.
697 * @param pProps PCM properties to use.
698 * @param cb The size (in bytes) to check.
699 */
700DECLINLINE(bool) PDMAudioPropsIsSizeAligned(PCPDMAUDIOPCMPROPS pProps, uint32_t cb)
701{
702 AssertPtrReturn(pProps, false);
703 uint32_t const cbFrame = PDMAUDIOPCMPROPS_F2B(pProps, 1 /* Frame */);
704 AssertReturn(cbFrame, false);
705 return cb % cbFrame == 0;
706}
707
708/**
709 * Converts bytes to frames (rounding down of course).
710 *
711 * @returns Number of frames.
712 * @param pProps PCM properties to use.
713 * @param cb The number of bytes to convert.
714 */
715DECLINLINE(uint32_t) PDMAudioPropsBytesToFrames(PCPDMAUDIOPCMPROPS pProps, uint32_t cb)
716{
717 AssertPtrReturn(pProps, 0);
718 return PDMAUDIOPCMPROPS_B2F(pProps, cb);
719}
720
721/**
722 * Converts bytes to milliseconds.
723 *
724 * @return Number milliseconds @a cb takes to play or record.
725 * @param pProps PCM properties to use.
726 * @param cb The number of bytes to convert.
727 *
728 * @note Rounds up the result.
729 */
730DECLINLINE(uint64_t) PDMAudioPropsBytesToMilli(PCPDMAUDIOPCMPROPS pProps, uint32_t cb)
731{
732 AssertPtrReturn(pProps, 0);
733
734 /* Check parameters to prevent division by chainsaw: */
735 uint32_t const uHz = pProps->uHz;
736 if (uHz)
737 {
738 const unsigned cbFrame = PDMAUDIOPCMPROPS_F2B(pProps, 1 /* Frame */);
739 if (cbFrame)
740 {
741 /* Round cb up to closest frame size: */
742 cb = (cb + cbFrame - 1) / cbFrame;
743
744 /* Convert to milliseconds. */
745 return (cb * (uint64_t)RT_MS_1SEC + uHz - 1) / uHz;
746 }
747 }
748 return 0;
749}
750
751/**
752 * Converts bytes to microseconds.
753 *
754 * @return Number microseconds @a cb takes to play or record.
755 * @param pProps PCM properties to use.
756 * @param cb The number of bytes to convert.
757 *
758 * @note Rounds up the result.
759 */
760DECLINLINE(uint64_t) PDMAudioPropsBytesToMicro(PCPDMAUDIOPCMPROPS pProps, uint32_t cb)
761{
762 AssertPtrReturn(pProps, 0);
763
764 /* Check parameters to prevent division by chainsaw: */
765 uint32_t const uHz = pProps->uHz;
766 if (uHz)
767 {
768 const unsigned cbFrame = PDMAUDIOPCMPROPS_F2B(pProps, 1 /* Frame */);
769 if (cbFrame)
770 {
771 /* Round cb up to closest frame size: */
772 cb = (cb + cbFrame - 1) / cbFrame;
773
774 /* Convert to microseconds. */
775 return (cb * (uint64_t)RT_US_1SEC + uHz - 1) / uHz;
776 }
777 }
778 return 0;
779}
780
781/**
782 * Converts bytes to nanoseconds.
783 *
784 * @return Number nanoseconds @a cb takes to play or record.
785 * @param pProps PCM properties to use.
786 * @param cb The number of bytes to convert.
787 *
788 * @note Rounds up the result.
789 */
790DECLINLINE(uint64_t) PDMAudioPropsBytesToNano(PCPDMAUDIOPCMPROPS pProps, uint32_t cb)
791{
792 AssertPtrReturn(pProps, 0);
793
794 /* Check parameters to prevent division by chainsaw: */
795 uint32_t const uHz = pProps->uHz;
796 if (uHz)
797 {
798 const unsigned cbFrame = PDMAUDIOPCMPROPS_F2B(pProps, 1 /* Frame */);
799 if (cbFrame)
800 {
801 /* Round cb up to closest frame size: */
802 cb = (cb + cbFrame - 1) / cbFrame;
803
804 /* Convert to nanoseconds. */
805 return (cb * (uint64_t)RT_NS_1SEC + uHz - 1) / uHz;
806 }
807 }
808 return 0;
809}
810
811/**
812 * Converts frames to bytes.
813 *
814 * @returns Number of bytes.
815 * @param pProps The PCM properties to use.
816 * @param cFrames Number of audio frames to convert.
817 * @sa PDMAUDIOPCMPROPS_F2B
818 */
819DECLINLINE(uint32_t) PDMAudioPropsFramesToBytes(PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames)
820{
821 AssertPtrReturn(pProps, 0);
822 return PDMAUDIOPCMPROPS_F2B(pProps, cFrames);
823}
824
825/**
826 * Converts frames to milliseconds.
827 *
828 * @returns milliseconds.
829 * @param pProps The PCM properties to use.
830 * @param cFrames Number of audio frames to convert.
831 * @note No rounding here, result is floored.
832 */
833DECLINLINE(uint64_t) PDMAudioPropsFramesToMilli(PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames)
834{
835 AssertPtrReturn(pProps, 0);
836
837 /* Check input to prevent division by chainsaw: */
838 uint32_t const uHz = pProps->uHz;
839 if (uHz)
840 return ASMMultU32ByU32DivByU32(cFrames, RT_MS_1SEC, uHz);
841 return 0;
842}
843
844/**
845 * Converts frames to microseconds.
846 *
847 * @returns microseconds.
848 * @param pProps The PCM properties to use.
849 * @param cFrames Number of audio frames to convert.
850 * @note No rounding here, result is floored.
851 */
852DECLINLINE(uint64_t) PDMAudioPropsFramesToMicro(PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames)
853{
854 AssertPtrReturn(pProps, 0);
855
856 /* Check input to prevent division by chainsaw: */
857 uint32_t const uHz = pProps->uHz;
858 if (uHz)
859 return ASMMultU32ByU32DivByU32(cFrames, RT_US_1SEC, uHz);
860 return 0;
861}
862
863/**
864 * Converts frames to nanoseconds.
865 *
866 * @returns Nanoseconds.
867 * @param pProps The PCM properties to use.
868 * @param cFrames Number of audio frames to convert.
869 * @note No rounding here, result is floored.
870 */
871DECLINLINE(uint64_t) PDMAudioPropsFramesToNano(PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames)
872{
873 AssertPtrReturn(pProps, 0);
874
875 /* Check input to prevent division by chainsaw: */
876 uint32_t const uHz = pProps->uHz;
877 if (uHz)
878 return ASMMultU32ByU32DivByU32(cFrames, RT_NS_1SEC, uHz);
879 return 0;
880}
881
882/**
883 * Converts frames to NT ticks (100 ns units).
884 *
885 * @returns NT ticks.
886 * @param pProps The PCM properties to use.
887 * @param cFrames Number of audio frames to convert.
888 * @note No rounding here, result is floored.
889 */
890DECLINLINE(uint64_t) PDMAudioPropsFramesToNtTicks(PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames)
891{
892 AssertPtrReturn(pProps, 0);
893
894 /* Check input to prevent division by chainsaw: */
895 uint32_t const uHz = pProps->uHz;
896 if (uHz)
897 return ASMMultU32ByU32DivByU32(cFrames, RT_NS_1SEC / 100, uHz);
898 return 0;
899}
900
901/**
902 * Converts milliseconds to frames.
903 *
904 * @returns Number of frames
905 * @param pProps The PCM properties to use.
906 * @param cMs The number of milliseconds to convert.
907 *
908 * @note The result is rounded rather than floored (hysterical raisins).
909 */
910DECLINLINE(uint32_t) PDMAudioPropsMilliToFrames(PCPDMAUDIOPCMPROPS pProps, uint64_t cMs)
911{
912 AssertPtrReturn(pProps, 0);
913
914 uint32_t const uHz = pProps->uHz;
915 uint32_t cFrames;
916 if (cMs < RT_MS_1SEC)
917 cFrames = 0;
918 else
919 {
920 cFrames = cMs / RT_MS_1SEC * uHz;
921 cMs %= RT_MS_1SEC;
922 }
923 cFrames += (ASMMult2xU32RetU64(uHz, (uint32_t)cMs) + RT_MS_1SEC - 1) / RT_MS_1SEC;
924 return cFrames;
925}
926
927/**
928 * Converts milliseconds to bytes.
929 *
930 * @returns Number of bytes (frame aligned).
931 * @param pProps The PCM properties to use.
932 * @param cMs The number of milliseconds to convert.
933 *
934 * @note The result is rounded rather than floored (hysterical raisins).
935 */
936DECLINLINE(uint32_t) PDMAudioPropsMilliToBytes(PCPDMAUDIOPCMPROPS pProps, uint64_t cMs)
937{
938 return PDMAUDIOPCMPROPS_F2B(pProps, PDMAudioPropsMilliToFrames(pProps, cMs));
939}
940
941/**
942 * Converts nanoseconds to frames.
943 *
944 * @returns Number of frames
945 * @param pProps The PCM properties to use.
946 * @param cNs The number of nanoseconds to convert.
947 *
948 * @note The result is rounded rather than floored (hysterical raisins).
949 */
950DECLINLINE(uint32_t) PDMAudioPropsNanoToFrames(PCPDMAUDIOPCMPROPS pProps, uint64_t cNs)
951{
952 AssertPtrReturn(pProps, 0);
953
954 uint32_t const uHz = pProps->uHz;
955 uint32_t cFrames;
956 if (cNs < RT_NS_1SEC)
957 cFrames = 0;
958 else
959 {
960 cFrames = cNs / RT_NS_1SEC * uHz;
961 cNs %= RT_NS_1SEC;
962 }
963 cFrames += (ASMMult2xU32RetU64(uHz, (uint32_t)cNs) + RT_NS_1SEC - 1) / RT_NS_1SEC;
964 return cFrames;
965}
966
967/**
968 * Converts nanoseconds to bytes.
969 *
970 * @returns Number of bytes (frame aligned).
971 * @param pProps The PCM properties to use.
972 * @param cNs The number of nanoseconds to convert.
973 *
974 * @note The result is rounded rather than floored (hysterical raisins).
975 */
976DECLINLINE(uint32_t) PDMAudioPropsNanoToBytes(PCPDMAUDIOPCMPROPS pProps, uint64_t cNs)
977{
978 return PDMAUDIOPCMPROPS_F2B(pProps, PDMAudioPropsNanoToFrames(pProps, cNs));
979}
980
981/**
982 * Clears a sample buffer by the given amount of audio frames with silence (according to the format
983 * given by the PCM properties).
984 *
985 * @param pProps The PCM properties to apply.
986 * @param pvBuf The buffer to clear.
987 * @param cbBuf The buffer size in bytes.
988 * @param cFrames The number of audio frames to clear. Capped at @a cbBuf
989 * if exceeding the buffer. If the size is an unaligned
990 * number of frames, the extra bytes may be left
991 * uninitialized in some configurations.
992 */
993DECLINLINE(void) PDMAudioPropsClearBuffer(PCPDMAUDIOPCMPROPS pProps, void *pvBuf, size_t cbBuf, uint32_t cFrames)
994{
995 /*
996 * Validate input
997 */
998 AssertPtrReturnVoid(pProps);
999 Assert(pProps->cbSampleX);
1000 if (!cbBuf || !cFrames)
1001 return;
1002 AssertPtrReturnVoid(pvBuf);
1003
1004 Assert(pProps->fSwapEndian == false); /** @todo Swapping Endianness is not supported yet. */
1005
1006 /*
1007 * Decide how much needs clearing.
1008 */
1009 size_t cbToClear = PDMAudioPropsFramesToBytes(pProps, cFrames);
1010 AssertStmt(cbToClear <= cbBuf, cbToClear = cbBuf);
1011
1012 Log2Func(("pProps=%p, pvBuf=%p, cFrames=%RU32, fSigned=%RTbool, cbSample=%RU8\n",
1013 pProps, pvBuf, cFrames, pProps->fSigned, pProps->cbSampleX));
1014
1015 /*
1016 * Do the job.
1017 */
1018 if (pProps->fSigned)
1019 RT_BZERO(pvBuf, cbToClear);
1020 else /* Unsigned formats. */
1021 {
1022 switch (pProps->cbSampleX)
1023 {
1024 case 1: /* 8 bit */
1025 memset(pvBuf, 0x80, cbToClear);
1026 break;
1027
1028 case 2: /* 16 bit */
1029 {
1030 uint16_t *pu16Dst = (uint16_t *)pvBuf;
1031 size_t cLeft = cbToClear / sizeof(uint16_t);
1032 while (cLeft-- > 0)
1033 *pu16Dst++ = 0x80;
1034 break;
1035 }
1036
1037 /** @todo Add 24 bit? */
1038
1039 case 4: /* 32 bit */
1040 ASMMemFill32(pvBuf, cbToClear & ~(size_t)3, 0x80);
1041 break;
1042
1043 default:
1044 AssertMsgFailed(("Invalid bytes per sample: %RU8\n", pProps->cbSampleX));
1045 }
1046 }
1047}
1048
1049/**
1050 * Compares two sets of PCM properties.
1051 *
1052 * @returns @c true if the same, @c false if not.
1053 * @param pProps1 The first set of properties to compare.
1054 * @param pProps2 The second set of properties to compare.
1055 */
1056DECLINLINE(bool) PDMAudioPropsAreEqual(PCPDMAUDIOPCMPROPS pProps1, PCPDMAUDIOPCMPROPS pProps2)
1057{
1058 AssertPtrReturn(pProps1, false);
1059 AssertPtrReturn(pProps2, false);
1060
1061 if (pProps1 == pProps2) /* If the pointers match, take a shortcut. */
1062 return true;
1063
1064 return pProps1->uHz == pProps2->uHz
1065 && pProps1->cChannelsX == pProps2->cChannelsX
1066 && pProps1->cbSampleX == pProps2->cbSampleX
1067 && pProps1->fSigned == pProps2->fSigned
1068 && pProps1->fSwapEndian == pProps2->fSwapEndian;
1069}
1070
1071/**
1072 * Checks whether the given PCM properties are valid or not.
1073 *
1074 * @returns true/false accordingly.
1075 * @param pProps The PCM properties to check.
1076 *
1077 * @remarks This just performs a generic check of value ranges. Further, it
1078 * will assert if the input is invalid.
1079 *
1080 * @sa PDMAudioStrmCfgIsValid
1081 */
1082DECLINLINE(bool) PDMAudioPropsAreValid(PCPDMAUDIOPCMPROPS pProps)
1083{
1084 AssertPtrReturn(pProps, false);
1085
1086 AssertReturn(pProps->cChannelsX != 0, false);
1087 AssertMsgReturn( pProps->cbSampleX == 1 || pProps->cbSampleX == 2 || pProps->cbSampleX == 4 || (pProps->cbSampleX == 8 && pProps->fRaw),
1088 ("%u\n", pProps->cbSampleX), false);
1089 AssertMsgReturn(pProps->cbFrame == pProps->cbSampleX * pProps->cChannelsX,
1090 ("cbFrame=%u cbSample=%u cChannels=%u\n", pProps->cbFrame, pProps->cbSampleX, pProps->cChannelsX),
1091 false);
1092 AssertMsgReturn(pProps->uHz >= 1000 && pProps->uHz < 1000000, ("%u\n", pProps->uHz), false);
1093 AssertMsgReturn(pProps->cShiftX == PDMAUDIOPCMPROPS_MAKE_SHIFT(pProps),
1094 ("cShift=%u cbSample=%u cChannels=%u\n", pProps->cShiftX, pProps->cbSampleX, pProps->cChannelsX),
1095 false);
1096 AssertReturn(!pProps->fRaw || (pProps->fSigned && pProps->cbSampleX == sizeof(int64_t)), false);
1097 return true;
1098}
1099
1100/**
1101 * Get number of bytes per frame.
1102 *
1103 * @returns Number of bytes per audio frame.
1104 * @param pProps PCM properties to use.
1105 * @sa PDMAUDIOPCMPROPS_F2B
1106 */
1107DECLINLINE(uint32_t) PDMAudioPropsBytesPerFrame(PCPDMAUDIOPCMPROPS pProps)
1108{
1109 return PDMAUDIOPCMPROPS_F2B(pProps, 1 /*cFrames*/);
1110}
1111
1112/**
1113 * Prints PCM properties to the debug log.
1114 *
1115 * @param pProps PCM properties to use.
1116 */
1117DECLINLINE(void) PDMAudioPropsLog(PCPDMAUDIOPCMPROPS pProps)
1118{
1119 AssertPtrReturnVoid(pProps);
1120
1121 Log(("uHz=%RU32, cChannels=%RU8, cBits=%RU8%s",
1122 pProps->uHz, pProps->cChannelsX, pProps->cbSampleX * 8, pProps->fSigned ? "S" : "U"));
1123}
1124
1125/** Max necessary buffer space for PDMAudioPropsToString */
1126#define PDMAUDIOPROPSTOSTRING_MAX sizeof("16ch S64 4294967296Hz swap raw")
1127
1128/**
1129 * Formats the PCM audio properties into a string buffer.
1130 *
1131 * @returns pszDst
1132 * @param pProps PCM properties to use.
1133 * @param pszDst The destination buffer.
1134 * @param cchDst The size of the destination buffer. Recommended to be at
1135 * least PDMAUDIOPROPSTOSTRING_MAX bytes.
1136 */
1137DECLINLINE(char *) PDMAudioPropsToString(PCPDMAUDIOPCMPROPS pProps, char *pszDst, size_t cchDst)
1138{
1139 /* 2ch S64 44100Hz swap raw */
1140 RTStrPrintf(pszDst, cchDst, "%uch %c%u %RU32Hz%s%s",
1141 PDMAudioPropsChannels(pProps), PDMAudioPropsIsSigned(pProps) ? 'S' : 'U', PDMAudioPropsSampleBits(pProps),
1142 PDMAudioPropsHz(pProps), pProps->fSwapEndian ? " swap" : "", pProps->fRaw ? " raw" : "");
1143 return pszDst;
1144}
1145
1146
1147
1148/** @} */
1149
1150#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