VirtualBox

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

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

Audio: Removed PDMAUDIOSTREAMCFG::enmLayout and PDMAUDIOSTREAMLAYOUT. bugref:9890

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 23.8 KB
 
1/* $Id: AudioHlp.cpp 89379 2021-05-30 14:33:49Z 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 * Checks whether a given stream configuration is valid or not.
146 *
147 * @note See notes on AudioHlpPcmPropsAreValid().
148 *
149 * Returns @c true if configuration is valid, @c false if not.
150 * @param pCfg Stream configuration to check.
151 */
152bool AudioHlpStreamCfgIsValid(PCPDMAUDIOSTREAMCFG pCfg)
153{
154 /* Ugly! HDA attach code calls us with uninitialized (all zero) config. */
155 if (PDMAudioPropsHz(&pCfg->Props) != 0)
156 {
157 if (PDMAudioStrmCfgIsValid(pCfg))
158 {
159 if ( pCfg->enmDir == PDMAUDIODIR_IN
160 || pCfg->enmDir == PDMAUDIODIR_OUT)
161 return AudioHlpPcmPropsAreValid(&pCfg->Props);
162 }
163 }
164 return false;
165}
166
167/**
168 * Calculates the audio bit rate of the given bits per sample, the Hz and the number
169 * of audio channels.
170 *
171 * Divide the result by 8 to get the byte rate.
172 *
173 * @returns Bitrate.
174 * @param cBits Number of bits per sample.
175 * @param uHz Hz (Hertz) rate.
176 * @param cChannels Number of audio channels.
177 */
178uint32_t AudioHlpCalcBitrate(uint8_t cBits, uint32_t uHz, uint8_t cChannels)
179{
180 return cBits * uHz * cChannels;
181}
182
183
184/**
185 * Checks whether given PCM properties are valid or not.
186 *
187 * @note This is more of a supported than valid check. There is code for
188 * unsigned samples elsewhere (like DrvAudioHlpClearBuf()), but this
189 * function will flag such properties as not valid.
190 *
191 * @todo r=bird: See note and explain properly. Perhaps rename to
192 * AudioHlpPcmPropsAreValidAndSupported?
193 *
194 * @returns @c true if the properties are valid, @c false if not.
195 * @param pProps The PCM properties to check.
196 */
197bool AudioHlpPcmPropsAreValid(PCPDMAUDIOPCMPROPS pProps)
198{
199 AssertPtrReturn(pProps, false);
200 AssertReturn(PDMAudioPropsAreValid(pProps), false);
201
202 /* Minimum 1 channel (mono), maximum 7.1 (= 8) channels. */
203 if (PDMAudioPropsChannels(pProps) >= 1 && PDMAudioPropsChannels(pProps) <= 8)
204 {
205 switch (PDMAudioPropsSampleSize(pProps))
206 {
207 case 1: /* 8 bit */
208 if (PDMAudioPropsIsSigned(pProps))
209 return false;
210 break;
211 case 2: /* 16 bit */
212 if (!PDMAudioPropsIsSigned(pProps))
213 return false;
214 break;
215 /** @todo Do we need support for 24 bit samples? */
216 case 4: /* 32 bit */
217 if (!PDMAudioPropsIsSigned(pProps))
218 return false;
219 break;
220 case 8: /* 64-bit raw */
221 if ( !PDMAudioPropsIsSigned(pProps)
222 || !pProps->fRaw)
223 return false;
224 break;
225 default:
226 return false;
227 }
228
229 if (pProps->uHz > 0)
230 {
231 if (!pProps->fSwapEndian) /** @todo Handling Big Endian audio data is not supported yet. */
232 return true;
233 }
234 }
235 return false;
236}
237
238
239/*********************************************************************************************************************************
240* Audio File Helpers *
241*********************************************************************************************************************************/
242
243/**
244 * Sanitizes the file name component so that unsupported characters
245 * will be replaced by an underscore ("_").
246 *
247 * @returns VBox status code.
248 * @param pszPath Path to sanitize.
249 * @param cbPath Size (in bytes) of path to sanitize.
250 */
251int AudioHlpFileNameSanitize(char *pszPath, size_t cbPath)
252{
253 RT_NOREF(cbPath);
254 int rc = VINF_SUCCESS;
255#ifdef RT_OS_WINDOWS
256 /* Filter out characters not allowed on Windows platforms, put in by
257 RTTimeSpecToString(). */
258 /** @todo Use something like RTPathSanitize() if available later some time. */
259 static RTUNICP const s_uszValidRangePairs[] =
260 {
261 ' ', ' ',
262 '(', ')',
263 '-', '.',
264 '0', '9',
265 'A', 'Z',
266 'a', 'z',
267 '_', '_',
268 0xa0, 0xd7af,
269 '\0'
270 };
271 ssize_t cReplaced = RTStrPurgeComplementSet(pszPath, s_uszValidRangePairs, '_' /* Replacement */);
272 if (cReplaced < 0)
273 rc = VERR_INVALID_UTF8_ENCODING;
274#else
275 RT_NOREF(pszPath);
276#endif
277 return rc;
278}
279
280/**
281 * Constructs an unique file name, based on the given path and the audio file type.
282 *
283 * @returns VBox status code.
284 * @param pszFile Where to store the constructed file name.
285 * @param cchFile Size (in characters) of the file name buffer.
286 * @param pszPath Base path to use.
287 * If NULL or empty, the system's temporary directory will be used.
288 * @param pszName A name for better identifying the file.
289 * @param uInstance Device / driver instance which is using this file.
290 * @param enmType Audio file type to construct file name for.
291 * @param fFlags File naming flags, AUDIOHLPFILENAME_FLAGS_XXX.
292 */
293int AudioHlpFileNameGet(char *pszFile, size_t cchFile, const char *pszPath, const char *pszName,
294 uint32_t uInstance, AUDIOHLPFILETYPE enmType, uint32_t fFlags)
295{
296 AssertPtrReturn(pszFile, VERR_INVALID_POINTER);
297 AssertReturn(cchFile, VERR_INVALID_PARAMETER);
298 /* pszPath can be NULL. */
299 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
300 /** @todo Validate fFlags. */
301
302 int rc;
303
304 char *pszPathTmp = NULL;
305
306 do
307 {
308 if ( pszPath == NULL
309 || !strlen(pszPath))
310 {
311 char szTemp[RTPATH_MAX];
312 rc = RTPathTemp(szTemp, sizeof(szTemp));
313 if (RT_SUCCESS(rc))
314 {
315 pszPathTmp = RTStrDup(szTemp);
316 }
317 else
318 break;
319 }
320 else
321 pszPathTmp = RTStrDup(pszPath);
322
323 AssertPtrBreakStmt(pszPathTmp, rc = VERR_NO_MEMORY);
324
325 char szFilePath[RTPATH_MAX];
326 rc = RTStrCopy(szFilePath, sizeof(szFilePath), pszPathTmp);
327 AssertRCBreak(rc);
328
329 /* Create it when necessary. */
330 if (!RTDirExists(szFilePath))
331 {
332 rc = RTDirCreateFullPath(szFilePath, RTFS_UNIX_IRWXU);
333 if (RT_FAILURE(rc))
334 break;
335 }
336
337 char szFileName[RTPATH_MAX];
338 szFileName[0] = '\0';
339
340 if (fFlags & AUDIOHLPFILENAME_FLAGS_TS)
341 {
342 RTTIMESPEC time;
343 if (!RTTimeSpecToString(RTTimeNow(&time), szFileName, sizeof(szFileName)))
344 {
345 rc = VERR_BUFFER_OVERFLOW;
346 break;
347 }
348
349 rc = AudioHlpFileNameSanitize(szFileName, sizeof(szFileName));
350 if (RT_FAILURE(rc))
351 break;
352
353 rc = RTStrCat(szFileName, sizeof(szFileName), "-");
354 if (RT_FAILURE(rc))
355 break;
356 }
357
358 rc = RTStrCat(szFileName, sizeof(szFileName), pszName);
359 if (RT_FAILURE(rc))
360 break;
361
362 rc = RTStrCat(szFileName, sizeof(szFileName), "-");
363 if (RT_FAILURE(rc))
364 break;
365
366 char szInst[16];
367 RTStrPrintf2(szInst, sizeof(szInst), "%RU32", uInstance);
368 rc = RTStrCat(szFileName, sizeof(szFileName), szInst);
369 if (RT_FAILURE(rc))
370 break;
371
372 switch (enmType)
373 {
374 case AUDIOHLPFILETYPE_RAW:
375 rc = RTStrCat(szFileName, sizeof(szFileName), ".pcm");
376 break;
377
378 case AUDIOHLPFILETYPE_WAV:
379 rc = RTStrCat(szFileName, sizeof(szFileName), ".wav");
380 break;
381
382 default:
383 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
384 break;
385 }
386
387 if (RT_FAILURE(rc))
388 break;
389
390 rc = RTPathAppend(szFilePath, sizeof(szFilePath), szFileName);
391 if (RT_FAILURE(rc))
392 break;
393
394 rc = RTStrCopy(pszFile, cchFile, szFilePath);
395
396 } while (0);
397
398 RTStrFree(pszPathTmp);
399
400 LogFlowFuncLeaveRC(rc);
401 return rc;
402}
403
404/**
405 * Creates an audio file.
406 *
407 * @returns VBox status code.
408 * @param enmType Audio file type to open / create.
409 * @param pszFile File path of file to open or create.
410 * @param fFlags Audio file flags, AUDIOHLPFILE_FLAGS_XXX.
411 * @param ppFile Where to store the created audio file handle.
412 * Needs to be destroyed with AudioHlpFileDestroy().
413 */
414int AudioHlpFileCreate(AUDIOHLPFILETYPE enmType, const char *pszFile, uint32_t fFlags, PAUDIOHLPFILE *ppFile)
415{
416 AssertPtrReturn(pszFile, VERR_INVALID_POINTER);
417 /** @todo Validate fFlags. */
418
419 PAUDIOHLPFILE pFile = (PAUDIOHLPFILE)RTMemAlloc(sizeof(AUDIOHLPFILE));
420 if (!pFile)
421 return VERR_NO_MEMORY;
422
423 int rc = VINF_SUCCESS;
424
425 switch (enmType)
426 {
427 case AUDIOHLPFILETYPE_RAW:
428 case AUDIOHLPFILETYPE_WAV:
429 pFile->enmType = enmType;
430 break;
431
432 default:
433 rc = VERR_INVALID_PARAMETER;
434 break;
435 }
436
437 if (RT_SUCCESS(rc))
438 {
439 RTStrPrintf(pFile->szName, RT_ELEMENTS(pFile->szName), "%s", pszFile);
440 pFile->hFile = NIL_RTFILE;
441 pFile->fFlags = fFlags;
442 pFile->pvData = NULL;
443 pFile->cbData = 0;
444 }
445
446 if (RT_FAILURE(rc))
447 {
448 RTMemFree(pFile);
449 pFile = NULL;
450 }
451 else
452 *ppFile = pFile;
453
454 return rc;
455}
456
457/**
458 * Destroys a formerly created audio file.
459 *
460 * @param pFile Audio file (object) to destroy.
461 */
462void AudioHlpFileDestroy(PAUDIOHLPFILE pFile)
463{
464 if (!pFile)
465 return;
466
467 AudioHlpFileClose(pFile);
468
469 RTMemFree(pFile);
470 pFile = NULL;
471}
472
473/**
474 * Opens or creates an audio file.
475 *
476 * @returns VBox status code.
477 * @param pFile Pointer to audio file handle to use.
478 * @param fOpen Open flags.
479 * Use AUDIOHLPFILE_DEFAULT_OPEN_FLAGS for the default open flags.
480 * @param pProps PCM properties to use.
481 */
482int AudioHlpFileOpen(PAUDIOHLPFILE pFile, uint32_t fOpen, PCPDMAUDIOPCMPROPS pProps)
483{
484 AssertPtrReturn(pFile, VERR_INVALID_POINTER);
485 /** @todo Validate fOpen flags. */
486 AssertPtrReturn(pProps, VERR_INVALID_POINTER);
487 Assert(PDMAudioPropsAreValid(pProps));
488
489 int rc;
490
491 if (pFile->enmType == AUDIOHLPFILETYPE_RAW)
492 {
493 rc = RTFileOpen(&pFile->hFile, pFile->szName, fOpen);
494 }
495 else if (pFile->enmType == AUDIOHLPFILETYPE_WAV)
496 {
497 pFile->pvData = (PAUDIOWAVFILEDATA)RTMemAllocZ(sizeof(AUDIOWAVFILEDATA));
498 if (pFile->pvData)
499 {
500 pFile->cbData = sizeof(PAUDIOWAVFILEDATA);
501
502 PAUDIOWAVFILEDATA pData = (PAUDIOWAVFILEDATA)pFile->pvData;
503 AssertPtr(pData);
504
505 /* Header. */
506 pData->Hdr.u32RIFF = AUDIO_MAKE_FOURCC('R','I','F','F');
507 pData->Hdr.u32Size = 36;
508 pData->Hdr.u32WAVE = AUDIO_MAKE_FOURCC('W','A','V','E');
509
510 pData->Hdr.u32Fmt = AUDIO_MAKE_FOURCC('f','m','t',' ');
511 pData->Hdr.u32Size1 = 16; /* Means PCM. */
512 pData->Hdr.u16AudioFormat = 1; /* PCM, linear quantization. */
513 pData->Hdr.u16NumChannels = PDMAudioPropsChannels(pProps);
514 pData->Hdr.u32SampleRate = pProps->uHz;
515 pData->Hdr.u32ByteRate = PDMAudioPropsGetBitrate(pProps) / 8;
516 pData->Hdr.u16BlockAlign = PDMAudioPropsFrameSize(pProps);
517 pData->Hdr.u16BitsPerSample = PDMAudioPropsSampleBits(pProps);
518
519 /* Data chunk. */
520 pData->Hdr.u32ID2 = AUDIO_MAKE_FOURCC('d','a','t','a');
521 pData->Hdr.u32Size2 = 0;
522
523 rc = RTFileOpen(&pFile->hFile, pFile->szName, fOpen);
524 if (RT_SUCCESS(rc))
525 {
526 rc = RTFileWrite(pFile->hFile, &pData->Hdr, sizeof(pData->Hdr), NULL);
527 if (RT_FAILURE(rc))
528 {
529 RTFileClose(pFile->hFile);
530 pFile->hFile = NIL_RTFILE;
531 }
532 }
533
534 if (RT_FAILURE(rc))
535 {
536 RTMemFree(pFile->pvData);
537 pFile->pvData = NULL;
538 pFile->cbData = 0;
539 }
540 }
541 else
542 rc = VERR_NO_MEMORY;
543 }
544 else
545 rc = VERR_INVALID_PARAMETER;
546
547 if (RT_SUCCESS(rc))
548 LogRel2(("Audio: Opened file '%s'\n", pFile->szName));
549 else
550 LogRel(("Audio: Failed opening file '%s', rc=%Rrc\n", pFile->szName, rc));
551
552 return rc;
553}
554
555/**
556 * Creates a debug file structure and opens a file for it, extended version.
557 *
558 * @returns VBox status code.
559 * @param ppFile Where to return the debug file instance on success.
560 * @param enmType The file type.
561 * @param pszDir The directory to open the file in.
562 * @param pszName The base filename.
563 * @param iInstance The device/driver instance.
564 * @param fFilename AUDIOHLPFILENAME_FLAGS_XXX.
565 * @param fCreate AUDIOHLPFILE_FLAGS_XXX.
566 * @param pProps PCM audio properties for the file.
567 * @param fOpen RTFILE_O_XXX or AUDIOHLPFILE_DEFAULT_OPEN_FLAGS.
568 */
569int AudioHlpFileCreateAndOpenEx(PAUDIOHLPFILE *ppFile, AUDIOHLPFILETYPE enmType, const char *pszDir, const char *pszName,
570 uint32_t iInstance, uint32_t fFilename, uint32_t fCreate,
571 PCPDMAUDIOPCMPROPS pProps, uint64_t fOpen)
572{
573 char szFile[RTPATH_MAX];
574 int rc = AudioHlpFileNameGet(szFile, sizeof(szFile), pszDir, pszName, iInstance, enmType, fFilename);
575 if (RT_SUCCESS(rc))
576 {
577 PAUDIOHLPFILE pFile = NULL;
578 rc = AudioHlpFileCreate(enmType, szFile, fCreate, &pFile);
579 if (RT_SUCCESS(rc))
580 {
581 rc = AudioHlpFileOpen(pFile, fOpen, pProps);
582 if (RT_SUCCESS(rc))
583 {
584 *ppFile = pFile;
585 return rc;
586 }
587 AudioHlpFileDestroy(pFile);
588 }
589 }
590 *ppFile = NULL;
591 return rc;
592}
593
594/**
595 * Creates a debug wav-file structure and opens a file for it, default flags.
596 *
597 * @returns VBox status code.
598 * @param ppFile Where to return the debug file instance on success.
599 * @param pszDir The directory to open the file in.
600 * @param pszName The base filename.
601 * @param iInstance The device/driver instance.
602 * @param pProps PCM audio properties for the file.
603 */
604int AudioHlpFileCreateAndOpen(PAUDIOHLPFILE *ppFile, const char *pszDir, const char *pszName,
605 uint32_t iInstance, PCPDMAUDIOPCMPROPS pProps)
606{
607 return AudioHlpFileCreateAndOpenEx(ppFile, AUDIOHLPFILETYPE_WAV, pszDir, pszName, iInstance,
608 AUDIOHLPFILENAME_FLAGS_NONE, AUDIOHLPFILE_FLAGS_NONE,
609 pProps, AUDIOHLPFILE_DEFAULT_OPEN_FLAGS);
610}
611
612
613/**
614 * Closes an audio file.
615 *
616 * @returns VBox status code.
617 * @param pFile Audio file handle to close.
618 */
619int AudioHlpFileClose(PAUDIOHLPFILE pFile)
620{
621 if (!pFile)
622 return VINF_SUCCESS;
623
624 size_t cbSize = AudioHlpFileGetDataSize(pFile);
625
626 int rc = VINF_SUCCESS;
627
628 if (pFile->enmType == AUDIOHLPFILETYPE_RAW)
629 {
630 if (RTFileIsValid(pFile->hFile))
631 rc = RTFileClose(pFile->hFile);
632 }
633 else if (pFile->enmType == AUDIOHLPFILETYPE_WAV)
634 {
635 if (RTFileIsValid(pFile->hFile))
636 {
637 PAUDIOWAVFILEDATA pData = (PAUDIOWAVFILEDATA)pFile->pvData;
638 if (pData) /* The .WAV file data only is valid when a file actually has been created. */
639 {
640 /* Update the header with the current data size. */
641 RTFileWriteAt(pFile->hFile, 0, &pData->Hdr, sizeof(pData->Hdr), NULL);
642 }
643
644 rc = RTFileClose(pFile->hFile);
645 }
646
647 if (pFile->pvData)
648 {
649 RTMemFree(pFile->pvData);
650 pFile->pvData = NULL;
651 }
652 }
653 else
654 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
655
656 if ( RT_SUCCESS(rc)
657 && !cbSize
658 && !(pFile->fFlags & AUDIOHLPFILE_FLAGS_KEEP_IF_EMPTY))
659 {
660 rc = AudioHlpFileDelete(pFile);
661 }
662
663 pFile->cbData = 0;
664
665 if (RT_SUCCESS(rc))
666 {
667 pFile->hFile = NIL_RTFILE;
668 LogRel2(("Audio: Closed file '%s' (%zu bytes)\n", pFile->szName, cbSize));
669 }
670 else
671 LogRel(("Audio: Failed closing file '%s', rc=%Rrc\n", pFile->szName, rc));
672
673 return rc;
674}
675
676/**
677 * Deletes an audio file.
678 *
679 * @returns VBox status code.
680 * @param pFile Audio file handle to delete.
681 */
682int AudioHlpFileDelete(PAUDIOHLPFILE pFile)
683{
684 AssertPtrReturn(pFile, VERR_INVALID_POINTER);
685
686 int rc = RTFileDelete(pFile->szName);
687 if (RT_SUCCESS(rc))
688 {
689 LogRel2(("Audio: Deleted file '%s'\n", pFile->szName));
690 }
691 else if (rc == VERR_FILE_NOT_FOUND) /* Don't bitch if the file is not around (anymore). */
692 rc = VINF_SUCCESS;
693
694 if (RT_FAILURE(rc))
695 LogRel(("Audio: Failed deleting file '%s', rc=%Rrc\n", pFile->szName, rc));
696
697 return rc;
698}
699
700/**
701 * Returns the raw audio data size of an audio file.
702 *
703 * Note: This does *not* include file headers and other data which does
704 * not belong to the actual PCM audio data.
705 *
706 * @returns Size (in bytes) of the raw PCM audio data.
707 * @param pFile Audio file handle to retrieve the audio data size for.
708 */
709size_t AudioHlpFileGetDataSize(PAUDIOHLPFILE pFile)
710{
711 AssertPtrReturn(pFile, 0);
712
713 size_t cbSize = 0;
714
715 if (pFile->enmType == AUDIOHLPFILETYPE_RAW)
716 {
717 cbSize = RTFileTell(pFile->hFile);
718 }
719 else if (pFile->enmType == AUDIOHLPFILETYPE_WAV)
720 {
721 PAUDIOWAVFILEDATA pData = (PAUDIOWAVFILEDATA)pFile->pvData;
722 if (pData) /* The .WAV file data only is valid when a file actually has been created. */
723 cbSize = pData->Hdr.u32Size2;
724 }
725
726 return cbSize;
727}
728
729/**
730 * Returns whether the given audio file is open and in use or not.
731 *
732 * @return bool True if open, false if not.
733 * @param pFile Audio file handle to check open status for.
734 */
735bool AudioHlpFileIsOpen(PAUDIOHLPFILE pFile)
736{
737 if (!pFile)
738 return false;
739
740 return RTFileIsValid(pFile->hFile);
741}
742
743/**
744 * Write PCM data to a wave (.WAV) file.
745 *
746 * @returns VBox status code.
747 * @param pFile Audio file handle to write PCM data to.
748 * @param pvBuf Audio data to write.
749 * @param cbBuf Size (in bytes) of audio data to write.
750 * @param fFlags Additional write flags. Not being used at the moment and must be 0.
751 */
752int AudioHlpFileWrite(PAUDIOHLPFILE pFile, const void *pvBuf, size_t cbBuf, uint32_t fFlags)
753{
754 AssertPtrReturn(pFile, VERR_INVALID_POINTER);
755 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
756
757 AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER); /** @todo fFlags are currently not implemented. */
758
759 if (!cbBuf)
760 return VINF_SUCCESS;
761
762 AssertReturn(RTFileIsValid(pFile->hFile), VERR_WRONG_ORDER);
763
764 int rc;
765
766 if (pFile->enmType == AUDIOHLPFILETYPE_RAW)
767 {
768 rc = RTFileWrite(pFile->hFile, pvBuf, cbBuf, NULL);
769 }
770 else if (pFile->enmType == AUDIOHLPFILETYPE_WAV)
771 {
772 PAUDIOWAVFILEDATA pData = (PAUDIOWAVFILEDATA)pFile->pvData;
773 AssertPtr(pData);
774
775 rc = RTFileWrite(pFile->hFile, pvBuf, cbBuf, NULL);
776 if (RT_SUCCESS(rc))
777 {
778 pData->Hdr.u32Size += (uint32_t)cbBuf;
779 pData->Hdr.u32Size2 += (uint32_t)cbBuf;
780 }
781 }
782 else
783 rc = VERR_NOT_SUPPORTED;
784
785 return rc;
786}
787
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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