00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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;
00034 m_instance = new ExManagerSound;
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
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
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
00126
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
00148 std::string bufferload("../Data/sound/");
00149 bufferload+=file;
00150 alutLoadWAVFile((ALbyte*)bufferload.c_str(),&format,&data,&size,&freq,&loop);
00151
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
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
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
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:
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