Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

soundserver.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2004 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: soundserver.cpp,v 1.5 2004/04/08 07:28:26 rollmark Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 // SoundServer implementation
00035 //------------------------------------------------------------------------------------------------
00036 
00037 using namespace boost;
00038 using namespace kerosin;
00039 using namespace salt;
00040 using namespace std;
00041 using namespace zeitgeist;
00042 
00043 // constructor
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         // create the soundsystem
00059         mSoundSystem = shared_static_cast<SoundSystem>(GetCore()->New(sndSysName));
00060 
00061         if(!mSoundSystem)
00062         {
00063                 // could not create SoundSystem
00064                 GetLog()->Error().Printf("ERROR: Unable to create '%s'\n", sndSysName.c_str());
00065                 return false;
00066         }
00067 
00068         // we have a soundsystem, so initialize it
00069         if(mSoundSystem->Init(mQuality) == false)
00070         {
00071                 // something happened when we wanted to initialize the soundsystem
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         // we don't have the sound in the cache, so create it
00098         shared_ptr<SoundEffect> effect(mSoundSystem->CreateEffect(*this));
00099 
00100         // now, we want to load the file from our fileserver
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                 // could not open file for some strange reason
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         // now, we have to add it to the cache
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         // we don't have the sound in the cache, so create it
00138         shared_ptr<SoundStream> stream(mSoundSystem->CreateStream(*this));
00139 
00140         // now, we want to load the file from our fileserver
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                 // could not open file for some strange reason
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         // now, we have to add it to the cache
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         // we don't have the sound in the cache, so create it
00177         shared_ptr<SoundModule> module(mSoundSystem->CreateModule(*this));
00178 
00179         // now, we want to load the file from our fileserver
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                 // could not open file for some strange reason
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         // now, we have to add it to the cache
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         // if we have no sound system loaded, then we can't load a sound
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         // we have a sound system, so let's check if the sound has been loaded already
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 }

Generated on Thu Apr 6 15:25:40 2006 for rcssserver3d by  doxygen 1.4.4