Main Page   Namespace List   Class Hierarchy   Alphabetical List   Data Structures   File List   Namespace Members   Data Fields   Globals  

ExManagerSound.cpp

Go to the documentation of this file.
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.17 2002/12/08 17:57:06 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("idiot.wav");
00089 UnGuard
00090 }       
00091 
00092 void ExManagerSound::Reset (void)
00093 {
00094 Guard (void ExManagerSound::Reset (void))
00095         ExManagerObject<ExCSound>::Reset();
00096 UnGuard
00097 }
00098 
00099 void ExManagerSound::DisplayAviableInput (void) {
00100 Guard (void ExManagerSound::DisplayAviableInput (void))
00101         if(!m_initok)return;
00102         *Consol<<"****************************************"<<std::endl;
00103         *Consol<<"* Soundsystem init success             *"<<std::endl;
00104         *Consol<<"****************************************"<<std::endl;
00105         *Consol<<"Company        : "<<alGetString (AL_VENDOR)<<std::endl;
00106         *Consol<<"Version        : "<<alGetString (AL_VERSION)<<std::endl;
00107         *Consol<<"Renderer       : "<<alGetString (AL_RENDERER)<<std::endl;
00108         *Consol<<"Extensions     : "<<alGetString (AL_EXTENSIONS)<<std::endl;
00109         *Consol<<"****************************************"<<std::endl;
00110 UnGuard
00111 }
00112 
00113 bool ExManagerSound::LoadSound (std::string file) {
00114 Guard (bool ExManagerSound::LoadSound(std::string file))
00115   //We check if the file already exist
00116         //For wav file
00117         ALsizei     size,freq;
00118         ALenum      format;
00119         ALvoid      *data;
00120          bool found = false;
00121 
00122         for(m_ItMap=m_Map->begin();m_ItMap!=m_Map->end();m_ItMap++)
00123         {
00124                 if(m_ItMap->second.GetName()==file)
00125                 {
00126                         found = true;
00127                         break;
00128                 }
00129         }
00130         if(found)
00131         {
00132                 *Consol << " ***************************************" << std::endl;
00133                 *Consol << " ** File " << file.c_str() << " already load" << std::endl;
00134                 return -1;
00135         }
00136   
00137   // Load the wav file
00138         //std::string bufferload("../Data/sound/");
00139         //bufferload+=file;
00140         //alutLoadWAVFile((ALbyte*)bufferload.c_str(),&format,&data,&size,&freq,&loop);
00141         alutLoadWAVFile((ALbyte*)file.c_str(),&format,&data,&size,&freq,&loop);
00142   *Consol << " ***************************************" << std::endl;
00143   *Consol << " ** Loading " << file.c_str() << std::endl;
00144   *Consol << " - Format " << format << std::endl;
00145   *Consol << " - Data " << data << std::endl;
00146   *Consol << " - Size " << size << std::endl;
00147   *Consol << " - Freq " << freq << std::endl;
00148   if(loop) *Consol << " - Loop ON";
00149   else *Consol << " - Loop OFF";
00150 
00151         if ((error = alGetError()) != AL_NO_ERROR)
00152         {
00153                 *Consol<<"ExManagerSound:: LoadWavFile error:"<<error<<std::endl;
00154                 return -1;
00155         }
00156   // Copy wav file into the buffer
00157         alBufferData(buffer[buffer_index],format,data,size,freq);
00158         if ((error = alGetError()) != AL_NO_ERROR)
00159         {
00160                 *Consol<<"ExManagerSound:: alBufferData error:"<<error<<std::endl;
00161                 return -1;
00162         }
00163         else
00164         {
00165                 ExCSound Sound;
00166                 Sound.SetName(file.c_str());
00167                 Sound.SetIdent(buffer_index);
00168                 Add(Sound);
00169                 *Consol << " - ident " << buffer_index << std::endl;
00170         }
00171 
00172   // Unload the wav file
00173         alutUnloadWAV(format,data,size,freq);
00174         if ((error = alGetError()) != AL_NO_ERROR)
00175         {
00176                 *Consol<<"ExManagerSound:: alutUnloadWAV error:"<<error<<std::endl;
00177                 return -1;
00178         }
00179 
00180         // Generate source for this wav file
00181         alSourcei(source[source_index],AL_BUFFER,buffer[buffer_index]);
00182         if ((error = alGetError()) != AL_NO_ERROR)
00183         {
00184                 *Consol<<"ExManagerSound:: alSourcei error:"<<error<<std::endl;
00185                 return -1;
00186         }
00187   
00188         source_index++;
00189         buffer_index++;
00190 
00191         *Consol << " ----> " << file.c_str() << " load ok :-)"  << std::endl;
00192   
00193 
00194 
00195   return true;
00196         
00197 UnGuard
00198 }
00199 
00200 void ExManagerSound::PlaySound (std::string file) {
00201 Guard (void ExManagerSound::PlaySound (std::string file))
00202         for(m_ItMap=m_Map->begin();m_ItMap!=m_Map->end();m_ItMap++)
00203         {
00204                 if(m_ItMap->second.GetName()==file)
00205                 {
00206                         alSourcePlay(source[m_ItMap->second.GetIdent()]);
00207                         *Consol<<"Play sound :"<<file<<" ident:"<<m_ItMap->second.GetIdent()<<std::endl;
00208                         if ((error = alGetError()) != AL_NO_ERROR)
00209                         {
00210                                 *Consol<<"ExManagerSound:: alSourcePlay error:"<<error<<std::endl;
00211                         }
00212                         return;
00213                 }
00214         }
00215 UnGuard
00216 }
00217 
00218 void ExManagerSound::StopSound (std::string file) {
00219 Guard (void ExManagerSound::StopSound (std::string file))
00220 UnGuard
00221 }
00222 
00223 int ExManagerSound::GetSound (std::string file) {
00224 Guard (ExCSound* ExManagerSound::GetSound (std::string file))
00225         return -1;
00226 UnGuard
00227 }
00228 
00229 bool ExManagerSound::RemoveSound (std::string file) {
00230 Guard (bool ExManagerSound::RemoveSound (std::string file))
00231         return true;
00232 UnGuard
00233 }
00234 
00235 ExCAction ExManagerSound::InputCommand(ExCCommand Command)
00236 {
00237 Guard(ExCAction ExManagerSound::InputCommand(ExCCommand Command))
00238         return NOTHING;
00239 UnGuard
00240 }
00241 
00242 ExCAction ExManagerSound::InputAction(ExCAction Action)
00243 {
00244 Guard(ExCAction ExManagerSound::InputAction(ExCAction Action))
00245         switch(Action.m_Action)
00246         {
00247         case LIST_SOUND:List(); break;
00248         case REMOVE_SOUND:RemoveSound(Action.m_Param);break;
00249         case PLAY_SOUND:PlaySound(Action.m_Param);break;
00250         case STOP_SOUND:StopSound(Action.m_Param);break;
00251         case ADD_SOUND:LoadSound(Action.m_Param);break;
00252         case SET_REPEATS_SOUND: //ManagerSound->SetRepeats(Action.m_Param);break;
00253     case GET_REPEATS_SOUND:     //ManagerSound->GetRepeats(Action.m_Param);break;
00254         
00255         default:
00256                 *Consol<<"Can not file action in SOUND_ACTION_MAP"<<std::endl;
00257                 break;
00258         }       
00259 
00260         if(Action.m_NextAction){return *Action.m_NextAction;}
00261         else{return NOTHING;}
00262 UnGuard
00263 }
00264 
00265 
00266 

Generated on Tue Dec 10 18:20:07 2002 for ExNihilo by doxygen1.3-rc1