Enable real-time priority for ALSA, OSS, and DirectSound mixing loops
[openal-soft.git] / OpenAL32 / Include / alMain.h
blob0820b9dc7e190b95383a13de02c6f0e5612b185b
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;
184 static __inline void EnableRTPrio()
186 #ifdef _WIN32
187 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
188 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
189 struct sched_param param;
191 /* Use the minimum real-time priority possible for now (on Linux this
192 * should be 1 for SCHED_RR) */
193 param.sched_priority = sched_get_priority_min(SCHED_RR);
194 pthread_setschedparam(pthread_self(), SCHED_RR, &param);
195 #else
196 /* Real-time priority not available */
197 #endif
201 typedef struct {
202 ALCboolean (*OpenPlayback)(ALCdevice*, const ALCchar*);
203 void (*ClosePlayback)(ALCdevice*);
204 ALCboolean (*ResetPlayback)(ALCdevice*);
205 void (*StopPlayback)(ALCdevice*);
207 ALCboolean (*OpenCapture)(ALCdevice*, const ALCchar*);
208 void (*CloseCapture)(ALCdevice*);
209 void (*StartCapture)(ALCdevice*);
210 void (*StopCapture)(ALCdevice*);
211 void (*CaptureSamples)(ALCdevice*, void*, ALCuint);
212 ALCuint (*AvailableSamples)(ALCdevice*);
213 } BackendFuncs;
215 enum {
216 DEVICE_PROBE,
217 ALL_DEVICE_PROBE,
218 CAPTURE_DEVICE_PROBE
221 void alc_alsa_init(BackendFuncs *func_list);
222 void alc_alsa_deinit(void);
223 void alc_alsa_probe(int type);
224 void alc_oss_init(BackendFuncs *func_list);
225 void alc_oss_deinit(void);
226 void alc_oss_probe(int type);
227 void alc_solaris_init(BackendFuncs *func_list);
228 void alc_solaris_deinit(void);
229 void alc_solarise_probe(int type);
230 void alcDSoundInit(BackendFuncs *func_list);
231 void alcDSoundDeinit(void);
232 void alcDSoundProbe(int type);
233 void alcWinMMInit(BackendFuncs *FuncList);
234 void alcWinMMDeinit(void);
235 void alcWinMMProbe(int type);
236 void alc_pa_init(BackendFuncs *func_list);
237 void alc_pa_deinit(void);
238 void alc_pa_probe(int type);
239 void alc_wave_init(BackendFuncs *func_list);
240 void alc_wave_deinit(void);
241 void alc_wave_probe(int type);
242 void alc_pulse_init(BackendFuncs *func_list);
243 void alc_pulse_deinit(void);
244 void alc_pulse_probe(int type);
247 struct ALCdevice_struct
249 ALCboolean Connected;
250 ALboolean IsCaptureDevice;
252 ALuint Frequency;
253 ALuint UpdateSize;
254 ALuint NumUpdates;
255 ALenum Format;
257 ALCchar *szDeviceName;
259 // Maximum number of sources that can be created
260 ALuint MaxNoOfSources;
261 // Maximum number of slots that can be created
262 ALuint AuxiliaryEffectSlotMax;
264 ALint lNumMonoSources;
265 ALint lNumStereoSources;
266 ALuint NumAuxSends;
268 // Linked List of Buffers for this device
269 struct ALbuffer *Buffers;
270 ALuint BufferCount;
272 // Linked List of Effects for this device
273 struct ALeffect *EffectList;
274 ALuint EffectCount;
276 // Linked List of Filters for this device
277 struct ALfilter *FilterList;
278 ALuint FilterCount;
280 // Linked List of Databuffers for this device
281 struct ALdatabuffer *Databuffers;
282 ALuint DatabufferCount;
284 // Stereo-to-binaural filter
285 struct bs2b *Bs2b;
286 ALCint Bs2bLevel;
288 // Dry path buffer mix
289 float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
291 // Contexts created on this device
292 ALCcontext **Contexts;
293 ALuint NumContexts;
295 BackendFuncs *Funcs;
296 void *ExtraData; // For the backend's use
298 ALCdevice *next;
301 #define ALCdevice_OpenPlayback(a,b) ((a)->Funcs->OpenPlayback((a), (b)))
302 #define ALCdevice_ClosePlayback(a) ((a)->Funcs->ClosePlayback((a)))
303 #define ALCdevice_ResetPlayback(a) ((a)->Funcs->ResetPlayback((a)))
304 #define ALCdevice_StopPlayback(a) ((a)->Funcs->StopPlayback((a)))
305 #define ALCdevice_OpenCapture(a,b) ((a)->Funcs->OpenCapture((a), (b)))
306 #define ALCdevice_CloseCapture(a) ((a)->Funcs->CloseCapture((a)))
307 #define ALCdevice_StartCapture(a) ((a)->Funcs->StartCapture((a)))
308 #define ALCdevice_StopCapture(a) ((a)->Funcs->StopCapture((a)))
309 #define ALCdevice_CaptureSamples(a,b,c) ((a)->Funcs->CaptureSamples((a), (b), (c)))
310 #define ALCdevice_AvailableSamples(a) ((a)->Funcs->AvailableSamples((a)))
312 struct ALCcontext_struct
314 ALlistener Listener;
316 struct ALsource *Source;
317 ALuint SourceCount;
319 struct ALeffectslot *AuxiliaryEffectSlot;
320 ALuint AuxiliaryEffectSlotCount;
322 struct ALdatabuffer *SampleSource;
323 struct ALdatabuffer *SampleSink;
325 ALenum LastError;
326 ALboolean InUse;
328 ALenum DistanceModel;
329 ALboolean SourceDistanceModel;
331 ALfloat DopplerFactor;
332 ALfloat DopplerVelocity;
333 ALfloat flSpeedOfSound;
335 ALfloat PanningLUT[OUTPUTCHANNELS * LUT_NUM];
336 ALint NumChan;
338 ALfloat ChannelMatrix[OUTPUTCHANNELS][OUTPUTCHANNELS];
340 ALCdevice *Device;
341 const ALCchar *ExtensionList;
343 ALCcontext *next;
346 ALCvoid ReleaseALC(ALCvoid);
348 void AppendDeviceList(const ALCchar *name);
349 void AppendAllDeviceList(const ALCchar *name);
350 void AppendCaptureDeviceList(const ALCchar *name);
352 ALCvoid alcSetError(ALenum errorCode);
354 ALCvoid SuspendContext(ALCcontext *context);
355 ALCvoid ProcessContext(ALCcontext *context);
357 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr);
358 ALuint StopThread(ALvoid *thread);
360 ALCcontext *GetContextSuspended(void);
362 typedef struct RingBuffer RingBuffer;
363 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length);
364 void DestroyRingBuffer(RingBuffer *ring);
365 ALsizei RingBufferSize(RingBuffer *ring);
366 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len);
367 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len);
369 void ReadALConfig(void);
370 void FreeALConfig(void);
371 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def);
372 int GetConfigValueInt(const char *blockName, const char *keyName, int def);
373 float GetConfigValueFloat(const char *blockName, const char *keyName, float def);
374 int GetConfigValueBool(const char *blockName, const char *keyName, float def);
376 ALCboolean ALCAPIENTRY alcMakeCurrent(ALCcontext *context);
377 ALCcontext* ALCAPIENTRY alcGetThreadContext(void);
379 #ifdef __cplusplus
381 #endif
383 #endif