Use a realtime clock for measuring time
[openal-soft.git] / OpenAL32 / Include / alMain.h
blobbc6c6d8b7bb8fed0e4278a56e06dc3ff6d62fe92
1 #ifndef AL_MAIN_H
2 #define AL_MAIN_H
4 #include <string.h>
5 #include <stdio.h>
6 #include <stdarg.h>
8 #ifdef HAVE_FENV_H
9 #include <fenv.h>
10 #endif
12 #include "AL/al.h"
13 #include "AL/alc.h"
14 #include "AL/alext.h"
16 #ifdef _WIN32
18 #ifndef _WIN32_WINNT
19 #define _WIN32_WINNT 0x0500
20 #endif
21 #include <windows.h>
23 typedef DWORD tls_type;
24 #define tls_create(x) (*(x) = TlsAlloc())
25 #define tls_delete(x) TlsFree((x))
26 #define tls_get(x) TlsGetValue((x))
27 #define tls_set(x, a) TlsSetValue((x), (a))
29 #else
31 #include <unistd.h>
32 #include <assert.h>
33 #include <pthread.h>
34 #ifdef HAVE_PTHREAD_NP_H
35 #include <pthread_np.h>
36 #endif
37 #include <sys/time.h>
38 #include <time.h>
39 #include <errno.h>
41 #define IsBadWritePtr(a,b) (0)
43 typedef pthread_key_t tls_type;
44 #define tls_create(x) pthread_key_create((x), NULL)
45 #define tls_delete(x) pthread_key_delete((x))
46 #define tls_get(x) pthread_getspecific((x))
47 #define tls_set(x, a) pthread_setspecific((x), (a))
49 typedef pthread_mutex_t CRITICAL_SECTION;
50 static inline void EnterCriticalSection(CRITICAL_SECTION *cs)
52 int ret;
53 ret = pthread_mutex_lock(cs);
54 assert(ret == 0);
56 static inline void LeaveCriticalSection(CRITICAL_SECTION *cs)
58 int ret;
59 ret = pthread_mutex_unlock(cs);
60 assert(ret == 0);
62 static inline void InitializeCriticalSection(CRITICAL_SECTION *cs)
64 pthread_mutexattr_t attrib;
65 int ret;
67 ret = pthread_mutexattr_init(&attrib);
68 assert(ret == 0);
70 ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
71 #ifdef HAVE_PTHREAD_NP_H
72 if(ret != 0)
73 ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
74 #endif
75 assert(ret == 0);
76 ret = pthread_mutex_init(cs, &attrib);
77 assert(ret == 0);
79 pthread_mutexattr_destroy(&attrib);
82 static inline void DeleteCriticalSection(CRITICAL_SECTION *cs)
84 int ret;
85 ret = pthread_mutex_destroy(cs);
86 assert(ret == 0);
89 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
90 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
91 * Additionally, Win32 is supposed to measure the time since Windows started,
92 * as opposed to the actual time. */
93 static inline ALuint timeGetTime(void)
95 int ret;
96 #ifdef _POSIX_TIMERS
97 struct timespec ts;
99 ret = clock_gettime(CLOCK_REALTIME, &ts);
100 assert(ret == 0);
102 return ts.tv_nsec/1000000 + ts.tv_sec*1000;
103 #else
104 struct timeval tv;
106 ret = gettimeofday(&tv, NULL);
107 assert(ret == 0);
109 return tv.tv_usec/1000 + tv.tv_sec*1000;
110 #endif
113 static inline void Sleep(ALuint t)
115 struct timespec tv, rem;
116 tv.tv_nsec = (t*1000000)%1000000000;
117 tv.tv_sec = t/1000;
119 while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
120 tv = rem;
122 #define min(x,y) (((x)<(y))?(x):(y))
123 #define max(x,y) (((x)>(y))?(x):(y))
124 #endif
126 #include "alListener.h"
127 #include "alu.h"
129 #ifdef __cplusplus
130 extern "C" {
131 #endif
133 static __inline void al_print(const char *fname, unsigned int line, const char *fmt, ...)
135 const char *fn;
136 char str[256];
137 int i;
139 fn = strrchr(fname, '/');
140 if(!fn) fn = strrchr(fname, '\\');;
141 if(!fn) fn = fname;
142 else fn += 1;
144 i = snprintf(str, sizeof(str), "AL lib: %s:%d: ", fn, line);
145 if(i < (int)sizeof(str) && i > 0)
147 va_list ap;
148 va_start(ap, fmt);
149 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
150 va_end(ap);
152 str[sizeof(str)-1] = 0;
154 fprintf(stderr, "%s", str);
156 #define AL_PRINT(...) al_print(__FILE__, __LINE__, __VA_ARGS__)
159 #define SWMIXER_OUTPUT_RATE 44100
161 #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
162 #define AIRABSORBGAINDBHF (-0.05f)
164 #define LOWPASSFREQCUTOFF (5000)
166 #define QUADRANT_NUM 128
167 #define LUT_NUM (4 * QUADRANT_NUM)
170 typedef struct {
171 ALCboolean (*OpenPlayback)(ALCdevice*, const ALCchar*);
172 void (*ClosePlayback)(ALCdevice*);
173 ALCboolean (*ResetPlayback)(ALCdevice*);
174 void (*StopPlayback)(ALCdevice*);
176 ALCboolean (*OpenCapture)(ALCdevice*, const ALCchar*);
177 void (*CloseCapture)(ALCdevice*);
178 void (*StartCapture)(ALCdevice*);
179 void (*StopCapture)(ALCdevice*);
180 void (*CaptureSamples)(ALCdevice*, void*, ALCuint);
181 ALCuint (*AvailableSamples)(ALCdevice*);
182 } BackendFuncs;
184 enum {
185 DEVICE_PROBE,
186 ALL_DEVICE_PROBE,
187 CAPTURE_DEVICE_PROBE
190 void alc_alsa_init(BackendFuncs *func_list);
191 void alc_alsa_deinit(void);
192 void alc_alsa_probe(int type);
193 void alc_oss_init(BackendFuncs *func_list);
194 void alc_oss_deinit(void);
195 void alc_oss_probe(int type);
196 void alc_solaris_init(BackendFuncs *func_list);
197 void alc_solaris_deinit(void);
198 void alc_solarise_probe(int type);
199 void alcDSoundInit(BackendFuncs *func_list);
200 void alcDSoundDeinit(void);
201 void alcDSoundProbe(int type);
202 void alcWinMMInit(BackendFuncs *FuncList);
203 void alcWinMMDeinit(void);
204 void alcWinMMProbe(int type);
205 void alc_pa_init(BackendFuncs *func_list);
206 void alc_pa_deinit(void);
207 void alc_pa_probe(int type);
208 void alc_wave_init(BackendFuncs *func_list);
209 void alc_wave_deinit(void);
210 void alc_wave_probe(int type);
211 void alc_pulse_init(BackendFuncs *func_list);
212 void alc_pulse_deinit(void);
213 void alc_pulse_probe(int type);
216 struct ALCdevice_struct
218 ALCboolean Connected;
219 ALboolean IsCaptureDevice;
221 ALuint Frequency;
222 ALuint UpdateSize;
223 ALuint NumUpdates;
224 ALenum Format;
226 ALCchar *szDeviceName;
228 // Maximum number of sources that can be created
229 ALuint MaxNoOfSources;
230 // Maximum number of slots that can be created
231 ALuint AuxiliaryEffectSlotMax;
233 ALint lNumMonoSources;
234 ALint lNumStereoSources;
235 ALuint NumAuxSends;
237 // Linked List of Buffers for this device
238 struct ALbuffer *Buffers;
239 ALuint BufferCount;
241 // Linked List of Effects for this device
242 struct ALeffect *EffectList;
243 ALuint EffectCount;
245 // Linked List of Filters for this device
246 struct ALfilter *FilterList;
247 ALuint FilterCount;
249 // Linked List of Databuffers for this device
250 struct ALdatabuffer *Databuffers;
251 ALuint DatabufferCount;
253 // Stereo-to-binaural filter
254 struct bs2b *Bs2b;
255 ALCint Bs2bLevel;
257 // Dry path buffer mix
258 float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
260 // Contexts created on this device
261 ALCcontext **Contexts;
262 ALuint NumContexts;
264 BackendFuncs *Funcs;
265 void *ExtraData; // For the backend's use
267 ALCdevice *next;
270 #define ALCdevice_OpenPlayback(a,b) ((a)->Funcs->OpenPlayback((a), (b)))
271 #define ALCdevice_ClosePlayback(a) ((a)->Funcs->ClosePlayback((a)))
272 #define ALCdevice_ResetPlayback(a) ((a)->Funcs->ResetPlayback((a)))
273 #define ALCdevice_StopPlayback(a) ((a)->Funcs->StopPlayback((a)))
274 #define ALCdevice_OpenCapture(a,b) ((a)->Funcs->OpenCapture((a), (b)))
275 #define ALCdevice_CloseCapture(a) ((a)->Funcs->CloseCapture((a)))
276 #define ALCdevice_StartCapture(a) ((a)->Funcs->StartCapture((a)))
277 #define ALCdevice_StopCapture(a) ((a)->Funcs->StopCapture((a)))
278 #define ALCdevice_CaptureSamples(a,b,c) ((a)->Funcs->CaptureSamples((a), (b), (c)))
279 #define ALCdevice_AvailableSamples(a) ((a)->Funcs->AvailableSamples((a)))
281 struct ALCcontext_struct
283 ALlistener Listener;
285 struct ALsource *Source;
286 ALuint SourceCount;
288 struct ALeffectslot *AuxiliaryEffectSlot;
289 ALuint AuxiliaryEffectSlotCount;
291 struct ALdatabuffer *SampleSource;
292 struct ALdatabuffer *SampleSink;
294 ALenum LastError;
295 ALboolean InUse;
297 ALenum DistanceModel;
299 ALfloat DopplerFactor;
300 ALfloat DopplerVelocity;
301 ALfloat flSpeedOfSound;
303 ALfloat PanningLUT[OUTPUTCHANNELS * LUT_NUM];
304 ALint NumChan;
306 ALfloat ChannelMatrix[OUTPUTCHANNELS][OUTPUTCHANNELS];
308 ALCdevice *Device;
309 const ALCchar *ExtensionList;
311 ALCcontext *next;
314 ALCvoid ReleaseALC(ALCvoid);
316 void AppendDeviceList(const ALCchar *name);
317 void AppendAllDeviceList(const ALCchar *name);
318 void AppendCaptureDeviceList(const ALCchar *name);
320 ALCvoid alcSetError(ALenum errorCode);
322 ALCvoid SuspendContext(ALCcontext *context);
323 ALCvoid ProcessContext(ALCcontext *context);
325 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr);
326 ALuint StopThread(ALvoid *thread);
328 ALCcontext *GetContextSuspended(void);
330 typedef struct RingBuffer RingBuffer;
331 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length);
332 void DestroyRingBuffer(RingBuffer *ring);
333 ALsizei RingBufferSize(RingBuffer *ring);
334 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len);
335 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len);
337 void ReadALConfig(void);
338 void FreeALConfig(void);
339 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def);
340 int GetConfigValueInt(const char *blockName, const char *keyName, int def);
341 float GetConfigValueFloat(const char *blockName, const char *keyName, float def);
342 int GetConfigValueBool(const char *blockName, const char *keyName, float def);
344 ALCboolean ALCAPIENTRY alcMakeCurrent(ALCcontext *context);
345 ALCcontext* ALCAPIENTRY alcGetThreadContext(void);
347 #ifdef __cplusplus
349 #endif
351 #endif