home *** CD-ROM | disk | FTP | other *** search
/ WarCraft 2000 - Nuclear Epidemic / W2000.nrg / SOURCE.War2000 / Cwave.cpp < prev    next >
C/C++ Source or Header  |  1998-08-17  |  4KB  |  142 lines

  1. ///////////////////////////////////////////////////////////
  2. // CWAVE.CPP: Implementation file for the WAVE class.
  3. ///////////////////////////////////////////////////////////
  4.  
  5. //#include <stdafx.h>
  6. #include <afx.h>
  7. #include "windowsx.h"
  8. #include "cwave.h"
  9.  
  10. ///////////////////////////////////////////////////////////
  11. // CWave::CWave()
  12. ///////////////////////////////////////////////////////////
  13. CWave::CWave(char* fileName)
  14. {
  15.     // Initialize the class's members.
  16.     m_waveSize = 0;
  17.     m_waveOK = FALSE;
  18.     m_pWave= NULL;
  19.  
  20.     // Load the wave file.
  21.     m_waveOK = LoadWaveFile(fileName);
  22. }
  23.  
  24. ///////////////////////////////////////////////////////////
  25. // CWave::~CWave()
  26. ///////////////////////////////////////////////////////////
  27. CWave::~CWave()
  28. {
  29.     // Free the memory assigned to the wave data.
  30.     GlobalFreePtr(m_pWave);
  31. }
  32.  
  33. ///////////////////////////////////////////////////////////
  34. // CWave::LoadWaveFile()
  35. //
  36. // This function loads a wave from disk into memory. It
  37. // also initializes various class data members.
  38. ///////////////////////////////////////////////////////////
  39. BOOL CWave::LoadWaveFile(char* fileName)
  40. {
  41.     MMCKINFO mmCkInfoRIFF;
  42.     MMCKINFO mmCkInfoChunk;
  43.     MMRESULT result;
  44.     HMMIO hMMIO;
  45.     long bytesRead;
  46.  
  47.     // Open the wave file.
  48.     hMMIO = mmioOpen(fileName, NULL, MMIO_READ | MMIO_ALLOCBUF);
  49.     if (hMMIO == NULL)
  50.         return FALSE;
  51.  
  52.     // Descend into the RIFF chunk.
  53.     mmCkInfoRIFF.fccType = mmioFOURCC('W', 'A', 'V', 'E');
  54.     result = mmioDescend(hMMIO, &mmCkInfoRIFF, NULL, MMIO_FINDRIFF);
  55.     if (result != MMSYSERR_NOERROR)
  56.         return FALSE;
  57.  
  58.     // Descend into the format chunk.
  59.     mmCkInfoChunk.ckid = mmioFOURCC('f', 'm', 't', ' ');
  60.     result = mmioDescend(hMMIO, &mmCkInfoChunk,
  61.         &mmCkInfoRIFF, MMIO_FINDCHUNK);
  62.     if (result != MMSYSERR_NOERROR)
  63.         return FALSE;
  64.  
  65.     // Read the format information into the WAVEFORMATEX structure.
  66.     bytesRead = mmioRead(hMMIO, (char*)&m_waveFormatEx,
  67.         sizeof(WAVEFORMATEX));
  68.     if (bytesRead == -1)
  69.         return FALSE;
  70.  
  71.     // Ascend out of the format chunk.
  72.     result = mmioAscend(hMMIO, &mmCkInfoChunk, 0);
  73.     if (result != MMSYSERR_NOERROR)
  74.         return FALSE;
  75.  
  76.     // Descend into the data chunk.
  77.     mmCkInfoChunk.ckid = mmioFOURCC('d', 'a', 't', 'a');
  78.     result = mmioDescend(hMMIO, &mmCkInfoChunk,
  79.         &mmCkInfoRIFF, MMIO_FINDCHUNK);
  80.     if (result != MMSYSERR_NOERROR)
  81.         return FALSE;
  82.  
  83.     // Save the size of the wave data.
  84.     m_waveSize = mmCkInfoChunk.cksize;
  85.  
  86.     // Allocate a buffer for the wave data.
  87.     m_pWave = (char*)GlobalAllocPtr(GMEM_MOVEABLE, m_waveSize);
  88.     if (m_pWave == NULL)
  89.         return FALSE;
  90.  
  91.     // Read the wave data into the buffer.
  92.     bytesRead = mmioRead(hMMIO, (char*)m_pWave, m_waveSize);
  93.     if (bytesRead == -1)
  94.         return FALSE;
  95.     mmioClose(hMMIO, 0);
  96.  
  97.     return TRUE;
  98. }
  99.  
  100. ///////////////////////////////////////////////////////////
  101. // CWave::GetWaveSize()
  102. //
  103. // This returns the size in bytes of the wave data.
  104. ///////////////////////////////////////////////////////////
  105. DWORD CWave::GetWaveSize()
  106. {
  107.     return m_waveSize;
  108. }
  109.  
  110. ///////////////////////////////////////////////////////////
  111. // CWave::GetWaveFormatPtr()
  112. //
  113. // This function returns a pointer to the wave file's
  114. // WAVEFORMATEX structure.
  115. ///////////////////////////////////////////////////////////
  116. LPWAVEFORMATEX CWave::GetWaveFormatPtr()
  117. {
  118.     return &m_waveFormatEx;
  119. }
  120.  
  121. ///////////////////////////////////////////////////////////
  122. // CWave::GetWaveDataPtr()
  123. //
  124. // This function returns a pointer to the wave's
  125. // actual sound data.
  126. ///////////////////////////////////////////////////////////
  127. char* CWave::GetWaveDataPtr()
  128. {
  129.     return m_pWave;
  130. }
  131.  
  132. ///////////////////////////////////////////////////////////
  133. // CWave::WaveOK()
  134. //
  135. // This function returns a Boolean value indicating whether
  136. // the wave file object was set up successfully.
  137. ///////////////////////////////////////////////////////////
  138. BOOL CWave::WaveOK()
  139. {
  140.     return m_waveOK;
  141. }
  142.