#include <conio.h>
#include <stdlib.h>
#include <al.h>
#include <alc.h>
#include <alut.h>
#include <stdio.h>
// Maximum data buffers we will need.
#define NUM_BUFFERS 2
// Maximum emissions we will need.
#define NUM_SOURCES 2
// These index the buffers and sources.
#define WALK 0
#define MAIN 1
// Buffers hold sound data.
ALuint Buffers[NUM_BUFFERS];
// Sources are points of emitting sound.
ALuint Sources[NUM_SOURCES];
// Position of the source sounds.
ALfloat SourcePos[NUM_SOURCES][2];
// Velocity of the source sounds.
ALfloat SourceVel[NUM_SOURCES][2];
// Position of the listener.
ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };
// Velocity of the listener.
ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
// Orientation of the listener. (first 3 elements are "at", second 3 are "up")
ALfloat ListenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 };
ALboolean LoadALData()
{
// Variables to load into.
ALenum format;
ALsizei size;
ALvoid* data;
ALsizei freq;
ALboolean loop;
// Load wav data into a buffer.
alGenBuffers(NUM_BUFFERS, Buffers);
if (alGetError() != AL_NO_ERROR)
{
printf("ERROR 10: in loading wave files into buffer\n");
return AL_FALSE;
}
alutLoadWAVFile("Data/foot.wav", &format, &data, &size, &freq, &loop);
alBufferData(Buffers[WALK], format, data, size, freq);
alutUnloadWAV(format, data, size, freq);
alutLoadWAVFile("Data/main.wav", &format, &data, &size, &freq, &loop);
alBufferData(Buffers[MAIN], format, data, size, freq);
alutUnloadWAV(format, data, size, freq);
// Bind buffers into audio sources.
alGenSources(NUM_SOURCES, Sources);
if (alGetError() != AL_NO_ERROR)
{
printf("ERROR 11: in binding buffers to audio sources\n");
return AL_FALSE;
}
alSourcei (Sources[WALK], AL_BUFFER, Buffers[WALK] );
alSourcef (Sources[WALK], AL_PITCH, 1.0 );
alSourcef (Sources[WALK], AL_GAIN, 1.0 );
alSourcefv(Sources[WALK], AL_POSITION, SourcePos[WALK]);
alSourcefv(Sources[WALK], AL_VELOCITY, SourceVel[WALK]);
alSourcei (Sources[WALK], AL_LOOPING, AL_TRUE );
alSourcei (Sources[MAIN], AL_BUFFER, Buffers[MAIN] );
alSourcef (Sources[MAIN], AL_PITCH, 1.0 );
alSourcef (Sources[MAIN], AL_GAIN, 1.0 );
alSourcefv(Sources[MAIN], AL_POSITION, SourcePos[MAIN]);
alSourcefv(Sources[MAIN], AL_VELOCITY, SourceVel[MAIN]);
alSourcei (Sources[MAIN], AL_LOOPING, AL_TRUE );
// Do another error check and return.
if( alGetError() != AL_NO_ERROR)
return AL_FALSE;
return AL_TRUE;
};
void SetListenerValues()
{
alListenerfv(AL_POSITION, ListenerPos);
alListenerfv(AL_VELOCITY, ListenerVel);
alListenerfv(AL_ORIENTATION, ListenerOri);
};
void KillALData()
{
alDeleteBuffers(NUM_BUFFERS, &Buffers[0]);
alDeleteSources(NUM_SOURCES, &Sources[0]);
alutExit();
};
// I add this to my Main function
/Initialize OpenAL and clear the error bit.
alutInit(NULL, 0);
alGetError();
// Load the wav data.
if (LoadALData() == AL_FALSE)
{
printf("FAILED LOADING DATA \n");
return -1;
};
SetListenerValues();
alSourcePlay(Sources[WALK]);//Footsteps sound
alSourcePlay(Sources[MAIN]);
Thanks












