#include <windows.h>
#include <stdio.h>
#include <mmsystem.h>
#include <math.h>
WAVEFORMATEX WaveFormat;
HWAVEOUT HWaveOut;
DWORD WaveBufSize;
WAVEHDR WaveHeader[2];
unsigned char onda[1024];
void CALLBACK WaveOutProc(HWAVEOUT waveOut, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
{
if(uMsg==MM_WOM_DONE) waveOutWrite(HWaveOut, (WAVEHDR *)dwParam1, sizeof(WAVEHDR));
}
int main(int argc, char **argv)
{
//Creo la onda
float PI=3.14159265358979323846;
for(int i=0;i<1024;i++) onda[i]=int(sin(i*2*PI/16.0)*128)+127;
//Establesco los parametros de sonido
WaveFormat.nSamplesPerSec = 26000;
WaveFormat.wBitsPerSample = 8;
WaveFormat.nChannels = 1;
WaveFormat.cbSize = 0;
WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
WaveFormat.nBlockAlign = (WaveFormat.wBitsPerSample /8) * WaveFormat.nChannels;
WaveFormat.nAvgBytesPerSec = WaveFormat.nBlockAlign * WaveFormat.nSamplesPerSec;
//Pongo el driver a sonar
waveOutOpen(&HWaveOut, WAVE_MAPPER, &WaveFormat, (DWORD)WaveOutProc, 0, CALLBACK_FUNCTION);
WaveBufSize=1024;
WaveHeader[0].lpData=(char *)onda;
WaveHeader[1].lpData=(char *)onda;
WaveHeader[0].dwBufferLength=WaveBufSize;
WaveHeader[1].dwBufferLength=WaveBufSize;
waveOutPrepareHeader(HWaveOut, &WaveHeader[0], sizeof(WAVEHDR));
waveOutPrepareHeader(HWaveOut, &WaveHeader[1], sizeof(WAVEHDR));
waveOutWrite(HWaveOut, &WaveHeader[0], sizeof(WAVEHDR));
waveOutWrite(HWaveOut, &WaveHeader[1], sizeof(WAVEHDR));
while(GetKeyState(0x31)>=0) printf("");//Esta linea solo detiene el programa para que no se cierre
waveOutReset(HWaveOut);
waveOutUnprepareHeader(HWaveOut, &WaveHeader[1], sizeof(WAVEHDR));
waveOutUnprepareHeader(HWaveOut, &WaveHeader[0], sizeof(WAVEHDR));
waveOutClose(HWaveOut);
}