MezzanineEngine March 18, 2012

audiomanager.cpp

00001 //© Copyright 2010 - 2011 BlackTopp Studios Inc.
00002 /* This file is part of The Mezzanine Engine.
00003 
00004     The Mezzanine Engine is free software: you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation, either version 3 of the License, or
00007     (at your option) any later version.
00008 
00009     The Mezzanine Engine is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with The Mezzanine Engine.  If not, see <http://www.gnu.org/licenses/>.
00016 */
00017 /* The original authors have included a copy of the license specified above in the
00018    'Docs' folder. See 'gpl.txt'
00019 */
00020 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
00021    Build professional software and charge for their product.
00022 
00023    However there are some practical restrictions, so if your project involves
00024    any of the following you should contact us and we will try to work something
00025    out:
00026     - DRM or Copy Protection of any kind(except Copyrights)
00027     - Software Patents You Do Not Wish to Freely License
00028     - Any Kind of Linking to Non-GPL licensed Works
00029     - Are Currently In Violation of Another Copyright Holder's GPL License
00030     - If You want to change our code and not add a few hundred MB of stuff to
00031         your distribution
00032 
00033    These and other limitations could cause serious legal problems if you ignore
00034    them, so it is best to simply contact us or the Free Software Foundation, if
00035    you have any questions.
00036 
00037    Joseph Toppi - toppij@gmail.com
00038    John Blackwood - makoenergy02@gmail.com
00039 */
00040 #ifndef _audiomanager_cpp
00041 #define _audiomanager_cpp
00042 
00043 #include "audiomanager.h"
00044 #include "audiosound.h"
00045 #include "audiolistener.h"
00046 #include "audiosoundset.h"
00047 #include "audiomusicplayer.h"
00048 #include "world.h"
00049 #include <cAudio.h>
00050 
00051 namespace Mezzanine
00052 {
00053     template<> AudioManager* Singleton<AudioManager>::SingletonPtr = 0;
00054 
00055     AudioManager::AudioManager(bool DefaultSettings)
00056         : AmbientVolume(1.0),
00057           DialogVolume(1.0),
00058           EffectVolume(1.0),
00059           MusicVolume(1.0),
00060           MasterVolume(1.0),
00061           MuteStandby(0.0)
00062     {
00063         cAudioManager = cAudio::createAudioManager(DefaultSettings);
00064         Listener = new Audio::Listener(cAudioManager->getListener());
00065         MusicPlayer = new Audio::MusicPlayer();
00066         this->Priority = 50;
00067     }
00068 
00069     AudioManager::~AudioManager()
00070     {
00071         DestroyAllSounds();
00072         cAudio::destroyAudioManager(cAudioManager);
00073     }
00074 
00075     Audio::Sound* AudioManager::CreateAmbientSound(ConstString& SoundName, ConstString& FileName, ConstString& Group)
00076     {
00077         Audio::Sound* pSound = new Audio::Sound(SoundName,FileName,Group,Audio::ST_Ambient);
00078         Sounds.insert(std::pair<String,Audio::Sound*>(SoundName,pSound));
00079         AmbientSounds.push_back(pSound);
00080         return pSound;
00081     }
00082 
00083     Audio::Sound* AudioManager::CreateDialogSound(ConstString& SoundName, ConstString& FileName, ConstString& Group)
00084     {
00085         Audio::Sound* pSound = new Audio::Sound(SoundName,FileName,Group,Audio::ST_Dialog);
00086         Sounds.insert(std::pair<String,Audio::Sound*>(SoundName,pSound));
00087         DialogSounds.push_back(pSound);
00088         return pSound;
00089     }
00090 
00091     Audio::Sound* AudioManager::CreateEffectSound(ConstString& SoundName, ConstString& FileName, ConstString& Group)
00092     {
00093         Audio::Sound* pSound = new Audio::Sound(SoundName,FileName,Group,Audio::ST_Effect);
00094         Sounds.insert(std::pair<String,Audio::Sound*>(SoundName,pSound));
00095         EffectSounds.push_back(pSound);
00096         return pSound;
00097     }
00098 
00099     Audio::Sound* AudioManager::CreateMusicSound(ConstString& SoundName, ConstString& FileName, ConstString& Group)
00100     {
00101         Audio::Sound* pSound = new Audio::Sound(SoundName,FileName,Group,Audio::ST_Music);
00102         Sounds.insert(std::pair<String,Audio::Sound*>(SoundName,pSound));
00103         MusicSounds.push_back(pSound);
00104         return pSound;
00105     }
00106 
00107     Audio::Sound* AudioManager::GetSoundByName(const String& SoundName) const
00108     {
00109         std::map<String,Audio::Sound*>::const_iterator SoundIt = Sounds.find(SoundName);
00110         if(SoundIt != Sounds.end()) return (*SoundIt).second;
00111         else return NULL;
00112     }
00113 
00114     void AudioManager::DestroySound(Audio::Sound* ToBeDestroyed)
00115     {
00116         for( std::map<String,Audio::Sound*>::iterator It = Sounds.begin() ; It != Sounds.end() ; ++It )
00117         {
00118             if(ToBeDestroyed == (*It).second)
00119             {
00120                 delete (*It).second;
00121                 Sounds.erase(It);
00122                 return;
00123             }
00124         }
00125     }
00126 
00127     void AudioManager::DestroyAllSounds()
00128     {
00129         for( std::map<String,Audio::Sound*>::iterator It = Sounds.begin() ; It != Sounds.end() ; ++It )
00130         {
00131             delete (*It).second;
00132         }
00133         Sounds.clear();
00134         cAudioManager->releaseAllSources();
00135     }
00136 
00137     Audio::SoundSet* AudioManager::CreateSoundSet(const String& SoundSetName)
00138     {
00139         Audio::SoundSet* Set = new Audio::SoundSet();
00140         this->SoundSets[SoundSetName] = Set;
00141         return Set;
00142     }
00143 
00144     Audio::SoundSet* AudioManager::GetSoundSet(const String& SoundSetName) const
00145     {
00146         std::map< String, Audio::SoundSet* >::const_iterator SS = this->SoundSets.find(SoundSetName);
00147         if(SS != SoundSets.end()) return (*SS).second;
00148         else return NULL;
00149     }
00150 
00151     void AudioManager::AddSoundToSoundSet(const String& SoundSetName, Audio::Sound* SoundInst)
00152     {
00153         Audio::SoundSet* Set = this->SoundSets[SoundSetName];
00154         Set->push_back(SoundInst);
00155     }
00156 
00157     void AudioManager::SetAmbientVolume(const Real& Ambient)
00158     {
00159         AmbientVolume = Ambient;
00160         for( std::vector<Audio::Sound*>::iterator it = AmbientSounds.begin() ; it != AmbientSounds.end() ; it++ )
00161         {
00162             (*it)->UpdateVolume();
00163         }
00164     }
00165 
00166     Real AudioManager::GetAmbientVolume() const
00167     {
00168         return AmbientVolume;
00169     }
00170 
00171     void AudioManager::SetDialogVolume(const Real& Dialog)
00172     {
00173         DialogVolume = Dialog;
00174         for( std::vector<Audio::Sound*>::iterator it = DialogSounds.begin() ; it != DialogSounds.end() ; it++ )
00175         {
00176             (*it)->UpdateVolume();
00177         }
00178     }
00179 
00180     Real AudioManager::GetDialogVolume() const
00181     {
00182         return DialogVolume;
00183     }
00184 
00185     void AudioManager::SetEffectVolume(const Real& Effect)
00186     {
00187         EffectVolume = Effect;
00188         for( std::vector<Audio::Sound*>::iterator it = EffectSounds.begin() ; it != EffectSounds.end() ; it++ )
00189         {
00190             (*it)->UpdateVolume();
00191         }
00192     }
00193 
00194     Real AudioManager::GetEffectVolume() const
00195     {
00196         return EffectVolume;
00197     }
00198 
00199     void AudioManager::SetMusicVolume(const Real& Music)
00200     {
00201         MusicVolume = Music;
00202         for( std::vector<Audio::Sound*>::iterator it = MusicSounds.begin() ; it != MusicSounds.end() ; it++ )
00203         {
00204             (*it)->UpdateVolume();
00205         }
00206     }
00207 
00208     Real AudioManager::GetMusicVolume() const
00209     {
00210         return MusicVolume;
00211     }
00212 
00213     void AudioManager::SetMasterVolume(const Real& Master)
00214     {
00215         MasterVolume = Master;
00216         UpdateAllVolumes();
00217     }
00218 
00219     Real AudioManager::GetMasterVolume() const
00220     {
00221         return MasterVolume;
00222     }
00223 
00224     void AudioManager::Mute(bool Enable)
00225     {
00226         if(Enable == Muted)
00227             return;
00228         if(Enable)
00229         {
00230             MuteStandby = MasterVolume;
00231             MasterVolume = 0;
00232         }else{
00233             MasterVolume = MuteStandby;
00234             MuteStandby = 0;
00235         }
00236         Muted = Enable;
00237         UpdateAllVolumes();
00238     }
00239 
00240     bool AudioManager::IsMuted() const
00241     {
00242         return Muted;
00243     }
00244 
00245     void AudioManager::UpdateAllVolumes()
00246     {
00247         for( std::map<String,Audio::Sound*>::iterator it = Sounds.begin() ; it != Sounds.end() ; it++ )
00248         {
00249             (*it).second->UpdateVolume();
00250         }
00251     }
00252 
00253     String AudioManager::GetAvailableDeviceNameByIndex(const Whole& Index) const
00254     {
00255         const char* Name = cAudioManager->getAvailableDeviceName(Index);
00256         String Device(Name);
00257         return Device;
00258     }
00259 
00260     String AudioManager::GetDefaultDeviceName() const
00261     {
00262         const char* Name = cAudioManager->getDefaultDeviceName();
00263         String Device(Name);
00264         return Device;
00265     }
00266 
00267     Whole AudioManager::GetAvailableDeviceCount() const
00268     {
00269         return cAudioManager->getAvailableDeviceCount();
00270     }
00271 
00272     std::stringstream* AudioManager::GetLogs() const
00273     {
00274         return 0; //cAudioManager->GetLogs();
00275     }
00276 
00277     void AudioManager::ClearLogs()
00278     {
00279         //cAudioManager->ClearLogs();
00280     }
00281 
00282     void AudioManager::InitializeManager(ConstString &DeviceName, int OutputFrequency, int EAXEffectSlots)
00283     {
00284         cAudioManager->initialize(DeviceName.c_str(), OutputFrequency, EAXEffectSlots);
00285     }
00286 
00287     Audio::Listener* AudioManager::GetListener() const
00288     {
00289         return Listener;
00290     }
00291 
00292     Audio::MusicPlayer* AudioManager::GetMusicPlayer() const
00293     {
00294         return MusicPlayer;
00295     }
00296 
00297     cAudio::IAudioManager* AudioManager::GetcAudioManager() const
00298     {
00299         return cAudioManager;
00300     }
00301 
00302     void AudioManager::Initialize()
00303         {}
00304 
00305     void AudioManager::DoMainLoopItems()
00306         { MusicPlayer->Update(); }
00307 
00308     ManagerBase::ManagerTypeName AudioManager::GetType() const
00309         { return ManagerBase::AudioManager; }
00310 }//Mezzanine
00311 
00312 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines