Go to the documentation of this file.00001 #include "../header/OpenAL.h"
00002
00003 STARTNAMESPACE
00004
00005 OpenAL::OpenAL() :
00006 defaultPos(0.0f,0.0f,0.0f),
00007 defaultVel(0.0f,0.0f,0.0f),
00008 defaultOri(float3(0.0f,0.0f,-1.0f),float3(0.0f,1.0f,0.0f))
00009 {
00010 m_context = 0;
00011 m_device = 0;
00012 m_listener.position = &defaultPos;
00013 m_listener.velocity = &defaultVel;
00014 m_listener.orientation = &defaultOri;
00015 }
00016
00017 OpenAL::~OpenAL(){
00018 uninit();
00019 }
00020
00021 FILEFORMAT OpenAL::getFormatByExt(const wchar* ext){
00022 if(ext == 0)
00023 return fileUnknown;
00024 const int len = wcslen(ext);
00025
00026 switch(ext[0])
00027 {
00028 case 'w':
00029 case 'W':
00030 if(len >= 3 && (ext[1] == 'a' || ext[1] == 'A') && (ext[2] == 'v' || ext[2] == 'V'))
00031 return fileWAVE;
00032 break;
00033 case 'o':
00034 case 'O':
00035 if(len == 3 && (ext[1] == 'g' || ext[1] == 'G') && (ext[2] == 'g' || ext[2] == 'G'))
00036 return fileOGG;
00037 break;
00038 }
00039 return fileUnknown;
00040 }
00041
00042 bool OpenAL::init(void){
00043
00044
00045 m_soundlist.init(32);
00046 m_musiclist.init(8);
00047
00048 if(m_device == 0)
00049 m_device = alcOpenDevice(0);
00050 if(m_device == 0){
00051 LOGLN("OpenAL::init: OpenAL could not be initialized!");
00052 return false;
00053 }
00054
00055 if(m_context == 0)
00056 m_context = alcCreateContext(m_device, 0);
00057 if(m_context == 0){
00058 alcCloseDevice(m_device);
00059 LOGLN("OpenAL::init: OpenAL context could not be initialized!");
00060 return false;
00061 }
00062
00063 if(alcMakeContextCurrent(m_context) == false){
00064 alcDestroyContext(m_context);
00065 alcCloseDevice(m_device);
00066 LOGLN("OpenAL::init: OpenAL context could not be made current!");
00067 return false;
00068 }
00069
00070 alDistanceModel(AL_LINEAR_DISTANCE);
00071 alSpeedOfSound(343.3f);
00072
00073 return true;
00074 }
00075
00076 void OpenAL::uninit(){
00077 if(m_device != 0){
00078 alcCloseDevice(m_device);
00079 m_device = 0;
00080 }
00081 if(m_context != 0){
00082 alcMakeContextCurrent(0);
00083 alcDestroyContext(m_context);
00084 m_context = 0;
00085 }
00086
00087 m_soundlist.uninit();
00088 m_musiclist.uninit();
00089
00090
00091 }
00092
00093 ::pBuffer OpenAL::createBuffer(const wchar* file, FILEFORMAT format){
00094 SoundBuffer* buffer;
00095
00096
00097
00098
00099 if(format == fileUnknown){
00100 LOGLN("OpenAL::createMusic: File format is not known!");
00101 return 0;
00102 }
00103
00104
00105
00106
00107 switch(format){
00108
00109
00110 case fileWAVE:
00111
00112 buffer = new SoundBufferWAVE;
00113 break;
00114
00115
00116
00117 default:
00118 LOGLN("OpenAL::createMusic: File format is not supported!");
00119 return 0;
00120 }
00121
00122
00123 if(buffer == 0){
00124 LOGLN("OpenAL::createBuffer: Memory could not be allocated!");
00125 return 0;
00126 }
00127
00128
00129 if(buffer->load(file) == false){
00130 LOGLN("OpenAL::createBuffer: File could not be loaded!");
00131 SAVEDELETE(buffer);
00132 return 0;
00133 }
00134
00135 return (::pBuffer)buffer;
00136 }
00137
00138 ::pSound OpenAL::createSound(::pBuffer buffer, bool dynamic,
00139 float3* position,
00140 float3* velocity,
00141 float3* direction,
00142 float distance, float coneAngle,
00143 bool loop, float volume)
00144 {
00145 Sound* source;
00146
00147
00148 if(buffer == 0){
00149 LOGLN("OpenAL::createSound: The buffer parameter is NULL!");
00150 return 0;
00151 }
00152
00153
00154 if((source = new Sound(m_soundlist)) == 0){
00155 LOGLN("OpenAL::createSound: Memory could not be alocated!");
00156 return 0;
00157 }
00158
00159
00160 if(position == 0 && direction == 0 && velocity == 0)
00161 dynamic = false;
00162 position = position ? position : &defaultPos;
00163 velocity = velocity ? velocity : &defaultVel;
00164 direction = direction ? direction : &defaultOri.dir;
00165
00166
00167 if(source->load(buffer, dynamic, position, velocity, direction, distance, coneAngle, loop, volume) == false){
00168 LOGLN("OpenAL::createSound: Memory could not be alocated!");
00169 SAVEDELETE(source);
00170 return 0;
00171 }
00172
00173 return (::pSound)source;
00174 }
00175
00176 ::pMusic OpenAL::createMusic(const wchar* file, bool loop, float volume, FILEFORMAT format){
00177 Music* music;
00178
00179
00180
00181
00182 if(format == fileUnknown){
00183 LOGLN("OpenAL::createMusic: File format is not known!");
00184 return 0;
00185 }
00186
00187
00188
00189
00190 switch(format){
00191
00192
00193 case fileOGG:
00194
00195 music = new SMusicOGG(m_musiclist);
00196 break;
00197
00198
00199
00200 default:
00201 LOGLN("OpenAL::createMusic: File format is not supported!");
00202 return 0;
00203 }
00204
00205
00206 if(music == 0){
00207 LOGLN("OpenAL::createMusic: Memory could not be allocated!");
00208 return 0;
00209 }
00210
00211
00212 if(music->load(file, loop, volume) == false){
00213 LOGLN("OpenAL::createMusic: File could not be loaded!");
00214 SAVEDELETE(music);
00215 return 0;
00216 }
00217
00218
00219 if(alGetError() != AL_NO_ERROR){
00220 LOGLN("OpenAL::createMusik: Musik could not be added to playlist!");
00221 SAVEDELETE(music);
00222 return 0;
00223 }
00224
00225 return (::pMusic)music;
00226 }
00227
00228 void OpenAL::free(::pBuffer& buffer){
00229 SAVEDELETE((SoundBuffer*&)buffer);
00230 }
00231
00232 void OpenAL::free(::pSound& sound){
00233 if(sound != 0){
00234 sound->stop();
00235 m_soundlist.remove((Sound*)sound);
00236 SAVEDELETE((Sound*&)sound);
00237 }
00238 }
00239
00240 void OpenAL::free(::pMusic& music){
00241 if(music != 0){
00242 music->stop();
00243 SAVEDELETE((Music*&)music);
00244 }
00245 }
00246
00247 void OpenAL::listener(float3 position, float3 velocity, float3 direction){
00248 defaultPos = position;
00249 defaultVel = velocity;
00250 defaultOri.dir = direction;
00251 m_listener.position = &defaultPos;
00252 m_listener.velocity = &defaultVel;
00253 m_listener.orientation = &defaultOri;
00254 }
00255
00256 void OpenAL::options(float doppler, float speedofsound){
00257 alDopplerFactor(doppler);
00258 alSpeedOfSound(speedofsound);
00259 }
00260
00261 void OpenAL::update() {
00262 unsigned int i;
00263
00264
00265 alListenerfv(AL_POSITION, (float*)m_listener.position);
00266 alListenerfv(AL_VELOCITY, (float*)m_listener.velocity);
00267 alListenerfv(AL_ORIENTATION, (float*)m_listener.orientation);
00268
00269
00270 for(i=0; i<m_soundlist.size(); i++)
00271
00272 m_soundlist[i]->update(m_listener.position);
00273
00274
00275 for(i=0; i<m_musiclist.size(); i++)
00276
00277 if(m_musiclist[i]->m_play)
00278
00279 m_musiclist[i]->update();
00280 else
00281
00282 m_musiclist.remove(m_musiclist[i]);
00283
00284 }
00285
00286 bool OpenAL::supports(FILEFORMAT fileformat, bool forSound){
00287 if(forSound){
00288 switch(fileformat){
00289 case fileRAW:
00290 case fileAU:
00291 case fileWAVE: return true;
00292 default: break;
00293 }
00294 }else{
00295 switch(fileformat){
00296 case fileWAVE:
00297 case fileOGG: return true;
00298 default: break;
00299 }
00300 }
00301 return false;
00302 }
00303
00304 ENDNAMESPACE