SoundBuffer.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. ** Command & Conquer Renegade(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:: 11/01/01 11:00a $*
  29. * *
  30. * $Revision:: 12 $*
  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. #include "wwprofile.h"
  42. /////////////////////////////////////////////////////////////////////////////////
  43. // FileMappingClass
  44. /////////////////////////////////////////////////////////////////////////////////
  45. class FileMappingClass
  46. {
  47. public:
  48. StringClass Filename;
  49. HANDLE FileMapping;
  50. int RefCount;
  51. bool operator== (const FileMappingClass &src) { return false; }
  52. bool operator!= (const FileMappingClass &src) { return false; }
  53. };
  54. static DynamicVectorClass<FileMappingClass> MappingList;
  55. /////////////////////////////////////////////////////////////////////////////////
  56. //
  57. // SoundBufferClass
  58. //
  59. SoundBufferClass::SoundBufferClass (void)
  60. : m_Buffer (NULL),
  61. m_Length (0),
  62. m_Filename (NULL),
  63. m_Duration (0),
  64. m_Rate (0),
  65. m_Bits (0),
  66. m_Channels (0),
  67. m_Type (WAVE_FORMAT_IMA_ADPCM)
  68. {
  69. return ;
  70. }
  71. /////////////////////////////////////////////////////////////////////////////////
  72. //
  73. // ~SoundBufferClass
  74. //
  75. SoundBufferClass::~SoundBufferClass (void)
  76. {
  77. SAFE_FREE (m_Filename);
  78. Free_Buffer ();
  79. return ;
  80. }
  81. /////////////////////////////////////////////////////////////////////////////////
  82. //
  83. // Free_Buffer
  84. //
  85. void
  86. SoundBufferClass::Free_Buffer (void)
  87. {
  88. // Free the buffer's memory
  89. if (m_Buffer != NULL) {
  90. delete [] m_Buffer;
  91. m_Buffer = NULL;
  92. }
  93. // Make sure we reset the length
  94. m_Length = 0L;
  95. return ;
  96. }
  97. /////////////////////////////////////////////////////////////////////////////////
  98. //
  99. // Determine_Stats
  100. //
  101. void
  102. SoundBufferClass::Determine_Stats (unsigned char *buffer)
  103. {
  104. WWPROFILE ("Determine_Stats");
  105. MMSLockClass lock;
  106. m_Duration = 0;
  107. m_Rate = 0;
  108. m_Channels = 0;
  109. m_Bits = 0;
  110. m_Type = WAVE_FORMAT_IMA_ADPCM;
  111. // Attempt to get statistical information about this sound
  112. AILSOUNDINFO info = { 0 };
  113. if ((buffer != NULL) && (::AIL_WAV_info (buffer, &info) != 0)) {
  114. // Cache this information
  115. m_Rate = info.rate;
  116. m_Channels = info.channels;
  117. m_Bits = info.bits;
  118. m_Type = info.format;
  119. // Determine how long this sound will play for
  120. float bytes_sec = float((m_Channels * m_Rate * m_Bits) >> 3);
  121. m_Duration = (unsigned long)((((float)m_Length) / bytes_sec) * 1000.0F);
  122. }
  123. return ;
  124. }
  125. /////////////////////////////////////////////////////////////////////////////////
  126. //
  127. // Set_Filename
  128. //
  129. void
  130. SoundBufferClass::Set_Filename (const char *name)
  131. {
  132. SAFE_FREE (m_Filename);
  133. if (name != NULL) {
  134. m_Filename = ::strdup (name);
  135. }
  136. return ;
  137. }
  138. /////////////////////////////////////////////////////////////////////////////////
  139. //
  140. // Load_From_File
  141. //
  142. bool
  143. SoundBufferClass::Load_From_File (const char *filename)
  144. {
  145. WWPROFILE ("SoundBufferClass::Load_From_File");
  146. WWDEBUG_SAY(( "Loading sound file %s.\r\n", filename));
  147. // Assume failure
  148. bool retval = false;
  149. // Param OK?
  150. WWASSERT (filename != NULL);
  151. if (filename != NULL) {
  152. // Create a file object and pass it onto the appropriate function
  153. FileClass *file=_TheFileFactory->Get_File(filename);
  154. if ( file ) {
  155. retval = Load_From_File(*file);
  156. _TheFileFactory->Return_File(file);
  157. }
  158. file=NULL;
  159. }
  160. // Return the true/false result code
  161. return retval;
  162. }
  163. /////////////////////////////////////////////////////////////////////////////////
  164. //
  165. // Load_From_File
  166. //
  167. bool
  168. SoundBufferClass::Load_From_File (FileClass &file)
  169. {
  170. WWPROFILE ("SoundBufferClass::Load_From_File");
  171. MMSLockClass lock;
  172. // Assume failure
  173. bool retval = false;
  174. // Start from scratch
  175. Free_Buffer ();
  176. Set_Filename (file.File_Name ());
  177. // Open the file if necessary
  178. bool we_opened = false;
  179. if (file.Is_Open () == false) {
  180. we_opened = (file.Open () == TRUE);
  181. }
  182. // Determine the size of the buffer
  183. m_Length = file.Size ();
  184. WWASSERT (m_Length > 0L);
  185. if (m_Length > 0L) {
  186. // Allocate a new buffer of the correct length and read the contents
  187. // of the file into the buffer
  188. m_Buffer = new unsigned char[m_Length];
  189. retval = bool(file.Read (m_Buffer, m_Length) == (int)m_Length);
  190. // If we failed, free the buffer
  191. if (retval == false) {
  192. Free_Buffer ();
  193. }
  194. Determine_Stats (m_Buffer);
  195. }
  196. // Close the file if necessary
  197. if (we_opened) {
  198. file.Close ();
  199. }
  200. // Return the true/false result code
  201. return retval;
  202. }
  203. /////////////////////////////////////////////////////////////////////////////////
  204. //
  205. // Load_From_Memory
  206. //
  207. bool
  208. SoundBufferClass::Load_From_Memory
  209. (
  210. unsigned char *mem_buffer,
  211. unsigned long size
  212. )
  213. {
  214. MMSLockClass lock;
  215. // Assume failure
  216. bool retval = false;
  217. // Start from scratch
  218. Free_Buffer ();
  219. Set_Filename ("unknown.wav");
  220. // Params OK?
  221. WWASSERT (mem_buffer != NULL);
  222. WWASSERT (size > 0L);
  223. if ((mem_buffer != NULL) && (size > 0L)) {
  224. // Allocate a new buffer of the correct length and copy the contents
  225. // into the buffer
  226. m_Length = size;
  227. m_Buffer = new unsigned char[m_Length];
  228. ::memcpy (m_Buffer, mem_buffer, size);
  229. retval = true;
  230. // If we failed, free the buffer
  231. if (retval == false) {
  232. Free_Buffer ();
  233. }
  234. Determine_Stats (m_Buffer);
  235. }
  236. // Return the true/false result code
  237. return retval;
  238. }
  239. /////////////////////////////////////////////////////////////////////////////////
  240. //
  241. // StreamSoundBufferClass
  242. //
  243. StreamSoundBufferClass::StreamSoundBufferClass (void) :
  244. SoundBufferClass ()
  245. {
  246. return ;
  247. }
  248. /////////////////////////////////////////////////////////////////////////////////
  249. //
  250. // ~StreamSoundBufferClass
  251. //
  252. StreamSoundBufferClass::~StreamSoundBufferClass (void)
  253. {
  254. return ;
  255. }
  256. /////////////////////////////////////////////////////////////////////////////////
  257. //
  258. // Free_Buffer
  259. //
  260. void
  261. StreamSoundBufferClass::Free_Buffer (void)
  262. {
  263. return ;
  264. }
  265. /////////////////////////////////////////////////////////////////////////////////
  266. //
  267. // Load_From_File
  268. //
  269. /////////////////////////////////////////////////////////////////////////////////
  270. bool
  271. StreamSoundBufferClass::Load_From_File
  272. (
  273. HANDLE /*hfile*/,
  274. unsigned long /*size*/,
  275. unsigned long /*offset*/
  276. )
  277. {
  278. WWPROFILE ("StreamSoundBufferClass::Load_From_File");
  279. return true;
  280. }
  281. /////////////////////////////////////////////////////////////////////////////////
  282. //
  283. // Load_From_File
  284. //
  285. /////////////////////////////////////////////////////////////////////////////////
  286. bool
  287. StreamSoundBufferClass::Load_From_File (const char *filename)
  288. {
  289. WWPROFILE ("StreamSoundBufferClass::Load_From_File");
  290. return true;
  291. }
  292. /////////////////////////////////////////////////////////////////////////////////
  293. //
  294. // Load_From_File
  295. //
  296. /////////////////////////////////////////////////////////////////////////////////
  297. bool
  298. StreamSoundBufferClass::Load_From_File (FileClass &file)
  299. {
  300. WWPROFILE ("StreamSoundBufferClass::Load_From_File");
  301. MMSLockClass lock;
  302. // Start from scratch
  303. Free_Buffer ();
  304. Set_Filename (file.File_Name ());
  305. // Open the file if necessary
  306. bool we_opened = false;
  307. if (file.Is_Open () == false) {
  308. we_opened = (file.Open () == TRUE);
  309. }
  310. m_Length = file.Size ();
  311. // Allocate a new buffer of the correct length and read the contents
  312. // of the file into the buffer
  313. unsigned char buffer[4096] = { 0 };
  314. file.Read (buffer, sizeof (buffer));
  315. Determine_Stats (buffer);
  316. // Close the file if necessary
  317. if (we_opened) {
  318. file.Close ();
  319. }
  320. return true;
  321. }