Sign in to follow this  


Recommended Posts

Kevin2k    100
The standard set of headers are these:

#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <AL/al.h>
#include <AL/alut.h>

However, for Mac OS X, there are slight changes necessary for the GLUT and OpenAL libraries.

#include <stdio.h>
#include <stdlib.h>
#include <GLUT/glut.h>
#include <OpenAL/alut.h>

A standard has been developed to avoid the use of global variables, because they are generally considered bad programming style. This is to help avoid odd programming errors which can occur if global variables are used. When used with proper care, they can be useful and make programming an easier process, especially with OpenGL and OpenAL. Good practice dictates that most variables should remain local to several functions, and if the variables need to be shared, they are then passed as parameters. Such careful programming style can create unnecessary complications when working with OpenGL and OpenAL, so several global variables are made available to the program. In this example, only one sound is being loaded, which explains why only one buffer and source is being declared.

#define NUM_BUFFERS 1
#define NUM_SOURCES 1

There is only one listener, and these arrays define the initial set up of the listener for its position, direction, and velocity. The listener is not moving at the start, but can move around any other sound source.

ALfloat listenerPos[]={0.0,0.0,4.0};
ALfloat listenerVel[]={0.0,0.0,0.0};
Alfloat listenerOri[]={0.0,0.0,1.0, 0.0,1.0,0.0};

Each sound source has similar properties that the listener has. The source0Pos and source0Vel arrays show the position and velocity of the sound source.

ALfloat source0Pos[]={ -2.0, 0.0, 0.0};
ALfloat source0Vel[]={ 0.0, 0.0, 0.0};

Sounds need to be stored in an array, similar to textures. Several buffers are needed to contain the sounds and other necessary information. The size, freq, format, and data variables are used when loading the sound files.

Aluint buffer[NUM_BUFFERS];
Aluint source[NUM_SOURCES];
ALuint environment[NUM_ENVIRONMENTS];

ALsizei size,freq;
ALenum format;
ALvoid *data;

The init function is called from the program's main function. It begins by setting the initial positions for the listener position, velocity, and orientation.

void init(void)

Next, the program creates the buffers and checks for any problems in the process.

alGetError(); // clear any error messages

// Generate buffers, or else no sound will happen!
alGenBuffers(NUM_BUFFERS, buffer);

if(alGetError() != AL_NO_ERROR)
printf("- Error creating buffers !!\n");
printf("init() - No errors yet.");

The next three lines are the most important in loading the wave file. If more than one sound is loaded, then these three lines of code are repeated, but they reference different sound files and buffer positions. Refer to the table below to view how the alutLoadWAVFile call differs between platforms.

Operating System Source Code

alutLoadWAVFile((Albyte *) "c.wav", &format, &data, size,&freq, &al_bool);

alutLoadWAVFile("c.wav", &format, &data, &size, &freq);

alutLoadWAVFile("c.wav", &format, &data, &size, &freq, &al_bool);


Similar to how the buffers were created, the sources are now created and checked for any errors.

alGetError(); /* clear error */
alGenSources(NUM_SOURCES, source);

if(alGetError() != AL_NO_ERROR)
printf("- Error creating sources !!\n");
printf("init - no errors after alGenSources\n");

Finally, the properties of the sound are set. First, the sound pitch and gain are set, then the source's position and orientation are defined, and finally the source is associated with its buffer to make it loop through the wave file. Setting the AL_LOOPING value to AL_TRUE determines whether or not the sound will play continuously, or if it will just play once and then stop. For ambient, background music, one might consider setting this to AL_TRUE, but for a sporadic sound such as a gun shot or a foot stomp, then AL_LOOPING should be AL_FALSE.

alSourcef(source[0], AL_PITCH, 1.0f);
alSourcef(source[0], AL_GAIN, 1.0f);
alSourcefv(source[0], AL_POSITION, source0Pos);
alSourcefv(source[0], AL_VELOCITY, source0Vel);
alSourcei(source[0], AL_BUFFER,buffer[0]);
alSourcei(source[0], AL_LOOPING, AL_TRUE);


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this