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.

Our company is specialized in supplying all the Refrigerant Gas .
We devoting to fluoro refrigerants manufacturing , distribution and new product development, and offering a series of safe and fast business including  produce, filling, sales,  delivery and storage.Our main products cover the traditional refrigerants such as R12,R22,R134a,R600a,R152a.R141b.R142b.R143a.R123.R124.R125.R32.R401a.R402a.R404a.R406a.R407c.R408a.R409a.R410a.R415b.R502 etc, and our new  environment  friendly  products  which is Freon-free  gas,The voriety of our products can satisfy different applications and various technical requirements. The package such as 300g,500g,1000g,13.6kg,22.7kg,400L,800L,1000kg and ISO Tank.

Refrigerant Gas

Refrigerant Gas R410A,Refrigerant Gas R134A,Refrigerant Gas R22,High Purity Refrigerant Gas R22

ZHEJIANG ICE LOONG ENVIRONMENTAL SCI-TECH CO.,LTD. , http://www.ice-loong.com