VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/AudioHlp.cpp@ 88253

最後變更 在這個檔案從88253是 88235,由 vboxsync 提交於 4 年 前

Audio: File header adjustments. bugref:9890

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 22.4 KB
 
1/* $Id: AudioHlp.cpp 88235 2021-03-22 10:44:43Z vboxsync $ */
2/** @file
3 * Audio helper routines.
4 *
5 * These are used with both drivers and devices.
6 */
7
8/*
9 * Copyright (C) 2006-2020 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.alldomusa.eu.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20
21/*********************************************************************************************************************************
22* Header Files *
23*********************************************************************************************************************************/
24#include <iprt/alloc.h>
25#include <iprt/asm-math.h>
26#include <iprt/assert.h>
27#include <iprt/dir.h>
28#include <iprt/file.h>
29#include <iprt/string.h>
30#include <iprt/uuid.h>
31
32#define LOG_GROUP LOG_GROUP_DRV_AUDIO
33#include <VBox/log.h>
34
35#include <VBox/err.h>
36#include <VBox/vmm/pdmdev.h>
37#include <VBox/vmm/pdm.h>
38#include <VBox/vmm/pdmaudioinline.h>
39#include <VBox/vmm/mm.h>
40
41#include <ctype.h>
42#include <stdlib.h>
43
44#include "AudioHlp.h"
45#include "AudioMixBuffer.h"
46
47
48/*********************************************************************************************************************************
49* Structures and Typedefs *
50*********************************************************************************************************************************/
51/**
52 * Structure for building up a .WAV file header.
53 */
54typedef struct AUDIOWAVFILEHDR
55{
56 uint32_t u32RIFF;
57 uint32_t u32Size;
58 uint32_t u32WAVE;
59
60 uint32_t u32Fmt;
61 uint32_t u32Size1;
62 uint16_t u16AudioFormat;
63 uint16_t u16NumChannels;
64 uint32_t u32SampleRate;
65 uint32_t u32ByteRate;
66 uint16_t u16BlockAlign;
67 uint16_t u16BitsPerSample;
68
69 uint32_t u32ID2;
70 uint32_t u32Size2;
71} AUDIOWAVFILEHDR, *PAUDIOWAVFILEHDR;
72AssertCompileSize(AUDIOWAVFILEHDR, 11*4);
73
74/**
75 * Structure for keeeping the internal .WAV file data
76 */
77typedef struct AUDIOWAVFILEDATA
78{
79 /** The file header/footer. */
80 AUDIOWAVFILEHDR Hdr;
81} AUDIOWAVFILEDATA, *PAUDIOWAVFILEDATA;
82
83
84
85#if 0 /* unused, no header prototypes */
86
87/**
88 * Retrieves the matching PDMAUDIOFMT for the given bits + signing flag.
89 *
90 * @return Matching PDMAUDIOFMT value.
91 * @retval PDMAUDIOFMT_INVALID if unsupported @a cBits value.
92 *
93 * @param cBits The number of bits in the audio format.
94 * @param fSigned Whether the audio format is signed @c true or not.
95 */
96PDMAUDIOFMT DrvAudioAudFmtBitsToFormat(uint8_t cBits, bool fSigned)
97{
98 if (fSigned)
99 {
100 switch (cBits)
101 {
102 case 8: return PDMAUDIOFMT_S8;
103 case 16: return PDMAUDIOFMT_S16;
104 case 32: return PDMAUDIOFMT_S32;
105 default: AssertMsgFailedReturn(("Bogus audio bits %RU8\n", cBits), PDMAUDIOFMT_INVALID);
106 }
107 }
108 else
109 {
110 switch (cBits)
111 {
112 case 8: return PDMAUDIOFMT_U8;
113 case 16: return PDMAUDIOFMT_U16;
114 case 32: return PDMAUDIOFMT_U32;
115 default: AssertMsgFailedReturn(("Bogus audio bits %RU8\n", cBits), PDMAUDIOFMT_INVALID);
116 }
117 }
118}
119
120/**
121 * Returns an unique file name for this given audio connector instance.
122 *
123 * @return Allocated file name. Must be free'd using RTStrFree().
124 * @param uInstance Driver / device instance.
125 * @param pszPath Path name of the file to delete. The path must exist.
126 * @param pszSuffix File name suffix to use.
127 */
128char *DrvAudioDbgGetFileNameA(uint8_t uInstance, const char *pszPath, const char *pszSuffix)
129{
130 char szFileName[64];
131 RTStrPrintf(szFileName, sizeof(szFileName), "drvAudio%RU8-%s", uInstance, pszSuffix);
132
133 char szFilePath[RTPATH_MAX];
134 int rc2 = RTStrCopy(szFilePath, sizeof(szFilePath), pszPath);
135 AssertRC(rc2);
136 rc2 = RTPathAppend(szFilePath, sizeof(szFilePath), szFileName);
137 AssertRC(rc2);
138
139 return RTStrDup(szFilePath);
140}
141
142#endif /* unused */
143
144/**
145 * Converts a given string to an audio format.
146 *
147 * @returns Audio format for the given string, or PDMAUDIOFMT_INVALID if not found.
148 * @param pszFmt String to convert to an audio format.
149 */
150PDMAUDIOFMT AudioHlpStrToAudFmt(const char *pszFmt)
151{
152 AssertPtrReturn(pszFmt, PDMAUDIOFMT_INVALID);
153
154 if (!RTStrICmp(pszFmt, "u8"))
155 return PDMAUDIOFMT_U8;
156 if (!RTStrICmp(pszFmt, "u16"))
157 return PDMAUDIOFMT_U16;
158 if (!RTStrICmp(pszFmt, "u32"))
159 return PDMAUDIOFMT_U32;
160 if (!RTStrICmp(pszFmt, "s8"))
161 return PDMAUDIOFMT_S8;
162 if (!RTStrICmp(pszFmt, "s16"))
163 return PDMAUDIOFMT_S16;
164 if (!RTStrICmp(pszFmt, "s32"))
165 return PDMAUDIOFMT_S32;
166
167 AssertMsgFailed(("Invalid audio format '%s'\n", pszFmt));
168 return PDMAUDIOFMT_INVALID;
169}
170
171/**
172 * Checks whether a given stream configuration is valid or not.
173 *
174 * @note See notes on AudioHlpPcmPropsAreValid().
175 *
176 * Returns @c true if configuration is valid, @c false if not.
177 * @param pCfg Stream configuration to check.
178 */
179bool AudioHlpStreamCfgIsValid(PCPDMAUDIOSTREAMCFG pCfg)
180{
181 AssertPtrReturn(pCfg, false);
182
183 AssertReturn(PDMAudioStrmCfgIsValid(pCfg), false);
184
185 bool fValid = ( pCfg->enmDir == PDMAUDIODIR_IN
186 || pCfg->enmDir == PDMAUDIODIR_OUT);
187
188 fValid &= ( pCfg->enmLayout == PDMAUDIOSTREAMLAYOUT_NON_INTERLEAVED
189 || pCfg->enmLayout == PDMAUDIOSTREAMLAYOUT_RAW);
190
191 if (fValid)
192 fValid = AudioHlpPcmPropsAreValid(&pCfg->Props);
193
194 return fValid;
195}
196
197/**
198 * Calculates the audio bit rate of the given bits per sample, the Hz and the number
199 * of audio channels.
200 *
201 * Divide the result by 8 to get the byte rate.
202 *
203 * @returns Bitrate.
204 * @param cBits Number of bits per sample.
205 * @param uHz Hz (Hertz) rate.
206 * @param cChannels Number of audio channels.
207 */
208uint32_t AudioHlpCalcBitrate(uint8_t cBits, uint32_t uHz, uint8_t cChannels)
209{
210 return cBits * uHz * cChannels;
211}
212
213
214/**
215 * Checks whether given PCM properties are valid or not.
216 *
217 * @note This is more of a supported than valid check. There is code for
218 * unsigned samples elsewhere (like DrvAudioHlpClearBuf()), but this
219 * function will flag such properties as not valid.
220 *
221 * @todo r=bird: See note and explain properly.
222 *
223 * @returns @c true if the properties are valid, @c false if not.
224 * @param pProps The PCM properties to check.
225 */
226bool AudioHlpPcmPropsAreValid(PCPDMAUDIOPCMPROPS pProps)
227{
228 AssertPtrReturn(pProps, false);
229
230 AssertReturn(PDMAudioPropsAreValid(pProps), false);
231
232 /** @todo r=bird: This code is cannot make up its mind whether to return on
233 * false, or whether to return at the end. (hint: just return
234 * immediately, duh.) */
235
236 /* Minimum 1 channel (mono), maximum 7.1 (= 8) channels. */
237 bool fValid = ( pProps->cChannels >= 1
238 && pProps->cChannels <= 8);
239
240 if (fValid)
241 {
242 switch (pProps->cbSample)
243 {
244 case 1: /* 8 bit */
245 if (pProps->fSigned)
246 fValid = false;
247 break;
248 case 2: /* 16 bit */
249 if (!pProps->fSigned)
250 fValid = false;
251 break;
252 /** @todo Do we need support for 24 bit samples? */
253 case 4: /* 32 bit */
254 if (!pProps->fSigned)
255 fValid = false;
256 break;
257 default:
258 fValid = false;
259 break;
260 }
261 }
262
263 if (!fValid)
264 return false;
265
266 fValid &= pProps->uHz > 0;
267 fValid &= pProps->cShift == PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pProps->cbSample, pProps->cChannels);
268 fValid &= pProps->fSwapEndian == false; /** @todo Handling Big Endian audio data is not supported yet. */
269
270 return fValid;
271}
272
273
274/*********************************************************************************************************************************
275* Audio File Helpers *
276*********************************************************************************************************************************/
277
278/**
279 * Sanitizes the file name component so that unsupported characters
280 * will be replaced by an underscore ("_").
281 *
282 * @return IPRT status code.
283 * @param pszPath Path to sanitize.
284 * @param cbPath Size (in bytes) of path to sanitize.
285 */
286int AudioHlpFileNameSanitize(char *pszPath, size_t cbPath)
287{
288 RT_NOREF(cbPath);
289 int rc = VINF_SUCCESS;
290#ifdef RT_OS_WINDOWS
291 /* Filter out characters not allowed on Windows platforms, put in by
292 RTTimeSpecToString(). */
293 /** @todo Use something like RTPathSanitize() if available later some time. */
294 static RTUNICP const s_uszValidRangePairs[] =
295 {
296 ' ', ' ',
297 '(', ')',
298 '-', '.',
299 '0', '9',
300 'A', 'Z',
301 'a', 'z',
302 '_', '_',
303 0xa0, 0xd7af,
304 '\0'
305 };
306 ssize_t cReplaced = RTStrPurgeComplementSet(pszPath, s_uszValidRangePairs, '_' /* Replacement */);
307 if (cReplaced < 0)
308 rc = VERR_INVALID_UTF8_ENCODING;
309#else
310 RT_NOREF(pszPath);
311#endif
312 return rc;
313}
314
315/**
316 * Constructs an unique file name, based on the given path and the audio file type.
317 *
318 * @returns IPRT status code.
319 * @param pszFile Where to store the constructed file name.
320 * @param cchFile Size (in characters) of the file name buffer.
321 * @param pszPath Base path to use.
322 * If NULL or empty, the system's temporary directory will be used.
323 * @param pszName A name for better identifying the file.
324 * @param uInstance Device / driver instance which is using this file.
325 * @param enmType Audio file type to construct file name for.
326 * @param fFlags File naming flags, PDMAUDIOFILENAME_FLAGS_XXX.
327 */
328int AudioHlpFileNameGet(char *pszFile, size_t cchFile, const char *pszPath, const char *pszName,
329 uint32_t uInstance, PDMAUDIOFILETYPE enmType, uint32_t fFlags)
330{
331 AssertPtrReturn(pszFile, VERR_INVALID_POINTER);
332 AssertReturn(cchFile, VERR_INVALID_PARAMETER);
333 /* pszPath can be NULL. */
334 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
335 /** @todo Validate fFlags. */
336
337 int rc;
338
339 char *pszPathTmp = NULL;
340
341 do
342 {
343 if ( pszPath == NULL
344 || !strlen(pszPath))
345 {
346 char szTemp[RTPATH_MAX];
347 rc = RTPathTemp(szTemp, sizeof(szTemp));
348 if (RT_SUCCESS(rc))
349 {
350 pszPathTmp = RTStrDup(szTemp);
351 }
352 else
353 break;
354 }
355 else
356 pszPathTmp = RTStrDup(pszPath);
357
358 AssertPtrBreakStmt(pszPathTmp, rc = VERR_NO_MEMORY);
359
360 char szFilePath[RTPATH_MAX];
361 rc = RTStrCopy(szFilePath, sizeof(szFilePath), pszPathTmp);
362 AssertRCBreak(rc);
363
364 /* Create it when necessary. */
365 if (!RTDirExists(szFilePath))
366 {
367 rc = RTDirCreateFullPath(szFilePath, RTFS_UNIX_IRWXU);
368 if (RT_FAILURE(rc))
369 break;
370 }
371
372 char szFileName[RTPATH_MAX];
373 szFileName[0] = '\0';
374
375 if (fFlags & PDMAUDIOFILENAME_FLAGS_TS)
376 {
377 RTTIMESPEC time;
378 if (!RTTimeSpecToString(RTTimeNow(&time), szFileName, sizeof(szFileName)))
379 {
380 rc = VERR_BUFFER_OVERFLOW;
381 break;
382 }
383
384 rc = AudioHlpFileNameSanitize(szFileName, sizeof(szFileName));
385 if (RT_FAILURE(rc))
386 break;
387
388 rc = RTStrCat(szFileName, sizeof(szFileName), "-");
389 if (RT_FAILURE(rc))
390 break;
391 }
392
393 rc = RTStrCat(szFileName, sizeof(szFileName), pszName);
394 if (RT_FAILURE(rc))
395 break;
396
397 rc = RTStrCat(szFileName, sizeof(szFileName), "-");
398 if (RT_FAILURE(rc))
399 break;
400
401 char szInst[16];
402 RTStrPrintf2(szInst, sizeof(szInst), "%RU32", uInstance);
403 rc = RTStrCat(szFileName, sizeof(szFileName), szInst);
404 if (RT_FAILURE(rc))
405 break;
406
407 switch (enmType)
408 {
409 case PDMAUDIOFILETYPE_RAW:
410 rc = RTStrCat(szFileName, sizeof(szFileName), ".pcm");
411 break;
412
413 case PDMAUDIOFILETYPE_WAV:
414 rc = RTStrCat(szFileName, sizeof(szFileName), ".wav");
415 break;
416
417 default:
418 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
419 break;
420 }
421
422 if (RT_FAILURE(rc))
423 break;
424
425 rc = RTPathAppend(szFilePath, sizeof(szFilePath), szFileName);
426 if (RT_FAILURE(rc))
427 break;
428
429 rc = RTStrCopy(pszFile, cchFile, szFilePath);
430
431 } while (0);
432
433 RTStrFree(pszPathTmp);
434
435 LogFlowFuncLeaveRC(rc);
436 return rc;
437}
438
439/**
440 * Creates an audio file.
441 *
442 * @returns IPRT status code.
443 * @param enmType Audio file type to open / create.
444 * @param pszFile File path of file to open or create.
445 * @param fFlags Audio file flags, PDMAUDIOFILE_FLAGS_XXX.
446 * @param ppFile Where to store the created audio file handle.
447 * Needs to be destroyed with AudioHlpFileDestroy().
448 */
449int AudioHlpFileCreate(PDMAUDIOFILETYPE enmType, const char *pszFile, uint32_t fFlags, PPDMAUDIOFILE *ppFile)
450{
451 AssertPtrReturn(pszFile, VERR_INVALID_POINTER);
452 /** @todo Validate fFlags. */
453
454 PPDMAUDIOFILE pFile = (PPDMAUDIOFILE)RTMemAlloc(sizeof(PDMAUDIOFILE));
455 if (!pFile)
456 return VERR_NO_MEMORY;
457
458 int rc = VINF_SUCCESS;
459
460 switch (enmType)
461 {
462 case PDMAUDIOFILETYPE_RAW:
463 case PDMAUDIOFILETYPE_WAV:
464 pFile->enmType = enmType;
465 break;
466
467 default:
468 rc = VERR_INVALID_PARAMETER;
469 break;
470 }
471
472 if (RT_SUCCESS(rc))
473 {
474 RTStrPrintf(pFile->szName, RT_ELEMENTS(pFile->szName), "%s", pszFile);
475 pFile->hFile = NIL_RTFILE;
476 pFile->fFlags = fFlags;
477 pFile->pvData = NULL;
478 pFile->cbData = 0;
479 }
480
481 if (RT_FAILURE(rc))
482 {
483 RTMemFree(pFile);
484 pFile = NULL;
485 }
486 else
487 *ppFile = pFile;
488
489 return rc;
490}
491
492/**
493 * Destroys a formerly created audio file.
494 *
495 * @param pFile Audio file (object) to destroy.
496 */
497void AudioHlpFileDestroy(PPDMAUDIOFILE pFile)
498{
499 if (!pFile)
500 return;
501
502 AudioHlpFileClose(pFile);
503
504 RTMemFree(pFile);
505 pFile = NULL;
506}
507
508/**
509 * Opens or creates an audio file.
510 *
511 * @returns IPRT status code.
512 * @param pFile Pointer to audio file handle to use.
513 * @param fOpen Open flags.
514 * Use PDMAUDIOFILE_DEFAULT_OPEN_FLAGS for the default open flags.
515 * @param pProps PCM properties to use.
516 */
517int AudioHlpFileOpen(PPDMAUDIOFILE pFile, uint32_t fOpen, PCPDMAUDIOPCMPROPS pProps)
518{
519 AssertPtrReturn(pFile, VERR_INVALID_POINTER);
520 /** @todo Validate fOpen flags. */
521 AssertPtrReturn(pProps, VERR_INVALID_POINTER);
522
523 int rc;
524
525 if (pFile->enmType == PDMAUDIOFILETYPE_RAW)
526 {
527 rc = RTFileOpen(&pFile->hFile, pFile->szName, fOpen);
528 }
529 else if (pFile->enmType == PDMAUDIOFILETYPE_WAV)
530 {
531 Assert(pProps->cChannels);
532 Assert(pProps->uHz);
533 Assert(pProps->cbSample);
534
535 pFile->pvData = (PAUDIOWAVFILEDATA)RTMemAllocZ(sizeof(AUDIOWAVFILEDATA));
536 if (pFile->pvData)
537 {
538 pFile->cbData = sizeof(PAUDIOWAVFILEDATA);
539
540 PAUDIOWAVFILEDATA pData = (PAUDIOWAVFILEDATA)pFile->pvData;
541 AssertPtr(pData);
542
543 /* Header. */
544 pData->Hdr.u32RIFF = AUDIO_MAKE_FOURCC('R','I','F','F');
545 pData->Hdr.u32Size = 36;
546 pData->Hdr.u32WAVE = AUDIO_MAKE_FOURCC('W','A','V','E');
547
548 pData->Hdr.u32Fmt = AUDIO_MAKE_FOURCC('f','m','t',' ');
549 pData->Hdr.u32Size1 = 16; /* Means PCM. */
550 pData->Hdr.u16AudioFormat = 1; /* PCM, linear quantization. */
551 pData->Hdr.u16NumChannels = pProps->cChannels;
552 pData->Hdr.u32SampleRate = pProps->uHz;
553 pData->Hdr.u32ByteRate = PDMAudioPropsGetBitrate(pProps) / 8;
554 pData->Hdr.u16BlockAlign = pProps->cChannels * pProps->cbSample;
555 pData->Hdr.u16BitsPerSample = pProps->cbSample * 8;
556
557 /* Data chunk. */
558 pData->Hdr.u32ID2 = AUDIO_MAKE_FOURCC('d','a','t','a');
559 pData->Hdr.u32Size2 = 0;
560
561 rc = RTFileOpen(&pFile->hFile, pFile->szName, fOpen);
562 if (RT_SUCCESS(rc))
563 {
564 rc = RTFileWrite(pFile->hFile, &pData->Hdr, sizeof(pData->Hdr), NULL);
565 if (RT_FAILURE(rc))
566 {
567 RTFileClose(pFile->hFile);
568 pFile->hFile = NIL_RTFILE;
569 }
570 }
571
572 if (RT_FAILURE(rc))
573 {
574 RTMemFree(pFile->pvData);
575 pFile->pvData = NULL;
576 pFile->cbData = 0;
577 }
578 }
579 else
580 rc = VERR_NO_MEMORY;
581 }
582 else
583 rc = VERR_INVALID_PARAMETER;
584
585 if (RT_SUCCESS(rc))
586 {
587 LogRel2(("Audio: Opened file '%s'\n", pFile->szName));
588 }
589 else
590 LogRel(("Audio: Failed opening file '%s', rc=%Rrc\n", pFile->szName, rc));
591
592 return rc;
593}
594
595/**
596 * Closes an audio file.
597 *
598 * @returns IPRT status code.
599 * @param pFile Audio file handle to close.
600 */
601int AudioHlpFileClose(PPDMAUDIOFILE pFile)
602{
603 if (!pFile)
604 return VINF_SUCCESS;
605
606 size_t cbSize = AudioHlpFileGetDataSize(pFile);
607
608 int rc = VINF_SUCCESS;
609
610 if (pFile->enmType == PDMAUDIOFILETYPE_RAW)
611 {
612 if (RTFileIsValid(pFile->hFile))
613 rc = RTFileClose(pFile->hFile);
614 }
615 else if (pFile->enmType == PDMAUDIOFILETYPE_WAV)
616 {
617 if (RTFileIsValid(pFile->hFile))
618 {
619 PAUDIOWAVFILEDATA pData = (PAUDIOWAVFILEDATA)pFile->pvData;
620 if (pData) /* The .WAV file data only is valid when a file actually has been created. */
621 {
622 /* Update the header with the current data size. */
623 RTFileWriteAt(pFile->hFile, 0, &pData->Hdr, sizeof(pData->Hdr), NULL);
624 }
625
626 rc = RTFileClose(pFile->hFile);
627 }
628
629 if (pFile->pvData)
630 {
631 RTMemFree(pFile->pvData);
632 pFile->pvData = NULL;
633 }
634 }
635 else
636 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
637
638 if ( RT_SUCCESS(rc)
639 && !cbSize
640 && !(pFile->fFlags & PDMAUDIOFILE_FLAGS_KEEP_IF_EMPTY))
641 {
642 rc = AudioHlpFileDelete(pFile);
643 }
644
645 pFile->cbData = 0;
646
647 if (RT_SUCCESS(rc))
648 {
649 pFile->hFile = NIL_RTFILE;
650 LogRel2(("Audio: Closed file '%s' (%zu bytes)\n", pFile->szName, cbSize));
651 }
652 else
653 LogRel(("Audio: Failed closing file '%s', rc=%Rrc\n", pFile->szName, rc));
654
655 return rc;
656}
657
658/**
659 * Deletes an audio file.
660 *
661 * @returns IPRT status code.
662 * @param pFile Audio file handle to delete.
663 */
664int AudioHlpFileDelete(PPDMAUDIOFILE pFile)
665{
666 AssertPtrReturn(pFile, VERR_INVALID_POINTER);
667
668 int rc = RTFileDelete(pFile->szName);
669 if (RT_SUCCESS(rc))
670 {
671 LogRel2(("Audio: Deleted file '%s'\n", pFile->szName));
672 }
673 else if (rc == VERR_FILE_NOT_FOUND) /* Don't bitch if the file is not around (anymore). */
674 rc = VINF_SUCCESS;
675
676 if (RT_FAILURE(rc))
677 LogRel(("Audio: Failed deleting file '%s', rc=%Rrc\n", pFile->szName, rc));
678
679 return rc;
680}
681
682/**
683 * Returns the raw audio data size of an audio file.
684 *
685 * Note: This does *not* include file headers and other data which does
686 * not belong to the actual PCM audio data.
687 *
688 * @returns Size (in bytes) of the raw PCM audio data.
689 * @param pFile Audio file handle to retrieve the audio data size for.
690 */
691size_t AudioHlpFileGetDataSize(PPDMAUDIOFILE pFile)
692{
693 AssertPtrReturn(pFile, 0);
694
695 size_t cbSize = 0;
696
697 if (pFile->enmType == PDMAUDIOFILETYPE_RAW)
698 {
699 cbSize = RTFileTell(pFile->hFile);
700 }
701 else if (pFile->enmType == PDMAUDIOFILETYPE_WAV)
702 {
703 PAUDIOWAVFILEDATA pData = (PAUDIOWAVFILEDATA)pFile->pvData;
704 if (pData) /* The .WAV file data only is valid when a file actually has been created. */
705 cbSize = pData->Hdr.u32Size2;
706 }
707
708 return cbSize;
709}
710
711/**
712 * Returns whether the given audio file is open and in use or not.
713 *
714 * @return bool True if open, false if not.
715 * @param pFile Audio file handle to check open status for.
716 */
717bool AudioHlpFileIsOpen(PPDMAUDIOFILE pFile)
718{
719 if (!pFile)
720 return false;
721
722 return RTFileIsValid(pFile->hFile);
723}
724
725/**
726 * Write PCM data to a wave (.WAV) file.
727 *
728 * @returns IPRT status code.
729 * @param pFile Audio file handle to write PCM data to.
730 * @param pvBuf Audio data to write.
731 * @param cbBuf Size (in bytes) of audio data to write.
732 * @param fFlags Additional write flags. Not being used at the moment and must be 0.
733 */
734int AudioHlpFileWrite(PPDMAUDIOFILE pFile, const void *pvBuf, size_t cbBuf, uint32_t fFlags)
735{
736 AssertPtrReturn(pFile, VERR_INVALID_POINTER);
737 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
738
739 AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER); /** @todo fFlags are currently not implemented. */
740
741 if (!cbBuf)
742 return VINF_SUCCESS;
743
744 AssertReturn(RTFileIsValid(pFile->hFile), VERR_WRONG_ORDER);
745
746 int rc;
747
748 if (pFile->enmType == PDMAUDIOFILETYPE_RAW)
749 {
750 rc = RTFileWrite(pFile->hFile, pvBuf, cbBuf, NULL);
751 }
752 else if (pFile->enmType == PDMAUDIOFILETYPE_WAV)
753 {
754 PAUDIOWAVFILEDATA pData = (PAUDIOWAVFILEDATA)pFile->pvData;
755 AssertPtr(pData);
756
757 rc = RTFileWrite(pFile->hFile, pvBuf, cbBuf, NULL);
758 if (RT_SUCCESS(rc))
759 {
760 pData->Hdr.u32Size += (uint32_t)cbBuf;
761 pData->Hdr.u32Size2 += (uint32_t)cbBuf;
762 }
763 }
764 else
765 rc = VERR_NOT_SUPPORTED;
766
767 return rc;
768}
769
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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