La frecuencia del tono esta determinada por la variable samples_por_ciclo, cuanto mas baja sea esta variable es mas agudo el sonido
Anda bien con valores 8, 16, 32, 64, 128 (los valores tienen q ser potencia de 2 para que suene agradable), pero si pongo un numero mas grande (como 256) no suena nada.
¿saben porque?
miren la linea 33, prueben el programa con samples_por_ciclo=128, y luego con samples_por_ciclo=256
Código C++:
Ver original
#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 } 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)); waveOutUnprepareHeader(HWaveOut, &WaveHeader[1], sizeof(WAVEHDR)); waveOutUnprepareHeader(HWaveOut, &WaveHeader[0], sizeof(WAVEHDR)); waveOutReset(HWaveOut); waveOutClose(HWaveOut); }