1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
|
/* $Id: AudioHlp.cpp $ */
/** @file
* Audio helper routines.
*
* These are used with both drivers and devices.
*/
/*
* Copyright (C) 2006-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/assert.h>
#include <iprt/dir.h>
#include <iprt/file.h>
#include <iprt/mem.h>
#include <iprt/string.h>
#include <iprt/uuid.h>
#include <iprt/formats/riff.h>
#define LOG_GROUP LOG_GROUP_DRV_AUDIO
#include <VBox/log.h>
#include <VBox/err.h>
#include <VBox/vmm/pdmdev.h>
#include <VBox/vmm/pdm.h>
#include <VBox/vmm/pdmaudioinline.h>
#include "AudioHlp.h"
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
typedef struct AUDIOWAVEFILEHDR
{
RTRIFFHDR Hdr;
RTRIFFWAVEFMTEXTCHUNK FmtExt;
RTRIFFCHUNK Data;
} AUDIOWAVEFILEHDR;
#if 0 /* unused, no header prototypes */
/**
* Retrieves the matching PDMAUDIOFMT for the given bits + signing flag.
*
* @return Matching PDMAUDIOFMT value.
* @retval PDMAUDIOFMT_INVALID if unsupported @a cBits value.
*
* @param cBits The number of bits in the audio format.
* @param fSigned Whether the audio format is signed @c true or not.
*/
PDMAUDIOFMT DrvAudioAudFmtBitsToFormat(uint8_t cBits, bool fSigned)
{
if (fSigned)
{
switch (cBits)
{
case 8: return PDMAUDIOFMT_S8;
case 16: return PDMAUDIOFMT_S16;
case 32: return PDMAUDIOFMT_S32;
default: AssertMsgFailedReturn(("Bogus audio bits %RU8\n", cBits), PDMAUDIOFMT_INVALID);
}
}
else
{
switch (cBits)
{
case 8: return PDMAUDIOFMT_U8;
case 16: return PDMAUDIOFMT_U16;
case 32: return PDMAUDIOFMT_U32;
default: AssertMsgFailedReturn(("Bogus audio bits %RU8\n", cBits), PDMAUDIOFMT_INVALID);
}
}
}
/**
* Returns an unique file name for this given audio connector instance.
*
* @return Allocated file name. Must be free'd using RTStrFree().
* @param uInstance Driver / device instance.
* @param pszPath Path name of the file to delete. The path must exist.
* @param pszSuffix File name suffix to use.
*/
char *DrvAudioDbgGetFileNameA(uint8_t uInstance, const char *pszPath, const char *pszSuffix)
{
char szFileName[64];
RTStrPrintf(szFileName, sizeof(szFileName), "drvAudio%RU8-%s", uInstance, pszSuffix);
char szFilePath[RTPATH_MAX];
int rc2 = RTStrCopy(szFilePath, sizeof(szFilePath), pszPath);
AssertRC(rc2);
rc2 = RTPathAppend(szFilePath, sizeof(szFilePath), szFileName);
AssertRC(rc2);
return RTStrDup(szFilePath);
}
#endif /* unused */
/**
* Checks whether a given stream configuration is valid or not.
*
* @note See notes on AudioHlpPcmPropsAreValid().
*
* Returns @c true if configuration is valid, @c false if not.
* @param pCfg Stream configuration to check.
*/
bool AudioHlpStreamCfgIsValid(PCPDMAUDIOSTREAMCFG pCfg)
{
/* Ugly! HDA attach code calls us with uninitialized (all zero) config. */
if (PDMAudioPropsHz(&pCfg->Props) != 0)
{
if (PDMAudioStrmCfgIsValid(pCfg))
{
if ( pCfg->enmDir == PDMAUDIODIR_IN
|| pCfg->enmDir == PDMAUDIODIR_OUT)
return AudioHlpPcmPropsAreValidAndSupported(&pCfg->Props);
}
}
return false;
}
/**
* Calculates the audio bit rate of the given bits per sample, the Hz and the number
* of audio channels.
*
* Divide the result by 8 to get the byte rate.
*
* @returns Bitrate.
* @param cBits Number of bits per sample.
* @param uHz Hz (Hertz) rate.
* @param cChannels Number of audio channels.
*/
uint32_t AudioHlpCalcBitrate(uint8_t cBits, uint32_t uHz, uint8_t cChannels)
{
return cBits * uHz * cChannels;
}
/**
* Checks whether given PCM properties are valid *and* supported by the audio stack or not.
*
* @returns @c true if the properties are valid and supported, @c false if not.
* @param pProps The PCM properties to check.
*
* @note Use PDMAudioPropsAreValid() to just check the validation bits.
*/
bool AudioHlpPcmPropsAreValidAndSupported(PCPDMAUDIOPCMPROPS pProps)
{
AssertPtrReturn(pProps, false);
if (!PDMAudioPropsAreValid(pProps))
return false;
/* Properties seem valid, now check if we actually support those. */
switch (PDMAudioPropsSampleSize(pProps))
{
case 1: /* 8 bit */
/* Signed / unsigned. */
break;
case 2: /* 16 bit */
/* Signed / unsigned. */
break;
/** @todo Do we need support for 24 bit samples? */
case 4: /* 32 bit */
/* Signed / unsigned. */
break;
case 8: /* 64-bit raw */
if ( !PDMAudioPropsIsSigned(pProps)
|| !pProps->fRaw)
return false;
break;
default:
return false;
}
if (!pProps->fSwapEndian) /** @todo Handling Big Endian audio data is not supported yet. */
return true;
return false;
}
/*********************************************************************************************************************************
* Audio File Helpers *
*********************************************************************************************************************************/
/**
* Constructs an unique file name, based on the given path and the audio file type.
*
* @returns VBox status code.
* @param pszDst Where to store the constructed file name.
* @param cbDst Size of the destination buffer (bytes; incl terminator).
* @param pszPath Base path to use. If NULL or empty, the user's
* temporary directory will be used.
* @param pszNameFmt A name for better identifying the file.
* @param va Arguments for @a pszNameFmt.
* @param uInstance Device / driver instance which is using this file.
* @param enmType Audio file type to construct file name for.
* @param fFlags File naming flags, AUDIOHLPFILENAME_FLAGS_XXX.
* @param chTweak Retry tweak character.
*/
static int audioHlpConstructPathWorker(char *pszDst, size_t cbDst, const char *pszPath, const char *pszNameFmt, va_list va,
uint32_t uInstance, AUDIOHLPFILETYPE enmType, uint32_t fFlags, char chTweak)
{
/*
* Validate input.
*/
AssertPtrNullReturn(pszPath, VERR_INVALID_POINTER);
AssertPtrReturn(pszNameFmt, VERR_INVALID_POINTER);
AssertReturn(!(fFlags & ~AUDIOHLPFILENAME_FLAGS_VALID_MASK), VERR_INVALID_FLAGS);
/* Validate the type and translate it into a suffix. */
const char *pszSuffix = NULL;
switch (enmType)
{
case AUDIOHLPFILETYPE_RAW: pszSuffix = ".pcm"; break;
case AUDIOHLPFILETYPE_WAV: pszSuffix = ".wav"; break;
case AUDIOHLPFILETYPE_INVALID:
case AUDIOHLPFILETYPE_32BIT_HACK:
break; /* no default */
}
AssertMsgReturn(pszSuffix, ("enmType=%d\n", enmType), VERR_INVALID_PARAMETER);
/*
* The directory. Make sure it exists and ends with a path separator.
*/
int rc;
if (!pszPath || !*pszPath)
rc = RTPathTemp(pszDst, cbDst);
else
{
AssertPtrReturn(pszDst, VERR_INVALID_POINTER);
rc = RTStrCopy(pszDst, cbDst, pszPath);
}
AssertRCReturn(rc, rc);
if (!RTDirExists(pszDst))
{
rc = RTDirCreateFullPath(pszDst, RTFS_UNIX_IRWXU);
AssertRCReturn(rc, rc);
}
size_t offDst = RTPathEnsureTrailingSeparator(pszDst, cbDst);
AssertReturn(offDst > 0, VERR_BUFFER_OVERFLOW);
Assert(offDst < cbDst);
/*
* The filename.
*/
/* Start with a ISO timestamp w/ colons replaced by dashes if requested. */
if (fFlags & AUDIOHLPFILENAME_FLAGS_TS)
{
RTTIMESPEC NowTimeSpec;
RTTIME NowUtc;
AssertReturn(RTTimeToString(RTTimeExplode(&NowUtc, RTTimeNow(&NowTimeSpec)), &pszDst[offDst], cbDst - offDst),
VERR_BUFFER_OVERFLOW);
/* Change the two colons in the time part to dashes. */
char *pchColon = &pszDst[offDst];
while ((pchColon = strchr(pchColon, ':')) != NULL)
*pchColon++ = '-';
offDst += strlen(&pszDst[offDst]);
Assert(pszDst[offDst - 1] == 'Z');
/* Append a dash to separate the timestamp from the name. */
AssertReturn(offDst + 2 <= cbDst, VERR_BUFFER_OVERFLOW);
pszDst[offDst++] = '-';
pszDst[offDst] = '\0';
}
/* Append the filename, instance, retry-tweak and suffix. */
va_list vaCopy;
va_copy(vaCopy, va);
ssize_t cchTail;
if (chTweak == '\0')
cchTail = RTStrPrintf2(&pszDst[offDst], cbDst - offDst, "%N-%u%s", pszNameFmt, &vaCopy, uInstance, pszSuffix);
else
cchTail = RTStrPrintf2(&pszDst[offDst], cbDst - offDst, "%N-%u%c%s", pszNameFmt, &vaCopy, uInstance, chTweak, pszSuffix);
va_end(vaCopy);
AssertReturn(cchTail > 0, VERR_BUFFER_OVERFLOW);
return VINF_SUCCESS;
}
/**
* Worker for AudioHlpFileCreateF and AudioHlpFileCreateAndOpenEx that allocates
* and initializes a AUDIOHLPFILE instance.
*/
static int audioHlpFileCreateWorker(PAUDIOHLPFILE *ppFile, uint32_t fFlags, AUDIOHLPFILETYPE enmType, const char *pszPath)
{
AssertReturn(!(fFlags & ~AUDIOHLPFILE_FLAGS_VALID_MASK), VERR_INVALID_FLAGS);
size_t const cbPath = strlen(pszPath) + 1;
PAUDIOHLPFILE pFile = (PAUDIOHLPFILE)RTMemAllocVar(RT_UOFFSETOF_DYN(AUDIOHLPFILE, szName[cbPath]));
AssertPtrReturn(pFile, VERR_NO_MEMORY);
pFile->enmType = enmType;
pFile->fFlags = fFlags;
pFile->cbWaveData = 0;
pFile->hFile = NIL_RTFILE;
memcpy(pFile->szName, pszPath, cbPath);
*ppFile = pFile;
return VINF_SUCCESS;
}
/**
* Creates an instance of AUDIOHLPFILE with the given filename and type.
*
* @note This does <b>NOT</b> create the file, see AudioHlpFileOpen for that.
*
* @returns VBox status code.
* @param ppFile Where to return the pointer to the audio debug file
* instance on success.
* @param fFlags AUDIOHLPFILE_FLAGS_XXX.
* @param enmType The audio file type to produce.
* @param pszPath The directory path. The temporary directory will be
* used if NULL or empty.
* @param fFilename AUDIOHLPFILENAME_FLAGS_XXX.
* @param uInstance The instance number (will be appended to the filename
* with a dash inbetween).
* @param pszNameFmt The filename format string.
* @param ... Arguments to the filename format string.
*/
int AudioHlpFileCreateF(PAUDIOHLPFILE *ppFile, uint32_t fFlags, AUDIOHLPFILETYPE enmType,
const char *pszPath, uint32_t fFilename, uint32_t uInstance, const char *pszNameFmt, ...)
{
*ppFile = NULL;
/*
* Construct the filename first.
*/
char szPath[RTPATH_MAX];
va_list va;
va_start(va, pszNameFmt);
int rc = audioHlpConstructPathWorker(szPath, sizeof(szPath), pszPath, pszNameFmt, va, uInstance, enmType, fFilename, '\0');
va_end(va);
AssertRCReturn(rc, rc);
/*
* Allocate and initializes a debug file instance with that filename path.
*/
return audioHlpFileCreateWorker(ppFile, fFlags, enmType, szPath);
}
/**
* Destroys a formerly created audio file.
*
* @param pFile Audio file (object) to destroy.
*/
void AudioHlpFileDestroy(PAUDIOHLPFILE pFile)
{
if (pFile)
{
AudioHlpFileClose(pFile);
RTMemFree(pFile);
}
}
/**
* Opens or creates an audio file.
*
* @returns VBox status code.
* @param pFile Pointer to audio file handle to use.
* @param fOpen Open flags.
* Use AUDIOHLPFILE_DEFAULT_OPEN_FLAGS for the default open flags.
* @param pProps PCM properties to use.
*/
int AudioHlpFileOpen(PAUDIOHLPFILE pFile, uint64_t fOpen, PCPDMAUDIOPCMPROPS pProps)
{
int rc;
AssertPtrReturn(pFile, VERR_INVALID_POINTER);
/** @todo Validate fOpen flags. */
AssertPtrReturn(pProps, VERR_INVALID_POINTER);
Assert(PDMAudioPropsAreValid(pProps));
/*
* Raw files just needs to be opened.
*/
if (pFile->enmType == AUDIOHLPFILETYPE_RAW)
rc = RTFileOpen(&pFile->hFile, pFile->szName, fOpen);
/*
* Wave files needs a header to be constructed and we need to take note of where
* there are sizes to update later when closing the file.
*/
else if (pFile->enmType == AUDIOHLPFILETYPE_WAV)
{
/* Construct the header. */
AUDIOWAVEFILEHDR FileHdr;
FileHdr.Hdr.uMagic = RTRIFFHDR_MAGIC;
FileHdr.Hdr.cbFile = 0; /* need to update this later */
FileHdr.Hdr.uFileType = RTRIFF_FILE_TYPE_WAVE;
FileHdr.FmtExt.Chunk.uMagic = RTRIFFWAVEFMT_MAGIC;
FileHdr.FmtExt.Chunk.cbChunk = sizeof(RTRIFFWAVEFMTEXTCHUNK) - sizeof(RTRIFFCHUNK);
FileHdr.FmtExt.Data.Core.uFormatTag = RTRIFFWAVEFMT_TAG_EXTENSIBLE;
FileHdr.FmtExt.Data.Core.cChannels = PDMAudioPropsChannels(pProps);
FileHdr.FmtExt.Data.Core.uHz = PDMAudioPropsHz(pProps);
FileHdr.FmtExt.Data.Core.cbRate = PDMAudioPropsFramesToBytes(pProps, PDMAudioPropsHz(pProps));
FileHdr.FmtExt.Data.Core.cbFrame = PDMAudioPropsFrameSize(pProps);
FileHdr.FmtExt.Data.Core.cBitsPerSample = PDMAudioPropsSampleBits(pProps);
FileHdr.FmtExt.Data.cbExtra = sizeof(FileHdr.FmtExt.Data) - sizeof(FileHdr.FmtExt.Data.Core);
FileHdr.FmtExt.Data.cValidBitsPerSample = PDMAudioPropsSampleBits(pProps);
FileHdr.FmtExt.Data.fChannelMask = 0;
for (uintptr_t idxCh = 0; idxCh < FileHdr.FmtExt.Data.Core.cChannels; idxCh++)
{
PDMAUDIOCHANNELID const idCh = (PDMAUDIOCHANNELID)pProps->aidChannels[idxCh];
AssertLogRelMsgReturn(idCh >= PDMAUDIOCHANNELID_FIRST_STANDARD && idCh < PDMAUDIOCHANNELID_END_STANDARD,
("Invalid channel ID %d for channel #%u", idCh, idxCh), VERR_INVALID_PARAMETER);
AssertLogRelMsgReturn(!(FileHdr.FmtExt.Data.fChannelMask & RT_BIT_32(idCh - PDMAUDIOCHANNELID_FIRST_STANDARD)),
("Channel #%u repeats channel ID %d", idxCh, idCh), VERR_INVALID_PARAMETER);
FileHdr.FmtExt.Data.fChannelMask |= RT_BIT_32(idCh - PDMAUDIOCHANNELID_FIRST_STANDARD);
}
RTUUID UuidTmp;
rc = RTUuidFromStr(&UuidTmp, RTRIFFWAVEFMTEXT_SUBTYPE_PCM);
AssertRCReturn(rc, rc);
FileHdr.FmtExt.Data.SubFormat = UuidTmp; /* (64-bit field maybe unaligned) */
FileHdr.Data.uMagic = RTRIFFWAVEDATACHUNK_MAGIC;
FileHdr.Data.cbChunk = 0; /* need to update this later */
/* Open the file and write out the header. */
rc = RTFileOpen(&pFile->hFile, pFile->szName, fOpen);
if (RT_SUCCESS(rc))
{
rc = RTFileWrite(pFile->hFile, &FileHdr, sizeof(FileHdr), NULL);
if (RT_FAILURE(rc))
{
RTFileClose(pFile->hFile);
pFile->hFile = NIL_RTFILE;
}
}
}
else
AssertFailedStmt(rc = VERR_INTERNAL_ERROR_3);
if (RT_SUCCESS(rc))
{
pFile->cbWaveData = 0;
LogRel2(("Audio: Opened file '%s'\n", pFile->szName));
}
else
LogRel(("Audio: Failed opening file '%s': %Rrc\n", pFile->szName, rc));
return rc;
}
/**
* Creates a debug file structure and opens a file for it, extended version.
*
* @returns VBox status code.
* @param ppFile Where to return the debug file instance on success.
* @param enmType The file type.
* @param pszDir The directory to open the file in.
* @param iInstance The device/driver instance.
* @param fFilename AUDIOHLPFILENAME_FLAGS_XXX.
* @param fCreate AUDIOHLPFILE_FLAGS_XXX.
* @param pProps PCM audio properties for the file.
* @param fOpen RTFILE_O_XXX or AUDIOHLPFILE_DEFAULT_OPEN_FLAGS.
* @param pszNameFmt The base filename.
* @param ... Filename format arguments.
*/
int AudioHlpFileCreateAndOpenEx(PAUDIOHLPFILE *ppFile, AUDIOHLPFILETYPE enmType, const char *pszDir,
uint32_t iInstance, uint32_t fFilename, uint32_t fCreate,
PCPDMAUDIOPCMPROPS pProps, uint64_t fOpen, const char *pszNameFmt, ...)
{
*ppFile = NULL;
for (uint32_t iTry = 0; ; iTry++)
{
/* Format the path to the filename. */
char szFile[RTPATH_MAX];
va_list va;
va_start(va, pszNameFmt);
int rc = audioHlpConstructPathWorker(szFile, sizeof(szFile), pszDir, pszNameFmt, va, iInstance, enmType, fFilename,
iTry == 0 ? '\0' : iTry + 'a');
va_end(va);
AssertRCReturn(rc, rc);
/* Create an debug audio file instance with the filename path. */
PAUDIOHLPFILE pFile = NULL;
rc = audioHlpFileCreateWorker(&pFile, fCreate, enmType, szFile);
AssertRCReturn(rc, rc);
/* Try open it. */
rc = AudioHlpFileOpen(pFile, fOpen, pProps);
if (RT_SUCCESS(rc))
{
*ppFile = pFile;
return rc;
}
AudioHlpFileDestroy(pFile);
AssertReturn(iTry < 16, rc);
}
}
/**
* Creates a debug wav-file structure and opens a file for it, default flags.
*
* @returns VBox status code.
* @param ppFile Where to return the debug file instance on success.
* @param pszDir The directory to open the file in.
* @param pszName The base filename.
* @param iInstance The device/driver instance.
* @param pProps PCM audio properties for the file.
*/
int AudioHlpFileCreateAndOpen(PAUDIOHLPFILE *ppFile, const char *pszDir, const char *pszName,
uint32_t iInstance, PCPDMAUDIOPCMPROPS pProps)
{
return AudioHlpFileCreateAndOpenEx(ppFile, AUDIOHLPFILETYPE_WAV, pszDir, iInstance,
AUDIOHLPFILENAME_FLAGS_NONE, AUDIOHLPFILE_FLAGS_NONE,
pProps, AUDIOHLPFILE_DEFAULT_OPEN_FLAGS, "%s", pszName);
}
/**
* Closes an audio file.
*
* @returns VBox status code.
* @param pFile Audio file handle to close.
*/
int AudioHlpFileClose(PAUDIOHLPFILE pFile)
{
if (!pFile || pFile->hFile == NIL_RTFILE)
return VINF_SUCCESS;
/*
* Wave files needs to update the data size and file size in the header.
*/
if (pFile->enmType == AUDIOHLPFILETYPE_WAV)
{
uint32_t const cbFile = sizeof(AUDIOWAVEFILEHDR) - sizeof(RTRIFFCHUNK) + (uint32_t)pFile->cbWaveData;
uint32_t const cbData = (uint32_t)pFile->cbWaveData;
int rc2;
rc2 = RTFileWriteAt(pFile->hFile, RT_UOFFSETOF(AUDIOWAVEFILEHDR, Hdr.cbFile), &cbFile, sizeof(cbFile), NULL);
AssertRC(rc2);
rc2 = RTFileWriteAt(pFile->hFile, RT_UOFFSETOF(AUDIOWAVEFILEHDR, Data.cbChunk), &cbData, sizeof(cbData), NULL);
AssertRC(rc2);
}
/*
* Do the closing.
*/
int rc = RTFileClose(pFile->hFile);
if (RT_SUCCESS(rc) || rc == VERR_INVALID_HANDLE)
pFile->hFile = NIL_RTFILE;
if (RT_SUCCESS(rc))
LogRel2(("Audio: Closed file '%s' (%'RU64 bytes PCM data)\n", pFile->szName, pFile->cbWaveData));
else
LogRel(("Audio: Failed closing file '%s': %Rrc\n", pFile->szName, rc));
/*
* Delete empty file if requested.
*/
if ( !(pFile->fFlags & AUDIOHLPFILE_FLAGS_KEEP_IF_EMPTY)
&& pFile->cbWaveData == 0
&& RT_SUCCESS(rc))
AudioHlpFileDelete(pFile);
return rc;
}
/**
* Deletes an audio file.
*
* @returns VBox status code.
* @param pFile Audio file to delete.
*/
int AudioHlpFileDelete(PAUDIOHLPFILE pFile)
{
AssertPtrReturn(pFile, VERR_INVALID_POINTER);
int rc = RTFileDelete(pFile->szName);
if (RT_SUCCESS(rc))
LogRel2(("Audio: Deleted file '%s'\n", pFile->szName));
else if (rc == VERR_FILE_NOT_FOUND) /* Don't bitch if the file is not around anymore. */
rc = VINF_SUCCESS;
if (RT_FAILURE(rc))
LogRel(("Audio: Failed deleting file '%s', rc=%Rrc\n", pFile->szName, rc));
return rc;
}
/**
* Returns whether the given audio file is open and in use or not.
*
* @returns True if open, false if not.
* @param pFile Audio file to check open status for.
*/
bool AudioHlpFileIsOpen(PAUDIOHLPFILE pFile)
{
if (!pFile || pFile->hFile == NIL_RTFILE)
return false;
return RTFileIsValid(pFile->hFile);
}
/**
* Write PCM data to a wave (.WAV) file.
*
* @returns VBox status code.
* @param pFile Audio file to write PCM data to.
* @param pvBuf Audio data to write.
* @param cbBuf Size (in bytes) of audio data to write.
*/
int AudioHlpFileWrite(PAUDIOHLPFILE pFile, const void *pvBuf, size_t cbBuf)
{
AssertPtrReturn(pFile, VERR_INVALID_POINTER);
AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
if (!cbBuf)
return VINF_SUCCESS;
int rc = RTFileWrite(pFile->hFile, pvBuf, cbBuf, NULL);
if (RT_SUCCESS(rc))
pFile->cbWaveData += cbBuf;
return rc;
}
|