#include <windows.h>
#include <stdio.h>
#include <mmsystem.h>
#include <math.h>
#define BUFFER 2048
#define FREC 44100
WAVEFORMATEX WaveFormat;
HWAVEOUT HWaveOut;
DWORD WaveBufSize;
WAVEHDR WaveHeader[2];
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)
{
WaveFormat.nSamplesPerSec = FREC;
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;
WaveBufSize=BUFFER;
float PI=3.14159265358979323846;
unsigned char onda[BUFFER];
for(int i=0;i<BUFFER;i++)
{
int samples_por_ciclo=128;//ACA
onda[i]=int(sin( i*2*PI/(samples_por_ciclo) )*127)+127;
}
waveOutOpen(&HWaveOut, WAVE_MAPPER, &WaveFormat, (DWORD)WaveOutProc, 0, CALLBACK_FUNCTION);
WaveHeader[0].dwBufferLength=WaveBufSize;
WaveHeader[1].dwBufferLength=WaveBufSize;
WaveHeader[0].lpData=(char *)onda;
WaveHeader[1].lpData=(char *)onda;
waveOutSetVolume(HWaveOut,0x1101);
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 detiene el hasta que se aprete la tecla "1"
waveOutUnprepareHeader(HWaveOut, &WaveHeader[1], sizeof(WAVEHDR));
waveOutUnprepareHeader(HWaveOut, &WaveHeader[0], sizeof(WAVEHDR));
waveOutReset(HWaveOut);
waveOutClose(HWaveOut);
}