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>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "nel/misc/file.h"
22 #include "nel/misc/path.h"
23 #include "nel/sound/async_file_manager_sound.h"
24 #include "nel/misc/async_file_manager.h"
25 #include "nel/sound/driver/buffer.h"
26 #include "nel/sound/audio_mixer_user.h"
28 using namespace NLMISC
;
33 //CAsyncFileManagerSound *CAsyncFileManagerSound::_Singleton;
34 NLMISC_SAFE_SINGLETON_IMPL(CAsyncFileManagerSound
);
37 /*CAsyncFileManagerSound &CAsyncFileManagerSound::getInstance()
39 if (_Singleton == NULL)
41 _Singleton = new CAsyncFileManagerSound();
47 void CAsyncFileManagerSound::terminate()
49 if (_Instance
!= NULL
)
51 INelContext::getInstance().releaseSingletonPointer("CAsyncFileManagerSound", _Instance
);
57 void CAsyncFileManagerSound::loadWavFile(IBuffer
*pdestBuffer
, const std::string
&filename
)
59 CAsyncFileManager::getInstance().addLoadTask(new CLoadWavFile(pdestBuffer
, filename
));
63 class CCancelLoadWavFile
: public CAsyncFileManager::ICancelCallback
65 std::string _Filename
;
67 bool callback(const NLMISC::IRunnable
*prunnable
) const
69 const CAsyncFileManagerSound::CLoadWavFile
*pLWF
= dynamic_cast<const CAsyncFileManagerSound::CLoadWavFile
*>(prunnable
);
73 if (pLWF
->_Filename
== _Filename
)
80 CCancelLoadWavFile (const std::string
&filename
)
81 : _Filename (filename
)
85 void CAsyncFileManagerSound::cancelLoadWaveFile(const std::string
&/* filename */)
87 nlwarning("CAsyncFileManagerSound::cancelLoadWaveFile : not implemented yet !");
88 // CAsyncFileManager::getInstance().cancelLoadTask(CCancelLoadWavFile(filename));
92 // Do not use these methods with the bigfile manager
93 void CAsyncFileManagerSound::loadFile (const std::string
&fileName
, uint8
**pPtr
)
95 CAsyncFileManager::getInstance().loadFile(fileName
, pPtr
);
98 void CAsyncFileManagerSound::loadFiles (const std::vector
<std::string
> &vFileNames
, const std::vector
<uint8
**> &vPtrs
)
100 if (vFileNames
.size() != vPtrs
.size())
102 nlwarning("CAsyncFileManagerSound::loadFiles : number of filenames and pointer differ ! (%u file, %u ptr)", vFileNames
.size(), vPtrs
.size());
103 // ignore load request...
106 CAsyncFileManager::getInstance().loadFiles(vFileNames
, vPtrs
);
109 void CAsyncFileManagerSound::signal (bool *pSgn
)
113 nlwarning("CAsyncFileManagerSound::signal : trying to signal with a null pointer !");
116 CAsyncFileManager::getInstance().signal(pSgn
);
119 void CAsyncFileManagerSound::cancelSignal (bool *pSgn
)
123 nlwarning("CAsyncFileManagerSound::cancelSignal : trying to remove a signal with a null pointer !");
126 CAsyncFileManager::getInstance().cancelSignal(pSgn
);
131 CAsyncFileManagerSound::CLoadWavFile::CLoadWavFile (IBuffer
*pdestBuffer
, const std::string
&filename
)
132 : _pDestbuffer(pdestBuffer
), _Filename(filename
)
134 if (_Filename
.empty())
136 nlwarning("CAsyncFileManagerSound::CLoadWavFile::CLoadWavFile : file name is empty !");
138 if (_pDestbuffer
== 0)
140 nlwarning("CAsyncFileManagerSound::CLoadWavFile::CLoadWavFile : dest buffer ptr is null!");
144 void CAsyncFileManagerSound::CLoadWavFile::run (void)
146 nldebug("Loading sample %s...", _Filename
.c_str());
148 CAudioMixerUser
*mixer
= CAudioMixerUser::instance();
151 nlwarning("CAsyncFileManagerSound::CLoadWavFile::run : mixer is not avalable !");
155 ISoundDriver
*sndDrv
= mixer
->getSoundDriver();
158 nlwarning("CAsyncFileManagerSound::CLoadWavFile::run : sound driver is null !");
162 if (_pDestbuffer
== 0)
164 nlwarning("CAsyncFileManagerSound::CLoadWavFile::run : dest buffer is null !");
168 if (_Filename
.empty())
170 nlwarning("CAsyncFileManagerSound::CLoadWavFile::run : filename is empty !");
175 static NLMISC::TStringId
empty(CStringMapper::map(""));
176 NLMISC::TStringId nameId
= CStringMapper::map(CFile::getFilenameWithoutExtension(_Filename
));
177 if (nameId
!= empty
) nlassertex(nameId
== _pDestbuffer
->getName(), ("The preset buffer name doesn't match!"));
178 _pDestbuffer
->setName(nameId
);
180 NLMISC::CIFile
ifile(NLMISC::CPath::lookup(_Filename
));
181 uint size
= ifile
.getFileSize();
182 std::vector
<uint8
> buffer
;
184 ifile
.serialBuffer(&buffer
[0], size
);
186 std::vector
<uint8
> result
;
187 IBuffer::TBufferFormat bufferFormat
;
192 if (!IBuffer::readWav(&buffer
[0], size
, result
, bufferFormat
, channels
, bitsPerSample
, frequency
))
194 nlwarning("CAsyncFileManagerSound::CLoadWavFile::run : IBuffer::readWav returned false !");
198 _pDestbuffer
->setFormat(bufferFormat
, channels
, bitsPerSample
, frequency
);
199 if (!_pDestbuffer
->fill(&result
[0], (uint
)result
.size()))
201 nlwarning("CAsyncFileManagerSound::CLoadWavFile::run : _pDestbuffer->fill returned false !");
207 nlwarning("CAsyncFileManagerSound::CLoadWavFile::run : Exeption detected during IDriver::loadWavFile(%p, %s)", _pDestbuffer
, _Filename
.c_str());