Add a head-dampening option
[openal-soft/openal-hmr.git] / OpenAL32 / Include / alMain.h
blobcb290e0cae072eb8eb9d857c9b847cd436902684
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) ((a) == NULL && (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 DEFAULT_HEAD_DAMPEN (0.25f)
169 // Find the next power-of-2 for non-power-of-2 numbers.
170 static __inline ALuint NextPowerOf2(ALuint value)
172 ALuint powerOf2 = 1;
174 if(value)
176 value--;
177 while(value)
179 value >>= 1;
180 powerOf2 <<= 1;
183 return powerOf2;
187 typedef struct {
188 ALCboolean (*OpenPlayback)(ALCdevice*, const ALCchar*);
189 void (*ClosePlayback)(ALCdevice*);
190 ALCboolean (*ResetPlayback)(ALCdevice*);
191 void (*StopPlayback)(ALCdevice*);
193 ALCboolean (*OpenCapture)(ALCdevice*, const ALCchar*);
194 void (*CloseCapture)(ALCdevice*);
195 void (*StartCapture)(ALCdevice*);
196 void (*StopCapture)(ALCdevice*);
197 void (*CaptureSamples)(ALCdevice*, void*, ALCuint);
198 ALCuint (*AvailableSamples)(ALCdevice*);
199 } BackendFuncs;
201 enum {
202 DEVICE_PROBE,
203 ALL_DEVICE_PROBE,
204 CAPTURE_DEVICE_PROBE
207 void alc_alsa_init(BackendFuncs *func_list);
208 void alc_alsa_deinit(void);
209 void alc_alsa_probe(int type);
210 void alc_oss_init(BackendFuncs *func_list);
211 void alc_oss_deinit(void);
212 void alc_oss_probe(int type);
213 void alc_solaris_init(BackendFuncs *func_list);
214 void alc_solaris_deinit(void);
215 void alc_solarise_probe(int type);
216 void alcDSoundInit(BackendFuncs *func_list);
217 void alcDSoundDeinit(void);
218 void alcDSoundProbe(int type);
219 void alcWinMMInit(BackendFuncs *FuncList);
220 void alcWinMMDeinit(void);
221 void alcWinMMProbe(int type);
222 void alc_pa_init(BackendFuncs *func_list);
223 void alc_pa_deinit(void);
224 void alc_pa_probe(int type);
225 void alc_wave_init(BackendFuncs *func_list);
226 void alc_wave_deinit(void);
227 void alc_wave_probe(int type);
228 void alc_pulse_init(BackendFuncs *func_list);
229 void alc_pulse_deinit(void);
230 void alc_pulse_probe(int type);
233 struct ALCdevice_struct
235 ALCboolean Connected;
236 ALboolean IsCaptureDevice;
238 ALuint Frequency;
239 ALuint UpdateSize;
240 ALuint NumUpdates;
241 ALenum Format;
243 ALCchar *szDeviceName;
245 // Maximum number of sources that can be created
246 ALuint MaxNoOfSources;
247 // Maximum number of slots that can be created
248 ALuint AuxiliaryEffectSlotMax;
250 ALint lNumMonoSources;
251 ALint lNumStereoSources;
252 ALuint NumAuxSends;
254 // Linked List of Buffers for this device
255 struct ALbuffer *Buffers;
256 ALuint BufferCount;
258 // Linked List of Effects for this device
259 struct ALeffect *EffectList;
260 ALuint EffectCount;
262 // Linked List of Filters for this device
263 struct ALfilter *FilterList;
264 ALuint FilterCount;
266 // Linked List of Databuffers for this device
267 struct ALdatabuffer *Databuffers;
268 ALuint DatabufferCount;
270 // Stereo-to-binaural filter
271 struct bs2b *Bs2b;
272 ALCint Bs2bLevel;
274 // Simulated dampening from head occlusion
275 ALfloat HeadDampen;
277 // Dry path buffer mix
278 float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
280 Channel DevChannels[OUTPUTCHANNELS];
282 // Contexts created on this device
283 ALCcontext **Contexts;
284 ALuint NumContexts;
286 BackendFuncs *Funcs;
287 void *ExtraData; // For the backend's use
289 ALCdevice *next;
292 #define ALCdevice_OpenPlayback(a,b) ((a)->Funcs->OpenPlayback((a), (b)))
293 #define ALCdevice_ClosePlayback(a) ((a)->Funcs->ClosePlayback((a)))
294 #define ALCdevice_ResetPlayback(a) ((a)->Funcs->ResetPlayback((a)))
295 #define ALCdevice_StopPlayback(a) ((a)->Funcs->StopPlayback((a)))
296 #define ALCdevice_OpenCapture(a,b) ((a)->Funcs->OpenCapture((a), (b)))
297 #define ALCdevice_CloseCapture(a) ((a)->Funcs->CloseCapture((a)))
298 #define ALCdevice_StartCapture(a) ((a)->Funcs->StartCapture((a)))
299 #define ALCdevice_StopCapture(a) ((a)->Funcs->StopCapture((a)))
300 #define ALCdevice_CaptureSamples(a,b,c) ((a)->Funcs->CaptureSamples((a), (b), (c)))
301 #define ALCdevice_AvailableSamples(a) ((a)->Funcs->AvailableSamples((a)))
303 struct ALCcontext_struct
305 ALlistener Listener;
307 struct ALsource *Source;
308 ALuint SourceCount;
310 struct ALeffectslot *AuxiliaryEffectSlot;
311 ALuint AuxiliaryEffectSlotCount;
313 struct ALdatabuffer *SampleSource;
314 struct ALdatabuffer *SampleSink;
316 ALenum LastError;
317 ALboolean InUse;
319 ALenum DistanceModel;
320 ALboolean SourceDistanceModel;
322 ALfloat DopplerFactor;
323 ALfloat DopplerVelocity;
324 ALfloat flSpeedOfSound;
326 ALfloat PanningLUT[OUTPUTCHANNELS * LUT_NUM];
327 ALint NumChan;
329 ALfloat ChannelMatrix[OUTPUTCHANNELS][OUTPUTCHANNELS];
331 ALCdevice *Device;
332 const ALCchar *ExtensionList;
334 ALCcontext *next;
337 extern ALint RTPrioLevel;
339 ALCvoid ReleaseALC(ALCvoid);
341 void AppendDeviceList(const ALCchar *name);
342 void AppendAllDeviceList(const ALCchar *name);
343 void AppendCaptureDeviceList(const ALCchar *name);
345 ALCvoid alcSetError(ALenum errorCode);
347 ALCvoid SuspendContext(ALCcontext *context);
348 ALCvoid ProcessContext(ALCcontext *context);
350 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr);
351 ALuint StopThread(ALvoid *thread);
353 ALCcontext *GetContextSuspended(void);
355 typedef struct RingBuffer RingBuffer;
356 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length);
357 void DestroyRingBuffer(RingBuffer *ring);
358 ALsizei RingBufferSize(RingBuffer *ring);
359 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len);
360 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len);
362 void ReadALConfig(void);
363 void FreeALConfig(void);
364 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def);
365 int GetConfigValueInt(const char *blockName, const char *keyName, int def);
366 float GetConfigValueFloat(const char *blockName, const char *keyName, float def);
367 int GetConfigValueBool(const char *blockName, const char *keyName, float def);
369 void EnableRTPrio(ALint level);
371 void SetDefaultChannelOrder(ALCdevice *device);
372 void SetDefaultWFXChannelOrder(ALCdevice *device);
374 ALCboolean ALCAPIENTRY alcMakeCurrent(ALCcontext *context);
375 ALCcontext* ALCAPIENTRY alcGetThreadContext(void);
377 #ifdef __cplusplus
379 #endif
381 #endif