Original Post
Hey guys, I've recently switched my game Gang War over from my DirectSound implementation, to use OpenAL. Everything works great, sounds great. There is one issue though...sometimes calls to alGenSources or alDeleteSources will fail...with the error "Invalid Name". Due to the large nature of the game environments and the large number of potential sound sources (vehicles in particular, I have 1000s in each city) that need to constantly emit sounds, I've come up with this method for handling the large number of potential sound sources. Basicly I'm creating a sphere around the camera and dynamically creating/deleteing the sound sources of the vehicle as it enters/leaves the shpere around the camera. That code is inside the CVehicle::Update() member function of the vehicle class. This function handles the updates of the vehicles and is called every timestep. These are also the calls to alGenSources & alDeleteSources that fail occasionally...I noticed that some vehicles in game do not have engine sounds, which would make sense if the calls fail. I'm wondering if OpenAL allows such frequent creation/destruction of sound sources, or if there are any better methods to use when approaching this problem...I've only been using OpenAL for a few days, I've got a pretty good grasp of it, though information about this type of situation is somewhat hard to come by. I'd appreciate any advice, seasoned OpenAL programmers may have to offer :-D - Dan
class CVehicle
{
....
private:
bool m_SoundActive; //If the sound source is active or not.
ALuint m_SoundSource[1]; //Sound source for the vehicle.
...
}
void CVehicle::Update(void)
{
...
//Entering sphere
if(m_LOD <= VEHICLE_SOUND_DISTANCE_CUTOFF && m_SoundActive == false)
{
//Create source, bind source to engine sound, play sound, etc.
m_SoundActive = true;
alGenSources(1, m_SoundSource);
if((error = alGetError()) != AL_NO_ERROR)
{
GameEngine.LogEntry("ERROR, failure to generate vehicle source : %s", alGetString(error));
return;
} //end of if
alSourceStop(m_SoundSource[0]);
alSourcei(m_SoundSource[0], AL_BUFFER, GameEngine.OpenAL.m_Buffers[VehicleTypes[m_VehicleType].m_PrimarySoundIndex]);
alSourcei(m_SoundSource[0], AL_LOOPING, AL_TRUE);
alSourcePlay(m_SoundSource[0]);
alSourcef(m_SoundSource[0], AL_GAIN, GameVariableValues[34]);
alSourcef(m_SoundSource[0], AL_ROLLOFF_FACTOR, 0.09);
} //end of if
//Already in sphere
if(m_LOD <= VEHICLE_SOUND_DISTANCE_CUTOFF && m_SoundActive == true)
{
//Update source with new position, velocity, frequency, etc.
float sPosition[3] = { -m_Position.x, m_Position.y, -m_Position.z };
float sVelocity[3] = { -m_MovementVector.x, m_MovementVector.y, -m_MovementVector.z };
alSourcefv(m_SoundSource[0], AL_POSITION, sPosition);
alSourcefv(m_SoundSource[0], AL_VELOCITY, sVelocity);
alSourcef(m_SoundSource[0], AL_PITCH, (m_RPM/m_Redline)*1.5);
} //end of if
//Exiting sphere
if(m_LOD > VEHICLE_SOUND_DISTANCE_CUTOFF && m_SoundActive == true)
{
//Delete sound source
m_SoundActive = false;
alSourceStopv(1, m_SoundSource);
alDeleteSources(1, m_SoundSource);
if((error = alGetError()) != AL_NO_ERROR)
{
GameEngine.LogEntry("ERROR, failure to delete vehicle source : %s", alGetString(error));
return;
} //end of if
} //end of if
//Already outside sphere
if(m_LOD > VEHICLE_SOUND_DISTANCE_CUTOFF && m_SoundActive == false)
{
//Do nothing, the vehicle is too far away
} //end of if
} //end of Update function