00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "soundserver.h"
00023 #include "soundsystem.h"
00024 #include "soundeffect.h"
00025 #include "soundstream.h"
00026 #include "soundmodule.h"
00027 #include <salt/fileclasses.h>
00028 #include <zeitgeist/fileserver/fileserver.h>
00029 #include <zeitgeist/logserver/logserver.h>
00030 #include <zeitgeist/core.h>
00031 #include <boost/scoped_ptr.hpp>
00032
00033
00034
00035
00036
00037 using namespace boost;
00038 using namespace kerosin;
00039 using namespace salt;
00040 using namespace std;
00041 using namespace zeitgeist;
00042
00043
00044 SoundServer::SoundServer() : Leaf(), mQuality(SOUNDQUALITY_BEST)
00045 {
00046 }
00047
00048 SoundServer::~SoundServer()
00049 {
00050 Reset();
00051 }
00052
00053 bool SoundServer::Init(const std::string &sndSysName)
00054 {
00055 GetLog()->Normal().Printf("SoundServer::Init -> '%s'\n", sndSysName.c_str());
00056 Reset();
00057
00058
00059 mSoundSystem = shared_static_cast<SoundSystem>(GetCore()->New(sndSysName));
00060
00061 if(!mSoundSystem)
00062 {
00063
00064 GetLog()->Error().Printf("ERROR: Unable to create '%s'\n", sndSysName.c_str());
00065 return false;
00066 }
00067
00068
00069 if(mSoundSystem->Init(mQuality) == false)
00070 {
00071
00072 GetLog()->Error().Printf("ERROR: Could not init '%s'\n", sndSysName.c_str());
00073 return false;
00074 }
00075
00076 return true;
00077 }
00078
00079 float SoundServer::GetCPU()
00080 {
00081 return mSoundSystem->GetCPU();
00082 }
00083
00084 boost::shared_ptr<SoundEffect> SoundServer::LoadEffect(const string& inName)
00085 {
00086 shared_ptr<SoundObject> soundObject;
00087
00088 if (LoadSoundObject(inName, mEffects, soundObject) == false)
00089 return shared_ptr<SoundEffect>();
00090
00091 if (soundObject)
00092 {
00093 GetLog()->Debug() << "Found a cached sound" << endl;
00094 return shared_static_cast<SoundEffect>(soundObject);
00095 }
00096
00097
00098 shared_ptr<SoundEffect> effect(mSoundSystem->CreateEffect(*this));
00099
00100
00101 shared_ptr<FileServer> fileServer = shared_static_cast<FileServer>(GetCore()->Get("/sys/server/file"));
00102 shared_ptr<salt::RFile> file = fileServer->Open(inName.c_str());
00103
00104 if(file.get() == NULL)
00105 {
00106 GetLog()->Error() << "ERROR: Could not open file" << endl;
00107
00108 return shared_ptr<SoundEffect>();
00109 }
00110
00111 shared_ptr<char> buffer(new char[file->Size()]);
00112 file->Read(buffer.get(), file->Size());
00113
00114 effect->Load(buffer.get(), file->Size());
00115 effect->SetFileName(inName);
00116
00117
00118 mEffects[inName] = effect;
00119
00120 return effect;
00121 }
00122
00123 boost::shared_ptr<SoundStream> SoundServer::LoadStream(const string& inName)
00124 {
00125 GetLog()->Debug() << "SoundServer::LoadStream " << inName << endl;
00126 shared_ptr<SoundObject> soundObject;
00127
00128 if (LoadSoundObject(inName, mStreams, soundObject) == false)
00129 return shared_ptr<SoundStream>();
00130
00131 if (soundObject)
00132 {
00133 GetLog()->Debug() << "Found a cached sound" << endl;
00134 return shared_static_cast<SoundStream>(soundObject);
00135 }
00136
00137
00138 shared_ptr<SoundStream> stream(mSoundSystem->CreateStream(*this));
00139
00140
00141 shared_ptr<FileServer> fileServer = shared_static_cast<FileServer>(GetCore()->Get("/sys/server/file"));
00142 shared_ptr<salt::RFile> file = fileServer->Open(inName.c_str());
00143
00144 if(file.get() == NULL)
00145 {
00146 GetLog()->Error() << "ERROR: Could not open file" << endl;
00147
00148 return shared_ptr<SoundStream>();
00149 }
00150
00151 char* buffer = new char[file->Size()];
00152 file->Read(buffer, file->Size());
00153
00154 stream->Load(buffer, file->Size());
00155 stream->SetFileName(inName);
00156
00157
00158 mStreams[inName] = stream;
00159
00160 return stream;
00161 }
00162
00163 boost::shared_ptr<SoundModule> SoundServer::LoadModule(const string& inName)
00164 {
00165 shared_ptr<SoundObject> soundObject;
00166
00167 if (LoadSoundObject(inName, mModules, soundObject) == false)
00168 return shared_ptr<SoundModule>();
00169
00170 if (soundObject)
00171 {
00172 GetLog()->Debug() << "Found a cached sound" << endl;
00173 return shared_static_cast<SoundModule>(soundObject);
00174 }
00175
00176
00177 shared_ptr<SoundModule> module(mSoundSystem->CreateModule(*this));
00178
00179
00180 shared_ptr<FileServer> fileServer = shared_static_cast<FileServer>(GetCore()->Get("/sys/server/file"));
00181 shared_ptr<salt::RFile> file = fileServer->Open(inName.c_str());
00182
00183 if(file.get() == NULL)
00184 {
00185 GetLog()->Error() << "ERROR: Could not open file" << endl;
00186
00187 return shared_ptr<SoundModule>();
00188 }
00189
00190 shared_ptr<char> buffer(new char[file->Size()]);
00191 file->Read(buffer.get(), file->Size());
00192
00193 module->Load(buffer.get(), file->Size());
00194 module->SetFileName(inName);
00195
00196
00197 mModules[inName] = module;
00198
00199 return module;
00200 }
00201
00202 void SoundServer::Reset()
00203 {
00204 mSoundSystem.reset();
00205
00206 TSoundHashMap::const_iterator i;
00207
00208 mEffects.clear();
00209
00210 mModules.clear();
00211
00212 mStreams.clear();
00213 }
00214
00215 bool SoundServer::LoadSoundObject(const std::string& inName, const TSoundHashMap& map, boost::shared_ptr<SoundObject> &soundObject) const
00216 {
00217
00218 if (mSoundSystem.get() == NULL)
00219 {
00220 GetLog()->Error() << "ERROR: No SoundSystem loaded!" << endl;
00221 soundObject = shared_ptr<SoundObject>();
00222 return false;
00223 }
00224
00225
00226 TSoundHashMap::const_iterator i = map.find(inName);
00227
00228 if(i != map.end())
00229 {
00230 soundObject = (*i).second;
00231 }
00232 else
00233 {
00234 soundObject = shared_ptr<SoundObject>();
00235 }
00236
00237 return true;
00238 }