# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

background_sound.cpp

Go to the documentation of this file.
00001 
00006 /* Copyright, 2001 Nevrax Ltd.
00007  *
00008  * This file is part of NEVRAX NEL.
00009  * NEVRAX NEL is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2, or (at your option)
00012  * any later version.
00013 
00014  * NEVRAX NEL is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017  * General Public License for more details.
00018 
00019  * You should have received a copy of the GNU General Public License
00020  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00021  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00022  * MA 02111-1307, USA.
00023  */
00024 
00025 #include "stdsound.h"
00026 
00027 #include "background_sound.h"
00028 #include "nel/misc/path.h"
00029 #include <numeric>
00030 /*#include "simple_sound.h"
00031 #include "sound_bank.h"
00032 #include "sample_bank.h"
00033 #include "driver/sound_driver.h"
00034 #include "driver/buffer.h"
00035 */
00036 using namespace std;
00037 using namespace NLMISC;
00038 
00039 
00040 namespace NLSOUND {
00041 
00042 CBackgroundSound::CBackgroundSound()
00043 : _Duration(0), _DurationValid(false)
00044 {
00045 }
00046 
00047 CBackgroundSound::~CBackgroundSound()
00048 {
00049 }
00050 
00051 void CBackgroundSound::serial(NLMISC::IStream &s)
00052 {
00053         CSound::serial(s);
00054 
00055         s.serialCont(_Sounds);
00056 
00057         if (s.isReading())
00058                 _DurationValid = false;
00059 }
00060 
00061 void CBackgroundSound::importForm(const std::string& filename, NLGEORGES::UFormElm& formRoot)
00062 {
00063         NLGEORGES::UFormElm *psoundType;
00064         std::string dfnName;
00065 
00066         // some basic checking.
00067         formRoot.getNodeByName(&psoundType, ".SoundType");
00068         nlassert(psoundType != NULL);
00069         psoundType->getDfnName(dfnName);
00070         nlassert(dfnName == "background_sound.dfn");
00071 
00072         // Call the base class
00073         CSound::importForm(filename, formRoot);
00074 
00075         // Read the array of sound with there respective filter.
00076         {
00077                 _Sounds.clear();
00078 
00079                 NLGEORGES::UFormElm *psoundList;
00080 
00081                 formRoot.getNodeByName(&psoundList, ".SoundType.Sounds");
00082 
00083                 if (psoundList != 0 && psoundList->isArray())
00084                 {
00085                         uint size;
00086                         psoundList->getArraySize(size);
00087 
00088                         for (uint i=0; i<size; ++i)
00089                         {
00090                                 TSoundInfo      sound;
00091                                 NLGEORGES::UFormElm     *psoundItem;
00092 
00093                                 psoundList->getArrayNode(&psoundItem, i);
00094 
00095                                 if (psoundItem != NULL)
00096                                 {
00097                                         // Read the sound name.
00098                                         psoundItem->getValueByName(sound.SoundName, "Sound");
00099                                         sound.SoundName = CFile::getFilenameWithoutExtension(sound.SoundName);
00100 
00101                                         psoundItem->getValueByName(sound.FilterFadeIn, "FilterFadeIn");
00102                                         psoundItem->getValueByName(sound.FilterFadeOut, "FilterFadeOut");
00103 
00104                                         // Read the environnement flag.
00105 
00106                                         for (uint j=0; j<16; j++)
00107                                         {
00108                                                 char tmp[200];
00109                                                 sprintf(tmp, "Filter%2.2u", j);
00110                                                 psoundItem->getValueByName(sound.Filter.Flags[j], tmp);
00111                                         }
00112                                 }
00113 
00114                                 _Sounds.push_back(sound);
00115                         }
00116                 }
00117         }
00118 
00119         _DurationValid = false;
00120 }
00121 
00122 uint32 CBackgroundSound::getDuration()
00123 {
00124         if (_DurationValid)
00125                 return _Duration;
00126 
00127         vector<sint32>  durations;
00128         CAudioMixerUser *mixer = CAudioMixerUser::instance();
00129         std::vector<TSoundInfo>::const_iterator first(_Sounds.begin()), last(_Sounds.end());
00130         for (; first != last; ++first)
00131         {
00132                 CSound *sound = mixer->getSoundId(first->SoundName);
00133                 if (sound != NULL)
00134                         durations.push_back(sound->getDuration());
00135         }
00136         if (durations.empty())
00137                 return 0;
00138         _Duration = *(std::max_element(durations.begin(), durations.end()));
00139         _DurationValid = true;
00140 
00141         return _Duration;
00142 }
00143 
00144 void CBackgroundSound::getSubSoundList(std::vector<std::pair<std::string, CSound*> > &subsounds) const
00145 {
00146         CAudioMixerUser *mixer = CAudioMixerUser::instance();
00147         std::vector<TSoundInfo>::const_iterator first(_Sounds.begin()), last(_Sounds.end());
00148         for (; first != last; ++first)
00149         {
00150                 CSound *sound = mixer->getSoundId(first->SoundName);
00151                 subsounds.push_back(make_pair(first->SoundName, sound));
00152         }
00153 }
00154 
00155 float CBackgroundSound::getMaxDistance() const
00156 {
00157         CAudioMixerUser *mixer = CAudioMixerUser::instance();
00158         float ret = 0.0f;
00159         std::vector<TSoundInfo>::const_iterator first(_Sounds.begin()), last(_Sounds.end());
00160         for (; first != last; ++first)
00161         {
00162                 CSound *sound = mixer->getSoundId(first->SoundName);
00163                 if (sound != 0)
00164                 {
00165                         ret = max(ret, sound->getMaxDistance());
00166                 }
00167         }
00168         if (ret == 0)
00169                 ret  = 1;
00170 
00171         return ret;
00172 }
00173 
00174 
00175 
00176 } // NLSOUND