Change Encyclo button name and macros icon
[ryzomcore.git] / nel / src / sound / background_sound.cpp
blobd8c52bd7047f83cc1770ef1ccd23855fea80fc7c
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2010 Matt RAYKOWSKI (sfb) <matt.raykowski@gmail.com>
6 // Copyright (C) 2019 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "stdsound.h"
23 #include "nel/sound/background_sound.h"
24 #include "nel/sound/audio_mixer_user.h"
25 #include "nel/misc/path.h"
27 using namespace std;
28 using namespace NLMISC;
31 namespace NLSOUND {
33 CBackgroundSound::CBackgroundSound()
34 : _Duration(0), _DurationValid(false)
38 CBackgroundSound::~CBackgroundSound()
42 void CBackgroundSound::serial(NLMISC::IStream &s)
44 CSound::serial(s);
46 s.serialCont(_Sounds);
48 if (s.isReading())
49 _DurationValid = false;
52 void CBackgroundSound::importForm(const std::string& filename, NLGEORGES::UFormElm& formRoot)
54 NLGEORGES::UFormElm *psoundType;
55 std::string dfnName;
57 // some basic checking.
58 formRoot.getNodeByName(&psoundType, ".SoundType");
59 nlassert(psoundType != NULL);
60 psoundType->getDfnName(dfnName);
61 nlassert(dfnName == "background_sound.dfn");
63 // Call the base class
64 CSound::importForm(filename, formRoot);
66 // Read the array of sound with there respective filter.
68 _Sounds.clear();
70 NLGEORGES::UFormElm *psoundList;
72 formRoot.getNodeByName(&psoundList, ".SoundType.Sounds");
74 if (psoundList != 0 && psoundList->isArray())
76 uint size;
77 psoundList->getArraySize(size);
79 for (uint i=0; i<size; ++i)
81 TSoundInfo sound;
82 NLGEORGES::UFormElm *psoundItem;
84 psoundList->getArrayNode(&psoundItem, i);
86 if (psoundItem != NULL)
88 // Read the sound name.
89 std::string soundName;
90 psoundItem->getValueByName(soundName, "Sound");
91 sound.SoundName = CStringMapper::map(CFile::getFilenameWithoutExtension(soundName));
94 // Read the environnement flag.
95 for (uint j=0; j<UAudioMixer::TBackgroundFlags::NB_BACKGROUND_FLAGS; j++)
97 char tmp[200];
98 sprintf(tmp, "Filter%2.2u", j);
99 psoundItem->getValueByName(sound.Filter.Flags[j], tmp);
103 _Sounds.push_back(sound);
108 _DurationValid = false;
111 uint32 CBackgroundSound::getDuration()
113 if (_DurationValid)
114 return _Duration;
116 vector<sint32> durations;
117 CAudioMixerUser *mixer = CAudioMixerUser::instance();
118 std::vector<TSoundInfo>::const_iterator first(_Sounds.begin()), last(_Sounds.end());
119 for (; first != last; ++first)
121 CSound *sound = mixer->getSoundId(first->SoundName);
122 if (sound != NULL)
123 durations.push_back(sound->getDuration());
125 if (durations.empty())
126 return 0;
127 _Duration = *(std::max_element(durations.begin(), durations.end()));
128 _DurationValid = true;
130 return _Duration;
133 void CBackgroundSound::getSubSoundList(std::vector<std::pair<std::string, CSound*> > &subsounds) const
135 CAudioMixerUser *mixer = CAudioMixerUser::instance();
136 std::vector<TSoundInfo>::const_iterator first(_Sounds.begin()), last(_Sounds.end());
137 for (; first != last; ++first)
139 CSound *sound = mixer->getSoundId(first->SoundName);
140 subsounds.push_back(make_pair(CStringMapper::unmap(first->SoundName), sound));
144 float CBackgroundSound::getMaxDistance() const
146 CAudioMixerUser *mixer = CAudioMixerUser::instance();
147 float ret = 0.0f;
148 std::vector<TSoundInfo>::const_iterator first(_Sounds.begin()), last(_Sounds.end());
149 for (; first != last; ++first)
151 CSound *sound = mixer->getSoundId(first->SoundName);
152 if (sound != 0)
154 ret = max(ret, sound->getMaxDistance());
157 if (ret == 0)
158 ret = 1;
160 return ret;
165 } // NLSOUND