ALc.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682
  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 }
  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", ALC_MONO },
  272. { "ALC_STEREO", ALC_STEREO },
  273. { "ALC_QUAD", ALC_QUAD },
  274. { "ALC_5POINT1", ALC_5POINT1 },
  275. { "ALC_6POINT1", ALC_6POINT1 },
  276. { "ALC_7POINT1", ALC_7POINT1 },
  277. // Buffer Sample Types
  278. { "ALC_BYTE", ALC_BYTE },
  279. { "ALC_UNSIGNED_BYTE", ALC_UNSIGNED_BYTE },
  280. { "ALC_SHORT", ALC_SHORT },
  281. { "ALC_UNSIGNED_SHORT", ALC_UNSIGNED_SHORT },
  282. { "ALC_INT", ALC_INT },
  283. { "ALC_UNSIGNED_INT", ALC_UNSIGNED_INT },
  284. { "ALC_FLOAT", ALC_FLOAT },
  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 ALCchar *alcDeviceList;
  304. static size_t alcDeviceListSize;
  305. static ALCchar *alcAllDeviceList;
  306. static size_t alcAllDeviceListSize;
  307. static ALCchar *alcCaptureDeviceList;
  308. static size_t alcCaptureDeviceListSize;
  309. /* Default is always the first in the list */
  310. static ALCchar *alcDefaultDeviceSpecifier;
  311. static ALCchar *alcDefaultAllDeviceSpecifier;
  312. static ALCchar *alcCaptureDefaultDeviceSpecifier;
  313. static const ALCchar alcNoDeviceExtList[] =
  314. "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
  315. "ALC_EXT_thread_local_context ALC_SOFTX_loopback_device";
  316. static const ALCchar alcExtensionList[] =
  317. "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
  318. "ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX "
  319. "ALC_EXT_thread_local_context ALC_SOFTX_loopback_device";
  320. static const ALCint alcMajorVersion = 1;
  321. static const ALCint alcMinorVersion = 1;
  322. static const ALCint alcEFXMajorVersion = 1;
  323. static const ALCint alcEFXMinorVersion = 0;
  324. ///////////////////////////////////////////////////////
  325. ///////////////////////////////////////////////////////
  326. // Global Variables
  327. static CRITICAL_SECTION ListLock;
  328. /* Device List */
  329. static ALCdevice *volatile DeviceList = NULL;
  330. // Thread-local current context
  331. static pthread_key_t LocalContext;
  332. // Process-wide current context
  333. static ALCcontext *GlobalContext;
  334. /* Device Error */
  335. static volatile ALCenum g_eLastNullDeviceError = ALC_NO_ERROR;
  336. // Default context extensions
  337. static const ALchar alExtList[] =
  338. "AL_EXT_ALAW AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 "
  339. "AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW "
  340. "AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model "
  341. "AL_LOKI_quadriphonic AL_SOFTX_buffer_samples AL_SOFT_buffer_sub_data "
  342. "AL_SOFTX_deferred_updates AL_SOFT_loop_points "
  343. "AL_SOFTX_non_virtual_channels";
  344. // Mixing Priority Level
  345. ALint RTPrioLevel;
  346. // Output Log File
  347. FILE *LogFile;
  348. // Output Log Level
  349. #ifdef _DEBUG
  350. enum LogLevel LogLevel = LogWarning;
  351. #else
  352. enum LogLevel LogLevel = LogError;
  353. #endif
  354. /* Flag to trap ALC device errors */
  355. static ALCboolean TrapALCError = ALC_FALSE;
  356. /* One-time configuration init control */
  357. static pthread_once_t alc_config_once = PTHREAD_ONCE_INIT;
  358. ///////////////////////////////////////////////////////
  359. ///////////////////////////////////////////////////////
  360. // ALC Related helper functions
  361. static void ReleaseALC(void);
  362. static void ReleaseThreadCtx(void *ptr);
  363. static void alc_initconfig(void);
  364. #define DO_INITCONFIG() pthread_once(&alc_config_once, alc_initconfig)
  365. #if defined(_WIN32)
  366. static void alc_init(void);
  367. static void alc_deinit(void);
  368. static void alc_deinit_safe(void);
  369. #ifndef AL_LIBTYPE_STATIC
  370. UIntMap TlsDestructor;
  371. BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
  372. {
  373. ALsizei i;
  374. (void)hModule;
  375. // Perform actions based on the reason for calling.
  376. switch(ul_reason_for_call)
  377. {
  378. case DLL_PROCESS_ATTACH:
  379. InitUIntMap(&TlsDestructor, ~0);
  380. alc_init();
  381. break;
  382. case DLL_THREAD_DETACH:
  383. LockUIntMapRead(&TlsDestructor);
  384. for(i = 0;i < TlsDestructor.size;i++)
  385. {
  386. void *ptr = pthread_getspecific(TlsDestructor.array[i].key);
  387. void (*callback)(void*) = (void(*)(void*))TlsDestructor.array[i].value;
  388. if(ptr && callback)
  389. callback(ptr);
  390. }
  391. UnlockUIntMapRead(&TlsDestructor);
  392. break;
  393. case DLL_PROCESS_DETACH:
  394. if(!lpReserved)
  395. alc_deinit();
  396. else
  397. alc_deinit_safe();
  398. ResetUIntMap(&TlsDestructor);
  399. break;
  400. }
  401. return TRUE;
  402. }
  403. #elif defined(_MSC_VER)
  404. #pragma section(".CRT$XCU",read)
  405. static void alc_constructor(void);
  406. static void alc_destructor(void);
  407. __declspec(allocate(".CRT$XCU")) void (__cdecl* alc_constructor_)(void) = alc_constructor;
  408. static void alc_constructor(void)
  409. {
  410. atexit(alc_destructor);
  411. alc_init();
  412. }
  413. static void alc_destructor(void)
  414. {
  415. alc_deinit();
  416. }
  417. #elif defined(HAVE_GCC_DESTRUCTOR)
  418. static void alc_init(void) __attribute__((constructor));
  419. static void alc_deinit(void) __attribute__((destructor));
  420. #else
  421. #error "No static initialization available on this platform!"
  422. #endif
  423. #elif defined(HAVE_GCC_DESTRUCTOR)
  424. static void alc_init(void) __attribute__((constructor));
  425. static void alc_deinit(void) __attribute__((destructor));
  426. #else
  427. #error "No global initialization available on this platform!"
  428. #endif
  429. static void alc_init(void)
  430. {
  431. const char *str;
  432. LogFile = stderr;
  433. str = getenv("__ALSOFT_HALF_ANGLE_CONES");
  434. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  435. ConeScale = 1.0f;
  436. str = getenv("__ALSOFT_REVERSE_Z");
  437. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  438. ZScale = -1.0f;
  439. str = getenv("__ALSOFT_TRAP_ERROR");
  440. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  441. {
  442. TrapALError = AL_TRUE;
  443. TrapALCError = AL_TRUE;
  444. }
  445. else
  446. {
  447. str = getenv("__ALSOFT_TRAP_AL_ERROR");
  448. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  449. TrapALError = AL_TRUE;
  450. str = getenv("__ALSOFT_TRAP_ALC_ERROR");
  451. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  452. TrapALCError = ALC_TRUE;
  453. }
  454. pthread_key_create(&LocalContext, ReleaseThreadCtx);
  455. InitializeCriticalSection(&ListLock);
  456. ThunkInit();
  457. }
  458. static void alc_deinit_safe(void)
  459. {
  460. ReleaseALC();
  461. FreeHrtf();
  462. FreeALConfig();
  463. ThunkExit();
  464. DeleteCriticalSection(&ListLock);
  465. pthread_key_delete(LocalContext);
  466. if(LogFile != stderr)
  467. fclose(LogFile);
  468. LogFile = NULL;
  469. }
  470. static void alc_deinit(void)
  471. {
  472. int i;
  473. ReleaseALC();
  474. memset(&PlaybackBackend, 0, sizeof(PlaybackBackend));
  475. memset(&CaptureBackend, 0, sizeof(CaptureBackend));
  476. for(i = 0;BackendList[i].Deinit;i++)
  477. BackendList[i].Deinit();
  478. BackendLoopback.Deinit();
  479. alc_deinit_safe();
  480. }
  481. static void alc_initconfig(void)
  482. {
  483. const char *devs, *str;
  484. float valf;
  485. int i, n;
  486. str = getenv("ALSOFT_LOGLEVEL");
  487. if(str)
  488. {
  489. long lvl = strtol(str, NULL, 0);
  490. if(lvl >= NoLog && lvl <= LogRef)
  491. LogLevel = lvl;
  492. }
  493. str = getenv("ALSOFT_LOGFILE");
  494. if(str && str[0])
  495. {
  496. FILE *logfile = fopen(str, "wat");
  497. if(logfile) LogFile = logfile;
  498. else ERR("Failed to open log file '%s'\n", str);
  499. }
  500. ReadALConfig();
  501. InitHrtf();
  502. #ifdef _WIN32
  503. RTPrioLevel = 1;
  504. #else
  505. RTPrioLevel = 0;
  506. #endif
  507. ConfigValueInt(NULL, "rt-prio", &RTPrioLevel);
  508. if(ConfigValueInt(NULL, "resampler", &n))
  509. {
  510. if(n < RESAMPLER_MAX && n > RESAMPLER_MIN)
  511. DefaultResampler = n;
  512. }
  513. if(!TrapALCError)
  514. TrapALCError = GetConfigValueBool(NULL, "trap-alc-error", ALC_FALSE);
  515. if(!TrapALError)
  516. TrapALError = GetConfigValueBool(NULL, "trap-al-error", AL_FALSE);
  517. if(ConfigValueFloat("reverb", "boost", &valf))
  518. ReverbBoost *= aluPow(10.0f, valf / 20.0f);
  519. EmulateEAXReverb = GetConfigValueBool("reverb", "emulate-eax", AL_FALSE);
  520. if(ConfigValueStr(NULL, "drivers", &devs))
  521. {
  522. int n;
  523. size_t len;
  524. const char *next = devs;
  525. int endlist, delitem;
  526. i = 0;
  527. do {
  528. devs = next;
  529. next = strchr(devs, ',');
  530. delitem = (devs[0] == '-');
  531. if(devs[0] == '-') devs++;
  532. if(!devs[0] || devs[0] == ',')
  533. {
  534. endlist = 0;
  535. continue;
  536. }
  537. endlist = 1;
  538. len = (next ? ((size_t)(next-devs)) : strlen(devs));
  539. for(n = i;BackendList[n].Init;n++)
  540. {
  541. if(len == strlen(BackendList[n].name) &&
  542. strncmp(BackendList[n].name, devs, len) == 0)
  543. {
  544. if(delitem)
  545. {
  546. do {
  547. BackendList[n] = BackendList[n+1];
  548. ++n;
  549. } while(BackendList[n].Init);
  550. }
  551. else
  552. {
  553. struct BackendInfo Bkp = BackendList[n];
  554. while(n > i)
  555. {
  556. BackendList[n] = BackendList[n-1];
  557. --n;
  558. }
  559. BackendList[n] = Bkp;
  560. i++;
  561. }
  562. break;
  563. }
  564. }
  565. } while(next++);
  566. if(endlist)
  567. {
  568. BackendList[i].name = NULL;
  569. BackendList[i].Init = NULL;
  570. BackendList[i].Deinit = NULL;
  571. BackendList[i].Probe = NULL;
  572. }
  573. }
  574. for(i = 0;BackendList[i].Init && (!PlaybackBackend.name || !CaptureBackend.name);i++)
  575. {
  576. if(!BackendList[i].Init(&BackendList[i].Funcs))
  577. {
  578. WARN("Failed to initialize backend \"%s\"\n", BackendList[i].name);
  579. continue;
  580. }
  581. TRACE("Initialized backend \"%s\"\n", BackendList[i].name);
  582. if(BackendList[i].Funcs.OpenPlayback && !PlaybackBackend.name)
  583. {
  584. PlaybackBackend = BackendList[i];
  585. TRACE("Added \"%s\" for playback\n", PlaybackBackend.name);
  586. }
  587. if(BackendList[i].Funcs.OpenCapture && !CaptureBackend.name)
  588. {
  589. CaptureBackend = BackendList[i];
  590. TRACE("Added \"%s\" for capture\n", CaptureBackend.name);
  591. }
  592. }
  593. BackendLoopback.Init(&BackendLoopback.Funcs);
  594. if(ConfigValueStr(NULL, "excludefx", &str))
  595. {
  596. size_t len;
  597. const char *next = str;
  598. do {
  599. str = next;
  600. next = strchr(str, ',');
  601. if(!str[0] || next == str)
  602. continue;
  603. len = (next ? ((size_t)(next-str)) : strlen(str));
  604. for(n = 0;EffectList[n].name;n++)
  605. {
  606. if(len == strlen(EffectList[n].name) &&
  607. strncmp(EffectList[n].name, str, len) == 0)
  608. DisabledEffects[EffectList[n].type] = AL_TRUE;
  609. }
  610. } while(next++);
  611. }
  612. }
  613. static void LockLists(void)
  614. {
  615. EnterCriticalSection(&ListLock);
  616. }
  617. static void UnlockLists(void)
  618. {
  619. LeaveCriticalSection(&ListLock);
  620. }
  621. static void ProbeList(ALCchar **list, size_t *listsize, enum DevProbe type)
  622. {
  623. DO_INITCONFIG();
  624. LockLists();
  625. free(*list);
  626. *list = NULL;
  627. *listsize = 0;
  628. if(type == CAPTURE_DEVICE_PROBE)
  629. CaptureBackend.Probe(type);
  630. else
  631. PlaybackBackend.Probe(type);
  632. UnlockLists();
  633. }
  634. static void ProbeDeviceList(void)
  635. { ProbeList(&alcDeviceList, &alcDeviceListSize, DEVICE_PROBE); }
  636. static void ProbeAllDeviceList(void)
  637. { ProbeList(&alcAllDeviceList, &alcAllDeviceListSize, ALL_DEVICE_PROBE); }
  638. static void ProbeCaptureDeviceList(void)
  639. { ProbeList(&alcCaptureDeviceList, &alcCaptureDeviceListSize, CAPTURE_DEVICE_PROBE); }
  640. static void AppendList(const ALCchar *name, ALCchar **List, size_t *ListSize)
  641. {
  642. size_t len = strlen(name);
  643. void *temp;
  644. if(len == 0)
  645. return;
  646. temp = realloc(*List, (*ListSize) + len + 2);
  647. if(!temp)
  648. {
  649. ERR("Realloc failed to add %s!\n", name);
  650. return;
  651. }
  652. *List = temp;
  653. memcpy((*List)+(*ListSize), name, len+1);
  654. *ListSize += len+1;
  655. (*List)[*ListSize] = 0;
  656. }
  657. #define DECL_APPEND_LIST_FUNC(type) \
  658. void Append##type##List(const ALCchar *name) \
  659. { AppendList(name, &alc##type##List, &alc##type##ListSize); }
  660. DECL_APPEND_LIST_FUNC(Device)
  661. DECL_APPEND_LIST_FUNC(AllDevice)
  662. DECL_APPEND_LIST_FUNC(CaptureDevice)
  663. #undef DECL_APPEND_LIST_FUNC
  664. /* Sets the default channel order used by most non-WaveFormatEx-based APIs */
  665. void SetDefaultChannelOrder(ALCdevice *device)
  666. {
  667. switch(device->FmtChans)
  668. {
  669. case DevFmtX51: device->DevChannels[0] = FRONT_LEFT;
  670. device->DevChannels[1] = FRONT_RIGHT;
  671. device->DevChannels[2] = BACK_LEFT;
  672. device->DevChannels[3] = BACK_RIGHT;
  673. device->DevChannels[4] = FRONT_CENTER;
  674. device->DevChannels[5] = LFE;
  675. return;
  676. case DevFmtX71: device->DevChannels[0] = FRONT_LEFT;
  677. device->DevChannels[1] = FRONT_RIGHT;
  678. device->DevChannels[2] = BACK_LEFT;
  679. device->DevChannels[3] = BACK_RIGHT;
  680. device->DevChannels[4] = FRONT_CENTER;
  681. device->DevChannels[5] = LFE;
  682. device->DevChannels[6] = SIDE_LEFT;
  683. device->DevChannels[7] = SIDE_RIGHT;
  684. return;
  685. /* Same as WFX order */
  686. case DevFmtMono:
  687. case DevFmtStereo:
  688. case DevFmtQuad:
  689. case DevFmtX51Side:
  690. case DevFmtX61:
  691. break;
  692. }
  693. SetDefaultWFXChannelOrder(device);
  694. }
  695. /* Sets the default order used by WaveFormatEx */
  696. void SetDefaultWFXChannelOrder(ALCdevice *device)
  697. {
  698. switch(device->FmtChans)
  699. {
  700. case DevFmtMono: device->DevChannels[0] = FRONT_CENTER; break;
  701. case DevFmtStereo: device->DevChannels[0] = FRONT_LEFT;
  702. device->DevChannels[1] = FRONT_RIGHT; break;
  703. case DevFmtQuad: device->DevChannels[0] = FRONT_LEFT;
  704. device->DevChannels[1] = FRONT_RIGHT;
  705. device->DevChannels[2] = BACK_LEFT;
  706. device->DevChannels[3] = BACK_RIGHT; break;
  707. case DevFmtX51: device->DevChannels[0] = FRONT_LEFT;
  708. device->DevChannels[1] = FRONT_RIGHT;
  709. device->DevChannels[2] = FRONT_CENTER;
  710. device->DevChannels[3] = LFE;
  711. device->DevChannels[4] = BACK_LEFT;
  712. device->DevChannels[5] = BACK_RIGHT; break;
  713. case DevFmtX51Side: device->DevChannels[0] = FRONT_LEFT;
  714. device->DevChannels[1] = FRONT_RIGHT;
  715. device->DevChannels[2] = FRONT_CENTER;
  716. device->DevChannels[3] = LFE;
  717. device->DevChannels[4] = SIDE_LEFT;
  718. device->DevChannels[5] = SIDE_RIGHT; break;
  719. case DevFmtX61: device->DevChannels[0] = FRONT_LEFT;
  720. device->DevChannels[1] = FRONT_RIGHT;
  721. device->DevChannels[2] = FRONT_CENTER;
  722. device->DevChannels[3] = LFE;
  723. device->DevChannels[4] = BACK_CENTER;
  724. device->DevChannels[5] = SIDE_LEFT;
  725. device->DevChannels[6] = SIDE_RIGHT; break;
  726. case DevFmtX71: device->DevChannels[0] = FRONT_LEFT;
  727. device->DevChannels[1] = FRONT_RIGHT;
  728. device->DevChannels[2] = FRONT_CENTER;
  729. device->DevChannels[3] = LFE;
  730. device->DevChannels[4] = BACK_LEFT;
  731. device->DevChannels[5] = BACK_RIGHT;
  732. device->DevChannels[6] = SIDE_LEFT;
  733. device->DevChannels[7] = SIDE_RIGHT; break;
  734. }
  735. }
  736. const ALCchar *DevFmtTypeString(enum DevFmtType type)
  737. {
  738. switch(type)
  739. {
  740. case DevFmtByte: return "Signed Byte";
  741. case DevFmtUByte: return "Unsigned Byte";
  742. case DevFmtShort: return "Signed Short";
  743. case DevFmtUShort: return "Unsigned Short";
  744. case DevFmtFloat: return "Float";
  745. }
  746. return "(unknown type)";
  747. }
  748. const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans)
  749. {
  750. switch(chans)
  751. {
  752. case DevFmtMono: return "Mono";
  753. case DevFmtStereo: return "Stereo";
  754. case DevFmtQuad: return "Quadraphonic";
  755. case DevFmtX51: return "5.1 Surround";
  756. case DevFmtX51Side: return "5.1 Side";
  757. case DevFmtX61: return "6.1 Surround";
  758. case DevFmtX71: return "7.1 Surround";
  759. }
  760. return "(unknown channels)";
  761. }
  762. ALuint BytesFromDevFmt(enum DevFmtType type)
  763. {
  764. switch(type)
  765. {
  766. case DevFmtByte: return sizeof(ALbyte);
  767. case DevFmtUByte: return sizeof(ALubyte);
  768. case DevFmtShort: return sizeof(ALshort);
  769. case DevFmtUShort: return sizeof(ALushort);
  770. case DevFmtFloat: return sizeof(ALfloat);
  771. }
  772. return 0;
  773. }
  774. ALuint ChannelsFromDevFmt(enum DevFmtChannels chans)
  775. {
  776. switch(chans)
  777. {
  778. case DevFmtMono: return 1;
  779. case DevFmtStereo: return 2;
  780. case DevFmtQuad: return 4;
  781. case DevFmtX51: return 6;
  782. case DevFmtX51Side: return 6;
  783. case DevFmtX61: return 7;
  784. case DevFmtX71: return 8;
  785. }
  786. return 0;
  787. }
  788. static ALboolean DecomposeDevFormat(ALenum format, enum DevFmtChannels *chans,
  789. enum DevFmtType *type)
  790. {
  791. static const struct {
  792. ALenum format;
  793. enum DevFmtChannels channels;
  794. enum DevFmtType type;
  795. } list[] = {
  796. { AL_FORMAT_MONO8, DevFmtMono, DevFmtUByte },
  797. { AL_FORMAT_MONO16, DevFmtMono, DevFmtShort },
  798. { AL_FORMAT_MONO_FLOAT32, DevFmtMono, DevFmtFloat },
  799. { AL_FORMAT_STEREO8, DevFmtStereo, DevFmtUByte },
  800. { AL_FORMAT_STEREO16, DevFmtStereo, DevFmtShort },
  801. { AL_FORMAT_STEREO_FLOAT32, DevFmtStereo, DevFmtFloat },
  802. { AL_FORMAT_QUAD8, DevFmtQuad, DevFmtUByte },
  803. { AL_FORMAT_QUAD16, DevFmtQuad, DevFmtShort },
  804. { AL_FORMAT_QUAD32, DevFmtQuad, DevFmtFloat },
  805. { AL_FORMAT_51CHN8, DevFmtX51, DevFmtUByte },
  806. { AL_FORMAT_51CHN16, DevFmtX51, DevFmtShort },
  807. { AL_FORMAT_51CHN32, DevFmtX51, DevFmtFloat },
  808. { AL_FORMAT_61CHN8, DevFmtX61, DevFmtUByte },
  809. { AL_FORMAT_61CHN16, DevFmtX61, DevFmtShort },
  810. { AL_FORMAT_61CHN32, DevFmtX61, DevFmtFloat },
  811. { AL_FORMAT_71CHN8, DevFmtX71, DevFmtUByte },
  812. { AL_FORMAT_71CHN16, DevFmtX71, DevFmtShort },
  813. { AL_FORMAT_71CHN32, DevFmtX71, DevFmtFloat },
  814. };
  815. ALuint i;
  816. for(i = 0;i < sizeof(list)/sizeof(list[0]);i++)
  817. {
  818. if(list[i].format == format)
  819. {
  820. *chans = list[i].channels;
  821. *type = list[i].type;
  822. return AL_TRUE;
  823. }
  824. }
  825. return AL_FALSE;
  826. }
  827. static ALCboolean IsValidALCType(ALCenum type)
  828. {
  829. switch(type)
  830. {
  831. case ALC_BYTE:
  832. case ALC_UNSIGNED_BYTE:
  833. case ALC_SHORT:
  834. case ALC_UNSIGNED_SHORT:
  835. case ALC_INT:
  836. case ALC_UNSIGNED_INT:
  837. case ALC_FLOAT:
  838. return ALC_TRUE;
  839. }
  840. return ALC_FALSE;
  841. }
  842. static ALCboolean IsValidALCChannels(ALCenum channels)
  843. {
  844. switch(channels)
  845. {
  846. case ALC_MONO:
  847. case ALC_STEREO:
  848. case ALC_QUAD:
  849. case ALC_5POINT1:
  850. case ALC_6POINT1:
  851. case ALC_7POINT1:
  852. return ALC_TRUE;
  853. }
  854. return ALC_FALSE;
  855. }
  856. /* alcSetError
  857. *
  858. * Stores the latest ALC Error
  859. */
  860. static void alcSetError(ALCdevice *device, ALCenum errorCode)
  861. {
  862. if(TrapALCError)
  863. {
  864. #ifdef _WIN32
  865. /* DebugBreak() will cause an exception if there is no debugger */
  866. if(IsDebuggerPresent())
  867. DebugBreak();
  868. #elif defined(SIGTRAP)
  869. raise(SIGTRAP);
  870. #endif
  871. }
  872. if(device)
  873. device->LastError = errorCode;
  874. else
  875. g_eLastNullDeviceError = errorCode;
  876. }
  877. /* UpdateDeviceParams
  878. *
  879. * Updates device parameters according to the attribute list (caller is
  880. * responsible for holding the list lock).
  881. */
  882. static ALCboolean UpdateDeviceParams(ALCdevice *device, const ALCint *attrList)
  883. {
  884. ALCcontext *context;
  885. int oldMode;
  886. ALuint i;
  887. // Check for attributes
  888. if(attrList && attrList[0])
  889. {
  890. ALCuint freq, numMono, numStereo, numSends;
  891. enum DevFmtChannels schans;
  892. enum DevFmtType stype;
  893. ALuint attrIdx;
  894. // If a context is already running on the device, stop playback so the
  895. // device attributes can be updated
  896. if((device->Flags&DEVICE_RUNNING))
  897. ALCdevice_StopPlayback(device);
  898. device->Flags &= ~DEVICE_RUNNING;
  899. freq = device->Frequency;
  900. schans = device->FmtChans;
  901. stype = device->FmtType;
  902. numMono = device->NumMonoSources;
  903. numStereo = device->NumStereoSources;
  904. numSends = device->NumAuxSends;
  905. attrIdx = 0;
  906. while(attrList[attrIdx])
  907. {
  908. if(attrList[attrIdx] == ALC_FORMAT_CHANNELS_SOFT &&
  909. device->IsLoopbackDevice)
  910. {
  911. ALCint val = attrList[attrIdx + 1];
  912. if(!IsValidALCChannels(val) || !ChannelsFromDevFmt(val))
  913. {
  914. alcSetError(device, ALC_INVALID_VALUE);
  915. return ALC_FALSE;
  916. }
  917. schans = val;
  918. }
  919. if(attrList[attrIdx] == ALC_FORMAT_TYPE_SOFT &&
  920. device->IsLoopbackDevice)
  921. {
  922. ALCint val = attrList[attrIdx + 1];
  923. if(!IsValidALCType(val) || !BytesFromDevFmt(val))
  924. {
  925. alcSetError(device, ALC_INVALID_VALUE);
  926. return ALC_FALSE;
  927. }
  928. stype = val;
  929. }
  930. if(attrList[attrIdx] == ALC_FREQUENCY)
  931. {
  932. if(device->IsLoopbackDevice)
  933. {
  934. freq = attrList[attrIdx + 1];
  935. if(freq < 8000)
  936. {
  937. alcSetError(device, ALC_INVALID_VALUE);
  938. return ALC_FALSE;
  939. }
  940. }
  941. else
  942. {
  943. freq = attrList[attrIdx + 1];
  944. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  945. }
  946. }
  947. if(attrList[attrIdx] == ALC_STEREO_SOURCES)
  948. {
  949. numStereo = attrList[attrIdx + 1];
  950. if(numStereo > device->MaxNoOfSources)
  951. numStereo = device->MaxNoOfSources;
  952. numMono = device->MaxNoOfSources - numStereo;
  953. }
  954. if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
  955. numSends = attrList[attrIdx + 1];
  956. attrIdx += 2;
  957. }
  958. if(!device->IsLoopbackDevice)
  959. {
  960. ConfigValueUInt(NULL, "frequency", &freq);
  961. freq = maxu(freq, 8000);
  962. }
  963. ConfigValueUInt(NULL, "sends", &numSends);
  964. numSends = minu(MAX_SENDS, numSends);
  965. device->UpdateSize = (ALuint64)device->UpdateSize * freq /
  966. device->Frequency;
  967. device->Frequency = freq;
  968. device->FmtChans = schans;
  969. device->FmtType = stype;
  970. device->NumMonoSources = numMono;
  971. device->NumStereoSources = numStereo;
  972. device->NumAuxSends = numSends;
  973. }
  974. if((device->Flags&DEVICE_RUNNING))
  975. return ALC_TRUE;
  976. LockDevice(device);
  977. TRACE("Format pre-setup: %s%s, %s, %uhz%s, %u update size x%d\n",
  978. DevFmtChannelsString(device->FmtChans),
  979. (device->Flags&DEVICE_CHANNELS_REQUEST)?" (requested)":"",
  980. DevFmtTypeString(device->FmtType), device->Frequency,
  981. (device->Flags&DEVICE_FREQUENCY_REQUEST)?" (requested)":"",
  982. device->UpdateSize, device->NumUpdates);
  983. if(ALCdevice_ResetPlayback(device) == ALC_FALSE)
  984. {
  985. UnlockDevice(device);
  986. return ALC_FALSE;
  987. }
  988. device->Flags |= DEVICE_RUNNING;
  989. TRACE("Format post-setup: %s%s, %s, %uhz%s, %u update size x%d\n",
  990. DevFmtChannelsString(device->FmtChans),
  991. (device->Flags&DEVICE_CHANNELS_REQUEST)?" (requested)":"",
  992. DevFmtTypeString(device->FmtType), device->Frequency,
  993. (device->Flags&DEVICE_FREQUENCY_REQUEST)?" (requested)":"",
  994. device->UpdateSize, device->NumUpdates);
  995. aluInitPanning(device);
  996. for(i = 0;i < MAXCHANNELS;i++)
  997. {
  998. device->ClickRemoval[i] = 0.0f;
  999. device->PendingClicks[i] = 0.0f;
  1000. }
  1001. device->Hrtf = NULL;
  1002. if(!device->IsLoopbackDevice && GetConfigValueBool(NULL, "hrtf", AL_FALSE))
  1003. device->Hrtf = GetHrtf(device);
  1004. TRACE("HRTF %s\n", device->Hrtf?"enabled":"disabled");
  1005. if(!device->Hrtf && device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
  1006. {
  1007. if(!device->Bs2b)
  1008. {
  1009. device->Bs2b = calloc(1, sizeof(*device->Bs2b));
  1010. bs2b_clear(device->Bs2b);
  1011. }
  1012. bs2b_set_srate(device->Bs2b, device->Frequency);
  1013. bs2b_set_level(device->Bs2b, device->Bs2bLevel);
  1014. TRACE("BS2B level %d\n", device->Bs2bLevel);
  1015. }
  1016. else
  1017. {
  1018. free(device->Bs2b);
  1019. device->Bs2b = NULL;
  1020. TRACE("BS2B disabled\n");
  1021. }
  1022. device->Flags &= ~DEVICE_DUPLICATE_STEREO;
  1023. switch(device->FmtChans)
  1024. {
  1025. case DevFmtMono:
  1026. case DevFmtStereo:
  1027. break;
  1028. case DevFmtQuad:
  1029. case DevFmtX51:
  1030. case DevFmtX51Side:
  1031. case DevFmtX61:
  1032. case DevFmtX71:
  1033. if(GetConfigValueBool(NULL, "stereodup", AL_TRUE))
  1034. device->Flags |= DEVICE_DUPLICATE_STEREO;
  1035. break;
  1036. }
  1037. TRACE("Stereo duplication %s\n", (device->Flags&DEVICE_DUPLICATE_STEREO)?"enabled":"disabled");
  1038. oldMode = SetMixerFPUMode();
  1039. context = device->ContextList;
  1040. while(context)
  1041. {
  1042. ALsizei pos;
  1043. context->UpdateSources = AL_FALSE;
  1044. LockUIntMapRead(&context->EffectSlotMap);
  1045. for(pos = 0;pos < context->EffectSlotMap.size;pos++)
  1046. {
  1047. ALeffectslot *slot = context->EffectSlotMap.array[pos].value;
  1048. if(ALeffectState_DeviceUpdate(slot->EffectState, device) == AL_FALSE)
  1049. {
  1050. UnlockUIntMapRead(&context->EffectSlotMap);
  1051. UnlockDevice(device);
  1052. ALCdevice_StopPlayback(device);
  1053. device->Flags &= ~DEVICE_RUNNING;
  1054. return ALC_FALSE;
  1055. }
  1056. slot->NeedsUpdate = AL_FALSE;
  1057. ALeffectState_Update(slot->EffectState, context, slot);
  1058. }
  1059. UnlockUIntMapRead(&context->EffectSlotMap);
  1060. LockUIntMapRead(&context->SourceMap);
  1061. for(pos = 0;pos < context->SourceMap.size;pos++)
  1062. {
  1063. ALsource *source = context->SourceMap.array[pos].value;
  1064. ALuint s = device->NumAuxSends;
  1065. while(s < MAX_SENDS)
  1066. {
  1067. if(source->Send[s].Slot)
  1068. DecrementRef(&source->Send[s].Slot->ref);
  1069. source->Send[s].Slot = NULL;
  1070. source->Send[s].WetGain = 1.0f;
  1071. source->Send[s].WetGainHF = 1.0f;
  1072. s++;
  1073. }
  1074. source->NeedsUpdate = AL_FALSE;
  1075. ALsource_Update(source, context);
  1076. }
  1077. UnlockUIntMapRead(&context->SourceMap);
  1078. context = context->next;
  1079. }
  1080. RestoreFPUMode(oldMode);
  1081. UnlockDevice(device);
  1082. return ALC_TRUE;
  1083. }
  1084. /* FreeDevice
  1085. *
  1086. * Frees the device structure, and destroys any objects the app failed to
  1087. * delete. Called once there's no more references on the device.
  1088. */
  1089. static ALCvoid FreeDevice(ALCdevice *device)
  1090. {
  1091. TRACE("%p\n", device);
  1092. if(device->BufferMap.size > 0)
  1093. {
  1094. WARN("(%p) Deleting %d Buffer(s)\n", device, device->BufferMap.size);
  1095. ReleaseALBuffers(device);
  1096. }
  1097. ResetUIntMap(&device->BufferMap);
  1098. if(device->EffectMap.size > 0)
  1099. {
  1100. WARN("(%p) Deleting %d Effect(s)\n", device, device->EffectMap.size);
  1101. ReleaseALEffects(device);
  1102. }
  1103. ResetUIntMap(&device->EffectMap);
  1104. if(device->FilterMap.size > 0)
  1105. {
  1106. WARN("(%p) Deleting %d Filter(s)\n", device, device->FilterMap.size);
  1107. ReleaseALFilters(device);
  1108. }
  1109. ResetUIntMap(&device->FilterMap);
  1110. free(device->Bs2b);
  1111. device->Bs2b = NULL;
  1112. free(device->szDeviceName);
  1113. device->szDeviceName = NULL;
  1114. DeleteCriticalSection(&device->Mutex);
  1115. free(device);
  1116. }
  1117. void ALCdevice_IncRef(ALCdevice *device)
  1118. {
  1119. RefCount ref;
  1120. ref = IncrementRef(&device->ref);
  1121. TRACEREF("%p increasing refcount to %u\n", device, ref);
  1122. }
  1123. void ALCdevice_DecRef(ALCdevice *device)
  1124. {
  1125. RefCount ref;
  1126. ref = DecrementRef(&device->ref);
  1127. TRACEREF("%p decreasing refcount to %u\n", device, ref);
  1128. if(ref == 0) FreeDevice(device);
  1129. }
  1130. /* VerifyDevice
  1131. *
  1132. * Checks if the device handle is valid, and increments its ref count if so.
  1133. */
  1134. static ALCdevice *VerifyDevice(ALCdevice *device)
  1135. {
  1136. ALCdevice *tmpDevice;
  1137. if(!device)
  1138. return NULL;
  1139. LockLists();
  1140. tmpDevice = DeviceList;
  1141. while(tmpDevice && tmpDevice != device)
  1142. tmpDevice = tmpDevice->next;
  1143. if(tmpDevice)
  1144. ALCdevice_IncRef(tmpDevice);
  1145. UnlockLists();
  1146. return tmpDevice;
  1147. }
  1148. ALCvoid LockDevice(ALCdevice *device)
  1149. {
  1150. EnterCriticalSection(&device->Mutex);
  1151. }
  1152. ALCvoid UnlockDevice(ALCdevice *device)
  1153. {
  1154. LeaveCriticalSection(&device->Mutex);
  1155. }
  1156. /* InitContext
  1157. *
  1158. * Initializes context variables
  1159. */
  1160. static ALvoid InitContext(ALCcontext *pContext)
  1161. {
  1162. //Initialise listener
  1163. pContext->Listener.Gain = 1.0f;
  1164. pContext->Listener.MetersPerUnit = 1.0f;
  1165. pContext->Listener.Position[0] = 0.0f;
  1166. pContext->Listener.Position[1] = 0.0f;
  1167. pContext->Listener.Position[2] = 0.0f;
  1168. pContext->Listener.Velocity[0] = 0.0f;
  1169. pContext->Listener.Velocity[1] = 0.0f;
  1170. pContext->Listener.Velocity[2] = 0.0f;
  1171. pContext->Listener.Forward[0] = 0.0f;
  1172. pContext->Listener.Forward[1] = 0.0f;
  1173. pContext->Listener.Forward[2] = -1.0f;
  1174. pContext->Listener.Up[0] = 0.0f;
  1175. pContext->Listener.Up[1] = 1.0f;
  1176. pContext->Listener.Up[2] = 0.0f;
  1177. //Validate pContext
  1178. pContext->LastError = AL_NO_ERROR;
  1179. pContext->UpdateSources = AL_FALSE;
  1180. pContext->ActiveSourceCount = 0;
  1181. InitUIntMap(&pContext->SourceMap, pContext->Device->MaxNoOfSources);
  1182. InitUIntMap(&pContext->EffectSlotMap, pContext->Device->AuxiliaryEffectSlotMax);
  1183. //Set globals
  1184. pContext->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
  1185. pContext->SourceDistanceModel = AL_FALSE;
  1186. pContext->DopplerFactor = 1.0f;
  1187. pContext->DopplerVelocity = 1.0f;
  1188. pContext->flSpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
  1189. pContext->DeferUpdates = AL_FALSE;
  1190. pContext->ExtensionList = alExtList;
  1191. }
  1192. /* FreeContext
  1193. *
  1194. * Cleans up the context, and destroys any remaining objects the app failed to
  1195. * delete. Called once there's no more references on the context.
  1196. */
  1197. static ALCvoid FreeContext(ALCcontext *context)
  1198. {
  1199. TRACE("%p\n", context);
  1200. if(context->SourceMap.size > 0)
  1201. {
  1202. ERR("(%p) Deleting %d Source(s)\n", context, context->SourceMap.size);
  1203. ReleaseALSources(context);
  1204. }
  1205. ResetUIntMap(&context->SourceMap);
  1206. if(context->EffectSlotMap.size > 0)
  1207. {
  1208. ERR("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context, context->EffectSlotMap.size);
  1209. ReleaseALAuxiliaryEffectSlots(context);
  1210. }
  1211. ResetUIntMap(&context->EffectSlotMap);
  1212. context->ActiveSourceCount = 0;
  1213. free(context->ActiveSources);
  1214. context->ActiveSources = NULL;
  1215. context->MaxActiveSources = 0;
  1216. context->ActiveEffectSlotCount = 0;
  1217. free(context->ActiveEffectSlots);
  1218. context->ActiveEffectSlots = NULL;
  1219. context->MaxActiveEffectSlots = 0;
  1220. ALCdevice_DecRef(context->Device);
  1221. context->Device = NULL;
  1222. //Invalidate context
  1223. memset(context, 0, sizeof(ALCcontext));
  1224. free(context);
  1225. }
  1226. /* ReleaseContext
  1227. *
  1228. * Removes the context reference from the given device and removes it from
  1229. * being current on the running thread or globally.
  1230. */
  1231. static void ReleaseContext(ALCcontext *context, ALCdevice *device)
  1232. {
  1233. ALCcontext *volatile*tmp_ctx;
  1234. if(pthread_getspecific(LocalContext) == context)
  1235. {
  1236. WARN("%p released while current on thread\n", context);
  1237. pthread_setspecific(LocalContext, NULL);
  1238. ALCcontext_DecRef(context);
  1239. }
  1240. if(CompExchangePtr((void**)&GlobalContext, context, NULL))
  1241. {
  1242. WARN("%p released while current\n", context);
  1243. ALCcontext_DecRef(context);
  1244. }
  1245. tmp_ctx = &device->ContextList;
  1246. while(*tmp_ctx)
  1247. {
  1248. if(*tmp_ctx == context)
  1249. {
  1250. *tmp_ctx = context->next;
  1251. break;
  1252. }
  1253. tmp_ctx = &(*tmp_ctx)->next;
  1254. }
  1255. LockDevice(device);
  1256. /* Lock the device to make sure the mixer is not using the contexts in the
  1257. * list before we go about deleting this one. There should be a more
  1258. * efficient way of doing this. */
  1259. UnlockDevice(device);
  1260. ALCcontext_DecRef(context);
  1261. }
  1262. void ALCcontext_IncRef(ALCcontext *context)
  1263. {
  1264. RefCount ref;
  1265. ref = IncrementRef(&context->ref);
  1266. TRACEREF("%p increasing refcount to %u\n", context, ref);
  1267. }
  1268. void ALCcontext_DecRef(ALCcontext *context)
  1269. {
  1270. RefCount ref;
  1271. ref = DecrementRef(&context->ref);
  1272. TRACEREF("%p decreasing refcount to %u\n", context, ref);
  1273. if(ref == 0) FreeContext(context);
  1274. }
  1275. static void ReleaseThreadCtx(void *ptr)
  1276. {
  1277. WARN("%p current for thread being destroyed\n", ptr);
  1278. ALCcontext_DecRef(ptr);
  1279. }
  1280. /* VerifyContext
  1281. *
  1282. * Checks that the given context is valid, and increments its reference count.
  1283. */
  1284. static ALCcontext *VerifyContext(ALCcontext *context)
  1285. {
  1286. ALCdevice *dev;
  1287. LockLists();
  1288. dev = DeviceList;
  1289. while(dev)
  1290. {
  1291. ALCcontext *tmp_ctx = dev->ContextList;
  1292. while(tmp_ctx)
  1293. {
  1294. if(tmp_ctx == context)
  1295. {
  1296. ALCcontext_IncRef(tmp_ctx);
  1297. UnlockLists();
  1298. return tmp_ctx;
  1299. }
  1300. tmp_ctx = tmp_ctx->next;
  1301. }
  1302. dev = dev->next;
  1303. }
  1304. UnlockLists();
  1305. return NULL;
  1306. }
  1307. ALCvoid LockContext(ALCcontext *context)
  1308. {
  1309. EnterCriticalSection(&context->Device->Mutex);
  1310. }
  1311. ALCvoid UnlockContext(ALCcontext *context)
  1312. {
  1313. LeaveCriticalSection(&context->Device->Mutex);
  1314. }
  1315. /* GetContextRef
  1316. *
  1317. * Returns the currently active context, and adds a reference without locking
  1318. * it.
  1319. */
  1320. ALCcontext *GetContextRef(void)
  1321. {
  1322. ALCcontext *context;
  1323. context = pthread_getspecific(LocalContext);
  1324. if(context)
  1325. ALCcontext_IncRef(context);
  1326. else
  1327. {
  1328. LockLists();
  1329. context = GlobalContext;
  1330. if(context)
  1331. ALCcontext_IncRef(context);
  1332. UnlockLists();
  1333. }
  1334. return context;
  1335. }
  1336. ///////////////////////////////////////////////////////
  1337. ///////////////////////////////////////////////////////
  1338. // ALC Functions calls
  1339. // This should probably move to another c file but for now ...
  1340. ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
  1341. {
  1342. ALCdevice *device = NULL;
  1343. ALCenum err;
  1344. DO_INITCONFIG();
  1345. if(!CaptureBackend.name)
  1346. {
  1347. alcSetError(NULL, ALC_INVALID_VALUE);
  1348. return NULL;
  1349. }
  1350. if(SampleSize <= 0)
  1351. {
  1352. alcSetError(NULL, ALC_INVALID_VALUE);
  1353. return NULL;
  1354. }
  1355. if(deviceName && (!deviceName[0] || strcasecmp(deviceName, "openal soft") == 0 || strcasecmp(deviceName, "openal-soft") == 0))
  1356. deviceName = NULL;
  1357. device = calloc(1, sizeof(ALCdevice));
  1358. if(!device)
  1359. {
  1360. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  1361. return NULL;
  1362. }
  1363. //Validate device
  1364. device->Funcs = &CaptureBackend.Funcs;
  1365. device->ref = 1;
  1366. device->Connected = ALC_TRUE;
  1367. device->IsCaptureDevice = AL_TRUE;
  1368. device->IsLoopbackDevice = AL_FALSE;
  1369. InitializeCriticalSection(&device->Mutex);
  1370. InitUIntMap(&device->BufferMap, ~0);
  1371. InitUIntMap(&device->EffectMap, ~0);
  1372. InitUIntMap(&device->FilterMap, ~0);
  1373. device->szDeviceName = NULL;
  1374. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  1375. device->Frequency = frequency;
  1376. device->Flags |= DEVICE_CHANNELS_REQUEST;
  1377. if(DecomposeDevFormat(format, &device->FmtChans, &device->FmtType) == AL_FALSE)
  1378. {
  1379. DeleteCriticalSection(&device->Mutex);
  1380. free(device);
  1381. alcSetError(NULL, ALC_INVALID_ENUM);
  1382. return NULL;
  1383. }
  1384. device->UpdateSize = SampleSize;
  1385. device->NumUpdates = 1;
  1386. LockLists();
  1387. if((err=ALCdevice_OpenCapture(device, deviceName)) != ALC_NO_ERROR)
  1388. {
  1389. UnlockLists();
  1390. DeleteCriticalSection(&device->Mutex);
  1391. free(device);
  1392. alcSetError(NULL, err);
  1393. return NULL;
  1394. }
  1395. UnlockLists();
  1396. do {
  1397. device->next = DeviceList;
  1398. } while(!CompExchangePtr((void**)&DeviceList, device->next, device));
  1399. TRACE("Created device %p\n", device);
  1400. return device;
  1401. }
  1402. ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *pDevice)
  1403. {
  1404. ALCdevice *volatile*list;
  1405. LockLists();
  1406. list = &DeviceList;
  1407. while(*list && *list != pDevice)
  1408. list = &(*list)->next;
  1409. if(!*list || !(*list)->IsCaptureDevice)
  1410. {
  1411. alcSetError(*list, ALC_INVALID_DEVICE);
  1412. UnlockLists();
  1413. return ALC_FALSE;
  1414. }
  1415. *list = (*list)->next;
  1416. UnlockLists();
  1417. LockDevice(pDevice);
  1418. ALCdevice_CloseCapture(pDevice);
  1419. UnlockDevice(pDevice);
  1420. ALCdevice_DecRef(pDevice);
  1421. return ALC_TRUE;
  1422. }
  1423. ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
  1424. {
  1425. if(!(device=VerifyDevice(device)) || !device->IsCaptureDevice)
  1426. {
  1427. alcSetError(device, ALC_INVALID_DEVICE);
  1428. if(device) ALCdevice_DecRef(device);
  1429. return;
  1430. }
  1431. LockDevice(device);
  1432. if(device->Connected)
  1433. ALCdevice_StartCapture(device);
  1434. UnlockDevice(device);
  1435. ALCdevice_DecRef(device);
  1436. }
  1437. ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
  1438. {
  1439. if(!(device=VerifyDevice(device)) || !device->IsCaptureDevice)
  1440. {
  1441. alcSetError(device, ALC_INVALID_DEVICE);
  1442. if(device) ALCdevice_DecRef(device);
  1443. return;
  1444. }
  1445. LockDevice(device);
  1446. if(device->Connected)
  1447. ALCdevice_StopCapture(device);
  1448. UnlockDevice(device);
  1449. ALCdevice_DecRef(device);
  1450. }
  1451. ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
  1452. {
  1453. ALCenum err = ALC_INVALID_DEVICE;
  1454. if((device=VerifyDevice(device)) != NULL && device->IsCaptureDevice)
  1455. {
  1456. err = ALC_INVALID_VALUE;
  1457. LockDevice(device);
  1458. if(samples >= 0 && ALCdevice_AvailableSamples(device) >= (ALCuint)samples)
  1459. err = ALCdevice_CaptureSamples(device, buffer, samples);
  1460. UnlockDevice(device);
  1461. }
  1462. if(err != ALC_NO_ERROR)
  1463. alcSetError(device, err);
  1464. if(device) ALCdevice_DecRef(device);
  1465. }
  1466. /*
  1467. alcGetError
  1468. Return last ALC generated error code
  1469. */
  1470. ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
  1471. {
  1472. ALCenum errorCode;
  1473. if(VerifyDevice(device))
  1474. {
  1475. errorCode = ExchangeInt(&device->LastError, ALC_NO_ERROR);
  1476. ALCdevice_DecRef(device);
  1477. }
  1478. else
  1479. errorCode = ExchangeInt(&g_eLastNullDeviceError, ALC_NO_ERROR);
  1480. return errorCode;
  1481. }
  1482. /* alcSuspendContext
  1483. *
  1484. * Not functional
  1485. */
  1486. ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *Context)
  1487. {
  1488. (void)Context;
  1489. }
  1490. /* alcProcessContext
  1491. *
  1492. * Not functional
  1493. */
  1494. ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *Context)
  1495. {
  1496. (void)Context;
  1497. }
  1498. /* alcGetString
  1499. *
  1500. * Returns information about the Device, and error strings
  1501. */
  1502. ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *pDevice,ALCenum param)
  1503. {
  1504. const ALCchar *value = NULL;
  1505. switch(param)
  1506. {
  1507. case ALC_NO_ERROR:
  1508. value = alcNoError;
  1509. break;
  1510. case ALC_INVALID_ENUM:
  1511. value = alcErrInvalidEnum;
  1512. break;
  1513. case ALC_INVALID_VALUE:
  1514. value = alcErrInvalidValue;
  1515. break;
  1516. case ALC_INVALID_DEVICE:
  1517. value = alcErrInvalidDevice;
  1518. break;
  1519. case ALC_INVALID_CONTEXT:
  1520. value = alcErrInvalidContext;
  1521. break;
  1522. case ALC_OUT_OF_MEMORY:
  1523. value = alcErrOutOfMemory;
  1524. break;
  1525. case ALC_DEVICE_SPECIFIER:
  1526. if(VerifyDevice(pDevice))
  1527. {
  1528. value = pDevice->szDeviceName;
  1529. ALCdevice_DecRef(pDevice);
  1530. }
  1531. else
  1532. {
  1533. ProbeDeviceList();
  1534. value = alcDeviceList;
  1535. }
  1536. break;
  1537. case ALC_ALL_DEVICES_SPECIFIER:
  1538. ProbeAllDeviceList();
  1539. value = alcAllDeviceList;
  1540. break;
  1541. case ALC_CAPTURE_DEVICE_SPECIFIER:
  1542. if(VerifyDevice(pDevice))
  1543. {
  1544. value = pDevice->szDeviceName;
  1545. ALCdevice_DecRef(pDevice);
  1546. }
  1547. else
  1548. {
  1549. ProbeCaptureDeviceList();
  1550. value = alcCaptureDeviceList;
  1551. }
  1552. break;
  1553. /* Default devices are always first in the list */
  1554. case ALC_DEFAULT_DEVICE_SPECIFIER:
  1555. if(!alcDeviceList)
  1556. ProbeDeviceList();
  1557. pDevice = VerifyDevice(pDevice);
  1558. free(alcDefaultDeviceSpecifier);
  1559. alcDefaultDeviceSpecifier = strdup(alcDeviceList ? alcDeviceList : "");
  1560. if(!alcDefaultDeviceSpecifier)
  1561. alcSetError(pDevice, ALC_OUT_OF_MEMORY);
  1562. value = alcDefaultDeviceSpecifier;
  1563. if(pDevice) ALCdevice_DecRef(pDevice);
  1564. break;
  1565. case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
  1566. if(!alcAllDeviceList)
  1567. ProbeAllDeviceList();
  1568. pDevice = VerifyDevice(pDevice);
  1569. free(alcDefaultAllDeviceSpecifier);
  1570. alcDefaultAllDeviceSpecifier = strdup(alcAllDeviceList ?
  1571. alcAllDeviceList : "");
  1572. if(!alcDefaultAllDeviceSpecifier)
  1573. alcSetError(pDevice, ALC_OUT_OF_MEMORY);
  1574. value = alcDefaultAllDeviceSpecifier;
  1575. if(pDevice) ALCdevice_DecRef(pDevice);
  1576. break;
  1577. case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
  1578. if(!alcCaptureDeviceList)
  1579. ProbeCaptureDeviceList();
  1580. pDevice = VerifyDevice(pDevice);
  1581. free(alcCaptureDefaultDeviceSpecifier);
  1582. alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
  1583. alcCaptureDeviceList : "");
  1584. if(!alcCaptureDefaultDeviceSpecifier)
  1585. alcSetError(pDevice, ALC_OUT_OF_MEMORY);
  1586. value = alcCaptureDefaultDeviceSpecifier;
  1587. if(pDevice) ALCdevice_DecRef(pDevice);
  1588. break;
  1589. case ALC_EXTENSIONS:
  1590. if(!VerifyDevice(pDevice))
  1591. value = alcNoDeviceExtList;
  1592. else
  1593. {
  1594. value = alcExtensionList;
  1595. ALCdevice_DecRef(pDevice);
  1596. }
  1597. break;
  1598. default:
  1599. pDevice = VerifyDevice(pDevice);
  1600. alcSetError(pDevice, ALC_INVALID_ENUM);
  1601. if(pDevice) ALCdevice_DecRef(pDevice);
  1602. break;
  1603. }
  1604. return value;
  1605. }
  1606. /* alcGetIntegerv
  1607. *
  1608. * Returns information about the Device and the version of Open AL
  1609. */
  1610. ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
  1611. {
  1612. device = VerifyDevice(device);
  1613. if(size == 0 || data == NULL)
  1614. {
  1615. alcSetError(device, ALC_INVALID_VALUE);
  1616. if(device) ALCdevice_DecRef(device);
  1617. return;
  1618. }
  1619. if(!device)
  1620. {
  1621. switch(param)
  1622. {
  1623. case ALC_MAJOR_VERSION:
  1624. *data = alcMajorVersion;
  1625. break;
  1626. case ALC_MINOR_VERSION:
  1627. *data = alcMinorVersion;
  1628. break;
  1629. case ALC_ATTRIBUTES_SIZE:
  1630. case ALC_ALL_ATTRIBUTES:
  1631. case ALC_FREQUENCY:
  1632. case ALC_REFRESH:
  1633. case ALC_SYNC:
  1634. case ALC_MONO_SOURCES:
  1635. case ALC_STEREO_SOURCES:
  1636. case ALC_CAPTURE_SAMPLES:
  1637. case ALC_FORMAT_CHANNELS_SOFT:
  1638. case ALC_FORMAT_TYPE_SOFT:
  1639. alcSetError(NULL, ALC_INVALID_DEVICE);
  1640. break;
  1641. default:
  1642. alcSetError(NULL, ALC_INVALID_ENUM);
  1643. break;
  1644. }
  1645. }
  1646. else if(device->IsCaptureDevice)
  1647. {
  1648. switch(param)
  1649. {
  1650. case ALC_CAPTURE_SAMPLES:
  1651. LockDevice(device);
  1652. *data = ALCdevice_AvailableSamples(device);
  1653. UnlockDevice(device);
  1654. break;
  1655. case ALC_CONNECTED:
  1656. *data = device->Connected;
  1657. break;
  1658. default:
  1659. alcSetError(device, ALC_INVALID_ENUM);
  1660. break;
  1661. }
  1662. }
  1663. else /* render device */
  1664. {
  1665. switch(param)
  1666. {
  1667. case ALC_MAJOR_VERSION:
  1668. *data = alcMajorVersion;
  1669. break;
  1670. case ALC_MINOR_VERSION:
  1671. *data = alcMinorVersion;
  1672. break;
  1673. case ALC_EFX_MAJOR_VERSION:
  1674. *data = alcEFXMajorVersion;
  1675. break;
  1676. case ALC_EFX_MINOR_VERSION:
  1677. *data = alcEFXMinorVersion;
  1678. break;
  1679. case ALC_ATTRIBUTES_SIZE:
  1680. *data = 13;
  1681. break;
  1682. case ALC_ALL_ATTRIBUTES:
  1683. if(size < 13)
  1684. alcSetError(device, ALC_INVALID_VALUE);
  1685. else
  1686. {
  1687. int i = 0;
  1688. data[i++] = ALC_FREQUENCY;
  1689. data[i++] = device->Frequency;
  1690. if(!device->IsLoopbackDevice)
  1691. {
  1692. data[i++] = ALC_REFRESH;
  1693. data[i++] = device->Frequency / device->UpdateSize;
  1694. data[i++] = ALC_SYNC;
  1695. data[i++] = ALC_FALSE;
  1696. }
  1697. else
  1698. {
  1699. data[i++] = ALC_FORMAT_CHANNELS_SOFT;
  1700. data[i++] = device->FmtChans;
  1701. data[i++] = ALC_FORMAT_TYPE_SOFT;
  1702. data[i++] = device->FmtType;
  1703. }
  1704. data[i++] = ALC_MONO_SOURCES;
  1705. data[i++] = device->NumMonoSources;
  1706. data[i++] = ALC_STEREO_SOURCES;
  1707. data[i++] = device->NumStereoSources;
  1708. data[i++] = ALC_MAX_AUXILIARY_SENDS;
  1709. data[i++] = device->NumAuxSends;
  1710. data[i++] = 0;
  1711. }
  1712. break;
  1713. case ALC_FREQUENCY:
  1714. *data = device->Frequency;
  1715. break;
  1716. case ALC_REFRESH:
  1717. if(device->IsLoopbackDevice)
  1718. alcSetError(device, ALC_INVALID_DEVICE);
  1719. else
  1720. *data = device->Frequency / device->UpdateSize;
  1721. break;
  1722. case ALC_SYNC:
  1723. if(device->IsLoopbackDevice)
  1724. alcSetError(device, ALC_INVALID_DEVICE);
  1725. else
  1726. *data = ALC_FALSE;
  1727. break;
  1728. case ALC_FORMAT_CHANNELS_SOFT:
  1729. if(!device->IsLoopbackDevice)
  1730. alcSetError(device, ALC_INVALID_DEVICE);
  1731. else
  1732. *data = device->FmtChans;
  1733. break;
  1734. case ALC_FORMAT_TYPE_SOFT:
  1735. if(!device->IsLoopbackDevice)
  1736. alcSetError(device, ALC_INVALID_DEVICE);
  1737. else
  1738. *data = device->FmtType;
  1739. break;
  1740. case ALC_MONO_SOURCES:
  1741. *data = device->NumMonoSources;
  1742. break;
  1743. case ALC_STEREO_SOURCES:
  1744. *data = device->NumStereoSources;
  1745. break;
  1746. case ALC_MAX_AUXILIARY_SENDS:
  1747. *data = device->NumAuxSends;
  1748. break;
  1749. case ALC_CONNECTED:
  1750. *data = device->Connected;
  1751. break;
  1752. default:
  1753. alcSetError(device, ALC_INVALID_ENUM);
  1754. break;
  1755. }
  1756. }
  1757. if(device)
  1758. ALCdevice_DecRef(device);
  1759. }
  1760. /* alcIsExtensionPresent
  1761. *
  1762. * Determines if there is support for a particular extension
  1763. */
  1764. ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
  1765. {
  1766. ALCboolean bResult = ALC_FALSE;
  1767. device = VerifyDevice(device);
  1768. if(!extName)
  1769. alcSetError(device, ALC_INVALID_VALUE);
  1770. else
  1771. {
  1772. size_t len = strlen(extName);
  1773. const char *ptr = (device ? alcExtensionList : alcNoDeviceExtList);
  1774. while(ptr && *ptr)
  1775. {
  1776. if(strncasecmp(ptr, extName, len) == 0 &&
  1777. (ptr[len] == '\0' || isspace(ptr[len])))
  1778. {
  1779. bResult = ALC_TRUE;
  1780. break;
  1781. }
  1782. if((ptr=strchr(ptr, ' ')) != NULL)
  1783. {
  1784. do {
  1785. ++ptr;
  1786. } while(isspace(*ptr));
  1787. }
  1788. }
  1789. }
  1790. if(device)
  1791. ALCdevice_DecRef(device);
  1792. return bResult;
  1793. }
  1794. /* alcGetProcAddress
  1795. *
  1796. * Retrieves the function address for a particular extension function
  1797. */
  1798. ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
  1799. {
  1800. ALCvoid *ptr = NULL;
  1801. device = VerifyDevice(device);
  1802. if(!funcName)
  1803. alcSetError(device, ALC_INVALID_VALUE);
  1804. else
  1805. {
  1806. ALsizei i = 0;
  1807. while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName,funcName) != 0)
  1808. i++;
  1809. ptr = alcFunctions[i].address;
  1810. }
  1811. if(device)
  1812. ALCdevice_DecRef(device);
  1813. return ptr;
  1814. }
  1815. /* alcGetEnumValue
  1816. *
  1817. * Get the value for a particular ALC Enumerated Value
  1818. */
  1819. ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
  1820. {
  1821. ALCenum val = 0;
  1822. device = VerifyDevice(device);
  1823. if(!enumName)
  1824. alcSetError(device, ALC_INVALID_VALUE);
  1825. else
  1826. {
  1827. ALsizei i = 0;
  1828. while(enumeration[i].enumName && strcmp(enumeration[i].enumName,enumName) != 0)
  1829. i++;
  1830. val = enumeration[i].value;
  1831. }
  1832. if(device)
  1833. ALCdevice_DecRef(device);
  1834. return val;
  1835. }
  1836. /* alcCreateContext
  1837. *
  1838. * Create and attach a Context to a particular Device.
  1839. */
  1840. ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
  1841. {
  1842. ALCcontext *ALContext;
  1843. LockLists();
  1844. if(!(device=VerifyDevice(device)) || device->IsCaptureDevice || !device->Connected)
  1845. {
  1846. UnlockLists();
  1847. alcSetError(device, ALC_INVALID_DEVICE);
  1848. if(device) ALCdevice_DecRef(device);
  1849. return NULL;
  1850. }
  1851. /* Reset Context Last Error code */
  1852. device->LastError = ALC_NO_ERROR;
  1853. if(UpdateDeviceParams(device, attrList) == ALC_FALSE)
  1854. {
  1855. UnlockLists();
  1856. alcSetError(device, ALC_INVALID_DEVICE);
  1857. aluHandleDisconnect(device);
  1858. ALCdevice_DecRef(device);
  1859. return NULL;
  1860. }
  1861. ALContext = calloc(1, sizeof(ALCcontext));
  1862. if(ALContext)
  1863. {
  1864. ALContext->ref = 1;
  1865. ALContext->MaxActiveSources = 256;
  1866. ALContext->ActiveSources = malloc(sizeof(ALContext->ActiveSources[0]) *
  1867. ALContext->MaxActiveSources);
  1868. }
  1869. if(!ALContext || !ALContext->ActiveSources)
  1870. {
  1871. if(!device->ContextList)
  1872. {
  1873. ALCdevice_StopPlayback(device);
  1874. device->Flags &= ~DEVICE_RUNNING;
  1875. }
  1876. UnlockLists();
  1877. free(ALContext);
  1878. ALContext = NULL;
  1879. alcSetError(device, ALC_OUT_OF_MEMORY);
  1880. ALCdevice_DecRef(device);
  1881. return NULL;
  1882. }
  1883. ALContext->Device = device;
  1884. ALCdevice_IncRef(device);
  1885. InitContext(ALContext);
  1886. ALContext->next = device->ContextList;
  1887. device->ContextList = ALContext;
  1888. UnlockLists();
  1889. ALCdevice_DecRef(device);
  1890. TRACE("Created context %p\n", ALContext);
  1891. return ALContext;
  1892. }
  1893. /* alcDestroyContext
  1894. *
  1895. * Remove a Context
  1896. */
  1897. ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
  1898. {
  1899. ALCdevice *Device;
  1900. LockLists();
  1901. /* alcGetContextsDevice sets an error for invalid contexts */
  1902. Device = alcGetContextsDevice(context);
  1903. if(Device)
  1904. {
  1905. ReleaseContext(context, Device);
  1906. if(!Device->ContextList)
  1907. {
  1908. ALCdevice_StopPlayback(Device);
  1909. Device->Flags &= ~DEVICE_RUNNING;
  1910. }
  1911. }
  1912. UnlockLists();
  1913. }
  1914. /* alcGetCurrentContext
  1915. *
  1916. * Returns the currently active Context
  1917. */
  1918. ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(ALCvoid)
  1919. {
  1920. ALCcontext *Context;
  1921. Context = pthread_getspecific(LocalContext);
  1922. if(!Context) Context = GlobalContext;
  1923. return Context;
  1924. }
  1925. /* alcGetThreadContext
  1926. *
  1927. * Returns the currently active thread-local Context
  1928. */
  1929. ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
  1930. {
  1931. ALCcontext *Context;
  1932. Context = pthread_getspecific(LocalContext);
  1933. return Context;
  1934. }
  1935. /* alcMakeContextCurrent
  1936. *
  1937. * Makes the given Context the active Context
  1938. */
  1939. ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
  1940. {
  1941. /* context must be a valid Context or NULL */
  1942. if(context && !(context=VerifyContext(context)))
  1943. {
  1944. alcSetError(NULL, ALC_INVALID_CONTEXT);
  1945. return ALC_FALSE;
  1946. }
  1947. /* context's reference count is already incremented */
  1948. context = ExchangePtr((void**)&GlobalContext, context);
  1949. if(context) ALCcontext_DecRef(context);
  1950. if((context=pthread_getspecific(LocalContext)) != NULL)
  1951. {
  1952. pthread_setspecific(LocalContext, NULL);
  1953. ALCcontext_DecRef(context);
  1954. }
  1955. return ALC_TRUE;
  1956. }
  1957. /* alcSetThreadContext
  1958. *
  1959. * Makes the given Context the active Context for the current thread
  1960. */
  1961. ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
  1962. {
  1963. ALCcontext *old;
  1964. /* context must be a valid Context or NULL */
  1965. if(context && !(context=VerifyContext(context)))
  1966. {
  1967. alcSetError(NULL, ALC_INVALID_CONTEXT);
  1968. return ALC_FALSE;
  1969. }
  1970. /* context's reference count is already incremented */
  1971. old = pthread_getspecific(LocalContext);
  1972. pthread_setspecific(LocalContext, context);
  1973. if(old) ALCcontext_DecRef(old);
  1974. return ALC_TRUE;
  1975. }
  1976. /* alcGetContextsDevice
  1977. *
  1978. * Returns the Device that a particular Context is attached to
  1979. */
  1980. ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context)
  1981. {
  1982. ALCdevice *Device;
  1983. if(!(Context=VerifyContext(Context)))
  1984. {
  1985. alcSetError(NULL, ALC_INVALID_CONTEXT);
  1986. return NULL;
  1987. }
  1988. Device = Context->Device;
  1989. ALCcontext_DecRef(Context);
  1990. return Device;
  1991. }
  1992. static void GetFormatFromString(const char *str, enum DevFmtChannels *chans, enum DevFmtType *type)
  1993. {
  1994. static const struct {
  1995. const char name[32];
  1996. enum DevFmtChannels channels;
  1997. enum DevFmtType type;
  1998. } formats[] = {
  1999. { "AL_FORMAT_MONO32", DevFmtMono, DevFmtFloat },
  2000. { "AL_FORMAT_STEREO32", DevFmtStereo, DevFmtFloat },
  2001. { "AL_FORMAT_QUAD32", DevFmtQuad, DevFmtFloat },
  2002. { "AL_FORMAT_51CHN32", DevFmtX51, DevFmtFloat },
  2003. { "AL_FORMAT_61CHN32", DevFmtX61, DevFmtFloat },
  2004. { "AL_FORMAT_71CHN32", DevFmtX71, DevFmtFloat },
  2005. { "AL_FORMAT_MONO16", DevFmtMono, DevFmtShort },
  2006. { "AL_FORMAT_STEREO16", DevFmtStereo, DevFmtShort },
  2007. { "AL_FORMAT_QUAD16", DevFmtQuad, DevFmtShort },
  2008. { "AL_FORMAT_51CHN16", DevFmtX51, DevFmtShort },
  2009. { "AL_FORMAT_61CHN16", DevFmtX61, DevFmtShort },
  2010. { "AL_FORMAT_71CHN16", DevFmtX71, DevFmtShort },
  2011. { "AL_FORMAT_MONO8", DevFmtMono, DevFmtByte },
  2012. { "AL_FORMAT_STEREO8", DevFmtStereo, DevFmtByte },
  2013. { "AL_FORMAT_QUAD8", DevFmtQuad, DevFmtByte },
  2014. { "AL_FORMAT_51CHN8", DevFmtX51, DevFmtByte },
  2015. { "AL_FORMAT_61CHN8", DevFmtX61, DevFmtByte },
  2016. { "AL_FORMAT_71CHN8", DevFmtX71, DevFmtByte }
  2017. };
  2018. size_t i;
  2019. for(i = 0;i < sizeof(formats)/sizeof(formats[0]);i++)
  2020. {
  2021. if(strcasecmp(str, formats[i].name) == 0)
  2022. {
  2023. *chans = formats[i].channels;
  2024. *type = formats[i].type;
  2025. return;
  2026. }
  2027. }
  2028. ERR("Unknown format: \"%s\"\n", str);
  2029. *chans = DevFmtStereo;
  2030. *type = DevFmtShort;
  2031. }
  2032. /* alcOpenDevice
  2033. *
  2034. * Open the Device specified.
  2035. */
  2036. ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
  2037. {
  2038. const ALCchar *fmt;
  2039. ALCdevice *device;
  2040. ALCenum err;
  2041. DO_INITCONFIG();
  2042. if(!PlaybackBackend.name)
  2043. {
  2044. alcSetError(NULL, ALC_INVALID_VALUE);
  2045. return NULL;
  2046. }
  2047. if(deviceName && (!deviceName[0] || strcasecmp(deviceName, "openal soft") == 0 || strcasecmp(deviceName, "openal-soft") == 0))
  2048. deviceName = NULL;
  2049. device = calloc(1, sizeof(ALCdevice));
  2050. if(!device)
  2051. {
  2052. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2053. return NULL;
  2054. }
  2055. //Validate device
  2056. device->Funcs = &PlaybackBackend.Funcs;
  2057. device->ref = 1;
  2058. device->Connected = ALC_TRUE;
  2059. device->IsCaptureDevice = AL_FALSE;
  2060. device->IsLoopbackDevice = AL_FALSE;
  2061. InitializeCriticalSection(&device->Mutex);
  2062. device->LastError = ALC_NO_ERROR;
  2063. device->Flags = 0;
  2064. device->Bs2b = NULL;
  2065. device->Bs2bLevel = 0;
  2066. device->szDeviceName = NULL;
  2067. device->ContextList = NULL;
  2068. device->MaxNoOfSources = 256;
  2069. device->AuxiliaryEffectSlotMax = 4;
  2070. device->NumAuxSends = MAX_SENDS;
  2071. InitUIntMap(&device->BufferMap, ~0);
  2072. InitUIntMap(&device->EffectMap, ~0);
  2073. InitUIntMap(&device->FilterMap, ~0);
  2074. //Set output format
  2075. device->NumUpdates = 4;
  2076. device->UpdateSize = 1024;
  2077. device->Frequency = DEFAULT_OUTPUT_RATE;
  2078. if(ConfigValueUInt(NULL, "frequency", &device->Frequency))
  2079. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  2080. device->Frequency = maxu(device->Frequency, 8000);
  2081. fmt = "AL_FORMAT_STEREO16";
  2082. if(ConfigValueStr(NULL, "format", &fmt))
  2083. device->Flags |= DEVICE_CHANNELS_REQUEST;
  2084. GetFormatFromString(fmt, &device->FmtChans, &device->FmtType);
  2085. ConfigValueUInt(NULL, "periods", &device->NumUpdates);
  2086. if(device->NumUpdates < 2) device->NumUpdates = 4;
  2087. ConfigValueUInt(NULL, "period_size", &device->UpdateSize);
  2088. if(device->UpdateSize == 0) device->UpdateSize = 1024;
  2089. ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
  2090. if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
  2091. ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
  2092. if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
  2093. ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
  2094. if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
  2095. ConfigValueInt(NULL, "cf_level", &device->Bs2bLevel);
  2096. device->NumStereoSources = 1;
  2097. device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
  2098. // Find a playback device to open
  2099. LockLists();
  2100. if((err=ALCdevice_OpenPlayback(device, deviceName)) != ALC_NO_ERROR)
  2101. {
  2102. UnlockLists();
  2103. DeleteCriticalSection(&device->Mutex);
  2104. free(device);
  2105. alcSetError(NULL, err);
  2106. return NULL;
  2107. }
  2108. UnlockLists();
  2109. do {
  2110. device->next = DeviceList;
  2111. } while(!CompExchangePtr((void**)&DeviceList, device->next, device));
  2112. TRACE("Created device %p\n", device);
  2113. return device;
  2114. }
  2115. /* alcCloseDevice
  2116. *
  2117. * Close the specified Device
  2118. */
  2119. ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *pDevice)
  2120. {
  2121. ALCdevice *volatile*list;
  2122. ALCcontext *ctx;
  2123. LockLists();
  2124. list = &DeviceList;
  2125. while(*list && *list != pDevice)
  2126. list = &(*list)->next;
  2127. if(!*list || (*list)->IsCaptureDevice)
  2128. {
  2129. alcSetError(*list, ALC_INVALID_DEVICE);
  2130. UnlockLists();
  2131. return ALC_FALSE;
  2132. }
  2133. *list = (*list)->next;
  2134. UnlockLists();
  2135. if((ctx=pDevice->ContextList) != NULL)
  2136. {
  2137. do {
  2138. WARN("Releasing context %p\n", ctx);
  2139. ReleaseContext(ctx, pDevice);
  2140. } while((ctx=pDevice->ContextList) != NULL);
  2141. ALCdevice_StopPlayback(pDevice);
  2142. pDevice->Flags &= ~DEVICE_RUNNING;
  2143. }
  2144. ALCdevice_ClosePlayback(pDevice);
  2145. ALCdevice_DecRef(pDevice);
  2146. return ALC_TRUE;
  2147. }
  2148. /* alcLoopbackOpenDeviceSOFT
  2149. *
  2150. * Open a loopback device, for manual rendering.
  2151. */
  2152. ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(void)
  2153. {
  2154. ALCdevice *device;
  2155. DO_INITCONFIG();
  2156. device = calloc(1, sizeof(ALCdevice));
  2157. if(!device)
  2158. {
  2159. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2160. return NULL;
  2161. }
  2162. //Validate device
  2163. device->Funcs = &BackendLoopback.Funcs;
  2164. device->ref = 1;
  2165. device->Connected = ALC_TRUE;
  2166. device->IsCaptureDevice = AL_FALSE;
  2167. device->IsLoopbackDevice = AL_TRUE;
  2168. InitializeCriticalSection(&device->Mutex);
  2169. device->LastError = ALC_NO_ERROR;
  2170. device->Flags = 0;
  2171. device->Bs2b = NULL;
  2172. device->Bs2bLevel = 0;
  2173. device->szDeviceName = NULL;
  2174. device->ContextList = NULL;
  2175. device->MaxNoOfSources = 256;
  2176. device->AuxiliaryEffectSlotMax = 4;
  2177. device->NumAuxSends = MAX_SENDS;
  2178. InitUIntMap(&device->BufferMap, ~0);
  2179. InitUIntMap(&device->EffectMap, ~0);
  2180. InitUIntMap(&device->FilterMap, ~0);
  2181. //Set output format
  2182. device->NumUpdates = 0;
  2183. device->UpdateSize = 0;
  2184. device->Frequency = 44100;
  2185. device->FmtChans = DevFmtStereo;
  2186. device->FmtType = DevFmtShort;
  2187. ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
  2188. if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
  2189. ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
  2190. if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
  2191. ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
  2192. if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
  2193. ConfigValueInt(NULL, "cf_level", &device->Bs2bLevel);
  2194. device->NumStereoSources = 1;
  2195. device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
  2196. // Open the "backend"
  2197. ALCdevice_OpenPlayback(device, "Loopback");
  2198. do {
  2199. device->next = DeviceList;
  2200. } while(!CompExchangePtr((void**)&DeviceList, device->next, device));
  2201. TRACE("Created device %p\n", device);
  2202. return device;
  2203. }
  2204. /* alcIsRenderFormatSupportedSOFT
  2205. *
  2206. * Determines if the loopback device supports the given format for rendering.
  2207. */
  2208. ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type)
  2209. {
  2210. ALCboolean ret = ALC_FALSE;
  2211. if(!(device=VerifyDevice(device)) || !device->IsLoopbackDevice)
  2212. alcSetError(device, ALC_INVALID_DEVICE);
  2213. else if(freq <= 0)
  2214. alcSetError(device, ALC_INVALID_VALUE);
  2215. else if(!IsValidALCType(type) || !IsValidALCChannels(channels))
  2216. alcSetError(device, ALC_INVALID_ENUM);
  2217. else
  2218. {
  2219. if(BytesFromDevFmt(type) > 0 && ChannelsFromDevFmt(channels) > 0 &&
  2220. freq >= 8000)
  2221. ret = ALC_TRUE;
  2222. }
  2223. if(device) ALCdevice_DecRef(device);
  2224. return ret;
  2225. }
  2226. /* alcRenderSamplesSOFT
  2227. *
  2228. * Renders some samples into a buffer, using the format last set by the
  2229. * attributes given to alcCreateContext.
  2230. */
  2231. ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
  2232. {
  2233. if(!(device=VerifyDevice(device)) || !device->IsLoopbackDevice)
  2234. alcSetError(device, ALC_INVALID_DEVICE);
  2235. else if(samples < 0 || (samples > 0 && buffer == NULL))
  2236. alcSetError(device, ALC_INVALID_VALUE);
  2237. else
  2238. aluMixData(device, buffer, samples);
  2239. if(device) ALCdevice_DecRef(device);
  2240. }
  2241. static void ReleaseALC(void)
  2242. {
  2243. ALCdevice *dev;
  2244. free(alcDeviceList); alcDeviceList = NULL;
  2245. alcDeviceListSize = 0;
  2246. free(alcAllDeviceList); alcAllDeviceList = NULL;
  2247. alcAllDeviceListSize = 0;
  2248. free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
  2249. alcCaptureDeviceListSize = 0;
  2250. free(alcDefaultDeviceSpecifier);
  2251. alcDefaultDeviceSpecifier = NULL;
  2252. free(alcDefaultAllDeviceSpecifier);
  2253. alcDefaultAllDeviceSpecifier = NULL;
  2254. free(alcCaptureDefaultDeviceSpecifier);
  2255. alcCaptureDefaultDeviceSpecifier = NULL;
  2256. if((dev=ExchangePtr((void**)&DeviceList, NULL)) != NULL)
  2257. {
  2258. ALCuint num = 0;
  2259. do {
  2260. num++;
  2261. } while((dev=dev->next) != NULL);
  2262. ERR("%u device%s not closed\n", num, (num>1)?"s":"");
  2263. }
  2264. }
  2265. ///////////////////////////////////////////////////////