Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

OpenAL-initialization

This topic is 2806 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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
#define NUM_ENVIRONMENTS 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)
{
alListenerfv(AL_POSITION,listenerPos);
alListenerfv(AL_VELOCITY,listenerVel);
alListenerfv(AL_ORIENTATION,listenerOri);

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");
exit(1);
}
else
{
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

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

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

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


alutLoadWAVFile("a.wav",&format,&data,&size,&freq);
alBufferData(buffer[0],format,data,size,freq);
alutUnloadWAV(format,data,size,freq);

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");
exit(2);
}
else
{
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!