ALc.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847
  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 <signal.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 "alError.h"
  36. #include "bs2b.h"
  37. #include "alu.h"
  38. #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
  39. static struct BackendInfo BackendList[] = {
  40. #ifdef HAVE_PULSEAUDIO
  41. { "pulse", alc_pulse_init, alc_pulse_deinit, alc_pulse_probe, EmptyFuncs },
  42. #endif
  43. #ifdef HAVE_ALSA
  44. { "alsa", alc_alsa_init, alc_alsa_deinit, alc_alsa_probe, EmptyFuncs },
  45. #endif
  46. #ifdef HAVE_COREAUDIO
  47. { "core", alc_ca_init, alc_ca_deinit, alc_ca_probe, EmptyFuncs },
  48. #endif
  49. #ifdef HAVE_OSS
  50. { "oss", alc_oss_init, alc_oss_deinit, alc_oss_probe, EmptyFuncs },
  51. #endif
  52. #ifdef HAVE_SOLARIS
  53. { "solaris", alc_solaris_init, alc_solaris_deinit, alc_solaris_probe, EmptyFuncs },
  54. #endif
  55. #ifdef HAVE_SNDIO
  56. { "sndio", alc_sndio_init, alc_sndio_deinit, alc_sndio_probe, EmptyFuncs },
  57. #endif
  58. #ifdef HAVE_MMDEVAPI
  59. { "mmdevapi", alcMMDevApiInit, alcMMDevApiDeinit, alcMMDevApiProbe, EmptyFuncs },
  60. #endif
  61. #ifdef HAVE_DSOUND
  62. { "dsound", alcDSoundInit, alcDSoundDeinit, alcDSoundProbe, EmptyFuncs },
  63. #endif
  64. #ifdef HAVE_WINMM
  65. { "winmm", alcWinMMInit, alcWinMMDeinit, alcWinMMProbe, EmptyFuncs },
  66. #endif
  67. #ifdef HAVE_PORTAUDIO
  68. { "port", alc_pa_init, alc_pa_deinit, alc_pa_probe, EmptyFuncs },
  69. #endif
  70. #ifdef HAVE_OPENSL
  71. { "opensl", alc_opensl_init, alc_opensl_deinit, alc_opensl_probe, EmptyFuncs },
  72. #endif
  73. #ifdef HAVE_ANDROID
  74. { "android", alc_android_init, alc_android_deinit, alc_android_probe, EmptyFuncs },
  75. #endif
  76. { "null", alc_null_init, alc_null_deinit, alc_null_probe, EmptyFuncs },
  77. #ifdef HAVE_WAVE
  78. { "wave", alc_wave_init, alc_wave_deinit, alc_wave_probe, EmptyFuncs },
  79. #endif
  80. { NULL, NULL, NULL, NULL, EmptyFuncs }
  81. };
  82. static struct BackendInfo BackendLoopback = {
  83. "loopback", alc_loopback_init, alc_loopback_deinit, alc_loopback_probe, EmptyFuncs
  84. };
  85. #undef EmptyFuncs
  86. static struct BackendInfo PlaybackBackend;
  87. static struct BackendInfo CaptureBackend;
  88. ///////////////////////////////////////////////////////
  89. // STRING and EXTENSIONS
  90. typedef struct ALCfunction {
  91. const ALCchar *funcName;
  92. ALCvoid *address;
  93. } ALCfunction;
  94. typedef struct ALCenums {
  95. const ALCchar *enumName;
  96. ALCenum value;
  97. } ALCenums;
  98. static const ALCfunction alcFunctions[] = {
  99. { "alcCreateContext", (ALCvoid *) alcCreateContext },
  100. { "alcMakeContextCurrent", (ALCvoid *) alcMakeContextCurrent },
  101. { "alcProcessContext", (ALCvoid *) alcProcessContext },
  102. { "alcSuspendContext", (ALCvoid *) alcSuspendContext },
  103. { "alcDestroyContext", (ALCvoid *) alcDestroyContext },
  104. { "alcGetCurrentContext", (ALCvoid *) alcGetCurrentContext },
  105. { "alcGetContextsDevice", (ALCvoid *) alcGetContextsDevice },
  106. { "alcOpenDevice", (ALCvoid *) alcOpenDevice },
  107. { "alcCloseDevice", (ALCvoid *) alcCloseDevice },
  108. { "alcGetError", (ALCvoid *) alcGetError },
  109. { "alcIsExtensionPresent", (ALCvoid *) alcIsExtensionPresent },
  110. { "alcGetProcAddress", (ALCvoid *) alcGetProcAddress },
  111. { "alcGetEnumValue", (ALCvoid *) alcGetEnumValue },
  112. { "alcGetString", (ALCvoid *) alcGetString },
  113. { "alcGetIntegerv", (ALCvoid *) alcGetIntegerv },
  114. { "alcCaptureOpenDevice", (ALCvoid *) alcCaptureOpenDevice },
  115. { "alcCaptureCloseDevice", (ALCvoid *) alcCaptureCloseDevice },
  116. { "alcCaptureStart", (ALCvoid *) alcCaptureStart },
  117. { "alcCaptureStop", (ALCvoid *) alcCaptureStop },
  118. { "alcCaptureSamples", (ALCvoid *) alcCaptureSamples },
  119. { "alcSetThreadContext", (ALCvoid *) alcSetThreadContext },
  120. { "alcGetThreadContext", (ALCvoid *) alcGetThreadContext },
  121. { "alcLoopbackOpenDeviceSOFT", (ALCvoid *) alcLoopbackOpenDeviceSOFT},
  122. { "alcIsRenderFormatSupportedSOFT",(ALCvoid *) alcIsRenderFormatSupportedSOFT},
  123. { "alcRenderSamplesSOFT", (ALCvoid *) alcRenderSamplesSOFT },
  124. { "alEnable", (ALCvoid *) alEnable },
  125. { "alDisable", (ALCvoid *) alDisable },
  126. { "alIsEnabled", (ALCvoid *) alIsEnabled },
  127. { "alGetString", (ALCvoid *) alGetString },
  128. { "alGetBooleanv", (ALCvoid *) alGetBooleanv },
  129. { "alGetIntegerv", (ALCvoid *) alGetIntegerv },
  130. { "alGetFloatv", (ALCvoid *) alGetFloatv },
  131. { "alGetDoublev", (ALCvoid *) alGetDoublev },
  132. { "alGetBoolean", (ALCvoid *) alGetBoolean },
  133. { "alGetInteger", (ALCvoid *) alGetInteger },
  134. { "alGetFloat", (ALCvoid *) alGetFloat },
  135. { "alGetDouble", (ALCvoid *) alGetDouble },
  136. { "alGetError", (ALCvoid *) alGetError },
  137. { "alIsExtensionPresent", (ALCvoid *) alIsExtensionPresent },
  138. { "alGetProcAddress", (ALCvoid *) alGetProcAddress },
  139. { "alGetEnumValue", (ALCvoid *) alGetEnumValue },
  140. { "alListenerf", (ALCvoid *) alListenerf },
  141. { "alListener3f", (ALCvoid *) alListener3f },
  142. { "alListenerfv", (ALCvoid *) alListenerfv },
  143. { "alListeneri", (ALCvoid *) alListeneri },
  144. { "alListener3i", (ALCvoid *) alListener3i },
  145. { "alListeneriv", (ALCvoid *) alListeneriv },
  146. { "alGetListenerf", (ALCvoid *) alGetListenerf },
  147. { "alGetListener3f", (ALCvoid *) alGetListener3f },
  148. { "alGetListenerfv", (ALCvoid *) alGetListenerfv },
  149. { "alGetListeneri", (ALCvoid *) alGetListeneri },
  150. { "alGetListener3i", (ALCvoid *) alGetListener3i },
  151. { "alGetListeneriv", (ALCvoid *) alGetListeneriv },
  152. { "alGenSources", (ALCvoid *) alGenSources },
  153. { "alDeleteSources", (ALCvoid *) alDeleteSources },
  154. { "alIsSource", (ALCvoid *) alIsSource },
  155. { "alSourcef", (ALCvoid *) alSourcef },
  156. { "alSource3f", (ALCvoid *) alSource3f },
  157. { "alSourcefv", (ALCvoid *) alSourcefv },
  158. { "alSourcei", (ALCvoid *) alSourcei },
  159. { "alSource3i", (ALCvoid *) alSource3i },
  160. { "alSourceiv", (ALCvoid *) alSourceiv },
  161. { "alGetSourcef", (ALCvoid *) alGetSourcef },
  162. { "alGetSource3f", (ALCvoid *) alGetSource3f },
  163. { "alGetSourcefv", (ALCvoid *) alGetSourcefv },
  164. { "alGetSourcei", (ALCvoid *) alGetSourcei },
  165. { "alGetSource3i", (ALCvoid *) alGetSource3i },
  166. { "alGetSourceiv", (ALCvoid *) alGetSourceiv },
  167. { "alSourcePlayv", (ALCvoid *) alSourcePlayv },
  168. { "alSourceStopv", (ALCvoid *) alSourceStopv },
  169. { "alSourceRewindv", (ALCvoid *) alSourceRewindv },
  170. { "alSourcePausev", (ALCvoid *) alSourcePausev },
  171. { "alSourcePlay", (ALCvoid *) alSourcePlay },
  172. { "alSourceStop", (ALCvoid *) alSourceStop },
  173. { "alSourceRewind", (ALCvoid *) alSourceRewind },
  174. { "alSourcePause", (ALCvoid *) alSourcePause },
  175. { "alSourceQueueBuffers", (ALCvoid *) alSourceQueueBuffers },
  176. { "alSourceUnqueueBuffers", (ALCvoid *) alSourceUnqueueBuffers },
  177. { "alGenBuffers", (ALCvoid *) alGenBuffers },
  178. { "alDeleteBuffers", (ALCvoid *) alDeleteBuffers },
  179. { "alIsBuffer", (ALCvoid *) alIsBuffer },
  180. { "alBufferData", (ALCvoid *) alBufferData },
  181. { "alBufferf", (ALCvoid *) alBufferf },
  182. { "alBuffer3f", (ALCvoid *) alBuffer3f },
  183. { "alBufferfv", (ALCvoid *) alBufferfv },
  184. { "alBufferi", (ALCvoid *) alBufferi },
  185. { "alBuffer3i", (ALCvoid *) alBuffer3i },
  186. { "alBufferiv", (ALCvoid *) alBufferiv },
  187. { "alGetBufferf", (ALCvoid *) alGetBufferf },
  188. { "alGetBuffer3f", (ALCvoid *) alGetBuffer3f },
  189. { "alGetBufferfv", (ALCvoid *) alGetBufferfv },
  190. { "alGetBufferi", (ALCvoid *) alGetBufferi },
  191. { "alGetBuffer3i", (ALCvoid *) alGetBuffer3i },
  192. { "alGetBufferiv", (ALCvoid *) alGetBufferiv },
  193. { "alDopplerFactor", (ALCvoid *) alDopplerFactor },
  194. { "alDopplerVelocity", (ALCvoid *) alDopplerVelocity },
  195. { "alSpeedOfSound", (ALCvoid *) alSpeedOfSound },
  196. { "alDistanceModel", (ALCvoid *) alDistanceModel },
  197. { "alGenFilters", (ALCvoid *) alGenFilters },
  198. { "alDeleteFilters", (ALCvoid *) alDeleteFilters },
  199. { "alIsFilter", (ALCvoid *) alIsFilter },
  200. { "alFilteri", (ALCvoid *) alFilteri },
  201. { "alFilteriv", (ALCvoid *) alFilteriv },
  202. { "alFilterf", (ALCvoid *) alFilterf },
  203. { "alFilterfv", (ALCvoid *) alFilterfv },
  204. { "alGetFilteri", (ALCvoid *) alGetFilteri },
  205. { "alGetFilteriv", (ALCvoid *) alGetFilteriv },
  206. { "alGetFilterf", (ALCvoid *) alGetFilterf },
  207. { "alGetFilterfv", (ALCvoid *) alGetFilterfv },
  208. { "alGenEffects", (ALCvoid *) alGenEffects },
  209. { "alDeleteEffects", (ALCvoid *) alDeleteEffects },
  210. { "alIsEffect", (ALCvoid *) alIsEffect },
  211. { "alEffecti", (ALCvoid *) alEffecti },
  212. { "alEffectiv", (ALCvoid *) alEffectiv },
  213. { "alEffectf", (ALCvoid *) alEffectf },
  214. { "alEffectfv", (ALCvoid *) alEffectfv },
  215. { "alGetEffecti", (ALCvoid *) alGetEffecti },
  216. { "alGetEffectiv", (ALCvoid *) alGetEffectiv },
  217. { "alGetEffectf", (ALCvoid *) alGetEffectf },
  218. { "alGetEffectfv", (ALCvoid *) alGetEffectfv },
  219. { "alGenAuxiliaryEffectSlots", (ALCvoid *) alGenAuxiliaryEffectSlots},
  220. { "alDeleteAuxiliaryEffectSlots",(ALCvoid *) alDeleteAuxiliaryEffectSlots},
  221. { "alIsAuxiliaryEffectSlot", (ALCvoid *) alIsAuxiliaryEffectSlot },
  222. { "alAuxiliaryEffectSloti", (ALCvoid *) alAuxiliaryEffectSloti },
  223. { "alAuxiliaryEffectSlotiv", (ALCvoid *) alAuxiliaryEffectSlotiv },
  224. { "alAuxiliaryEffectSlotf", (ALCvoid *) alAuxiliaryEffectSlotf },
  225. { "alAuxiliaryEffectSlotfv", (ALCvoid *) alAuxiliaryEffectSlotfv },
  226. { "alGetAuxiliaryEffectSloti", (ALCvoid *) alGetAuxiliaryEffectSloti},
  227. { "alGetAuxiliaryEffectSlotiv", (ALCvoid *) alGetAuxiliaryEffectSlotiv},
  228. { "alGetAuxiliaryEffectSlotf", (ALCvoid *) alGetAuxiliaryEffectSlotf},
  229. { "alGetAuxiliaryEffectSlotfv", (ALCvoid *) alGetAuxiliaryEffectSlotfv},
  230. { "alBufferSubDataSOFT", (ALCvoid *) alBufferSubDataSOFT },
  231. { "alBufferSamplesSOFT", (ALCvoid *) alBufferSamplesSOFT },
  232. { "alBufferSubSamplesSOFT", (ALCvoid *) alBufferSubSamplesSOFT },
  233. { "alGetBufferSamplesSOFT", (ALCvoid *) alGetBufferSamplesSOFT },
  234. { "alIsBufferFormatSupportedSOFT",(ALCvoid *) alIsBufferFormatSupportedSOFT},
  235. { "alDeferUpdatesSOFT", (ALCvoid *) alDeferUpdatesSOFT },
  236. { "alProcessUpdatesSOFT", (ALCvoid *) alProcessUpdatesSOFT },
  237. { NULL, (ALCvoid *) NULL }
  238. };
  239. static const ALCenums enumeration[] = {
  240. // Types
  241. { "ALC_INVALID", ALC_INVALID },
  242. { "ALC_FALSE", ALC_FALSE },
  243. { "ALC_TRUE", ALC_TRUE },
  244. // ALC Properties
  245. { "ALC_MAJOR_VERSION", ALC_MAJOR_VERSION },
  246. { "ALC_MINOR_VERSION", ALC_MINOR_VERSION },
  247. { "ALC_ATTRIBUTES_SIZE", ALC_ATTRIBUTES_SIZE },
  248. { "ALC_ALL_ATTRIBUTES", ALC_ALL_ATTRIBUTES },
  249. { "ALC_DEFAULT_DEVICE_SPECIFIER", ALC_DEFAULT_DEVICE_SPECIFIER },
  250. { "ALC_DEVICE_SPECIFIER", ALC_DEVICE_SPECIFIER },
  251. { "ALC_ALL_DEVICES_SPECIFIER", ALC_ALL_DEVICES_SPECIFIER },
  252. { "ALC_DEFAULT_ALL_DEVICES_SPECIFIER", ALC_DEFAULT_ALL_DEVICES_SPECIFIER },
  253. { "ALC_EXTENSIONS", ALC_EXTENSIONS },
  254. { "ALC_FREQUENCY", ALC_FREQUENCY },
  255. { "ALC_REFRESH", ALC_REFRESH },
  256. { "ALC_SYNC", ALC_SYNC },
  257. { "ALC_MONO_SOURCES", ALC_MONO_SOURCES },
  258. { "ALC_STEREO_SOURCES", ALC_STEREO_SOURCES },
  259. { "ALC_CAPTURE_DEVICE_SPECIFIER", ALC_CAPTURE_DEVICE_SPECIFIER },
  260. { "ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER", ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER},
  261. { "ALC_CAPTURE_SAMPLES", ALC_CAPTURE_SAMPLES },
  262. { "ALC_CONNECTED", ALC_CONNECTED },
  263. // EFX Properties
  264. { "ALC_EFX_MAJOR_VERSION", ALC_EFX_MAJOR_VERSION },
  265. { "ALC_EFX_MINOR_VERSION", ALC_EFX_MINOR_VERSION },
  266. { "ALC_MAX_AUXILIARY_SENDS", ALC_MAX_AUXILIARY_SENDS },
  267. // Loopback device Properties
  268. { "ALC_FORMAT_CHANNELS_SOFT", ALC_FORMAT_CHANNELS_SOFT },
  269. { "ALC_FORMAT_TYPE_SOFT", ALC_FORMAT_TYPE_SOFT },
  270. // Buffer Channel Configurations
  271. { "ALC_MONO_SOFT", ALC_MONO_SOFT },
  272. { "ALC_STEREO_SOFT", ALC_STEREO_SOFT },
  273. { "ALC_QUAD_SOFT", ALC_QUAD_SOFT },
  274. { "ALC_5POINT1_SOFT", ALC_5POINT1_SOFT },
  275. { "ALC_6POINT1_SOFT", ALC_6POINT1_SOFT },
  276. { "ALC_7POINT1_SOFT", ALC_7POINT1_SOFT },
  277. // Buffer Sample Types
  278. { "ALC_BYTE_SOFT", ALC_BYTE_SOFT },
  279. { "ALC_UNSIGNED_BYTE_SOFT", ALC_UNSIGNED_BYTE_SOFT },
  280. { "ALC_SHORT_SOFT", ALC_SHORT_SOFT },
  281. { "ALC_UNSIGNED_SHORT_SOFT", ALC_UNSIGNED_SHORT_SOFT },
  282. { "ALC_INT_SOFT", ALC_INT_SOFT },
  283. { "ALC_UNSIGNED_INT_SOFT", ALC_UNSIGNED_INT_SOFT },
  284. { "ALC_FLOAT_SOFT", ALC_FLOAT_SOFT },
  285. // ALC Error Message
  286. { "ALC_NO_ERROR", ALC_NO_ERROR },
  287. { "ALC_INVALID_DEVICE", ALC_INVALID_DEVICE },
  288. { "ALC_INVALID_CONTEXT", ALC_INVALID_CONTEXT },
  289. { "ALC_INVALID_ENUM", ALC_INVALID_ENUM },
  290. { "ALC_INVALID_VALUE", ALC_INVALID_VALUE },
  291. { "ALC_OUT_OF_MEMORY", ALC_OUT_OF_MEMORY },
  292. { NULL, (ALCenum)0 }
  293. };
  294. // Error strings
  295. static const ALCchar alcNoError[] = "No Error";
  296. static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
  297. static const ALCchar alcErrInvalidContext[] = "Invalid Context";
  298. static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
  299. static const ALCchar alcErrInvalidValue[] = "Invalid Value";
  300. static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
  301. /* Device lists. Sizes only include the first ending null character, not the
  302. * second */
  303. static const ALCchar alcDefaultName[] = "OpenAL Soft\0";
  304. static ALCchar *alcAllDeviceList;
  305. static size_t alcAllDeviceListSize;
  306. static ALCchar *alcCaptureDeviceList;
  307. static size_t alcCaptureDeviceListSize;
  308. /* Default is always the first in the list */
  309. static ALCchar *alcDefaultAllDeviceSpecifier;
  310. static ALCchar *alcCaptureDefaultDeviceSpecifier;
  311. static const ALCchar alcNoDeviceExtList[] =
  312. "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
  313. "ALC_EXT_thread_local_context ALC_SOFT_loopback";
  314. static const ALCchar alcExtensionList[] =
  315. "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
  316. "ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX "
  317. "ALC_EXT_thread_local_context ALC_SOFT_loopback";
  318. static const ALCint alcMajorVersion = 1;
  319. static const ALCint alcMinorVersion = 1;
  320. static const ALCint alcEFXMajorVersion = 1;
  321. static const ALCint alcEFXMinorVersion = 0;
  322. ///////////////////////////////////////////////////////
  323. ///////////////////////////////////////////////////////
  324. // Global Variables
  325. static CRITICAL_SECTION ListLock;
  326. /* Device List */
  327. static ALCdevice *volatile DeviceList = NULL;
  328. // Thread-local current context
  329. static pthread_key_t LocalContext;
  330. // Process-wide current context
  331. static ALCcontext *volatile GlobalContext = NULL;
  332. /* Device Error */
  333. static volatile ALCenum g_eLastNullDeviceError = ALC_NO_ERROR;
  334. // Default context extensions
  335. static const ALchar alExtList[] =
  336. "AL_EXT_ALAW AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 "
  337. "AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW "
  338. "AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model "
  339. "AL_LOKI_quadriphonic AL_SOFT_buffer_samples AL_SOFT_buffer_sub_data "
  340. "AL_SOFTX_deferred_updates AL_SOFT_direct_channels AL_SOFT_loop_points";
  341. // Mixing Priority Level
  342. ALint RTPrioLevel;
  343. // Output Log File
  344. FILE *LogFile;
  345. // Output Log Level
  346. #ifdef _DEBUG
  347. enum LogLevel LogLevel = LogWarning;
  348. #else
  349. enum LogLevel LogLevel = LogError;
  350. #endif
  351. /* Flag to trap ALC device errors */
  352. static ALCboolean TrapALCError = ALC_FALSE;
  353. /* One-time configuration init control */
  354. static pthread_once_t alc_config_once = PTHREAD_ONCE_INIT;
  355. /* Forced effect that applies to sources that don't have an effect on send 0 */
  356. static ALeffect DefaultEffect;
  357. ///////////////////////////////////////////////////////
  358. ///////////////////////////////////////////////////////
  359. // ALC Related helper functions
  360. static void ReleaseALC(void);
  361. static void ReleaseThreadCtx(void *ptr);
  362. static void alc_initconfig(void);
  363. #define DO_INITCONFIG() pthread_once(&alc_config_once, alc_initconfig)
  364. #if defined(_WIN32)
  365. static void alc_init(void);
  366. static void alc_deinit(void);
  367. static void alc_deinit_safe(void);
  368. UIntMap TlsDestructor;
  369. #ifndef AL_LIBTYPE_STATIC
  370. BOOL APIENTRY DllMain(HINSTANCE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
  371. {
  372. ALsizei i;
  373. // Perform actions based on the reason for calling.
  374. switch(ul_reason_for_call)
  375. {
  376. case DLL_PROCESS_ATTACH:
  377. /* Pin the DLL so we won't get unloaded until the process terminates */
  378. GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
  379. (WCHAR*)hModule, &hModule);
  380. InitUIntMap(&TlsDestructor, ~0);
  381. alc_init();
  382. break;
  383. case DLL_THREAD_DETACH:
  384. LockUIntMapRead(&TlsDestructor);
  385. for(i = 0;i < TlsDestructor.size;i++)
  386. {
  387. void *ptr = pthread_getspecific(TlsDestructor.array[i].key);
  388. void (*callback)(void*) = (void(*)(void*))TlsDestructor.array[i].value;
  389. if(ptr && callback)
  390. callback(ptr);
  391. }
  392. UnlockUIntMapRead(&TlsDestructor);
  393. break;
  394. case DLL_PROCESS_DETACH:
  395. if(!lpReserved)
  396. alc_deinit();
  397. else
  398. alc_deinit_safe();
  399. ResetUIntMap(&TlsDestructor);
  400. break;
  401. }
  402. return TRUE;
  403. }
  404. #elif defined(_MSC_VER)
  405. #pragma section(".CRT$XCU",read)
  406. static void alc_constructor(void);
  407. static void alc_destructor(void);
  408. __declspec(allocate(".CRT$XCU")) void (__cdecl* alc_constructor_)(void) = alc_constructor;
  409. static void alc_constructor(void)
  410. {
  411. atexit(alc_destructor);
  412. alc_init();
  413. }
  414. static void alc_destructor(void)
  415. {
  416. alc_deinit();
  417. }
  418. #elif defined(HAVE_GCC_DESTRUCTOR)
  419. static void alc_init(void) __attribute__((constructor));
  420. static void alc_deinit(void) __attribute__((destructor));
  421. #else
  422. #error "No static initialization available on this platform!"
  423. #endif
  424. #elif defined(HAVE_GCC_DESTRUCTOR)
  425. static void alc_init(void) __attribute__((constructor));
  426. static void alc_deinit(void) __attribute__((destructor));
  427. #else
  428. #error "No global initialization available on this platform!"
  429. #endif
  430. static void alc_init(void)
  431. {
  432. const char *str;
  433. LogFile = stderr;
  434. str = getenv("__ALSOFT_HALF_ANGLE_CONES");
  435. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  436. ConeScale = 1.0f;
  437. str = getenv("__ALSOFT_REVERSE_Z");
  438. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  439. ZScale = -1.0f;
  440. str = getenv("ALSOFT_TRAP_ERROR");
  441. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  442. {
  443. TrapALError = AL_TRUE;
  444. TrapALCError = AL_TRUE;
  445. }
  446. else
  447. {
  448. str = getenv("ALSOFT_TRAP_AL_ERROR");
  449. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  450. TrapALError = AL_TRUE;
  451. str = getenv("ALSOFT_TRAP_ALC_ERROR");
  452. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  453. TrapALCError = ALC_TRUE;
  454. }
  455. pthread_key_create(&LocalContext, ReleaseThreadCtx);
  456. InitializeCriticalSection(&ListLock);
  457. ThunkInit();
  458. }
  459. static void alc_deinit_safe(void)
  460. {
  461. ReleaseALC();
  462. FreeHrtf();
  463. FreeALConfig();
  464. ThunkExit();
  465. DeleteCriticalSection(&ListLock);
  466. pthread_key_delete(LocalContext);
  467. if(LogFile != stderr)
  468. fclose(LogFile);
  469. LogFile = NULL;
  470. }
  471. static void alc_deinit(void)
  472. {
  473. int i;
  474. ReleaseALC();
  475. memset(&PlaybackBackend, 0, sizeof(PlaybackBackend));
  476. memset(&CaptureBackend, 0, sizeof(CaptureBackend));
  477. for(i = 0;BackendList[i].Deinit;i++)
  478. BackendList[i].Deinit();
  479. BackendLoopback.Deinit();
  480. alc_deinit_safe();
  481. }
  482. static void alc_initconfig(void)
  483. {
  484. const char *devs, *str;
  485. float valf;
  486. int i, n;
  487. str = getenv("ALSOFT_LOGLEVEL");
  488. if(str)
  489. {
  490. long lvl = strtol(str, NULL, 0);
  491. if(lvl >= NoLog && lvl <= LogRef)
  492. LogLevel = lvl;
  493. }
  494. str = getenv("ALSOFT_LOGFILE");
  495. if(str && str[0])
  496. {
  497. FILE *logfile = fopen(str, "wat");
  498. if(logfile) LogFile = logfile;
  499. else ERR("Failed to open log file '%s'\n", str);
  500. }
  501. ReadALConfig();
  502. InitHrtf();
  503. #ifdef _WIN32
  504. RTPrioLevel = 1;
  505. #else
  506. RTPrioLevel = 0;
  507. #endif
  508. ConfigValueInt(NULL, "rt-prio", &RTPrioLevel);
  509. if(ConfigValueStr(NULL, "resampler", &str))
  510. {
  511. if(strcasecmp(str, "point") == 0 || strcasecmp(str, "none") == 0)
  512. DefaultResampler = PointResampler;
  513. else if(strcasecmp(str, "linear") == 0)
  514. DefaultResampler = LinearResampler;
  515. else if(strcasecmp(str, "cubic") == 0)
  516. DefaultResampler = CubicResampler;
  517. else
  518. {
  519. char *end;
  520. n = strtol(str, &end, 0);
  521. if(*end == '\0' && (n == PointResampler || n == LinearResampler || n == CubicResampler))
  522. DefaultResampler = n;
  523. else
  524. WARN("Invalid resampler: %s\n", str);
  525. }
  526. }
  527. if(!TrapALCError)
  528. TrapALCError = GetConfigValueBool(NULL, "trap-alc-error", ALC_FALSE);
  529. if(!TrapALError)
  530. TrapALError = GetConfigValueBool(NULL, "trap-al-error", AL_FALSE);
  531. if(ConfigValueFloat("reverb", "boost", &valf))
  532. ReverbBoost *= aluPow(10.0f, valf / 20.0f);
  533. EmulateEAXReverb = GetConfigValueBool("reverb", "emulate-eax", AL_FALSE);
  534. if(((devs=getenv("ALSOFT_DRIVERS")) && devs[0]) ||
  535. ConfigValueStr(NULL, "drivers", &devs))
  536. {
  537. int n;
  538. size_t len;
  539. const char *next = devs;
  540. int endlist, delitem;
  541. i = 0;
  542. do {
  543. devs = next;
  544. next = strchr(devs, ',');
  545. delitem = (devs[0] == '-');
  546. if(devs[0] == '-') devs++;
  547. if(!devs[0] || devs[0] == ',')
  548. {
  549. endlist = 0;
  550. continue;
  551. }
  552. endlist = 1;
  553. len = (next ? ((size_t)(next-devs)) : strlen(devs));
  554. for(n = i;BackendList[n].Init;n++)
  555. {
  556. if(len == strlen(BackendList[n].name) &&
  557. strncmp(BackendList[n].name, devs, len) == 0)
  558. {
  559. if(delitem)
  560. {
  561. do {
  562. BackendList[n] = BackendList[n+1];
  563. ++n;
  564. } while(BackendList[n].Init);
  565. }
  566. else
  567. {
  568. struct BackendInfo Bkp = BackendList[n];
  569. while(n > i)
  570. {
  571. BackendList[n] = BackendList[n-1];
  572. --n;
  573. }
  574. BackendList[n] = Bkp;
  575. i++;
  576. }
  577. break;
  578. }
  579. }
  580. } while(next++);
  581. if(endlist)
  582. {
  583. BackendList[i].name = NULL;
  584. BackendList[i].Init = NULL;
  585. BackendList[i].Deinit = NULL;
  586. BackendList[i].Probe = NULL;
  587. }
  588. }
  589. for(i = 0;BackendList[i].Init && (!PlaybackBackend.name || !CaptureBackend.name);i++)
  590. {
  591. if(!BackendList[i].Init(&BackendList[i].Funcs))
  592. {
  593. WARN("Failed to initialize backend \"%s\"\n", BackendList[i].name);
  594. continue;
  595. }
  596. TRACE("Initialized backend \"%s\"\n", BackendList[i].name);
  597. if(BackendList[i].Funcs.OpenPlayback && !PlaybackBackend.name)
  598. {
  599. PlaybackBackend = BackendList[i];
  600. TRACE("Added \"%s\" for playback\n", PlaybackBackend.name);
  601. }
  602. if(BackendList[i].Funcs.OpenCapture && !CaptureBackend.name)
  603. {
  604. CaptureBackend = BackendList[i];
  605. TRACE("Added \"%s\" for capture\n", CaptureBackend.name);
  606. }
  607. }
  608. BackendLoopback.Init(&BackendLoopback.Funcs);
  609. if(ConfigValueStr(NULL, "excludefx", &str))
  610. {
  611. size_t len;
  612. const char *next = str;
  613. do {
  614. str = next;
  615. next = strchr(str, ',');
  616. if(!str[0] || next == str)
  617. continue;
  618. len = (next ? ((size_t)(next-str)) : strlen(str));
  619. for(n = 0;EffectList[n].name;n++)
  620. {
  621. if(len == strlen(EffectList[n].name) &&
  622. strncmp(EffectList[n].name, str, len) == 0)
  623. DisabledEffects[EffectList[n].type] = AL_TRUE;
  624. }
  625. } while(next++);
  626. }
  627. InitEffect(&DefaultEffect);
  628. str = getenv("ALSOFT_DEFAULT_REVERB");
  629. if((str && str[0]) || ConfigValueStr(NULL, "default-reverb", &str))
  630. LoadReverbPreset(str, &DefaultEffect);
  631. }
  632. static void LockLists(void)
  633. {
  634. EnterCriticalSection(&ListLock);
  635. }
  636. static void UnlockLists(void)
  637. {
  638. LeaveCriticalSection(&ListLock);
  639. }
  640. static void ProbeList(ALCchar **list, size_t *listsize, enum DevProbe type)
  641. {
  642. DO_INITCONFIG();
  643. LockLists();
  644. free(*list);
  645. *list = NULL;
  646. *listsize = 0;
  647. if(type == ALL_DEVICE_PROBE && PlaybackBackend.Probe)
  648. PlaybackBackend.Probe(type);
  649. else if(type == CAPTURE_DEVICE_PROBE && CaptureBackend.Probe)
  650. CaptureBackend.Probe(type);
  651. UnlockLists();
  652. }
  653. static void ProbeAllDeviceList(void)
  654. { ProbeList(&alcAllDeviceList, &alcAllDeviceListSize, ALL_DEVICE_PROBE); }
  655. static void ProbeCaptureDeviceList(void)
  656. { ProbeList(&alcCaptureDeviceList, &alcCaptureDeviceListSize, CAPTURE_DEVICE_PROBE); }
  657. static void AppendList(const ALCchar *name, ALCchar **List, size_t *ListSize)
  658. {
  659. size_t len = strlen(name);
  660. void *temp;
  661. if(len == 0)
  662. return;
  663. temp = realloc(*List, (*ListSize) + len + 2);
  664. if(!temp)
  665. {
  666. ERR("Realloc failed to add %s!\n", name);
  667. return;
  668. }
  669. *List = temp;
  670. memcpy((*List)+(*ListSize), name, len+1);
  671. *ListSize += len+1;
  672. (*List)[*ListSize] = 0;
  673. }
  674. #define DECL_APPEND_LIST_FUNC(type) \
  675. void Append##type##List(const ALCchar *name) \
  676. { AppendList(name, &alc##type##List, &alc##type##ListSize); }
  677. DECL_APPEND_LIST_FUNC(AllDevice)
  678. DECL_APPEND_LIST_FUNC(CaptureDevice)
  679. #undef DECL_APPEND_LIST_FUNC
  680. /* Sets the default channel order used by most non-WaveFormatEx-based APIs */
  681. void SetDefaultChannelOrder(ALCdevice *device)
  682. {
  683. switch(device->FmtChans)
  684. {
  685. case DevFmtX51: device->DevChannels[0] = FRONT_LEFT;
  686. device->DevChannels[1] = FRONT_RIGHT;
  687. device->DevChannels[2] = BACK_LEFT;
  688. device->DevChannels[3] = BACK_RIGHT;
  689. device->DevChannels[4] = FRONT_CENTER;
  690. device->DevChannels[5] = LFE;
  691. return;
  692. case DevFmtX71: device->DevChannels[0] = FRONT_LEFT;
  693. device->DevChannels[1] = FRONT_RIGHT;
  694. device->DevChannels[2] = BACK_LEFT;
  695. device->DevChannels[3] = BACK_RIGHT;
  696. device->DevChannels[4] = FRONT_CENTER;
  697. device->DevChannels[5] = LFE;
  698. device->DevChannels[6] = SIDE_LEFT;
  699. device->DevChannels[7] = SIDE_RIGHT;
  700. return;
  701. /* Same as WFX order */
  702. case DevFmtMono:
  703. case DevFmtStereo:
  704. case DevFmtQuad:
  705. case DevFmtX51Side:
  706. case DevFmtX61:
  707. break;
  708. }
  709. SetDefaultWFXChannelOrder(device);
  710. }
  711. /* Sets the default order used by WaveFormatEx */
  712. void SetDefaultWFXChannelOrder(ALCdevice *device)
  713. {
  714. switch(device->FmtChans)
  715. {
  716. case DevFmtMono: device->DevChannels[0] = FRONT_CENTER; break;
  717. case DevFmtStereo: device->DevChannels[0] = FRONT_LEFT;
  718. device->DevChannels[1] = FRONT_RIGHT; break;
  719. case DevFmtQuad: device->DevChannels[0] = FRONT_LEFT;
  720. device->DevChannels[1] = FRONT_RIGHT;
  721. device->DevChannels[2] = BACK_LEFT;
  722. device->DevChannels[3] = BACK_RIGHT; break;
  723. case DevFmtX51: device->DevChannels[0] = FRONT_LEFT;
  724. device->DevChannels[1] = FRONT_RIGHT;
  725. device->DevChannels[2] = FRONT_CENTER;
  726. device->DevChannels[3] = LFE;
  727. device->DevChannels[4] = BACK_LEFT;
  728. device->DevChannels[5] = BACK_RIGHT; break;
  729. case DevFmtX51Side: device->DevChannels[0] = FRONT_LEFT;
  730. device->DevChannels[1] = FRONT_RIGHT;
  731. device->DevChannels[2] = FRONT_CENTER;
  732. device->DevChannels[3] = LFE;
  733. device->DevChannels[4] = SIDE_LEFT;
  734. device->DevChannels[5] = SIDE_RIGHT; break;
  735. case DevFmtX61: device->DevChannels[0] = FRONT_LEFT;
  736. device->DevChannels[1] = FRONT_RIGHT;
  737. device->DevChannels[2] = FRONT_CENTER;
  738. device->DevChannels[3] = LFE;
  739. device->DevChannels[4] = BACK_CENTER;
  740. device->DevChannels[5] = SIDE_LEFT;
  741. device->DevChannels[6] = SIDE_RIGHT; break;
  742. case DevFmtX71: device->DevChannels[0] = FRONT_LEFT;
  743. device->DevChannels[1] = FRONT_RIGHT;
  744. device->DevChannels[2] = FRONT_CENTER;
  745. device->DevChannels[3] = LFE;
  746. device->DevChannels[4] = BACK_LEFT;
  747. device->DevChannels[5] = BACK_RIGHT;
  748. device->DevChannels[6] = SIDE_LEFT;
  749. device->DevChannels[7] = SIDE_RIGHT; break;
  750. }
  751. }
  752. const ALCchar *DevFmtTypeString(enum DevFmtType type)
  753. {
  754. switch(type)
  755. {
  756. case DevFmtByte: return "Signed Byte";
  757. case DevFmtUByte: return "Unsigned Byte";
  758. case DevFmtShort: return "Signed Short";
  759. case DevFmtUShort: return "Unsigned Short";
  760. case DevFmtInt: return "Signed Int";
  761. case DevFmtUInt: return "Unsigned Int";
  762. case DevFmtFloat: return "Float";
  763. }
  764. return "(unknown type)";
  765. }
  766. const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans)
  767. {
  768. switch(chans)
  769. {
  770. case DevFmtMono: return "Mono";
  771. case DevFmtStereo: return "Stereo";
  772. case DevFmtQuad: return "Quadraphonic";
  773. case DevFmtX51: return "5.1 Surround";
  774. case DevFmtX51Side: return "5.1 Side";
  775. case DevFmtX61: return "6.1 Surround";
  776. case DevFmtX71: return "7.1 Surround";
  777. }
  778. return "(unknown channels)";
  779. }
  780. ALuint BytesFromDevFmt(enum DevFmtType type)
  781. {
  782. switch(type)
  783. {
  784. case DevFmtByte: return sizeof(ALbyte);
  785. case DevFmtUByte: return sizeof(ALubyte);
  786. case DevFmtShort: return sizeof(ALshort);
  787. case DevFmtUShort: return sizeof(ALushort);
  788. case DevFmtInt: return sizeof(ALint);
  789. case DevFmtUInt: return sizeof(ALuint);
  790. case DevFmtFloat: return sizeof(ALfloat);
  791. }
  792. return 0;
  793. }
  794. ALuint ChannelsFromDevFmt(enum DevFmtChannels chans)
  795. {
  796. switch(chans)
  797. {
  798. case DevFmtMono: return 1;
  799. case DevFmtStereo: return 2;
  800. case DevFmtQuad: return 4;
  801. case DevFmtX51: return 6;
  802. case DevFmtX51Side: return 6;
  803. case DevFmtX61: return 7;
  804. case DevFmtX71: return 8;
  805. }
  806. return 0;
  807. }
  808. static ALboolean DecomposeDevFormat(ALenum format, enum DevFmtChannels *chans,
  809. enum DevFmtType *type)
  810. {
  811. static const struct {
  812. ALenum format;
  813. enum DevFmtChannels channels;
  814. enum DevFmtType type;
  815. } list[] = {
  816. { AL_FORMAT_MONO8, DevFmtMono, DevFmtUByte },
  817. { AL_FORMAT_MONO16, DevFmtMono, DevFmtShort },
  818. { AL_FORMAT_MONO_FLOAT32, DevFmtMono, DevFmtFloat },
  819. { AL_FORMAT_STEREO8, DevFmtStereo, DevFmtUByte },
  820. { AL_FORMAT_STEREO16, DevFmtStereo, DevFmtShort },
  821. { AL_FORMAT_STEREO_FLOAT32, DevFmtStereo, DevFmtFloat },
  822. { AL_FORMAT_QUAD8, DevFmtQuad, DevFmtUByte },
  823. { AL_FORMAT_QUAD16, DevFmtQuad, DevFmtShort },
  824. { AL_FORMAT_QUAD32, DevFmtQuad, DevFmtFloat },
  825. { AL_FORMAT_51CHN8, DevFmtX51, DevFmtUByte },
  826. { AL_FORMAT_51CHN16, DevFmtX51, DevFmtShort },
  827. { AL_FORMAT_51CHN32, DevFmtX51, DevFmtFloat },
  828. { AL_FORMAT_61CHN8, DevFmtX61, DevFmtUByte },
  829. { AL_FORMAT_61CHN16, DevFmtX61, DevFmtShort },
  830. { AL_FORMAT_61CHN32, DevFmtX61, DevFmtFloat },
  831. { AL_FORMAT_71CHN8, DevFmtX71, DevFmtUByte },
  832. { AL_FORMAT_71CHN16, DevFmtX71, DevFmtShort },
  833. { AL_FORMAT_71CHN32, DevFmtX71, DevFmtFloat },
  834. };
  835. ALuint i;
  836. for(i = 0;i < COUNTOF(list);i++)
  837. {
  838. if(list[i].format == format)
  839. {
  840. *chans = list[i].channels;
  841. *type = list[i].type;
  842. return AL_TRUE;
  843. }
  844. }
  845. return AL_FALSE;
  846. }
  847. static ALCboolean IsValidALCType(ALCenum type)
  848. {
  849. switch(type)
  850. {
  851. case ALC_BYTE_SOFT:
  852. case ALC_UNSIGNED_BYTE_SOFT:
  853. case ALC_SHORT_SOFT:
  854. case ALC_UNSIGNED_SHORT_SOFT:
  855. case ALC_INT_SOFT:
  856. case ALC_UNSIGNED_INT_SOFT:
  857. case ALC_FLOAT_SOFT:
  858. return ALC_TRUE;
  859. }
  860. return ALC_FALSE;
  861. }
  862. static ALCboolean IsValidALCChannels(ALCenum channels)
  863. {
  864. switch(channels)
  865. {
  866. case ALC_MONO_SOFT:
  867. case ALC_STEREO_SOFT:
  868. case ALC_QUAD_SOFT:
  869. case ALC_5POINT1_SOFT:
  870. case ALC_6POINT1_SOFT:
  871. case ALC_7POINT1_SOFT:
  872. return ALC_TRUE;
  873. }
  874. return ALC_FALSE;
  875. }
  876. /* alcSetError
  877. *
  878. * Stores the latest ALC Error
  879. */
  880. static void alcSetError(ALCdevice *device, ALCenum errorCode)
  881. {
  882. if(TrapALCError)
  883. {
  884. #ifdef _WIN32
  885. /* DebugBreak() will cause an exception if there is no debugger */
  886. if(IsDebuggerPresent())
  887. DebugBreak();
  888. #elif defined(SIGTRAP)
  889. raise(SIGTRAP);
  890. #endif
  891. }
  892. if(device)
  893. device->LastError = errorCode;
  894. else
  895. g_eLastNullDeviceError = errorCode;
  896. }
  897. /* UpdateDeviceParams
  898. *
  899. * Updates device parameters according to the attribute list (caller is
  900. * responsible for holding the list lock).
  901. */
  902. static ALCenum UpdateDeviceParams(ALCdevice *device, const ALCint *attrList)
  903. {
  904. ALCcontext *context;
  905. enum DevFmtChannels oldChans;
  906. enum DevFmtType oldType;
  907. ALCuint oldFreq;
  908. int oldMode;
  909. ALuint i;
  910. // Check for attributes
  911. if(device->Type == Loopback)
  912. {
  913. enum {
  914. GotFreq = 1<<0,
  915. GotChans = 1<<1,
  916. GotType = 1<<2,
  917. GotAll = GotFreq|GotChans|GotType
  918. };
  919. ALCuint freq, numMono, numStereo, numSends;
  920. enum DevFmtChannels schans;
  921. enum DevFmtType stype;
  922. ALCuint attrIdx = 0;
  923. ALCint gotFmt = 0;
  924. if(!attrList)
  925. {
  926. WARN("Missing attributes for loopback device\n");
  927. return ALC_INVALID_VALUE;
  928. }
  929. numMono = device->NumMonoSources;
  930. numStereo = device->NumStereoSources;
  931. numSends = device->NumAuxSends;
  932. schans = device->FmtChans;
  933. stype = device->FmtType;
  934. freq = device->Frequency;
  935. while(attrList[attrIdx])
  936. {
  937. if(attrList[attrIdx] == ALC_FORMAT_CHANNELS_SOFT)
  938. {
  939. ALCint val = attrList[attrIdx + 1];
  940. if(!IsValidALCChannels(val) || !ChannelsFromDevFmt(val))
  941. return ALC_INVALID_VALUE;
  942. schans = val;
  943. gotFmt |= GotChans;
  944. }
  945. if(attrList[attrIdx] == ALC_FORMAT_TYPE_SOFT)
  946. {
  947. ALCint val = attrList[attrIdx + 1];
  948. if(!IsValidALCType(val) || !BytesFromDevFmt(val))
  949. return ALC_INVALID_VALUE;
  950. stype = val;
  951. gotFmt |= GotType;
  952. }
  953. if(attrList[attrIdx] == ALC_FREQUENCY)
  954. {
  955. freq = attrList[attrIdx + 1];
  956. if(freq < MIN_OUTPUT_RATE)
  957. return ALC_INVALID_VALUE;
  958. gotFmt |= GotFreq;
  959. }
  960. if(attrList[attrIdx] == ALC_STEREO_SOURCES)
  961. {
  962. numStereo = attrList[attrIdx + 1];
  963. if(numStereo > device->MaxNoOfSources)
  964. numStereo = device->MaxNoOfSources;
  965. numMono = device->MaxNoOfSources - numStereo;
  966. }
  967. if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
  968. numSends = attrList[attrIdx + 1];
  969. attrIdx += 2;
  970. }
  971. if(gotFmt != GotAll)
  972. {
  973. WARN("Missing format for loopback device\n");
  974. return ALC_INVALID_VALUE;
  975. }
  976. ConfigValueUInt(NULL, "sends", &numSends);
  977. numSends = minu(MAX_SENDS, numSends);
  978. if((device->Flags&DEVICE_RUNNING))
  979. ALCdevice_StopPlayback(device);
  980. device->Flags &= ~DEVICE_RUNNING;
  981. device->Frequency = freq;
  982. device->FmtChans = schans;
  983. device->FmtType = stype;
  984. device->NumMonoSources = numMono;
  985. device->NumStereoSources = numStereo;
  986. device->NumAuxSends = numSends;
  987. }
  988. else if(attrList && attrList[0])
  989. {
  990. ALCuint freq, numMono, numStereo, numSends;
  991. ALCuint attrIdx = 0;
  992. /* If a context is already running on the device, stop playback so the
  993. * device attributes can be updated. */
  994. if((device->Flags&DEVICE_RUNNING))
  995. ALCdevice_StopPlayback(device);
  996. device->Flags &= ~DEVICE_RUNNING;
  997. freq = device->Frequency;
  998. numMono = device->NumMonoSources;
  999. numStereo = device->NumStereoSources;
  1000. numSends = device->NumAuxSends;
  1001. while(attrList[attrIdx])
  1002. {
  1003. if(attrList[attrIdx] == ALC_FREQUENCY)
  1004. {
  1005. freq = attrList[attrIdx + 1];
  1006. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  1007. }
  1008. if(attrList[attrIdx] == ALC_STEREO_SOURCES)
  1009. {
  1010. numStereo = attrList[attrIdx + 1];
  1011. if(numStereo > device->MaxNoOfSources)
  1012. numStereo = device->MaxNoOfSources;
  1013. numMono = device->MaxNoOfSources - numStereo;
  1014. }
  1015. if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
  1016. numSends = attrList[attrIdx + 1];
  1017. attrIdx += 2;
  1018. }
  1019. ConfigValueUInt(NULL, "frequency", &freq);
  1020. freq = maxu(freq, MIN_OUTPUT_RATE);
  1021. ConfigValueUInt(NULL, "sends", &numSends);
  1022. numSends = minu(MAX_SENDS, numSends);
  1023. device->UpdateSize = (ALuint64)device->UpdateSize * freq /
  1024. device->Frequency;
  1025. device->Frequency = freq;
  1026. device->NumMonoSources = numMono;
  1027. device->NumStereoSources = numStereo;
  1028. device->NumAuxSends = numSends;
  1029. }
  1030. if((device->Flags&DEVICE_RUNNING))
  1031. return ALC_NO_ERROR;
  1032. oldFreq = device->Frequency;
  1033. oldChans = device->FmtChans;
  1034. oldType = device->FmtType;
  1035. TRACE("Format pre-setup: %s%s, %s%s, %uhz%s, %u update size x%d\n",
  1036. DevFmtChannelsString(device->FmtChans),
  1037. (device->Flags&DEVICE_CHANNELS_REQUEST)?" (requested)":"",
  1038. DevFmtTypeString(device->FmtType),
  1039. (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST)?" (requested)":"",
  1040. device->Frequency,
  1041. (device->Flags&DEVICE_FREQUENCY_REQUEST)?" (requested)":"",
  1042. device->UpdateSize, device->NumUpdates);
  1043. if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
  1044. return ALC_INVALID_DEVICE;
  1045. if(device->FmtChans != oldChans && (device->Flags&DEVICE_CHANNELS_REQUEST))
  1046. {
  1047. ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans),
  1048. DevFmtChannelsString(device->FmtChans));
  1049. device->Flags &= ~DEVICE_CHANNELS_REQUEST;
  1050. }
  1051. if(device->FmtType != oldType && (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
  1052. {
  1053. ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType),
  1054. DevFmtTypeString(device->FmtType));
  1055. device->Flags &= ~DEVICE_SAMPLE_TYPE_REQUEST;
  1056. }
  1057. if(device->Frequency != oldFreq && (device->Flags&DEVICE_FREQUENCY_REQUEST))
  1058. {
  1059. ERR("Failed to set %uhz, got %uhz instead\n", oldFreq, device->Frequency);
  1060. device->Flags &= ~DEVICE_FREQUENCY_REQUEST;
  1061. }
  1062. TRACE("Format post-setup: %s, %s, %uhz, %u update size x%d\n",
  1063. DevFmtChannelsString(device->FmtChans),
  1064. DevFmtTypeString(device->FmtType), device->Frequency,
  1065. device->UpdateSize, device->NumUpdates);
  1066. aluInitPanning(device);
  1067. for(i = 0;i < MAXCHANNELS;i++)
  1068. {
  1069. device->ClickRemoval[i] = 0.0f;
  1070. device->PendingClicks[i] = 0.0f;
  1071. }
  1072. device->Hrtf = NULL;
  1073. if(device->Type != Loopback && GetConfigValueBool(NULL, "hrtf", AL_FALSE))
  1074. device->Hrtf = GetHrtf(device);
  1075. TRACE("HRTF %s\n", device->Hrtf?"enabled":"disabled");
  1076. if(!device->Hrtf && device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
  1077. {
  1078. if(!device->Bs2b)
  1079. {
  1080. device->Bs2b = calloc(1, sizeof(*device->Bs2b));
  1081. bs2b_clear(device->Bs2b);
  1082. }
  1083. bs2b_set_srate(device->Bs2b, device->Frequency);
  1084. bs2b_set_level(device->Bs2b, device->Bs2bLevel);
  1085. TRACE("BS2B level %d\n", device->Bs2bLevel);
  1086. }
  1087. else
  1088. {
  1089. free(device->Bs2b);
  1090. device->Bs2b = NULL;
  1091. TRACE("BS2B disabled\n");
  1092. }
  1093. device->Flags &= ~DEVICE_DUPLICATE_STEREO;
  1094. switch(device->FmtChans)
  1095. {
  1096. case DevFmtMono:
  1097. case DevFmtStereo:
  1098. break;
  1099. case DevFmtQuad:
  1100. case DevFmtX51:
  1101. case DevFmtX51Side:
  1102. case DevFmtX61:
  1103. case DevFmtX71:
  1104. if(GetConfigValueBool(NULL, "stereodup", AL_TRUE))
  1105. device->Flags |= DEVICE_DUPLICATE_STEREO;
  1106. break;
  1107. }
  1108. TRACE("Stereo duplication %s\n", (device->Flags&DEVICE_DUPLICATE_STEREO)?"enabled":"disabled");
  1109. oldMode = SetMixerFPUMode();
  1110. LockDevice(device);
  1111. context = device->ContextList;
  1112. while(context)
  1113. {
  1114. ALsizei pos;
  1115. context->UpdateSources = AL_FALSE;
  1116. LockUIntMapRead(&context->EffectSlotMap);
  1117. for(pos = 0;pos < context->EffectSlotMap.size;pos++)
  1118. {
  1119. ALeffectslot *slot = context->EffectSlotMap.array[pos].value;
  1120. if(ALeffectState_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
  1121. {
  1122. UnlockUIntMapRead(&context->EffectSlotMap);
  1123. UnlockDevice(device);
  1124. RestoreFPUMode(oldMode);
  1125. return ALC_INVALID_DEVICE;
  1126. }
  1127. slot->NeedsUpdate = AL_FALSE;
  1128. ALeffectState_Update(slot->EffectState, device, slot);
  1129. }
  1130. UnlockUIntMapRead(&context->EffectSlotMap);
  1131. LockUIntMapRead(&context->SourceMap);
  1132. for(pos = 0;pos < context->SourceMap.size;pos++)
  1133. {
  1134. ALsource *source = context->SourceMap.array[pos].value;
  1135. ALuint s = device->NumAuxSends;
  1136. while(s < MAX_SENDS)
  1137. {
  1138. if(source->Send[s].Slot)
  1139. DecrementRef(&source->Send[s].Slot->ref);
  1140. source->Send[s].Slot = NULL;
  1141. source->Send[s].WetGain = 1.0f;
  1142. source->Send[s].WetGainHF = 1.0f;
  1143. s++;
  1144. }
  1145. source->NeedsUpdate = AL_FALSE;
  1146. ALsource_Update(source, context);
  1147. }
  1148. UnlockUIntMapRead(&context->SourceMap);
  1149. context = context->next;
  1150. }
  1151. if(device->DefaultSlot)
  1152. {
  1153. ALeffectslot *slot = device->DefaultSlot;
  1154. if(ALeffectState_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
  1155. {
  1156. UnlockDevice(device);
  1157. RestoreFPUMode(oldMode);
  1158. return ALC_INVALID_DEVICE;
  1159. }
  1160. slot->NeedsUpdate = AL_FALSE;
  1161. ALeffectState_Update(slot->EffectState, device, slot);
  1162. }
  1163. UnlockDevice(device);
  1164. RestoreFPUMode(oldMode);
  1165. if(ALCdevice_StartPlayback(device) == ALC_FALSE)
  1166. return ALC_INVALID_DEVICE;
  1167. device->Flags |= DEVICE_RUNNING;
  1168. return ALC_NO_ERROR;
  1169. }
  1170. /* FreeDevice
  1171. *
  1172. * Frees the device structure, and destroys any objects the app failed to
  1173. * delete. Called once there's no more references on the device.
  1174. */
  1175. static ALCvoid FreeDevice(ALCdevice *device)
  1176. {
  1177. TRACE("%p\n", device);
  1178. if(device->DefaultSlot)
  1179. {
  1180. ALeffectState_Destroy(device->DefaultSlot->EffectState);
  1181. device->DefaultSlot->EffectState = NULL;
  1182. }
  1183. if(device->BufferMap.size > 0)
  1184. {
  1185. WARN("(%p) Deleting %d Buffer(s)\n", device, device->BufferMap.size);
  1186. ReleaseALBuffers(device);
  1187. }
  1188. ResetUIntMap(&device->BufferMap);
  1189. if(device->EffectMap.size > 0)
  1190. {
  1191. WARN("(%p) Deleting %d Effect(s)\n", device, device->EffectMap.size);
  1192. ReleaseALEffects(device);
  1193. }
  1194. ResetUIntMap(&device->EffectMap);
  1195. if(device->FilterMap.size > 0)
  1196. {
  1197. WARN("(%p) Deleting %d Filter(s)\n", device, device->FilterMap.size);
  1198. ReleaseALFilters(device);
  1199. }
  1200. ResetUIntMap(&device->FilterMap);
  1201. free(device->Bs2b);
  1202. device->Bs2b = NULL;
  1203. free(device->szDeviceName);
  1204. device->szDeviceName = NULL;
  1205. DeleteCriticalSection(&device->Mutex);
  1206. free(device);
  1207. }
  1208. void ALCdevice_IncRef(ALCdevice *device)
  1209. {
  1210. RefCount ref;
  1211. ref = IncrementRef(&device->ref);
  1212. TRACEREF("%p increasing refcount to %u\n", device, ref);
  1213. }
  1214. void ALCdevice_DecRef(ALCdevice *device)
  1215. {
  1216. RefCount ref;
  1217. ref = DecrementRef(&device->ref);
  1218. TRACEREF("%p decreasing refcount to %u\n", device, ref);
  1219. if(ref == 0) FreeDevice(device);
  1220. }
  1221. /* VerifyDevice
  1222. *
  1223. * Checks if the device handle is valid, and increments its ref count if so.
  1224. */
  1225. static ALCdevice *VerifyDevice(ALCdevice *device)
  1226. {
  1227. ALCdevice *tmpDevice;
  1228. if(!device)
  1229. return NULL;
  1230. LockLists();
  1231. tmpDevice = DeviceList;
  1232. while(tmpDevice && tmpDevice != device)
  1233. tmpDevice = tmpDevice->next;
  1234. if(tmpDevice)
  1235. ALCdevice_IncRef(tmpDevice);
  1236. UnlockLists();
  1237. return tmpDevice;
  1238. }
  1239. /* InitContext
  1240. *
  1241. * Initializes context variables
  1242. */
  1243. static ALvoid InitContext(ALCcontext *pContext)
  1244. {
  1245. ALint i, j;
  1246. //Initialise listener
  1247. pContext->Listener.Gain = 1.0f;
  1248. pContext->Listener.MetersPerUnit = 1.0f;
  1249. pContext->Listener.Position[0] = 0.0f;
  1250. pContext->Listener.Position[1] = 0.0f;
  1251. pContext->Listener.Position[2] = 0.0f;
  1252. pContext->Listener.Velocity[0] = 0.0f;
  1253. pContext->Listener.Velocity[1] = 0.0f;
  1254. pContext->Listener.Velocity[2] = 0.0f;
  1255. pContext->Listener.Forward[0] = 0.0f;
  1256. pContext->Listener.Forward[1] = 0.0f;
  1257. pContext->Listener.Forward[2] = -1.0f;
  1258. pContext->Listener.Up[0] = 0.0f;
  1259. pContext->Listener.Up[1] = 1.0f;
  1260. pContext->Listener.Up[2] = 0.0f;
  1261. for(i = 0;i < 4;i++)
  1262. {
  1263. for(j = 0;j < 4;j++)
  1264. pContext->Listener.Matrix[i][j] = ((i==j) ? 1.0f : 0.0f);
  1265. }
  1266. //Validate pContext
  1267. pContext->LastError = AL_NO_ERROR;
  1268. pContext->UpdateSources = AL_FALSE;
  1269. pContext->ActiveSourceCount = 0;
  1270. InitUIntMap(&pContext->SourceMap, pContext->Device->MaxNoOfSources);
  1271. InitUIntMap(&pContext->EffectSlotMap, pContext->Device->AuxiliaryEffectSlotMax);
  1272. //Set globals
  1273. pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
  1274. pContext->SourceDistanceModel = AL_FALSE;
  1275. pContext->DopplerFactor = 1.0f;
  1276. pContext->DopplerVelocity = 1.0f;
  1277. pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
  1278. pContext->DeferUpdates = AL_FALSE;
  1279. pContext->ExtensionList = alExtList;
  1280. }
  1281. /* FreeContext
  1282. *
  1283. * Cleans up the context, and destroys any remaining objects the app failed to
  1284. * delete. Called once there's no more references on the context.
  1285. */
  1286. static ALCvoid FreeContext(ALCcontext *context)
  1287. {
  1288. TRACE("%p\n", context);
  1289. if(context->SourceMap.size > 0)
  1290. {
  1291. ERR("(%p) Deleting %d Source(s)\n", context, context->SourceMap.size);
  1292. ReleaseALSources(context);
  1293. }
  1294. ResetUIntMap(&context->SourceMap);
  1295. if(context->EffectSlotMap.size > 0)
  1296. {
  1297. ERR("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context, context->EffectSlotMap.size);
  1298. ReleaseALAuxiliaryEffectSlots(context);
  1299. }
  1300. ResetUIntMap(&context->EffectSlotMap);
  1301. context->ActiveSourceCount = 0;
  1302. free(context->ActiveSources);
  1303. context->ActiveSources = NULL;
  1304. context->MaxActiveSources = 0;
  1305. context->ActiveEffectSlotCount = 0;
  1306. free(context->ActiveEffectSlots);
  1307. context->ActiveEffectSlots = NULL;
  1308. context->MaxActiveEffectSlots = 0;
  1309. ALCdevice_DecRef(context->Device);
  1310. context->Device = NULL;
  1311. //Invalidate context
  1312. memset(context, 0, sizeof(ALCcontext));
  1313. free(context);
  1314. }
  1315. /* ReleaseContext
  1316. *
  1317. * Removes the context reference from the given device and removes it from
  1318. * being current on the running thread or globally.
  1319. */
  1320. static void ReleaseContext(ALCcontext *context, ALCdevice *device)
  1321. {
  1322. ALCcontext *volatile*tmp_ctx;
  1323. if(pthread_getspecific(LocalContext) == context)
  1324. {
  1325. WARN("%p released while current on thread\n", context);
  1326. pthread_setspecific(LocalContext, NULL);
  1327. ALCcontext_DecRef(context);
  1328. }
  1329. if(CompExchangePtr((XchgPtr*)&GlobalContext, context, NULL))
  1330. ALCcontext_DecRef(context);
  1331. LockDevice(device);
  1332. tmp_ctx = &device->ContextList;
  1333. while(*tmp_ctx)
  1334. {
  1335. if(CompExchangePtr((XchgPtr*)tmp_ctx, context, context->next))
  1336. break;
  1337. tmp_ctx = &(*tmp_ctx)->next;
  1338. }
  1339. UnlockDevice(device);
  1340. ALCcontext_DecRef(context);
  1341. }
  1342. void ALCcontext_IncRef(ALCcontext *context)
  1343. {
  1344. RefCount ref;
  1345. ref = IncrementRef(&context->ref);
  1346. TRACEREF("%p increasing refcount to %u\n", context, ref);
  1347. }
  1348. void ALCcontext_DecRef(ALCcontext *context)
  1349. {
  1350. RefCount ref;
  1351. ref = DecrementRef(&context->ref);
  1352. TRACEREF("%p decreasing refcount to %u\n", context, ref);
  1353. if(ref == 0) FreeContext(context);
  1354. }
  1355. static void ReleaseThreadCtx(void *ptr)
  1356. {
  1357. WARN("%p current for thread being destroyed\n", ptr);
  1358. ALCcontext_DecRef(ptr);
  1359. }
  1360. /* VerifyContext
  1361. *
  1362. * Checks that the given context is valid, and increments its reference count.
  1363. */
  1364. static ALCcontext *VerifyContext(ALCcontext *context)
  1365. {
  1366. ALCdevice *dev;
  1367. LockLists();
  1368. dev = DeviceList;
  1369. while(dev)
  1370. {
  1371. ALCcontext *tmp_ctx = dev->ContextList;
  1372. while(tmp_ctx)
  1373. {
  1374. if(tmp_ctx == context)
  1375. {
  1376. ALCcontext_IncRef(tmp_ctx);
  1377. UnlockLists();
  1378. return tmp_ctx;
  1379. }
  1380. tmp_ctx = tmp_ctx->next;
  1381. }
  1382. dev = dev->next;
  1383. }
  1384. UnlockLists();
  1385. return NULL;
  1386. }
  1387. /* GetContextRef
  1388. *
  1389. * Returns the currently active context, and adds a reference without locking
  1390. * it.
  1391. */
  1392. ALCcontext *GetContextRef(void)
  1393. {
  1394. ALCcontext *context;
  1395. context = pthread_getspecific(LocalContext);
  1396. if(context)
  1397. ALCcontext_IncRef(context);
  1398. else
  1399. {
  1400. LockLists();
  1401. context = GlobalContext;
  1402. if(context)
  1403. ALCcontext_IncRef(context);
  1404. UnlockLists();
  1405. }
  1406. return context;
  1407. }
  1408. ///////////////////////////////////////////////////////
  1409. ///////////////////////////////////////////////////////
  1410. // ALC Functions calls
  1411. // This should probably move to another c file but for now ...
  1412. ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
  1413. {
  1414. ALCdevice *device = NULL;
  1415. ALCenum err;
  1416. DO_INITCONFIG();
  1417. if(!CaptureBackend.name)
  1418. {
  1419. alcSetError(NULL, ALC_INVALID_VALUE);
  1420. return NULL;
  1421. }
  1422. if(SampleSize <= 0)
  1423. {
  1424. alcSetError(NULL, ALC_INVALID_VALUE);
  1425. return NULL;
  1426. }
  1427. if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
  1428. deviceName = NULL;
  1429. device = calloc(1, sizeof(ALCdevice));
  1430. if(!device)
  1431. {
  1432. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  1433. return NULL;
  1434. }
  1435. //Validate device
  1436. device->Funcs = &CaptureBackend.Funcs;
  1437. device->ref = 1;
  1438. device->Connected = ALC_TRUE;
  1439. device->Type = Capture;
  1440. InitializeCriticalSection(&device->Mutex);
  1441. InitUIntMap(&device->BufferMap, ~0);
  1442. InitUIntMap(&device->EffectMap, ~0);
  1443. InitUIntMap(&device->FilterMap, ~0);
  1444. device->szDeviceName = NULL;
  1445. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  1446. device->Frequency = frequency;
  1447. device->Flags |= DEVICE_CHANNELS_REQUEST | DEVICE_SAMPLE_TYPE_REQUEST;
  1448. if(DecomposeDevFormat(format, &device->FmtChans, &device->FmtType) == AL_FALSE)
  1449. {
  1450. DeleteCriticalSection(&device->Mutex);
  1451. free(device);
  1452. alcSetError(NULL, ALC_INVALID_ENUM);
  1453. return NULL;
  1454. }
  1455. device->UpdateSize = SampleSize;
  1456. device->NumUpdates = 1;
  1457. LockLists();
  1458. if((err=ALCdevice_OpenCapture(device, deviceName)) != ALC_NO_ERROR)
  1459. {
  1460. UnlockLists();
  1461. DeleteCriticalSection(&device->Mutex);
  1462. free(device);
  1463. alcSetError(NULL, err);
  1464. return NULL;
  1465. }
  1466. UnlockLists();
  1467. do {
  1468. device->next = DeviceList;
  1469. } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
  1470. TRACE("Created device %p\n", device);
  1471. return device;
  1472. }
  1473. ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
  1474. {
  1475. ALCdevice *volatile*list;
  1476. LockLists();
  1477. list = &DeviceList;
  1478. while(*list && *list != pDevice)
  1479. list = &(*list)->next;
  1480. if(!*list || (*list)->Type != Capture)
  1481. {
  1482. alcSetError(*list, ALC_INVALID_DEVICE);
  1483. UnlockLists();
  1484. return ALC_FALSE;
  1485. }
  1486. *list = (*list)->next;
  1487. UnlockLists();
  1488. ALCdevice_CloseCapture(pDevice);
  1489. ALCdevice_DecRef(pDevice);
  1490. return ALC_TRUE;
  1491. }
  1492. ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
  1493. {
  1494. LockLists();
  1495. if(!(device=VerifyDevice(device)) || device->Type != Capture)
  1496. {
  1497. UnlockLists();
  1498. alcSetError(device, ALC_INVALID_DEVICE);
  1499. if(device) ALCdevice_DecRef(device);
  1500. return;
  1501. }
  1502. if(device->Connected)
  1503. {
  1504. if(!(device->Flags&DEVICE_RUNNING))
  1505. ALCdevice_StartCapture(device);
  1506. device->Flags |= DEVICE_RUNNING;
  1507. }
  1508. UnlockLists();
  1509. ALCdevice_DecRef(device);
  1510. }
  1511. ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
  1512. {
  1513. LockLists();
  1514. if(!(device=VerifyDevice(device)) || device->Type != Capture)
  1515. {
  1516. UnlockLists();
  1517. alcSetError(device, ALC_INVALID_DEVICE);
  1518. if(device) ALCdevice_DecRef(device);
  1519. return;
  1520. }
  1521. if((device->Flags&DEVICE_RUNNING))
  1522. ALCdevice_StopCapture(device);
  1523. device->Flags &= ~DEVICE_RUNNING;
  1524. UnlockLists();
  1525. ALCdevice_DecRef(device);
  1526. }
  1527. ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
  1528. {
  1529. ALCenum err = ALC_INVALID_DEVICE;
  1530. LockLists();
  1531. if((device=VerifyDevice(device)) != NULL && device->Type == Capture)
  1532. {
  1533. err = ALC_INVALID_VALUE;
  1534. if(samples >= 0 && ALCdevice_AvailableSamples(device) >= (ALCuint)samples)
  1535. err = ALCdevice_CaptureSamples(device, buffer, samples);
  1536. }
  1537. UnlockLists();
  1538. if(err != ALC_NO_ERROR)
  1539. alcSetError(device, err);
  1540. if(device) ALCdevice_DecRef(device);
  1541. }
  1542. /*
  1543. alcGetError
  1544. Return last ALC generated error code
  1545. */
  1546. ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
  1547. {
  1548. ALCenum errorCode;
  1549. if(VerifyDevice(device))
  1550. {
  1551. errorCode = ExchangeInt(&device->LastError, ALC_NO_ERROR);
  1552. ALCdevice_DecRef(device);
  1553. }
  1554. else
  1555. errorCode = ExchangeInt(&g_eLastNullDeviceError, ALC_NO_ERROR);
  1556. return errorCode;
  1557. }
  1558. /* alcSuspendContext
  1559. *
  1560. * Not functional
  1561. */
  1562. ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *Context)
  1563. {
  1564. (void)Context;
  1565. }
  1566. /* alcProcessContext
  1567. *
  1568. * Not functional
  1569. */
  1570. ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *Context)
  1571. {
  1572. (void)Context;
  1573. }
  1574. /* alcGetString
  1575. *
  1576. * Returns information about the Device, and error strings
  1577. */
  1578. ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
  1579. {
  1580. const ALCchar *value = NULL;
  1581. switch(param)
  1582. {
  1583. case ALC_NO_ERROR:
  1584. value = alcNoError;
  1585. break;
  1586. case ALC_INVALID_ENUM:
  1587. value = alcErrInvalidEnum;
  1588. break;
  1589. case ALC_INVALID_VALUE:
  1590. value = alcErrInvalidValue;
  1591. break;
  1592. case ALC_INVALID_DEVICE:
  1593. value = alcErrInvalidDevice;
  1594. break;
  1595. case ALC_INVALID_CONTEXT:
  1596. value = alcErrInvalidContext;
  1597. break;
  1598. case ALC_OUT_OF_MEMORY:
  1599. value = alcErrOutOfMemory;
  1600. break;
  1601. case ALC_DEVICE_SPECIFIER:
  1602. value = alcDefaultName;
  1603. break;
  1604. case ALC_ALL_DEVICES_SPECIFIER:
  1605. if(VerifyDevice(pDevice))
  1606. {
  1607. value = pDevice->szDeviceName;
  1608. ALCdevice_DecRef(pDevice);
  1609. }
  1610. else
  1611. {
  1612. ProbeAllDeviceList();
  1613. value = alcAllDeviceList;
  1614. }
  1615. break;
  1616. case ALC_CAPTURE_DEVICE_SPECIFIER:
  1617. if(VerifyDevice(pDevice))
  1618. {
  1619. value = pDevice->szDeviceName;
  1620. ALCdevice_DecRef(pDevice);
  1621. }
  1622. else
  1623. {
  1624. ProbeCaptureDeviceList();
  1625. value = alcCaptureDeviceList;
  1626. }
  1627. break;
  1628. /* Default devices are always first in the list */
  1629. case ALC_DEFAULT_DEVICE_SPECIFIER:
  1630. value = alcDefaultName;
  1631. break;
  1632. case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
  1633. if(!alcAllDeviceList)
  1634. ProbeAllDeviceList();
  1635. pDevice = VerifyDevice(pDevice);
  1636. free(alcDefaultAllDeviceSpecifier);
  1637. alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
  1638. alcAllDeviceList : "");
  1639. if(!alcDefaultAllDeviceSpecifier)
  1640. alcSetError(pDevice, ALC_OUT_OF_MEMORY);
  1641. value = alcDefaultAllDeviceSpecifier;
  1642. if(pDevice) ALCdevice_DecRef(pDevice);
  1643. break;
  1644. case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
  1645. if(!alcCaptureDeviceList)
  1646. ProbeCaptureDeviceList();
  1647. pDevice = VerifyDevice(pDevice);
  1648. free(alcCaptureDefaultDeviceSpecifier);
  1649. alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
  1650. alcCaptureDeviceList : "");
  1651. if(!alcCaptureDefaultDeviceSpecifier)
  1652. alcSetError(pDevice, ALC_OUT_OF_MEMORY);
  1653. value = alcCaptureDefaultDeviceSpecifier;
  1654. if(pDevice) ALCdevice_DecRef(pDevice);
  1655. break;
  1656. case ALC_EXTENSIONS:
  1657. if(!VerifyDevice(pDevice))
  1658. value = alcNoDeviceExtList;
  1659. else
  1660. {
  1661. value = alcExtensionList;
  1662. ALCdevice_DecRef(pDevice);
  1663. }
  1664. break;
  1665. default:
  1666. pDevice = VerifyDevice(pDevice);
  1667. alcSetError(pDevice, ALC_INVALID_ENUM);
  1668. if(pDevice) ALCdevice_DecRef(pDevice);
  1669. break;
  1670. }
  1671. return value;
  1672. }
  1673. /* alcGetIntegerv
  1674. *
  1675. * Returns information about the Device and the version of Open AL
  1676. */
  1677. ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
  1678. {
  1679. device = VerifyDevice(device);
  1680. if(size == 0 || data == NULL)
  1681. {
  1682. alcSetError(device, ALC_INVALID_VALUE);
  1683. if(device) ALCdevice_DecRef(device);
  1684. return;
  1685. }
  1686. if(!device)
  1687. {
  1688. switch(param)
  1689. {
  1690. case ALC_MAJOR_VERSION:
  1691. *data = alcMajorVersion;
  1692. break;
  1693. case ALC_MINOR_VERSION:
  1694. *data = alcMinorVersion;
  1695. break;
  1696. case ALC_ATTRIBUTES_SIZE:
  1697. case ALC_ALL_ATTRIBUTES:
  1698. case ALC_FREQUENCY:
  1699. case ALC_REFRESH:
  1700. case ALC_SYNC:
  1701. case ALC_MONO_SOURCES:
  1702. case ALC_STEREO_SOURCES:
  1703. case ALC_CAPTURE_SAMPLES:
  1704. case ALC_FORMAT_CHANNELS_SOFT:
  1705. case ALC_FORMAT_TYPE_SOFT:
  1706. alcSetError(NULL, ALC_INVALID_DEVICE);
  1707. break;
  1708. default:
  1709. alcSetError(NULL, ALC_INVALID_ENUM);
  1710. break;
  1711. }
  1712. }
  1713. else if(device->Type == Capture)
  1714. {
  1715. switch(param)
  1716. {
  1717. case ALC_CAPTURE_SAMPLES:
  1718. LockLists();
  1719. /* Re-validate the device since it may have been closed */
  1720. ALCdevice_DecRef(device);
  1721. if((device=VerifyDevice(device)) != NULL)
  1722. *data = ALCdevice_AvailableSamples(device);
  1723. else
  1724. alcSetError(NULL, ALC_INVALID_DEVICE);
  1725. UnlockLists();
  1726. break;
  1727. case ALC_CONNECTED:
  1728. *data = device->Connected;
  1729. break;
  1730. default:
  1731. alcSetError(device, ALC_INVALID_ENUM);
  1732. break;
  1733. }
  1734. }
  1735. else /* render device */
  1736. {
  1737. switch(param)
  1738. {
  1739. case ALC_MAJOR_VERSION:
  1740. *data = alcMajorVersion;
  1741. break;
  1742. case ALC_MINOR_VERSION:
  1743. *data = alcMinorVersion;
  1744. break;
  1745. case ALC_EFX_MAJOR_VERSION:
  1746. *data = alcEFXMajorVersion;
  1747. break;
  1748. case ALC_EFX_MINOR_VERSION:
  1749. *data = alcEFXMinorVersion;
  1750. break;
  1751. case ALC_ATTRIBUTES_SIZE:
  1752. *data = 13;
  1753. break;
  1754. case ALC_ALL_ATTRIBUTES:
  1755. if(size < 13)
  1756. alcSetError(device, ALC_INVALID_VALUE);
  1757. else
  1758. {
  1759. int i = 0;
  1760. data[i++] = ALC_FREQUENCY;
  1761. data[i++] = device->Frequency;
  1762. if(device->Type != Loopback)
  1763. {
  1764. data[i++] = ALC_REFRESH;
  1765. data[i++] = device->Frequency / device->UpdateSize;
  1766. data[i++] = ALC_SYNC;
  1767. data[i++] = ALC_FALSE;
  1768. }
  1769. else
  1770. {
  1771. data[i++] = ALC_FORMAT_CHANNELS_SOFT;
  1772. data[i++] = device->FmtChans;
  1773. data[i++] = ALC_FORMAT_TYPE_SOFT;
  1774. data[i++] = device->FmtType;
  1775. }
  1776. data[i++] = ALC_MONO_SOURCES;
  1777. data[i++] = device->NumMonoSources;
  1778. data[i++] = ALC_STEREO_SOURCES;
  1779. data[i++] = device->NumStereoSources;
  1780. data[i++] = ALC_MAX_AUXILIARY_SENDS;
  1781. data[i++] = device->NumAuxSends;
  1782. data[i++] = 0;
  1783. }
  1784. break;
  1785. case ALC_FREQUENCY:
  1786. *data = device->Frequency;
  1787. break;
  1788. case ALC_REFRESH:
  1789. if(device->Type == Loopback)
  1790. alcSetError(device, ALC_INVALID_DEVICE);
  1791. else
  1792. *data = device->Frequency / device->UpdateSize;
  1793. break;
  1794. case ALC_SYNC:
  1795. if(device->Type == Loopback)
  1796. alcSetError(device, ALC_INVALID_DEVICE);
  1797. else
  1798. *data = ALC_FALSE;
  1799. break;
  1800. case ALC_FORMAT_CHANNELS_SOFT:
  1801. if(device->Type != Loopback)
  1802. alcSetError(device, ALC_INVALID_DEVICE);
  1803. else
  1804. *data = device->FmtChans;
  1805. break;
  1806. case ALC_FORMAT_TYPE_SOFT:
  1807. if(device->Type != Loopback)
  1808. alcSetError(device, ALC_INVALID_DEVICE);
  1809. else
  1810. *data = device->FmtType;
  1811. break;
  1812. case ALC_MONO_SOURCES:
  1813. *data = device->NumMonoSources;
  1814. break;
  1815. case ALC_STEREO_SOURCES:
  1816. *data = device->NumStereoSources;
  1817. break;
  1818. case ALC_MAX_AUXILIARY_SENDS:
  1819. *data = device->NumAuxSends;
  1820. break;
  1821. case ALC_CONNECTED:
  1822. *data = device->Connected;
  1823. break;
  1824. default:
  1825. alcSetError(device, ALC_INVALID_ENUM);
  1826. break;
  1827. }
  1828. }
  1829. if(device)
  1830. ALCdevice_DecRef(device);
  1831. }
  1832. /* alcIsExtensionPresent
  1833. *
  1834. * Determines if there is support for a particular extension
  1835. */
  1836. ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
  1837. {
  1838. ALCboolean bResult = ALC_FALSE;
  1839. device = VerifyDevice(device);
  1840. if(!extName)
  1841. alcSetError(device, ALC_INVALID_VALUE);
  1842. else
  1843. {
  1844. size_t len = strlen(extName);
  1845. const char *ptr = (device ? alcExtensionList : alcNoDeviceExtList);
  1846. while(ptr && *ptr)
  1847. {
  1848. if(strncasecmp(ptr, extName, len) == 0 &&
  1849. (ptr[len] == '\0' || isspace(ptr[len])))
  1850. {
  1851. bResult = ALC_TRUE;
  1852. break;
  1853. }
  1854. if((ptr=strchr(ptr, ' ')) != NULL)
  1855. {
  1856. do {
  1857. ++ptr;
  1858. } while(isspace(*ptr));
  1859. }
  1860. }
  1861. }
  1862. if(device)
  1863. ALCdevice_DecRef(device);
  1864. return bResult;
  1865. }
  1866. /* alcGetProcAddress
  1867. *
  1868. * Retrieves the function address for a particular extension function
  1869. */
  1870. ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
  1871. {
  1872. ALCvoid *ptr = NULL;
  1873. device = VerifyDevice(device);
  1874. if(!funcName)
  1875. alcSetError(device, ALC_INVALID_VALUE);
  1876. else
  1877. {
  1878. ALsizei i = 0;
  1879. while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName,funcName) != 0)
  1880. i++;
  1881. ptr = alcFunctions[i].address;
  1882. }
  1883. if(device)
  1884. ALCdevice_DecRef(device);
  1885. return ptr;
  1886. }
  1887. /* alcGetEnumValue
  1888. *
  1889. * Get the value for a particular ALC Enumerated Value
  1890. */
  1891. ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
  1892. {
  1893. ALCenum val = 0;
  1894. device = VerifyDevice(device);
  1895. if(!enumName)
  1896. alcSetError(device, ALC_INVALID_VALUE);
  1897. else
  1898. {
  1899. ALsizei i = 0;
  1900. while(enumeration[i].enumName && strcmp(enumeration[i].enumName,enumName) != 0)
  1901. i++;
  1902. val = enumeration[i].value;
  1903. }
  1904. if(device)
  1905. ALCdevice_DecRef(device);
  1906. return val;
  1907. }
  1908. /* alcCreateContext
  1909. *
  1910. * Create and attach a Context to a particular Device.
  1911. */
  1912. ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
  1913. {
  1914. ALCcontext *ALContext;
  1915. ALCenum err;
  1916. LockLists();
  1917. if(!(device=VerifyDevice(device)) || device->Type == Capture || !device->Connected)
  1918. {
  1919. UnlockLists();
  1920. alcSetError(device, ALC_INVALID_DEVICE);
  1921. if(device) ALCdevice_DecRef(device);
  1922. return NULL;
  1923. }
  1924. /* Reset Context Last Error code */
  1925. device->LastError = ALC_NO_ERROR;
  1926. if((err=UpdateDeviceParams(device, attrList)) != ALC_NO_ERROR)
  1927. {
  1928. UnlockLists();
  1929. alcSetError(device, err);
  1930. if(err == ALC_INVALID_DEVICE)
  1931. aluHandleDisconnect(device);
  1932. ALCdevice_DecRef(device);
  1933. return NULL;
  1934. }
  1935. ALContext = calloc(1, sizeof(ALCcontext));
  1936. if(ALContext)
  1937. {
  1938. ALContext->ref = 1;
  1939. ALContext->MaxActiveSources = 256;
  1940. ALContext->ActiveSources = malloc(sizeof(ALContext->ActiveSources[0]) *
  1941. ALContext->MaxActiveSources);
  1942. }
  1943. if(!ALContext || !ALContext->ActiveSources)
  1944. {
  1945. if(!device->ContextList)
  1946. {
  1947. ALCdevice_StopPlayback(device);
  1948. device->Flags &= ~DEVICE_RUNNING;
  1949. }
  1950. UnlockLists();
  1951. free(ALContext);
  1952. ALContext = NULL;
  1953. alcSetError(device, ALC_OUT_OF_MEMORY);
  1954. ALCdevice_DecRef(device);
  1955. return NULL;
  1956. }
  1957. ALContext->Device = device;
  1958. ALCdevice_IncRef(device);
  1959. InitContext(ALContext);
  1960. do {
  1961. ALContext->next = device->ContextList;
  1962. } while(!CompExchangePtr((XchgPtr*)&device->ContextList, ALContext->next, ALContext));
  1963. UnlockLists();
  1964. ALCdevice_DecRef(device);
  1965. TRACE("Created context %p\n", ALContext);
  1966. return ALContext;
  1967. }
  1968. /* alcDestroyContext
  1969. *
  1970. * Remove a Context
  1971. */
  1972. ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
  1973. {
  1974. ALCdevice *Device;
  1975. LockLists();
  1976. /* alcGetContextsDevice sets an error for invalid contexts */
  1977. Device = alcGetContextsDevice(context);
  1978. if(Device)
  1979. {
  1980. ReleaseContext(context, Device);
  1981. if(!Device->ContextList)
  1982. {
  1983. ALCdevice_StopPlayback(Device);
  1984. Device->Flags &= ~DEVICE_RUNNING;
  1985. }
  1986. }
  1987. UnlockLists();
  1988. }
  1989. /* alcGetCurrentContext
  1990. *
  1991. * Returns the currently active Context
  1992. */
  1993. ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
  1994. {
  1995. ALCcontext *Context;
  1996. Context = pthread_getspecific(LocalContext);
  1997. if(!Context) Context = GlobalContext;
  1998. return Context;
  1999. }
  2000. /* alcGetThreadContext
  2001. *
  2002. * Returns the currently active thread-local Context
  2003. */
  2004. ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
  2005. {
  2006. ALCcontext *Context;
  2007. Context = pthread_getspecific(LocalContext);
  2008. return Context;
  2009. }
  2010. /* alcMakeContextCurrent
  2011. *
  2012. * Makes the given Context the active Context
  2013. */
  2014. ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
  2015. {
  2016. /* context must be a valid Context or NULL */
  2017. if(context && !(context=VerifyContext(context)))
  2018. {
  2019. alcSetError(NULL, ALC_INVALID_CONTEXT);
  2020. return ALC_FALSE;
  2021. }
  2022. /* context's reference count is already incremented */
  2023. context = ExchangePtr((XchgPtr*)&GlobalContext, context);
  2024. if(context) ALCcontext_DecRef(context);
  2025. if((context=pthread_getspecific(LocalContext)) != NULL)
  2026. {
  2027. pthread_setspecific(LocalContext, NULL);
  2028. ALCcontext_DecRef(context);
  2029. }
  2030. return ALC_TRUE;
  2031. }
  2032. /* alcSetThreadContext
  2033. *
  2034. * Makes the given Context the active Context for the current thread
  2035. */
  2036. ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
  2037. {
  2038. ALCcontext *old;
  2039. /* context must be a valid Context or NULL */
  2040. if(context && !(context=VerifyContext(context)))
  2041. {
  2042. alcSetError(NULL, ALC_INVALID_CONTEXT);
  2043. return ALC_FALSE;
  2044. }
  2045. /* context's reference count is already incremented */
  2046. old = pthread_getspecific(LocalContext);
  2047. pthread_setspecific(LocalContext, context);
  2048. if(old) ALCcontext_DecRef(old);
  2049. return ALC_TRUE;
  2050. }
  2051. /* alcGetContextsDevice
  2052. *
  2053. * Returns the Device that a particular Context is attached to
  2054. */
  2055. ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context)
  2056. {
  2057. ALCdevice *Device;
  2058. if(!(Context=VerifyContext(Context)))
  2059. {
  2060. alcSetError(NULL, ALC_INVALID_CONTEXT);
  2061. return NULL;
  2062. }
  2063. Device = Context->Device;
  2064. ALCcontext_DecRef(Context);
  2065. return Device;
  2066. }
  2067. /* alcOpenDevice
  2068. *
  2069. * Open the Device specified.
  2070. */
  2071. ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
  2072. {
  2073. const ALCchar *fmt;
  2074. ALCdevice *device;
  2075. ALCenum err;
  2076. DO_INITCONFIG();
  2077. if(!PlaybackBackend.name)
  2078. {
  2079. alcSetError(NULL, ALC_INVALID_VALUE);
  2080. return NULL;
  2081. }
  2082. if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
  2083. deviceName = NULL;
  2084. device = calloc(1, sizeof(ALCdevice)+sizeof(ALeffectslot));
  2085. if(!device)
  2086. {
  2087. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2088. return NULL;
  2089. }
  2090. //Validate device
  2091. device->Funcs = &PlaybackBackend.Funcs;
  2092. device->ref = 1;
  2093. device->Connected = ALC_TRUE;
  2094. device->Type = Playback;
  2095. InitializeCriticalSection(&device->Mutex);
  2096. device->LastError = ALC_NO_ERROR;
  2097. device->Flags = 0;
  2098. device->Bs2b = NULL;
  2099. device->Bs2bLevel = 0;
  2100. device->szDeviceName = NULL;
  2101. device->ContextList = NULL;
  2102. device->MaxNoOfSources = 256;
  2103. device->AuxiliaryEffectSlotMax = 4;
  2104. device->NumAuxSends = MAX_SENDS;
  2105. InitUIntMap(&device->BufferMap, ~0);
  2106. InitUIntMap(&device->EffectMap, ~0);
  2107. InitUIntMap(&device->FilterMap, ~0);
  2108. //Set output format
  2109. device->FmtChans = DevFmtChannelsDefault;
  2110. device->FmtType = DevFmtTypeDefault;
  2111. device->Frequency = DEFAULT_OUTPUT_RATE;
  2112. device->NumUpdates = 4;
  2113. device->UpdateSize = 1024;
  2114. if(ConfigValueStr(NULL, "channels", &fmt))
  2115. {
  2116. static const struct {
  2117. const char name[16];
  2118. enum DevFmtChannels chans;
  2119. } chanlist[] = {
  2120. { "mono", DevFmtMono },
  2121. { "stereo", DevFmtStereo },
  2122. { "quad", DevFmtQuad },
  2123. { "surround51", DevFmtX51 },
  2124. { "surround61", DevFmtX61 },
  2125. { "surround71", DevFmtX71 },
  2126. };
  2127. size_t i;
  2128. for(i = 0;i < COUNTOF(chanlist);i++)
  2129. {
  2130. if(strcasecmp(chanlist[i].name, fmt) == 0)
  2131. {
  2132. device->FmtChans = chanlist[i].chans;
  2133. device->Flags |= DEVICE_CHANNELS_REQUEST;
  2134. break;
  2135. }
  2136. }
  2137. if(i == COUNTOF(chanlist))
  2138. ERR("Unsupported channels: %s\n", fmt);
  2139. }
  2140. if(ConfigValueStr(NULL, "sample-type", &fmt))
  2141. {
  2142. static const struct {
  2143. const char name[16];
  2144. enum DevFmtType type;
  2145. } typelist[] = {
  2146. { "int8", DevFmtByte },
  2147. { "uint8", DevFmtUByte },
  2148. { "int16", DevFmtShort },
  2149. { "uint16", DevFmtUShort },
  2150. { "int32", DevFmtInt },
  2151. { "uint32", DevFmtUInt },
  2152. { "float32", DevFmtFloat },
  2153. };
  2154. size_t i;
  2155. for(i = 0;i < COUNTOF(typelist);i++)
  2156. {
  2157. if(strcasecmp(typelist[i].name, fmt) == 0)
  2158. {
  2159. device->FmtType = typelist[i].type;
  2160. device->Flags |= DEVICE_SAMPLE_TYPE_REQUEST;
  2161. break;
  2162. }
  2163. }
  2164. if(i == COUNTOF(typelist))
  2165. ERR("Unsupported sample-type: %s\n", fmt);
  2166. }
  2167. #define DEVICE_FORMAT_REQUEST (DEVICE_CHANNELS_REQUEST|DEVICE_SAMPLE_TYPE_REQUEST)
  2168. if((device->Flags&DEVICE_FORMAT_REQUEST) != DEVICE_FORMAT_REQUEST &&
  2169. ConfigValueStr(NULL, "format", &fmt))
  2170. {
  2171. static const struct {
  2172. const char name[32];
  2173. enum DevFmtChannels channels;
  2174. enum DevFmtType type;
  2175. } formats[] = {
  2176. { "AL_FORMAT_MONO32", DevFmtMono, DevFmtFloat },
  2177. { "AL_FORMAT_STEREO32", DevFmtStereo, DevFmtFloat },
  2178. { "AL_FORMAT_QUAD32", DevFmtQuad, DevFmtFloat },
  2179. { "AL_FORMAT_51CHN32", DevFmtX51, DevFmtFloat },
  2180. { "AL_FORMAT_61CHN32", DevFmtX61, DevFmtFloat },
  2181. { "AL_FORMAT_71CHN32", DevFmtX71, DevFmtFloat },
  2182. { "AL_FORMAT_MONO16", DevFmtMono, DevFmtShort },
  2183. { "AL_FORMAT_STEREO16", DevFmtStereo, DevFmtShort },
  2184. { "AL_FORMAT_QUAD16", DevFmtQuad, DevFmtShort },
  2185. { "AL_FORMAT_51CHN16", DevFmtX51, DevFmtShort },
  2186. { "AL_FORMAT_61CHN16", DevFmtX61, DevFmtShort },
  2187. { "AL_FORMAT_71CHN16", DevFmtX71, DevFmtShort },
  2188. { "AL_FORMAT_MONO8", DevFmtMono, DevFmtByte },
  2189. { "AL_FORMAT_STEREO8", DevFmtStereo, DevFmtByte },
  2190. { "AL_FORMAT_QUAD8", DevFmtQuad, DevFmtByte },
  2191. { "AL_FORMAT_51CHN8", DevFmtX51, DevFmtByte },
  2192. { "AL_FORMAT_61CHN8", DevFmtX61, DevFmtByte },
  2193. { "AL_FORMAT_71CHN8", DevFmtX71, DevFmtByte }
  2194. };
  2195. size_t i;
  2196. ERR("Option 'format' is deprecated, please use 'channels' and 'sample-type'\n");
  2197. for(i = 0;i < COUNTOF(formats);i++)
  2198. {
  2199. if(strcasecmp(fmt, formats[i].name) == 0)
  2200. {
  2201. if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
  2202. device->FmtChans = formats[i].channels;
  2203. if(!(device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
  2204. device->FmtType = formats[i].type;
  2205. device->Flags |= DEVICE_FORMAT_REQUEST;
  2206. break;
  2207. }
  2208. }
  2209. if(i == COUNTOF(formats))
  2210. ERR("Unsupported format: %s\n", fmt);
  2211. }
  2212. #undef DEVICE_FORMAT_REQUEST
  2213. if(ConfigValueUInt(NULL, "frequency", &device->Frequency))
  2214. {
  2215. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  2216. if(device->Frequency < MIN_OUTPUT_RATE)
  2217. ERR("%uhz request clamped to %uhz minimum\n", device->Frequency, MIN_OUTPUT_RATE);
  2218. device->Frequency = maxu(device->Frequency, MIN_OUTPUT_RATE);
  2219. }
  2220. ConfigValueUInt(NULL, "periods", &device->NumUpdates);
  2221. device->NumUpdates = clampu(device->NumUpdates, 2, 16);
  2222. ConfigValueUInt(NULL, "period_size", &device->UpdateSize);
  2223. device->UpdateSize = clampu(device->UpdateSize, 64, 8192);
  2224. ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
  2225. if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
  2226. ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
  2227. if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
  2228. ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
  2229. if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
  2230. ConfigValueInt(NULL, "cf_level", &device->Bs2bLevel);
  2231. device->NumStereoSources = 1;
  2232. device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
  2233. // Find a playback device to open
  2234. LockLists();
  2235. if((err=ALCdevice_OpenPlayback(device, deviceName)) != ALC_NO_ERROR)
  2236. {
  2237. UnlockLists();
  2238. DeleteCriticalSection(&device->Mutex);
  2239. free(device);
  2240. alcSetError(NULL, err);
  2241. return NULL;
  2242. }
  2243. UnlockLists();
  2244. if(DefaultEffect.type != AL_EFFECT_NULL)
  2245. {
  2246. device->DefaultSlot = (ALeffectslot*)(device+1);
  2247. if(InitEffectSlot(device->DefaultSlot) != AL_NO_ERROR)
  2248. {
  2249. device->DefaultSlot = NULL;
  2250. ERR("Failed to initialize the default effect slot\n");
  2251. }
  2252. else if(InitializeEffect(device, device->DefaultSlot, &DefaultEffect) != AL_NO_ERROR)
  2253. {
  2254. ALeffectState_Destroy(device->DefaultSlot->EffectState);
  2255. device->DefaultSlot = NULL;
  2256. ERR("Failed to initialize the default effect\n");
  2257. }
  2258. }
  2259. do {
  2260. device->next = DeviceList;
  2261. } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
  2262. TRACE("Created device %p, \"%s\"\n", device, device->szDeviceName);
  2263. return device;
  2264. }
  2265. /* alcCloseDevice
  2266. *
  2267. * Close the specified Device
  2268. */
  2269. ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *pDevice)
  2270. {
  2271. ALCdevice *volatile*list;
  2272. ALCcontext *ctx;
  2273. LockLists();
  2274. list = &DeviceList;
  2275. while(*list && *list != pDevice)
  2276. list = &(*list)->next;
  2277. if(!*list || (*list)->Type == Capture)
  2278. {
  2279. alcSetError(*list, ALC_INVALID_DEVICE);
  2280. UnlockLists();
  2281. return ALC_FALSE;
  2282. }
  2283. *list = (*list)->next;
  2284. UnlockLists();
  2285. while((ctx=pDevice->ContextList) != NULL)
  2286. {
  2287. WARN("Releasing context %p\n", ctx);
  2288. ReleaseContext(ctx, pDevice);
  2289. }
  2290. if((pDevice->Flags&DEVICE_RUNNING))
  2291. ALCdevice_StopPlayback(pDevice);
  2292. pDevice->Flags &= ~DEVICE_RUNNING;
  2293. ALCdevice_ClosePlayback(pDevice);
  2294. ALCdevice_DecRef(pDevice);
  2295. return ALC_TRUE;
  2296. }
  2297. /* alcLoopbackOpenDeviceSOFT
  2298. *
  2299. * Open a loopback device, for manual rendering.
  2300. */
  2301. ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceName)
  2302. {
  2303. ALCdevice *device;
  2304. DO_INITCONFIG();
  2305. /* Make sure the device name, if specified, is us. */
  2306. if(deviceName && strcmp(deviceName, alcDefaultName) != 0)
  2307. {
  2308. alcSetError(NULL, ALC_INVALID_VALUE);
  2309. return NULL;
  2310. }
  2311. device = calloc(1, sizeof(ALCdevice));
  2312. if(!device)
  2313. {
  2314. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2315. return NULL;
  2316. }
  2317. //Validate device
  2318. device->Funcs = &BackendLoopback.Funcs;
  2319. device->ref = 1;
  2320. device->Connected = ALC_TRUE;
  2321. device->Type = Loopback;
  2322. InitializeCriticalSection(&device->Mutex);
  2323. device->LastError = ALC_NO_ERROR;
  2324. device->Flags = 0;
  2325. device->Bs2b = NULL;
  2326. device->Bs2bLevel = 0;
  2327. device->szDeviceName = NULL;
  2328. device->ContextList = NULL;
  2329. device->MaxNoOfSources = 256;
  2330. device->AuxiliaryEffectSlotMax = 4;
  2331. device->NumAuxSends = MAX_SENDS;
  2332. InitUIntMap(&device->BufferMap, ~0);
  2333. InitUIntMap(&device->EffectMap, ~0);
  2334. InitUIntMap(&device->FilterMap, ~0);
  2335. //Set output format
  2336. device->NumUpdates = 0;
  2337. device->UpdateSize = 0;
  2338. device->Frequency = DEFAULT_OUTPUT_RATE;
  2339. device->FmtChans = DevFmtChannelsDefault;
  2340. device->FmtType = DevFmtTypeDefault;
  2341. ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
  2342. if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
  2343. ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
  2344. if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
  2345. ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
  2346. if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
  2347. device->NumStereoSources = 1;
  2348. device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
  2349. // Open the "backend"
  2350. ALCdevice_OpenPlayback(device, "Loopback");
  2351. do {
  2352. device->next = DeviceList;
  2353. } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
  2354. TRACE("Created device %p\n", device);
  2355. return device;
  2356. }
  2357. /* alcIsRenderFormatSupportedSOFT
  2358. *
  2359. * Determines if the loopback device supports the given format for rendering.
  2360. */
  2361. ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type)
  2362. {
  2363. ALCboolean ret = ALC_FALSE;
  2364. if(!(device=VerifyDevice(device)) || device->Type != Loopback)
  2365. alcSetError(device, ALC_INVALID_DEVICE);
  2366. else if(freq <= 0)
  2367. alcSetError(device, ALC_INVALID_VALUE);
  2368. else
  2369. {
  2370. if(IsValidALCType(type) && BytesFromDevFmt(type) > 0 &&
  2371. IsValidALCChannels(channels) && ChannelsFromDevFmt(channels) > 0 &&
  2372. freq >= MIN_OUTPUT_RATE)
  2373. ret = ALC_TRUE;
  2374. }
  2375. if(device) ALCdevice_DecRef(device);
  2376. return ret;
  2377. }
  2378. /* alcRenderSamplesSOFT
  2379. *
  2380. * Renders some samples into a buffer, using the format last set by the
  2381. * attributes given to alcCreateContext.
  2382. */
  2383. ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
  2384. {
  2385. if(!(device=VerifyDevice(device)) || device->Type != Loopback)
  2386. alcSetError(device, ALC_INVALID_DEVICE);
  2387. else if(samples < 0 || (samples > 0 && buffer == NULL))
  2388. alcSetError(device, ALC_INVALID_VALUE);
  2389. else
  2390. aluMixData(device, buffer, samples);
  2391. if(device) ALCdevice_DecRef(device);
  2392. }
  2393. static void ReleaseALC(void)
  2394. {
  2395. ALCdevice *dev;
  2396. free(alcAllDeviceList); alcAllDeviceList = NULL;
  2397. alcAllDeviceListSize = 0;
  2398. free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
  2399. alcCaptureDeviceListSize = 0;
  2400. free(alcDefaultAllDeviceSpecifier);
  2401. alcDefaultAllDeviceSpecifier = NULL;
  2402. free(alcCaptureDefaultDeviceSpecifier);
  2403. alcCaptureDefaultDeviceSpecifier = NULL;
  2404. if((dev=ExchangePtr((XchgPtr*)&DeviceList, NULL)) != NULL)
  2405. {
  2406. ALCuint num = 0;
  2407. do {
  2408. num++;
  2409. } while((dev=dev->next) != NULL);
  2410. ERR("%u device%s not closed\n", num, (num>1)?"s":"");
  2411. }
  2412. }
  2413. ///////////////////////////////////////////////////////