Attempting to use OpenAL in C++ for a video game
Colin DeClue <[email protected]>
| Newsgroups | gmane.comp.lib.openal |
|---|---|
| Message-ID | <[email protected]> |
Hi all,
I'm attempting to use OpenAL to play sound effects for a video game I'm
writing as part of a school project. It's written in C++ and uses the
freealut library to ease interface with OpenAL. I'm using
alutCreateBufferFromFile to load the wav file into a buffer (created with
alGenBuffers), and then tie that buffer to a source (generated with
alGenSources). When I go to play it, I verify that the source is actually a
source, and it is. However, when I call alSourcePlay, the sound does not
play. Additionally, the source state stays at AL_INITIAL, rather than going
to AL_PLAYING. Anyone have any idea how I can fix this? I've included some
of the code below.
Here's the code where I actually play the sound:
// Make sure the audio source ident is valid and usable
if ( audioID >= MAX_AUDIO_SOURCES || !mAudioSourceInUse[audioID])
return false;
int sourceAudioState = 0;
if(!alIsSource(mAudioSources[audioID]) == AL_TRUE) return false;
alGetError();
// Are we currently playing the audio source?
alGetSourcei( mAudioSources[audioID], AL_SOURCE_STATE, &sourceAudioState );
if ( sourceAudioState == AL_PLAYING )
{
printf("Currently playing!\n");
if ( forceRestart )
stopAudio( audioID );
else
return false; // Not forced, so we don't do anything
}
alSourcePlay( mAudioSources[ audioID ] );
if ( checkALError( "playAudio::alSourcePlay: ") )
return false;
alGetSourcei( mAudioSources[audioID], AL_SOURCE_STATE, &sourceAudioState );
if(sourceAudioState == AL_PLAYING)
{
printf("Now playing!\n\n");
}
return true;
Here's the code where I initialize everything:
// It's an error to initialise twice OpenAl
if ( isInitialised ) return true;
// Open an audio device
mSoundDevice = alcOpenDevice( NULL ); // TODO ((ALubyte*) "DirectSound3D");
// mSoundDevice = alcOpenDevice( "DirectSound3D" );
// Check for errors
if ( !mSoundDevice )
{
printf( "SoundManager::init error : No sound device.\n");
return false;
}
mSoundContext = alcCreateContext( mSoundDevice, NULL );
// if ( checkAlError() || !mSoundContext ) // TODO seems not to work! why
?
if ( !mSoundContext )
{
printf( "SoundManager::init error : No sound context.\n");
return false;
}
// Make the context current and active
alcMakeContextCurrent( mSoundContext );
if ( checkALError( "Init()" ) )
{
printf( "SoundManager::init error : could not make sound context current and
active.\n");
return false;
}
// Test if Ogg Vorbis extension is present
isOggExtensionPresent();
// Create the Audio Buffers
alGenBuffers( MAX_AUDIO_BUFFERS, mAudioBuffers );
if (checkALError("init::alGenBuffers:") )
return false;
std::cout << "Number of available sources: " << MAX_AUDIO_BUFFERS;
// Generate Sources
alGenSources( MAX_AUDIO_SOURCES, mAudioSources );
if (checkALError( "init::alGenSources :") )
return false;
// Setup the initial listener parameters
// -> location
alListenerfv( AL_POSITION, position );
if(checkALError("Position")) return false;
// -> velocity
alListenerfv( AL_VELOCITY, velocity );
if(checkALError("velocity")) return false;
// -> orientation
alListenerfv( AL_ORIENTATION, orientation );
if(checkALError("orientation")) return false;
// Gain
alListenerf( AL_GAIN, 1.0 );
if(checkALError("gain")) return false;
// Initialise Doppler
alDopplerFactor( 1.2 ); // 1.2 = exaggerate the pitch shift by 20%
alDopplerVelocity( 343.0f ); // m/s this may need to be scaled at some point
// Ok
isInitialised = true;
isSoundOn = true;
printf( "\n\nSoundManager initialised.\n\n");
return true;
And here's where I load the audio file into the buffer:
std::string mFullPath = mAudioPath;
alGetError(); // Clear Error Code
// Load in the WAV file from disk
//mFullPath += "\\";
mFullPath += filename;
mAudioBuffers[bufferID] = alutCreateBufferFromFile(filename.c_str());
if ( checkALError("loadWAV::alutCreateBufferFromFile: ") )
return false;
return true;
And here's where I tie the buffer to the source:
if ( filename.empty() || filename.length() > MAX_FILENAME_LENGTH )
return false;
if ( mAudioSourcesInUseCount == MAX_AUDIO_SOURCES )
return false; // out of Audio Source slots!
int bufferID = -1; // Identity of the Sound Buffer to use
int sourceID = -1; // Identity of the Source Buffer to use
alGetError(); // Clear Error Code
for(int i = 0; i < 100; i++) alGetError();
// if(checkALError("why?")) return false;
// Check and see if the pSoundFile is already loaded into a buffer
bufferID = locateAudioBuffer( filename );
if ( bufferID < 0 )
{
// The sound file isn't loaded in a buffer, lets attempt to load it on the
fly
bufferID = loadAudioInToSystem( filename );
if ( bufferID < 0 ) return false; // failed!
}
// If you are here, the sound the requester wants to reference is in a
buffer.
// Now, we need to find a free Audio Source slot in the sound system
sourceID = 0;
while ( mAudioSourceInUse[ sourceID ] == true ) sourceID++;
// When you are here, 'mSourceID' now represents a free Audio Source slot
// The free slot may not be at the end of the array but in the middle of it.
*audioId = sourceID; // return the Audio Source ID to the caller
mAudioSourceInUse[ sourceID ] = true; // mark this Source slot as in use
mAudioSourcesInUseCount++; // bump the 'in use' counter
// Now inform OpenAL of the sound assignment and attach the audio buffer
// to the audio source
alSourcei( mAudioSources[sourceID], AL_BUFFER, mAudioBuffers[bufferID] );
alSourcef(mAudioSources[sourceID], AL_PITCH, 1.0f);
alSourcef(mAudioSources[sourceID], AL_GAIN, 1.0f);
alSourcefv(mAudioSources[sourceID], AL_POSITION, position);
alSourcefv(mAudioSources[sourceID], AL_VELOCITY, velocity);
// Steven : Not in the original code !!!!!
alSourcei( mAudioSources[sourceID], AL_LOOPING, loop );
if ( checkALError( "loadSource()::alSourcei" ) )
return false;
return true;
--
Colin DeClue
_______________________________________________
Openal mailing list
[email protected]
http://opensource.creative.com/mailman/listinfo/openal