ALc.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <math.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <memory.h>
  25. #include <ctype.h>
  26. #include "qDebugWrapper.h"
  27. #include "alMain.h"
  28. #include "alSource.h"
  29. #include "AL/al.h"
  30. #include "AL/alc.h"
  31. #include "alThunk.h"
  32. #include "alSource.h"
  33. #include "alBuffer.h"
  34. #include "alAuxEffectSlot.h"
  35. #include "alDatabuffer.h"
  36. #include "bs2b.h"
  37. #include "alu.h"
  38. #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
  39. typedef struct BackendInfo {
  40. const char *name;
  41. void (*Init)(BackendFuncs*);
  42. void (*Deinit)(void);
  43. void (*Probe)(int);
  44. BackendFuncs Funcs;
  45. } BackendInfo;
  46. static BackendInfo BackendList[] = {
  47. #ifdef HAVE_PULSEAUDIO
  48. { "pulse", alc_pulse_init, alc_pulse_deinit, alc_pulse_probe, EmptyFuncs },
  49. #endif
  50. #ifdef HAVE_ALSA
  51. { "alsa", alc_alsa_init, alc_alsa_deinit, alc_alsa_probe, EmptyFuncs },
  52. #endif
  53. #ifdef HAVE_OSS
  54. { "oss", alc_oss_init, alc_oss_deinit, alc_oss_probe, EmptyFuncs },
  55. #endif
  56. #ifdef HAVE_SOLARIS
  57. { "solaris", alc_solaris_init, alc_solaris_deinit, alc_solaris_probe, EmptyFuncs },
  58. #endif
  59. #ifdef HAVE_DSOUND
  60. { "dsound", alcDSoundInit, alcDSoundDeinit, alcDSoundProbe, EmptyFuncs },
  61. #endif
  62. #ifdef HAVE_WINMM
  63. { "winmm", alcWinMMInit, alcWinMMDeinit, alcWinMMProbe, EmptyFuncs },
  64. #endif
  65. #ifdef HAVE_PORTAUDIO
  66. { "port", alc_pa_init, alc_pa_deinit, alc_pa_probe, EmptyFuncs },
  67. #endif
  68. #ifdef HAVE_QT
  69. { "qt", alc_qt_init, alc_qt_deinit, alc_qt_probe, EmptyFuncs },
  70. #endif
  71. { "null", alc_null_init, alc_null_deinit, alc_null_probe, EmptyFuncs },
  72. #ifdef HAVE_WAVE
  73. { "wave", alc_wave_init, alc_wave_deinit, alc_wave_probe, EmptyFuncs },
  74. #endif
  75. { NULL, NULL, NULL, NULL, EmptyFuncs }
  76. };
  77. #undef EmptyFuncs
  78. ///////////////////////////////////////////////////////
  79. #define ALC_EFX_MAJOR_VERSION 0x20001
  80. #define ALC_EFX_MINOR_VERSION 0x20002
  81. #define ALC_MAX_AUXILIARY_SENDS 0x20003
  82. ///////////////////////////////////////////////////////
  83. // STRING and EXTENSIONS
  84. typedef struct ALCfunction {
  85. const ALCchar *funcName;
  86. ALCvoid *address;
  87. } ALCfunction;
  88. typedef struct ALCenums {
  89. const ALCchar *enumName;
  90. ALCenum value;
  91. } ALCenums;
  92. static const ALCfunction alcFunctions[] = {
  93. { "alcCreateContext", (ALCvoid *) alcCreateContext },
  94. { "alcMakeContextCurrent", (ALCvoid *) alcMakeContextCurrent },
  95. { "alcProcessContext", (ALCvoid *) alcProcessContext },
  96. { "alcSuspendContext", (ALCvoid *) alcSuspendContext },
  97. { "alcDestroyContext", (ALCvoid *) alcDestroyContext },
  98. { "alcGetCurrentContext", (ALCvoid *) alcGetCurrentContext },
  99. { "alcGetContextsDevice", (ALCvoid *) alcGetContextsDevice },
  100. { "alcOpenDevice", (ALCvoid *) alcOpenDevice },
  101. { "alcCloseDevice", (ALCvoid *) alcCloseDevice },
  102. { "alcGetError", (ALCvoid *) alcGetError },
  103. { "alcIsExtensionPresent", (ALCvoid *) alcIsExtensionPresent },
  104. { "alcGetProcAddress", (ALCvoid *) alcGetProcAddress },
  105. { "alcGetEnumValue", (ALCvoid *) alcGetEnumValue },
  106. { "alcGetString", (ALCvoid *) alcGetString },
  107. { "alcGetIntegerv", (ALCvoid *) alcGetIntegerv },
  108. { "alcCaptureOpenDevice", (ALCvoid *) alcCaptureOpenDevice },
  109. { "alcCaptureCloseDevice", (ALCvoid *) alcCaptureCloseDevice },
  110. { "alcCaptureStart", (ALCvoid *) alcCaptureStart },
  111. { "alcCaptureStop", (ALCvoid *) alcCaptureStop },
  112. { "alcCaptureSamples", (ALCvoid *) alcCaptureSamples },
  113. { "alcSetThreadContext", (ALCvoid *) alcSetThreadContext },
  114. { "alcGetThreadContext", (ALCvoid *) alcGetThreadContext },
  115. { "alEnable", (ALCvoid *) alEnable },
  116. { "alDisable", (ALCvoid *) alDisable },
  117. { "alIsEnabled", (ALCvoid *) alIsEnabled },
  118. { "alGetString", (ALCvoid *) alGetString },
  119. { "alGetBooleanv", (ALCvoid *) alGetBooleanv },
  120. { "alGetIntegerv", (ALCvoid *) alGetIntegerv },
  121. { "alGetFloatv", (ALCvoid *) alGetFloatv },
  122. { "alGetDoublev", (ALCvoid *) alGetDoublev },
  123. { "alGetBoolean", (ALCvoid *) alGetBoolean },
  124. { "alGetInteger", (ALCvoid *) alGetInteger },
  125. { "alGetFloat", (ALCvoid *) alGetFloat },
  126. { "alGetDouble", (ALCvoid *) alGetDouble },
  127. { "alGetError", (ALCvoid *) alGetError },
  128. { "alIsExtensionPresent", (ALCvoid *) alIsExtensionPresent },
  129. { "alGetProcAddress", (ALCvoid *) alGetProcAddress },
  130. { "alGetEnumValue", (ALCvoid *) alGetEnumValue },
  131. { "alListenerf", (ALCvoid *) alListenerf },
  132. { "alListener3f", (ALCvoid *) alListener3f },
  133. { "alListenerfv", (ALCvoid *) alListenerfv },
  134. { "alListeneri", (ALCvoid *) alListeneri },
  135. { "alListener3i", (ALCvoid *) alListener3i },
  136. { "alListeneriv", (ALCvoid *) alListeneriv },
  137. { "alGetListenerf", (ALCvoid *) alGetListenerf },
  138. { "alGetListener3f", (ALCvoid *) alGetListener3f },
  139. { "alGetListenerfv", (ALCvoid *) alGetListenerfv },
  140. { "alGetListeneri", (ALCvoid *) alGetListeneri },
  141. { "alGetListener3i", (ALCvoid *) alGetListener3i },
  142. { "alGetListeneriv", (ALCvoid *) alGetListeneriv },
  143. { "alGenSources", (ALCvoid *) alGenSources },
  144. { "alDeleteSources", (ALCvoid *) alDeleteSources },
  145. { "alIsSource", (ALCvoid *) alIsSource },
  146. { "alSourcef", (ALCvoid *) alSourcef },
  147. { "alSource3f", (ALCvoid *) alSource3f },
  148. { "alSourcefv", (ALCvoid *) alSourcefv },
  149. { "alSourcei", (ALCvoid *) alSourcei },
  150. { "alSource3i", (ALCvoid *) alSource3i },
  151. { "alSourceiv", (ALCvoid *) alSourceiv },
  152. { "alGetSourcef", (ALCvoid *) alGetSourcef },
  153. { "alGetSource3f", (ALCvoid *) alGetSource3f },
  154. { "alGetSourcefv", (ALCvoid *) alGetSourcefv },
  155. { "alGetSourcei", (ALCvoid *) alGetSourcei },
  156. { "alGetSource3i", (ALCvoid *) alGetSource3i },
  157. { "alGetSourceiv", (ALCvoid *) alGetSourceiv },
  158. { "alSourcePlayv", (ALCvoid *) alSourcePlayv },
  159. { "alSourceStopv", (ALCvoid *) alSourceStopv },
  160. { "alSourceRewindv", (ALCvoid *) alSourceRewindv },
  161. { "alSourcePausev", (ALCvoid *) alSourcePausev },
  162. { "alSourcePlay", (ALCvoid *) alSourcePlay },
  163. { "alSourceStop", (ALCvoid *) alSourceStop },
  164. { "alSourceRewind", (ALCvoid *) alSourceRewind },
  165. { "alSourcePause", (ALCvoid *) alSourcePause },
  166. { "alSourceQueueBuffers", (ALCvoid *) alSourceQueueBuffers },
  167. { "alSourceUnqueueBuffers", (ALCvoid *) alSourceUnqueueBuffers },
  168. { "alGenBuffers", (ALCvoid *) alGenBuffers },
  169. { "alDeleteBuffers", (ALCvoid *) alDeleteBuffers },
  170. { "alIsBuffer", (ALCvoid *) alIsBuffer },
  171. { "alBufferData", (ALCvoid *) alBufferData },
  172. { "alBufferf", (ALCvoid *) alBufferf },
  173. { "alBuffer3f", (ALCvoid *) alBuffer3f },
  174. { "alBufferfv", (ALCvoid *) alBufferfv },
  175. { "alBufferi", (ALCvoid *) alBufferi },
  176. { "alBuffer3i", (ALCvoid *) alBuffer3i },
  177. { "alBufferiv", (ALCvoid *) alBufferiv },
  178. { "alGetBufferf", (ALCvoid *) alGetBufferf },
  179. { "alGetBuffer3f", (ALCvoid *) alGetBuffer3f },
  180. { "alGetBufferfv", (ALCvoid *) alGetBufferfv },
  181. { "alGetBufferi", (ALCvoid *) alGetBufferi },
  182. { "alGetBuffer3i", (ALCvoid *) alGetBuffer3i },
  183. { "alGetBufferiv", (ALCvoid *) alGetBufferiv },
  184. { "alDopplerFactor", (ALCvoid *) alDopplerFactor },
  185. { "alDopplerVelocity", (ALCvoid *) alDopplerVelocity },
  186. { "alSpeedOfSound", (ALCvoid *) alSpeedOfSound },
  187. { "alDistanceModel", (ALCvoid *) alDistanceModel },
  188. { "alGenFilters", (ALCvoid *) alGenFilters },
  189. { "alDeleteFilters", (ALCvoid *) alDeleteFilters },
  190. { "alIsFilter", (ALCvoid *) alIsFilter },
  191. { "alFilteri", (ALCvoid *) alFilteri },
  192. { "alFilteriv", (ALCvoid *) alFilteriv },
  193. { "alFilterf", (ALCvoid *) alFilterf },
  194. { "alFilterfv", (ALCvoid *) alFilterfv },
  195. { "alGetFilteri", (ALCvoid *) alGetFilteri },
  196. { "alGetFilteriv", (ALCvoid *) alGetFilteriv },
  197. { "alGetFilterf", (ALCvoid *) alGetFilterf },
  198. { "alGetFilterfv", (ALCvoid *) alGetFilterfv },
  199. { "alGenEffects", (ALCvoid *) alGenEffects },
  200. { "alDeleteEffects", (ALCvoid *) alDeleteEffects },
  201. { "alIsEffect", (ALCvoid *) alIsEffect },
  202. { "alEffecti", (ALCvoid *) alEffecti },
  203. { "alEffectiv", (ALCvoid *) alEffectiv },
  204. { "alEffectf", (ALCvoid *) alEffectf },
  205. { "alEffectfv", (ALCvoid *) alEffectfv },
  206. { "alGetEffecti", (ALCvoid *) alGetEffecti },
  207. { "alGetEffectiv", (ALCvoid *) alGetEffectiv },
  208. { "alGetEffectf", (ALCvoid *) alGetEffectf },
  209. { "alGetEffectfv", (ALCvoid *) alGetEffectfv },
  210. { "alGenAuxiliaryEffectSlots", (ALCvoid *) alGenAuxiliaryEffectSlots},
  211. { "alDeleteAuxiliaryEffectSlots",(ALCvoid *) alDeleteAuxiliaryEffectSlots},
  212. { "alIsAuxiliaryEffectSlot", (ALCvoid *) alIsAuxiliaryEffectSlot },
  213. { "alAuxiliaryEffectSloti", (ALCvoid *) alAuxiliaryEffectSloti },
  214. { "alAuxiliaryEffectSlotiv", (ALCvoid *) alAuxiliaryEffectSlotiv },
  215. { "alAuxiliaryEffectSlotf", (ALCvoid *) alAuxiliaryEffectSlotf },
  216. { "alAuxiliaryEffectSlotfv", (ALCvoid *) alAuxiliaryEffectSlotfv },
  217. { "alGetAuxiliaryEffectSloti", (ALCvoid *) alGetAuxiliaryEffectSloti},
  218. { "alGetAuxiliaryEffectSlotiv", (ALCvoid *) alGetAuxiliaryEffectSlotiv},
  219. { "alGetAuxiliaryEffectSlotf", (ALCvoid *) alGetAuxiliaryEffectSlotf},
  220. { "alGetAuxiliaryEffectSlotfv", (ALCvoid *) alGetAuxiliaryEffectSlotfv},
  221. { "alBufferSubDataEXT", (ALCvoid *) alBufferSubDataEXT },
  222. { "alGenDatabuffersEXT", (ALCvoid *) alGenDatabuffersEXT },
  223. { "alDeleteDatabuffersEXT", (ALCvoid *) alDeleteDatabuffersEXT },
  224. { "alIsDatabufferEXT", (ALCvoid *) alIsDatabufferEXT },
  225. { "alDatabufferDataEXT", (ALCvoid *) alDatabufferDataEXT },
  226. { "alDatabufferSubDataEXT", (ALCvoid *) alDatabufferSubDataEXT },
  227. { "alGetDatabufferSubDataEXT", (ALCvoid *) alGetDatabufferSubDataEXT},
  228. { "alDatabufferfEXT", (ALCvoid *) alDatabufferfEXT },
  229. { "alDatabufferfvEXT", (ALCvoid *) alDatabufferfvEXT },
  230. { "alDatabufferiEXT", (ALCvoid *) alDatabufferiEXT },
  231. { "alDatabufferivEXT", (ALCvoid *) alDatabufferivEXT },
  232. { "alGetDatabufferfEXT", (ALCvoid *) alGetDatabufferfEXT },
  233. { "alGetDatabufferfvEXT", (ALCvoid *) alGetDatabufferfvEXT },
  234. { "alGetDatabufferiEXT", (ALCvoid *) alGetDatabufferiEXT },
  235. { "alGetDatabufferivEXT", (ALCvoid *) alGetDatabufferivEXT },
  236. { "alSelectDatabufferEXT", (ALCvoid *) alSelectDatabufferEXT },
  237. { "alMapDatabufferEXT", (ALCvoid *) alMapDatabufferEXT },
  238. { "alUnmapDatabufferEXT", (ALCvoid *) alUnmapDatabufferEXT },
  239. { NULL, (ALCvoid *) NULL }
  240. };
  241. static const ALCenums enumeration[] = {
  242. // Types
  243. { "ALC_INVALID", ALC_INVALID },
  244. { "ALC_FALSE", ALC_FALSE },
  245. { "ALC_TRUE", ALC_TRUE },
  246. // ALC Properties
  247. { "ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
  248. { "ALC_MINOR_VERSION", ALC_MINOR_VERSION },
  249. { "ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
  250. { "ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
  251. { "ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
  252. { "ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
  253. { "ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
  254. { "ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
  255. { "ALC_EXTENSIONS", ALC_EXTENSIONS },
  256. { "ALC_FREQUENCY", ALC_FREQUENCY },
  257. { "ALC_REFRESH", ALC_REFRESH },
  258. { "ALC_SYNC", ALC_SYNC },
  259. { "ALC_MONO_SOURCES", ALC_MONO_SOURCES },
  260. { "ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
  261. { "ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
  262. { "ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
  263. { "ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
  264. { "ALC_CONNECTED", ALC_CONNECTED },
  265. // EFX Properties
  266. { "ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
  267. { "ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
  268. { "ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
  269. // ALC Error Message
  270. { "ALC_NO_ERROR", ALC_NO_ERROR },
  271. { "ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
  272. { "ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
  273. { "ALC_INVALID_ENUM", ALC_INVALID_ENUM },
  274. { "ALC_INVALID_VALUE", ALC_INVALID_VALUE },
  275. { "ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
  276. { NULL, (ALCenum)0 }
  277. };
  278. // Error strings
  279. static const ALCchar alcNoError[] = "No Error";
  280. static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
  281. static const ALCchar alcErrInvalidContext[] = "Invalid Context";
  282. static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
  283. static const ALCchar alcErrInvalidValue[] = "Invalid Value";
  284. static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
  285. /* Device lists. Sizes only include the first ending null character, not the
  286. * second */
  287. static ALCchar *alcDeviceList;
  288. static size_t alcDeviceListSize;
  289. static ALCchar *alcAllDeviceList;
  290. static size_t alcAllDeviceListSize;
  291. static ALCchar *alcCaptureDeviceList;
  292. static size_t alcCaptureDeviceListSize;
  293. // Default is always the first in the list
  294. static ALCchar *alcDefaultDeviceSpecifier;
  295. static ALCchar *alcDefaultAllDeviceSpecifier;
  296. static ALCchar *alcCaptureDefaultDeviceSpecifier;
  297. static const ALCchar alcNoDeviceExtList[] =
  298. "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
  299. "ALC_EXT_thread_local_context";
  300. static const ALCchar alcExtensionList[] =
  301. "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
  302. "ALC_EXT_disconnect ALC_EXT_EFX ALC_EXT_thread_local_context";
  303. static const ALCint alcMajorVersion = 1;
  304. static const ALCint alcMinorVersion = 1;
  305. static const ALCint alcEFXMajorVersion = 1;
  306. static const ALCint alcEFXMinorVersion = 0;
  307. ///////////////////////////////////////////////////////
  308. ///////////////////////////////////////////////////////
  309. // Global Variables
  310. static ALCdevice *g_pDeviceList = NULL;
  311. static ALCuint g_ulDeviceCount = 0;
  312. static CRITICAL_SECTION g_csMutex;
  313. // Context List
  314. static ALCcontext *g_pContextList = NULL;
  315. static ALCuint g_ulContextCount = 0;
  316. // Thread-local current context
  317. static tls_type LocalContext;
  318. // Process-wide current context
  319. static ALCcontext *GlobalContext;
  320. // Context Error
  321. static ALCenum g_eLastNullDeviceError = ALC_NO_ERROR;
  322. // Default context extensions
  323. static const ALchar alExtList[] =
  324. "AL_EXTX_buffer_sub_data AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE "
  325. "AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXTX_loop_points "
  326. "AL_EXT_MCFORMATS AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET "
  327. "AL_EXTX_sample_buffer_object AL_EXT_source_distance_model "
  328. "AL_LOKI_quadriphonic";
  329. // Mixing Priority Level
  330. static ALint RTPrioLevel;
  331. // Resampler Quality
  332. resampler_t DefaultResampler;
  333. // Output Log File
  334. static FILE *LogFile;
  335. ///////////////////////////////////////////////////////
  336. ///////////////////////////////////////////////////////
  337. // ALC Related helper functions
  338. #ifdef _WIN32
  339. static void alc_init(void);
  340. static void alc_deinit(void);
  341. BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
  342. {
  343. (void)lpReserved;
  344. // Perform actions based on the reason for calling.
  345. switch(ul_reason_for_call)
  346. {
  347. case DLL_PROCESS_ATTACH:
  348. DisableThreadLibraryCalls(hModule);
  349. alc_init();
  350. break;
  351. case DLL_PROCESS_DETACH:
  352. alc_deinit();
  353. break;
  354. }
  355. return TRUE;
  356. }
  357. #else
  358. #ifdef HAVE_GCC_DESTRUCTOR
  359. static void alc_init(void) __attribute__((constructor));
  360. static void alc_deinit(void) __attribute__((destructor));
  361. #endif
  362. #endif
  363. static void alc_init(void)
  364. {
  365. int i;
  366. const char *devs, *str;
  367. str = getenv("ALSOFT_LOGFILE");
  368. if(str && str[0])
  369. {
  370. LogFile = fopen(str, "w");
  371. if(!LogFile)
  372. fprintf(stderr, "AL lib: Failed to open log file '%s'\n", str);
  373. }
  374. if(!LogFile)
  375. LogFile = stderr;
  376. InitializeCriticalSection(&g_csMutex);
  377. ALTHUNK_INIT();
  378. ReadALConfig();
  379. tls_create(&LocalContext);
  380. RTPrioLevel = GetConfigValueInt(NULL, "rt-prio", 0);
  381. DefaultResampler = GetConfigValueInt(NULL, "resampler", RESAMPLER_DEFAULT);
  382. if(DefaultResampler >= RESAMPLER_MAX || DefaultResampler <= RESAMPLER_MIN)
  383. DefaultResampler = RESAMPLER_DEFAULT;
  384. devs = GetConfigValue(NULL, "drivers", "");
  385. if(devs[0])
  386. {
  387. int n;
  388. size_t len;
  389. const char *next = devs;
  390. int endlist, delitem;
  391. i = 0;
  392. do {
  393. devs = next;
  394. next = strchr(devs, ',');
  395. delitem = (devs[0] == '-');
  396. if(devs[0] == '-') devs++;
  397. if(!devs[0] || devs[0] == ',')
  398. {
  399. endlist = 0;
  400. continue;
  401. }
  402. endlist = 1;
  403. len = (next ? ((size_t)(next-devs)) : strlen(devs));
  404. for(n = i;BackendList[n].Init;n++)
  405. {
  406. if(len == strlen(BackendList[n].name) &&
  407. strncmp(BackendList[n].name, devs, len) == 0)
  408. {
  409. if(delitem)
  410. {
  411. do {
  412. BackendList[n] = BackendList[n+1];
  413. ++n;
  414. } while(BackendList[n].Init);
  415. }
  416. else
  417. {
  418. BackendInfo Bkp = BackendList[n];
  419. while(n > i)
  420. {
  421. BackendList[n] = BackendList[n-1];
  422. --n;
  423. }
  424. BackendList[n] = Bkp;
  425. i++;
  426. }
  427. break;
  428. }
  429. }
  430. } while(next++);
  431. if(endlist)
  432. {
  433. BackendList[i].name = NULL;
  434. BackendList[i].Init = NULL;
  435. BackendList[i].Deinit = NULL;
  436. BackendList[i].Probe = NULL;
  437. }
  438. }
  439. for(i = 0;BackendList[i].Init;i++)
  440. BackendList[i].Init(&BackendList[i].Funcs);
  441. str = GetConfigValue(NULL, "excludefx", "");
  442. if(str[0])
  443. {
  444. const struct {
  445. const char *name;
  446. int type;
  447. } EffectList[] = {
  448. { "eaxreverb", EAXREVERB },
  449. { "reverb", REVERB },
  450. { "echo", ECHO },
  451. { "modulator", MODULATOR },
  452. { NULL, 0 }
  453. };
  454. int n;
  455. size_t len;
  456. const char *next = str;
  457. do {
  458. str = next;
  459. next = strchr(str, ',');
  460. if(!str[0] || next == str)
  461. continue;
  462. len = (next ? ((size_t)(next-str)) : strlen(str));
  463. for(n = 0;EffectList[n].name;n++)
  464. {
  465. if(len == strlen(EffectList[n].name) &&
  466. strncmp(EffectList[n].name, str, len) == 0)
  467. DisabledEffects[EffectList[n].type] = AL_TRUE;
  468. }
  469. } while(next++);
  470. }
  471. }
  472. static void alc_deinit(void)
  473. {
  474. int i;
  475. ReleaseALC();
  476. for(i = 0;BackendList[i].Deinit;i++)
  477. BackendList[i].Deinit();
  478. if(LogFile != stderr)
  479. fclose(LogFile);
  480. LogFile = NULL;
  481. tls_delete(LocalContext);
  482. FreeALConfig();
  483. ALTHUNK_EXIT();
  484. DeleteCriticalSection(&g_csMutex);
  485. }
  486. static void ProbeDeviceList()
  487. {
  488. ALint i;
  489. free(alcDeviceList); alcDeviceList = NULL;
  490. alcDeviceListSize = 0;
  491. for(i = 0;BackendList[i].Probe;i++)
  492. BackendList[i].Probe(DEVICE_PROBE);
  493. }
  494. static void ProbeAllDeviceList()
  495. {
  496. ALint i;
  497. free(alcAllDeviceList); alcAllDeviceList = NULL;
  498. alcAllDeviceListSize = 0;
  499. for(i = 0;BackendList[i].Probe;i++)
  500. BackendList[i].Probe(ALL_DEVICE_PROBE);
  501. }
  502. static void ProbeCaptureDeviceList()
  503. {
  504. ALint i;
  505. free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
  506. alcCaptureDeviceListSize = 0;
  507. for(i = 0;BackendList[i].Probe;i++)
  508. BackendList[i].Probe(CAPTURE_DEVICE_PROBE);
  509. }
  510. #define DECL_APPEND_LIST_FUNC(type) \
  511. void Append##type##List(const ALCchar *name) \
  512. { \
  513. size_t len = strlen(name); \
  514. void *temp; \
  515. \
  516. if(len == 0) \
  517. return; \
  518. \
  519. temp = realloc(alc##type##List, alc##type##ListSize + len + 2); \
  520. if(!temp) \
  521. { \
  522. AL_PRINT("Realloc failed to add %s!\n", name); \
  523. return; \
  524. } \
  525. alc##type##List = temp; \
  526. sprintf(alc##type##List+alc##type##ListSize, "%s", name); \
  527. alc##type##ListSize += len+1; \
  528. alc##type##List[alc##type##ListSize] = 0; \
  529. }
  530. DECL_APPEND_LIST_FUNC(Device)
  531. DECL_APPEND_LIST_FUNC(AllDevice)
  532. DECL_APPEND_LIST_FUNC(CaptureDevice)
  533. void al_print(const char *fname, unsigned int line, const char *fmt, ...)
  534. {
  535. const char *fn;
  536. char str[256];
  537. int i;
  538. fn = strrchr(fname, '/');
  539. if(!fn) fn = strrchr(fname, '\\');;
  540. if(!fn) fn = fname;
  541. else fn += 1;
  542. i = snprintf(str, sizeof(str), "AL lib: %s:%d: ", fn, line);
  543. if(i < (int)sizeof(str) && i > 0)
  544. {
  545. va_list ap;
  546. va_start(ap, fmt);
  547. vsnprintf(str+i, sizeof(str)-i, fmt, ap);
  548. va_end(ap);
  549. }
  550. str[sizeof(str)-1] = 0;
  551. #ifdef ANDROID
  552. __android_log_write(ANDROID_LOG_WARN, "OpenAL", str);
  553. #elif defined(_QT)
  554. qPrint(str);
  555. #else
  556. fprintf(LogFile, "%s", str);
  557. fflush(LogFile);
  558. #endif
  559. }
  560. void SetRTPriority(void)
  561. {
  562. ALboolean failed;
  563. #ifdef _WIN32
  564. if(RTPrioLevel > 0)
  565. failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  566. else
  567. failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  568. #elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
  569. struct sched_param param;
  570. if(RTPrioLevel > 0)
  571. {
  572. /* Use the minimum real-time priority possible for now (on Linux this
  573. * should be 1 for SCHED_RR) */
  574. param.sched_priority = sched_get_priority_min(SCHED_RR);
  575. failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
  576. }
  577. else
  578. {
  579. param.sched_priority = 0;
  580. failed = !!pthread_setschedparam(pthread_self(), SCHED_OTHER, &param);
  581. }
  582. #else
  583. /* Real-time priority not available */
  584. failed = (RTPrioLevel>0);
  585. #endif
  586. if(failed)
  587. AL_PRINT("Failed to set priority level for thread\n");
  588. }
  589. void InitUIntMap(UIntMap *map)
  590. {
  591. map->array = NULL;
  592. map->size = 0;
  593. map->maxsize = 0;
  594. }
  595. void ResetUIntMap(UIntMap *map)
  596. {
  597. free(map->array);
  598. map->array = NULL;
  599. map->size = 0;
  600. map->maxsize = 0;
  601. }
  602. ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
  603. {
  604. ALsizei pos = 0;
  605. if(map->size > 0)
  606. {
  607. ALsizei low = 0;
  608. ALsizei high = map->size - 1;
  609. while(low < high)
  610. {
  611. ALsizei mid = low + (high-low)/2;
  612. if(map->array[mid].key < key)
  613. low = mid + 1;
  614. else
  615. high = mid;
  616. }
  617. if(map->array[low].key < key)
  618. low++;
  619. pos = low;
  620. }
  621. if(pos == map->size || map->array[pos].key != key)
  622. {
  623. if(map->size == map->maxsize)
  624. {
  625. ALvoid *temp;
  626. ALsizei newsize;
  627. newsize = (map->maxsize ? (map->maxsize<<1) : 4);
  628. if(newsize < map->maxsize)
  629. return AL_OUT_OF_MEMORY;
  630. temp = realloc(map->array, newsize*sizeof(map->array[0]));
  631. if(!temp) return AL_OUT_OF_MEMORY;
  632. map->array = temp;
  633. map->maxsize = newsize;
  634. }
  635. map->size++;
  636. if(pos < map->size-1)
  637. memmove(&map->array[pos+1], &map->array[pos],
  638. (map->size-1-pos)*sizeof(map->array[0]));
  639. }
  640. map->array[pos].key = key;
  641. map->array[pos].value = value;
  642. return AL_NO_ERROR;
  643. }
  644. void RemoveUIntMapKey(UIntMap *map, ALuint key)
  645. {
  646. if(map->size > 0)
  647. {
  648. ALsizei low = 0;
  649. ALsizei high = map->size - 1;
  650. while(low < high)
  651. {
  652. ALsizei mid = low + (high-low)/2;
  653. if(map->array[mid].key < key)
  654. low = mid + 1;
  655. else
  656. high = mid;
  657. }
  658. if(map->array[low].key == key)
  659. {
  660. if(low < map->size-1)
  661. memmove(&map->array[low], &map->array[low+1],
  662. (map->size-1-low)*sizeof(map->array[0]));
  663. map->size--;
  664. }
  665. }
  666. }
  667. /*
  668. IsDevice
  669. Check pDevice is a valid Device pointer
  670. */
  671. static ALCboolean IsDevice(ALCdevice *pDevice)
  672. {
  673. ALCdevice *pTempDevice;
  674. SuspendContext(NULL);
  675. pTempDevice = g_pDeviceList;
  676. while(pTempDevice && pTempDevice != pDevice)
  677. pTempDevice = pTempDevice->next;
  678. ProcessContext(NULL);
  679. return (pTempDevice ? ALC_TRUE : ALC_FALSE);
  680. }
  681. /*
  682. IsContext
  683. Check pContext is a valid Context pointer
  684. */
  685. static ALCboolean IsContext(ALCcontext *pContext)
  686. {
  687. ALCcontext *pTempContext;
  688. SuspendContext(NULL);
  689. pTempContext = g_pContextList;
  690. while (pTempContext && pTempContext != pContext)
  691. pTempContext = pTempContext->next;
  692. ProcessContext(NULL);
  693. return (pTempContext ? ALC_TRUE : ALC_FALSE);
  694. }
  695. /*
  696. alcSetError
  697. Store latest ALC Error
  698. */
  699. ALCvoid alcSetError(ALCdevice *device, ALenum errorCode)
  700. {
  701. if(IsDevice(device))
  702. device->LastError = errorCode;
  703. else
  704. g_eLastNullDeviceError = errorCode;
  705. }
  706. /*
  707. SuspendContext
  708. Thread-safe entry
  709. */
  710. ALCvoid SuspendContext(ALCcontext *pContext)
  711. {
  712. (void)pContext;
  713. EnterCriticalSection(&g_csMutex);
  714. }
  715. /*
  716. ProcessContext
  717. Thread-safe exit
  718. */
  719. ALCvoid ProcessContext(ALCcontext *pContext)
  720. {
  721. (void)pContext;
  722. LeaveCriticalSection(&g_csMutex);
  723. }
  724. /*
  725. GetContextSuspended
  726. Returns the currently active Context, in a locked state
  727. */
  728. ALCcontext *GetContextSuspended(void)
  729. {
  730. ALCcontext *pContext = NULL;
  731. SuspendContext(NULL);
  732. pContext = tls_get(LocalContext);
  733. if(pContext && !IsContext(pContext))
  734. {
  735. tls_set(LocalContext, NULL);
  736. pContext = NULL;
  737. }
  738. if(!pContext)
  739. pContext = GlobalContext;
  740. if(pContext)
  741. SuspendContext(pContext);
  742. ProcessContext(NULL);
  743. return pContext;
  744. }
  745. /*
  746. InitContext
  747. Initialize Context variables
  748. */
  749. static ALvoid InitContext(ALCcontext *pContext)
  750. {
  751. //Initialise listener
  752. pContext->Listener.Gain = 1.0f;
  753. pContext->Listener.MetersPerUnit = 1.0f;
  754. pContext->Listener.Position[0] = 0.0f;
  755. pContext->Listener.Position[1] = 0.0f;
  756. pContext->Listener.Position[2] = 0.0f;
  757. pContext->Listener.Velocity[0] = 0.0f;
  758. pContext->Listener.Velocity[1] = 0.0f;
  759. pContext->Listener.Velocity[2] = 0.0f;
  760. pContext->Listener.Forward[0] = 0.0f;
  761. pContext->Listener.Forward[1] = 0.0f;
  762. pContext->Listener.Forward[2] = -1.0f;
  763. pContext->Listener.Up[0] = 0.0f;
  764. pContext->Listener.Up[1] = 1.0f;
  765. pContext->Listener.Up[2] = 0.0f;
  766. //Validate pContext
  767. pContext->LastError = AL_NO_ERROR;
  768. pContext->Suspended = AL_FALSE;
  769. pContext->ActiveSourceCount = 0;
  770. InitUIntMap(&pContext->SourceMap);
  771. InitUIntMap(&pContext->EffectSlotMap);
  772. //Set globals
  773. pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
  774. pContext->SourceDistanceModel = AL_FALSE;
  775. pContext->DopplerFactor = 1.0f;
  776. pContext->DopplerVelocity = 1.0f;
  777. pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
  778. pContext->ExtensionList = alExtList;
  779. }
  780. /*
  781. ExitContext
  782. Clean up Context, destroy any remaining Sources
  783. */
  784. static ALCvoid ExitContext(ALCcontext *pContext)
  785. {
  786. //Invalidate context
  787. pContext->LastError = AL_NO_ERROR;
  788. }
  789. ///////////////////////////////////////////////////////
  790. ///////////////////////////////////////////////////////
  791. // ALC Functions calls
  792. // This should probably move to another c file but for now ...
  793. ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
  794. {
  795. ALCboolean DeviceFound = ALC_FALSE;
  796. ALCdevice *device = NULL;
  797. ALCint i;
  798. if(SampleSize <= 0)
  799. {
  800. alcSetError(NULL, ALC_INVALID_VALUE);
  801. return NULL;
  802. }
  803. if(deviceName && !deviceName[0])
  804. deviceName = NULL;
  805. device = calloc(1, sizeof(ALCdevice));
  806. if(!device)
  807. {
  808. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  809. return NULL;
  810. }
  811. //Validate device
  812. device->Connected = ALC_TRUE;
  813. device->IsCaptureDevice = AL_TRUE;
  814. device->szDeviceName = NULL;
  815. device->Frequency = frequency;
  816. device->Format = format;
  817. device->UpdateSize = SampleSize;
  818. device->NumUpdates = 1;
  819. SuspendContext(NULL);
  820. for(i = 0;BackendList[i].Init;i++)
  821. {
  822. device->Funcs = &BackendList[i].Funcs;
  823. if(ALCdevice_OpenCapture(device, deviceName))
  824. {
  825. device->next = g_pDeviceList;
  826. g_pDeviceList = device;
  827. g_ulDeviceCount++;
  828. DeviceFound = ALC_TRUE;
  829. break;
  830. }
  831. }
  832. ProcessContext(NULL);
  833. if(!DeviceFound)
  834. {
  835. alcSetError(NULL, ALC_INVALID_VALUE);
  836. free(device);
  837. device = NULL;
  838. }
  839. return device;
  840. }
  841. ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
  842. {
  843. ALCdevice **list;
  844. if(!IsDevice(pDevice) || !pDevice->IsCaptureDevice)
  845. {
  846. alcSetError(pDevice, ALC_INVALID_DEVICE);
  847. return ALC_FALSE;
  848. }
  849. SuspendContext(NULL);
  850. list = &g_pDeviceList;
  851. while(*list != pDevice)
  852. list = &(*list)->next;
  853. *list = (*list)->next;
  854. g_ulDeviceCount--;
  855. ProcessContext(NULL);
  856. ALCdevice_CloseCapture(pDevice);
  857. free(pDevice->szDeviceName);
  858. pDevice->szDeviceName = NULL;
  859. free(pDevice);
  860. return ALC_TRUE;
  861. }
  862. ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
  863. {
  864. SuspendContext(NULL);
  865. if(!IsDevice(device) || !device->IsCaptureDevice)
  866. alcSetError(device, ALC_INVALID_DEVICE);
  867. else if(device->Connected)
  868. ALCdevice_StartCapture(device);
  869. ProcessContext(NULL);
  870. }
  871. ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
  872. {
  873. SuspendContext(NULL);
  874. if(!IsDevice(device) || !device->IsCaptureDevice)
  875. alcSetError(device, ALC_INVALID_DEVICE);
  876. else
  877. ALCdevice_StopCapture(device);
  878. ProcessContext(NULL);
  879. }
  880. ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
  881. {
  882. SuspendContext(NULL);
  883. if(!IsDevice(device) || !device->IsCaptureDevice)
  884. alcSetError(device, ALC_INVALID_DEVICE);
  885. else
  886. ALCdevice_CaptureSamples(device, buffer, samples);
  887. ProcessContext(NULL);
  888. }
  889. /*
  890. alcGetError
  891. Return last ALC generated error code
  892. */
  893. ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
  894. {
  895. ALCenum errorCode;
  896. if(IsDevice(device))
  897. {
  898. errorCode = device->LastError;
  899. device->LastError = ALC_NO_ERROR;
  900. }
  901. else
  902. {
  903. errorCode = g_eLastNullDeviceError;
  904. g_eLastNullDeviceError = ALC_NO_ERROR;
  905. }
  906. return errorCode;
  907. }
  908. /*
  909. alcSuspendContext
  910. Not functional
  911. */
  912. ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *pContext)
  913. {
  914. SuspendContext(NULL);
  915. if(IsContext(pContext))
  916. pContext->Suspended = AL_TRUE;
  917. ProcessContext(NULL);
  918. }
  919. /*
  920. alcProcessContext
  921. Not functional
  922. */
  923. ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *pContext)
  924. {
  925. SuspendContext(NULL);
  926. if(IsContext(pContext))
  927. pContext->Suspended = AL_FALSE;
  928. ProcessContext(NULL);
  929. }
  930. /*
  931. alcGetString
  932. Returns information about the Device, and error strings
  933. */
  934. ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
  935. {
  936. const ALCchar *value = NULL;
  937. switch (param)
  938. {
  939. case ALC_NO_ERROR:
  940. value = alcNoError;
  941. break;
  942. case ALC_INVALID_ENUM:
  943. value = alcErrInvalidEnum;
  944. break;
  945. case ALC_INVALID_VALUE:
  946. value = alcErrInvalidValue;
  947. break;
  948. case ALC_INVALID_DEVICE:
  949. value = alcErrInvalidDevice;
  950. break;
  951. case ALC_INVALID_CONTEXT:
  952. value = alcErrInvalidContext;
  953. break;
  954. case ALC_OUT_OF_MEMORY:
  955. value = alcErrOutOfMemory;
  956. break;
  957. case ALC_DEVICE_SPECIFIER:
  958. if(IsDevice(pDevice))
  959. value = pDevice->szDeviceName;
  960. else
  961. {
  962. ProbeDeviceList();
  963. value = alcDeviceList;
  964. }
  965. break;
  966. case ALC_ALL_DEVICES_SPECIFIER:
  967. ProbeAllDeviceList();
  968. value = alcAllDeviceList;
  969. break;
  970. case ALC_CAPTURE_DEVICE_SPECIFIER:
  971. if(IsDevice(pDevice))
  972. value = pDevice->szDeviceName;
  973. else
  974. {
  975. ProbeCaptureDeviceList();
  976. value = alcCaptureDeviceList;
  977. }
  978. break;
  979. /* Default devices are always first in the list */
  980. case ALC_DEFAULT_DEVICE_SPECIFIER:
  981. if(!alcDeviceList)
  982. ProbeDeviceList();
  983. free(alcDefaultDeviceSpecifier);
  984. alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
  985. if(!alcDefaultDeviceSpecifier)
  986. alcSetError(pDevice, ALC_OUT_OF_MEMORY);
  987. value = alcDefaultDeviceSpecifier;
  988. break;
  989. case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
  990. if(!alcAllDeviceList)
  991. ProbeAllDeviceList();
  992. free(alcDefaultAllDeviceSpecifier);
  993. alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
  994. alcAllDeviceList : "");
  995. if(!alcDefaultAllDeviceSpecifier)
  996. alcSetError(pDevice, ALC_OUT_OF_MEMORY);
  997. value = alcDefaultAllDeviceSpecifier;
  998. break;
  999. case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
  1000. if(!alcCaptureDeviceList)
  1001. ProbeCaptureDeviceList();
  1002. free(alcCaptureDefaultDeviceSpecifier);
  1003. alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
  1004. alcCaptureDeviceList : "");
  1005. if(!alcCaptureDefaultDeviceSpecifier)
  1006. alcSetError(pDevice, ALC_OUT_OF_MEMORY);
  1007. value = alcCaptureDefaultDeviceSpecifier;
  1008. break;
  1009. case ALC_EXTENSIONS:
  1010. if(IsDevice(pDevice))
  1011. value = alcExtensionList;
  1012. else
  1013. value = alcNoDeviceExtList;
  1014. break;
  1015. default:
  1016. alcSetError(pDevice, ALC_INVALID_ENUM);
  1017. break;
  1018. }
  1019. return value;
  1020. }
  1021. /*
  1022. alcGetIntegerv
  1023. Returns information about the Device and the version of Open AL
  1024. */
  1025. ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
  1026. {
  1027. if(size == 0 || data == NULL)
  1028. {
  1029. alcSetError(device, ALC_INVALID_VALUE);
  1030. return;
  1031. }
  1032. if(IsDevice(device) && device->IsCaptureDevice)
  1033. {
  1034. SuspendContext(NULL);
  1035. // Capture device
  1036. switch (param)
  1037. {
  1038. case ALC_CAPTURE_SAMPLES:
  1039. *data = ALCdevice_AvailableSamples(device);
  1040. break;
  1041. case ALC_CONNECTED:
  1042. *data = device->Connected;
  1043. break;
  1044. default:
  1045. alcSetError(device, ALC_INVALID_ENUM);
  1046. break;
  1047. }
  1048. ProcessContext(NULL);
  1049. return;
  1050. }
  1051. // Playback Device
  1052. switch (param)
  1053. {
  1054. case ALC_MAJOR_VERSION:
  1055. *data = alcMajorVersion;
  1056. break;
  1057. case ALC_MINOR_VERSION:
  1058. *data = alcMinorVersion;
  1059. break;
  1060. case ALC_EFX_MAJOR_VERSION:
  1061. *data = alcEFXMajorVersion;
  1062. break;
  1063. case ALC_EFX_MINOR_VERSION:
  1064. *data = alcEFXMinorVersion;
  1065. break;
  1066. case ALC_MAX_AUXILIARY_SENDS:
  1067. if(!IsDevice(device))
  1068. alcSetError(device, ALC_INVALID_DEVICE);
  1069. else
  1070. *data = device->NumAuxSends;
  1071. break;
  1072. case ALC_ATTRIBUTES_SIZE:
  1073. if(!IsDevice(device))
  1074. alcSetError(device, ALC_INVALID_DEVICE);
  1075. else
  1076. *data = 13;
  1077. break;
  1078. case ALC_ALL_ATTRIBUTES:
  1079. if(!IsDevice(device))
  1080. alcSetError(device, ALC_INVALID_DEVICE);
  1081. else if (size < 13)
  1082. alcSetError(device, ALC_INVALID_VALUE);
  1083. else
  1084. {
  1085. int i = 0;
  1086. SuspendContext(NULL);
  1087. data[i++] = ALC_FREQUENCY;
  1088. data[i++] = device->Frequency;
  1089. data[i++] = ALC_REFRESH;
  1090. data[i++] = device->Frequency / device->UpdateSize;
  1091. data[i++] = ALC_SYNC;
  1092. data[i++] = ALC_FALSE;
  1093. data[i++] = ALC_MONO_SOURCES;
  1094. data[i++] = device->NumMonoSources;
  1095. data[i++] = ALC_STEREO_SOURCES;
  1096. data[i++] = device->NumStereoSources;
  1097. data[i++] = ALC_MAX_AUXILIARY_SENDS;
  1098. data[i++] = device->NumAuxSends;
  1099. data[i++] = 0;
  1100. ProcessContext(NULL);
  1101. }
  1102. break;
  1103. case ALC_FREQUENCY:
  1104. if(!IsDevice(device))
  1105. alcSetError(device, ALC_INVALID_DEVICE);
  1106. else
  1107. *data = device->Frequency;
  1108. break;
  1109. case ALC_REFRESH:
  1110. if(!IsDevice(device))
  1111. alcSetError(device, ALC_INVALID_DEVICE);
  1112. else
  1113. *data = device->Frequency / device->UpdateSize;
  1114. break;
  1115. case ALC_SYNC:
  1116. if(!IsDevice(device))
  1117. alcSetError(device, ALC_INVALID_DEVICE);
  1118. else
  1119. *data = ALC_FALSE;
  1120. break;
  1121. case ALC_MONO_SOURCES:
  1122. if(!IsDevice(device))
  1123. alcSetError(device, ALC_INVALID_DEVICE);
  1124. else
  1125. *data = device->NumMonoSources;
  1126. break;
  1127. case ALC_STEREO_SOURCES:
  1128. if(!IsDevice(device))
  1129. alcSetError(device, ALC_INVALID_DEVICE);
  1130. else
  1131. *data = device->NumStereoSources;
  1132. break;
  1133. case ALC_CONNECTED:
  1134. if(!IsDevice(device))
  1135. alcSetError(device, ALC_INVALID_DEVICE);
  1136. else
  1137. *data = device->Connected;
  1138. break;
  1139. default:
  1140. alcSetError(device, ALC_INVALID_ENUM);
  1141. break;
  1142. }
  1143. }
  1144. /*
  1145. alcIsExtensionPresent
  1146. Determines if there is support for a particular extension
  1147. */
  1148. ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
  1149. {
  1150. ALCboolean bResult = ALC_FALSE;
  1151. const char *ptr;
  1152. size_t len;
  1153. if(!extName)
  1154. {
  1155. alcSetError(device, ALC_INVALID_VALUE);
  1156. return ALC_FALSE;
  1157. }
  1158. len = strlen(extName);
  1159. ptr = (IsDevice(device) ? alcExtensionList : alcNoDeviceExtList);
  1160. while(ptr && *ptr)
  1161. {
  1162. if(strncasecmp(ptr, extName, len) == 0 &&
  1163. (ptr[len] == '\0' || isspace(ptr[len])))
  1164. {
  1165. bResult = ALC_TRUE;
  1166. break;
  1167. }
  1168. if((ptr=strchr(ptr, ' ')) != NULL)
  1169. {
  1170. do {
  1171. ++ptr;
  1172. } while(isspace(*ptr));
  1173. }
  1174. }
  1175. return bResult;
  1176. }
  1177. /*
  1178. alcGetProcAddress
  1179. Retrieves the function address for a particular extension function
  1180. */
  1181. ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
  1182. {
  1183. ALsizei i = 0;
  1184. if(!funcName)
  1185. {
  1186. alcSetError(device, ALC_INVALID_VALUE);
  1187. return NULL;
  1188. }
  1189. while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName,funcName) != 0)
  1190. i++;
  1191. return alcFunctions[i].address;
  1192. }
  1193. /*
  1194. alcGetEnumValue
  1195. Get the value for a particular ALC Enumerated Value
  1196. */
  1197. ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
  1198. {
  1199. ALsizei i = 0;
  1200. if(!enumName)
  1201. {
  1202. alcSetError(device, ALC_INVALID_VALUE);
  1203. return (ALCenum)0;
  1204. }
  1205. while(enumeration[i].enumName && strcmp(enumeration[i].enumName,enumName) != 0)
  1206. i++;
  1207. return enumeration[i].value;
  1208. }
  1209. /*
  1210. alcCreateContext
  1211. Create and attach a Context to a particular Device.
  1212. */
  1213. ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
  1214. {
  1215. ALCcontext *ALContext;
  1216. ALboolean running;
  1217. ALuint attrIdx;
  1218. void *temp;
  1219. ALuint i;
  1220. SuspendContext(NULL);
  1221. if(!IsDevice(device) || device->IsCaptureDevice || !device->Connected)
  1222. {
  1223. alcSetError(device, ALC_INVALID_DEVICE);
  1224. ProcessContext(NULL);
  1225. return NULL;
  1226. }
  1227. running = ((device->NumContexts > 0) ? AL_TRUE : AL_FALSE);
  1228. // Reset Context Last Error code
  1229. device->LastError = ALC_NO_ERROR;
  1230. // Check for attributes
  1231. if(attrList && attrList[0])
  1232. {
  1233. ALCuint freq, numMono, numStereo, numSends;
  1234. // If a context is already running on the device, stop playback so the
  1235. // device attributes can be updated
  1236. if(running)
  1237. {
  1238. ProcessContext(NULL);
  1239. ALCdevice_StopPlayback(device);
  1240. SuspendContext(NULL);
  1241. running = AL_FALSE;
  1242. }
  1243. freq = device->Frequency;
  1244. numMono = device->NumMonoSources;
  1245. numStereo = device->NumStereoSources;
  1246. numSends = device->NumAuxSends;
  1247. attrIdx = 0;
  1248. while(attrList[attrIdx])
  1249. {
  1250. if(attrList[attrIdx] == ALC_FREQUENCY &&
  1251. !ConfigValueExists(NULL, "frequency"))
  1252. {
  1253. freq = attrList[attrIdx + 1];
  1254. if(freq < 8000)
  1255. freq = 8000;
  1256. }
  1257. if(attrList[attrIdx] == ALC_STEREO_SOURCES)
  1258. {
  1259. numStereo = attrList[attrIdx + 1];
  1260. if(numStereo > device->MaxNoOfSources)
  1261. numStereo = device->MaxNoOfSources;
  1262. numMono = device->MaxNoOfSources - numStereo;
  1263. }
  1264. if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS &&
  1265. !ConfigValueExists(NULL, "sends"))
  1266. {
  1267. numSends = attrList[attrIdx + 1];
  1268. if(numSends > MAX_SENDS)
  1269. numSends = MAX_SENDS;
  1270. }
  1271. attrIdx += 2;
  1272. }
  1273. device->UpdateSize = (ALuint64)device->UpdateSize * freq /
  1274. device->Frequency;
  1275. device->Frequency = freq;
  1276. device->NumMonoSources = numMono;
  1277. device->NumStereoSources = numStereo;
  1278. device->NumAuxSends = numSends;
  1279. }
  1280. if(running == AL_FALSE && ALCdevice_ResetPlayback(device) == ALC_FALSE)
  1281. {
  1282. alcSetError(device, ALC_INVALID_DEVICE);
  1283. aluHandleDisconnect(device);
  1284. ProcessContext(NULL);
  1285. return NULL;
  1286. }
  1287. aluInitPanning(device);
  1288. for(i = 0;i < device->NumContexts;i++)
  1289. {
  1290. ALCcontext *context = device->Contexts[i];
  1291. ALsizei pos;
  1292. SuspendContext(context);
  1293. for(pos = 0;pos < context->EffectSlotMap.size;pos++)
  1294. {
  1295. ALeffectslot *slot = context->EffectSlotMap.array[pos].value;
  1296. if(!slot->EffectState)
  1297. continue;
  1298. if(ALEffect_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
  1299. {
  1300. alcSetError(device, ALC_INVALID_DEVICE);
  1301. aluHandleDisconnect(device);
  1302. ProcessContext(context);
  1303. ProcessContext(NULL);
  1304. ALCdevice_StopPlayback(device);
  1305. return NULL;
  1306. }
  1307. ALEffect_Update(slot->EffectState, context, &slot->effect);
  1308. }
  1309. for(pos = 0;pos < context->SourceMap.size;pos++)
  1310. {
  1311. ALsource *source = context->SourceMap.array[pos].value;
  1312. ALuint s = device->NumAuxSends;
  1313. while(s < MAX_SENDS)
  1314. {
  1315. if(source->Send[s].Slot)
  1316. source->Send[s].Slot->refcount--;
  1317. source->Send[s].Slot = NULL;
  1318. source->Send[s].WetFilter.type = 0;
  1319. source->Send[s].WetFilter.filter = 0;
  1320. s++;
  1321. }
  1322. source->NeedsUpdate = AL_TRUE;
  1323. }
  1324. ProcessContext(context);
  1325. }
  1326. if(device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
  1327. {
  1328. if(!device->Bs2b)
  1329. {
  1330. device->Bs2b = calloc(1, sizeof(*device->Bs2b));
  1331. bs2b_clear(device->Bs2b);
  1332. }
  1333. bs2b_set_srate(device->Bs2b, device->Frequency);
  1334. bs2b_set_level(device->Bs2b, device->Bs2bLevel);
  1335. }
  1336. else
  1337. {
  1338. free(device->Bs2b);
  1339. device->Bs2b = NULL;
  1340. }
  1341. if(aluChannelsFromFormat(device->Format) <= 2)
  1342. {
  1343. device->HeadDampen = GetConfigValueFloat(NULL, "head_dampen", DEFAULT_HEAD_DAMPEN);
  1344. device->HeadDampen = __min(device->HeadDampen, 1.0f);
  1345. device->HeadDampen = __max(device->HeadDampen, 0.0f);
  1346. }
  1347. else
  1348. device->HeadDampen = 0.0f;
  1349. temp = realloc(device->Contexts, (device->NumContexts+1) * sizeof(*device->Contexts));
  1350. if(!temp)
  1351. {
  1352. alcSetError(device, ALC_OUT_OF_MEMORY);
  1353. ProcessContext(NULL);
  1354. return NULL;
  1355. }
  1356. device->Contexts = temp;
  1357. ALContext = calloc(1, sizeof(ALCcontext));
  1358. if(ALContext)
  1359. {
  1360. ALContext->MaxActiveSources = 256;
  1361. ALContext->ActiveSources = malloc(sizeof(*ALContext->ActiveSources) *
  1362. ALContext->MaxActiveSources);
  1363. }
  1364. if(!ALContext || !ALContext->ActiveSources)
  1365. {
  1366. free(ALContext);
  1367. alcSetError(device, ALC_OUT_OF_MEMORY);
  1368. ProcessContext(NULL);
  1369. return NULL;
  1370. }
  1371. device->Contexts[device->NumContexts++] = ALContext;
  1372. ALContext->Device = device;
  1373. InitContext(ALContext);
  1374. ALContext->next = g_pContextList;
  1375. g_pContextList = ALContext;
  1376. g_ulContextCount++;
  1377. ProcessContext(NULL);
  1378. return ALContext;
  1379. }
  1380. /*
  1381. alcDestroyContext
  1382. Remove a Context
  1383. */
  1384. ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
  1385. {
  1386. ALCdevice *Device;
  1387. ALCcontext **list;
  1388. ALuint i;
  1389. if(!IsContext(context))
  1390. {
  1391. alcSetError(NULL, ALC_INVALID_CONTEXT);
  1392. return;
  1393. }
  1394. Device = context->Device;
  1395. if(Device->NumContexts == 1)
  1396. ALCdevice_StopPlayback(Device);
  1397. SuspendContext(NULL);
  1398. if(context == GlobalContext)
  1399. GlobalContext = NULL;
  1400. for(i = 0;i < Device->NumContexts-1;i++)
  1401. {
  1402. if(Device->Contexts[i] == context)
  1403. {
  1404. Device->Contexts[i] = Device->Contexts[Device->NumContexts-1];
  1405. break;
  1406. }
  1407. }
  1408. Device->NumContexts--;
  1409. // Lock context
  1410. SuspendContext(context);
  1411. if(context->SourceMap.size > 0)
  1412. {
  1413. #ifdef _DEBUG
  1414. AL_PRINT("alcDestroyContext(): deleting %d Source(s)\n", context->SourceMap.size);
  1415. #endif
  1416. ReleaseALSources(context);
  1417. }
  1418. ResetUIntMap(&context->SourceMap);
  1419. if(context->EffectSlotMap.size > 0)
  1420. {
  1421. #ifdef _DEBUG
  1422. AL_PRINT("alcDestroyContext(): deleting %d AuxiliaryEffectSlot(s)\n", context->EffectSlotMap.size);
  1423. #endif
  1424. ReleaseALAuxiliaryEffectSlots(context);
  1425. }
  1426. ResetUIntMap(&context->EffectSlotMap);
  1427. free(context->ActiveSources);
  1428. context->ActiveSources = NULL;
  1429. context->MaxActiveSources = 0;
  1430. context->ActiveSourceCount = 0;
  1431. list = &g_pContextList;
  1432. while(*list != context)
  1433. list = &(*list)->next;
  1434. *list = (*list)->next;
  1435. g_ulContextCount--;
  1436. // Unlock context
  1437. ProcessContext(context);
  1438. ProcessContext(NULL);
  1439. ExitContext(context);
  1440. // Free memory (MUST do this after ProcessContext)
  1441. memset(context, 0, sizeof(ALCcontext));
  1442. free(context);
  1443. }
  1444. /*
  1445. alcGetCurrentContext
  1446. Returns the currently active Context
  1447. */
  1448. ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
  1449. {
  1450. ALCcontext *pContext;
  1451. if((pContext=GetContextSuspended()) != NULL)
  1452. ProcessContext(pContext);
  1453. return pContext;
  1454. }
  1455. /*
  1456. alcGetThreadContext
  1457. Returns the currently active thread-local Context
  1458. */
  1459. ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
  1460. {
  1461. ALCcontext *pContext = NULL;
  1462. SuspendContext(NULL);
  1463. pContext = tls_get(LocalContext);
  1464. if(pContext && !IsContext(pContext))
  1465. {
  1466. tls_set(LocalContext, NULL);
  1467. pContext = NULL;
  1468. }
  1469. ProcessContext(NULL);
  1470. return pContext;
  1471. }
  1472. /*
  1473. alcGetContextsDevice
  1474. Returns the Device that a particular Context is attached to
  1475. */
  1476. ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *pContext)
  1477. {
  1478. ALCdevice *pDevice = NULL;
  1479. SuspendContext(NULL);
  1480. if(IsContext(pContext))
  1481. pDevice = pContext->Device;
  1482. else
  1483. alcSetError(NULL, ALC_INVALID_CONTEXT);
  1484. ProcessContext(NULL);
  1485. return pDevice;
  1486. }
  1487. /*
  1488. alcMakeContextCurrent
  1489. Makes the given Context the active Context
  1490. */
  1491. ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
  1492. {
  1493. ALboolean bReturn = AL_TRUE;
  1494. SuspendContext(NULL);
  1495. // context must be a valid Context or NULL
  1496. if(context == NULL || IsContext(context))
  1497. {
  1498. GlobalContext = context;
  1499. tls_set(LocalContext, NULL);
  1500. }
  1501. else
  1502. {
  1503. alcSetError(NULL, ALC_INVALID_CONTEXT);
  1504. bReturn = AL_FALSE;
  1505. }
  1506. ProcessContext(NULL);
  1507. return bReturn;
  1508. }
  1509. /*
  1510. alcSetThreadContext
  1511. Makes the given Context the active Context for the current thread
  1512. */
  1513. ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
  1514. {
  1515. ALboolean bReturn = AL_TRUE;
  1516. SuspendContext(NULL);
  1517. // context must be a valid Context or NULL
  1518. if(context == NULL || IsContext(context))
  1519. tls_set(LocalContext, context);
  1520. else
  1521. {
  1522. alcSetError(NULL, ALC_INVALID_CONTEXT);
  1523. bReturn = AL_FALSE;
  1524. }
  1525. ProcessContext(NULL);
  1526. return bReturn;
  1527. }
  1528. // Sets the default channel order used by most non-WaveFormatEx-based APIs
  1529. void SetDefaultChannelOrder(ALCdevice *device)
  1530. {
  1531. switch(aluChannelsFromFormat(device->Format))
  1532. {
  1533. case 1: device->DevChannels[FRONT_CENTER] = 0; break;
  1534. case 2: device->DevChannels[FRONT_LEFT] = 0;
  1535. device->DevChannels[FRONT_RIGHT] = 1; break;
  1536. case 4: device->DevChannels[FRONT_LEFT] = 0;
  1537. device->DevChannels[FRONT_RIGHT] = 1;
  1538. device->DevChannels[BACK_LEFT] = 2;
  1539. device->DevChannels[BACK_RIGHT] = 3; break;
  1540. case 6: device->DevChannels[FRONT_LEFT] = 0;
  1541. device->DevChannels[FRONT_RIGHT] = 1;
  1542. device->DevChannels[BACK_LEFT] = 2;
  1543. device->DevChannels[BACK_RIGHT] = 3;
  1544. device->DevChannels[FRONT_CENTER] = 4;
  1545. device->DevChannels[LFE] = 5; break;
  1546. case 7: device->DevChannels[FRONT_LEFT] = 0;
  1547. device->DevChannels[FRONT_RIGHT] = 1;
  1548. device->DevChannels[FRONT_CENTER] = 2;
  1549. device->DevChannels[LFE] = 3;
  1550. device->DevChannels[BACK_CENTER] = 4;
  1551. device->DevChannels[SIDE_LEFT] = 5;
  1552. device->DevChannels[SIDE_RIGHT] = 6; break;
  1553. case 8: device->DevChannels[FRONT_LEFT] = 0;
  1554. device->DevChannels[FRONT_RIGHT] = 1;
  1555. device->DevChannels[BACK_LEFT] = 2;
  1556. device->DevChannels[BACK_RIGHT] = 3;
  1557. device->DevChannels[FRONT_CENTER] = 4;
  1558. device->DevChannels[LFE] = 5;
  1559. device->DevChannels[SIDE_LEFT] = 6;
  1560. device->DevChannels[SIDE_RIGHT] = 7; break;
  1561. }
  1562. }
  1563. // Sets the default order used by WaveFormatEx
  1564. void SetDefaultWFXChannelOrder(ALCdevice *device)
  1565. {
  1566. switch(aluChannelsFromFormat(device->Format))
  1567. {
  1568. case 1: device->DevChannels[FRONT_CENTER] = 0; break;
  1569. case 2: device->DevChannels[FRONT_LEFT] = 0;
  1570. device->DevChannels[FRONT_RIGHT] = 1; break;
  1571. case 4: device->DevChannels[FRONT_LEFT] = 0;
  1572. device->DevChannels[FRONT_RIGHT] = 1;
  1573. device->DevChannels[BACK_LEFT] = 2;
  1574. device->DevChannels[BACK_RIGHT] = 3; break;
  1575. case 6: device->DevChannels[FRONT_LEFT] = 0;
  1576. device->DevChannels[FRONT_RIGHT] = 1;
  1577. device->DevChannels[FRONT_CENTER] = 2;
  1578. device->DevChannels[LFE] = 3;
  1579. device->DevChannels[BACK_LEFT] = 4;
  1580. device->DevChannels[BACK_RIGHT] = 5; break;
  1581. case 7: device->DevChannels[FRONT_LEFT] = 0;
  1582. device->DevChannels[FRONT_RIGHT] = 1;
  1583. device->DevChannels[FRONT_CENTER] = 2;
  1584. device->DevChannels[LFE] = 3;
  1585. device->DevChannels[BACK_CENTER] = 4;
  1586. device->DevChannels[SIDE_LEFT] = 5;
  1587. device->DevChannels[SIDE_RIGHT] = 6; break;
  1588. case 8: device->DevChannels[FRONT_LEFT] = 0;
  1589. device->DevChannels[FRONT_RIGHT] = 1;
  1590. device->DevChannels[FRONT_CENTER] = 2;
  1591. device->DevChannels[LFE] = 3;
  1592. device->DevChannels[BACK_LEFT] = 4;
  1593. device->DevChannels[BACK_RIGHT] = 5;
  1594. device->DevChannels[SIDE_LEFT] = 6;
  1595. device->DevChannels[SIDE_RIGHT] = 7; break;
  1596. }
  1597. }
  1598. static ALenum GetFormatFromString(const char *str)
  1599. {
  1600. if(strcasecmp(str, "AL_FORMAT_MONO32") == 0) return AL_FORMAT_MONO_FLOAT32;
  1601. if(strcasecmp(str, "AL_FORMAT_STEREO32") == 0) return AL_FORMAT_STEREO_FLOAT32;
  1602. if(strcasecmp(str, "AL_FORMAT_QUAD32") == 0) return AL_FORMAT_QUAD32;
  1603. if(strcasecmp(str, "AL_FORMAT_51CHN32") == 0) return AL_FORMAT_51CHN32;
  1604. if(strcasecmp(str, "AL_FORMAT_61CHN32") == 0) return AL_FORMAT_61CHN32;
  1605. if(strcasecmp(str, "AL_FORMAT_71CHN32") == 0) return AL_FORMAT_71CHN32;
  1606. if(strcasecmp(str, "AL_FORMAT_MONO16") == 0) return AL_FORMAT_MONO16;
  1607. if(strcasecmp(str, "AL_FORMAT_STEREO16") == 0) return AL_FORMAT_STEREO16;
  1608. if(strcasecmp(str, "AL_FORMAT_QUAD16") == 0) return AL_FORMAT_QUAD16;
  1609. if(strcasecmp(str, "AL_FORMAT_51CHN16") == 0) return AL_FORMAT_51CHN16;
  1610. if(strcasecmp(str, "AL_FORMAT_61CHN16") == 0) return AL_FORMAT_61CHN16;
  1611. if(strcasecmp(str, "AL_FORMAT_71CHN16") == 0) return AL_FORMAT_71CHN16;
  1612. if(strcasecmp(str, "AL_FORMAT_MONO8") == 0) return AL_FORMAT_MONO8;
  1613. if(strcasecmp(str, "AL_FORMAT_STEREO8") == 0) return AL_FORMAT_STEREO8;
  1614. if(strcasecmp(str, "AL_FORMAT_QUAD8") == 0) return AL_FORMAT_QUAD8;
  1615. if(strcasecmp(str, "AL_FORMAT_51CHN8") == 0) return AL_FORMAT_51CHN8;
  1616. if(strcasecmp(str, "AL_FORMAT_61CHN8") == 0) return AL_FORMAT_61CHN8;
  1617. if(strcasecmp(str, "AL_FORMAT_71CHN8") == 0) return AL_FORMAT_71CHN8;
  1618. AL_PRINT("Unknown format: \"%s\"\n", str);
  1619. return AL_FORMAT_STEREO16;
  1620. }
  1621. #include "qAudioOutputWrapper.h"
  1622. /*
  1623. alcOpenDevice
  1624. Open the Device specified.
  1625. */
  1626. ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
  1627. {
  1628. ALboolean bDeviceFound = AL_FALSE;
  1629. const ALCchar *fmt;
  1630. ALCdevice *device;
  1631. ALint i;
  1632. if(deviceName && !deviceName[0])
  1633. deviceName = NULL;
  1634. device = calloc(1, sizeof(ALCdevice));
  1635. if(!device)
  1636. {
  1637. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  1638. return NULL;
  1639. }
  1640. //Validate device
  1641. device->Connected = ALC_TRUE;
  1642. device->IsCaptureDevice = AL_FALSE;
  1643. device->LastError = ALC_NO_ERROR;
  1644. device->Bs2b = NULL;
  1645. device->szDeviceName = NULL;
  1646. device->Contexts = NULL;
  1647. device->NumContexts = 0;
  1648. InitUIntMap(&device->BufferMap);
  1649. InitUIntMap(&device->EffectMap);
  1650. InitUIntMap(&device->FilterMap);
  1651. InitUIntMap(&device->DatabufferMap);
  1652. //Set output format
  1653. device->Frequency = GetConfigValueInt(NULL, "frequency", SWMIXER_OUTPUT_RATE);
  1654. if(device->Frequency < 8000)
  1655. device->Frequency = 8000;
  1656. fmt = GetConfigValue(NULL, "format", "AL_FORMAT_STEREO16");
  1657. device->Format = GetFormatFromString(fmt);
  1658. device->NumUpdates = GetConfigValueInt(NULL, "periods", 4);
  1659. if(device->NumUpdates < 2)
  1660. device->NumUpdates = 4;
  1661. device->UpdateSize = GetConfigValueInt(NULL, "period_size", 1024);
  1662. if(device->UpdateSize <= 0)
  1663. device->UpdateSize = 1024;
  1664. device->MaxNoOfSources = GetConfigValueInt(NULL, "sources", 256);
  1665. if((ALint)device->MaxNoOfSources <= 0)
  1666. device->MaxNoOfSources = 256;
  1667. device->AuxiliaryEffectSlotMax = GetConfigValueInt(NULL, "slots", 4);
  1668. if((ALint)device->AuxiliaryEffectSlotMax <= 0)
  1669. device->AuxiliaryEffectSlotMax = 4;
  1670. device->NumStereoSources = 1;
  1671. device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
  1672. device->NumAuxSends = GetConfigValueInt(NULL, "sends", MAX_SENDS);
  1673. if(device->NumAuxSends > MAX_SENDS)
  1674. device->NumAuxSends = MAX_SENDS;
  1675. device->Bs2bLevel = GetConfigValueInt(NULL, "cf_level", 0);
  1676. device->HeadDampen = 0.0f;
  1677. // Find a playback device to open
  1678. SuspendContext(NULL);
  1679. for(i = 0;BackendList[i].Init;i++)
  1680. {
  1681. device->Funcs = &BackendList[i].Funcs;
  1682. if(ALCdevice_OpenPlayback(device, deviceName))
  1683. {
  1684. device->next = g_pDeviceList;
  1685. g_pDeviceList = device;
  1686. g_ulDeviceCount++;
  1687. bDeviceFound = AL_TRUE;
  1688. break;
  1689. }
  1690. }
  1691. ProcessContext(NULL);
  1692. if(!bDeviceFound)
  1693. {
  1694. // No suitable output device found
  1695. alcSetError(NULL, ALC_INVALID_VALUE);
  1696. free(device);
  1697. device = NULL;
  1698. }
  1699. return device;
  1700. }
  1701. /*
  1702. alcCloseDevice
  1703. Close the specified Device
  1704. */
  1705. ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *pDevice)
  1706. {
  1707. ALCdevice **list;
  1708. if(!IsDevice(pDevice) || pDevice->IsCaptureDevice)
  1709. {
  1710. alcSetError(pDevice, ALC_INVALID_DEVICE);
  1711. return ALC_FALSE;
  1712. }
  1713. SuspendContext(NULL);
  1714. list = &g_pDeviceList;
  1715. while(*list != pDevice)
  1716. list = &(*list)->next;
  1717. *list = (*list)->next;
  1718. g_ulDeviceCount--;
  1719. ProcessContext(NULL);
  1720. if(pDevice->NumContexts > 0)
  1721. {
  1722. #ifdef _DEBUG
  1723. AL_PRINT("alcCloseDevice(): destroying %u Context(s)\n", pDevice->NumContexts);
  1724. #endif
  1725. while(pDevice->NumContexts > 0)
  1726. alcDestroyContext(pDevice->Contexts[0]);
  1727. }
  1728. ALCdevice_ClosePlayback(pDevice);
  1729. if(pDevice->BufferMap.size > 0)
  1730. {
  1731. #ifdef _DEBUG
  1732. AL_PRINT("alcCloseDevice(): deleting %d Buffer(s)\n", pDevice->BufferMap.size);
  1733. #endif
  1734. ReleaseALBuffers(pDevice);
  1735. }
  1736. ResetUIntMap(&pDevice->BufferMap);
  1737. if(pDevice->EffectMap.size > 0)
  1738. {
  1739. #ifdef _DEBUG
  1740. AL_PRINT("alcCloseDevice(): deleting %d Effect(s)\n", pDevice->EffectMap.size);
  1741. #endif
  1742. ReleaseALEffects(pDevice);
  1743. }
  1744. ResetUIntMap(&pDevice->EffectMap);
  1745. if(pDevice->FilterMap.size > 0)
  1746. {
  1747. #ifdef _DEBUG
  1748. AL_PRINT("alcCloseDevice(): deleting %d Filter(s)\n", pDevice->FilterMap.size);
  1749. #endif
  1750. ReleaseALFilters(pDevice);
  1751. }
  1752. ResetUIntMap(&pDevice->FilterMap);
  1753. if(pDevice->DatabufferMap.size > 0)
  1754. {
  1755. #ifdef _DEBUG
  1756. AL_PRINT("alcCloseDevice(): deleting %d Databuffer(s)\n", pDevice->DatabufferMap.size);
  1757. #endif
  1758. ReleaseALDatabuffers(pDevice);
  1759. }
  1760. ResetUIntMap(&pDevice->DatabufferMap);
  1761. free(pDevice->Bs2b);
  1762. pDevice->Bs2b = NULL;
  1763. free(pDevice->szDeviceName);
  1764. pDevice->szDeviceName = NULL;
  1765. free(pDevice->Contexts);
  1766. pDevice->Contexts = NULL;
  1767. //Release device structure
  1768. memset(pDevice, 0, sizeof(ALCdevice));
  1769. free(pDevice);
  1770. return ALC_TRUE;
  1771. }
  1772. ALCvoid ReleaseALC(ALCvoid)
  1773. {
  1774. free(alcDeviceList); alcDeviceList = NULL;
  1775. alcDeviceListSize = 0;
  1776. free(alcAllDeviceList); alcAllDeviceList = NULL;
  1777. alcAllDeviceListSize = 0;
  1778. free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
  1779. alcCaptureDeviceListSize = 0;
  1780. free(alcDefaultDeviceSpecifier);
  1781. alcDefaultDeviceSpecifier = NULL;
  1782. free(alcDefaultAllDeviceSpecifier);
  1783. alcDefaultAllDeviceSpecifier = NULL;
  1784. free(alcCaptureDefaultDeviceSpecifier);
  1785. alcCaptureDefaultDeviceSpecifier = NULL;
  1786. #ifdef _DEBUG
  1787. if(g_ulDeviceCount > 0)
  1788. AL_PRINT("exit(): closing %u Device%s\n", g_ulDeviceCount, (g_ulDeviceCount>1)?"s":"");
  1789. #endif
  1790. while(g_pDeviceList)
  1791. {
  1792. if(g_pDeviceList->IsCaptureDevice)
  1793. alcCaptureCloseDevice(g_pDeviceList);
  1794. else
  1795. alcCloseDevice(g_pDeviceList);
  1796. }
  1797. }
  1798. ///////////////////////////////////////////////////////