Solved

Problems with double / multiple buffering for low level wave input

Posted on 1997-09-22
6
1,071 Views
Last Modified: 2013-12-04
Hi,
I am trying to get realtime throughput of the wave input to the wave output in full duplex.  I am using the code suggested by Creative Lab examples.  It works , except that in my MM_WIM_DATA  when I have passed on the buffer to the output, and unprepared the input header, preparing the just received buffer to recycle it for adding into the input queue seems to give problems. Error no. 7 memory alloc error.
here is the code for my mem allocation, start of input, and the function for processing MM_WIM_DATA.

      for (int i = 0; i < MAX_BUFFERS; i++)
      {
      
            hWaveInHdr[i] = GlobalAlloc(GHND | GMEM_SHARE, sizeof(WAVEHDR));
            lpWaveInHdr[i] = (LPWAVEHDR) GlobalLock(hWaveInHdr[i]);
            hWaveInBuffer[i] = GlobalAlloc(GHND | GMEM_SHARE, BUFFER_SIZE);
            lpWaveInBuffer[i] = (LPSTR) GlobalLock(hWaveInBuffer[i]);
            lpWaveInHdr[i]->lpData = lpWaveInBuffer[i];
            lpWaveInHdr[i]->dwBufferLength = BUFFER_SIZE;

            hWaveOutBuffer[i] = GlobalAlloc(GHND | GMEM_SHARE, BUFFER_SIZE);
            lpWaveOutBuffer[i] = (LPSTR) GlobalLock(hWaveOutBuffer[i]);
            hWaveOutHdr[i] = GlobalAlloc(GHND | GMEM_SHARE, sizeof(WAVEHDR));
            lpWaveOutHdr[i] = (LPWAVEHDR) GlobalLock(hWaveOutHdr[i]);
            lpWaveOutHdr[i]->lpData = lpWaveInBuffer[i];
            lpWaveOutHdr[i]->dwBufferLength = BUFFER_SIZE;

      }



///////////////////////////////////////////////////////////
      // Prepare and send input buffers to input device
      /////////////////////////
      nBufferIn = 0;
      nBufferOut = 0;
      for(int i=0; i<MAX_BUFFERS; i++)
      {
            // Prepare wave in header
            if (waveInPrepareHeader(hWaveIn, lpWaveInHdr[i], sizeof(WAVEHDR)))
            {
                  AfxMessageBox("OnPlay-error preparing header");
            }
            // Add buffer to recording queue
            if (waveInAddBuffer(hWaveIn, lpWaveInHdr[i], sizeof(WAVEHDR)))
            {
                  AfxMessageBox("OnPlay-error adding buffer");
            }
            else nBufferIn++;
      }


      // Start the wave input - begin sampling
      ///////////////////////
      waveInStart(hWaveIn);
      CDC* pDC = GetDC();
      pDC->TextOut(100,100,"Wave Started");
      ReleaseDC(pDC);


///////////////////////////////////////////////////////////
LONG CCreativeAudioView::OnMMWIMDATA (UINT wParam, LONG lParam)
{
//  Procedure to handle message from device driver saying it has filled a buffer
//  code from creative labs sample code

 nBufferIn--;
LPWAVEHDR lpWaveHdrTmp;
 lpWaveHdrTemp = (LPWAVEHDR) lParam;
lpWaveHdrTempOut = (LPWAVEHDR) lParam;
waveInUnprepareHeader(hWaveIn, lpWaveHdrTemp, sizeof(WAVEHDR));

// Send received buffer to output queue
      ///////////////////////////////////////////////////////////////
    waveOutPrepareHeader(hWaveOut, ((LPWAVEHDR) lParam),sizeof(WAVEHDR));
    waveOutWrite(hWaveOut, ((LPWAVEHDR) lParam), sizeof(WAVEHDR));
      
      //add another buffer to input queue
//////   THIS IS WHERE THE PROBLEMS COME IN, IT WORKS FOR 16 BUFFERS AND THEN SOUND STOPS AND I GET ERROR 7 EACH TIME I TRY TO PREPARE THE HEADER///////////      ////////////////////////////////////////////////////////
nError = waveInPrepareHeader( hWaveIn, lpWaveHdrTmp, sizeof(WAVEHDR));
if (nError != 0)
{
      strMessageText.Format("Error preparing next input header %d", nError);
      AfxMessageBox(strMessageText);
};
waveInAddBuffer(hWaveIn, lpWaveHdrTemp, sizeof(WAVEHDR));
nBufferIn++;
      return 0L;
}


i'D BE GRATEFUL FOR ANY HELP
Thanks

Kingpie
0
Comment
Question by:kingpie
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:y96andha
ID: 1406974
Does the waveinunprepareheader always succeed? Do you unprepare the waveout buffers?
0
 
LVL 2

Accepted Solution

by:
alexxx earned 50 total points
ID: 1406975
Since you use the same buffers for both recording (wave in) and playback (wave out) you should synchronize their sending to these devices. Don't add buffers to wave in in its callback. Instead register callback function for wave out device and add buffer returned from wave out back to wave in. The data won't be lost because there are several buffers.


->Wave In-->(Wave In callback)-->Wave Out-->(Wave Out callback)-
^---<

0
 

Author Comment

by:kingpie
ID: 1406976
It seems that waveInUnprepareHeader does always work.  The wave out buffers are unprepared in OnMM_WOM_DONE:

LONG CCreativeAudioView::OnMMWOMDONE (UINT wParam, LONG lParam)
{      
      lpWaveHdrTempOut = (LPWAVEHDR) lParam;
      waveOutUnprepareHeader(hWaveOut, lpWaveHdrTempOut,         sizeof(WAVEHDR));
      return 0L;
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:kingpie
ID: 1406977
It seems that waveInUnprepareHeader does always work.  The wave out buffers are unprepared in OnMM_WOM_DONE:

LONG CCreativeAudioView::OnMMWOMDONE (UINT wParam, LONG lParam)
{      
      lpWaveHdrTempOut = (LPWAVEHDR) lParam;
      waveOutUnprepareHeader(hWaveOut, lpWaveHdrTempOut,         sizeof(WAVEHDR));
      return 0L;
}
0
 
LVL 2

Expert Comment

by:alexxx
ID: 1406978
So add buffers to Wave In on MM_WOM_DONE too and everything should be OK.
Good luck!
0
 

Author Comment

by:kingpie
ID: 1406979
It works!  thanks very much. (and it makes sense!)
KingPie
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now