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/misc/path.h"
24 #include "nel/sound/simple_sound.h"
25 #include "nel/sound/sound_bank.h"
26 #include "nel/sound/sample_bank_manager.h"
27 #include "nel/sound/sample_bank.h"
28 #include "nel/sound/driver/sound_driver.h"
29 #include "nel/sound/driver/buffer.h"
32 using namespace NLMISC
;
41 CSimpleSound::CSimpleSound() :
44 // _Detailed(false), // not used?
48 // init with NULL in case of unexecpted access
57 CSimpleSound::~CSimpleSound()
60 CAudioMixerUser::getInstance()->getSoundBank()->unregisterBufferAssoc(this, _Buffer
);
63 void CSimpleSound::setBuffer(IBuffer
*buffer
)
65 if (_Buffer
!= NULL
&& buffer
!= NULL
&& _Buffer
->getName() != buffer
->getName())
67 // if buffer name change, update the registration/
68 CAudioMixerUser::getInstance()->getSoundBank()->unregisterBufferAssoc(this, _Buffer
);
69 CAudioMixerUser::getInstance()->getSoundBank()->registerBufferAssoc(this, buffer
);
71 else if (!_Registered
&& buffer
!= NULL
)
73 // creater initial registration.
74 CAudioMixerUser::getInstance()->getSoundBank()->registerBufferAssoc(this, buffer
);
81 void CSimpleSound::getSubSoundList(std::vector
<std::pair
<std::string
, CSound
*> > &subsounds
) const
83 // A little hack, we use the reference vector to tag unavailable sample.
84 if (!(_Buffername
== CStringMapper::emptyId()) && const_cast<CSimpleSound
*>(this)->getBuffer() == 0)
85 subsounds
.push_back(pair
<string
, CSound
*>(CStringMapper::unmap(_Buffername
)+" (sample)", (CSound
*)NULL
));
90 * Return the sample buffer of this sound
92 IBuffer
* CSimpleSound::getBuffer()
96 // try to find the sample buffer in the sample bank.
97 CAudioMixerUser
*audioMixer
= CAudioMixerUser::instance();
98 _Buffer
= audioMixer
->getSampleBankManager()->get(_Buffername
);
99 audioMixer
->getSoundBank()->registerBufferAssoc(this, _Buffer
);
107 * Return the length of the sound in ms
109 uint32
CSimpleSound::getDuration()
111 IBuffer
* buffer
= getBuffer();
113 if ( buffer
== NULL
)
119 return (uint32
)(buffer
->getDuration());
124 void CSimpleSound::serial(NLMISC::IStream
&s
)
126 std::string bufferName
;
134 s
.serial(bufferName
);
135 _Buffername
= CStringMapper::map(bufferName
);
138 // contain % so it need a context to play
139 if (bufferName
.find ("%") != string::npos
)
146 bufferName
= CStringMapper::unmap(_Buffername
);
147 s
.serial(bufferName
);
153 * Load the sound parameters from georges' form
155 void CSimpleSound::importForm(const std::string
& filename
, NLGEORGES::UFormElm
& root
)
157 NLGEORGES::UFormElm
*psoundType
;
160 // some basic checking.
161 root
.getNodeByName(&psoundType
, ".SoundType");
162 nlassert(psoundType
!= NULL
);
163 psoundType
->getDfnName(dfnName
);
164 nlassert(dfnName
== "simple_sound.dfn");
166 // Call the base class
167 CSound::importForm(filename
, root
);
170 _Filename
= CStringMapper::map(filename
);
173 std::string bufferName
;
174 root
.getValueByName(bufferName
, ".SoundType.Filename");
175 bufferName
= CFile::getFilenameWithoutExtension(bufferName
);
176 _Buffername
= CStringMapper::map(bufferName
);
180 // contain % so it need a context to play
181 if (bufferName
.find ("%") != string::npos
)
187 root
.getValueByName(_MaxDist
, ".SoundType.MaxDistance");
190 root
.getValueByName(_MinDist
, ".SoundType.MinDistance");
193 root
.getValueByName(_Alpha
, ".SoundType.Alpha");