snd_wavefile.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 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 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. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "snd_local.h"
  23. //-----------------------------------------------------------------------------
  24. // Name: idWaveFile::idWaveFile()
  25. // Desc: Constructs the class. Call Open() to open a wave file for reading.
  26. // Then call Read() as needed. Calling the destructor or Close()
  27. // will close the file.
  28. //-----------------------------------------------------------------------------
  29. idWaveFile::idWaveFile( void ) {
  30. memset( &mpwfx, 0, sizeof( waveformatextensible_t ) );
  31. mhmmio = NULL;
  32. mdwSize = 0;
  33. mseekBase = 0;
  34. mbIsReadingFromMemory = false;
  35. mpbData = NULL;
  36. ogg = NULL;
  37. isOgg = false;
  38. }
  39. //-----------------------------------------------------------------------------
  40. // Name: idWaveFile::~idWaveFile()
  41. // Desc: Destructs the class
  42. //-----------------------------------------------------------------------------
  43. idWaveFile::~idWaveFile( void ) {
  44. Close();
  45. if ( mbIsReadingFromMemory && mpbData ) {
  46. Mem_Free( mpbData );
  47. }
  48. memset( &mpwfx, 0, sizeof( waveformatextensible_t ) );
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Name: idWaveFile::Open()
  52. // Desc: Opens a wave file for reading
  53. //-----------------------------------------------------------------------------
  54. int idWaveFile::Open( const char* strFileName, waveformatex_t* pwfx ) {
  55. mbIsReadingFromMemory = false;
  56. mpbData = NULL;
  57. mpbDataCur = mpbData;
  58. if( strFileName == NULL ) {
  59. return -1;
  60. }
  61. idStr name = strFileName;
  62. // note: used to only check for .wav when making a build
  63. name.SetFileExtension( ".ogg" );
  64. if ( fileSystem->ReadFile( name, NULL, NULL ) != -1 ) {
  65. return OpenOGG( name, pwfx );
  66. }
  67. memset( &mpwfx, 0, sizeof( waveformatextensible_t ) );
  68. mhmmio = fileSystem->OpenFileRead( strFileName );
  69. if ( !mhmmio ) {
  70. mdwSize = 0;
  71. return -1;
  72. }
  73. if ( mhmmio->Length() <= 0 ) {
  74. mhmmio = NULL;
  75. return -1;
  76. }
  77. if ( ReadMMIO() != 0 ) {
  78. // ReadMMIO will fail if its an not a wave file
  79. Close();
  80. return -1;
  81. }
  82. mfileTime = mhmmio->Timestamp();
  83. if ( ResetFile() != 0 ) {
  84. Close();
  85. return -1;
  86. }
  87. // After the reset, the size of the wav file is mck.cksize so store it now
  88. mdwSize = mck.cksize / sizeof( short );
  89. mMemSize = mck.cksize;
  90. if ( mck.cksize != 0xffffffff ) {
  91. if ( pwfx ) {
  92. memcpy( pwfx, (waveformatex_t *)&mpwfx, sizeof(waveformatex_t));
  93. }
  94. return 0;
  95. }
  96. return -1;
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Name: idWaveFile::OpenFromMemory()
  100. // Desc: copy data to idWaveFile member variable from memory
  101. //-----------------------------------------------------------------------------
  102. int idWaveFile::OpenFromMemory( short* pbData, int ulDataSize, waveformatextensible_t* pwfx ) {
  103. mpwfx = *pwfx;
  104. mulDataSize = ulDataSize;
  105. mpbData = pbData;
  106. mpbDataCur = mpbData;
  107. mdwSize = ulDataSize / sizeof( short );
  108. mMemSize = ulDataSize;
  109. mbIsReadingFromMemory = true;
  110. return 0;
  111. }
  112. //-----------------------------------------------------------------------------
  113. // Name: idWaveFile::ReadMMIO()
  114. // Desc: Support function for reading from a multimedia I/O stream.
  115. // mhmmio must be valid before calling. This function uses it to
  116. // update mckRiff, and mpwfx.
  117. //-----------------------------------------------------------------------------
  118. int idWaveFile::ReadMMIO( void ) {
  119. mminfo_t ckIn; // chunk info. for general use.
  120. pcmwaveformat_t pcmWaveFormat; // Temp PCM structure to load in.
  121. memset( &mpwfx, 0, sizeof( waveformatextensible_t ) );
  122. mhmmio->Read( &mckRiff, 12 );
  123. assert( !isOgg );
  124. mckRiff.ckid = LittleLong( mckRiff.ckid );
  125. mckRiff.cksize = LittleLong( mckRiff.cksize );
  126. mckRiff.fccType = LittleLong( mckRiff.fccType );
  127. mckRiff.dwDataOffset = 12;
  128. // Check to make sure this is a valid wave file
  129. if( (mckRiff.ckid != fourcc_riff) || (mckRiff.fccType != mmioFOURCC('W', 'A', 'V', 'E') ) ) {
  130. return -1;
  131. }
  132. // Search the input file for for the 'fmt ' chunk.
  133. ckIn.dwDataOffset = 12;
  134. do {
  135. if (8 != mhmmio->Read( &ckIn, 8 ) ) {
  136. return -1;
  137. }
  138. assert( !isOgg );
  139. ckIn.ckid = LittleLong( ckIn.ckid );
  140. ckIn.cksize = LittleLong( ckIn.cksize );
  141. ckIn.dwDataOffset += ckIn.cksize-8;
  142. } while (ckIn.ckid != mmioFOURCC('f', 'm', 't', ' '));
  143. // Expect the 'fmt' chunk to be at least as large as <PCMWAVEFORMAT>;
  144. // if there are extra parameters at the end, we'll ignore them
  145. if( ckIn.cksize < sizeof(pcmwaveformat_t) ) {
  146. return -1;
  147. }
  148. // Read the 'fmt ' chunk into <pcmWaveFormat>.
  149. if( mhmmio->Read( &pcmWaveFormat, sizeof(pcmWaveFormat) ) != sizeof(pcmWaveFormat) ) {
  150. return -1;
  151. }
  152. assert( !isOgg );
  153. pcmWaveFormat.wf.wFormatTag = LittleShort( pcmWaveFormat.wf.wFormatTag );
  154. pcmWaveFormat.wf.nChannels = LittleShort( pcmWaveFormat.wf.nChannels );
  155. pcmWaveFormat.wf.nSamplesPerSec = LittleLong( pcmWaveFormat.wf.nSamplesPerSec );
  156. pcmWaveFormat.wf.nAvgBytesPerSec = LittleLong( pcmWaveFormat.wf.nAvgBytesPerSec );
  157. pcmWaveFormat.wf.nBlockAlign = LittleShort( pcmWaveFormat.wf.nBlockAlign );
  158. pcmWaveFormat.wBitsPerSample = LittleShort( pcmWaveFormat.wBitsPerSample );
  159. // Copy the bytes from the pcm structure to the waveformatex_t structure
  160. memcpy( &mpwfx, &pcmWaveFormat, sizeof(pcmWaveFormat) );
  161. // Allocate the waveformatex_t, but if its not pcm format, read the next
  162. // word, and thats how many extra bytes to allocate.
  163. if( pcmWaveFormat.wf.wFormatTag == WAVE_FORMAT_TAG_PCM ) {
  164. mpwfx.Format.cbSize = 0;
  165. } else {
  166. return -1; // we don't handle these (32 bit wavefiles, etc)
  167. #if 0
  168. // Read in length of extra bytes.
  169. word cbExtraBytes = 0L;
  170. if( mhmmio->Read( (char*)&cbExtraBytes, sizeof(word) ) != sizeof(word) )
  171. return -1;
  172. mpwfx.Format.cbSize = cbExtraBytes;
  173. // Now, read those extra bytes into the structure, if cbExtraAlloc != 0.
  174. if( mhmmio->Read( (char*)(((byte*)&(mpwfx.Format.cbSize))+sizeof(word)), cbExtraBytes ) != cbExtraBytes ) {
  175. memset( &mpwfx, 0, sizeof( waveformatextensible_t ) );
  176. return -1;
  177. }
  178. #endif
  179. }
  180. return 0;
  181. }
  182. //-----------------------------------------------------------------------------
  183. // Name: idWaveFile::ResetFile()
  184. // Desc: Resets the internal mck pointer so reading starts from the
  185. // beginning of the file again
  186. //-----------------------------------------------------------------------------
  187. int idWaveFile::ResetFile( void ) {
  188. if( mbIsReadingFromMemory ) {
  189. mpbDataCur = mpbData;
  190. } else {
  191. if( mhmmio == NULL ) {
  192. return -1;
  193. }
  194. // Seek to the data
  195. if( -1 == mhmmio->Seek( mckRiff.dwDataOffset + sizeof(fourcc), FS_SEEK_SET ) ) {
  196. return -1;
  197. }
  198. // Search the input file for for the 'fmt ' chunk.
  199. mck.ckid = 0;
  200. do {
  201. byte ioin;
  202. if ( !mhmmio->Read( &ioin, 1 ) ) {
  203. return -1;
  204. }
  205. mck.ckid = (mck.ckid>>8) | (ioin<<24);
  206. } while (mck.ckid != mmioFOURCC('d', 'a', 't', 'a'));
  207. mhmmio->Read( &mck.cksize, 4 );
  208. assert( !isOgg );
  209. mck.cksize = LittleLong( mck.cksize );
  210. mseekBase = mhmmio->Tell();
  211. }
  212. return 0;
  213. }
  214. //-----------------------------------------------------------------------------
  215. // Name: idWaveFile::Read()
  216. // Desc: Reads section of data from a wave file into pBuffer and returns
  217. // how much read in pdwSizeRead, reading not more than dwSizeToRead.
  218. // This uses mck to determine where to start reading from. So
  219. // subsequent calls will be continue where the last left off unless
  220. // Reset() is called.
  221. //-----------------------------------------------------------------------------
  222. int idWaveFile::Read( byte* pBuffer, int dwSizeToRead, int *pdwSizeRead ) {
  223. if ( ogg != NULL ) {
  224. return ReadOGG( pBuffer, dwSizeToRead, pdwSizeRead );
  225. } else if ( mbIsReadingFromMemory ) {
  226. if( mpbDataCur == NULL ) {
  227. return -1;
  228. }
  229. if( (byte*)(mpbDataCur + dwSizeToRead) > (byte*)(mpbData + mulDataSize) ) {
  230. dwSizeToRead = mulDataSize - (int)(mpbDataCur - mpbData);
  231. }
  232. SIMDProcessor->Memcpy( pBuffer, mpbDataCur, dwSizeToRead );
  233. mpbDataCur += dwSizeToRead;
  234. if ( pdwSizeRead != NULL ) {
  235. *pdwSizeRead = dwSizeToRead;
  236. }
  237. return dwSizeToRead;
  238. } else {
  239. if( mhmmio == NULL ) {
  240. return -1;
  241. }
  242. if( pBuffer == NULL ) {
  243. return -1;
  244. }
  245. dwSizeToRead = mhmmio->Read( pBuffer, dwSizeToRead );
  246. // this is hit by ogg code, which does it's own byte swapping internally
  247. if ( !isOgg ) {
  248. LittleRevBytes( pBuffer, 2, dwSizeToRead / 2 );
  249. }
  250. if ( pdwSizeRead != NULL ) {
  251. *pdwSizeRead = dwSizeToRead;
  252. }
  253. return dwSizeToRead;
  254. }
  255. }
  256. //-----------------------------------------------------------------------------
  257. // Name: idWaveFile::Close()
  258. // Desc: Closes the wave file
  259. //-----------------------------------------------------------------------------
  260. int idWaveFile::Close( void ) {
  261. if ( ogg != NULL ) {
  262. return CloseOGG();
  263. }
  264. if( mhmmio != NULL ) {
  265. fileSystem->CloseFile( mhmmio );
  266. mhmmio = NULL;
  267. }
  268. return 0;
  269. }
  270. //-----------------------------------------------------------------------------
  271. // Name: idWaveFile::Seek()
  272. //-----------------------------------------------------------------------------
  273. int idWaveFile::Seek( int offset ) {
  274. if ( ogg != NULL ) {
  275. common->FatalError( "idWaveFile::Seek: cannot seek on an OGG file\n" );
  276. } else if( mbIsReadingFromMemory ) {
  277. mpbDataCur = mpbData + offset;
  278. } else {
  279. if( mhmmio == NULL ) {
  280. return -1;
  281. }
  282. if ((int)(offset+mseekBase) == mhmmio->Tell()) {
  283. return 0;
  284. }
  285. mhmmio->Seek( offset + mseekBase, FS_SEEK_SET );
  286. return 0;
  287. }
  288. return -1;
  289. }