view OpenAL32/Include/alMain.h @ 0:f9476ff7637e

initial forking of open-al to create multiple listeners
author Robert McIntyre <rlm@mit.edu>
date Tue, 25 Oct 2011 13:02:31 -0700
parents
children 96298d83959c
line wrap: on
line source
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 ALC_SOFT_device_loopback
17 #define ALC_SOFT_device_loopback 1
18 #define ALC_FORMAT_CHANNELS_SOFT 0x1990
19 #define ALC_FORMAT_TYPE_SOFT 0x1991
21 /* Sample types */
22 #define ALC_BYTE 0x1400
23 #define ALC_UNSIGNED_BYTE 0x1401
24 #define ALC_SHORT 0x1402
25 #define ALC_UNSIGNED_SHORT 0x1403
26 #define ALC_INT 0x1404
27 #define ALC_UNSIGNED_INT 0x1405
28 #define ALC_FLOAT 0x1406
30 /* Channel configurations */
31 #define ALC_MONO 0x1500
32 #define ALC_STEREO 0x1501
33 #define ALC_QUAD 0x1503
34 #define ALC_5POINT1 0x1504 /* (WFX order) */
35 #define ALC_6POINT1 0x1505 /* (WFX order) */
36 #define ALC_7POINT1 0x1506 /* (WFX order) */
38 typedef ALCdevice* (ALC_APIENTRY*LPALCLOOPBACKOPENDEVICESOFT)(void);
39 typedef ALCboolean (ALC_APIENTRY*LPALCISRENDERFORMATSUPPORTEDSOFT)(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type);
40 typedef void (ALC_APIENTRY*LPALCRENDERSAMPLESSOFT)(ALCdevice *device, ALCvoid *buffer, ALCsizei samples);
41 #ifdef AL_ALEXT_PROTOTYPES
42 ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(void);
43 ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type);
44 ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples);
45 #endif
46 #endif
48 #ifndef AL_SOFT_buffer_samples
49 #define AL_SOFT_buffer_samples 1
50 /* Sample types */
51 #define AL_BYTE 0x1400
52 #define AL_UNSIGNED_BYTE 0x1401
53 #define AL_SHORT 0x1402
54 #define AL_UNSIGNED_SHORT 0x1403
55 #define AL_INT 0x1404
56 #define AL_UNSIGNED_INT 0x1405
57 #define AL_FLOAT 0x1406
58 #define AL_DOUBLE 0x1407
59 #define AL_BYTE3 0x1408
60 #define AL_UNSIGNED_BYTE3 0x1409
61 #define AL_MULAW 0x1410
62 #define AL_IMA4 0x1411
64 /* Channel configurations */
65 #define AL_MONO 0x1500
66 #define AL_STEREO 0x1501
67 #define AL_REAR 0x1502
68 #define AL_QUAD 0x1503
69 #define AL_5POINT1 0x1504 /* (WFX order) */
70 #define AL_6POINT1 0x1505 /* (WFX order) */
71 #define AL_7POINT1 0x1506 /* (WFX order) */
73 /* Storage formats */
74 #define AL_MONO8 0x1100
75 #define AL_MONO16 0x1101
76 #define AL_MONO32F 0x10010
77 #define AL_STEREO8 0x1102
78 #define AL_STEREO16 0x1103
79 #define AL_STEREO32F 0x10011
80 #define AL_QUAD8 0x1204
81 #define AL_QUAD16 0x1205
82 #define AL_QUAD32F 0x1206
83 #define AL_REAR8 0x1207
84 #define AL_REAR16 0x1208
85 #define AL_REAR32F 0x1209
86 #define AL_5POINT1_8 0x120A
87 #define AL_5POINT1_16 0x120B
88 #define AL_5POINT1_32F 0x120C
89 #define AL_6POINT1_8 0x120D
90 #define AL_6POINT1_16 0x120E
91 #define AL_6POINT1_32F 0x120F
92 #define AL_7POINT1_8 0x1210
93 #define AL_7POINT1_16 0x1211
94 #define AL_7POINT1_32F 0x1212
96 typedef void (AL_APIENTRY*LPALBUFFERSAMPLESSOFT)(ALuint,ALuint,ALenum,ALsizei,ALenum,ALenum,const ALvoid*);
97 typedef void (AL_APIENTRY*LPALBUFFERSUBSAMPLESSOFT)(ALuint,ALsizei,ALsizei,ALenum,ALenum,const ALvoid*);
98 typedef void (AL_APIENTRY*LPALGETBUFFERSAMPLESSOFT)(ALuint,ALsizei,ALsizei,ALenum,ALenum,ALvoid*);
99 typedef ALboolean (AL_APIENTRY*LPALISBUFFERFORMATSUPPORTEDSOFT)(ALenum);
100 #ifdef AL_ALEXT_PROTOTYPES
101 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint buffer,
102 ALuint samplerate, ALenum internalformat, ALsizei frames,
103 ALenum channels, ALenum type, const ALvoid *data);
104 AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint buffer,
105 ALsizei offset, ALsizei frames,
106 ALenum channels, ALenum type, const ALvoid *data);
107 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint buffer,
108 ALsizei offset, ALsizei frames,
109 ALenum channels, ALenum type, ALvoid *data);
110 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum format);
111 #endif
112 #endif
114 #ifndef AL_SOFT_non_virtual_channels
115 #define AL_SOFT_non_virtual_channels 1
116 #define AL_VIRTUAL_CHANNELS_SOFT 0x1033
117 #endif
119 #ifndef AL_SOFT_deferred_updates
120 #define AL_SOFT_deferred_updates 1
121 #define AL_DEFERRED_UPDATES_SOFT 0xC002
122 typedef ALvoid (AL_APIENTRY*LPALDEFERUPDATESSOFT)(void);
123 typedef ALvoid (AL_APIENTRY*LPALPROCESSUPDATESSOFT)(void);
124 #ifdef AL_ALEXT_PROTOTYPES
125 AL_API ALvoid AL_APIENTRY alDeferUpdatesSOFT(void);
126 AL_API ALvoid AL_APIENTRY alProcessUpdatesSOFT(void);
127 #endif
128 #endif
131 #if defined(HAVE_STDINT_H)
132 #include <stdint.h>
133 typedef int64_t ALint64;
134 typedef uint64_t ALuint64;
135 #elif defined(HAVE___INT64)
136 typedef __int64 ALint64;
137 typedef unsigned __int64 ALuint64;
138 #elif (SIZEOF_LONG == 8)
139 typedef long ALint64;
140 typedef unsigned long ALuint64;
141 #elif (SIZEOF_LONG_LONG == 8)
142 typedef long long ALint64;
143 typedef unsigned long long ALuint64;
144 #endif
146 typedef ptrdiff_t ALintptrEXT;
147 typedef ptrdiff_t ALsizeiptrEXT;
149 #ifdef HAVE_GCC_FORMAT
150 #define PRINTF_STYLE(x, y) __attribute__((format(printf, (x), (y))))
151 #else
152 #define PRINTF_STYLE(x, y)
153 #endif
155 #if defined(HAVE_RESTRICT)
156 #define RESTRICT restrict
157 #elif defined(HAVE___RESTRICT)
158 #define RESTRICT __restrict
159 #else
160 #define RESTRICT
161 #endif
163 #ifdef _WIN32
165 #ifndef _WIN32_WINNT
166 #define _WIN32_WINNT 0x0500
167 #endif
168 #include <windows.h>
170 typedef DWORD tls_type;
171 #define tls_create(x) (*(x) = TlsAlloc())
172 #define tls_delete(x) TlsFree((x))
173 #define tls_get(x) TlsGetValue((x))
174 #define tls_set(x, a) TlsSetValue((x), (a))
176 #define HAVE_DYNLOAD 1
177 void *LoadLib(const char *name);
178 void CloseLib(void *handle);
179 void *GetSymbol(void *handle, const char *name);
181 typedef LONG pthread_once_t;
182 #define PTHREAD_ONCE_INIT 0
183 void pthread_once(pthread_once_t *once, void (*callback)(void));
185 #else
187 #include <unistd.h>
188 #include <assert.h>
189 #include <pthread.h>
190 #ifdef HAVE_PTHREAD_NP_H
191 #include <pthread_np.h>
192 #endif
193 #include <sys/time.h>
194 #include <time.h>
195 #include <errno.h>
197 #define IsBadWritePtr(a,b) ((a) == NULL && (b) != 0)
199 typedef pthread_key_t tls_type;
200 #define tls_create(x) pthread_key_create((x), NULL)
201 #define tls_delete(x) pthread_key_delete((x))
202 #define tls_get(x) pthread_getspecific((x))
203 #define tls_set(x, a) pthread_setspecific((x), (a))
205 typedef pthread_mutex_t CRITICAL_SECTION;
206 void InitializeCriticalSection(CRITICAL_SECTION *cs);
207 void DeleteCriticalSection(CRITICAL_SECTION *cs);
208 void EnterCriticalSection(CRITICAL_SECTION *cs);
209 void LeaveCriticalSection(CRITICAL_SECTION *cs);
211 ALuint timeGetTime(void);
213 static __inline void Sleep(ALuint t)
214 {
215 struct timespec tv, rem;
216 tv.tv_nsec = (t*1000000)%1000000000;
217 tv.tv_sec = t/1000;
219 while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
220 tv = rem;
221 }
223 #if defined(HAVE_DLFCN_H)
224 #define HAVE_DYNLOAD 1
225 void *LoadLib(const char *name);
226 void CloseLib(void *handle);
227 void *GetSymbol(void *handle, const char *name);
228 #endif
230 #endif
232 #include "alListener.h"
233 #include "alu.h"
235 #ifdef __cplusplus
236 extern "C" {
237 #endif
240 #define DEFAULT_OUTPUT_RATE (44100)
242 #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
243 #define AIRABSORBGAINHF (0.99426) /* -0.05dB */
245 #define LOWPASSFREQCUTOFF (5000)
248 // Find the next power-of-2 for non-power-of-2 numbers.
249 static __inline ALuint NextPowerOf2(ALuint value)
250 {
251 ALuint powerOf2 = 1;
253 if(value)
254 {
255 value--;
256 while(value)
257 {
258 value >>= 1;
259 powerOf2 <<= 1;
260 }
261 }
262 return powerOf2;
263 }
266 enum DevProbe {
267 DEVICE_PROBE,
268 ALL_DEVICE_PROBE,
269 CAPTURE_DEVICE_PROBE
270 };
272 typedef struct {
273 ALCboolean (*OpenPlayback)(ALCdevice*, const ALCchar*);
274 void (*ClosePlayback)(ALCdevice*);
275 ALCboolean (*ResetPlayback)(ALCdevice*);
276 void (*StopPlayback)(ALCdevice*);
278 ALCboolean (*OpenCapture)(ALCdevice*, const ALCchar*);
279 void (*CloseCapture)(ALCdevice*);
280 void (*StartCapture)(ALCdevice*);
281 void (*StopCapture)(ALCdevice*);
282 void (*CaptureSamples)(ALCdevice*, void*, ALCuint);
283 ALCuint (*AvailableSamples)(ALCdevice*);
284 } BackendFuncs;
286 struct BackendInfo {
287 const char *name;
288 ALCboolean (*Init)(BackendFuncs*);
289 void (*Deinit)(void);
290 void (*Probe)(enum DevProbe);
291 BackendFuncs Funcs;
292 };
294 ALCboolean alc_alsa_init(BackendFuncs *func_list);
295 void alc_alsa_deinit(void);
296 void alc_alsa_probe(enum DevProbe type);
297 ALCboolean alc_oss_init(BackendFuncs *func_list);
298 void alc_oss_deinit(void);
299 void alc_oss_probe(enum DevProbe type);
300 ALCboolean alc_solaris_init(BackendFuncs *func_list);
301 void alc_solaris_deinit(void);
302 void alc_solaris_probe(enum DevProbe type);
303 ALCboolean alc_sndio_init(BackendFuncs *func_list);
304 void alc_sndio_deinit(void);
305 void alc_sndio_probe(enum DevProbe type);
306 ALCboolean alcMMDevApiInit(BackendFuncs *func_list);
307 void alcMMDevApiDeinit(void);
308 void alcMMDevApiProbe(enum DevProbe type);
309 ALCboolean alcDSoundInit(BackendFuncs *func_list);
310 void alcDSoundDeinit(void);
311 void alcDSoundProbe(enum DevProbe type);
312 ALCboolean alcWinMMInit(BackendFuncs *FuncList);
313 void alcWinMMDeinit(void);
314 void alcWinMMProbe(enum DevProbe type);
315 ALCboolean alc_pa_init(BackendFuncs *func_list);
316 void alc_pa_deinit(void);
317 void alc_pa_probe(enum DevProbe type);
318 ALCboolean alc_wave_init(BackendFuncs *func_list);
319 void alc_wave_deinit(void);
320 void alc_wave_probe(enum DevProbe type);
321 ALCboolean alc_pulse_init(BackendFuncs *func_list);
322 void alc_pulse_deinit(void);
323 void alc_pulse_probe(enum DevProbe type);
324 ALCboolean alc_ca_init(BackendFuncs *func_list);
325 void alc_ca_deinit(void);
326 void alc_ca_probe(enum DevProbe type);
327 ALCboolean alc_opensl_init(BackendFuncs *func_list);
328 void alc_opensl_deinit(void);
329 void alc_opensl_probe(enum DevProbe type);
330 ALCboolean alc_null_init(BackendFuncs *func_list);
331 void alc_null_deinit(void);
332 void alc_null_probe(enum DevProbe type);
333 ALCboolean alc_loopback_init(BackendFuncs *func_list);
334 void alc_loopback_deinit(void);
335 void alc_loopback_probe(enum DevProbe type);
337 //RLM: aurellem send device insertion
338 ALCboolean alc_send_init(BackendFuncs *func_list);
339 void alc_send_deinit(void);
340 void alc_send_probe(enum DevProbe type);
343 typedef struct UIntMap {
344 struct {
345 ALuint key;
346 ALvoid *value;
347 } *array;
348 ALsizei size;
349 ALsizei maxsize;
350 } UIntMap;
352 void InitUIntMap(UIntMap *map);
353 void ResetUIntMap(UIntMap *map);
354 ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value);
355 void RemoveUIntMapKey(UIntMap *map, ALuint key);
356 ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key);
358 /* Device formats */
359 enum DevFmtType {
360 DevFmtByte = AL_BYTE,
361 DevFmtUByte = AL_UNSIGNED_BYTE,
362 DevFmtShort = AL_SHORT,
363 DevFmtUShort = AL_UNSIGNED_SHORT,
364 DevFmtFloat = AL_FLOAT
365 };
366 enum DevFmtChannels {
367 DevFmtMono = AL_MONO,
368 DevFmtStereo = AL_STEREO,
369 DevFmtQuad = AL_QUAD,
370 DevFmtX51 = AL_5POINT1,
371 DevFmtX61 = AL_6POINT1,
372 DevFmtX71 = AL_7POINT1,
374 /* Similar to 5.1, except using the side channels instead of back */
375 DevFmtX51Side = 0x80000000 | AL_5POINT1
376 };
378 ALuint BytesFromDevFmt(enum DevFmtType type);
379 ALuint ChannelsFromDevFmt(enum DevFmtChannels chans);
380 static __inline ALuint FrameSizeFromDevFmt(enum DevFmtChannels chans,
381 enum DevFmtType type)
382 {
383 return ChannelsFromDevFmt(chans) * BytesFromDevFmt(type);
384 }
387 extern const struct EffectList {
388 const char *name;
389 int type;
390 const char *ename;
391 ALenum val;
392 } EffectList[];
395 struct ALCdevice_struct
396 {
397 ALCboolean Connected;
398 ALboolean IsCaptureDevice;
399 ALboolean IsLoopbackDevice;
401 CRITICAL_SECTION Mutex;
403 ALuint Frequency;
404 ALuint UpdateSize;
405 ALuint NumUpdates;
406 enum DevFmtChannels FmtChans;
407 enum DevFmtType FmtType;
409 ALCchar *szDeviceName;
411 ALCenum LastError;
413 // Maximum number of sources that can be created
414 ALuint MaxNoOfSources;
415 // Maximum number of slots that can be created
416 ALuint AuxiliaryEffectSlotMax;
418 ALCuint NumMonoSources;
419 ALCuint NumStereoSources;
420 ALuint NumAuxSends;
422 // Map of Buffers for this device
423 UIntMap BufferMap;
425 // Map of Effects for this device
426 UIntMap EffectMap;
428 // Map of Filters for this device
429 UIntMap FilterMap;
431 // Stereo-to-binaural filter
432 struct bs2b *Bs2b;
433 ALCint Bs2bLevel;
435 // Device flags
436 ALuint Flags;
438 // Dry path buffer mix
439 ALfloat DryBuffer[BUFFERSIZE][MAXCHANNELS];
441 enum Channel DevChannels[MAXCHANNELS];
443 enum Channel Speaker2Chan[MAXCHANNELS];
444 ALfloat PanningLUT[LUT_NUM][MAXCHANNELS];
445 ALuint NumChan;
447 ALfloat ClickRemoval[MAXCHANNELS];
448 ALfloat PendingClicks[MAXCHANNELS];
450 // Contexts created on this device
451 ALCcontext **Contexts;
452 ALuint NumContexts;
454 BackendFuncs *Funcs;
455 void *ExtraData; // For the backend's use
457 ALCdevice *next;
458 };
460 #define ALCdevice_OpenPlayback(a,b) ((a)->Funcs->OpenPlayback((a), (b)))
461 #define ALCdevice_ClosePlayback(a) ((a)->Funcs->ClosePlayback((a)))
462 #define ALCdevice_ResetPlayback(a) ((a)->Funcs->ResetPlayback((a)))
463 #define ALCdevice_StopPlayback(a) ((a)->Funcs->StopPlayback((a)))
464 #define ALCdevice_OpenCapture(a,b) ((a)->Funcs->OpenCapture((a), (b)))
465 #define ALCdevice_CloseCapture(a) ((a)->Funcs->CloseCapture((a)))
466 #define ALCdevice_StartCapture(a) ((a)->Funcs->StartCapture((a)))
467 #define ALCdevice_StopCapture(a) ((a)->Funcs->StopCapture((a)))
468 #define ALCdevice_CaptureSamples(a,b,c) ((a)->Funcs->CaptureSamples((a), (b), (c)))
469 #define ALCdevice_AvailableSamples(a) ((a)->Funcs->AvailableSamples((a)))
471 // Duplicate stereo sources on the side/rear channels
472 #define DEVICE_DUPLICATE_STEREO (1<<0)
473 // Use HRTF filters for mixing sounds
474 #define DEVICE_USE_HRTF (1<<1)
475 // Frequency was requested by the app or config file
476 #define DEVICE_FREQUENCY_REQUEST (1<<2)
477 // Channel configuration was requested by the config file
478 #define DEVICE_CHANNELS_REQUEST (1<<3)
480 // Specifies if the device is currently running
481 #define DEVICE_RUNNING (1<<31)
483 struct ALCcontext_struct
484 {
485 ALlistener Listener;
487 UIntMap SourceMap;
488 UIntMap EffectSlotMap;
490 ALenum LastError;
492 ALboolean UpdateSources;
494 enum DistanceModel DistanceModel;
495 ALboolean SourceDistanceModel;
497 ALfloat DopplerFactor;
498 ALfloat DopplerVelocity;
499 ALfloat flSpeedOfSound;
500 ALboolean DeferUpdates;
502 struct ALsource **ActiveSources;
503 ALsizei ActiveSourceCount;
504 ALsizei MaxActiveSources;
506 ALCdevice *Device;
507 const ALCchar *ExtensionList;
509 ALCcontext *next;
510 };
512 void AppendDeviceList(const ALCchar *name);
513 void AppendAllDeviceList(const ALCchar *name);
514 void AppendCaptureDeviceList(const ALCchar *name);
516 ALCvoid alcSetError(ALCdevice *device, ALenum errorCode);
518 ALCvoid LockDevice(ALCdevice *device);
519 ALCvoid UnlockDevice(ALCdevice *device);
520 ALCvoid LockContext(ALCcontext *context);
521 ALCvoid UnlockContext(ALCcontext *context);
523 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr);
524 ALuint StopThread(ALvoid *thread);
526 ALCcontext *GetLockedContext(void);
528 typedef struct RingBuffer RingBuffer;
529 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length);
530 void DestroyRingBuffer(RingBuffer *ring);
531 ALsizei RingBufferSize(RingBuffer *ring);
532 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len);
533 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len);
535 void ReadALConfig(void);
536 void FreeALConfig(void);
537 int ConfigValueExists(const char *blockName, const char *keyName);
538 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def);
539 int GetConfigValueInt(const char *blockName, const char *keyName, int def);
540 float GetConfigValueFloat(const char *blockName, const char *keyName, float def);
541 int GetConfigValueBool(const char *blockName, const char *keyName, int def);
543 void SetRTPriority(void);
545 void SetDefaultChannelOrder(ALCdevice *device);
546 void SetDefaultWFXChannelOrder(ALCdevice *device);
548 const ALCchar *DevFmtTypeString(enum DevFmtType type);
549 const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans);
551 #define HRIR_BITS (5)
552 #define HRIR_LENGTH (1<<HRIR_BITS)
553 #define HRIR_MASK (HRIR_LENGTH-1)
554 void InitHrtf(void);
555 ALCboolean IsHrtfCompatible(ALCdevice *device);
556 ALfloat CalcHrtfDelta(ALfloat oldGain, ALfloat newGain, const ALfloat olddir[3], const ALfloat newdir[3]);
557 void GetLerpedHrtfCoeffs(ALfloat elevation, ALfloat azimuth, ALfloat gain, ALfloat (*coeffs)[2], ALuint *delays);
558 ALuint GetMovingHrtfCoeffs(ALfloat elevation, ALfloat azimuth, ALfloat gain, ALfloat delta, ALint counter, ALfloat (*coeffs)[2], ALuint *delays, ALfloat (*coeffStep)[2], ALint *delayStep);
560 void al_print(const char *fname, unsigned int line, const char *fmt, ...)
561 PRINTF_STYLE(3,4);
562 #define AL_PRINT(...) al_print(__FILE__, __LINE__, __VA_ARGS__)
564 extern FILE *LogFile;
565 enum LogLevel {
566 NoLog,
567 LogError,
568 LogWarning,
569 LogTrace
570 };
571 extern enum LogLevel LogLevel;
573 #define TRACE(...) do { \
574 if(LogLevel >= LogTrace) \
575 AL_PRINT(__VA_ARGS__); \
576 } while(0)
578 #define WARN(...) do { \
579 if(LogLevel >= LogWarning) \
580 AL_PRINT(__VA_ARGS__); \
581 } while(0)
583 #define ERR(...) do { \
584 if(LogLevel >= LogError) \
585 AL_PRINT(__VA_ARGS__); \
586 } while(0)
589 extern ALdouble ConeScale;
590 extern ALdouble ZScale;
592 extern ALint RTPrioLevel;
594 #ifdef __cplusplus
595 }
596 #endif
598 #endif