Some code to test that
#define WIN32_DEFAULT_LIBS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
#define _USE_MATH_DEFINES
#include <math.h>
//#define CHANNEL 2
#define SAMPLE_RATE 44100
void writeAudioBlock(HWAVEOUT hWaveOut, LPSTR block, DWORD size);
VOID FillBufferLR(short *pBuffer, int nSize, int iFreq, int iSampleRate);
char *WinMMErrorString(MMRESULT res)
{
static char szError[MAXERRORLENGTH];
waveOutGetErrorText(res, szError, sizeof(szError));
return szError;
}
int WINAPI WinMain(HINSTANCE Instance, HINSTANCE Previous, LPSTR Command, int Show)
{
HWAVEOUT hWaveOut;
WAVEFORMATEX wfx;
WAVEHDR whdr;
MMRESULT rc;
short aSine[16385];
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = 2;
wfx.nSamplesPerSec = SAMPLE_RATE;
wfx.wBitsPerSample = 16;
wfx.cbSize = 0;
wfx.nBlockAlign = (wfx.wBitsPerSample >> 3) * wfx.nChannels;
wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
if ((rc = waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, 0, 0, CALLBACK_NULL)) != MMSYSERR_NOERROR)
{
MessageBox(0, WinMMErrorString(rc), 0, MB_ICONERROR | MB_OK);
return 1;
}
// Some tests
FillBufferLR(aSine, sizeof(aSine)/sizeof(aSine[0]), 440, SAMPLE_RATE);
whdr.lpData = (LPSTR)&aSine;
whdr.dwBufferLength = sizeof(aSine)/sizeof(aSine[0]);
whdr.dwUser = 0;
whdr.dwFlags = 0;
whdr.dwLoops = 0;
whdr.lpNext = NULL;
whdr.reserved = 0;
waveOutPrepareHeader(hWaveOut, &whdr, sizeof(WAVEHDR));
waveOutWrite(hWaveOut, &whdr, sizeof(WAVEHDR));
Sleep(100);
while (waveOutUnprepareHeader(hWaveOut, &whdr, sizeof(WAVEHDR)) == WAVERR_STILLPLAYING)
Sleep(100);
waveOutClose(hWaveOut);
return 0;
}
// Stereo L R 8-bit data is unsigned, but 16-bit data is signed
VOID FillBufferLR(short *pBuffer, int nSize, int iFreq, int iSampleRate)
{
double fAngle;
int i;
fAngle = 0;
for (i = 0; i < nSize; i+=2) {
pBuffer[i] = (short) (0x7FFF * sin(fAngle)*0.5);
pBuffer[i+1] = pBuffer[i];
fAngle += 2 * M_PI * iFreq / iSampleRate;
if (fAngle > 2 * M_PI)
fAngle -= 2 * M_PI;
}
}