Use configured resolution for login/outgame/ingame
[ryzomcore.git] / nel / src / sound / async_file_manager_sound.cpp
blob380a6cebb80d8760b0c65f5bb6cc3d924d0b942e
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 //
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/>.
20 #include "stdsound.h"
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;
30 namespace NLSOUND
33 //CAsyncFileManagerSound *CAsyncFileManagerSound::_Singleton;
34 NLMISC_SAFE_SINGLETON_IMPL(CAsyncFileManagerSound);
37 /*CAsyncFileManagerSound &CAsyncFileManagerSound::getInstance()
39 if (_Singleton == NULL)
41 _Singleton = new CAsyncFileManagerSound();
43 return *_Singleton;
47 void CAsyncFileManagerSound::terminate()
49 if (_Instance != NULL)
51 INelContext::getInstance().releaseSingletonPointer("CAsyncFileManagerSound", _Instance);
52 delete _Instance;
53 _Instance = NULL;
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);
71 if (pLWF != NULL)
73 if (pLWF->_Filename == _Filename)
74 return true;
76 return false;
79 public:
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...
104 return;
106 CAsyncFileManager::getInstance().loadFiles(vFileNames, vPtrs);
109 void CAsyncFileManagerSound::signal (bool *pSgn)
111 if (pSgn == 0)
113 nlwarning("CAsyncFileManagerSound::signal : trying to signal with a null pointer !");
114 return;
116 CAsyncFileManager::getInstance().signal(pSgn);
119 void CAsyncFileManagerSound::cancelSignal (bool *pSgn)
121 if (pSgn == 0)
123 nlwarning("CAsyncFileManagerSound::cancelSignal : trying to remove a signal with a null pointer !");
124 return;
126 CAsyncFileManager::getInstance().cancelSignal(pSgn);
130 // Load task.
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());
147 // nlSleep(500);
148 CAudioMixerUser *mixer = CAudioMixerUser::instance();
149 if (mixer == 0)
151 nlwarning("CAsyncFileManagerSound::CLoadWavFile::run : mixer is not avalable !");
152 return;
155 ISoundDriver *sndDrv = mixer->getSoundDriver();
156 if (sndDrv == 0)
158 nlwarning("CAsyncFileManagerSound::CLoadWavFile::run : sound driver is null !");
159 return;
162 if (_pDestbuffer == 0)
164 nlwarning("CAsyncFileManagerSound::CLoadWavFile::run : dest buffer is null !");
165 return;
168 if (_Filename.empty())
170 nlwarning("CAsyncFileManagerSound::CLoadWavFile::run : filename is empty !");
171 return;
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;
183 buffer.resize(size);
184 ifile.serialBuffer(&buffer[0], size);
186 std::vector<uint8> result;
187 IBuffer::TBufferFormat bufferFormat;
188 uint8 channels;
189 uint8 bitsPerSample;
190 uint32 frequency;
192 if (!IBuffer::readWav(&buffer[0], size, result, bufferFormat, channels, bitsPerSample, frequency))
194 nlwarning("CAsyncFileManagerSound::CLoadWavFile::run : IBuffer::readWav returned false !");
195 return;
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 !");
202 return;
205 catch(...)
207 nlwarning("CAsyncFileManagerSound::CLoadWavFile::run : Exeption detected during IDriver::loadWavFile(%p, %s)", _pDestbuffer, _Filename.c_str());
213 } // NLSOUND