Solved

Problems with double / multiple buffering for low level wave input

Posted on 1997-09-22
6
1,112 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

717 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