wavfile.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. Copyright (C) 2004 Michael Liebscher
  3. Copyright (C) 1997-2001 Id Software, Inc.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. /*
  17. * wav.c: Wav file loader.
  18. *
  19. * Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
  20. * Date: 2004
  21. *
  22. * Acknowledgement:
  23. * This code was derived from Quake II, and was originally
  24. * written by Id Software, Inc.
  25. *
  26. */
  27. #include "../wolfiphone.h"
  28. PRIVATE W8 *iff_pdata;
  29. PRIVATE W8 *iff_end;
  30. PRIVATE W8 *iff_last_chunk;
  31. PRIVATE W8 *iff_data;
  32. PRIVATE int iff_chunk_len;
  33. PRIVATE short Wav_GetLittleShort( void )
  34. {
  35. short val = 0;
  36. val = *iff_pdata;
  37. val += (*(iff_pdata + 1) << 8);
  38. iff_pdata += 2;
  39. return val;
  40. }
  41. PRIVATE int Wav_GetLittleLong( void )
  42. {
  43. int val = 0;
  44. val = *iff_pdata;
  45. val += (*(iff_pdata + 1) << 8);
  46. val += (*(iff_pdata + 2) << 16);
  47. val += (*(iff_pdata + 3) << 24);
  48. iff_pdata += 4;
  49. return val;
  50. }
  51. PRIVATE void Wav_FindNextChunk( const char *name )
  52. {
  53. while( 1 )
  54. {
  55. iff_pdata = iff_last_chunk;
  56. if( iff_pdata >= iff_end )
  57. {
  58. // Didn't find the chunk
  59. iff_pdata = NULL;
  60. return;
  61. }
  62. iff_pdata += 4;
  63. iff_chunk_len = Wav_GetLittleLong();
  64. if( iff_chunk_len < 0 )
  65. {
  66. iff_pdata = NULL;
  67. return;
  68. }
  69. iff_pdata -= 8;
  70. iff_last_chunk = iff_pdata + 8 + ((iff_chunk_len + 1) & ~1);
  71. if( ! my_strnicmp((const char *)iff_pdata, name, 4) )
  72. {
  73. return;
  74. }
  75. }
  76. }
  77. PRIVATE void Wav_FindChunk( const char *name )
  78. {
  79. iff_last_chunk = iff_data;
  80. Wav_FindNextChunk( name );
  81. }
  82. PRIVATE void DumpChunks( void )
  83. {
  84. char str[ 5 ];
  85. str[ 4 ] = 0;
  86. iff_pdata = iff_data;
  87. do
  88. {
  89. memcpy( str, iff_pdata, 4 );
  90. iff_pdata += 4;
  91. iff_chunk_len = Wav_GetLittleLong();
  92. Com_Printf( "0x%x : %s (%d)\n", (int)(iff_pdata - 4), str, iff_chunk_len );
  93. iff_pdata += (iff_chunk_len + 1) & ~1;
  94. } while( iff_pdata < iff_end );
  95. }
  96. /*
  97. -----------------------------------------------------------------------------
  98. Function: LoadWavInfo -Load wav file.
  99. Parameters: filename -[in] Name of wav file to load.
  100. wav -[out] wav data.
  101. info -[out] wav sound info.
  102. Returns: True if file loaded, otherwise false.
  103. Notes: Caller is responsible for freeing wav data by calling Z_Free.
  104. -----------------------------------------------------------------------------
  105. */
  106. PUBLIC _boolean LoadWavInfo( const char *filename, W8 **wav, soundInfo_t *info )
  107. {
  108. filehandle_t *hFile;
  109. W8 *data;
  110. W32 wavlength;
  111. hFile = FS_OpenFile( filename, 0 );
  112. if( ! hFile )
  113. {
  114. return false;
  115. }
  116. data = (PW8)FS_GetLoadedFilePointer( hFile, SEEK_SET );
  117. wavlength = FS_GetFileSize( hFile );
  118. iff_data = data;
  119. iff_end = data + wavlength;
  120. // look for RIFF signature
  121. Wav_FindChunk( "RIFF" );
  122. if( ! (iff_pdata && ! my_strnicmp( (const char *)iff_pdata + 8, "WAVE", 4 ) ) )
  123. {
  124. Com_DPrintf( "[LoadWavInfo]: Missing RIFF/WAVE chunks (%s)\n", filename );
  125. FS_CloseFile( hFile );
  126. return false;
  127. }
  128. // Get "fmt " chunk
  129. iff_data = iff_pdata + 12;
  130. Wav_FindChunk( "fmt " );
  131. if( ! iff_pdata )
  132. {
  133. Com_DPrintf( "[LoadWavInfo]: Missing fmt chunk (%s)\n", filename );
  134. FS_CloseFile( hFile );
  135. return false;
  136. }
  137. iff_pdata += 8;
  138. if( Wav_GetLittleShort() != 1 )
  139. {
  140. Com_DPrintf( "[LoadWavInfo]: Microsoft PCM format only (%s)\n", filename );
  141. FS_CloseFile( hFile );
  142. return false;
  143. }
  144. info->channels = Wav_GetLittleShort();
  145. info->sample_rate = Wav_GetLittleLong();
  146. iff_pdata += 4;
  147. info->sample_size = Wav_GetLittleShort(); // Bytes Per Sample
  148. if (info->sample_size != 1 && info->sample_size != 2)
  149. {
  150. Com_DPrintf( "[LoadWavInfo]: only 8 and 16 bit WAV files supported (%s)\n", filename );
  151. FS_CloseFile( hFile );
  152. return false;
  153. }
  154. iff_pdata += 2;
  155. // Find data chunk
  156. Wav_FindChunk( "data" );
  157. if( ! iff_pdata )
  158. {
  159. Com_DPrintf( "[LoadWavInfo]: missing 'data' chunk (%s)\n", filename );
  160. FS_CloseFile( hFile );
  161. return false;
  162. }
  163. iff_pdata += 4;
  164. info->samples = Wav_GetLittleLong() / info->sample_size;
  165. if( info->samples <= 0 )
  166. {
  167. Com_DPrintf( "[LoadWavInfo]: file with 0 samples (%s)\n", filename );
  168. FS_CloseFile( hFile );
  169. return false;
  170. }
  171. // Load the data
  172. *wav = Z_Malloc( info->samples * info->sample_size );
  173. memcpy( *wav, data + (iff_pdata - data), info->samples * info->sample_size );
  174. FS_CloseFile( hFile );
  175. return true;
  176. }