Solved

Problems with double / multiple buffering for low level wave input

Posted on 1997-09-22
6
1,066 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
Comment Utility
Does the waveinunprepareheader always succeed? Do you unprepare the waveout buffers?
0
 
LVL 2

Accepted Solution

by:
alexxx earned 50 total points
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:kingpie
Comment Utility
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
Comment Utility
So add buffers to Wave In on MM_WOM_DONE too and everything should be OK.
Good luck!
0
 

Author Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

7 Experts available now in Live!

Get 1:1 Help Now