Double buffering usage of playing PCM audio under Windows Mobile

Recently, I used libmad to do some mp3 decoding work. By the way, I also studied the double buffering usage of playing PCM audio data under Windows.

This article refers to the address: http://

The libmad call is temporarily omitted here.

The libmad decodes the 16-bit PCM data, which can be played by calling the Windows API. However, if you decode a section for a while, it sounds like a meal, not smooth, the reason is that there is no double buffering.

I studied it for a long time, finally coding, and the effect of playing it is very smooth.

The process is as follows:

1) Declare two WAVEHDR structures waveHeader1, waveHeader2, and allocate buffers buf1, buf2 to their lpData parameters respectively;

2) Declare the WAVEFORMATEX structure waveFormat and the HWAVEOUT structure hWaveOut. Call the function waveOutOpen( &hWaveOut, WAVE_MAPPER, &waveFormat, (DWORD)waveOutProc, NULL, CALLBACK_FUNCTION );
waveOutProc is a callback function, which will be mentioned later.

Also talk about the parameters of waveFormat. According to MSDN interpretation, nChannels is the number of channels, nSamplesPerSec is the sampling rate, wFormatTag is WAVE_FORMAT_PCM, wBitsPerSample is 16, nBlockAlign is nChannels*wBitsPerSample/8, nAvgBytesPerSec is nSamplesPerSec*nBlockAlign;

3) Read buf1, buf2, and set the corresponding length;

4) Write waveHeader1, waveHeader2 to the wave device:
waveOutPrepareHeader( hWaveOut, &waveHeader1, sizeof(WAVEHDR));
waveOutPrepareHeader( hWaveOut, &waveHeader2, sizeof(WAVEHDR));

waveOutWrite( hWaveOut, &waveHeader1, sizeof(WAVEHDR) );
waveOutWrite( hWaveOut, &waveHeader2, sizeof(WAVEHDR) );

5) About the callback void CALLBACK waveOutProc( HWAVEOUT hwo,
UINT uMsg,
DWORD dwInstance,
DWORD dwParam1,
DWORD dwParam2 )
{
If(uMsg == WOM_DONE)
{
LPWAVEHDR pWaveHeader = (LPWAVEHDR)dwParam1;//The system automatically recognizes which WAVEHDR has finished playing.

waveOutUnprepareHeader( hwo, pWaveHeader, sizeof(WAVEHDR) );//This function must be called after playing

/ / Fill here WAVEHDR lpdate buffer

waveOutPrepareHeader( hwo, pWaveHeader, sizeof(WAVEHDR));
waveOutWrite( hwo, pWaveHeader, sizeof(WAVEHDR) );

//...
}
Return ;
}

6) After playing, call waveOutClose to release the buffer. Other waveOut functions, such as waveOutPause, waveOutReset, etc., are used when playing the player. If the playback is terminated, you must first call waveOutReset and then call waveOutClose.

This article is a winning article for the Windows Embedded Essay Competition.

Memory RAM

Shenzhen Kaixuanye Technology Co., Ltd. , https://www.icoilne.com