Direct Sound Help (Creating a sound buffer)

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 22
Reputation: Mr Violent is an unknown quantity at this point 
Solved Threads: 0
Mr Violent Mr Violent is offline Offline
Newbie Poster

Direct Sound Help (Creating a sound buffer)

 
0
  #1
Jul 19th, 2006
Okay, I'm learning how to load a WAV file into DirectSound in order to play it. I've successfully surfed through the headers of the WAV file, and loaded it. However, when I go to create a buffer with this WAV sound data I've run into a road block. I'm getting a DSERR_INVALIDPARAM error when I try to create this sound buffer from a WAV format variable. I have successfully created a blank primary buffer with the wfxformat tag being NULL and the size being 0. Here's my code for my WAV buffer:

Here's my class that I use to access my m_lpDSBuffer
  1. class DSound {
  2. private:
  3. DWORD m_dwDSBufferSize;
  4. DWave* m_lpWaveFile;
  5. DWORD m_dwNumBuffers;
  6. DWORD m_dwCreationFlags;
  7. public:
  8. LPDIRECTSOUNDBUFFER m_lpDSBuffer;
  9. bool inUse;
  10. bool Open(LPTSTR strFileName);
  11. };

  1. bool DWave::Open(LPTSTR strFileName, WAVEFORMATEX* wavFmt, DWORD dwFlags) {
  2.  
  3. HMMIO hWav; // WAVE's handle
  4. WAVEFORMATEX wfmex; // WAVE's format
  5. MMCKINFO child, parent;
  6. UCHAR *p_wavData; // file loads here
  7. UCHAR *p_wavBuffer1; // pointer to first buffer
  8. UCHAR *p_wavBuffer2; // second buffer
  9. DWORD dwLength1; // length of first write buffer
  10. DWORD dwLength2; // length of second write buffer
  11. DSound wavFile; // temporary buffer
  12. DSound *p_wavFile;
  13. int iCurrSize, i;
  14. DSBUFFERDESC dsbd; // local direct sound buffer description
  15.  
  16. p_wavFile = &wavFile;
  17.  
  18. hWav = mmioOpen(strFileName, NULL, MMIO_READ | MMIO_ALLOCBUF);
  19.  
  20. if (hWav==NULL) {
  21. ErrorBox("Could not find / load file at mmioOpen()");
  22. return false;
  23. }
  24.  
  25. parent.fccType = mmioFOURCC('W', 'A', 'V', 'E');
  26.  
  27. // descend to riff
  28. if (mmioDescend(hWav, &parent, NULL, MMIO_FINDRIFF)) {
  29. ErrorBox("Could not descend to RIFF, file is corrupt or not a WAVE file.");
  30. mmioClose(hWav, 0);
  31. return false;
  32. }
  33.  
  34. // descend to WAVEfmt
  35. child.ckid = mmioFOURCC('f', 'm', 't', ' ');
  36. if (mmioDescend(hWav, &child, &parent, 0)) {
  37. ErrorBox("Could not descend to WAVEfmt, file is corrupt or not a WAVE file.");
  38. mmioClose(hWav, 0);
  39. return false;
  40. }
  41.  
  42. // open WAV file for reading, check to make sure format is correct
  43. if (mmioRead(hWav, (char*)&wfmex, sizeof(WAVEFORMATEX)) != sizeof(WAVEFORMATEX)) {
  44. ErrorBox("Error reading WAV format, file is corrupt or not a WAVE file.");
  45. mmioClose(hWav, 0);
  46. return false;
  47. }
  48.  
  49. if (wfmex.wFormatTag != WAVE_FORMAT_PCM) {
  50. ErrorBox("WAVE file is not of PCM format.");
  51. mmioClose(hWav, 0);
  52. return false;
  53. }
  54.  
  55. if (mmioAscend(hWav, &child, 0)) {
  56. ErrorBox("Error ascending to WAVE data.");
  57. mmioClose(hWav, 0);
  58. return false;
  59. }
  60.  
  61. // descend to data
  62. child.ckid = mmioFOURCC('d', 'a', 't', 'a');
  63. if (mmioDescend(hWav, &child, &parent, MMIO_FINDCHUNK)) {
  64. ErrorBox("Error descending to the WAVE data level.");
  65. mmioClose(hWav, 0);
  66. return false;
  67. }
  68.  
  69. // verification complete, now read information
  70.  
  71. // allocate memory and load pointer
  72. if (!(p_wavData = new UCHAR [child.cksize])) {
  73. ErrorBox("Insufficient memory to load WAVE file.");
  74. mmioClose(hWav, 0);
  75. return false;
  76. }
  77.  
  78. // read WAVE data
  79.  
  80. long lBytesRead;
  81. lBytesRead = mmioRead(hWav, (char*)p_wavData, child.cksize);
  82. if (lBytesRead<0) {
  83. ErrorBox("Unable to read WAVE file data.");
  84. mmioClose(hWav, 0);
  85. return false;
  86. }
  87.  
  88. mmioClose(hWav, 0);
  89.  
  90. // place our information on our buffer, prepare our description for direct sound
  91. dsbd.dwSize = sizeof(DSBUFFERDESC);
  92. dsbd.dwBufferBytes = child.cksize; // size of buffer is equal to the wave's size
  93. dsbd.dwFlags = DSBCAPS_STATIC |
  94. DSBCAPS_LOCSOFTWARE | // Force loading in software so we can't loose it.
  95. DSBCAPS_GLOBALFOCUS | // Don't mute on 'lostfocus'
  96. DSBCAPS_CTRLPAN |
  97. DSBCAPS_CTRLVOLUME |
  98. DSBCAPS_CTRLFREQUENCY;
  99. dsbd.lpwfxFormat = &wfmex;
  100.  
  101. // create our sound buffer
  102.  
  103. HRESULT hr = dsSound.m_lpDS->CreateSoundBuffer(&dsbd, &p_wavFile->m_lpDSBuffer, NULL);
  104. if (hr==DSERR_INVALIDPARAM) {
  105. delete [] p_wavData;
  106. p_wavFile = NULL;
  107. ErrorBox("Error creating sound buffer.");
  108. return false;
  109. }
  110.  
  111. return true;
  112. }

I keep getting a DSERR_INVALIDPARAM error, help!
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 22
Reputation: Mr Violent is an unknown quantity at this point 
Solved Threads: 0
Mr Violent Mr Violent is offline Offline
Newbie Poster

Re: Direct Sound Help (Creating a sound buffer)

 
0
  #2
Jul 27th, 2006
anyone?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 6708 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC