Win32 API Examples
» Create and play a wave signal
» Create a modal dialog box
» Create a message box
» Create a save dialog box
» Create an open dialog box
» Enable/Disable window items
» Set text in edit controls
» Get text from edit controls
» Set integer in edit controls
» Get integer from edit controls
» Open a homepage
» Get size of screen
» Get/Set position of mouse
» Set/Read check box
» Use Progress Bars

// include mmsystem.h
// include math.h
// #pragma comment(lib,"winmm.lib")

WAVEFORMATEX wave;
HWAVEOUT ghwo;
unsigned char *cBuff;
WAVEHDR pwh;

// Create wave with 16 bits per sample and 44100 samples/sec
wave.wFormatTag = WAVE_FORMAT_PCM;
wave.nChannels = 1;
wave.wBitsPerSample = 16;
wave.nSamplesPerSec = 44100;
wave.nBlockAlign = wave.nChannels*wave.wBitsPerSample/8;
wave.nAvgBytesPerSec = wave.nSamplesPerSec*wave.nBlockAlign;
waveOutOpen(&ghwo, WAVE_MAPPER, &wave, NULL, NULL, CALLBACK_NULL);

// Create buffer
cBuff = new unsigned char[44100*2];
pwh.lpData = (LPSTR)cBuff;
pwh.dwBufferLength = 44100*2;
pwh.dwFlags = WHDR_BEGINLOOP;
pwh.dwLoops = 1;
waveOutPrepareHeader(ghwo, &pwh, sizeof(pwh));
pwh.dwFlags = WHDR_BEGINLOOP | WHDR_ENDLOOP | WHDR_PREPARED;
pwh.dwLoops = 2;

// Create signal	
double freq = 500;
double x = freq*3.14159265358979323846/44100;
double amp = 7000;
short int nBuff;
int i,j;
int s = 0;

for(j = 0; j < 10; j++)
{
	for(i = 0; i < 4410; i++)
	{
		nBuff = (int)(sin(i*x) * amp);
		memcpy(cBuff+s, &nBuff, 2);
		s+=2;
	}
	freq += 500;
	x = freq*3.14159265358979323846/44100;
}

// Play sound
waveOutReset(ghwo);
waveOutWrite(ghwo, &pwh, sizeof(WAVEHDR));

Sleep(2000);

waveOutReset(ghwo);
waveOutClose(ghwo);
delete[] cBuff;
Copyright (c) 2004 PerErik Klarenfjord, All Rights Reserved
All trademarks are the property of their owners
Any actions you take after reading this website are your own responsibility
MADE IN SWEDEN
Hosted by GMQ