[Solved]Sound plays twice

mcimolin

27-07-2010 18:18:21

In my application a crash sounds plays when ever a user drives into an object. The problem I'm having is
that when this happens, the sound plays twice. I've checked the audio file, and the effect only plays once
in it, and I've checked to make sure that I'm only calling the play() method once for the sounds and that
the sound is set to not repeat. I've even put in debug output statements to see if it was a logic error that
had the function being called more than once, but it is only being called once so I have no idea what is
making the sound play twice.
Here is the relevant code:
The audio core code:
#include "AudioCore.h"
#include "HelpfulUtilities.h"

/*
Constructor
*/
AudioCore::AudioCore(){
mSoundManager = OgreOggSound::OgreOggSoundManager::getSingletonPtr();
}


/*
Destructor
*/
AudioCore::~AudioCore(){
mSoundManager->destroyAllSounds();
delete mSoundManager;
}


/*
Iinitializes the sound manager
*/
void AudioCore::init(){
mSoundManager->init();
sCrash = mSoundManager->createSound("Crash", "thud.wav", true, false);
sCrash->setVolume(0.5);
sBackground = mSoundManager->createSound("Background", "Background.ogg", true, true);
sBackground->setVolume(0.3);
mListener = mSoundManager->getListener();
}


/*
Plays a sound effect
*/
void AudioCore::playSoundEffect(){
sCrash->play();
}

/*
Plays the background music
*/
void AudioCore::playBackgroundSound(){
sBackground->play();
}


/*
Updates the sounds
*/
void AudioCore::update(Ogre::Real time){
mSoundManager->update(time);
}


/*
Returns sCrash
*/
OgreOggSound::OgreOggISound* AudioCore::getSoundEffect(){
return sCrash;
}


/*
Returns sBackground
*/
OgreOggSound::OgreOggISound* AudioCore::getBackgroundMusic(){
return sBackground;
}


/*
Returns a listener
*/
OgreOggSound::OgreOggListener* AudioCore::getListener(){
return mListener;
}


The collision code that calls the audio core:
int Application::updateScore(int collisions){

if(currentCollisions < collisions){
audioCore->playSoundEffect();
}

currentCollisions = collisions;
return(100 - currentCollisions);
}


And the code that builds the object that both plays the sound and is the listener:
void Application::startGame(int position, int chairType){

if(controller->gameReady() == false){
return;
}

wheelChair = new WheelChairVehicle(NxOgre::Vec3(0,10000000,0), mScene, mRenderSystem, mSceneMgr, chairType);

wheelChair->mActor->putToSleep();

chairPosition(position);
cameraPosition(position);
changeChairMode();

mScene->getScene()->setGroupCollisionFlag(1, 2, true);
mScene->getScene()->setActorGroupPairFlags(1, 2, NX_NOTIFY_ON_START_TOUCH);

reporter = new ContactReport();

//listener
mScene->getScene()->setUserContactReport(reporter);
controller->setGameReady(false);

wheelChair->mCameraNode->getParentSceneNode()->attachObject(audioCore->getListener());
wheelChair->mWheelChairNode->attachObject(audioCore->getBackgroundMusic());
wheelChair->mWheelChairNode->attachObject(audioCore->getSoundEffect());

toggleCameraMode();
audioCore->playBackgroundSound();
}


I'm using the latest version of OgreOggSound, the latest version of OpenAL, and Ogre 1.7.0 in
Visual Studio 2008.

Everything else is updating correctly on a collision other than the sound playing twice so I'm not sure
what to do here. Any help is greately appretiated.

stickymango

27-07-2010 22:03:55

Try not setting 'stream' to true when creating the sound:

sCrash = mSoundManager->createSound("Crash", "thud.wav", false, false);
Streamed sounds should only be used for extended sounds not short sounds, I think it maybe queueing up a minimum amount of audio data which could be wrapping around.

mcimolin

27-07-2010 22:22:34

Beauty, that worked like a charm. Didn't even think of that.

Thanks for the quick reply.

stickymango

28-07-2010 11:32:56

No problem, guess I need to take a look at that :oops: