Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problems with double / multiple buffering for low level wave input

Posted on 1997-09-22
6
Medium Priority
?
1,152 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 100 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

876 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