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