ALc.c 69 KB

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