SoundBuffer.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program 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. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWAudio *
  23. * *
  24. * $Archive:: /Commando/Code/WWAudio/SoundBuffer.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 8/17/01 11:22a $*
  29. * *
  30. * $Revision:: 11 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "soundbuffer.h"
  36. #include "rawfile.h"
  37. #include "wwdebug.h"
  38. #include "utils.h"
  39. #include "ffactory.h"
  40. #include "win.h"
  41. /////////////////////////////////////////////////////////////////////////////////
  42. // FileMappingClass
  43. /////////////////////////////////////////////////////////////////////////////////
  44. class FileMappingClass
  45. {
  46. public:
  47. StringClass Filename;
  48. HANDLE FileMapping;
  49. int RefCount;
  50. bool operator== (const FileMappingClass &src) { return false; }
  51. bool operator!= (const FileMappingClass &src) { return false; }
  52. };
  53. static DynamicVectorClass<FileMappingClass> MappingList;
  54. /////////////////////////////////////////////////////////////////////////////////
  55. //
  56. // SoundBufferClass
  57. //
  58. SoundBufferClass::SoundBufferClass (void)
  59. : m_Buffer (NULL),
  60. m_Length (0),
  61. m_Filename (NULL),
  62. m_Duration (0),
  63. m_Rate (0),
  64. m_Bits (0),
  65. m_Channels (0),
  66. m_Type (WAVE_FORMAT_IMA_ADPCM)
  67. {
  68. return ;
  69. }
  70. /////////////////////////////////////////////////////////////////////////////////
  71. //
  72. // ~SoundBufferClass
  73. //
  74. SoundBufferClass::~SoundBufferClass (void)
  75. {
  76. SAFE_FREE (m_Filename);
  77. Free_Buffer ();
  78. return ;
  79. }
  80. /////////////////////////////////////////////////////////////////////////////////
  81. //
  82. // Free_Buffer
  83. //
  84. void
  85. SoundBufferClass::Free_Buffer (void)
  86. {
  87. // Free the buffer's memory
  88. if (m_Buffer != NULL) {
  89. delete [] m_Buffer;
  90. m_Buffer = NULL;
  91. }
  92. // Make sure we reset the length
  93. m_Length = 0L;
  94. return ;
  95. }
  96. /////////////////////////////////////////////////////////////////////////////////
  97. //
  98. // Determine_Stats
  99. //
  100. void
  101. SoundBufferClass::Determine_Stats (unsigned char *buffer)
  102. {
  103. MMSLockClass lock;
  104. m_Duration = 0;
  105. m_Rate = 0;
  106. m_Channels = 0;
  107. m_Bits = 0;
  108. m_Type = WAVE_FORMAT_IMA_ADPCM;
  109. // Attempt to get statistical information about this sound
  110. AILSOUNDINFO info = { 0 };
  111. if ((buffer != NULL) && (::AIL_WAV_info (buffer, &info) != 0)) {
  112. // Cache this information
  113. m_Rate = info.rate;
  114. m_Channels = info.channels;
  115. m_Bits = info.bits;
  116. m_Type = info.format;
  117. // Determine how long this sound will play for
  118. float bytes_sec = float((m_Channels * m_Rate * m_Bits) >> 3);
  119. m_Duration = (unsigned long)((((float)m_Length) / bytes_sec) * 1000.0F);
  120. }
  121. return ;
  122. }
  123. /////////////////////////////////////////////////////////////////////////////////
  124. //
  125. // Set_Filename
  126. //
  127. void
  128. SoundBufferClass::Set_Filename (const char *name)
  129. {
  130. SAFE_FREE (m_Filename);
  131. if (name != NULL) {
  132. m_Filename = ::strdup (name);
  133. }
  134. return ;
  135. }
  136. /////////////////////////////////////////////////////////////////////////////////
  137. //
  138. // Load_From_File
  139. //
  140. bool
  141. SoundBufferClass::Load_From_File (const char *filename)
  142. {
  143. // Assume failure
  144. bool retval = false;
  145. // Param OK?
  146. WWASSERT (filename != NULL);
  147. if (filename != NULL) {
  148. // Create a file object and pass it onto the appropriate function
  149. FileClass *file=_TheFileFactory->Get_File(filename);
  150. if ( file ) {
  151. retval = Load_From_File(*file);
  152. _TheFileFactory->Return_File(file);
  153. }
  154. file=NULL;
  155. }
  156. // Return the true/false result code
  157. return retval;
  158. }
  159. /////////////////////////////////////////////////////////////////////////////////
  160. //
  161. // Load_From_File
  162. //
  163. bool
  164. SoundBufferClass::Load_From_File (FileClass &file)
  165. {
  166. MMSLockClass lock;
  167. // Assume failure
  168. bool retval = false;
  169. // Start from scratch
  170. Free_Buffer ();
  171. Set_Filename (file.File_Name ());
  172. // Open the file if necessary
  173. bool we_opened = false;
  174. if (file.Is_Open () == false) {
  175. we_opened = (file.Open () == TRUE);
  176. }
  177. // Determine the size of the buffer
  178. m_Length = file.Size ();
  179. WWASSERT (m_Length > 0L);
  180. if (m_Length > 0L) {
  181. // Allocate a new buffer of the correct length and read the contents
  182. // of the file into the buffer
  183. m_Buffer = W3DNEWARRAY unsigned char[m_Length];
  184. retval = bool(file.Read (m_Buffer, m_Length) == (int)m_Length);
  185. // If we failed, free the buffer
  186. if (retval == false) {
  187. Free_Buffer ();
  188. }
  189. Determine_Stats (m_Buffer);
  190. }
  191. // Close the file if necessary
  192. if (we_opened) {
  193. file.Close ();
  194. }
  195. // Return the true/false result code
  196. return retval;
  197. }
  198. /////////////////////////////////////////////////////////////////////////////////
  199. //
  200. // Load_From_Memory
  201. //
  202. bool
  203. SoundBufferClass::Load_From_Memory
  204. (
  205. unsigned char *mem_buffer,
  206. unsigned long size
  207. )
  208. {
  209. MMSLockClass lock;
  210. // Assume failure
  211. bool retval = false;
  212. // Start from scratch
  213. Free_Buffer ();
  214. Set_Filename ("unknown.wav");
  215. // Params OK?
  216. WWASSERT (mem_buffer != NULL);
  217. WWASSERT (size > 0L);
  218. if ((mem_buffer != NULL) && (size > 0L)) {
  219. // Allocate a new buffer of the correct length and copy the contents
  220. // into the buffer
  221. m_Length = size;
  222. m_Buffer = W3DNEWARRAY unsigned char[m_Length];
  223. ::memcpy (m_Buffer, mem_buffer, size);
  224. retval = true;
  225. // If we failed, free the buffer
  226. if (retval == false) {
  227. Free_Buffer ();
  228. }
  229. Determine_Stats (m_Buffer);
  230. }
  231. // Return the true/false result code
  232. return retval;
  233. }
  234. /////////////////////////////////////////////////////////////////////////////////
  235. //
  236. // StreamSoundBufferClass
  237. //
  238. StreamSoundBufferClass::StreamSoundBufferClass (void) :
  239. SoundBufferClass ()
  240. {
  241. return ;
  242. }
  243. /////////////////////////////////////////////////////////////////////////////////
  244. //
  245. // ~StreamSoundBufferClass
  246. //
  247. StreamSoundBufferClass::~StreamSoundBufferClass (void)
  248. {
  249. return ;
  250. }
  251. /////////////////////////////////////////////////////////////////////////////////
  252. //
  253. // Free_Buffer
  254. //
  255. void
  256. StreamSoundBufferClass::Free_Buffer (void)
  257. {
  258. return ;
  259. }
  260. /////////////////////////////////////////////////////////////////////////////////
  261. //
  262. // Load_From_File
  263. //
  264. /////////////////////////////////////////////////////////////////////////////////
  265. bool
  266. StreamSoundBufferClass::Load_From_File
  267. (
  268. HANDLE /*hfile*/,
  269. unsigned long /*size*/,
  270. unsigned long /*offset*/
  271. )
  272. {
  273. return true;
  274. }
  275. /////////////////////////////////////////////////////////////////////////////////
  276. //
  277. // Load_From_File
  278. //
  279. /////////////////////////////////////////////////////////////////////////////////
  280. bool
  281. StreamSoundBufferClass::Load_From_File (const char *filename)
  282. {
  283. return true;
  284. }
  285. /////////////////////////////////////////////////////////////////////////////////
  286. //
  287. // Load_From_File
  288. //
  289. /////////////////////////////////////////////////////////////////////////////////
  290. bool
  291. StreamSoundBufferClass::Load_From_File (FileClass &file)
  292. {
  293. MMSLockClass lock;
  294. // Start from scratch
  295. Free_Buffer ();
  296. Set_Filename (file.File_Name ());
  297. // Open the file if necessary
  298. bool we_opened = false;
  299. if (file.Is_Open () == false) {
  300. we_opened = (file.Open () == TRUE);
  301. }
  302. m_Length = file.Size ();
  303. // Allocate a new buffer of the correct length and read the contents
  304. // of the file into the buffer
  305. unsigned char buffer[4096] = { 0 };
  306. file.Read (buffer, sizeof (buffer));
  307. Determine_Stats (buffer);
  308. // Close the file if necessary
  309. if (we_opened) {
  310. file.Close ();
  311. }
  312. return true;
  313. }