WaveFile.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __WAVEFILE_H
  21. #define __WAVEFILE_H
  22. /*
  23. ================================================================================================
  24. Contains the WaveFile declaration.
  25. ================================================================================================
  26. */
  27. /*
  28. ================================================
  29. idWaveFile is used for reading generic RIFF WAVE files.
  30. ================================================
  31. */
  32. class idWaveFile {
  33. public:
  34. ID_INLINE idWaveFile();
  35. ID_INLINE ~idWaveFile();
  36. bool Open( const char * filename );
  37. void Close();
  38. uint32 SeekToChunk( uint32 id );
  39. size_t Read( void * buffer, size_t len ) { return file->Read( buffer, len ); }
  40. uint32 GetChunkOffset( uint32 id );
  41. ID_TIME_T Timestamp() { return file->Timestamp(); }
  42. const char * Name() { return ( file == NULL ? "" : file->GetName() ); }
  43. // This maps to the channel mask in waveFmtExtensible_t
  44. enum {
  45. CHANNEL_INDEX_FRONT_LEFT,
  46. CHANNEL_INDEX_FRONT_RIGHT,
  47. CHANNEL_INDEX_FRONT_CENTER,
  48. CHANNEL_INDEX_LOW_FREQUENCY,
  49. CHANNEL_INDEX_BACK_LEFT,
  50. CHANNEL_INDEX_BACK_RIGHT,
  51. CHANNEL_INDEX_FRONT_LEFT_CENTER,
  52. CHANNEL_INDEX_FRONT_RIGHT_CENTER,
  53. CHANNEL_INDEX_BACK_CENTER,
  54. CHANNEL_INDEX_SIDE_LEFT,
  55. CHANNEL_INDEX_SIDE_RIGHT,
  56. CHANNEL_INDEX_MAX
  57. };
  58. enum {
  59. CHANNEL_MASK_FRONT_LEFT = BIT( CHANNEL_INDEX_FRONT_LEFT ),
  60. CHANNEL_MASK_FRONT_RIGHT = BIT( CHANNEL_INDEX_FRONT_RIGHT ),
  61. CHANNEL_MASK_FRONT_CENTER = BIT( CHANNEL_INDEX_FRONT_CENTER ),
  62. CHANNEL_MASK_LOW_FREQUENCY = BIT( CHANNEL_INDEX_LOW_FREQUENCY ),
  63. CHANNEL_MASK_BACK_LEFT = BIT( CHANNEL_INDEX_BACK_LEFT ),
  64. CHANNEL_MASK_BACK_RIGHT = BIT( CHANNEL_INDEX_BACK_RIGHT ),
  65. CHANNEL_MASK_FRONT_LEFT_CENTER = BIT( CHANNEL_INDEX_FRONT_LEFT_CENTER ),
  66. CHANNEL_MASK_FRONT_RIGHT_CENTER = BIT( CHANNEL_INDEX_FRONT_RIGHT_CENTER ),
  67. CHANNEL_MASK_BACK_CENTER = BIT( CHANNEL_INDEX_BACK_CENTER ),
  68. CHANNEL_MASK_SIDE_LEFT = BIT( CHANNEL_INDEX_SIDE_LEFT ),
  69. CHANNEL_MASK_SIDE_RIGHT = BIT( CHANNEL_INDEX_SIDE_RIGHT ),
  70. CHANNEL_MASK_ALL = BIT( CHANNEL_INDEX_MAX ) - 1,
  71. };
  72. // This matches waveFmt_t::formatTag
  73. // These are the only wave formats that we understand
  74. enum {
  75. FORMAT_UNKNOWN = 0x0000,
  76. FORMAT_PCM = 0x0001,
  77. FORMAT_ADPCM = 0x0002,
  78. FORMAT_XMA2 = 0x0166,
  79. FORMAT_EXTENSIBLE = 0xFFFF,
  80. };
  81. #pragma pack( push, 1 )
  82. struct waveFmt_t {
  83. static const uint32 id = 'fmt ';
  84. // This is the basic data we'd expect to see in any valid wave file
  85. struct basic_t {
  86. uint16 formatTag;
  87. uint16 numChannels;
  88. uint32 samplesPerSec;
  89. uint32 avgBytesPerSec;
  90. uint16 blockSize;
  91. uint16 bitsPerSample;
  92. } basic;
  93. // Some wav file formats have extra data after the basic header
  94. uint16 extraSize;
  95. // We have a few known formats that we handle:
  96. union extra_t {
  97. // Valid if basic.formatTag == FORMAT_EXTENSIBLE
  98. struct extensible_t {
  99. uint16 validBitsPerSample; // Valid bits in each sample container
  100. uint32 channelMask; // Positions of the audio channels
  101. struct guid_t {
  102. uint32 data1;
  103. uint16 data2;
  104. uint16 data3;
  105. uint16 data4;
  106. byte data5[ 6 ];
  107. } subFormat; // Format identifier GUID
  108. } extensible;
  109. // Valid if basic.formatTag == FORMAT_ADPCM
  110. // The microsoft ADPCM struct has a zero-sized array at the end
  111. // but the array is always 7 entries, so we set it to that size
  112. // so we can embed it in our format union. Otherwise, the struct
  113. // is exactly the same as the one in audiodefs.h
  114. struct adpcm_t {
  115. uint16 samplesPerBlock;
  116. uint16 numCoef;
  117. struct adpcmcoef_t {
  118. short coef1;
  119. short coef2;
  120. } aCoef[7]; // Always 7 coefficient pairs for MS ADPCM
  121. } adpcm;
  122. // Valid if basic.formatTag == FORMAT_XMA2
  123. struct xma2_t {
  124. uint16 numStreams; // Number of audio streams (1 or 2 channels each)
  125. uint32 channelMask; // matches the CHANNEL_MASK enum above
  126. uint32 samplesEncoded; // Total number of PCM samples the file decodes to
  127. uint32 bytesPerBlock; // XMA block size (but the last one may be shorter)
  128. uint32 playBegin; // First valid sample in the decoded audio
  129. uint32 playLength; // Length of the valid part of the decoded audio
  130. uint32 loopBegin; // Beginning of the loop region in decoded sample terms
  131. uint32 loopLength; // Length of the loop region in decoded sample terms
  132. byte loopCount; // Number of loop repetitions; 255 = infinite
  133. byte encoderVersion; // Version of XMA encoder that generated the file
  134. uint16 blockCount; // XMA blocks in file (and entries in its seek table)
  135. } xma2;
  136. } extra;
  137. };
  138. #pragma pack( pop )
  139. struct dataChunk_t {
  140. static const uint32 id = 'data';
  141. uint32 size;
  142. void * data;
  143. };
  144. struct formatChunk_t {
  145. static const uint32 id = 'fmt ';
  146. uint32 size;
  147. uint16 compressionCode;
  148. uint16 numChannels;
  149. uint32 sampleRate;
  150. uint32 averageBytesPerSecond;
  151. uint16 blockAlign;
  152. uint16 bitsPerSample;
  153. uint16 numExtraFormatByte;
  154. };
  155. struct samplerChunk_t {
  156. static const uint32 id = 'smpl';
  157. uint32 manufacturer; // ignored
  158. uint32 product; // ignored
  159. uint32 samplePeriod; // ignored (normally 1000000000/samplesPerSec)
  160. uint32 MIDIUnityNote; // ignored
  161. uint32 MIDIPitchFraction; // ignored
  162. uint32 SMPTEFormat; // ignored
  163. uint32 SMPTEOffset; // ignored
  164. uint32 numSampleLoops; // number of samples in wave file
  165. uint32 extraSamplerData; // ignored, should always be 0
  166. };
  167. struct sampleData_t {
  168. uint32 identifier; // ignored
  169. uint32 type; // 0 for loop 33 multi-sample sample type
  170. uint32 start; // start of the loop point
  171. uint32 end; // end of the loop point
  172. uint32 fraction; // ignored
  173. uint32 playCount; // ignored
  174. };
  175. const char * ReadWaveFormat( waveFmt_t & waveFmt );
  176. static bool ReadWaveFormatDirect( waveFmt_t & format, idFile *file );
  177. static bool WriteWaveFormatDirect( waveFmt_t & format, idFile *file );
  178. static bool WriteSampleDataDirect( idList< sampleData_t > & sampleData, idFile *file );
  179. static bool WriteDataDirect( char * _data, uint32 size, idFile * file );
  180. static bool WriteHeaderDirect( uint32 fileSize, idFile * file );
  181. bool ReadLoopData( int & start, int & end );
  182. private:
  183. idFile * file;
  184. struct chunk_t {
  185. uint32 id;
  186. uint32 size;
  187. uint32 offset;
  188. };
  189. idStaticList< chunk_t, 32 > chunks;
  190. };
  191. /*
  192. ========================
  193. idWaveFile::idWaveFile
  194. ========================
  195. */
  196. ID_INLINE idWaveFile::idWaveFile() : file( NULL ) {
  197. }
  198. /*
  199. ========================
  200. idWaveFile::~idWaveFile
  201. ========================
  202. */
  203. ID_INLINE idWaveFile::~idWaveFile() {
  204. Close();
  205. }
  206. #endif // !__WAVEFILE_H__