From: Markus R. <rol...@us...> - 2005-12-05 21:38:32
|
Update of /cvsroot/simspark/simspark/spark/kerosin/soundserver In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19774/soundserver Added Files: .cvsignore soundeffect.h soundmodule.h soundobject.h soundserver.cpp soundserver.h soundserver_c.cpp soundstream.h soundsystem.h Log Message: --- NEW FILE: .cvsignore --- *.lo .deps .dirstamp .libs --- NEW FILE: soundserver_c.cpp --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: soundserver_c.cpp,v 1.1 2005/12/05 21:38:23 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "soundserver.h" #include "soundeffect.h" #include "soundmodule.h" #include "soundstream.h" using namespace boost; using namespace kerosin; using namespace zeitgeist; using namespace std; FUNCTION(SoundServer,init) { string inSndSysName; return( (in.GetSize() == 1) && (in.GetValue(in.begin(), inSndSysName)) && (obj->Init(inSndSysName)) ); } FUNCTION(SoundServer,getCPU) { return obj->GetCPU(); } FUNCTION(SoundServer,playStream) { string inName; if ( (in.GetSize() != 1) || (! in.GetValue(in.begin(), inName)) ) { return false; } shared_ptr<SoundStream> stream = obj->LoadStream(inName); if (stream.get() == 0) { return false; } stream->Play(); return true; } FUNCTION(SoundServer,playModule) { string inName; if ( (in.GetSize() != 1) || (! in.GetValue(in.begin(), inName)) ) { return false; } shared_ptr<SoundModule> module = obj->LoadModule(inName); if (module.get() == 0) { return false; } module->Play(); return true; } FUNCTION(SoundServer,playEffect) { string inName; if ( (in.GetSize() != 1) || (! in.GetValue(in.begin(), inName)) ) { return false; } shared_ptr<SoundEffect> effect = obj->LoadEffect(inName); if (effect.get() == 0) { return false; } effect->Play(); return true; } void CLASS(SoundServer)::DefineClass() { DEFINE_BASECLASS(zeitgeist/Leaf); DEFINE_FUNCTION(init); DEFINE_FUNCTION(getCPU); DEFINE_FUNCTION(playStream); DEFINE_FUNCTION(playModule); DEFINE_FUNCTION(playEffect); } --- NEW FILE: soundsystem.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: soundsystem.h,v 1.1 2005/12/05 21:38:23 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef KEROSIN_SOUNDSYSTEM_H #define KEROSIN_SOUNDSYSTEM_H #include <zeitgeist/leaf.h> namespace kerosin { class SoundEffect; class SoundStream; class SoundModule; class SoundServer; /* SoundSystem Here we define the interface which an actual SoundSystem will need to implement. Actual SoundSystems would be derived from this interface as ClassServer plugins. You would (in general) associate a SoundSystem with a SoundLibrary, such as FMOD or BASS. NOTE: HISTORY: 18.09.01 - MK - Initial version TODO: - much more functionality (volume control, 3D sounds) TOFIX: */ class SoundSystem : public zeitgeist::Leaf { public: SoundSystem() : zeitgeist::Leaf() {} virtual ~SoundSystem() {} virtual bool Init(int inFreq) = 0; virtual void Shutdown() = 0; virtual float GetCPU() = 0; virtual SoundEffect* CreateEffect(SoundServer &soundServer) = 0; virtual SoundStream* CreateStream(SoundServer &soundServer) = 0; virtual SoundModule* CreateModule(SoundServer &soundServer) = 0; }; } //namespace kerosin #endif //KEROSIN_SOUNDSYSTEM_H --- NEW FILE: soundstream.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: soundstream.h,v 1.1 2005/12/05 21:38:23 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef KEROSIN_SOUNDSTREAM_H #define KEROSIN_SOUNDSTREAM_H #include "soundobject.h" namespace kerosin { class SoundStream : public SoundObject { public: SoundStream(SoundServer &inServer) : SoundObject(inServer) {} virtual ~SoundStream() {} virtual void Load(const std::string& inName) = 0; virtual void Load(void *inBuffer, int inSize) = 0; virtual void Play() = 0; }; } //namespace kerosin #endif //KEROSIN_SOUNDSTREAM_H --- NEW FILE: soundmodule.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: soundmodule.h,v 1.1 2005/12/05 21:38:23 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef KEROSIN_SOUNDMODULE_H #define KEROSIN_SOUNDMODULE_H #include "soundobject.h" namespace kerosin { class SoundModule : public SoundObject { public: SoundModule(SoundServer &inServer) : SoundObject(inServer) {} virtual ~SoundModule() {} virtual void Load(const std::string& inName) = 0; virtual void Load(void *inBuffer, int inSize) = 0; virtual void Play() = 0; }; } //namespace kerosin #endif //KEROSIN_SOUNDMODULE_H --- NEW FILE: soundserver.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: soundserver.h,v 1.1 2005/12/05 21:38:23 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef KEROSIN_SOUNDSERVER_H #define KEROSIN_SOUNDSERVER_H /* $Id: soundserver.h,v 1.1 2005/12/05 21:38:23 rollmark Exp $ SoundServer The SoundServer is the engine object which lets the application satisfy its audio needs. The SoundServer is an access layer to a specific SoundSystem. Another purpose of the SoundServer is managing a cache of SoundObjects. Actually, we use three distinctive caches, one for effects, one for modules and one for streams. NOTE: HISTORY: 18.09.01 - MK - Initial version 20.09.01 - MK - Added caching 11.10.01 - MK - Made singleton functionality more secure TODO: - expose more functionality TOFIX: */ #include <list> #include <zeitgeist/class.h> #include <zeitgeist/leaf.h> namespace kerosin { // forward declarations class SoundSystem; class SoundObject; class SoundEffect; class SoundStream; class SoundModule; class SystemWindow; class SoundServer : public zeitgeist::Leaf { // // Types // public: // sound quality levels enum ESoundQuality { SOUNDQUALITY_BEST = 48000, // above CD quality (slowest) SOUNDQUALITY_GOOD = 44100, // CD quality SOUNDQUALITY_AVERAGE = 22000, // radio quality SOUNDQUALITY_BAD = 11000, // bad quality SOUNDQUALITY_VERYBAD = 8000 // very bad quality (fastest) }; private: #ifdef HAVE_HASH_MAP typedef std::hash_map<std::string, boost::shared_ptr<SoundObject> > TSoundHashMap; #else typedef std::map<std::string, boost::shared_ptr<SoundObject> > TSoundHashMap; #endif // // Methods // public: SoundServer(); virtual ~SoundServer(); bool Init(const std::string &sndSysName); float GetCPU(); boost::shared_ptr<SoundEffect> LoadEffect(const std::string& inName); boost::shared_ptr<SoundStream> LoadStream(const std::string& inName); boost::shared_ptr<SoundModule> LoadModule(const std::string& inName); // // Members // private: //! this function resets the cached sounds in the hashmaps void Reset(); //! a helper function which wraps some common loading code (trivial rejects, etc..) bool LoadSoundObject(const std::string& inName, const TSoundHashMap& map, boost::shared_ptr<SoundObject> &soundObject) const; boost::shared_ptr<SoundSystem> mSoundSystem; TSoundHashMap mEffects; TSoundHashMap mModules; TSoundHashMap mStreams; ESoundQuality mQuality; // the frequency which will be used [default=SOUNDQUALITY_BEST] // make singleton functionality more secure SoundServer(const SoundServer&); SoundServer& operator=(const SoundServer&); }; DECLARE_CLASS(SoundServer); } //namespace kerosin #endif //KEROSIN_SOUNDSERVER_H --- NEW FILE: soundobject.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: soundobject.h,v 1.1 2005/12/05 21:38:23 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef KEROSIN_SOUNDOBJECT_H #define KEROSIN_SOUNDOBJECT_H #include <string> namespace kerosin { class SoundServer; class SoundObject { public: SoundObject(SoundServer &inServer) : mServer(inServer), mFileName("<null>") {} virtual ~SoundObject() {} void SetFileName(const std::string& inName) { mFileName = inName; } const std::string& GetFileName() const { return mFileName; } protected: // this is a reference to the server the sound belongs to SoundServer& mServer; private: // the name of the file this sound came from std::string mFileName; }; } //namespace kerosin #endif //KEROSIN_SOUNDOBJECT_H --- NEW FILE: soundeffect.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: soundeffect.h,v 1.1 2005/12/05 21:38:23 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef KEROSIN_SOUNDEFFECT_H #define KEROSIN_SOUNDEFFECT_H #include "soundobject.h" namespace kerosin { class SoundEffect : public SoundObject { public: SoundEffect(SoundServer &inServer) : SoundObject(inServer) {} virtual ~SoundEffect() {} virtual void Load(const std::string& inName) = 0; virtual void Load(void *inBuffer, int inSize) = 0; virtual void Play() = 0; }; } //namespace kerosin #endif //KEROSIN_SOUNDEFFECT_H --- NEW FILE: soundserver.cpp --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2004 RoboCup Soccer Server 3D Maintenance Group $Id: soundserver.cpp,v 1.1 2005/12/05 21:38:23 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "soundserver.h" #include "soundsystem.h" #include "soundeffect.h" #include "soundstream.h" #include "soundmodule.h" #include <salt/fileclasses.h> #include <zeitgeist/fileserver/fileserver.h> #include <zeitgeist/logserver/logserver.h> #include <zeitgeist/core.h> #include <boost/scoped_ptr.hpp> //------------------------------------------------------------------------------------------------ // SoundServer implementation //------------------------------------------------------------------------------------------------ using namespace boost; using namespace kerosin; using namespace salt; using namespace std; using namespace zeitgeist; // constructor SoundServer::SoundServer() : Leaf(), mQuality(SOUNDQUALITY_BEST) { } SoundServer::~SoundServer() { Reset(); } bool SoundServer::Init(const std::string &sndSysName) { GetLog()->Normal().Printf("SoundServer::Init -> '%s'\n", sndSysName.c_str()); Reset(); // create the soundsystem mSoundSystem = shared_static_cast<SoundSystem>(GetCore()->New(sndSysName)); if(!mSoundSystem) { // could not create SoundSystem GetLog()->Error().Printf("ERROR: Unable to create '%s'\n", sndSysName.c_str()); return false; } // we have a soundsystem, so initialize it if(mSoundSystem->Init(mQuality) == false) { // something happened when we wanted to initialize the soundsystem GetLog()->Error().Printf("ERROR: Could not init '%s'\n", sndSysName.c_str()); return false; } return true; } float SoundServer::GetCPU() { return mSoundSystem->GetCPU(); } boost::shared_ptr<SoundEffect> SoundServer::LoadEffect(const string& inName) { shared_ptr<SoundObject> soundObject; if (LoadSoundObject(inName, mEffects, soundObject) == false) return shared_ptr<SoundEffect>(); if (soundObject) { GetLog()->Debug() << "Found a cached sound" << endl; return shared_static_cast<SoundEffect>(soundObject); } // we don't have the sound in the cache, so create it shared_ptr<SoundEffect> effect(mSoundSystem->CreateEffect(*this)); // now, we want to load the file from our fileserver shared_ptr<FileServer> fileServer = shared_static_cast<FileServer>(GetCore()->Get("/sys/server/file")); shared_ptr<salt::RFile> file = fileServer->Open(inName.c_str()); if(file.get() == NULL) { GetLog()->Error() << "ERROR: Could not open file" << endl; // could not open file for some strange reason return shared_ptr<SoundEffect>(); } shared_ptr<char> buffer(new char[file->Size()]); file->Read(buffer.get(), file->Size()); effect->Load(buffer.get(), file->Size()); effect->SetFileName(inName); // now, we have to add it to the cache mEffects[inName] = effect; return effect; } boost::shared_ptr<SoundStream> SoundServer::LoadStream(const string& inName) { GetLog()->Debug() << "SoundServer::LoadStream " << inName << endl; shared_ptr<SoundObject> soundObject; if (LoadSoundObject(inName, mStreams, soundObject) == false) return shared_ptr<SoundStream>(); if (soundObject) { GetLog()->Debug() << "Found a cached sound" << endl; return shared_static_cast<SoundStream>(soundObject); } // we don't have the sound in the cache, so create it shared_ptr<SoundStream> stream(mSoundSystem->CreateStream(*this)); // now, we want to load the file from our fileserver shared_ptr<FileServer> fileServer = shared_static_cast<FileServer>(GetCore()->Get("/sys/server/file")); shared_ptr<salt::RFile> file = fileServer->Open(inName.c_str()); if(file.get() == NULL) { GetLog()->Error() << "ERROR: Could not open file" << endl; // could not open file for some strange reason return shared_ptr<SoundStream>(); } char* buffer = new char[file->Size()]; file->Read(buffer, file->Size()); stream->Load(buffer, file->Size()); stream->SetFileName(inName); // now, we have to add it to the cache mStreams[inName] = stream; return stream; } boost::shared_ptr<SoundModule> SoundServer::LoadModule(const string& inName) { shared_ptr<SoundObject> soundObject; if (LoadSoundObject(inName, mModules, soundObject) == false) return shared_ptr<SoundModule>(); if (soundObject) { GetLog()->Debug() << "Found a cached sound" << endl; return shared_static_cast<SoundModule>(soundObject); } // we don't have the sound in the cache, so create it shared_ptr<SoundModule> module(mSoundSystem->CreateModule(*this)); // now, we want to load the file from our fileserver shared_ptr<FileServer> fileServer = shared_static_cast<FileServer>(GetCore()->Get("/sys/server/file")); shared_ptr<salt::RFile> file = fileServer->Open(inName.c_str()); if(file.get() == NULL) { GetLog()->Error() << "ERROR: Could not open file" << endl; // could not open file for some strange reason return shared_ptr<SoundModule>(); } shared_ptr<char> buffer(new char[file->Size()]); file->Read(buffer.get(), file->Size()); module->Load(buffer.get(), file->Size()); module->SetFileName(inName); // now, we have to add it to the cache mModules[inName] = module; return module; } void SoundServer::Reset() { mSoundSystem.reset(); TSoundHashMap::const_iterator i; mEffects.clear(); mModules.clear(); mStreams.clear(); } bool SoundServer::LoadSoundObject(const std::string& inName, const TSoundHashMap& map, boost::shared_ptr<SoundObject> &soundObject) const { // if we have no sound system loaded, then we can't load a sound if (mSoundSystem.get() == NULL) { GetLog()->Error() << "ERROR: No SoundSystem loaded!" << endl; soundObject = shared_ptr<SoundObject>(); return false; } // we have a sound system, so let's check if the sound has been loaded already TSoundHashMap::const_iterator i = map.find(inName); if(i != map.end()) { soundObject = (*i).second; } else { soundObject = shared_ptr<SoundObject>(); } return true; } |