MPMA Framework 0.4
AL_LockedCalls.h
00001 //internal use: Wrappers around OpenAL calls to avoid multithreading bugs in OpenAL.  As much as it claims to be thread safe, I see intermitant failures returned when more than one thread calls OpenAL at the same time.
00002 //Luke Lenhart, 2008
00003 //See /docs/License.txt for details on how this code may be used.
00004 
00005 #pragma once
00006 
00007 #include "../base/Setup.h"
00008 
00009 #ifdef MPMA_COMPILE_AUDIO
00010 
00011 #include "AL_Include.h"
00012 #include "../base/Locks.h"
00013 #include "../base/DebugRouter.h"
00014 
00015 namespace AUDIO_INTERNAL
00016 {
00017     //used for locking around calls
00018     extern MPMA::SpinLock *lockedCallLock;
00019 }
00020 
00021 //use this one to try it with locks
00022 #define TAKE_AL_CALL_LOCK MPMA::TakeSpinLock autoTakeSpinLockForScope(*AUDIO_INTERNAL::lockedCallLock)
00023 //use this one to try it without locks:
00024 //#define TAKE_AL_CALL_LOCK  
00025 
00026 //only declare them inline in non-debug
00027 //#ifndef _DEBUG
00028     #define CALL_LOCK_INLINE inline
00029 //#else
00030 //    #define CALL_LOCK_INLINE  
00031 //#endif
00032 
00033 //TODO: Make error output optional in some way, to avoid negatively impacting streaming
00034 
00035 //namespace
00036 //{
00037     CALL_LOCK_INLINE bool Call_alSourcePlay(ALuint source)
00038     {
00039         TAKE_AL_CALL_LOCK;
00040         alGetError();
00041         alSourcePlay(source);
00042         nuint err=alGetError();
00043         if (err!=AL_NO_ERROR)
00044         {
00045             MPMA::ErrorReport()<<"alSourcePlay failed with error "<<err<<"\n";
00046             return false;
00047         }
00048         return true;
00049     }
00050 
00051     CALL_LOCK_INLINE bool Call_alSourceStop(ALuint source)
00052     {
00053         TAKE_AL_CALL_LOCK;
00054         alGetError();
00055         alSourceStop(source);
00056         nuint err=alGetError();
00057         if (err!=AL_NO_ERROR)
00058         {
00059             MPMA::ErrorReport()<<"alSourceStop failed with error "<<err<<"\n";
00060             return false;
00061         }
00062         return true;
00063     }
00064 
00065     CALL_LOCK_INLINE bool Call_alGetSourcei(ALuint source, ALenum param, int *state)
00066     {
00067         TAKE_AL_CALL_LOCK;
00068         alGetError();
00069         alGetSourcei(source, param, state);
00070         nuint err=alGetError();
00071         if (err!=AL_NO_ERROR)
00072         {
00073             MPMA::ErrorReport()<<"alGetSourcei failed with error "<<err<<"\n";
00074             return false;
00075         }
00076         return true;
00077     }
00078 
00079     CALL_LOCK_INLINE bool Call_alSourcei(ALuint source, ALenum param, int value)
00080     {
00081         TAKE_AL_CALL_LOCK;
00082         alGetError();
00083         alSourcei(source, param, value);
00084         nuint err=alGetError();
00085         if (err!=AL_NO_ERROR)
00086         {
00087             MPMA::ErrorReport()<<"alSourcei failed with error "<<err<<"\n";
00088             return false;
00089         }
00090         return true;
00091     }
00092 
00093     CALL_LOCK_INLINE bool Call_alSourcef(ALuint source, ALenum param, float value)
00094     {
00095         TAKE_AL_CALL_LOCK;
00096         alGetError();
00097         alSourcef(source, param, value);
00098         nuint err=alGetError();
00099         if (err!=AL_NO_ERROR)
00100         {
00101             MPMA::ErrorReport()<<"alSourcef failed with error "<<err<<"\n";
00102             return false;
00103         }
00104         return true;
00105     }
00106 
00107     CALL_LOCK_INLINE bool Call_alSource3f(ALuint source, ALenum param, float v1, float v2, float v3)
00108     {
00109         TAKE_AL_CALL_LOCK;
00110         alGetError();
00111         alSource3f(source, param, v1, v2, v3);
00112         nuint err=alGetError();
00113         if (err!=AL_NO_ERROR)
00114         {
00115             MPMA::ErrorReport()<<"alSource3f failed with error "<<err<<"\n";
00116             return false;
00117         }
00118         return true;
00119     }
00120 
00121     CALL_LOCK_INLINE bool Call_alSourceQueueBuffers(ALuint source, ALsizei size, ALuint *buffers)
00122     {
00123         TAKE_AL_CALL_LOCK;
00124         alGetError();
00125         alSourceQueueBuffers(source, size, buffers);
00126         nuint err=alGetError();
00127         if (err!=AL_NO_ERROR)
00128         {
00129             MPMA::ErrorReport()<<"alSourceQueueBuffers failed with error "<<err<<"\n";
00130             return false;
00131         }
00132         return true;
00133     }
00134 
00135     CALL_LOCK_INLINE bool Call_alSourceUnqueueBuffers(ALuint source, ALsizei size, ALuint *buffers)
00136     {
00137         TAKE_AL_CALL_LOCK;
00138         alGetError();
00139         alSourceUnqueueBuffers(source, size, buffers);
00140         nuint err=alGetError();
00141         if (err!=AL_NO_ERROR)
00142         {
00143             MPMA::ErrorReport()<<"alSourceUnqueueBuffers failed with error "<<err<<"\n";
00144             return false;
00145         }
00146         return true;
00147     }
00148 
00149     CALL_LOCK_INLINE bool Call_alGenBuffers(ALsizei size, ALuint *buffers)
00150     {
00151         TAKE_AL_CALL_LOCK;
00152         alGetError();
00153         alGenBuffers(size, buffers);
00154         nuint err=alGetError();
00155         if (err!=AL_NO_ERROR)
00156         {
00157             MPMA::ErrorReport()<<"alGenBuffers failed with error "<<err<<"\n";
00158             return false;
00159         }
00160         return true;
00161     }
00162 
00163     CALL_LOCK_INLINE bool Call_alDeleteBuffers(ALsizei size, ALuint *buffers)
00164     {
00165         TAKE_AL_CALL_LOCK;
00166         alGetError();
00167         alDeleteBuffers(size, buffers);
00168         nuint err=alGetError();
00169         if (err!=AL_NO_ERROR)
00170         {
00171             MPMA::ErrorReport()<<"alDeleteBuffers failed with error "<<err<<"\n";
00172             return false;
00173         }
00174         return true;
00175     }
00176 
00177     CALL_LOCK_INLINE bool Call_alBufferData(ALuint buffer, ALenum format, const void *data, ALsizei size, ALsizei freq)
00178     {
00179         TAKE_AL_CALL_LOCK;
00180         alGetError();
00181         alBufferData(buffer, format, data, size, freq);
00182         nuint err=alGetError();
00183         if (err!=AL_NO_ERROR)
00184         {
00185             MPMA::ErrorReport()<<"alBufferData failed with error "<<err<<"\n";
00186             return false;
00187         }
00188         return true;
00189     }
00190 //}
00191 
00192 #endif //#ifdef MPMA_COMPILE_AUDIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends