Don't needlessly expose a variable for the backends
[openal-soft/android/lowlatency.git] / OpenAL32 / Include / alMain.h
blobc1f06d65e6a1e4e5253a27071ddc28aa09827175
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 #ifndef AL_EXT_buffer_sub_data
17 #define AL_EXT_buffer_sub_data 1
18 #define AL_BYTE_RW_OFFSETS_EXT 0x1031
19 #define AL_SAMPLE_RW_OFFSETS_EXT 0x1032
20 typedef ALvoid (AL_APIENTRY*PFNALBUFFERSUBDATAEXTPROC)(ALuint,ALenum,const ALvoid*,ALsizei,ALsizei);
21 #ifdef AL_ALEXT_PROTOTYPES
22 AL_API ALvoid AL_APIENTRY alBufferSubDataEXT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length);
23 #endif
24 #endif
26 #ifndef AL_EXT_sample_buffer_object
27 #define AL_EXT_sample_buffer_object 1
28 typedef ptrdiff_t ALintptrEXT;
29 typedef ptrdiff_t ALsizeiptrEXT;
30 #define AL_SAMPLE_SOURCE_EXT 0x1040
31 #define AL_SAMPLE_SINK_EXT 0x1041
32 #define AL_READ_ONLY_EXT 0x1042
33 #define AL_WRITE_ONLY_EXT 0x1043
34 #define AL_READ_WRITE_EXT 0x1044
35 #define AL_STREAM_WRITE_EXT 0x1045
36 #define AL_STREAM_READ_EXT 0x1046
37 #define AL_STREAM_COPY_EXT 0x1047
38 #define AL_STATIC_WRITE_EXT 0x1048
39 #define AL_STATIC_READ_EXT 0x1049
40 #define AL_STATIC_COPY_EXT 0x104A
41 #define AL_DYNAMIC_WRITE_EXT 0x104B
42 #define AL_DYNAMIC_READ_EXT 0x104C
43 #define AL_DYNAMIC_COPY_EXT 0x104D
44 typedef ALvoid (AL_APIENTRY*PFNALGENDATABUFFERSEXTPROC)(ALsizei n,ALuint *puiBuffers);
45 typedef ALvoid (AL_APIENTRY*PFNALDELETEDATABUFFERSEXTPROC)(ALsizei n, const ALuint *puiBuffers);
46 typedef ALboolean (AL_APIENTRY*PFNALISDATABUFFEREXTPROC)(ALuint uiBuffer);
47 typedef ALvoid (AL_APIENTRY*PFNALDATABUFFERDATAEXTPROC)(ALuint buffer,const ALvoid *data,ALsizeiptrEXT size,ALenum usage);
48 typedef ALvoid (AL_APIENTRY*PFNALDATABUFFERSUBDATAEXTPROC)(ALuint buffer, ALintptrEXT start, ALsizeiptrEXT length, const ALvoid *);
49 typedef ALvoid (AL_APIENTRY*PFNALGETDATABUFFERSUBDATAEXTPROC)(ALuint buffer, ALintptrEXT start, ALsizeiptrEXT length, ALvoid *);
50 typedef ALvoid (AL_APIENTRY*PFNALDATABUFFERFEXTPROC)(ALuint buffer, ALenum eParam, ALfloat flValue);
51 typedef ALvoid (AL_APIENTRY*PFNALDATABUFFERFVEXTPROC)(ALuint buffer, ALenum eParam, const ALfloat* flValues);
52 typedef ALvoid (AL_APIENTRY*PFNALDATABUFFERIEXTPROC)(ALuint buffer, ALenum eParam, ALint lValue);
53 typedef ALvoid (AL_APIENTRY*PFNALDATABUFFERIVEXTPROC)(ALuint buffer, ALenum eParam, const ALint* plValues);
54 typedef ALvoid (AL_APIENTRY*PFNALGETDATABUFFERFEXTPROC)(ALuint buffer, ALenum eParam, ALfloat *pflValue);
55 typedef ALvoid (AL_APIENTRY*PFNALGETDATABUFFERFVEXTPROC)(ALuint buffer, ALenum eParam, ALfloat* pflValues);
56 typedef ALvoid (AL_APIENTRY*PFNALGETDATABUFFERIEXTPROC)(ALuint buffer, ALenum eParam, ALint *plValue);
57 typedef ALvoid (AL_APIENTRY*PFNALGETDATABUFFERIVEXTPROC)(ALuint buffer, ALenum eParam, ALint* plValues);
58 typedef ALvoid (AL_APIENTRY*PFNALSELECTDATABUFFEREXTPROC)(ALenum target, ALuint uiBuffer);
59 typedef ALvoid* (AL_APIENTRY*PFNALMAPDATABUFFEREXTPROC)(ALuint uiBuffer, ALintptrEXT start, ALsizeiptrEXT length, ALenum access);
60 typedef ALvoid (AL_APIENTRY*PFNALUNMAPDATABUFFEREXTPROC)(ALuint uiBuffer);
61 #ifdef AL_ALEXT_PROTOTYPES
62 AL_API ALvoid AL_APIENTRY alGenDatabuffersEXT(ALsizei n,ALuint *puiBuffers);
63 AL_API ALvoid AL_APIENTRY alDeleteDatabuffersEXT(ALsizei n, const ALuint *puiBuffers);
64 AL_API ALboolean AL_APIENTRY alIsDatabufferEXT(ALuint uiBuffer);
65 AL_API ALvoid AL_APIENTRY alDatabufferDataEXT(ALuint buffer,const ALvoid *data,ALsizeiptrEXT size,ALenum usage);
66 AL_API ALvoid AL_APIENTRY alDatabufferSubDataEXT(ALuint buffer, ALintptrEXT start, ALsizeiptrEXT length, const ALvoid *data);
67 AL_API ALvoid AL_APIENTRY alGetDatabufferSubDataEXT(ALuint buffer, ALintptrEXT start, ALsizeiptrEXT length, ALvoid *data);
68 AL_API ALvoid AL_APIENTRY alDatabufferfEXT(ALuint buffer, ALenum eParam, ALfloat flValue);
69 AL_API ALvoid AL_APIENTRY alDatabufferfvEXT(ALuint buffer, ALenum eParam, const ALfloat* flValues);
70 AL_API ALvoid AL_APIENTRY alDatabufferiEXT(ALuint buffer, ALenum eParam, ALint lValue);
71 AL_API ALvoid AL_APIENTRY alDatabufferivEXT(ALuint buffer, ALenum eParam, const ALint* plValues);
72 AL_API ALvoid AL_APIENTRY alGetDatabufferfEXT(ALuint buffer, ALenum eParam, ALfloat *pflValue);
73 AL_API ALvoid AL_APIENTRY alGetDatabufferfvEXT(ALuint buffer, ALenum eParam, ALfloat* pflValues);
74 AL_API ALvoid AL_APIENTRY alGetDatabufferiEXT(ALuint buffer, ALenum eParam, ALint *plValue);
75 AL_API ALvoid AL_APIENTRY alGetDatabufferivEXT(ALuint buffer, ALenum eParam, ALint* plValues);
76 AL_API ALvoid AL_APIENTRY alSelectDatabufferEXT(ALenum target, ALuint uiBuffer);
77 AL_API ALvoid* AL_APIENTRY alMapDatabufferEXT(ALuint uiBuffer, ALintptrEXT start, ALsizeiptrEXT length, ALenum access);
78 AL_API ALvoid AL_APIENTRY alUnmapDatabufferEXT(ALuint uiBuffer);
79 #endif
80 #endif
82 #ifndef AL_EXT_loop_points
83 #define AL_EXT_loop_points 1
84 #define AL_LOOP_POINTS 0x2015
85 #endif
87 #if defined(HAVE_STDINT_H)
88 #include <stdint.h>
89 typedef int64_t ALint64;
90 typedef uint64_t ALuint64;
91 #elif defined(HAVE___INT64)
92 typedef __int64 ALint64;
93 typedef unsigned __int64 ALuint64;
94 #elif (SIZEOF_LONG == 8)
95 typedef long ALint64;
96 typedef unsigned long ALuint64;
97 #elif (SIZEOF_LONG_LONG == 8)
98 typedef long long ALint64;
99 typedef unsigned long long ALuint64;
100 #endif
102 #ifdef HAVE_GCC_FORMAT
103 #define PRINTF_STYLE(x, y) __attribute__((format(printf, (x), (y))))
104 #else
105 #define PRINTF_STYLE(x, y)
106 #endif
108 #ifdef _WIN32
110 #ifndef _WIN32_WINNT
111 #define _WIN32_WINNT 0x0500
112 #endif
113 #include <windows.h>
115 typedef DWORD tls_type;
116 #define tls_create(x) (*(x) = TlsAlloc())
117 #define tls_delete(x) TlsFree((x))
118 #define tls_get(x) TlsGetValue((x))
119 #define tls_set(x, a) TlsSetValue((x), (a))
121 #else
123 #include <unistd.h>
124 #include <assert.h>
125 #include <pthread.h>
126 #ifdef HAVE_PTHREAD_NP_H
127 #include <pthread_np.h>
128 #endif
129 #include <sys/time.h>
130 #include <time.h>
131 #include <errno.h>
133 #define IsBadWritePtr(a,b) ((a) == NULL && (b) != 0)
135 typedef pthread_key_t tls_type;
136 #define tls_create(x) pthread_key_create((x), NULL)
137 #define tls_delete(x) pthread_key_delete((x))
138 #define tls_get(x) pthread_getspecific((x))
139 #define tls_set(x, a) pthread_setspecific((x), (a))
141 typedef pthread_mutex_t CRITICAL_SECTION;
142 static inline void EnterCriticalSection(CRITICAL_SECTION *cs)
144 int ret;
145 ret = pthread_mutex_lock(cs);
146 assert(ret == 0);
148 static inline void LeaveCriticalSection(CRITICAL_SECTION *cs)
150 int ret;
151 ret = pthread_mutex_unlock(cs);
152 assert(ret == 0);
154 static inline void InitializeCriticalSection(CRITICAL_SECTION *cs)
156 pthread_mutexattr_t attrib;
157 int ret;
159 ret = pthread_mutexattr_init(&attrib);
160 assert(ret == 0);
162 ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
163 #ifdef HAVE_PTHREAD_NP_H
164 if(ret != 0)
165 ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
166 #endif
167 assert(ret == 0);
168 ret = pthread_mutex_init(cs, &attrib);
169 assert(ret == 0);
171 pthread_mutexattr_destroy(&attrib);
174 static inline void DeleteCriticalSection(CRITICAL_SECTION *cs)
176 int ret;
177 ret = pthread_mutex_destroy(cs);
178 assert(ret == 0);
181 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
182 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
183 * Additionally, Win32 is supposed to measure the time since Windows started,
184 * as opposed to the actual time. */
185 static inline ALuint timeGetTime(void)
187 int ret;
188 #if _POSIX_TIMERS > 0
189 struct timespec ts;
191 ret = clock_gettime(CLOCK_REALTIME, &ts);
192 assert(ret == 0);
194 return ts.tv_nsec/1000000 + ts.tv_sec*1000;
195 #else
196 struct timeval tv;
198 ret = gettimeofday(&tv, NULL);
199 assert(ret == 0);
201 return tv.tv_usec/1000 + tv.tv_sec*1000;
202 #endif
205 static inline void Sleep(ALuint t)
207 struct timespec tv, rem;
208 tv.tv_nsec = (t*1000000)%1000000000;
209 tv.tv_sec = t/1000;
211 while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
212 tv = rem;
214 #define min(x,y) (((x)<(y))?(x):(y))
215 #define max(x,y) (((x)>(y))?(x):(y))
216 #endif
218 #include "alListener.h"
219 #include "alu.h"
221 #ifdef __cplusplus
222 extern "C" {
223 #endif
226 #define SWMIXER_OUTPUT_RATE 44100
228 #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
229 #define AIRABSORBGAINDBHF (-0.05f)
231 #define LOWPASSFREQCUTOFF (5000)
233 #define DEFAULT_HEAD_DAMPEN (0.25f)
236 // Find the next power-of-2 for non-power-of-2 numbers.
237 static __inline ALuint NextPowerOf2(ALuint value)
239 ALuint powerOf2 = 1;
241 if(value)
243 value--;
244 while(value)
246 value >>= 1;
247 powerOf2 <<= 1;
250 return powerOf2;
254 typedef struct {
255 ALCboolean (*OpenPlayback)(ALCdevice*, const ALCchar*);
256 void (*ClosePlayback)(ALCdevice*);
257 ALCboolean (*ResetPlayback)(ALCdevice*);
258 void (*StopPlayback)(ALCdevice*);
260 ALCboolean (*OpenCapture)(ALCdevice*, const ALCchar*);
261 void (*CloseCapture)(ALCdevice*);
262 void (*StartCapture)(ALCdevice*);
263 void (*StopCapture)(ALCdevice*);
264 void (*CaptureSamples)(ALCdevice*, void*, ALCuint);
265 ALCuint (*AvailableSamples)(ALCdevice*);
266 } BackendFuncs;
268 enum {
269 DEVICE_PROBE,
270 ALL_DEVICE_PROBE,
271 CAPTURE_DEVICE_PROBE
274 void alc_alsa_init(BackendFuncs *func_list);
275 void alc_alsa_deinit(void);
276 void alc_alsa_probe(int type);
277 void alc_oss_init(BackendFuncs *func_list);
278 void alc_oss_deinit(void);
279 void alc_oss_probe(int type);
280 void alc_solaris_init(BackendFuncs *func_list);
281 void alc_solaris_deinit(void);
282 void alc_solaris_probe(int type);
283 void alcDSoundInit(BackendFuncs *func_list);
284 void alcDSoundDeinit(void);
285 void alcDSoundProbe(int type);
286 void alcWinMMInit(BackendFuncs *FuncList);
287 void alcWinMMDeinit(void);
288 void alcWinMMProbe(int type);
289 void alc_pa_init(BackendFuncs *func_list);
290 void alc_pa_deinit(void);
291 void alc_pa_probe(int type);
292 void alc_wave_init(BackendFuncs *func_list);
293 void alc_wave_deinit(void);
294 void alc_wave_probe(int type);
295 void alc_pulse_init(BackendFuncs *func_list);
296 void alc_pulse_deinit(void);
297 void alc_pulse_probe(int type);
300 typedef struct UIntMap {
301 struct {
302 ALuint key;
303 ALvoid *value;
304 } *array;
305 ALsizei size;
306 ALsizei maxsize;
307 } UIntMap;
309 void InitUIntMap(UIntMap *map);
310 void ResetUIntMap(UIntMap *map);
311 ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value);
312 void RemoveUIntMapKey(UIntMap *map, ALuint key);
314 static inline ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
316 if(map->size > 0)
318 ALsizei low = 0;
319 ALsizei high = map->size - 1;
320 while(low < high)
322 ALsizei mid = low + (high-low)/2;
323 if(map->array[mid].key < key)
324 low = mid + 1;
325 else
326 high = mid;
328 if(map->array[low].key == key)
329 return map->array[low].value;
331 return NULL;
335 struct ALCdevice_struct
337 ALCboolean Connected;
338 ALboolean IsCaptureDevice;
340 ALuint Frequency;
341 ALuint UpdateSize;
342 ALuint NumUpdates;
343 ALenum Format;
345 ALCchar *szDeviceName;
347 ALCenum LastError;
349 // Maximum number of sources that can be created
350 ALuint MaxNoOfSources;
351 // Maximum number of slots that can be created
352 ALuint AuxiliaryEffectSlotMax;
354 ALCuint NumMonoSources;
355 ALCuint NumStereoSources;
356 ALuint NumAuxSends;
358 // Map of Buffers for this device
359 UIntMap BufferMap;
361 // Linked List of Effects for this device
362 struct ALeffect *EffectList;
363 ALuint EffectCount;
365 // Linked List of Filters for this device
366 struct ALfilter *FilterList;
367 ALuint FilterCount;
369 // Linked List of Databuffers for this device
370 struct ALdatabuffer *DatabufferList;
371 ALuint DatabufferCount;
373 // Stereo-to-binaural filter
374 struct bs2b *Bs2b;
375 ALCint Bs2bLevel;
377 // Simulated dampening from head occlusion
378 ALfloat HeadDampen;
380 // Duplicate stereo sources on the side/rear channels
381 ALboolean DuplicateStereo;
383 // Dry path buffer mix
384 float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
386 ALuint DevChannels[OUTPUTCHANNELS];
388 ALfloat ChannelMatrix[OUTPUTCHANNELS][OUTPUTCHANNELS];
390 Channel Speaker2Chan[OUTPUTCHANNELS];
391 ALfloat PanningLUT[OUTPUTCHANNELS * LUT_NUM];
392 ALuint NumChan;
394 // Contexts created on this device
395 ALCcontext **Contexts;
396 ALuint NumContexts;
398 BackendFuncs *Funcs;
399 void *ExtraData; // For the backend's use
401 ALCdevice *next;
404 #define ALCdevice_OpenPlayback(a,b) ((a)->Funcs->OpenPlayback((a), (b)))
405 #define ALCdevice_ClosePlayback(a) ((a)->Funcs->ClosePlayback((a)))
406 #define ALCdevice_ResetPlayback(a) ((a)->Funcs->ResetPlayback((a)))
407 #define ALCdevice_StopPlayback(a) ((a)->Funcs->StopPlayback((a)))
408 #define ALCdevice_OpenCapture(a,b) ((a)->Funcs->OpenCapture((a), (b)))
409 #define ALCdevice_CloseCapture(a) ((a)->Funcs->CloseCapture((a)))
410 #define ALCdevice_StartCapture(a) ((a)->Funcs->StartCapture((a)))
411 #define ALCdevice_StopCapture(a) ((a)->Funcs->StopCapture((a)))
412 #define ALCdevice_CaptureSamples(a,b,c) ((a)->Funcs->CaptureSamples((a), (b), (c)))
413 #define ALCdevice_AvailableSamples(a) ((a)->Funcs->AvailableSamples((a)))
415 struct ALCcontext_struct
417 ALlistener Listener;
419 UIntMap SourceMap;
420 UIntMap EffectSlotMap;
422 struct ALdatabuffer *SampleSource;
423 struct ALdatabuffer *SampleSink;
425 ALenum LastError;
427 ALboolean Suspended;
429 ALenum DistanceModel;
430 ALboolean SourceDistanceModel;
432 ALfloat DopplerFactor;
433 ALfloat DopplerVelocity;
434 ALfloat flSpeedOfSound;
436 ALCdevice *Device;
437 const ALCchar *ExtensionList;
439 ALCcontext *next;
442 ALCvoid ReleaseALC(ALCvoid);
444 void AppendDeviceList(const ALCchar *name);
445 void AppendAllDeviceList(const ALCchar *name);
446 void AppendCaptureDeviceList(const ALCchar *name);
448 ALCvoid alcSetError(ALCdevice *device, ALenum errorCode);
450 ALCvoid SuspendContext(ALCcontext *context);
451 ALCvoid ProcessContext(ALCcontext *context);
453 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr);
454 ALuint StopThread(ALvoid *thread);
456 ALCcontext *GetContextSuspended(void);
458 typedef struct RingBuffer RingBuffer;
459 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length);
460 void DestroyRingBuffer(RingBuffer *ring);
461 ALsizei RingBufferSize(RingBuffer *ring);
462 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len);
463 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len);
465 void ReadALConfig(void);
466 void FreeALConfig(void);
467 int ConfigValueExists(const char *blockName, const char *keyName);
468 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def);
469 int GetConfigValueInt(const char *blockName, const char *keyName, int def);
470 float GetConfigValueFloat(const char *blockName, const char *keyName, float def);
471 int GetConfigValueBool(const char *blockName, const char *keyName, int def);
473 void SetRTPriority(void);
475 void SetDefaultChannelOrder(ALCdevice *device);
476 void SetDefaultWFXChannelOrder(ALCdevice *device);
478 void al_print(const char *fname, unsigned int line, const char *fmt, ...)
479 PRINTF_STYLE(3,4);
480 #define AL_PRINT(...) al_print(__FILE__, __LINE__, __VA_ARGS__)
482 #define DECL_VERIFIER(name, type, field) \
483 static type* Verify##name(type *list, ALuint id) \
485 while(list && list->field != id) \
486 list = list->next; \
487 return list; \
490 #ifdef __cplusplus
492 #endif
494 #endif