Use a channel-map to specify the output device channel order
[openal-soft/android/lowlatency.git] / OpenAL32 / Include / alMain.h
blob4094d894f0b3411693db628dbe09cd6d5b5cd4a0
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)
167 // Find the next power-of-2 for non-power-of-2 numbers.
168 static __inline ALuint NextPowerOf2(ALuint value)
170 ALuint powerOf2 = 1;
172 if(value)
174 value--;
175 while(value)
177 value >>= 1;
178 powerOf2 <<= 1;
181 return powerOf2;
185 typedef struct {
186 ALCboolean (*OpenPlayback)(ALCdevice*, const ALCchar*);
187 void (*ClosePlayback)(ALCdevice*);
188 ALCboolean (*ResetPlayback)(ALCdevice*);
189 void (*StopPlayback)(ALCdevice*);
191 ALCboolean (*OpenCapture)(ALCdevice*, const ALCchar*);
192 void (*CloseCapture)(ALCdevice*);
193 void (*StartCapture)(ALCdevice*);
194 void (*StopCapture)(ALCdevice*);
195 void (*CaptureSamples)(ALCdevice*, void*, ALCuint);
196 ALCuint (*AvailableSamples)(ALCdevice*);
197 } BackendFuncs;
199 enum {
200 DEVICE_PROBE,
201 ALL_DEVICE_PROBE,
202 CAPTURE_DEVICE_PROBE
205 void alc_alsa_init(BackendFuncs *func_list);
206 void alc_alsa_deinit(void);
207 void alc_alsa_probe(int type);
208 void alc_oss_init(BackendFuncs *func_list);
209 void alc_oss_deinit(void);
210 void alc_oss_probe(int type);
211 void alc_solaris_init(BackendFuncs *func_list);
212 void alc_solaris_deinit(void);
213 void alc_solarise_probe(int type);
214 void alcDSoundInit(BackendFuncs *func_list);
215 void alcDSoundDeinit(void);
216 void alcDSoundProbe(int type);
217 void alcWinMMInit(BackendFuncs *FuncList);
218 void alcWinMMDeinit(void);
219 void alcWinMMProbe(int type);
220 void alc_pa_init(BackendFuncs *func_list);
221 void alc_pa_deinit(void);
222 void alc_pa_probe(int type);
223 void alc_wave_init(BackendFuncs *func_list);
224 void alc_wave_deinit(void);
225 void alc_wave_probe(int type);
226 void alc_pulse_init(BackendFuncs *func_list);
227 void alc_pulse_deinit(void);
228 void alc_pulse_probe(int type);
231 struct ALCdevice_struct
233 ALCboolean Connected;
234 ALboolean IsCaptureDevice;
236 ALuint Frequency;
237 ALuint UpdateSize;
238 ALuint NumUpdates;
239 ALenum Format;
241 ALCchar *szDeviceName;
243 // Maximum number of sources that can be created
244 ALuint MaxNoOfSources;
245 // Maximum number of slots that can be created
246 ALuint AuxiliaryEffectSlotMax;
248 ALint lNumMonoSources;
249 ALint lNumStereoSources;
250 ALuint NumAuxSends;
252 // Linked List of Buffers for this device
253 struct ALbuffer *Buffers;
254 ALuint BufferCount;
256 // Linked List of Effects for this device
257 struct ALeffect *EffectList;
258 ALuint EffectCount;
260 // Linked List of Filters for this device
261 struct ALfilter *FilterList;
262 ALuint FilterCount;
264 // Linked List of Databuffers for this device
265 struct ALdatabuffer *Databuffers;
266 ALuint DatabufferCount;
268 // Stereo-to-binaural filter
269 struct bs2b *Bs2b;
270 ALCint Bs2bLevel;
272 // Dry path buffer mix
273 float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
275 Channel DevChannels[OUTPUTCHANNELS];
277 // Contexts created on this device
278 ALCcontext **Contexts;
279 ALuint NumContexts;
281 BackendFuncs *Funcs;
282 void *ExtraData; // For the backend's use
284 ALCdevice *next;
287 #define ALCdevice_OpenPlayback(a,b) ((a)->Funcs->OpenPlayback((a), (b)))
288 #define ALCdevice_ClosePlayback(a) ((a)->Funcs->ClosePlayback((a)))
289 #define ALCdevice_ResetPlayback(a) ((a)->Funcs->ResetPlayback((a)))
290 #define ALCdevice_StopPlayback(a) ((a)->Funcs->StopPlayback((a)))
291 #define ALCdevice_OpenCapture(a,b) ((a)->Funcs->OpenCapture((a), (b)))
292 #define ALCdevice_CloseCapture(a) ((a)->Funcs->CloseCapture((a)))
293 #define ALCdevice_StartCapture(a) ((a)->Funcs->StartCapture((a)))
294 #define ALCdevice_StopCapture(a) ((a)->Funcs->StopCapture((a)))
295 #define ALCdevice_CaptureSamples(a,b,c) ((a)->Funcs->CaptureSamples((a), (b), (c)))
296 #define ALCdevice_AvailableSamples(a) ((a)->Funcs->AvailableSamples((a)))
298 struct ALCcontext_struct
300 ALlistener Listener;
302 struct ALsource *Source;
303 ALuint SourceCount;
305 struct ALeffectslot *AuxiliaryEffectSlot;
306 ALuint AuxiliaryEffectSlotCount;
308 struct ALdatabuffer *SampleSource;
309 struct ALdatabuffer *SampleSink;
311 ALenum LastError;
312 ALboolean InUse;
314 ALenum DistanceModel;
315 ALboolean SourceDistanceModel;
317 ALfloat DopplerFactor;
318 ALfloat DopplerVelocity;
319 ALfloat flSpeedOfSound;
321 ALfloat PanningLUT[OUTPUTCHANNELS * LUT_NUM];
322 ALint NumChan;
324 ALfloat ChannelMatrix[OUTPUTCHANNELS][OUTPUTCHANNELS];
326 ALCdevice *Device;
327 const ALCchar *ExtensionList;
329 ALCcontext *next;
332 extern ALint RTPrioLevel;
334 ALCvoid ReleaseALC(ALCvoid);
336 void AppendDeviceList(const ALCchar *name);
337 void AppendAllDeviceList(const ALCchar *name);
338 void AppendCaptureDeviceList(const ALCchar *name);
340 ALCvoid alcSetError(ALenum errorCode);
342 ALCvoid SuspendContext(ALCcontext *context);
343 ALCvoid ProcessContext(ALCcontext *context);
345 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr);
346 ALuint StopThread(ALvoid *thread);
348 ALCcontext *GetContextSuspended(void);
350 typedef struct RingBuffer RingBuffer;
351 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length);
352 void DestroyRingBuffer(RingBuffer *ring);
353 ALsizei RingBufferSize(RingBuffer *ring);
354 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len);
355 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len);
357 void ReadALConfig(void);
358 void FreeALConfig(void);
359 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def);
360 int GetConfigValueInt(const char *blockName, const char *keyName, int def);
361 float GetConfigValueFloat(const char *blockName, const char *keyName, float def);
362 int GetConfigValueBool(const char *blockName, const char *keyName, float def);
364 void EnableRTPrio(ALint level);
366 ALCboolean ALCAPIENTRY alcMakeCurrent(ALCcontext *context);
367 ALCcontext* ALCAPIENTRY alcGetThreadContext(void);
369 // Sets the default channel order used by most non-WaveFormatEx-based APIs
370 static __inline void SetDefaultChannelOrder(ALCdevice *device)
372 switch(aluChannelsFromFormat(device->Format))
374 case 1: /* Mono is rendered as stereo; fall-through... */
375 case 2: device->DevChannels[0] = FRONT_LEFT;
376 device->DevChannels[1] = FRONT_RIGHT; break;
378 case 4: device->DevChannels[0] = FRONT_LEFT;
379 device->DevChannels[1] = FRONT_RIGHT;
380 device->DevChannels[2] = BACK_LEFT;
381 device->DevChannels[3] = BACK_RIGHT; break;
383 case 6: device->DevChannels[0] = FRONT_LEFT;
384 device->DevChannels[1] = FRONT_RIGHT;
385 device->DevChannels[2] = BACK_LEFT;
386 device->DevChannels[3] = BACK_RIGHT;
387 device->DevChannels[4] = FRONT_CENTER;
388 device->DevChannels[5] = LFE; break;
390 case 7: device->DevChannels[0] = FRONT_LEFT;
391 device->DevChannels[1] = FRONT_RIGHT;
392 device->DevChannels[2] = FRONT_CENTER;
393 device->DevChannels[3] = LFE;
394 device->DevChannels[4] = FRONT_CENTER;
395 device->DevChannels[5] = SIDE_LEFT;
396 device->DevChannels[6] = SIDE_RIGHT; break;
398 case 8: device->DevChannels[0] = FRONT_LEFT;
399 device->DevChannels[1] = FRONT_RIGHT;
400 device->DevChannels[2] = BACK_LEFT;
401 device->DevChannels[3] = BACK_RIGHT;
402 device->DevChannels[4] = FRONT_CENTER;
403 device->DevChannels[5] = LFE;
404 device->DevChannels[6] = SIDE_LEFT;
405 device->DevChannels[7] = SIDE_RIGHT; break;
408 // Sets the default order used by WaveFormatEx
409 static __inline void SetDefaultWFXChannelOrder(ALCdevice *device)
411 switch(aluChannelsFromFormat(device->Format))
413 case 1: /* Mono is rendered as stereo; fall-through... */
414 case 2: device->DevChannels[0] = FRONT_LEFT;
415 device->DevChannels[1] = FRONT_RIGHT; break;
417 case 4: device->DevChannels[0] = FRONT_LEFT;
418 device->DevChannels[1] = FRONT_RIGHT;
419 device->DevChannels[2] = BACK_LEFT;
420 device->DevChannels[3] = BACK_RIGHT; break;
422 case 6: device->DevChannels[0] = FRONT_LEFT;
423 device->DevChannels[1] = FRONT_RIGHT;
424 device->DevChannels[2] = FRONT_CENTER;
425 device->DevChannels[3] = LFE;
426 device->DevChannels[4] = BACK_LEFT;
427 device->DevChannels[5] = BACK_RIGHT; break;
429 case 7: device->DevChannels[0] = FRONT_LEFT;
430 device->DevChannels[1] = FRONT_RIGHT;
431 device->DevChannels[2] = FRONT_CENTER;
432 device->DevChannels[3] = LFE;
433 device->DevChannels[4] = FRONT_CENTER;
434 device->DevChannels[5] = SIDE_LEFT;
435 device->DevChannels[6] = SIDE_RIGHT; break;
437 case 8: device->DevChannels[0] = FRONT_LEFT;
438 device->DevChannels[1] = FRONT_RIGHT;
439 device->DevChannels[2] = FRONT_CENTER;
440 device->DevChannels[3] = LFE;
441 device->DevChannels[4] = BACK_LEFT;
442 device->DevChannels[5] = BACK_RIGHT;
443 device->DevChannels[6] = SIDE_LEFT;
444 device->DevChannels[7] = SIDE_RIGHT; break;
448 #ifdef __cplusplus
450 #endif
452 #endif