Solved

Problems with double / multiple buffering for low level wave input

Posted on 1997-09-22
6
1,076 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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