Merge branch 'ryzom/ark-features' into main/gingo-test
[ryzomcore.git] / nel / src / sound / simple_sound.cpp
blobacc292ea0ef1b9a2de171ff97ed02251f687b19b
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/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"
31 using namespace std;
32 using namespace NLMISC;
35 namespace NLSOUND {
39 * Constructor
41 CSimpleSound::CSimpleSound() :
42 _Registered(false),
43 _Buffer(NULL),
44 // _Detailed(false), // not used?
45 _Alpha(1.0),
46 _NeedContext(false)
48 // init with NULL in case of unexecpted access
49 _Filename= NULL;
50 _Buffername= NULL;
55 * Destructor
57 CSimpleSound::~CSimpleSound()
59 if (_Buffer != NULL)
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);
75 _Registered = true;
77 _Buffer = 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()
94 if (_Buffer == 0)
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);
100 _Registered = true;
102 return _Buffer;
107 * Return the length of the sound in ms
109 uint32 CSimpleSound::getDuration()
111 IBuffer* buffer = getBuffer();
113 if ( buffer == NULL )
115 return 0;
117 else
119 return (uint32)(buffer->getDuration());
124 void CSimpleSound::serial(NLMISC::IStream &s)
126 std::string bufferName;
127 CSound::serial(s);
129 s.serial(_MinDist);
130 s.serial(_Alpha);
132 if (s.isReading())
134 s.serial(bufferName);
135 _Buffername = CStringMapper::map(bufferName);
136 setBuffer(NULL);
138 // contain % so it need a context to play
139 if (bufferName.find ("%") != string::npos)
141 _NeedContext = true;
144 else
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;
158 std::string dfnName;
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);
169 // Name
170 _Filename = CStringMapper::map(filename);
172 // Buffername
173 std::string bufferName;
174 root.getValueByName(bufferName, ".SoundType.Filename");
175 bufferName = CFile::getFilenameWithoutExtension(bufferName);
176 _Buffername = CStringMapper::map(bufferName);
178 setBuffer(NULL);
180 // contain % so it need a context to play
181 if (bufferName.find ("%") != string::npos)
183 _NeedContext = true;
186 // MaxDistance
187 root.getValueByName(_MaxDist, ".SoundType.MaxDistance");
189 // MinDistance
190 root.getValueByName(_MinDist, ".SoundType.MinDistance");
192 // Alpha
193 root.getValueByName(_Alpha, ".SoundType.Alpha");
197 } // NLSOUND