Page principale | Liste des namespaces | Hiérarchie des classes | Liste par ordre alphabétique | Liste des composants | Liste des fichiers | Membres des namespaces | Composants | Déclarations

# ExManagerSound.cpp

Aller à la documentation de ce fichier.

    00001 /*
    00002  * ExNihilo 3D Engine
    00003  *
    00004  *  This program 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 2 of the License, or
    00007  *  (at your option) any later version.
    00008  *
    00009  *  This program 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 Library General Public License for more details.
    00013  *
    00014  *  You should have received a copy of the GNU General Public License
    00015  *  along with this program; if not, write to the Free Software
    00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    00017  *
    00018  * Please read AUTHORS file !!!
    00019  *
    00020  * $Id: ExManagerSound.cpp,v 1.18 2002/12/13 11:45:01 data Exp $
    00021  *
    00022  */
    00023
    00024 #include "ExManagerSound.h"
    00025
    00026 bool ExManagerSound::m_flag = false;
    00027 ExManagerSound* ExManagerSound::m_instance = NULL;
    00028
    00029 ExManagerSound* ExManagerSound::CreateSingleton(void){
    00030 Guard(ExManagerSound* ExManagerSound::CreateSingleton(void))
    00031         if(!m_flag)
    00032         {
    00033                 m_flag = true; // We are creating the error log now, so set flag to true
    00034                 m_instance = new ExManagerSound; // Create the error log
    00035         }else
    00036         {
    00037                 std::cout<<"Error singleton already created"<<std::endl;
    00038         }
    00039         return m_instance;
    00040 UnGuard
    00041 }
    00042
    00043 ExManagerSound::ExManagerSound (void)
    00044 {
    00045   buffer_index = 0;
    00046   source_index = 0;
    00047   source_available = 0;
    00048   m_initok=false;
    00049 }
    00050
    00051 ExManagerSound::~ExManagerSound (void)
    00052 {
    00053         alcDestroyContext (context);
    00054 }
    00055
    00056 void ExManagerSound::Init (void)
    00057 {
    00058 Guard (void ExManagerSound::init (void))
    00059         ExManagerObject<ExCSound>::Init();
    00060         device = alcOpenDevice(NULL);
    00061         if(device == NULL)
    00062         {
    00063                 *Consol <<"ExManagerSound:: alcOpenDevice error "<<std::endl;
    00064                 return ;
    00065         }
    00066         context=alcCreateContext(device,NULL);
    00067         alcMakeContextCurrent(context);
    00068         //Generate buffers
    00069         alGenBuffers(MAX_BUFFER,buffer);
    00070         if ((error = alGetError()) != AL_NO_ERROR)
    00071         {
    00072                 *Consol<<"ExManagerSound:: alGenBuffers error:"<<error<<std::endl;
    00073                 return ;
    00074         }
    00075         //Generate sources
    00076         for(int i = 0; i < MAX_SOURCE ; i++)
    00077         {
    00078                 alGenSources(1 , &source[i]);
    00079                 if ((error = alGetError()) != AL_NO_ERROR)
    00080                 {
    00081                         *Consol<<"ExManagerSound:: alGenSources error:"<<error<<std::endl;
    00082                         return ;
    00083                 }
    00084         else source_available++;
    00085         }
    00086         m_initok=true;
    00087
    00088         LoadSound("DebutKarminaBas.wav");
    00089         LoadSound("MilieuBas.wav");
    00090         LoadSound("FinKarminaBas.wav");
    00091
    00092 UnGuard
    00093 }
    00094
    00095 void ExManagerSound::Reset (void)
    00096 {
    00097 Guard (void ExManagerSound::Reset (void))
    00098         for(m_ItMap=m_Map->begin();m_ItMap!=m_Map->end();m_ItMap++)
    00099         {
    00100                 StopSound(m_ItMap->second.GetName());
    00101         }
    00102         ExManagerObject<ExCSound>::Reset();
    00103         LoadSound("DebutKarminaBas.wav");
    00104         LoadSound("MilieuBas.wav");
    00105         LoadSound("FinKarminaBas.wav");
    00106 UnGuard
    00107 }
    00108
    00109 void ExManagerSound::DisplayAviableInput (void) {
    00110 Guard (void ExManagerSound::DisplayAviableInput (void))
    00111         if(!m_initok)return;
    00112         *Consol<<"****************************************"<<std::endl;
    00113         *Consol<<"* Soundsystem init success             *"<<std::endl;
    00114         *Consol<<"****************************************"<<std::endl;
    00115         *Consol<<"Company        : "<<alGetString (AL_VENDOR)<<std::endl;
    00116         *Consol<<"Version        : "<<alGetString (AL_VERSION)<<std::endl;
    00117         *Consol<<"Renderer       : "<<alGetString (AL_RENDERER)<<std::endl;
    00118         *Consol<<"Extensions     : "<<alGetString (AL_EXTENSIONS)<<std::endl;
    00119         *Consol<<"****************************************"<<std::endl;
    00120 UnGuard
    00121 }
    00122
    00123 bool ExManagerSound::LoadSound (std::string file) {
    00124 Guard (bool ExManagerSound::LoadSound(std::string file))
    00125   //We check if the file already exist
    00126         //For wav file
    00127         ALsizei     size,freq;
    00128         ALenum      format;
    00129         ALvoid      *data;
    00130          bool found = false;
    00131
    00132         for(m_ItMap=m_Map->begin();m_ItMap!=m_Map->end();m_ItMap++)
    00133         {
    00134                 if(m_ItMap->second.GetName()==file)
    00135                 {
    00136                         found = true;
    00137                         break;
    00138                 }
    00139         }
    00140         if(found)
    00141         {
    00142                 *Consol << " ***************************************" << std::endl;
    00143                 *Consol << " ** File " << file.c_str() << " already load" << std::endl;
    00144                 return -1;
    00145         }
    00146
    00147   // Load the wav file
    00148         std::string bufferload("../Data/sound/");
    00149         bufferload+=file;
    00150         alutLoadWAVFile((ALbyte*)bufferload.c_str(),&format,&data,&size,&freq,&loop);
    00151         //alutLoadWAVFile((ALbyte*)file.c_str(),&format,&data,&size,&freq,&loop);
    00152   *Consol << " ***************************************" << std::endl;
    00153   *Consol << " ** Loading " << file.c_str() << std::endl;
    00154   *Consol << " - Format " << format << std::endl;
    00155   *Consol << " - Data " << data << std::endl;
    00156   *Consol << " - Size " << size << std::endl;
    00157   *Consol << " - Freq " << freq << std::endl;
    00158   if(loop) *Consol << " - Loop ON";
    00159   else *Consol << " - Loop OFF";
    00160
    00161         if ((error = alGetError()) != AL_NO_ERROR)
    00162         {
    00163                 *Consol<<"ExManagerSound:: LoadWavFile error:"<<error<<std::endl;
    00164                 return -1;
    00165         }
    00166   // Copy wav file into the buffer
    00167         alBufferData(buffer[buffer_index],format,data,size,freq);
    00168         if ((error = alGetError()) != AL_NO_ERROR)
    00169         {
    00170                 *Consol<<"ExManagerSound:: alBufferData error:"<<error<<std::endl;
    00171                 return -1;
    00172         }
    00173         else
    00174         {
    00175                 ExCSound Sound;
    00176                 Sound.SetName(file.c_str());
    00177                 Sound.SetIdent(buffer_index);
    00178                 Add(Sound);
    00179                 *Consol << " - ident " << buffer_index << std::endl;
    00180         }
    00181
    00182   // Unload the wav file
    00183         alutUnloadWAV(format,data,size,freq);
    00184         if ((error = alGetError()) != AL_NO_ERROR)
    00185         {
    00186                 *Consol<<"ExManagerSound:: alutUnloadWAV error:"<<error<<std::endl;
    00187                 return -1;
    00188         }
    00189
    00190         // Generate source for this wav file
    00191         alSourcei(source[source_index],AL_BUFFER,buffer[buffer_index]);
    00192         if ((error = alGetError()) != AL_NO_ERROR)
    00193         {
    00194                 *Consol<<"ExManagerSound:: alSourcei error:"<<error<<std::endl;
    00195                 return -1;
    00196         }
    00197
    00198         source_index++;
    00199         buffer_index++;
    00200
    00201         *Consol << " ----> " << file.c_str() << " load ok :-)"  << std::endl;
    00202
    00203
    00204
    00205   return true;
    00206
    00207 UnGuard
    00208 }
    00209
    00210 void ExManagerSound::PlaySound (std::string file) {
    00211 Guard (void ExManagerSound::PlaySound (std::string file))
    00212
    00213         for(m_ItMap=m_Map->begin();m_ItMap!=m_Map->end();m_ItMap++)
    00214         {
    00215                 if(m_ItMap->second.GetName()==file)
    00216                 {
    00217                         alSourcePlay(source[m_ItMap->second.GetIdent()]);
    00218                         *Consol<<"Play sound :"<<file<<" ident:"<<m_ItMap->second.GetIdent()<<std::endl;
    00219                         if ((error = alGetError()) != AL_NO_ERROR)
    00220                         {
    00221                                 *Consol<<"ExManagerSound:: alSourcePlay error:"<<error<<std::endl;
    00222                         }
    00223                         return;
    00224                 }
    00225         }
    00226 UnGuard
    00227 }
    00228
    00229 void ExManagerSound::StopSound (std::string file) {
    00230 Guard (void ExManagerSound::StopSound (std::string file))
    00231         for(m_ItMap=m_Map->begin();m_ItMap!=m_Map->end();m_ItMap++)
    00232         {
    00233                 if(m_ItMap->second.GetName()==file)
    00234                 {
    00235                         alSourceStop(source[m_ItMap->second.GetIdent()]);
    00236                         *Consol<<"stop sound :"<<file<<" ident:"<<m_ItMap->second.GetIdent()<<std::endl;
    00237                         if ((error = alGetError()) != AL_NO_ERROR)
    00238                         {
    00239                                 *Consol<<"ExManagerSound:: alSourcePlay error:"<<error<<std::endl;
    00240                         }
    00241                         return;
    00242                 }
    00243         }
    00244 UnGuard
    00245 }
    00246
    00247 void ExManagerSound::PauseSound (std::string file) {
    00248 Guard (void ExManagerSound::PauseSound (std::string file))
    00249                 for(m_ItMap=m_Map->begin();m_ItMap!=m_Map->end();m_ItMap++)
    00250                 {
    00251                         if(m_ItMap->second.GetName()==file)
    00252                         {
    00253                                 alSourceStop(source[m_ItMap->second.GetIdent()]);
    00254                                 *Consol<<"stop sound :"<<file<<" ident:"<<m_ItMap->second.GetIdent()<<std::endl;
    00255                                 if ((error = alGetError()) != AL_NO_ERROR)
    00256                                 {
    00257                                         *Consol<<"ExManagerSound:: alSourcePlay error:"<<error<<std::endl;
    00258                                 }
    00259                                 return;
    00260                         }
    00261                 }
    00262 UnGuard
    00263 }
    00264
    00265 void ExManagerSound::SetRepeatSound (std::string file)
    00266 {
    00267 Guard (void ExManagerSound::SetRepeatSound (std::string file ))
    00268         for(m_ItMap=m_Map->begin();m_ItMap!=m_Map->end();m_ItMap++)
    00269         {
    00270                 if(m_ItMap->second.GetName()==file)
    00271                 {
    00272                         alSourcei(source[m_ItMap->second.GetIdent()],AL_LOOPING,AL_TRUE);
    00273                         *Consol<<"stop sound :"<<file<<" ident:"<<m_ItMap->second.GetIdent()<<std::endl;
    00274                         if ((error = alGetError()) != AL_NO_ERROR)
    00275                         {
    00276                                 *Consol<<"ExManagerSound:: alSourcePlay error:"<<error<<std::endl;
    00277                         }
    00278                         return;
    00279                 }
    00280         }
    00281 UnGuard
    00282 }
    00283 int ExManagerSound::GetSound (std::string file) {
    00284 Guard (ExCSound* ExManagerSound::GetSound (std::string file))
    00285         return -1;
    00286 UnGuard
    00287 }
    00288
    00289 bool ExManagerSound::RemoveSound (std::string file) {
    00290 Guard (bool ExManagerSound::RemoveSound (std::string file))
    00291         return true;
    00292 UnGuard
    00293 }
    00294
    00295 ExCAction ExManagerSound::InputCommand(ExCCommand Command)
    00296 {
    00297 Guard(ExCAction ExManagerSound::InputCommand(ExCCommand Command))
    00298         return NOTHING;
    00299 UnGuard
    00300 }
    00301
    00302 ExCAction ExManagerSound::InputAction(ExCAction Action)
    00303 {
    00304 Guard(ExCAction ExManagerSound::InputAction(ExCAction Action))
    00305
    00306         switch(Action.m_Action)
    00307         {
    00308         case LIST_SOUND:List(); break;
    00309         case REMOVE_SOUND:RemoveSound(Action.m_Param);break;
    00310         case PLAY_SOUND:PlaySound(Action.m_Param);break;
    00311         case STOP_SOUND:StopSound(Action.m_Param);break;
    00312         case PAUSE_SOUND:PauseSound(Action.m_Param);break;
    00313         case ADD_SOUND:LoadSound(Action.m_Param);break;
    00314         case SET_REPEATS_SOUND: SetRepeatSound(Action.m_Param);break;
    00315     case GET_REPEATS_SOUND:     //ManagerSound->GetRepeats(Action.m_Param);break;
    00316
    00317         default:
    00318                 *Consol<<"Can not file action in SOUND_ACTION_MAP"<<std::endl;
    00319                 break;
    00320         }
    00321
    00322         if(Action.m_NextAction){return *Action.m_NextAction;}
    00323         else{return NOTHING;}
    00324 UnGuard
    00325 }
    00326
    00327
    00328