alMain.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. #ifndef AL_MAIN_H
  2. #define AL_MAIN_H
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #ifdef HAVE_FENV_H
  7. #include <fenv.h>
  8. #endif
  9. #include "AL/al.h"
  10. #include "AL/alc.h"
  11. #include "AL/alext.h"
  12. #ifndef AL_EXT_buffer_sub_data
  13. #define AL_EXT_buffer_sub_data 1
  14. #define AL_BYTE_RW_OFFSETS_EXT 0x1031
  15. #define AL_SAMPLE_RW_OFFSETS_EXT 0x1032
  16. typedef ALvoid (AL_APIENTRY*PFNALBUFFERSUBDATAEXTPROC)(ALuint,ALenum,const ALvoid*,ALsizei,ALsizei);
  17. #ifdef AL_ALEXT_PROTOTYPES
  18. AL_API ALvoid AL_APIENTRY alBufferSubDataEXT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length);
  19. #endif
  20. #endif
  21. #ifndef AL_EXT_sample_buffer_object
  22. #define AL_EXT_sample_buffer_object 1
  23. typedef ptrdiff_t ALintptrEXT;
  24. typedef ptrdiff_t ALsizeiptrEXT;
  25. #define AL_SAMPLE_SOURCE_EXT 0x1040
  26. #define AL_SAMPLE_SINK_EXT 0x1041
  27. #define AL_READ_ONLY_EXT 0x1042
  28. #define AL_WRITE_ONLY_EXT 0x1043
  29. #define AL_READ_WRITE_EXT 0x1044
  30. #define AL_STREAM_WRITE_EXT 0x1045
  31. #define AL_STREAM_READ_EXT 0x1046
  32. #define AL_STREAM_COPY_EXT 0x1047
  33. #define AL_STATIC_WRITE_EXT 0x1048
  34. #define AL_STATIC_READ_EXT 0x1049
  35. #define AL_STATIC_COPY_EXT 0x104A
  36. #define AL_DYNAMIC_WRITE_EXT 0x104B
  37. #define AL_DYNAMIC_READ_EXT 0x104C
  38. #define AL_DYNAMIC_COPY_EXT 0x104D
  39. typedef ALvoid (AL_APIENTRY*PFNALGENDATABUFFERSEXTPROC)(ALsizei n,ALuint *puiBuffers);
  40. typedef ALvoid (AL_APIENTRY*PFNALDELETEDATABUFFERSEXTPROC)(ALsizei n, const ALuint *puiBuffers);
  41. typedef ALboolean (AL_APIENTRY*PFNALISDATABUFFEREXTPROC)(ALuint uiBuffer);
  42. typedef ALvoid (AL_APIENTRY*PFNALDATABUFFERDATAEXTPROC)(ALuint buffer,const ALvoid *data,ALsizeiptrEXT size,ALenum usage);
  43. typedef ALvoid (AL_APIENTRY*PFNALDATABUFFERSUBDATAEXTPROC)(ALuint buffer, ALintptrEXT start, ALsizeiptrEXT length, const ALvoid *);
  44. typedef ALvoid (AL_APIENTRY*PFNALGETDATABUFFERSUBDATAEXTPROC)(ALuint buffer, ALintptrEXT start, ALsizeiptrEXT length, ALvoid *);
  45. typedef ALvoid (AL_APIENTRY*PFNALDATABUFFERFEXTPROC)(ALuint buffer, ALenum eParam, ALfloat flValue);
  46. typedef ALvoid (AL_APIENTRY*PFNALDATABUFFERFVEXTPROC)(ALuint buffer, ALenum eParam, const ALfloat* flValues);
  47. typedef ALvoid (AL_APIENTRY*PFNALDATABUFFERIEXTPROC)(ALuint buffer, ALenum eParam, ALint lValue);
  48. typedef ALvoid (AL_APIENTRY*PFNALDATABUFFERIVEXTPROC)(ALuint buffer, ALenum eParam, const ALint* plValues);
  49. typedef ALvoid (AL_APIENTRY*PFNALGETDATABUFFERFEXTPROC)(ALuint buffer, ALenum eParam, ALfloat *pflValue);
  50. typedef ALvoid (AL_APIENTRY*PFNALGETDATABUFFERFVEXTPROC)(ALuint buffer, ALenum eParam, ALfloat* pflValues);
  51. typedef ALvoid (AL_APIENTRY*PFNALGETDATABUFFERIEXTPROC)(ALuint buffer, ALenum eParam, ALint *plValue);
  52. typedef ALvoid (AL_APIENTRY*PFNALGETDATABUFFERIVEXTPROC)(ALuint buffer, ALenum eParam, ALint* plValues);
  53. typedef ALvoid (AL_APIENTRY*PFNALSELECTDATABUFFEREXTPROC)(ALenum target, ALuint uiBuffer);
  54. typedef ALvoid* (AL_APIENTRY*PFNALMAPDATABUFFEREXTPROC)(ALuint uiBuffer, ALintptrEXT start, ALsizeiptrEXT length, ALenum access);
  55. typedef ALvoid (AL_APIENTRY*PFNALUNMAPDATABUFFEREXTPROC)(ALuint uiBuffer);
  56. #ifdef AL_ALEXT_PROTOTYPES
  57. AL_API ALvoid AL_APIENTRY alGenDatabuffersEXT(ALsizei n,ALuint *puiBuffers);
  58. AL_API ALvoid AL_APIENTRY alDeleteDatabuffersEXT(ALsizei n, const ALuint *puiBuffers);
  59. AL_API ALboolean AL_APIENTRY alIsDatabufferEXT(ALuint uiBuffer);
  60. AL_API ALvoid AL_APIENTRY alDatabufferDataEXT(ALuint buffer,const ALvoid *data,ALsizeiptrEXT size,ALenum usage);
  61. AL_API ALvoid AL_APIENTRY alDatabufferSubDataEXT(ALuint buffer, ALintptrEXT start, ALsizeiptrEXT length, const ALvoid *data);
  62. AL_API ALvoid AL_APIENTRY alGetDatabufferSubDataEXT(ALuint buffer, ALintptrEXT start, ALsizeiptrEXT length, ALvoid *data);
  63. AL_API ALvoid AL_APIENTRY alDatabufferfEXT(ALuint buffer, ALenum eParam, ALfloat flValue);
  64. AL_API ALvoid AL_APIENTRY alDatabufferfvEXT(ALuint buffer, ALenum eParam, const ALfloat* flValues);
  65. AL_API ALvoid AL_APIENTRY alDatabufferiEXT(ALuint buffer, ALenum eParam, ALint lValue);
  66. AL_API ALvoid AL_APIENTRY alDatabufferivEXT(ALuint buffer, ALenum eParam, const ALint* plValues);
  67. AL_API ALvoid AL_APIENTRY alGetDatabufferfEXT(ALuint buffer, ALenum eParam, ALfloat *pflValue);
  68. AL_API ALvoid AL_APIENTRY alGetDatabufferfvEXT(ALuint buffer, ALenum eParam, ALfloat* pflValues);
  69. AL_API ALvoid AL_APIENTRY alGetDatabufferiEXT(ALuint buffer, ALenum eParam, ALint *plValue);
  70. AL_API ALvoid AL_APIENTRY alGetDatabufferivEXT(ALuint buffer, ALenum eParam, ALint* plValues);
  71. AL_API ALvoid AL_APIENTRY alSelectDatabufferEXT(ALenum target, ALuint uiBuffer);
  72. AL_API ALvoid* AL_APIENTRY alMapDatabufferEXT(ALuint uiBuffer, ALintptrEXT start, ALsizeiptrEXT length, ALenum access);
  73. AL_API ALvoid AL_APIENTRY alUnmapDatabufferEXT(ALuint uiBuffer);
  74. #endif
  75. #endif
  76. #ifndef AL_EXT_loop_points
  77. #define AL_EXT_loop_points 1
  78. #define AL_LOOP_POINTS 0x2015
  79. #endif
  80. #if defined(HAVE_STDINT_H)
  81. #include <stdint.h>
  82. typedef int64_t ALint64;
  83. typedef uint64_t ALuint64;
  84. #elif defined(HAVE___INT64)
  85. typedef __int64 ALint64;
  86. typedef unsigned __int64 ALuint64;
  87. #elif (SIZEOF_LONG == 8)
  88. typedef long ALint64;
  89. typedef unsigned long ALuint64;
  90. #elif (SIZEOF_LONG_LONG == 8)
  91. typedef long long ALint64;
  92. typedef unsigned long long ALuint64;
  93. #endif
  94. #ifdef HAVE_GCC_FORMAT
  95. #define PRINTF_STYLE(x, y) __attribute__((format(printf, (x), (y))))
  96. #else
  97. #define PRINTF_STYLE(x, y)
  98. #endif
  99. #ifdef _WIN32
  100. #ifndef _WIN32_WINNT
  101. #define _WIN32_WINNT 0x0500
  102. #endif
  103. #include <windows.h>
  104. typedef DWORD tls_type;
  105. #define tls_create(x) (*(x) = TlsAlloc())
  106. #define tls_delete(x) TlsFree((x))
  107. #define tls_get(x) TlsGetValue((x))
  108. #define tls_set(x, a) TlsSetValue((x), (a))
  109. #else
  110. #include <unistd.h>
  111. #include <assert.h>
  112. #include <pthread.h>
  113. #ifdef HAVE_PTHREAD_NP_H
  114. #include <pthread_np.h>
  115. #endif
  116. #include <sys/time.h>
  117. #include <time.h>
  118. #include <errno.h>
  119. #ifdef ANDROID
  120. #include <android/log.h>
  121. #endif
  122. #define IsBadWritePtr(a,b) ((a) == NULL && (b) != 0)
  123. typedef pthread_key_t tls_type;
  124. #define tls_create(x) pthread_key_create((x), NULL)
  125. #define tls_delete(x) pthread_key_delete((x))
  126. #define tls_get(x) pthread_getspecific((x))
  127. #define tls_set(x, a) pthread_setspecific((x), (a))
  128. typedef pthread_mutex_t CRITICAL_SECTION;
  129. static __inline void EnterCriticalSection(CRITICAL_SECTION *cs)
  130. {
  131. int ret;
  132. ret = pthread_mutex_lock(cs);
  133. assert(ret == 0);
  134. }
  135. static __inline void LeaveCriticalSection(CRITICAL_SECTION *cs)
  136. {
  137. int ret;
  138. ret = pthread_mutex_unlock(cs);
  139. assert(ret == 0);
  140. }
  141. static __inline void InitializeCriticalSection(CRITICAL_SECTION *cs)
  142. {
  143. pthread_mutexattr_t attrib;
  144. int ret;
  145. ret = pthread_mutexattr_init(&attrib);
  146. assert(ret == 0);
  147. ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
  148. #ifdef HAVE_PTHREAD_NP_H
  149. if(ret != 0)
  150. ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
  151. #endif
  152. assert(ret == 0);
  153. ret = pthread_mutex_init(cs, &attrib);
  154. assert(ret == 0);
  155. pthread_mutexattr_destroy(&attrib);
  156. }
  157. static __inline void DeleteCriticalSection(CRITICAL_SECTION *cs)
  158. {
  159. int ret;
  160. ret = pthread_mutex_destroy(cs);
  161. assert(ret == 0);
  162. }
  163. /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
  164. * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
  165. * Additionally, Win32 is supposed to measure the time since Windows started,
  166. * as opposed to the actual time. */
  167. static __inline ALuint timeGetTime(void)
  168. {
  169. int ret;
  170. #if _POSIX_TIMERS > 0
  171. struct timespec ts;
  172. ret = clock_gettime(CLOCK_REALTIME, &ts);
  173. assert(ret == 0);
  174. return ts.tv_nsec/1000000 + ts.tv_sec*1000;
  175. #else
  176. struct timeval tv;
  177. ret = gettimeofday(&tv, NULL);
  178. assert(ret == 0);
  179. return tv.tv_usec/1000 + tv.tv_sec*1000;
  180. #endif
  181. }
  182. static __inline void Sleep(ALuint t)
  183. {
  184. struct timespec tv, rem;
  185. tv.tv_nsec = (t*1000000)%1000000000;
  186. tv.tv_sec = t/1000;
  187. while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
  188. tv = rem;
  189. }
  190. #define min(x,y) (((x)<(y))?(x):(y))
  191. #define max(x,y) (((x)>(y))?(x):(y))
  192. #endif
  193. #include "alListener.h"
  194. #include "alu.h"
  195. #ifdef __cplusplus
  196. extern "C" {
  197. #endif
  198. #define SWMIXER_OUTPUT_RATE 44100
  199. #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
  200. #define AIRABSORBGAINDBHF (-0.05f)
  201. #define LOWPASSFREQCUTOFF (5000)
  202. #define DEFAULT_HEAD_DAMPEN (0.25f)
  203. // Find the next power-of-2 for non-power-of-2 numbers.
  204. static __inline ALuint NextPowerOf2(ALuint value)
  205. {
  206. ALuint powerOf2 = 1;
  207. if(value)
  208. {
  209. value--;
  210. while(value)
  211. {
  212. value >>= 1;
  213. powerOf2 <<= 1;
  214. }
  215. }
  216. return powerOf2;
  217. }
  218. typedef struct {
  219. ALCboolean (*OpenPlayback)(ALCdevice*, const ALCchar*);
  220. void (*ClosePlayback)(ALCdevice*);
  221. ALCboolean (*ResetPlayback)(ALCdevice*);
  222. void (*StopPlayback)(ALCdevice*);
  223. ALCboolean (*OpenCapture)(ALCdevice*, const ALCchar*);
  224. void (*CloseCapture)(ALCdevice*);
  225. void (*StartCapture)(ALCdevice*);
  226. void (*StopCapture)(ALCdevice*);
  227. void (*CaptureSamples)(ALCdevice*, void*, ALCuint);
  228. ALCuint (*AvailableSamples)(ALCdevice*);
  229. } BackendFuncs;
  230. enum {
  231. DEVICE_PROBE,
  232. ALL_DEVICE_PROBE,
  233. CAPTURE_DEVICE_PROBE
  234. };
  235. void alc_alsa_init(BackendFuncs *func_list);
  236. void alc_alsa_deinit(void);
  237. void alc_alsa_probe(int type);
  238. void alc_oss_init(BackendFuncs *func_list);
  239. void alc_oss_deinit(void);
  240. void alc_oss_probe(int type);
  241. void alc_solaris_init(BackendFuncs *func_list);
  242. void alc_solaris_deinit(void);
  243. void alc_solaris_probe(int type);
  244. void alcDSoundInit(BackendFuncs *func_list);
  245. void alcDSoundDeinit(void);
  246. void alcDSoundProbe(int type);
  247. void alcWinMMInit(BackendFuncs *FuncList);
  248. void alcWinMMDeinit(void);
  249. void alcWinMMProbe(int type);
  250. void alc_pa_init(BackendFuncs *func_list);
  251. void alc_pa_deinit(void);
  252. void alc_pa_probe(int type);
  253. void alc_wave_init(BackendFuncs *func_list);
  254. void alc_wave_deinit(void);
  255. void alc_wave_probe(int type);
  256. void alc_pulse_init(BackendFuncs *func_list);
  257. void alc_pulse_deinit(void);
  258. void alc_pulse_probe(int type);
  259. void alc_qt_init(BackendFuncs *func_list);
  260. void alc_qt_deinit(void);
  261. void alc_qt_probe(int type);
  262. void alc_null_init(BackendFuncs *func_list);
  263. void alc_null_deinit(void);
  264. void alc_null_probe(int type);
  265. typedef struct UIntMap {
  266. struct {
  267. ALuint key;
  268. ALvoid *value;
  269. } *array;
  270. ALsizei size;
  271. ALsizei maxsize;
  272. } UIntMap;
  273. void InitUIntMap(UIntMap *map);
  274. void ResetUIntMap(UIntMap *map);
  275. ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value);
  276. void RemoveUIntMapKey(UIntMap *map, ALuint key);
  277. static __inline ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
  278. {
  279. if(map->size > 0)
  280. {
  281. ALsizei low = 0;
  282. ALsizei high = map->size - 1;
  283. while(low < high)
  284. {
  285. ALsizei mid = low + (high-low)/2;
  286. if(map->array[mid].key < key)
  287. low = mid + 1;
  288. else
  289. high = mid;
  290. }
  291. if(map->array[low].key == key)
  292. return map->array[low].value;
  293. }
  294. return NULL;
  295. }
  296. struct ALCdevice_struct
  297. {
  298. ALCboolean Connected;
  299. ALboolean IsCaptureDevice;
  300. ALuint Frequency;
  301. ALuint UpdateSize;
  302. ALuint NumUpdates;
  303. ALenum Format;
  304. ALCchar *szDeviceName;
  305. ALCenum LastError;
  306. // Maximum number of sources that can be created
  307. ALuint MaxNoOfSources;
  308. // Maximum number of slots that can be created
  309. ALuint AuxiliaryEffectSlotMax;
  310. ALCuint NumMonoSources;
  311. ALCuint NumStereoSources;
  312. ALuint NumAuxSends;
  313. // Map of Buffers for this device
  314. UIntMap BufferMap;
  315. // Map of Effects for this device
  316. UIntMap EffectMap;
  317. // Map of Filters for this device
  318. UIntMap FilterMap;
  319. // Map of Databuffers for this device
  320. UIntMap DatabufferMap;
  321. // Stereo-to-binaural filter
  322. struct bs2b *Bs2b;
  323. ALCint Bs2bLevel;
  324. // Simulated dampening from head occlusion
  325. ALfloat HeadDampen;
  326. // Duplicate stereo sources on the side/rear channels
  327. ALboolean DuplicateStereo;
  328. // Dry path buffer mix
  329. float DryBuffer[BUFFERSIZE][OUTPUTCHANNELS];
  330. ALuint DevChannels[OUTPUTCHANNELS];
  331. ALfloat ChannelMatrix[OUTPUTCHANNELS][OUTPUTCHANNELS];
  332. Channel Speaker2Chan[OUTPUTCHANNELS];
  333. ALfloat PanningLUT[OUTPUTCHANNELS * LUT_NUM];
  334. ALuint NumChan;
  335. // Contexts created on this device
  336. ALCcontext **Contexts;
  337. ALuint NumContexts;
  338. BackendFuncs *Funcs;
  339. void *ExtraData; // For the backend's use
  340. ALCdevice *next;
  341. };
  342. #define ALCdevice_OpenPlayback(a,b) ((a)->Funcs->OpenPlayback((a), (b)))
  343. #define ALCdevice_ClosePlayback(a) ((a)->Funcs->ClosePlayback((a)))
  344. #define ALCdevice_ResetPlayback(a) ((a)->Funcs->ResetPlayback((a)))
  345. #define ALCdevice_StopPlayback(a) ((a)->Funcs->StopPlayback((a)))
  346. #define ALCdevice_OpenCapture(a,b) ((a)->Funcs->OpenCapture((a), (b)))
  347. #define ALCdevice_CloseCapture(a) ((a)->Funcs->CloseCapture((a)))
  348. #define ALCdevice_StartCapture(a) ((a)->Funcs->StartCapture((a)))
  349. #define ALCdevice_StopCapture(a) ((a)->Funcs->StopCapture((a)))
  350. #define ALCdevice_CaptureSamples(a,b,c) ((a)->Funcs->CaptureSamples((a), (b), (c)))
  351. #define ALCdevice_AvailableSamples(a) ((a)->Funcs->AvailableSamples((a)))
  352. struct ALCcontext_struct
  353. {
  354. ALlistener Listener;
  355. UIntMap SourceMap;
  356. UIntMap EffectSlotMap;
  357. struct ALdatabuffer *SampleSource;
  358. struct ALdatabuffer *SampleSink;
  359. ALenum LastError;
  360. ALboolean Suspended;
  361. ALenum DistanceModel;
  362. ALboolean SourceDistanceModel;
  363. ALfloat DopplerFactor;
  364. ALfloat DopplerVelocity;
  365. ALfloat flSpeedOfSound;
  366. struct ALsource **ActiveSources;
  367. ALsizei ActiveSourceCount;
  368. ALsizei MaxActiveSources;
  369. ALCdevice *Device;
  370. const ALCchar *ExtensionList;
  371. ALCcontext *next;
  372. };
  373. ALCvoid ReleaseALC(ALCvoid);
  374. void AppendDeviceList(const ALCchar *name);
  375. void AppendAllDeviceList(const ALCchar *name);
  376. void AppendCaptureDeviceList(const ALCchar *name);
  377. ALCvoid alcSetError(ALCdevice *device, ALenum errorCode);
  378. ALCvoid SuspendContext(ALCcontext *context);
  379. ALCvoid ProcessContext(ALCcontext *context);
  380. ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr);
  381. ALuint StopThread(ALvoid *thread);
  382. ALCcontext *GetContextSuspended(void);
  383. typedef struct RingBuffer RingBuffer;
  384. RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length);
  385. void DestroyRingBuffer(RingBuffer *ring);
  386. ALsizei RingBufferSize(RingBuffer *ring);
  387. void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len);
  388. void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len);
  389. void ReadALConfig(void);
  390. void FreeALConfig(void);
  391. int ConfigValueExists(const char *blockName, const char *keyName);
  392. const char *GetConfigValue(const char *blockName, const char *keyName, const char *def);
  393. int GetConfigValueInt(const char *blockName, const char *keyName, int def);
  394. float GetConfigValueFloat(const char *blockName, const char *keyName, float def);
  395. int GetConfigValueBool(const char *blockName, const char *keyName, int def);
  396. void SetRTPriority(void);
  397. void SetDefaultChannelOrder(ALCdevice *device);
  398. void SetDefaultWFXChannelOrder(ALCdevice *device);
  399. void al_print(const char *fname, unsigned int line, const char *fmt, ...)
  400. PRINTF_STYLE(3,4);
  401. #define AL_PRINT(...) al_print(__FILE__, __LINE__, __VA_ARGS__)
  402. #ifdef __cplusplus
  403. }
  404. #endif
  405. #endif