From: <wha...@us...> - 2011-12-26 23:26:37
|
Revision: 7916 http://planeshift.svn.sourceforge.net/planeshift/?rev=7916&view=rev Author: whacko88 Date: 2011-12-26 23:26:31 +0000 (Mon, 26 Dec 2011) Log Message: ----------- cleaner initialization of the sound plugin, this should solve once for all crushes when something in the initialization goes wrong cleaned code in SoundSystem fixed coding standards and doc in SoundSystem Modified Paths: -------------- trunk/src/plugins/common/soundmanager/data.cpp trunk/src/plugins/common/soundmanager/handle.cpp trunk/src/plugins/common/soundmanager/songhandle.cpp trunk/src/plugins/common/soundmanager/system.cpp trunk/src/plugins/common/soundmanager/system.h Modified: trunk/src/plugins/common/soundmanager/data.cpp =================================================================== --- trunk/src/plugins/common/soundmanager/data.cpp 2011-12-26 23:22:47 UTC (rev 7915) +++ trunk/src/plugins/common/soundmanager/data.cpp 2011-12-26 23:26:31 UTC (rev 7916) @@ -130,6 +130,12 @@ bool error = false; bool isDynamic = false; + // checking if this has been initialized correctly + if(!sndLoader.IsValid() || !vfs.IsValid()) + { + return 0; + } + // checking the sound library soundFile = libSoundFiles.Get(name, 0); if(soundFile == 0) // maybe this is a dynamic file @@ -231,6 +237,12 @@ csRef<iDocumentNode> node; /* yet another node .... */ SoundFile* snd; ///< soundfile + // checking if this has been initialized correctly + if(!vfs.IsValid()) + { + return false; + } + if(!(xml = csQueryRegistry<iDocumentSystem>(objectReg))) xml = csPtr<iDocumentSystem>(new csTinyDocumentSystem); Modified: trunk/src/plugins/common/soundmanager/handle.cpp =================================================================== --- trunk/src/plugins/common/soundmanager/handle.cpp 2011-12-26 23:22:47 UTC (rev 7915) +++ trunk/src/plugins/common/soundmanager/handle.cpp 2011-12-26 23:26:31 UTC (rev 7916) @@ -109,7 +109,11 @@ return false; } - SoundSystemManager::GetSingleton().GetSoundSystem()->CreateSource(sndstream, sndsource); + if(!SoundSystemManager::GetSingleton().GetSoundSystem()->CreateSource(sndstream, sndsource)) + { + return false; + } + preset_volume = volume_preset; sndCtrl = ctrl; name = csString(resname); @@ -180,13 +184,13 @@ { maxDistance = maxdist; - SoundSystemManager::GetSingleton().GetSoundSystem()->Create3dSource(sndsource, sndsource3d, mindist, maxdist, + SoundSystemManager::GetSingleton().GetSoundSystem()->Create3DSource(sndsource, sndsource3d, mindist, maxdist, pos); /* create a directional source if rad > 0 */ if(rad > 0) { - SoundSystemManager::GetSingleton().GetSoundSystem()->CreateDirectional3dSource(sndsource3d, sndsourcedir, + SoundSystemManager::GetSingleton().GetSoundSystem()->CreateDirectional3DSource(sndsource3d, sndsourcedir, dir, rad); } } Modified: trunk/src/plugins/common/soundmanager/songhandle.cpp =================================================================== --- trunk/src/plugins/common/soundmanager/songhandle.cpp 2011-12-26 23:22:47 UTC (rev 7915) +++ trunk/src/plugins/common/soundmanager/songhandle.cpp 2011-12-26 23:26:31 UTC (rev 7916) @@ -68,7 +68,11 @@ } static_cast<SndSysSongStream*>(&(*sndstream))->SetErrorRate(errorRate); - SoundSystemManager::GetSingleton().GetSoundSystem()->CreateSource(sndstream, sndsource); + if(!SoundSystemManager::GetSingleton().GetSoundSystem()->CreateSource(sndstream, sndsource)) + { + return false; + } + preset_volume = volumePreset; sndCtrl = ctrl; Modified: trunk/src/plugins/common/soundmanager/system.cpp =================================================================== --- trunk/src/plugins/common/soundmanager/system.cpp 2011-12-26 23:22:47 UTC (rev 7915) +++ trunk/src/plugins/common/soundmanager/system.cpp 2011-12-26 23:26:31 UTC (rev 7916) @@ -27,22 +27,19 @@ #include <iutil/cfgmgr.h> -/* - * Get a renderer und returns false or true - */ bool SoundSystem::Initialize(iObjectRegistry* objectReg) { float rollOff; - sndrenderer = csQueryRegistry<iSndSysRenderer>(objectReg); - if(!sndrenderer.IsValid()) + sndRenderer = csQueryRegistry<iSndSysRenderer>(objectReg); + if(!sndRenderer.IsValid()) { Error1("Failed to locate Sound renderer!"); return false; } - listener = sndrenderer->GetListener(); + listener = sndRenderer->GetListener(); if(!listener.IsValid()) { Error1("Failed to get a sound global listener!"); @@ -64,125 +61,129 @@ return true; } -/* - * create a _paused_ stream using a iSndSysData handle - * loop >= 1 .. make it loop - * type can be one of those: - * - * CS_SND3D_DISABLE=0 - * CS_SND3D_RELATIVE=1 - * CS_SND3D_ABSOLUTE=2 - * - * modifications are done on objects to caller gave is - * returns false or true - */ - -bool SoundSystem::CreateStream(csRef<iSndSysData> &snddata, int loop, - int type, csRef<iSndSysStream> &sndstream) +bool SoundSystem::CreateStream(csRef<iSndSysData> &sndData, bool loop, + int type, csRef<iSndSysStream> &sndStream) { - if(!(sndstream = sndrenderer->CreateStream(snddata, type))) + // checking that SoundSystem has been correctly initialized + if(!sndRenderer.IsValid()) { - Error2("Can't create stream for '%s'!", - snddata->GetDescription()); return false; } + // the sound renderer must create the stream and keep track of it + sndStream = sndRenderer->CreateStream(sndData, type); + if(!sndStream.IsValid()) + { + Error2("Can't create stream for '%s'!", sndData->GetDescription()); + return false; + } + // make it loop if requested - if(loop >= 1) + if(loop) { - sndstream->SetLoopState(CS_SNDSYS_STREAM_LOOP); + sndStream->SetLoopState(CS_SNDSYS_STREAM_LOOP); } else { - sndstream->SetLoopState(CS_SNDSYS_STREAM_DONTLOOP); + sndStream->SetLoopState(CS_SNDSYS_STREAM_DONTLOOP); } return true; } -/* - * Removes the given stream - */ +void SoundSystem::RemoveStream(csRef<iSndSysStream> &sndStream) +{ + // checking that SoundSystem has been correctly initialized + if(!sndRenderer.IsValid()) + { + return; + } -void SoundSystem::RemoveStream(csRef<iSndSysStream> &sndstream) -{ - sndrenderer->RemoveStream(sndstream); + // making sound renderer remove the stream from the system + sndRenderer->RemoveStream(sndStream); } -/* - * Creates a source - * if its 2D or 3D depends on the stream - * volume is 1 by default - we set it to 0 - ALWAYS - */ +bool SoundSystem::CreateSource(csRef<iSndSysStream> &sndStream, + csRef<iSndSysSource> &sndSource) +{ + // checking that SoundSystem has been correctly initialized + if(!sndRenderer.IsValid()) + { + return false; + } -bool SoundSystem::CreateSource(csRef<iSndSysStream> &sndstream, - csRef<iSndSysSource> &sndsource) -{ - sndsource = sndrenderer->CreateSource(sndstream); - sndsource->SetVolume(0); + // the sound renderer must create the source and keep track of it + sndSource = sndRenderer->CreateSource(sndStream); + if(!sndSource.IsValid()) + { + Error2("Can't create source for '%s'!", sndStream->GetDescription()); + return false; + } + + // default volume is 1, we set to 0 + sndSource->SetVolume(0); + return true; } -/* - * removeing the source doesnt remove the stream! - * this is important! - */ +void SoundSystem::RemoveSource(csRef<iSndSysSource> &sndSource) +{ + // checking that SoundSystem has been correctly initialized + if(!sndRenderer.IsValid()) + { + return; + } -void SoundSystem::RemoveSource(csRef<iSndSysSource> &sndsource) -{ - sndrenderer->RemoveSource(sndsource); + // making sound renderer remove the source from the system + sndRenderer->RemoveSource(sndSource); } +void SoundSystem::Create3DSource(csRef<iSndSysSource> &sndSource, + csRef<iSndSysSource3D> &sndSource3D, + float minDist, float maxDist, csVector3 pos) +{ + // the renderer keep track of only one source; if the 2D source has been + // created with type != CS_SND3D_DISABLE the it will have this interface + sndSource3D = scfQueryInterface<iSndSysSource3D>(sndSource); -/* - * this creates the 3d source out of an normal source - * we need distance and positional parameters as well as volume - * this thing doesnt make a sound .. yet .. because the stream is still paused - * doesnt need a special remove functions because this is within openal - * use RemoveSource to remove it - */ - -void SoundSystem::Create3dSource(csRef<iSndSysSource> &sndsource, - csRef<iSndSysSource3D> &sndsource3d, - float mindist, float maxdist, csVector3 pos) -{ - sndsource3d = scfQueryInterface<iSndSysSource3D>(sndsource); - sndsource3d->SetMinimumDistance(mindist); - sndsource3d->SetMaximumDistance(maxdist); - sndsource3d->SetPosition(pos); + // setting parameters + sndSource3D->SetMinimumDistance(minDist); + sndSource3D->SetMaximumDistance(maxDist); + sndSource3D->SetPosition(pos); } /* - * create a directional source out of a 3d source - * we need the direction its sending to and the radius(?) of the cone it creates into + * We need the direction its sending to and the radius(?) of the cone it creates into * that direction - experimental - * doesnt need a special remove functions because this is within openal - * use RemoveSource to remove it */ - -void SoundSystem::CreateDirectional3dSource - (csRef<iSndSysSource3D>& /*sndsource3d*/, - csRef<iSndSysSource3DDirectionalSimple> &sndsourcedir, +void SoundSystem::CreateDirectional3DSource + (csRef<iSndSysSource3D>& /*sndSource3D*/, + csRef<iSndSysSource3DDirectionalSimple> &sndSourceDir, csVector3 direction, float rad) { - sndsourcedir->SetDirection(direction); - sndsourcedir->SetDirectionalRadiation(rad); + sndSourceDir->SetDirection(direction); + sndSourceDir->SetDirectionalRadiation(rad); } -/* - * Updates the listener position - * v is position - * f is front - * t is top - */ - void SoundSystem::UpdateListener(csVector3 v, csVector3 f, csVector3 t) { + // checking that SoundSystem has been correctly initialized + if(!listener.IsValid()) + { + return; + } + listener->SetPosition(v); listener->SetDirection(f,t); } -const csVector3& SoundSystem::GetListenerPosition() const +csVector3 SoundSystem::GetListenerPosition() const { + // checking that SoundSystem has been correctly initialized + if(!listener.IsValid()) + { + return csVector3(0, 0, 0); + } + return listener->GetPosition(); } Modified: trunk/src/plugins/common/soundmanager/system.h =================================================================== --- trunk/src/plugins/common/soundmanager/system.h 2011-12-26 23:22:47 UTC (rev 7915) +++ trunk/src/plugins/common/soundmanager/system.h 2011-12-26 23:26:31 UTC (rev 7916) @@ -30,93 +30,104 @@ #define DEFAULT_LISTENER_ROLL_OFF 1.0 + /** - * This is an Interface Class to the Crystalspace Soundrenderer. - * It works like a Wrapper and has some additional functionalities but - * no own data/objects. All it does is simplifying access to the Soundrenderer. + * This is an interface class to the CrystalSpace sound renderer. It works like a + * wrapper and has some additional functionalities but at the moment it hasn't own + * data/objects. All it does is simplifying access to the sound renderer. In the + * future this will be the class that controls the number of active sources. */ - class SoundSystem { public: /** - * initializes this object and tries to load the soundrenderer. - * returns true or false - * @param objectReg ps objectreg where we find our soundrenderer + * Initializes this object by loading the CS sound renderer and creating a + * global listener. + * @param objectReg ps iObjectRegistry used to get the sound renderer. + * @return true on success, false otherwise. */ bool Initialize(iObjectRegistry* objectReg); /** - * Creates a stream out of the given snddata. - * @param snddata valid iSndSysData object - * @param loop LOOP or DONT_LOOP - * @param type 3dtype of this sound CS_SND3D_DISABLE=0 CS_SND3D_RELATIVE=1 or CS_SND3D_ABSOLUTE=2 - * @param sndstream your iSndSysStream object + * Creates a stream out of the given sound data. + * @param sndData valid iSndSysData object. + * @param loop true if the sound must loop, false otherwise. + * @param type 3D type of this sound; it can have these values CS_SND3D_DISABLE=0, + * CS_SND3D_RELATIVE=1 or CS_SND3D_ABSOLUTE=2. + * @param sndStream the iSndSysStream object that will contain the stream. + * @return true on success, false if SoundSystem hasn't been initialized correctly. */ - bool CreateStream(csRef<iSndSysData> &snddata, int loop, int type, - csRef<iSndSysStream> &sndstream); + bool CreateStream(csRef<iSndSysData> &sndData, bool loop, int type, + csRef<iSndSysStream> &sndStream); /** - * Removes a stream. - * @param sndstream iSndSysStream object to remove + * Removes a stream from the sound renderer. + * @param sndStream iSndSysStream object to remove. */ - void RemoveStream(csRef<iSndSysStream> &sndstream); + void RemoveStream(csRef<iSndSysStream> &sndStream); /** - * Create a Source associated to your Stream. - * @param sndstream your iSndSysStream object - * @param sndsource your iSndSysSource object + * Create a source associated to your stream with volume 0. + * @param sndStream your iSndSysStream object. + * @param sndSource the iSndSysSource object that will contain the source. + * @return true on success, false if SoundSystem hasn't been initialized correctly. */ - bool CreateSource(csRef<iSndSysStream> &sndstream, - csRef<iSndSysSource> &sndsource); + bool CreateSource(csRef<iSndSysStream> &sndStream, + csRef<iSndSysSource> &sndSource); /** - * Removes a Source. - * @param sndsource iSndSysSource object to remove + * Removes a source from the sound renderer. + * @attention removing the source doesn't remove its stream! Call RemoveStream for + * that. + * @param sndSource iSndSysSource object to remove. */ - void RemoveSource(csRef<iSndSysSource> &sndsource); + void RemoveSource(csRef<iSndSysSource> &sndSource); /** - * Creates a 3d Source on top of a 2d source. - * Doesnt work of your stream type is CS_SND3D_DISABLE - * @param sndsource your iSndSysSource object - * @param sndsource3d your iSndSysSource3D object - * @param mindist distance when volume is at max - * @param maxdist distance when volume is at min - * @param pos 3d position of this source + * Creates a 3D source on top of a 2D source. It doesn't work if your stream type + * is CS_SND3D_DISABLE. To remove the 3D source it is enough to remove the original + * 2D source. + * @param sndSource your 2D iSndSysSource object. + * @param sndSource3D the iSndSysSource3D that will contain the 3D source. + * @param minDist greatest distance at which the sound is played at maximum volume. + * @param maxDist maximum distance at which the sound can be heard. + * @param pos 3D position of this source. */ - void Create3dSource(csRef<iSndSysSource> &sndsource, - csRef<iSndSysSource3D> &sndsource3d, - float mindist, float maxdist, csVector3 pos); + void Create3DSource(csRef<iSndSysSource> &sndSource, + csRef<iSndSysSource3D> &sndSource3D, + float minDist, float maxDist, csVector3 pos); /** - * Creates a directional source on top of a 3d source. - * @param sndsource3d your iSndSysSource3D object - * @param sndsourcedir your iSndSysSource3DDirectionalSimple object - * @param direction direction this source is emitting to - * @param rad radiation of the directional cone + * Creates a directional source on top of a 3D source. To remove the 3D source it + * is enough to remove the original 2D source. + * @param sndSource3D your iSndSysSource3D object. + * @param sndSourceDir the iSndSysSource3DDirectionalSimple object that will contain + * the directional source. + * @param direction direction this source emits to. + * @param rad radiation of the directional cone. */ - void CreateDirectional3dSource(csRef<iSndSysSource3D> &sndsource3d, - csRef<iSndSysSource3DDirectionalSimple> &sndsourcedir, + void CreateDirectional3DSource(csRef<iSndSysSource3D> &sndSource3D, + csRef<iSndSysSource3DDirectionalSimple> &sndSourceDir, csVector3 direction, float rad); /** - * Updates listener position - * @param v viewpoint or for that matter hearpoint - * @param f front - * @param t top + * Updates listener's position. + * @param v viewpoint or for that matter hearpoint. + * @param f front. + * @param t top. */ void UpdateListener(csVector3 v, csVector3 f, csVector3 t); /** - * Gets the current listener position. - * @return a vector containing the listener position. + * Gets the current listener's position. + * @return a vector containing the listener position. If SoundSystem has not been + * initialized correctly it returns a null vector (0, 0, 0). */ - const csVector3& GetListenerPosition() const; + csVector3 GetListenerPosition() const; private: - csRef<iSndSysRenderer> sndrenderer; ///< soundrenderer were using - csRef<iSndSysListener> listener; ///< our listener object + csRef<iSndSysRenderer> sndRenderer; ///< CrystalSpace sound renderer + csRef<iSndSysListener> listener; ///< global listener }; #endif /*_SOUND_SYSTEM_H_*/ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |