1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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>
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/>.
23 #include "nel/sound/background_sound.h"
24 #include "nel/sound/audio_mixer_user.h"
25 #include "nel/misc/path.h"
28 using namespace NLMISC
;
33 CBackgroundSound::CBackgroundSound()
34 : _Duration(0), _DurationValid(false)
38 CBackgroundSound::~CBackgroundSound()
42 void CBackgroundSound::serial(NLMISC::IStream
&s
)
46 s
.serialCont(_Sounds
);
49 _DurationValid
= false;
52 void CBackgroundSound::importForm(const std::string
& filename
, NLGEORGES::UFormElm
& formRoot
)
54 NLGEORGES::UFormElm
*psoundType
;
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.
70 NLGEORGES::UFormElm
*psoundList
;
72 formRoot
.getNodeByName(&psoundList
, ".SoundType.Sounds");
74 if (psoundList
!= 0 && psoundList
->isArray())
77 psoundList
->getArraySize(size
);
79 for (uint i
=0; i
<size
; ++i
)
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
++)
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()
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
);
123 durations
.push_back(sound
->getDuration());
125 if (durations
.empty())
127 _Duration
= *(std::max_element(durations
.begin(), durations
.end()));
128 _DurationValid
= true;
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();
148 std::vector
<TSoundInfo
>::const_iterator
first(_Sounds
.begin()), last(_Sounds
.end());
149 for (; first
!= last
; ++first
)
151 CSound
*sound
= mixer
->getSoundId(first
->SoundName
);
154 ret
= max(ret
, sound
->getMaxDistance());