From: <wel...@us...> - 2009-12-28 07:55:30
|
Revision: 5063 http://planeshift.svn.sourceforge.net/planeshift/?rev=5063&view=rev Author: weltall2 Date: 2009-12-28 07:55:23 +0000 (Mon, 28 Dec 2009) Log Message: ----------- improvements to sound code and added support for loopstart/loopend for ambient and bg music. The code which is needed in cs is not committed yet so the function actually doing it is commented Modified Paths: -------------- trunk/src/client/sound/pssoundmngr.cpp trunk/src/client/sound/pssoundmngr.h Modified: trunk/src/client/sound/pssoundmngr.cpp =================================================================== --- trunk/src/client/sound/pssoundmngr.cpp 2009-12-28 01:06:04 UTC (rev 5062) +++ trunk/src/client/sound/pssoundmngr.cpp 2009-12-28 07:55:23 UTC (rev 5063) @@ -1046,6 +1046,8 @@ int weather = ambientNode->GetAttributeValueAsInt("WEATHER"); csString trigS= ambientNode->GetAttributeValue("TRIGGER"); csString sec = ambientNode->GetAttributeValue("TRIG_SECTOR"); + size_t loopStart = ambientNode->GetAttributeValueAsInt("LOOPSTART"); + size_t loopEnd = ambientNode->GetAttributeValueAsInt("LOOPEND"); int trigger = TriggerStringToInt(trigS); psSectorSoundManager* trigSector = manager; @@ -1075,6 +1077,8 @@ timeOfDay, timeOfDayRange, weather, + loopStart, + loopEnd, true, trigSector, trigger); @@ -1147,6 +1151,8 @@ int timeOfDay = background->GetAttributeValueAsInt("TIME"); int timeOfDayRange = background->GetAttributeValueAsInt("TIME_RANGE"); int weather = background->GetAttributeValueAsInt("WEATHER"); + size_t loopStart = background->GetAttributeValueAsInt("LOOPSTART"); + size_t loopEnd = background->GetAttributeValueAsInt("LOOPEND"); csRef<SOUND_DATA_TYPE> snddata = sndmngr->GetSoundResource( resource ); if (!snddata) @@ -1168,7 +1174,7 @@ fadeDelay, timeOfDay, timeOfDayRange, - weather, + weather, loopStart,loopEnd, sndmngr->LoopBGM()); obj->SetResource( resource ); manager->NewBackground( obj ); @@ -1627,36 +1633,44 @@ void psSectorSoundManager::ChangeTime( int timeOfDay ) { - psSoundObject* bestTimeSong = NULL; - size_t z; + csArray<psSoundObject*> bestTimeSong; if ( music ) { - for ( z = 0; z < songs.GetSize(); z++ ) + for (size_t z = 0; z < songs.GetSize(); z++ ) { if ( songs[z]->MatchTime( timeOfDay ) ) - bestTimeSong = songs[z]; + bestTimeSong.Push(songs[z]); } } - - if ( bestTimeSong ) + if ( bestTimeSong.GetSize() ) { - Debug2( LOG_SOUND, 0, "Song now playing is: %s", bestTimeSong->GetName().GetData() ); - if (!mapsoundsystem->FindSameActiveSong( bestTimeSong )) - { - mainBG->StartFade( FADE_DOWN ); - mapsoundsystem->RegisterActiveSong(bestTimeSong); - bestTimeSong->StartFade( FADE_UP ); - mainBG = bestTimeSong; - } + SetBGSong(bestTimeSong[mapsoundsystem->GetRandomNumber(bestTimeSong.GetSize())]); } } +void psSectorSoundManager::SetBGSong(psSoundObject* song) +{ + Debug2( LOG_SOUND, 0, "Song now playing is: %s", song->GetName().GetData() ); + if (!mapsoundsystem->FindSameActiveSong( song )) + { + mapsoundsystem->RegisterActiveSong(song); + if(mapsoundsystem->sndmngr->LoopBGM()) + song->SetLooping(true); + else + song->SetLooping(false); + + song->StartFade( FADE_UP ); + mainBG = song; + } +} + void psSectorSoundManager::Enter( psSectorSoundManager* leaveFrom, int timeOfDay, int weather, csVector3& position ) { - psSoundObject* bestTimeSong = NULL; - psSoundObject* bestWeatherSong = NULL; + csArray<psSoundObject*> bestTimeSong; + csArray<psSoundObject*> bestWeatherSong; + csArray<psSoundObject*> noReferenceSong; this->weather = weather; size_t z; @@ -1665,50 +1679,40 @@ { for ( z = 0; z < songs.GetSize(); z++ ) { + //search for time restrained songs if ( songs[z]->MatchTime( timeOfDay ) ) { - bestTimeSong = songs[z]; + bestTimeSong.Push(songs[z]); } - + //search for weather restrained songs if ( songs[z]->MatchWeather( weather ) ) { - bestWeatherSong = songs[z]; + bestWeatherSong.Push(songs[z]); } + //search for no restrain songs + if ( songs[z]->HasNoTime() && songs[z]->HasNoWeather()) + { + noReferenceSong.Push(songs[z]); + } } - if ( bestTimeSong ) + if ( bestTimeSong.GetSize() ) { - if (!mapsoundsystem->FindSameActiveSong( bestTimeSong )) - { - mapsoundsystem->RegisterActiveSong(bestTimeSong); - bestTimeSong->StartFade( FADE_UP ); - mainBG = bestTimeSong; - } + SetBGSong(bestTimeSong[mapsoundsystem->GetRandomNumber(bestTimeSong.GetSize())]); } - else if ( bestWeatherSong ) + else if ( bestWeatherSong.GetSize() ) { - if (!mapsoundsystem->FindSameActiveSong( bestWeatherSong )) - { - mapsoundsystem->RegisterActiveSong(bestWeatherSong); - bestWeatherSong->StartFade( FADE_UP ); - mainBG = bestWeatherSong; - } + SetBGSong(bestWeatherSong[mapsoundsystem->GetRandomNumber(bestWeatherSong.GetSize())]); } - else + else if ( noReferenceSong.GetSize() ) { - if ( songs.GetSize() > 0 ) + SetBGSong(noReferenceSong[mapsoundsystem->GetRandomNumber(noReferenceSong.GetSize())]); + } + else //all failed get a random song + { + if ( songs.GetSize() > 0 ) //do we actually have a song? { - if (!mapsoundsystem->FindSameActiveSong( songs[0] )) - { - mapsoundsystem->RegisterActiveSong(songs[0]); - if(mapsoundsystem->sndmngr->LoopBGM()) - songs[0]->SetLooping(true); - else - songs[0]->SetLooping(false); - - songs[0]->StartFade( FADE_UP ); - mainBG = songs[0]; - } + SetBGSong(songs[mapsoundsystem->GetRandomNumber(songs.GetSize())]); } } } @@ -1833,7 +1837,8 @@ psSoundObject::psSoundObject(csRef<iSndSysStream> strm, psMapSoundSystem* mapSys, float maxVol, float minVol, - int fadeTime, int timeOfDay, int timeOfDayRange, int weather, bool looping, + int fadeTime, int timeOfDay, int timeOfDayRange, int weather, size_t loopStart, + size_t loopEnd, bool looping, psSectorSoundManager* sector,int connectWith ) : stream(strm) { @@ -1845,6 +1850,8 @@ this->timeOfDay = timeOfDay; this->timeOfDayRange = timeOfDayRange; this->weatherCondition = weather; + this->loopStart = loopStart; + this->loopEnd = loopEnd; currentVolume = 0.0f; ambientVolume = 1.0f; connectedSector = sector; @@ -1912,7 +1919,7 @@ void psSoundObject::StartSound() { - if ( !stream.Start(mapSystem->sndmngr->soundSystem,loop) ) + if ( !stream.Start(mapSystem->sndmngr->soundSystem,loop,loopStart,loopEnd) ) return; if (threeDee) @@ -2030,7 +2037,7 @@ if(!isPlaying) { isPlaying = true; - stream.Start(mapSystem->sndmngr->soundSystem,loop); + stream.Start(mapSystem->sndmngr->soundSystem,loop,loopStart,loopEnd); stream.SetVolume(currentVolume); } return; @@ -2076,10 +2083,11 @@ rangeConstant = ( (maxVol-minVol)/(rangeToStart-minRange)); } - bool psSoundObject::MatchTime( int time ) { + if(timeOfDay == -1) return false; + int timeEnd = timeOfDay + timeOfDayRange; if ( time >= timeOfDay && time < timeEnd ) @@ -2144,7 +2152,7 @@ } -bool psSndStreamHandle::Start(iSndSysRenderer* renderer, bool loop) +bool psSndStreamHandle::Start(iSndSysRenderer* renderer, bool loop, size_t loopStart, size_t loopEnd) { if (!soundSource) soundSource = renderer->CreateSource(soundStream); @@ -2154,6 +2162,7 @@ return false; soundStream->SetLoopState(loop); + //soundStream->SetLoopBonduaries(loopStart, loopEnd); SetVolume(0.0f); return true; } Modified: trunk/src/client/sound/pssoundmngr.h =================================================================== --- trunk/src/client/sound/pssoundmngr.h 2009-12-28 01:06:04 UTC (rev 5062) +++ trunk/src/client/sound/pssoundmngr.h 2009-12-28 07:55:23 UTC (rev 5063) @@ -353,7 +353,7 @@ public: psSndStreamHandle(csRef<iSndSysStream> s) : soundStream(s) {} - bool Start(iSndSysRenderer* renderer, bool loop); + bool Start(iSndSysRenderer* renderer, bool loop, size_t loopStart, size_t loopEnd); void Stop(iSndSysRenderer* renderer); void SetVolume(float volume); @@ -376,7 +376,8 @@ psSoundObject(csRef<iSndSysStream> soundData, psMapSoundSystem* mapSystem, float maxVol, float minVol, int fadeDelay = 0, - int timeOfDay = 0, int timeOfDayRange = 0, int weatherCondition = 0, bool looping = true, + int timeOfDay = 0, int timeOfDayRange = 0, int weatherCondition = 0, size_t loopStart = 0, + size_t loopEnd = 0, bool looping = true, psSectorSoundManager* sector = NULL, int connectWith = 0); @@ -426,8 +427,12 @@ void StartSound(); void Stop() { stream.SetVolume(0.0f); isPlaying = false; } + bool HasNoTime() { return (timeOfDay == -1); } bool MatchTime( int time ); + bool HasNoWeather() { return (weatherCondition == -1); } bool MatchWeather( int weather ) { return weatherCondition == weather; } + + float Volume() { return currentVolume * ambientVolume;} void SetVolume(float vol); @@ -471,26 +476,29 @@ float currentVolume; float ambientVolume; - csTicks startTime; // Use to track start of fade. + csTicks startTime; ///< Use to track start of fade. bool fadeComplete; Fade_Direction fadeDir; - csTicks fadeDelay; // Time this sound should complete it's fade. + csTicks fadeDelay; ///< Time this sound should complete it's fade. int timeOfDay; int timeOfDayRange; ///< The range from timeOfDay. int weatherCondition; psSectorSoundManager* connectedSector; psMapSoundSystem* mapSystem; - int connectedWith; // What we are connected with (SoundEvent) + int connectedWith; ///< What we are connected with (SoundEvent) - bool threeDee; // Track if this is a 3D sound object + bool threeDee; ///< Track if this is a 3D sound object csVector3 position; float rangeToStart; float minRange; - float rangeConstant; // Used to calculate 3D sound volume. + float rangeConstant; ///< Used to calculate 3D sound volume. - bool loop; + + bool loop; ///< indicates if this soundobject loops + size_t loopStart; ///< indicates the start of the loop sequence + size_t loopEnd; ///< indicates the end of the loop sequence }; //----------------------------------------------------------------------------- @@ -519,7 +527,12 @@ * @param newTime The new time of day. */ void ChangeTime( int newTime ); - + + /** Sets a new Background Song. + * + * @param song: the song to set as background song for this sector + */ + void SetBGSong(psSoundObject* song); void Enter( psSectorSoundManager* enterFrom, int timeOfDay, int weather, csVector3& position ); void StartBackground(); @@ -614,6 +627,7 @@ psSectorSoundManager* GetPendingSoundSector(const char* name); psSectorSoundManager* GetOrCreateSector(const char* name); // Creates a pending sector if it's not found int TriggerStringToInt(const char* str); + size_t GetRandomNumber(size_t max) { randomGen.Get(max); } private: @@ -621,6 +635,7 @@ csArray<psSoundObject *> active_songs; csArray<psSoundObject *> active_ambient; csArray<psSoundObject *> active_emitters; + csRandomGen randomGen; }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |