Hi, i don't know what is the goal of your program. I played a little around and it seems to me that this line Header->dwFlags = 0;
in your while loop makes memory leaks.
I have changed some lines and it seems to be ok now.
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
#include <mmreg.h>
#pragma lib "winmm.lib"
#define DEFAULT_CHANNEL_COUNT 1
#define DEFAULT_RATE 44100
#define DEFAULT_DEPTH 16
#define DEFAULT_BANK_COUNT 3
#define DEFAULT_BUFFER_LENGTH DEFAULT_RATE
#define PI 3.14159265
#define DEFAULT_FREQUENCY 440
#define DEFAULT_VOLUME 100
struct AudioSample {
WAVEHDR Header;
short Sample[DEFAULT_BUFFER_LENGTH];
};
WAVEFORMATEX Format;
HWAVEOUT WaveOut;
struct AudioSample Buffer[DEFAULT_BANK_COUNT];
size_t Phase = 0;
float Amplitude = 0;
float Period = 0;
void ShowWaveOutMessage(MMRESULT Result)
{
static char String[256];
waveOutGetErrorText(Result, String, sizeof(String));
MessageBox(NULL, String, "Error Message", MB_ICONERROR | MB_OK);
};
int WINAPI WinMain(HINSTANCE Instance, HINSTANCE Previous, LPSTR Command, int Show)
{
MMRESULT Result = 0;
size_t Bank = 0;
WAVEHDR *Header = NULL;
short *Wave = NULL;
short *Next = NULL;
short *Last = NULL;
float Sample = 0;
ZeroMemory(&Format, sizeof(Format));
Format.wFormatTag = WAVE_FORMAT_PCM;
Format.nChannels = DEFAULT_CHANNEL_COUNT;
Format.nSamplesPerSec = DEFAULT_RATE;
Format.wBitsPerSample = DEFAULT_DEPTH;
Format.nBlockAlign = Format.nChannels * Format.wBitsPerSample / 8;
Format.nAvgBytesPerSec = Format.nSamplesPerSec * Format.nBlockAlign;
Format.cbSize = 0;
Result = waveOutOpen(&WaveOut, WAVE_MAPPER, &Format, 0, 0, CALLBACK_FUNCTION);
if (Result != MMSYSERR_NOERROR)
{
ShowWaveOutMessage(Result);
return 1;
};
for (Bank = 0; Bank < DEFAULT_BANK_COUNT; Bank = Bank + 1)
{
Header = &Buffer[Bank].Header;
Wave = Buffer[Bank].Sample;
if ((Header->dwFlags & WHDR_PREPARED) == 0)
break;
if ((Header->dwFlags & WHDR_DONE) == WHDR_DONE)
{
Result = waveOutUnprepareHeader(WaveOut, Header, sizeof(*Header));
if (Result != MMSYSERR_NOERROR)
{
ShowWaveOutMessage(Result);
return 2;
};
break;
};
};
Next = Wave;
Last = &Wave[DEFAULT_BUFFER_LENGTH];
while (Next < Last)
{
Amplitude = DEFAULT_VOLUME / 100;
Amplitude = DEFAULT_FREQUENCY * Amplitude;
Period = (PI * 2.0f * DEFAULT_FREQUENCY) / DEFAULT_RATE;
Sample = Amplitude * sin(Period * (float)Phase);
*Next = Sample;
Phase = Phase + 1;
Next = Next + 1;
};
ZeroMemory(Header, sizeof(*Header));
Header->lpData = (char *)Wave;
Header->dwBufferLength = DEFAULT_BUFFER_LENGTH;
Header->dwLoops = -1;
Header->dwFlags = WHDR_BEGINLOOP | WHDR_ENDLOOP;
Result = waveOutPrepareHeader(WaveOut, Header, sizeof(*Header));
if (Result != MMSYSERR_NOERROR)
{
ShowWaveOutMessage(Result);
return 3;
};
while (1)
{
Result = waveOutWrite(WaveOut, Header, sizeof(*Header));
//if (Result != MMSYSERR_NOERROR) {
// ShowWaveOutMessage (Result);
// return 4;
//};
//Sleep(100);
};
return 0;
};