ALc.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558
  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 "alListener.h"
  30. #include "alThunk.h"
  31. #include "alSource.h"
  32. #include "alBuffer.h"
  33. #include "alAuxEffectSlot.h"
  34. #include "alError.h"
  35. #include "alMidi.h"
  36. #include "bs2b.h"
  37. #include "alu.h"
  38. #include "backends/base.h"
  39. #include "midi/base.h"
  40. /************************************************
  41. * Backends
  42. ************************************************/
  43. struct BackendInfo {
  44. const char *name;
  45. ALCbackendFactory* (*getFactory)(void);
  46. ALCboolean (*Init)(BackendFuncs*);
  47. void (*Deinit)(void);
  48. void (*Probe)(enum DevProbe);
  49. BackendFuncs Funcs;
  50. };
  51. #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
  52. static struct BackendInfo BackendList[] = {
  53. #ifdef HAVE_PULSEAUDIO
  54. { "pulse", ALCpulseBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
  55. #endif
  56. #ifdef HAVE_ALSA
  57. { "alsa", ALCalsaBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
  58. #endif
  59. #ifdef HAVE_COREAUDIO
  60. { "core", NULL, alc_ca_init, alc_ca_deinit, alc_ca_probe, EmptyFuncs },
  61. #endif
  62. #ifdef HAVE_OSS
  63. { "oss", ALCossBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
  64. #endif
  65. #ifdef HAVE_SOLARIS
  66. { "solaris", NULL, alc_solaris_init, alc_solaris_deinit, alc_solaris_probe, EmptyFuncs },
  67. #endif
  68. #ifdef HAVE_SNDIO
  69. { "sndio", NULL, alc_sndio_init, alc_sndio_deinit, alc_sndio_probe, EmptyFuncs },
  70. #endif
  71. #ifdef HAVE_QSA
  72. { "qsa", NULL, alc_qsa_init, alc_qsa_deinit, alc_qsa_probe, EmptyFuncs },
  73. #endif
  74. #ifdef HAVE_MMDEVAPI
  75. { "mmdevapi", NULL, alcMMDevApiInit, alcMMDevApiDeinit, alcMMDevApiProbe, EmptyFuncs },
  76. #endif
  77. #ifdef HAVE_DSOUND
  78. { "dsound", NULL, alcDSoundInit, alcDSoundDeinit, alcDSoundProbe, EmptyFuncs },
  79. #endif
  80. #ifdef HAVE_WINMM
  81. { "winmm", NULL, alcWinMMInit, alcWinMMDeinit, alcWinMMProbe, EmptyFuncs },
  82. #endif
  83. #ifdef HAVE_PORTAUDIO
  84. { "port", NULL, alc_pa_init, alc_pa_deinit, alc_pa_probe, EmptyFuncs },
  85. #endif
  86. #ifdef HAVE_OPENSL
  87. { "opensl", NULL, alc_opensl_init, alc_opensl_deinit, alc_opensl_probe, EmptyFuncs },
  88. #endif
  89. { "null", ALCnullBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
  90. #ifdef HAVE_WAVE
  91. { "wave", NULL, alc_wave_init, alc_wave_deinit, alc_wave_probe, EmptyFuncs },
  92. #endif
  93. { NULL, NULL, NULL, NULL, NULL, EmptyFuncs }
  94. };
  95. #undef EmptyFuncs
  96. static struct BackendInfo PlaybackBackend;
  97. static struct BackendInfo CaptureBackend;
  98. /************************************************
  99. * Functions, enums, and errors
  100. ************************************************/
  101. typedef struct ALCfunction {
  102. const ALCchar *funcName;
  103. ALCvoid *address;
  104. } ALCfunction;
  105. typedef struct ALCenums {
  106. const ALCchar *enumName;
  107. ALCenum value;
  108. } ALCenums;
  109. #define DECL(x) { #x, (ALCvoid*)(x) }
  110. static const ALCfunction alcFunctions[] = {
  111. DECL(alcCreateContext),
  112. DECL(alcMakeContextCurrent),
  113. DECL(alcProcessContext),
  114. DECL(alcSuspendContext),
  115. DECL(alcDestroyContext),
  116. DECL(alcGetCurrentContext),
  117. DECL(alcGetContextsDevice),
  118. DECL(alcOpenDevice),
  119. DECL(alcCloseDevice),
  120. DECL(alcGetError),
  121. DECL(alcIsExtensionPresent),
  122. DECL(alcGetProcAddress),
  123. DECL(alcGetEnumValue),
  124. DECL(alcGetString),
  125. DECL(alcGetIntegerv),
  126. DECL(alcCaptureOpenDevice),
  127. DECL(alcCaptureCloseDevice),
  128. DECL(alcCaptureStart),
  129. DECL(alcCaptureStop),
  130. DECL(alcCaptureSamples),
  131. DECL(alcSetThreadContext),
  132. DECL(alcGetThreadContext),
  133. DECL(alcLoopbackOpenDeviceSOFT),
  134. DECL(alcIsRenderFormatSupportedSOFT),
  135. DECL(alcRenderSamplesSOFT),
  136. DECL(alcDevicePauseSOFT),
  137. DECL(alcDeviceResumeSOFT),
  138. DECL(alEnable),
  139. DECL(alDisable),
  140. DECL(alIsEnabled),
  141. DECL(alGetString),
  142. DECL(alGetBooleanv),
  143. DECL(alGetIntegerv),
  144. DECL(alGetFloatv),
  145. DECL(alGetDoublev),
  146. DECL(alGetBoolean),
  147. DECL(alGetInteger),
  148. DECL(alGetFloat),
  149. DECL(alGetDouble),
  150. DECL(alGetError),
  151. DECL(alIsExtensionPresent),
  152. DECL(alGetProcAddress),
  153. DECL(alGetEnumValue),
  154. DECL(alListenerf),
  155. DECL(alListener3f),
  156. DECL(alListenerfv),
  157. DECL(alListeneri),
  158. DECL(alListener3i),
  159. DECL(alListeneriv),
  160. DECL(alGetListenerf),
  161. DECL(alGetListener3f),
  162. DECL(alGetListenerfv),
  163. DECL(alGetListeneri),
  164. DECL(alGetListener3i),
  165. DECL(alGetListeneriv),
  166. DECL(alGenSources),
  167. DECL(alDeleteSources),
  168. DECL(alIsSource),
  169. DECL(alSourcef),
  170. DECL(alSource3f),
  171. DECL(alSourcefv),
  172. DECL(alSourcei),
  173. DECL(alSource3i),
  174. DECL(alSourceiv),
  175. DECL(alGetSourcef),
  176. DECL(alGetSource3f),
  177. DECL(alGetSourcefv),
  178. DECL(alGetSourcei),
  179. DECL(alGetSource3i),
  180. DECL(alGetSourceiv),
  181. DECL(alSourcePlayv),
  182. DECL(alSourceStopv),
  183. DECL(alSourceRewindv),
  184. DECL(alSourcePausev),
  185. DECL(alSourcePlay),
  186. DECL(alSourceStop),
  187. DECL(alSourceRewind),
  188. DECL(alSourcePause),
  189. DECL(alSourceQueueBuffers),
  190. DECL(alSourceUnqueueBuffers),
  191. DECL(alGenBuffers),
  192. DECL(alDeleteBuffers),
  193. DECL(alIsBuffer),
  194. DECL(alBufferData),
  195. DECL(alBufferf),
  196. DECL(alBuffer3f),
  197. DECL(alBufferfv),
  198. DECL(alBufferi),
  199. DECL(alBuffer3i),
  200. DECL(alBufferiv),
  201. DECL(alGetBufferf),
  202. DECL(alGetBuffer3f),
  203. DECL(alGetBufferfv),
  204. DECL(alGetBufferi),
  205. DECL(alGetBuffer3i),
  206. DECL(alGetBufferiv),
  207. DECL(alDopplerFactor),
  208. DECL(alDopplerVelocity),
  209. DECL(alSpeedOfSound),
  210. DECL(alDistanceModel),
  211. DECL(alGenFilters),
  212. DECL(alDeleteFilters),
  213. DECL(alIsFilter),
  214. DECL(alFilteri),
  215. DECL(alFilteriv),
  216. DECL(alFilterf),
  217. DECL(alFilterfv),
  218. DECL(alGetFilteri),
  219. DECL(alGetFilteriv),
  220. DECL(alGetFilterf),
  221. DECL(alGetFilterfv),
  222. DECL(alGenEffects),
  223. DECL(alDeleteEffects),
  224. DECL(alIsEffect),
  225. DECL(alEffecti),
  226. DECL(alEffectiv),
  227. DECL(alEffectf),
  228. DECL(alEffectfv),
  229. DECL(alGetEffecti),
  230. DECL(alGetEffectiv),
  231. DECL(alGetEffectf),
  232. DECL(alGetEffectfv),
  233. DECL(alGenAuxiliaryEffectSlots),
  234. DECL(alDeleteAuxiliaryEffectSlots),
  235. DECL(alIsAuxiliaryEffectSlot),
  236. DECL(alAuxiliaryEffectSloti),
  237. DECL(alAuxiliaryEffectSlotiv),
  238. DECL(alAuxiliaryEffectSlotf),
  239. DECL(alAuxiliaryEffectSlotfv),
  240. DECL(alGetAuxiliaryEffectSloti),
  241. DECL(alGetAuxiliaryEffectSlotiv),
  242. DECL(alGetAuxiliaryEffectSlotf),
  243. DECL(alGetAuxiliaryEffectSlotfv),
  244. DECL(alBufferSubDataSOFT),
  245. DECL(alBufferSamplesSOFT),
  246. DECL(alBufferSubSamplesSOFT),
  247. DECL(alGetBufferSamplesSOFT),
  248. DECL(alIsBufferFormatSupportedSOFT),
  249. DECL(alDeferUpdatesSOFT),
  250. DECL(alProcessUpdatesSOFT),
  251. DECL(alSourcedSOFT),
  252. DECL(alSource3dSOFT),
  253. DECL(alSourcedvSOFT),
  254. DECL(alGetSourcedSOFT),
  255. DECL(alGetSource3dSOFT),
  256. DECL(alGetSourcedvSOFT),
  257. DECL(alSourcei64SOFT),
  258. DECL(alSource3i64SOFT),
  259. DECL(alSourcei64vSOFT),
  260. DECL(alGetSourcei64SOFT),
  261. DECL(alGetSource3i64SOFT),
  262. DECL(alGetSourcei64vSOFT),
  263. DECL(alGenSoundfontsSOFT),
  264. DECL(alDeleteSoundfontsSOFT),
  265. DECL(alIsSoundfontSOFT),
  266. DECL(alSoundfontSamplesSOFT),
  267. DECL(alGetSoundfontSamplesSOFT),
  268. DECL(alSoundfontMapSamplesSOFT),
  269. DECL(alSoundfontUnmapSamplesSOFT),
  270. DECL(alGetSoundfontivSOFT),
  271. DECL(alSoundfontPresetsSOFT),
  272. DECL(alGenPresetsSOFT),
  273. DECL(alDeletePresetsSOFT),
  274. DECL(alIsPresetSOFT),
  275. DECL(alPresetiSOFT),
  276. DECL(alPresetivSOFT),
  277. DECL(alGetPresetivSOFT),
  278. DECL(alPresetFontsoundsSOFT),
  279. DECL(alGenFontsoundsSOFT),
  280. DECL(alDeleteFontsoundsSOFT),
  281. DECL(alIsFontsoundSOFT),
  282. DECL(alFontsoundiSOFT),
  283. DECL(alFontsound2iSOFT),
  284. DECL(alFontsoundivSOFT),
  285. DECL(alGetFontsoundivSOFT),
  286. DECL(alFontsoundModulatoriSOFT),
  287. DECL(alGetFontsoundModulatorivSOFT),
  288. DECL(alMidiSoundfontSOFT),
  289. DECL(alMidiSoundfontvSOFT),
  290. DECL(alMidiEventSOFT),
  291. DECL(alMidiSysExSOFT),
  292. DECL(alMidiPlaySOFT),
  293. DECL(alMidiPauseSOFT),
  294. DECL(alMidiStopSOFT),
  295. DECL(alMidiResetSOFT),
  296. DECL(alMidiGainSOFT),
  297. DECL(alGetInteger64SOFT),
  298. DECL(alGetInteger64vSOFT),
  299. DECL(alLoadSoundfontSOFT),
  300. { NULL, NULL }
  301. };
  302. #undef DECL
  303. #define DECL(x) { #x, (x) }
  304. static const ALCenums enumeration[] = {
  305. DECL(ALC_INVALID),
  306. DECL(ALC_FALSE),
  307. DECL(ALC_TRUE),
  308. DECL(ALC_MAJOR_VERSION),
  309. DECL(ALC_MINOR_VERSION),
  310. DECL(ALC_ATTRIBUTES_SIZE),
  311. DECL(ALC_ALL_ATTRIBUTES),
  312. DECL(ALC_DEFAULT_DEVICE_SPECIFIER),
  313. DECL(ALC_DEVICE_SPECIFIER),
  314. DECL(ALC_ALL_DEVICES_SPECIFIER),
  315. DECL(ALC_DEFAULT_ALL_DEVICES_SPECIFIER),
  316. DECL(ALC_EXTENSIONS),
  317. DECL(ALC_FREQUENCY),
  318. DECL(ALC_REFRESH),
  319. DECL(ALC_SYNC),
  320. DECL(ALC_MONO_SOURCES),
  321. DECL(ALC_STEREO_SOURCES),
  322. DECL(ALC_CAPTURE_DEVICE_SPECIFIER),
  323. DECL(ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER),
  324. DECL(ALC_CAPTURE_SAMPLES),
  325. DECL(ALC_CONNECTED),
  326. DECL(ALC_EFX_MAJOR_VERSION),
  327. DECL(ALC_EFX_MINOR_VERSION),
  328. DECL(ALC_MAX_AUXILIARY_SENDS),
  329. DECL(ALC_FORMAT_CHANNELS_SOFT),
  330. DECL(ALC_FORMAT_TYPE_SOFT),
  331. DECL(ALC_MONO_SOFT),
  332. DECL(ALC_STEREO_SOFT),
  333. DECL(ALC_QUAD_SOFT),
  334. DECL(ALC_5POINT1_SOFT),
  335. DECL(ALC_6POINT1_SOFT),
  336. DECL(ALC_7POINT1_SOFT),
  337. DECL(ALC_BYTE_SOFT),
  338. DECL(ALC_UNSIGNED_BYTE_SOFT),
  339. DECL(ALC_SHORT_SOFT),
  340. DECL(ALC_UNSIGNED_SHORT_SOFT),
  341. DECL(ALC_INT_SOFT),
  342. DECL(ALC_UNSIGNED_INT_SOFT),
  343. DECL(ALC_FLOAT_SOFT),
  344. DECL(ALC_NO_ERROR),
  345. DECL(ALC_INVALID_DEVICE),
  346. DECL(ALC_INVALID_CONTEXT),
  347. DECL(ALC_INVALID_ENUM),
  348. DECL(ALC_INVALID_VALUE),
  349. DECL(ALC_OUT_OF_MEMORY),
  350. DECL(AL_INVALID),
  351. DECL(AL_NONE),
  352. DECL(AL_FALSE),
  353. DECL(AL_TRUE),
  354. DECL(AL_SOURCE_RELATIVE),
  355. DECL(AL_CONE_INNER_ANGLE),
  356. DECL(AL_CONE_OUTER_ANGLE),
  357. DECL(AL_PITCH),
  358. DECL(AL_POSITION),
  359. DECL(AL_DIRECTION),
  360. DECL(AL_VELOCITY),
  361. DECL(AL_LOOPING),
  362. DECL(AL_BUFFER),
  363. DECL(AL_GAIN),
  364. DECL(AL_MIN_GAIN),
  365. DECL(AL_MAX_GAIN),
  366. DECL(AL_ORIENTATION),
  367. DECL(AL_REFERENCE_DISTANCE),
  368. DECL(AL_ROLLOFF_FACTOR),
  369. DECL(AL_CONE_OUTER_GAIN),
  370. DECL(AL_MAX_DISTANCE),
  371. DECL(AL_SEC_OFFSET),
  372. DECL(AL_SAMPLE_OFFSET),
  373. DECL(AL_SAMPLE_RW_OFFSETS_SOFT),
  374. DECL(AL_BYTE_OFFSET),
  375. DECL(AL_BYTE_RW_OFFSETS_SOFT),
  376. DECL(AL_SOURCE_TYPE),
  377. DECL(AL_STATIC),
  378. DECL(AL_STREAMING),
  379. DECL(AL_UNDETERMINED),
  380. DECL(AL_METERS_PER_UNIT),
  381. DECL(AL_DIRECT_CHANNELS_SOFT),
  382. DECL(AL_DIRECT_FILTER),
  383. DECL(AL_AUXILIARY_SEND_FILTER),
  384. DECL(AL_AIR_ABSORPTION_FACTOR),
  385. DECL(AL_ROOM_ROLLOFF_FACTOR),
  386. DECL(AL_CONE_OUTER_GAINHF),
  387. DECL(AL_DIRECT_FILTER_GAINHF_AUTO),
  388. DECL(AL_AUXILIARY_SEND_FILTER_GAIN_AUTO),
  389. DECL(AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO),
  390. DECL(AL_SOURCE_STATE),
  391. DECL(AL_INITIAL),
  392. DECL(AL_PLAYING),
  393. DECL(AL_PAUSED),
  394. DECL(AL_STOPPED),
  395. DECL(AL_BUFFERS_QUEUED),
  396. DECL(AL_BUFFERS_PROCESSED),
  397. DECL(AL_FORMAT_MONO8),
  398. DECL(AL_FORMAT_MONO16),
  399. DECL(AL_FORMAT_MONO_FLOAT32),
  400. DECL(AL_FORMAT_MONO_DOUBLE_EXT),
  401. DECL(AL_FORMAT_STEREO8),
  402. DECL(AL_FORMAT_STEREO16),
  403. DECL(AL_FORMAT_STEREO_FLOAT32),
  404. DECL(AL_FORMAT_STEREO_DOUBLE_EXT),
  405. DECL(AL_FORMAT_MONO_IMA4),
  406. DECL(AL_FORMAT_STEREO_IMA4),
  407. DECL(AL_FORMAT_QUAD8_LOKI),
  408. DECL(AL_FORMAT_QUAD16_LOKI),
  409. DECL(AL_FORMAT_QUAD8),
  410. DECL(AL_FORMAT_QUAD16),
  411. DECL(AL_FORMAT_QUAD32),
  412. DECL(AL_FORMAT_51CHN8),
  413. DECL(AL_FORMAT_51CHN16),
  414. DECL(AL_FORMAT_51CHN32),
  415. DECL(AL_FORMAT_61CHN8),
  416. DECL(AL_FORMAT_61CHN16),
  417. DECL(AL_FORMAT_61CHN32),
  418. DECL(AL_FORMAT_71CHN8),
  419. DECL(AL_FORMAT_71CHN16),
  420. DECL(AL_FORMAT_71CHN32),
  421. DECL(AL_FORMAT_REAR8),
  422. DECL(AL_FORMAT_REAR16),
  423. DECL(AL_FORMAT_REAR32),
  424. DECL(AL_FORMAT_MONO_MULAW),
  425. DECL(AL_FORMAT_MONO_MULAW_EXT),
  426. DECL(AL_FORMAT_STEREO_MULAW),
  427. DECL(AL_FORMAT_STEREO_MULAW_EXT),
  428. DECL(AL_FORMAT_QUAD_MULAW),
  429. DECL(AL_FORMAT_51CHN_MULAW),
  430. DECL(AL_FORMAT_61CHN_MULAW),
  431. DECL(AL_FORMAT_71CHN_MULAW),
  432. DECL(AL_FORMAT_REAR_MULAW),
  433. DECL(AL_FORMAT_MONO_ALAW_EXT),
  434. DECL(AL_FORMAT_STEREO_ALAW_EXT),
  435. DECL(AL_MONO8_SOFT),
  436. DECL(AL_MONO16_SOFT),
  437. DECL(AL_MONO32F_SOFT),
  438. DECL(AL_STEREO8_SOFT),
  439. DECL(AL_STEREO16_SOFT),
  440. DECL(AL_STEREO32F_SOFT),
  441. DECL(AL_QUAD8_SOFT),
  442. DECL(AL_QUAD16_SOFT),
  443. DECL(AL_QUAD32F_SOFT),
  444. DECL(AL_REAR8_SOFT),
  445. DECL(AL_REAR16_SOFT),
  446. DECL(AL_REAR32F_SOFT),
  447. DECL(AL_5POINT1_8_SOFT),
  448. DECL(AL_5POINT1_16_SOFT),
  449. DECL(AL_5POINT1_32F_SOFT),
  450. DECL(AL_6POINT1_8_SOFT),
  451. DECL(AL_6POINT1_16_SOFT),
  452. DECL(AL_6POINT1_32F_SOFT),
  453. DECL(AL_7POINT1_8_SOFT),
  454. DECL(AL_7POINT1_16_SOFT),
  455. DECL(AL_7POINT1_32F_SOFT),
  456. DECL(AL_MONO_SOFT),
  457. DECL(AL_STEREO_SOFT),
  458. DECL(AL_QUAD_SOFT),
  459. DECL(AL_REAR_SOFT),
  460. DECL(AL_5POINT1_SOFT),
  461. DECL(AL_6POINT1_SOFT),
  462. DECL(AL_7POINT1_SOFT),
  463. DECL(AL_BYTE_SOFT),
  464. DECL(AL_UNSIGNED_BYTE_SOFT),
  465. DECL(AL_SHORT_SOFT),
  466. DECL(AL_UNSIGNED_SHORT_SOFT),
  467. DECL(AL_INT_SOFT),
  468. DECL(AL_UNSIGNED_INT_SOFT),
  469. DECL(AL_FLOAT_SOFT),
  470. DECL(AL_DOUBLE_SOFT),
  471. DECL(AL_BYTE3_SOFT),
  472. DECL(AL_UNSIGNED_BYTE3_SOFT),
  473. DECL(AL_FREQUENCY),
  474. DECL(AL_BITS),
  475. DECL(AL_CHANNELS),
  476. DECL(AL_SIZE),
  477. DECL(AL_INTERNAL_FORMAT_SOFT),
  478. DECL(AL_BYTE_LENGTH_SOFT),
  479. DECL(AL_SAMPLE_LENGTH_SOFT),
  480. DECL(AL_SEC_LENGTH_SOFT),
  481. DECL(AL_UNUSED),
  482. DECL(AL_PENDING),
  483. DECL(AL_PROCESSED),
  484. DECL(AL_NO_ERROR),
  485. DECL(AL_INVALID_NAME),
  486. DECL(AL_INVALID_ENUM),
  487. DECL(AL_INVALID_VALUE),
  488. DECL(AL_INVALID_OPERATION),
  489. DECL(AL_OUT_OF_MEMORY),
  490. DECL(AL_VENDOR),
  491. DECL(AL_VERSION),
  492. DECL(AL_RENDERER),
  493. DECL(AL_EXTENSIONS),
  494. DECL(AL_DOPPLER_FACTOR),
  495. DECL(AL_DOPPLER_VELOCITY),
  496. DECL(AL_DISTANCE_MODEL),
  497. DECL(AL_SPEED_OF_SOUND),
  498. DECL(AL_SOURCE_DISTANCE_MODEL),
  499. DECL(AL_DEFERRED_UPDATES_SOFT),
  500. DECL(AL_INVERSE_DISTANCE),
  501. DECL(AL_INVERSE_DISTANCE_CLAMPED),
  502. DECL(AL_LINEAR_DISTANCE),
  503. DECL(AL_LINEAR_DISTANCE_CLAMPED),
  504. DECL(AL_EXPONENT_DISTANCE),
  505. DECL(AL_EXPONENT_DISTANCE_CLAMPED),
  506. DECL(AL_FILTER_TYPE),
  507. DECL(AL_FILTER_NULL),
  508. DECL(AL_FILTER_LOWPASS),
  509. #if 0
  510. DECL(AL_FILTER_HIGHPASS),
  511. DECL(AL_FILTER_BANDPASS),
  512. #endif
  513. DECL(AL_LOWPASS_GAIN),
  514. DECL(AL_LOWPASS_GAINHF),
  515. DECL(AL_EFFECT_TYPE),
  516. DECL(AL_EFFECT_NULL),
  517. DECL(AL_EFFECT_REVERB),
  518. DECL(AL_EFFECT_EAXREVERB),
  519. DECL(AL_EFFECT_CHORUS),
  520. DECL(AL_EFFECT_DISTORTION),
  521. DECL(AL_EFFECT_ECHO),
  522. DECL(AL_EFFECT_FLANGER),
  523. #if 0
  524. DECL(AL_EFFECT_FREQUENCY_SHIFTER),
  525. DECL(AL_EFFECT_VOCAL_MORPHER),
  526. DECL(AL_EFFECT_PITCH_SHIFTER),
  527. #endif
  528. DECL(AL_EFFECT_RING_MODULATOR),
  529. DECL(AL_EFFECT_AUTOWAH),
  530. DECL(AL_EFFECT_COMPRESSOR),
  531. DECL(AL_EFFECT_EQUALIZER),
  532. DECL(AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT),
  533. DECL(AL_EFFECT_DEDICATED_DIALOGUE),
  534. DECL(AL_EAXREVERB_DENSITY),
  535. DECL(AL_EAXREVERB_DIFFUSION),
  536. DECL(AL_EAXREVERB_GAIN),
  537. DECL(AL_EAXREVERB_GAINHF),
  538. DECL(AL_EAXREVERB_GAINLF),
  539. DECL(AL_EAXREVERB_DECAY_TIME),
  540. DECL(AL_EAXREVERB_DECAY_HFRATIO),
  541. DECL(AL_EAXREVERB_DECAY_LFRATIO),
  542. DECL(AL_EAXREVERB_REFLECTIONS_GAIN),
  543. DECL(AL_EAXREVERB_REFLECTIONS_DELAY),
  544. DECL(AL_EAXREVERB_REFLECTIONS_PAN),
  545. DECL(AL_EAXREVERB_LATE_REVERB_GAIN),
  546. DECL(AL_EAXREVERB_LATE_REVERB_DELAY),
  547. DECL(AL_EAXREVERB_LATE_REVERB_PAN),
  548. DECL(AL_EAXREVERB_ECHO_TIME),
  549. DECL(AL_EAXREVERB_ECHO_DEPTH),
  550. DECL(AL_EAXREVERB_MODULATION_TIME),
  551. DECL(AL_EAXREVERB_MODULATION_DEPTH),
  552. DECL(AL_EAXREVERB_AIR_ABSORPTION_GAINHF),
  553. DECL(AL_EAXREVERB_HFREFERENCE),
  554. DECL(AL_EAXREVERB_LFREFERENCE),
  555. DECL(AL_EAXREVERB_ROOM_ROLLOFF_FACTOR),
  556. DECL(AL_EAXREVERB_DECAY_HFLIMIT),
  557. DECL(AL_REVERB_DENSITY),
  558. DECL(AL_REVERB_DIFFUSION),
  559. DECL(AL_REVERB_GAIN),
  560. DECL(AL_REVERB_GAINHF),
  561. DECL(AL_REVERB_DECAY_TIME),
  562. DECL(AL_REVERB_DECAY_HFRATIO),
  563. DECL(AL_REVERB_REFLECTIONS_GAIN),
  564. DECL(AL_REVERB_REFLECTIONS_DELAY),
  565. DECL(AL_REVERB_LATE_REVERB_GAIN),
  566. DECL(AL_REVERB_LATE_REVERB_DELAY),
  567. DECL(AL_REVERB_AIR_ABSORPTION_GAINHF),
  568. DECL(AL_REVERB_ROOM_ROLLOFF_FACTOR),
  569. DECL(AL_REVERB_DECAY_HFLIMIT),
  570. DECL(AL_CHORUS_WAVEFORM),
  571. DECL(AL_CHORUS_PHASE),
  572. DECL(AL_CHORUS_RATE),
  573. DECL(AL_CHORUS_DEPTH),
  574. DECL(AL_CHORUS_FEEDBACK),
  575. DECL(AL_CHORUS_DELAY),
  576. DECL(AL_DISTORTION_EDGE),
  577. DECL(AL_DISTORTION_GAIN),
  578. DECL(AL_DISTORTION_LOWPASS_CUTOFF),
  579. DECL(AL_DISTORTION_EQCENTER),
  580. DECL(AL_DISTORTION_EQBANDWIDTH),
  581. DECL(AL_ECHO_DELAY),
  582. DECL(AL_ECHO_LRDELAY),
  583. DECL(AL_ECHO_DAMPING),
  584. DECL(AL_ECHO_FEEDBACK),
  585. DECL(AL_ECHO_SPREAD),
  586. DECL(AL_FLANGER_WAVEFORM),
  587. DECL(AL_FLANGER_PHASE),
  588. DECL(AL_FLANGER_RATE),
  589. DECL(AL_FLANGER_DEPTH),
  590. DECL(AL_FLANGER_FEEDBACK),
  591. DECL(AL_FLANGER_DELAY),
  592. DECL(AL_RING_MODULATOR_FREQUENCY),
  593. DECL(AL_RING_MODULATOR_HIGHPASS_CUTOFF),
  594. DECL(AL_RING_MODULATOR_WAVEFORM),
  595. DECL(AL_AUTOWAH_ATTACK_TIME),
  596. DECL(AL_AUTOWAH_PEAK_GAIN),
  597. DECL(AL_AUTOWAH_RELEASE_TIME),
  598. DECL(AL_AUTOWAH_RESONANCE),
  599. DECL(AL_COMPRESSOR_ONOFF),
  600. DECL(AL_EQUALIZER_LOW_GAIN),
  601. DECL(AL_EQUALIZER_LOW_CUTOFF),
  602. DECL(AL_EQUALIZER_MID1_GAIN),
  603. DECL(AL_EQUALIZER_MID1_CENTER),
  604. DECL(AL_EQUALIZER_MID1_WIDTH),
  605. DECL(AL_EQUALIZER_MID2_GAIN),
  606. DECL(AL_EQUALIZER_MID2_CENTER),
  607. DECL(AL_EQUALIZER_MID2_WIDTH),
  608. DECL(AL_EQUALIZER_HIGH_GAIN),
  609. DECL(AL_EQUALIZER_HIGH_CUTOFF),
  610. DECL(AL_DEDICATED_GAIN),
  611. { NULL, (ALCenum)0 }
  612. };
  613. #undef DECL
  614. static const ALCchar alcNoError[] = "No Error";
  615. static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
  616. static const ALCchar alcErrInvalidContext[] = "Invalid Context";
  617. static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
  618. static const ALCchar alcErrInvalidValue[] = "Invalid Value";
  619. static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
  620. /************************************************
  621. * Global variables
  622. ************************************************/
  623. /* Enumerated device names */
  624. static const ALCchar alcDefaultName[] = "OpenAL Soft\0";
  625. static ALCchar *alcAllDevicesList;
  626. static ALCchar *alcCaptureDeviceList;
  627. /* Sizes only include the first ending null character, not the second */
  628. static size_t alcAllDevicesListSize;
  629. static size_t alcCaptureDeviceListSize;
  630. /* Default is always the first in the list */
  631. static ALCchar *alcDefaultAllDevicesSpecifier;
  632. static ALCchar *alcCaptureDefaultDeviceSpecifier;
  633. /* Default context extensions */
  634. static const ALchar alExtList[] =
  635. "AL_EXT_ALAW AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 "
  636. "AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW "
  637. "AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model "
  638. "AL_LOKI_quadriphonic AL_SOFT_buffer_samples AL_SOFT_buffer_sub_data "
  639. "AL_SOFT_deferred_updates AL_SOFT_direct_channels AL_SOFT_loop_points "
  640. "AL_SOFT_source_latency";
  641. static volatile ALCenum LastNullDeviceError = ALC_NO_ERROR;
  642. /* Thread-local current context */
  643. static althread_key_t LocalContext;
  644. /* Process-wide current context */
  645. static ALCcontext *volatile GlobalContext = NULL;
  646. /* Mixing thread piority level */
  647. ALint RTPrioLevel;
  648. FILE *LogFile;
  649. #ifdef _DEBUG
  650. enum LogLevel LogLevel = LogWarning;
  651. #else
  652. enum LogLevel LogLevel = LogError;
  653. #endif
  654. /* Flag to trap ALC device errors */
  655. static ALCboolean TrapALCError = ALC_FALSE;
  656. /* One-time configuration init control */
  657. static althread_once_t alc_config_once = ALTHREAD_ONCE_INIT;
  658. /* Default effect that applies to sources that don't have an effect on send 0 */
  659. static ALeffect DefaultEffect;
  660. /************************************************
  661. * ALC information
  662. ************************************************/
  663. static const ALCchar alcNoDeviceExtList[] =
  664. "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
  665. "ALC_EXT_thread_local_context ALC_SOFT_loopback";
  666. static const ALCchar alcExtensionList[] =
  667. "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
  668. "ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX "
  669. "ALC_EXT_thread_local_context ALC_SOFTX_HRTF ALC_SOFT_loopback "
  670. "ALC_SOFTX_midi_interface ALC_SOFTX_pause_device";
  671. static const ALCint alcMajorVersion = 1;
  672. static const ALCint alcMinorVersion = 1;
  673. static const ALCint alcEFXMajorVersion = 1;
  674. static const ALCint alcEFXMinorVersion = 0;
  675. /************************************************
  676. * Device lists
  677. ************************************************/
  678. static ALCdevice *volatile DeviceList = NULL;
  679. static CRITICAL_SECTION ListLock;
  680. static void LockLists(void)
  681. {
  682. EnterCriticalSection(&ListLock);
  683. }
  684. static void UnlockLists(void)
  685. {
  686. LeaveCriticalSection(&ListLock);
  687. }
  688. /************************************************
  689. * Library initialization
  690. ************************************************/
  691. #if defined(_WIN32)
  692. static void alc_init(void);
  693. static void alc_deinit(void);
  694. static void alc_deinit_safe(void);
  695. UIntMap TlsDestructor;
  696. #ifndef AL_LIBTYPE_STATIC
  697. BOOL APIENTRY DllMain(HINSTANCE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
  698. {
  699. ALsizei i;
  700. // Perform actions based on the reason for calling.
  701. switch(ul_reason_for_call)
  702. {
  703. case DLL_PROCESS_ATTACH:
  704. /* Pin the DLL so we won't get unloaded until the process terminates */
  705. GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
  706. (WCHAR*)hModule, &hModule);
  707. InitUIntMap(&TlsDestructor, ~0);
  708. alc_init();
  709. break;
  710. case DLL_THREAD_DETACH:
  711. LockUIntMapRead(&TlsDestructor);
  712. for(i = 0;i < TlsDestructor.size;i++)
  713. {
  714. void *ptr = althread_getspecific(TlsDestructor.array[i].key);
  715. void (*callback)(void*) = (void(*)(void*))TlsDestructor.array[i].value;
  716. if(ptr && callback)
  717. callback(ptr);
  718. }
  719. UnlockUIntMapRead(&TlsDestructor);
  720. break;
  721. case DLL_PROCESS_DETACH:
  722. if(!lpReserved)
  723. alc_deinit();
  724. else
  725. alc_deinit_safe();
  726. ResetUIntMap(&TlsDestructor);
  727. break;
  728. }
  729. return TRUE;
  730. }
  731. #elif defined(_MSC_VER)
  732. #pragma section(".CRT$XCU",read)
  733. static void alc_constructor(void);
  734. static void alc_destructor(void);
  735. __declspec(allocate(".CRT$XCU")) void (__cdecl* alc_constructor_)(void) = alc_constructor;
  736. static void alc_constructor(void)
  737. {
  738. atexit(alc_destructor);
  739. alc_init();
  740. }
  741. static void alc_destructor(void)
  742. {
  743. alc_deinit();
  744. }
  745. #elif defined(HAVE_GCC_DESTRUCTOR)
  746. static void alc_init(void) __attribute__((constructor));
  747. static void alc_deinit(void) __attribute__((destructor));
  748. #else
  749. #error "No static initialization available on this platform!"
  750. #endif
  751. #elif defined(HAVE_GCC_DESTRUCTOR)
  752. static void alc_init(void) __attribute__((constructor));
  753. static void alc_deinit(void) __attribute__((destructor));
  754. #else
  755. #error "No global initialization available on this platform!"
  756. #endif
  757. static void ReleaseThreadCtx(void *ptr);
  758. static void alc_init(void)
  759. {
  760. const char *str;
  761. LogFile = stderr;
  762. str = getenv("__ALSOFT_HALF_ANGLE_CONES");
  763. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  764. ConeScale *= 0.5f;
  765. str = getenv("__ALSOFT_REVERSE_Z");
  766. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  767. ZScale *= -1.0f;
  768. althread_key_create(&LocalContext, ReleaseThreadCtx);
  769. InitializeCriticalSection(&ListLock);
  770. ThunkInit();
  771. }
  772. static void alc_initconfig(void)
  773. {
  774. const char *devs, *str;
  775. ALuint capfilter;
  776. float valf;
  777. int i, n;
  778. str = getenv("ALSOFT_LOGLEVEL");
  779. if(str)
  780. {
  781. long lvl = strtol(str, NULL, 0);
  782. if(lvl >= NoLog && lvl <= LogRef)
  783. LogLevel = lvl;
  784. }
  785. str = getenv("ALSOFT_LOGFILE");
  786. if(str && str[0])
  787. {
  788. FILE *logfile = fopen(str, "wt");
  789. if(logfile) LogFile = logfile;
  790. else ERR("Failed to open log file '%s'\n", str);
  791. }
  792. {
  793. char buf[1024] = "";
  794. int len = snprintf(buf, sizeof(buf), "%s", BackendList[0].name);
  795. for(i = 1;BackendList[i].name;i++)
  796. len += snprintf(buf+len, sizeof(buf)-len, ", %s", BackendList[i].name);
  797. TRACE("Supported backends: %s\n", buf);
  798. }
  799. ReadALConfig();
  800. capfilter = 0;
  801. #ifdef HAVE_SSE
  802. capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2;
  803. #endif
  804. #ifdef HAVE_NEON
  805. capfilter |= CPU_CAP_NEON;
  806. #endif
  807. if(ConfigValueStr(NULL, "disable-cpu-exts", &str))
  808. {
  809. if(strcasecmp(str, "all") == 0)
  810. capfilter = 0;
  811. else
  812. {
  813. size_t len;
  814. const char *next = str;
  815. do {
  816. str = next;
  817. while(isspace(str[0]))
  818. str++;
  819. next = strchr(str, ',');
  820. if(!str[0] || str[0] == ',')
  821. continue;
  822. len = (next ? ((size_t)(next-str)) : strlen(str));
  823. while(len > 0 && isspace(str[len-1]))
  824. len--;
  825. if(len == 3 && strncasecmp(str, "sse", len) == 0)
  826. capfilter &= ~CPU_CAP_SSE;
  827. else if(len == 4 && strncasecmp(str, "sse2", len) == 0)
  828. capfilter &= ~CPU_CAP_SSE2;
  829. else if(len == 4 && strncasecmp(str, "neon", len) == 0)
  830. capfilter &= ~CPU_CAP_NEON;
  831. else
  832. WARN("Invalid CPU extension \"%s\"\n", str);
  833. } while(next++);
  834. }
  835. }
  836. FillCPUCaps(capfilter);
  837. #ifdef _WIN32
  838. RTPrioLevel = 1;
  839. #else
  840. RTPrioLevel = 0;
  841. #endif
  842. ConfigValueInt(NULL, "rt-prio", &RTPrioLevel);
  843. if(ConfigValueStr(NULL, "resampler", &str))
  844. {
  845. if(strcasecmp(str, "point") == 0 || strcasecmp(str, "none") == 0)
  846. DefaultResampler = PointResampler;
  847. else if(strcasecmp(str, "linear") == 0)
  848. DefaultResampler = LinearResampler;
  849. else if(strcasecmp(str, "cubic") == 0)
  850. DefaultResampler = CubicResampler;
  851. else
  852. {
  853. char *end;
  854. n = strtol(str, &end, 0);
  855. if(*end == '\0' && (n == PointResampler || n == LinearResampler || n == CubicResampler))
  856. DefaultResampler = n;
  857. else
  858. WARN("Invalid resampler: %s\n", str);
  859. }
  860. }
  861. str = getenv("ALSOFT_TRAP_ERROR");
  862. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  863. {
  864. TrapALError = AL_TRUE;
  865. TrapALCError = AL_TRUE;
  866. }
  867. else
  868. {
  869. str = getenv("ALSOFT_TRAP_AL_ERROR");
  870. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  871. TrapALError = AL_TRUE;
  872. TrapALError = GetConfigValueBool(NULL, "trap-al-error", TrapALError);
  873. str = getenv("ALSOFT_TRAP_ALC_ERROR");
  874. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  875. TrapALCError = ALC_TRUE;
  876. TrapALCError = GetConfigValueBool(NULL, "trap-alc-error", TrapALCError);
  877. }
  878. if(ConfigValueFloat("reverb", "boost", &valf))
  879. ReverbBoost *= powf(10.0f, valf / 20.0f);
  880. EmulateEAXReverb = GetConfigValueBool("reverb", "emulate-eax", AL_FALSE);
  881. if(((devs=getenv("ALSOFT_DRIVERS")) && devs[0]) ||
  882. ConfigValueStr(NULL, "drivers", &devs))
  883. {
  884. int n;
  885. size_t len;
  886. const char *next = devs;
  887. int endlist, delitem;
  888. i = 0;
  889. do {
  890. devs = next;
  891. while(isspace(devs[0]))
  892. devs++;
  893. next = strchr(devs, ',');
  894. delitem = (devs[0] == '-');
  895. if(devs[0] == '-') devs++;
  896. if(!devs[0] || devs[0] == ',')
  897. {
  898. endlist = 0;
  899. continue;
  900. }
  901. endlist = 1;
  902. len = (next ? ((size_t)(next-devs)) : strlen(devs));
  903. while(len > 0 && isspace(devs[len-1]))
  904. len--;
  905. for(n = i;BackendList[n].name;n++)
  906. {
  907. if(len == strlen(BackendList[n].name) &&
  908. strncmp(BackendList[n].name, devs, len) == 0)
  909. {
  910. if(delitem)
  911. {
  912. do {
  913. BackendList[n] = BackendList[n+1];
  914. ++n;
  915. } while(BackendList[n].name);
  916. }
  917. else
  918. {
  919. struct BackendInfo Bkp = BackendList[n];
  920. while(n > i)
  921. {
  922. BackendList[n] = BackendList[n-1];
  923. --n;
  924. }
  925. BackendList[n] = Bkp;
  926. i++;
  927. }
  928. break;
  929. }
  930. }
  931. } while(next++);
  932. if(endlist)
  933. {
  934. BackendList[i].name = NULL;
  935. BackendList[i].getFactory = NULL;
  936. BackendList[i].Init = NULL;
  937. BackendList[i].Deinit = NULL;
  938. BackendList[i].Probe = NULL;
  939. }
  940. }
  941. for(i = 0;(BackendList[i].Init || BackendList[i].getFactory) && (!PlaybackBackend.name || !CaptureBackend.name);i++)
  942. {
  943. if(BackendList[i].getFactory)
  944. {
  945. ALCbackendFactory *factory = BackendList[i].getFactory();
  946. if(!V0(factory,init)())
  947. {
  948. WARN("Failed to initialize backend \"%s\"\n", BackendList[i].name);
  949. continue;
  950. }
  951. TRACE("Initialized backend \"%s\"\n", BackendList[i].name);
  952. if(!PlaybackBackend.name && V(factory,querySupport)(ALCbackend_Playback))
  953. {
  954. PlaybackBackend = BackendList[i];
  955. TRACE("Added \"%s\" for playback\n", PlaybackBackend.name);
  956. }
  957. if(!CaptureBackend.name && V(factory,querySupport)(ALCbackend_Capture))
  958. {
  959. CaptureBackend = BackendList[i];
  960. TRACE("Added \"%s\" for capture\n", CaptureBackend.name);
  961. }
  962. continue;
  963. }
  964. if(!BackendList[i].Init(&BackendList[i].Funcs))
  965. {
  966. WARN("Failed to initialize backend \"%s\"\n", BackendList[i].name);
  967. continue;
  968. }
  969. TRACE("Initialized backend \"%s\"\n", BackendList[i].name);
  970. if(BackendList[i].Funcs.OpenPlayback && !PlaybackBackend.name)
  971. {
  972. PlaybackBackend = BackendList[i];
  973. TRACE("Added \"%s\" for playback\n", PlaybackBackend.name);
  974. }
  975. if(BackendList[i].Funcs.OpenCapture && !CaptureBackend.name)
  976. {
  977. CaptureBackend = BackendList[i];
  978. TRACE("Added \"%s\" for capture\n", CaptureBackend.name);
  979. }
  980. }
  981. {
  982. ALCbackendFactory *factory = ALCloopbackFactory_getFactory();
  983. V0(factory,init)();
  984. }
  985. if(ConfigValueStr(NULL, "excludefx", &str))
  986. {
  987. size_t len;
  988. const char *next = str;
  989. do {
  990. str = next;
  991. next = strchr(str, ',');
  992. if(!str[0] || next == str)
  993. continue;
  994. len = (next ? ((size_t)(next-str)) : strlen(str));
  995. for(n = 0;EffectList[n].name;n++)
  996. {
  997. if(len == strlen(EffectList[n].name) &&
  998. strncmp(EffectList[n].name, str, len) == 0)
  999. DisabledEffects[EffectList[n].type] = AL_TRUE;
  1000. }
  1001. } while(next++);
  1002. }
  1003. InitEffectFactoryMap();
  1004. InitEffect(&DefaultEffect);
  1005. str = getenv("ALSOFT_DEFAULT_REVERB");
  1006. if((str && str[0]) || ConfigValueStr(NULL, "default-reverb", &str))
  1007. LoadReverbPreset(str, &DefaultEffect);
  1008. }
  1009. #define DO_INITCONFIG() althread_once(&alc_config_once, alc_initconfig)
  1010. /************************************************
  1011. * Library deinitialization
  1012. ************************************************/
  1013. static void alc_cleanup(void)
  1014. {
  1015. ALCdevice *dev;
  1016. free(alcAllDevicesList); alcAllDevicesList = NULL;
  1017. alcAllDevicesListSize = 0;
  1018. free(alcCaptureDeviceList); alcCaptureDeviceList = NULL;
  1019. alcCaptureDeviceListSize = 0;
  1020. free(alcDefaultAllDevicesSpecifier);
  1021. alcDefaultAllDevicesSpecifier = NULL;
  1022. free(alcCaptureDefaultDeviceSpecifier);
  1023. alcCaptureDefaultDeviceSpecifier = NULL;
  1024. if((dev=ExchangePtr((XchgPtr*)&DeviceList, NULL)) != NULL)
  1025. {
  1026. ALCuint num = 0;
  1027. do {
  1028. num++;
  1029. } while((dev=dev->next) != NULL);
  1030. ERR("%u device%s not closed\n", num, (num>1)?"s":"");
  1031. }
  1032. DeinitEffectFactoryMap();
  1033. }
  1034. static void alc_deinit_safe(void)
  1035. {
  1036. alc_cleanup();
  1037. FreeHrtfs();
  1038. FreeALConfig();
  1039. ThunkExit();
  1040. DeleteCriticalSection(&ListLock);
  1041. althread_key_delete(LocalContext);
  1042. if(LogFile != stderr)
  1043. fclose(LogFile);
  1044. LogFile = NULL;
  1045. }
  1046. static void alc_deinit(void)
  1047. {
  1048. int i;
  1049. alc_cleanup();
  1050. memset(&PlaybackBackend, 0, sizeof(PlaybackBackend));
  1051. memset(&CaptureBackend, 0, sizeof(CaptureBackend));
  1052. for(i = 0;BackendList[i].Deinit || BackendList[i].getFactory;i++)
  1053. {
  1054. if(!BackendList[i].getFactory)
  1055. BackendList[i].Deinit();
  1056. else
  1057. {
  1058. ALCbackendFactory *factory = BackendList[i].getFactory();
  1059. V0(factory,deinit)();
  1060. }
  1061. }
  1062. {
  1063. ALCbackendFactory *factory = ALCloopbackFactory_getFactory();
  1064. V0(factory,deinit)();
  1065. }
  1066. alc_deinit_safe();
  1067. }
  1068. /************************************************
  1069. * Device enumeration
  1070. ************************************************/
  1071. static void ProbeList(ALCchar **list, size_t *listsize, enum DevProbe type)
  1072. {
  1073. DO_INITCONFIG();
  1074. LockLists();
  1075. free(*list);
  1076. *list = NULL;
  1077. *listsize = 0;
  1078. if(type == ALL_DEVICE_PROBE && (PlaybackBackend.Probe || PlaybackBackend.getFactory))
  1079. {
  1080. if(!PlaybackBackend.getFactory)
  1081. PlaybackBackend.Probe(type);
  1082. else
  1083. {
  1084. ALCbackendFactory *factory = PlaybackBackend.getFactory();
  1085. V(factory,probe)(type);
  1086. }
  1087. }
  1088. else if(type == CAPTURE_DEVICE_PROBE && (CaptureBackend.Probe || CaptureBackend.getFactory))
  1089. {
  1090. if(!CaptureBackend.getFactory)
  1091. CaptureBackend.Probe(type);
  1092. else
  1093. {
  1094. ALCbackendFactory *factory = CaptureBackend.getFactory();
  1095. V(factory,probe)(type);
  1096. }
  1097. }
  1098. UnlockLists();
  1099. }
  1100. static void ProbeAllDevicesList(void)
  1101. { ProbeList(&alcAllDevicesList, &alcAllDevicesListSize, ALL_DEVICE_PROBE); }
  1102. static void ProbeCaptureDeviceList(void)
  1103. { ProbeList(&alcCaptureDeviceList, &alcCaptureDeviceListSize, CAPTURE_DEVICE_PROBE); }
  1104. static void AppendList(const ALCchar *name, ALCchar **List, size_t *ListSize)
  1105. {
  1106. size_t len = strlen(name);
  1107. void *temp;
  1108. if(len == 0)
  1109. return;
  1110. temp = realloc(*List, (*ListSize) + len + 2);
  1111. if(!temp)
  1112. {
  1113. ERR("Realloc failed to add %s!\n", name);
  1114. return;
  1115. }
  1116. *List = temp;
  1117. memcpy((*List)+(*ListSize), name, len+1);
  1118. *ListSize += len+1;
  1119. (*List)[*ListSize] = 0;
  1120. }
  1121. #define DECL_APPEND_LIST_FUNC(type) \
  1122. void Append##type##List(const ALCchar *name) \
  1123. { AppendList(name, &alc##type##List, &alc##type##ListSize); }
  1124. DECL_APPEND_LIST_FUNC(AllDevices)
  1125. DECL_APPEND_LIST_FUNC(CaptureDevice)
  1126. #undef DECL_APPEND_LIST_FUNC
  1127. /************************************************
  1128. * Device format information
  1129. ************************************************/
  1130. const ALCchar *DevFmtTypeString(enum DevFmtType type)
  1131. {
  1132. switch(type)
  1133. {
  1134. case DevFmtByte: return "Signed Byte";
  1135. case DevFmtUByte: return "Unsigned Byte";
  1136. case DevFmtShort: return "Signed Short";
  1137. case DevFmtUShort: return "Unsigned Short";
  1138. case DevFmtInt: return "Signed Int";
  1139. case DevFmtUInt: return "Unsigned Int";
  1140. case DevFmtFloat: return "Float";
  1141. }
  1142. return "(unknown type)";
  1143. }
  1144. const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans)
  1145. {
  1146. switch(chans)
  1147. {
  1148. case DevFmtMono: return "Mono";
  1149. case DevFmtStereo: return "Stereo";
  1150. case DevFmtQuad: return "Quadraphonic";
  1151. case DevFmtX51: return "5.1 Surround";
  1152. case DevFmtX51Side: return "5.1 Side";
  1153. case DevFmtX61: return "6.1 Surround";
  1154. case DevFmtX71: return "7.1 Surround";
  1155. }
  1156. return "(unknown channels)";
  1157. }
  1158. extern inline ALuint FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type);
  1159. ALuint BytesFromDevFmt(enum DevFmtType type)
  1160. {
  1161. switch(type)
  1162. {
  1163. case DevFmtByte: return sizeof(ALbyte);
  1164. case DevFmtUByte: return sizeof(ALubyte);
  1165. case DevFmtShort: return sizeof(ALshort);
  1166. case DevFmtUShort: return sizeof(ALushort);
  1167. case DevFmtInt: return sizeof(ALint);
  1168. case DevFmtUInt: return sizeof(ALuint);
  1169. case DevFmtFloat: return sizeof(ALfloat);
  1170. }
  1171. return 0;
  1172. }
  1173. ALuint ChannelsFromDevFmt(enum DevFmtChannels chans)
  1174. {
  1175. switch(chans)
  1176. {
  1177. case DevFmtMono: return 1;
  1178. case DevFmtStereo: return 2;
  1179. case DevFmtQuad: return 4;
  1180. case DevFmtX51: return 6;
  1181. case DevFmtX51Side: return 6;
  1182. case DevFmtX61: return 7;
  1183. case DevFmtX71: return 8;
  1184. }
  1185. return 0;
  1186. }
  1187. static ALboolean DecomposeDevFormat(ALenum format, enum DevFmtChannels *chans,
  1188. enum DevFmtType *type)
  1189. {
  1190. static const struct {
  1191. ALenum format;
  1192. enum DevFmtChannels channels;
  1193. enum DevFmtType type;
  1194. } list[] = {
  1195. { AL_FORMAT_MONO8, DevFmtMono, DevFmtUByte },
  1196. { AL_FORMAT_MONO16, DevFmtMono, DevFmtShort },
  1197. { AL_FORMAT_MONO_FLOAT32, DevFmtMono, DevFmtFloat },
  1198. { AL_FORMAT_STEREO8, DevFmtStereo, DevFmtUByte },
  1199. { AL_FORMAT_STEREO16, DevFmtStereo, DevFmtShort },
  1200. { AL_FORMAT_STEREO_FLOAT32, DevFmtStereo, DevFmtFloat },
  1201. { AL_FORMAT_QUAD8, DevFmtQuad, DevFmtUByte },
  1202. { AL_FORMAT_QUAD16, DevFmtQuad, DevFmtShort },
  1203. { AL_FORMAT_QUAD32, DevFmtQuad, DevFmtFloat },
  1204. { AL_FORMAT_51CHN8, DevFmtX51, DevFmtUByte },
  1205. { AL_FORMAT_51CHN16, DevFmtX51, DevFmtShort },
  1206. { AL_FORMAT_51CHN32, DevFmtX51, DevFmtFloat },
  1207. { AL_FORMAT_61CHN8, DevFmtX61, DevFmtUByte },
  1208. { AL_FORMAT_61CHN16, DevFmtX61, DevFmtShort },
  1209. { AL_FORMAT_61CHN32, DevFmtX61, DevFmtFloat },
  1210. { AL_FORMAT_71CHN8, DevFmtX71, DevFmtUByte },
  1211. { AL_FORMAT_71CHN16, DevFmtX71, DevFmtShort },
  1212. { AL_FORMAT_71CHN32, DevFmtX71, DevFmtFloat },
  1213. };
  1214. ALuint i;
  1215. for(i = 0;i < COUNTOF(list);i++)
  1216. {
  1217. if(list[i].format == format)
  1218. {
  1219. *chans = list[i].channels;
  1220. *type = list[i].type;
  1221. return AL_TRUE;
  1222. }
  1223. }
  1224. return AL_FALSE;
  1225. }
  1226. static ALCboolean IsValidALCType(ALCenum type)
  1227. {
  1228. switch(type)
  1229. {
  1230. case ALC_BYTE_SOFT:
  1231. case ALC_UNSIGNED_BYTE_SOFT:
  1232. case ALC_SHORT_SOFT:
  1233. case ALC_UNSIGNED_SHORT_SOFT:
  1234. case ALC_INT_SOFT:
  1235. case ALC_UNSIGNED_INT_SOFT:
  1236. case ALC_FLOAT_SOFT:
  1237. return ALC_TRUE;
  1238. }
  1239. return ALC_FALSE;
  1240. }
  1241. static ALCboolean IsValidALCChannels(ALCenum channels)
  1242. {
  1243. switch(channels)
  1244. {
  1245. case ALC_MONO_SOFT:
  1246. case ALC_STEREO_SOFT:
  1247. case ALC_QUAD_SOFT:
  1248. case ALC_5POINT1_SOFT:
  1249. case ALC_6POINT1_SOFT:
  1250. case ALC_7POINT1_SOFT:
  1251. return ALC_TRUE;
  1252. }
  1253. return ALC_FALSE;
  1254. }
  1255. /************************************************
  1256. * Miscellaneous ALC helpers
  1257. ************************************************/
  1258. extern inline void LockContext(ALCcontext *context);
  1259. extern inline void UnlockContext(ALCcontext *context);
  1260. ALint64 ALCdevice_GetLatencyDefault(ALCdevice *UNUSED(device))
  1261. {
  1262. return 0;
  1263. }
  1264. ALint64 ALCdevice_GetLatency(ALCdevice *device)
  1265. {
  1266. return V0(device->Backend,getLatency)();
  1267. }
  1268. void ALCdevice_Lock(ALCdevice *device)
  1269. {
  1270. V0(device->Backend,lock)();
  1271. }
  1272. void ALCdevice_Unlock(ALCdevice *device)
  1273. {
  1274. V0(device->Backend,unlock)();
  1275. }
  1276. /* SetDefaultWFXChannelOrder
  1277. *
  1278. * Sets the default channel order used by WaveFormatEx.
  1279. */
  1280. void SetDefaultWFXChannelOrder(ALCdevice *device)
  1281. {
  1282. ALuint i;
  1283. for(i = 0;i < MaxChannels;i++)
  1284. device->ChannelOffsets[i] = INVALID_OFFSET;
  1285. switch(device->FmtChans)
  1286. {
  1287. case DevFmtMono: device->ChannelOffsets[FrontCenter] = 0;
  1288. break;
  1289. case DevFmtStereo: device->ChannelOffsets[FrontLeft] = 0;
  1290. device->ChannelOffsets[FrontRight] = 1;
  1291. break;
  1292. case DevFmtQuad: device->ChannelOffsets[FrontLeft] = 0;
  1293. device->ChannelOffsets[FrontRight] = 1;
  1294. device->ChannelOffsets[BackLeft] = 2;
  1295. device->ChannelOffsets[BackRight] = 3;
  1296. break;
  1297. case DevFmtX51: device->ChannelOffsets[FrontLeft] = 0;
  1298. device->ChannelOffsets[FrontRight] = 1;
  1299. device->ChannelOffsets[FrontCenter] = 2;
  1300. device->ChannelOffsets[LFE] = 3;
  1301. device->ChannelOffsets[BackLeft] = 4;
  1302. device->ChannelOffsets[BackRight] = 5;
  1303. break;
  1304. case DevFmtX51Side: device->ChannelOffsets[FrontLeft] = 0;
  1305. device->ChannelOffsets[FrontRight] = 1;
  1306. device->ChannelOffsets[FrontCenter] = 2;
  1307. device->ChannelOffsets[LFE] = 3;
  1308. device->ChannelOffsets[SideLeft] = 4;
  1309. device->ChannelOffsets[SideRight] = 5;
  1310. break;
  1311. case DevFmtX61: device->ChannelOffsets[FrontLeft] = 0;
  1312. device->ChannelOffsets[FrontRight] = 1;
  1313. device->ChannelOffsets[FrontCenter] = 2;
  1314. device->ChannelOffsets[LFE] = 3;
  1315. device->ChannelOffsets[BackCenter] = 4;
  1316. device->ChannelOffsets[SideLeft] = 5;
  1317. device->ChannelOffsets[SideRight] = 6;
  1318. break;
  1319. case DevFmtX71: device->ChannelOffsets[FrontLeft] = 0;
  1320. device->ChannelOffsets[FrontRight] = 1;
  1321. device->ChannelOffsets[FrontCenter] = 2;
  1322. device->ChannelOffsets[LFE] = 3;
  1323. device->ChannelOffsets[BackLeft] = 4;
  1324. device->ChannelOffsets[BackRight] = 5;
  1325. device->ChannelOffsets[SideLeft] = 6;
  1326. device->ChannelOffsets[SideRight] = 7;
  1327. break;
  1328. }
  1329. }
  1330. /* SetDefaultChannelOrder
  1331. *
  1332. * Sets the default channel order used by most non-WaveFormatEx-based APIs.
  1333. */
  1334. void SetDefaultChannelOrder(ALCdevice *device)
  1335. {
  1336. ALuint i;
  1337. for(i = 0;i < MaxChannels;i++)
  1338. device->ChannelOffsets[i] = INVALID_OFFSET;
  1339. switch(device->FmtChans)
  1340. {
  1341. case DevFmtX51: device->ChannelOffsets[FrontLeft] = 0;
  1342. device->ChannelOffsets[FrontRight] = 1;
  1343. device->ChannelOffsets[BackLeft] = 2;
  1344. device->ChannelOffsets[BackRight] = 3;
  1345. device->ChannelOffsets[FrontCenter] = 4;
  1346. device->ChannelOffsets[LFE] = 5;
  1347. return;
  1348. case DevFmtX71: device->ChannelOffsets[FrontLeft] = 0;
  1349. device->ChannelOffsets[FrontRight] = 1;
  1350. device->ChannelOffsets[BackLeft] = 2;
  1351. device->ChannelOffsets[BackRight] = 3;
  1352. device->ChannelOffsets[FrontCenter] = 4;
  1353. device->ChannelOffsets[LFE] = 5;
  1354. device->ChannelOffsets[SideLeft] = 6;
  1355. device->ChannelOffsets[SideRight] = 7;
  1356. return;
  1357. /* Same as WFX order */
  1358. case DevFmtMono:
  1359. case DevFmtStereo:
  1360. case DevFmtQuad:
  1361. case DevFmtX51Side:
  1362. case DevFmtX61:
  1363. break;
  1364. }
  1365. SetDefaultWFXChannelOrder(device);
  1366. }
  1367. /* alcSetError
  1368. *
  1369. * Stores the latest ALC device error
  1370. */
  1371. static void alcSetError(ALCdevice *device, ALCenum errorCode)
  1372. {
  1373. if(TrapALCError)
  1374. {
  1375. #ifdef _WIN32
  1376. /* DebugBreak() will cause an exception if there is no debugger */
  1377. if(IsDebuggerPresent())
  1378. DebugBreak();
  1379. #elif defined(SIGTRAP)
  1380. raise(SIGTRAP);
  1381. #endif
  1382. }
  1383. if(device)
  1384. device->LastError = errorCode;
  1385. else
  1386. LastNullDeviceError = errorCode;
  1387. }
  1388. /* UpdateDeviceParams
  1389. *
  1390. * Updates device parameters according to the attribute list (caller is
  1391. * responsible for holding the list lock).
  1392. */
  1393. static ALCenum UpdateDeviceParams(ALCdevice *device, const ALCint *attrList)
  1394. {
  1395. ALCcontext *context;
  1396. enum DevFmtChannels oldChans;
  1397. enum DevFmtType oldType;
  1398. ALCuint oldFreq;
  1399. FPUCtl oldMode;
  1400. ALuint i;
  1401. // Check for attributes
  1402. if(device->Type == Loopback)
  1403. {
  1404. enum {
  1405. GotFreq = 1<<0,
  1406. GotChans = 1<<1,
  1407. GotType = 1<<2,
  1408. GotAll = GotFreq|GotChans|GotType
  1409. };
  1410. ALCuint freq, numMono, numStereo, numSends;
  1411. enum DevFmtChannels schans;
  1412. enum DevFmtType stype;
  1413. ALCuint attrIdx = 0;
  1414. ALCint gotFmt = 0;
  1415. if(!attrList)
  1416. {
  1417. WARN("Missing attributes for loopback device\n");
  1418. return ALC_INVALID_VALUE;
  1419. }
  1420. numMono = device->NumMonoSources;
  1421. numStereo = device->NumStereoSources;
  1422. numSends = device->NumAuxSends;
  1423. schans = device->FmtChans;
  1424. stype = device->FmtType;
  1425. freq = device->Frequency;
  1426. while(attrList[attrIdx])
  1427. {
  1428. if(attrList[attrIdx] == ALC_FORMAT_CHANNELS_SOFT)
  1429. {
  1430. ALCint val = attrList[attrIdx + 1];
  1431. if(!IsValidALCChannels(val) || !ChannelsFromDevFmt(val))
  1432. return ALC_INVALID_VALUE;
  1433. schans = val;
  1434. gotFmt |= GotChans;
  1435. }
  1436. if(attrList[attrIdx] == ALC_FORMAT_TYPE_SOFT)
  1437. {
  1438. ALCint val = attrList[attrIdx + 1];
  1439. if(!IsValidALCType(val) || !BytesFromDevFmt(val))
  1440. return ALC_INVALID_VALUE;
  1441. stype = val;
  1442. gotFmt |= GotType;
  1443. }
  1444. if(attrList[attrIdx] == ALC_FREQUENCY)
  1445. {
  1446. freq = attrList[attrIdx + 1];
  1447. if(freq < MIN_OUTPUT_RATE)
  1448. return ALC_INVALID_VALUE;
  1449. gotFmt |= GotFreq;
  1450. }
  1451. if(attrList[attrIdx] == ALC_STEREO_SOURCES)
  1452. {
  1453. numStereo = attrList[attrIdx + 1];
  1454. if(numStereo > device->MaxNoOfSources)
  1455. numStereo = device->MaxNoOfSources;
  1456. numMono = device->MaxNoOfSources - numStereo;
  1457. }
  1458. if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
  1459. numSends = attrList[attrIdx + 1];
  1460. if(attrList[attrIdx] == ALC_HRTF_SOFT)
  1461. {
  1462. if(attrList[attrIdx + 1] != ALC_FALSE)
  1463. device->Flags |= DEVICE_HRTF_REQUEST;
  1464. else
  1465. device->Flags &= ~DEVICE_HRTF_REQUEST;
  1466. }
  1467. attrIdx += 2;
  1468. }
  1469. if(gotFmt != GotAll)
  1470. {
  1471. WARN("Missing format for loopback device\n");
  1472. return ALC_INVALID_VALUE;
  1473. }
  1474. ConfigValueUInt(NULL, "sends", &numSends);
  1475. numSends = minu(MAX_SENDS, numSends);
  1476. if((device->Flags&DEVICE_RUNNING))
  1477. V0(device->Backend,stop)();
  1478. device->Flags &= ~DEVICE_RUNNING;
  1479. device->Frequency = freq;
  1480. device->FmtChans = schans;
  1481. device->FmtType = stype;
  1482. device->NumMonoSources = numMono;
  1483. device->NumStereoSources = numStereo;
  1484. device->NumAuxSends = numSends;
  1485. }
  1486. else if(attrList && attrList[0])
  1487. {
  1488. ALCuint freq, numMono, numStereo, numSends;
  1489. ALCuint attrIdx = 0;
  1490. /* If a context is already running on the device, stop playback so the
  1491. * device attributes can be updated. */
  1492. if((device->Flags&DEVICE_RUNNING))
  1493. V0(device->Backend,stop)();
  1494. device->Flags &= ~DEVICE_RUNNING;
  1495. freq = device->Frequency;
  1496. numMono = device->NumMonoSources;
  1497. numStereo = device->NumStereoSources;
  1498. numSends = device->NumAuxSends;
  1499. while(attrList[attrIdx])
  1500. {
  1501. if(attrList[attrIdx] == ALC_FREQUENCY)
  1502. {
  1503. freq = attrList[attrIdx + 1];
  1504. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  1505. }
  1506. if(attrList[attrIdx] == ALC_STEREO_SOURCES)
  1507. {
  1508. numStereo = attrList[attrIdx + 1];
  1509. if(numStereo > device->MaxNoOfSources)
  1510. numStereo = device->MaxNoOfSources;
  1511. numMono = device->MaxNoOfSources - numStereo;
  1512. }
  1513. if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
  1514. numSends = attrList[attrIdx + 1];
  1515. if(attrList[attrIdx] == ALC_HRTF_SOFT)
  1516. {
  1517. if(attrList[attrIdx + 1] != ALC_FALSE)
  1518. device->Flags |= DEVICE_HRTF_REQUEST;
  1519. else
  1520. device->Flags &= ~DEVICE_HRTF_REQUEST;
  1521. }
  1522. attrIdx += 2;
  1523. }
  1524. ConfigValueUInt(NULL, "frequency", &freq);
  1525. freq = maxu(freq, MIN_OUTPUT_RATE);
  1526. ConfigValueUInt(NULL, "sends", &numSends);
  1527. numSends = minu(MAX_SENDS, numSends);
  1528. device->UpdateSize = (ALuint64)device->UpdateSize * freq /
  1529. device->Frequency;
  1530. /* SSE and Neon do best with the update size being a multiple of 4 */
  1531. if((CPUCapFlags&(CPU_CAP_SSE|CPU_CAP_NEON)) != 0)
  1532. device->UpdateSize = (device->UpdateSize+3)&~3;
  1533. device->Frequency = freq;
  1534. device->NumMonoSources = numMono;
  1535. device->NumStereoSources = numStereo;
  1536. device->NumAuxSends = numSends;
  1537. }
  1538. if((device->Flags&DEVICE_RUNNING))
  1539. return ALC_NO_ERROR;
  1540. oldFreq = device->Frequency;
  1541. oldChans = device->FmtChans;
  1542. oldType = device->FmtType;
  1543. TRACE("Pre-reset: %s%s, %s%s, %s%uhz, %u update size x%d\n",
  1544. (device->Flags&DEVICE_CHANNELS_REQUEST)?"*":"",
  1545. DevFmtChannelsString(device->FmtChans),
  1546. (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST)?"*":"",
  1547. DevFmtTypeString(device->FmtType),
  1548. (device->Flags&DEVICE_FREQUENCY_REQUEST)?"*":"",
  1549. device->Frequency,
  1550. device->UpdateSize, device->NumUpdates);
  1551. if((device->Flags&DEVICE_HRTF_REQUEST))
  1552. {
  1553. enum DevFmtChannels chans;
  1554. ALCuint freq;
  1555. FindHrtfFormat(device, &chans, &freq);
  1556. device->Frequency = freq;
  1557. device->FmtChans = chans;
  1558. device->Flags |= DEVICE_CHANNELS_REQUEST |
  1559. DEVICE_FREQUENCY_REQUEST;
  1560. }
  1561. if(V0(device->Backend,reset)() == ALC_FALSE)
  1562. return ALC_INVALID_DEVICE;
  1563. if(device->FmtChans != oldChans && (device->Flags&DEVICE_CHANNELS_REQUEST))
  1564. {
  1565. ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans),
  1566. DevFmtChannelsString(device->FmtChans));
  1567. device->Flags &= ~DEVICE_CHANNELS_REQUEST;
  1568. }
  1569. if(device->FmtType != oldType && (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
  1570. {
  1571. ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType),
  1572. DevFmtTypeString(device->FmtType));
  1573. device->Flags &= ~DEVICE_SAMPLE_TYPE_REQUEST;
  1574. }
  1575. if(device->Frequency != oldFreq && (device->Flags&DEVICE_FREQUENCY_REQUEST))
  1576. {
  1577. ERR("Failed to set %uhz, got %uhz instead\n", oldFreq, device->Frequency);
  1578. device->Flags &= ~DEVICE_FREQUENCY_REQUEST;
  1579. }
  1580. TRACE("Post-reset: %s, %s, %uhz, %u update size x%d\n",
  1581. DevFmtChannelsString(device->FmtChans),
  1582. DevFmtTypeString(device->FmtType), device->Frequency,
  1583. device->UpdateSize, device->NumUpdates);
  1584. aluInitPanning(device);
  1585. for(i = 0;i < MaxChannels;i++)
  1586. {
  1587. device->ClickRemoval[i] = 0.0f;
  1588. device->PendingClicks[i] = 0.0f;
  1589. }
  1590. V(device->Synth,update)(device);
  1591. device->Hrtf = NULL;
  1592. if(device->Type != Loopback && ConfigValueExists(NULL, "hrtf"))
  1593. {
  1594. if(GetConfigValueBool(NULL, "hrtf", AL_FALSE))
  1595. device->Flags |= DEVICE_HRTF_REQUEST;
  1596. else
  1597. device->Flags &= ~DEVICE_HRTF_REQUEST;
  1598. }
  1599. if((device->Flags&DEVICE_HRTF_REQUEST))
  1600. {
  1601. device->Hrtf = GetHrtf(device);
  1602. if(!device->Hrtf)
  1603. device->Flags &= ~DEVICE_HRTF_REQUEST;
  1604. }
  1605. TRACE("HRTF %s\n", device->Hrtf?"enabled":"disabled");
  1606. if(!device->Hrtf && device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
  1607. {
  1608. if(!device->Bs2b)
  1609. {
  1610. device->Bs2b = calloc(1, sizeof(*device->Bs2b));
  1611. bs2b_clear(device->Bs2b);
  1612. }
  1613. bs2b_set_srate(device->Bs2b, device->Frequency);
  1614. bs2b_set_level(device->Bs2b, device->Bs2bLevel);
  1615. TRACE("BS2B level %d\n", device->Bs2bLevel);
  1616. }
  1617. else
  1618. {
  1619. free(device->Bs2b);
  1620. device->Bs2b = NULL;
  1621. TRACE("BS2B disabled\n");
  1622. }
  1623. device->Flags &= ~DEVICE_WIDE_STEREO;
  1624. if(device->Type != Loopback && !device->Hrtf && GetConfigValueBool(NULL, "wide-stereo", AL_FALSE))
  1625. device->Flags |= DEVICE_WIDE_STEREO;
  1626. if(!device->Hrtf && (device->UpdateSize&3))
  1627. {
  1628. if((CPUCapFlags&CPU_CAP_SSE))
  1629. WARN("SSE performs best with multiple of 4 update sizes (%u)\n", device->UpdateSize);
  1630. if((CPUCapFlags&CPU_CAP_NEON))
  1631. WARN("NEON performs best with multiple of 4 update sizes (%u)\n", device->UpdateSize);
  1632. }
  1633. SetMixerFPUMode(&oldMode);
  1634. ALCdevice_Lock(device);
  1635. context = device->ContextList;
  1636. while(context)
  1637. {
  1638. ALsizei pos;
  1639. context->UpdateSources = AL_FALSE;
  1640. LockUIntMapRead(&context->EffectSlotMap);
  1641. for(pos = 0;pos < context->EffectSlotMap.size;pos++)
  1642. {
  1643. ALeffectslot *slot = context->EffectSlotMap.array[pos].value;
  1644. if(V(slot->EffectState,deviceUpdate)(device) == AL_FALSE)
  1645. {
  1646. UnlockUIntMapRead(&context->EffectSlotMap);
  1647. ALCdevice_Unlock(device);
  1648. RestoreFPUMode(&oldMode);
  1649. return ALC_INVALID_DEVICE;
  1650. }
  1651. slot->NeedsUpdate = AL_FALSE;
  1652. V(slot->EffectState,update)(device, slot);
  1653. }
  1654. UnlockUIntMapRead(&context->EffectSlotMap);
  1655. LockUIntMapRead(&context->SourceMap);
  1656. for(pos = 0;pos < context->SourceMap.size;pos++)
  1657. {
  1658. ALsource *source = context->SourceMap.array[pos].value;
  1659. ALuint s = device->NumAuxSends;
  1660. while(s < MAX_SENDS)
  1661. {
  1662. if(source->Send[s].Slot)
  1663. DecrementRef(&source->Send[s].Slot->ref);
  1664. source->Send[s].Slot = NULL;
  1665. source->Send[s].Gain = 1.0f;
  1666. source->Send[s].GainHF = 1.0f;
  1667. s++;
  1668. }
  1669. source->NeedsUpdate = AL_FALSE;
  1670. ALsource_Update(source, context);
  1671. }
  1672. UnlockUIntMapRead(&context->SourceMap);
  1673. context = context->next;
  1674. }
  1675. if(device->DefaultSlot)
  1676. {
  1677. ALeffectslot *slot = device->DefaultSlot;
  1678. if(V(slot->EffectState,deviceUpdate)(device) == AL_FALSE)
  1679. {
  1680. ALCdevice_Unlock(device);
  1681. RestoreFPUMode(&oldMode);
  1682. return ALC_INVALID_DEVICE;
  1683. }
  1684. slot->NeedsUpdate = AL_FALSE;
  1685. V(slot->EffectState,update)(device, slot);
  1686. }
  1687. ALCdevice_Unlock(device);
  1688. RestoreFPUMode(&oldMode);
  1689. if(!(device->Flags&DEVICE_PAUSED))
  1690. {
  1691. if(V0(device->Backend,start)() == ALC_FALSE)
  1692. return ALC_INVALID_DEVICE;
  1693. device->Flags |= DEVICE_RUNNING;
  1694. }
  1695. return ALC_NO_ERROR;
  1696. }
  1697. /* FreeDevice
  1698. *
  1699. * Frees the device structure, and destroys any objects the app failed to
  1700. * delete. Called once there's no more references on the device.
  1701. */
  1702. static ALCvoid FreeDevice(ALCdevice *device)
  1703. {
  1704. TRACE("%p\n", device);
  1705. V0(device->Backend,close)();
  1706. DELETE_OBJ(device->Backend);
  1707. device->Backend = NULL;
  1708. DELETE_OBJ(device->Synth);
  1709. device->Synth = NULL;
  1710. if(device->DefaultSlot)
  1711. {
  1712. ALeffectState *state = device->DefaultSlot->EffectState;
  1713. device->DefaultSlot = NULL;
  1714. DELETE_OBJ(state);
  1715. }
  1716. if(device->DefaultSfont)
  1717. ALsoundfont_deleteSoundfont(device->DefaultSfont, device);
  1718. device->DefaultSfont = NULL;
  1719. if(device->BufferMap.size > 0)
  1720. {
  1721. WARN("(%p) Deleting %d Buffer(s)\n", device, device->BufferMap.size);
  1722. ReleaseALBuffers(device);
  1723. }
  1724. ResetUIntMap(&device->BufferMap);
  1725. if(device->EffectMap.size > 0)
  1726. {
  1727. WARN("(%p) Deleting %d Effect(s)\n", device, device->EffectMap.size);
  1728. ReleaseALEffects(device);
  1729. }
  1730. ResetUIntMap(&device->EffectMap);
  1731. if(device->FilterMap.size > 0)
  1732. {
  1733. WARN("(%p) Deleting %d Filter(s)\n", device, device->FilterMap.size);
  1734. ReleaseALFilters(device);
  1735. }
  1736. ResetUIntMap(&device->FilterMap);
  1737. if(device->SfontMap.size > 0)
  1738. {
  1739. WARN("(%p) Deleting %d Soundfont(s)\n", device, device->SfontMap.size);
  1740. ReleaseALSoundfonts(device);
  1741. }
  1742. ResetUIntMap(&device->SfontMap);
  1743. if(device->PresetMap.size > 0)
  1744. {
  1745. WARN("(%p) Deleting %d Preset(s)\n", device, device->PresetMap.size);
  1746. ReleaseALPresets(device);
  1747. }
  1748. ResetUIntMap(&device->PresetMap);
  1749. if(device->FontsoundMap.size > 0)
  1750. {
  1751. WARN("(%p) Deleting %d Fontsound(s)\n", device, device->FontsoundMap.size);
  1752. ReleaseALFontsounds(device);
  1753. }
  1754. ResetUIntMap(&device->FontsoundMap);
  1755. free(device->Bs2b);
  1756. device->Bs2b = NULL;
  1757. free(device->DeviceName);
  1758. device->DeviceName = NULL;
  1759. al_free(device);
  1760. }
  1761. void ALCdevice_IncRef(ALCdevice *device)
  1762. {
  1763. RefCount ref;
  1764. ref = IncrementRef(&device->ref);
  1765. TRACEREF("%p increasing refcount to %u\n", device, ref);
  1766. }
  1767. void ALCdevice_DecRef(ALCdevice *device)
  1768. {
  1769. RefCount ref;
  1770. ref = DecrementRef(&device->ref);
  1771. TRACEREF("%p decreasing refcount to %u\n", device, ref);
  1772. if(ref == 0) FreeDevice(device);
  1773. }
  1774. /* VerifyDevice
  1775. *
  1776. * Checks if the device handle is valid, and increments its ref count if so.
  1777. */
  1778. static ALCdevice *VerifyDevice(ALCdevice *device)
  1779. {
  1780. ALCdevice *tmpDevice;
  1781. if(!device)
  1782. return NULL;
  1783. LockLists();
  1784. tmpDevice = DeviceList;
  1785. while(tmpDevice && tmpDevice != device)
  1786. tmpDevice = tmpDevice->next;
  1787. if(tmpDevice)
  1788. ALCdevice_IncRef(tmpDevice);
  1789. UnlockLists();
  1790. return tmpDevice;
  1791. }
  1792. /* InitContext
  1793. *
  1794. * Initializes context fields
  1795. */
  1796. static ALvoid InitContext(ALCcontext *Context)
  1797. {
  1798. ALint i, j;
  1799. //Initialise listener
  1800. Context->Listener->Gain = 1.0f;
  1801. Context->Listener->MetersPerUnit = 1.0f;
  1802. Context->Listener->Position[0] = 0.0f;
  1803. Context->Listener->Position[1] = 0.0f;
  1804. Context->Listener->Position[2] = 0.0f;
  1805. Context->Listener->Velocity[0] = 0.0f;
  1806. Context->Listener->Velocity[1] = 0.0f;
  1807. Context->Listener->Velocity[2] = 0.0f;
  1808. Context->Listener->Forward[0] = 0.0f;
  1809. Context->Listener->Forward[1] = 0.0f;
  1810. Context->Listener->Forward[2] = -1.0f;
  1811. Context->Listener->Up[0] = 0.0f;
  1812. Context->Listener->Up[1] = 1.0f;
  1813. Context->Listener->Up[2] = 0.0f;
  1814. for(i = 0;i < 4;i++)
  1815. {
  1816. for(j = 0;j < 4;j++)
  1817. Context->Listener->Params.Matrix[i][j] = ((i==j) ? 1.0f : 0.0f);
  1818. }
  1819. for(i = 0;i < 3;i++)
  1820. Context->Listener->Params.Velocity[i] = 0.0f;
  1821. //Validate Context
  1822. Context->LastError = AL_NO_ERROR;
  1823. Context->UpdateSources = AL_FALSE;
  1824. Context->ActiveSourceCount = 0;
  1825. InitUIntMap(&Context->SourceMap, Context->Device->MaxNoOfSources);
  1826. InitUIntMap(&Context->EffectSlotMap, Context->Device->AuxiliaryEffectSlotMax);
  1827. //Set globals
  1828. Context->DistanceModel = DefaultDistanceModel;
  1829. Context->SourceDistanceModel = AL_FALSE;
  1830. Context->DopplerFactor = 1.0f;
  1831. Context->DopplerVelocity = 1.0f;
  1832. Context->SpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
  1833. Context->DeferUpdates = AL_FALSE;
  1834. Context->ExtensionList = alExtList;
  1835. }
  1836. /* FreeContext
  1837. *
  1838. * Cleans up the context, and destroys any remaining objects the app failed to
  1839. * delete. Called once there's no more references on the context.
  1840. */
  1841. static ALCvoid FreeContext(ALCcontext *context)
  1842. {
  1843. TRACE("%p\n", context);
  1844. if(context->SourceMap.size > 0)
  1845. {
  1846. WARN("(%p) Deleting %d Source(s)\n", context, context->SourceMap.size);
  1847. ReleaseALSources(context);
  1848. }
  1849. ResetUIntMap(&context->SourceMap);
  1850. if(context->EffectSlotMap.size > 0)
  1851. {
  1852. WARN("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context, context->EffectSlotMap.size);
  1853. ReleaseALAuxiliaryEffectSlots(context);
  1854. }
  1855. ResetUIntMap(&context->EffectSlotMap);
  1856. context->ActiveSourceCount = 0;
  1857. free(context->ActiveSources);
  1858. context->ActiveSources = NULL;
  1859. context->MaxActiveSources = 0;
  1860. context->ActiveEffectSlotCount = 0;
  1861. free(context->ActiveEffectSlots);
  1862. context->ActiveEffectSlots = NULL;
  1863. context->MaxActiveEffectSlots = 0;
  1864. ALCdevice_DecRef(context->Device);
  1865. context->Device = NULL;
  1866. //Invalidate context
  1867. memset(context, 0, sizeof(ALCcontext));
  1868. free(context);
  1869. }
  1870. /* ReleaseContext
  1871. *
  1872. * Removes the context reference from the given device and removes it from
  1873. * being current on the running thread or globally.
  1874. */
  1875. static void ReleaseContext(ALCcontext *context, ALCdevice *device)
  1876. {
  1877. ALCcontext *volatile*tmp_ctx;
  1878. if(althread_getspecific(LocalContext) == context)
  1879. {
  1880. WARN("%p released while current on thread\n", context);
  1881. althread_setspecific(LocalContext, NULL);
  1882. ALCcontext_DecRef(context);
  1883. }
  1884. if(CompExchangePtr((XchgPtr*)&GlobalContext, context, NULL))
  1885. ALCcontext_DecRef(context);
  1886. ALCdevice_Lock(device);
  1887. tmp_ctx = &device->ContextList;
  1888. while(*tmp_ctx)
  1889. {
  1890. if(CompExchangePtr((XchgPtr*)tmp_ctx, context, context->next))
  1891. break;
  1892. tmp_ctx = &(*tmp_ctx)->next;
  1893. }
  1894. ALCdevice_Unlock(device);
  1895. ALCcontext_DecRef(context);
  1896. }
  1897. void ALCcontext_IncRef(ALCcontext *context)
  1898. {
  1899. RefCount ref;
  1900. ref = IncrementRef(&context->ref);
  1901. TRACEREF("%p increasing refcount to %u\n", context, ref);
  1902. }
  1903. void ALCcontext_DecRef(ALCcontext *context)
  1904. {
  1905. RefCount ref;
  1906. ref = DecrementRef(&context->ref);
  1907. TRACEREF("%p decreasing refcount to %u\n", context, ref);
  1908. if(ref == 0) FreeContext(context);
  1909. }
  1910. static void ReleaseThreadCtx(void *ptr)
  1911. {
  1912. WARN("%p current for thread being destroyed\n", ptr);
  1913. ALCcontext_DecRef(ptr);
  1914. }
  1915. /* VerifyContext
  1916. *
  1917. * Checks that the given context is valid, and increments its reference count.
  1918. */
  1919. static ALCcontext *VerifyContext(ALCcontext *context)
  1920. {
  1921. ALCdevice *dev;
  1922. LockLists();
  1923. dev = DeviceList;
  1924. while(dev)
  1925. {
  1926. ALCcontext *tmp_ctx = dev->ContextList;
  1927. while(tmp_ctx)
  1928. {
  1929. if(tmp_ctx == context)
  1930. {
  1931. ALCcontext_IncRef(tmp_ctx);
  1932. UnlockLists();
  1933. return tmp_ctx;
  1934. }
  1935. tmp_ctx = tmp_ctx->next;
  1936. }
  1937. dev = dev->next;
  1938. }
  1939. UnlockLists();
  1940. return NULL;
  1941. }
  1942. /* GetContextRef
  1943. *
  1944. * Returns the currently active context for this thread, and adds a reference
  1945. * without locking it.
  1946. */
  1947. ALCcontext *GetContextRef(void)
  1948. {
  1949. ALCcontext *context;
  1950. context = althread_getspecific(LocalContext);
  1951. if(context)
  1952. ALCcontext_IncRef(context);
  1953. else
  1954. {
  1955. LockLists();
  1956. context = GlobalContext;
  1957. if(context)
  1958. ALCcontext_IncRef(context);
  1959. UnlockLists();
  1960. }
  1961. return context;
  1962. }
  1963. /************************************************
  1964. * Standard ALC functions
  1965. ************************************************/
  1966. /* alcGetError
  1967. *
  1968. * Return last ALC generated error code for the given device
  1969. */
  1970. ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
  1971. {
  1972. ALCenum errorCode;
  1973. if(VerifyDevice(device))
  1974. {
  1975. errorCode = ExchangeInt(&device->LastError, ALC_NO_ERROR);
  1976. ALCdevice_DecRef(device);
  1977. }
  1978. else
  1979. errorCode = ExchangeInt(&LastNullDeviceError, ALC_NO_ERROR);
  1980. return errorCode;
  1981. }
  1982. /* alcSuspendContext
  1983. *
  1984. * Not functional
  1985. */
  1986. ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *UNUSED(context))
  1987. {
  1988. }
  1989. /* alcProcessContext
  1990. *
  1991. * Not functional
  1992. */
  1993. ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *UNUSED(context))
  1994. {
  1995. }
  1996. /* alcGetString
  1997. *
  1998. * Returns information about the device, and error strings
  1999. */
  2000. ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *Device, ALCenum param)
  2001. {
  2002. const ALCchar *value = NULL;
  2003. switch(param)
  2004. {
  2005. case ALC_NO_ERROR:
  2006. value = alcNoError;
  2007. break;
  2008. case ALC_INVALID_ENUM:
  2009. value = alcErrInvalidEnum;
  2010. break;
  2011. case ALC_INVALID_VALUE:
  2012. value = alcErrInvalidValue;
  2013. break;
  2014. case ALC_INVALID_DEVICE:
  2015. value = alcErrInvalidDevice;
  2016. break;
  2017. case ALC_INVALID_CONTEXT:
  2018. value = alcErrInvalidContext;
  2019. break;
  2020. case ALC_OUT_OF_MEMORY:
  2021. value = alcErrOutOfMemory;
  2022. break;
  2023. case ALC_DEVICE_SPECIFIER:
  2024. value = alcDefaultName;
  2025. break;
  2026. case ALC_ALL_DEVICES_SPECIFIER:
  2027. if(VerifyDevice(Device))
  2028. {
  2029. value = Device->DeviceName;
  2030. ALCdevice_DecRef(Device);
  2031. }
  2032. else
  2033. {
  2034. ProbeAllDevicesList();
  2035. value = alcAllDevicesList;
  2036. }
  2037. break;
  2038. case ALC_CAPTURE_DEVICE_SPECIFIER:
  2039. if(VerifyDevice(Device))
  2040. {
  2041. value = Device->DeviceName;
  2042. ALCdevice_DecRef(Device);
  2043. }
  2044. else
  2045. {
  2046. ProbeCaptureDeviceList();
  2047. value = alcCaptureDeviceList;
  2048. }
  2049. break;
  2050. /* Default devices are always first in the list */
  2051. case ALC_DEFAULT_DEVICE_SPECIFIER:
  2052. value = alcDefaultName;
  2053. break;
  2054. case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
  2055. if(!alcAllDevicesList)
  2056. ProbeAllDevicesList();
  2057. Device = VerifyDevice(Device);
  2058. free(alcDefaultAllDevicesSpecifier);
  2059. alcDefaultAllDevicesSpecifier = strdup(alcAllDevicesList ?
  2060. alcAllDevicesList : "");
  2061. if(!alcDefaultAllDevicesSpecifier)
  2062. alcSetError(Device, ALC_OUT_OF_MEMORY);
  2063. value = alcDefaultAllDevicesSpecifier;
  2064. if(Device) ALCdevice_DecRef(Device);
  2065. break;
  2066. case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
  2067. if(!alcCaptureDeviceList)
  2068. ProbeCaptureDeviceList();
  2069. Device = VerifyDevice(Device);
  2070. free(alcCaptureDefaultDeviceSpecifier);
  2071. alcCaptureDefaultDeviceSpecifier = strdup(alcCaptureDeviceList ?
  2072. alcCaptureDeviceList : "");
  2073. if(!alcCaptureDefaultDeviceSpecifier)
  2074. alcSetError(Device, ALC_OUT_OF_MEMORY);
  2075. value = alcCaptureDefaultDeviceSpecifier;
  2076. if(Device) ALCdevice_DecRef(Device);
  2077. break;
  2078. case ALC_EXTENSIONS:
  2079. if(!VerifyDevice(Device))
  2080. value = alcNoDeviceExtList;
  2081. else
  2082. {
  2083. value = alcExtensionList;
  2084. ALCdevice_DecRef(Device);
  2085. }
  2086. break;
  2087. default:
  2088. Device = VerifyDevice(Device);
  2089. alcSetError(Device, ALC_INVALID_ENUM);
  2090. if(Device) ALCdevice_DecRef(Device);
  2091. break;
  2092. }
  2093. return value;
  2094. }
  2095. /* alcGetIntegerv
  2096. *
  2097. * Returns information about the device and the version of OpenAL
  2098. */
  2099. ALC_API ALCvoid ALC_APIENTRY alcGetIntegerv(ALCdevice *device,ALCenum param,ALsizei size,ALCint *data)
  2100. {
  2101. device = VerifyDevice(device);
  2102. if(size == 0 || data == NULL)
  2103. {
  2104. alcSetError(device, ALC_INVALID_VALUE);
  2105. if(device) ALCdevice_DecRef(device);
  2106. return;
  2107. }
  2108. if(!device)
  2109. {
  2110. switch(param)
  2111. {
  2112. case ALC_MAJOR_VERSION:
  2113. *data = alcMajorVersion;
  2114. break;
  2115. case ALC_MINOR_VERSION:
  2116. *data = alcMinorVersion;
  2117. break;
  2118. case ALC_ATTRIBUTES_SIZE:
  2119. case ALC_ALL_ATTRIBUTES:
  2120. case ALC_FREQUENCY:
  2121. case ALC_REFRESH:
  2122. case ALC_SYNC:
  2123. case ALC_MONO_SOURCES:
  2124. case ALC_STEREO_SOURCES:
  2125. case ALC_CAPTURE_SAMPLES:
  2126. case ALC_FORMAT_CHANNELS_SOFT:
  2127. case ALC_FORMAT_TYPE_SOFT:
  2128. alcSetError(NULL, ALC_INVALID_DEVICE);
  2129. break;
  2130. default:
  2131. alcSetError(NULL, ALC_INVALID_ENUM);
  2132. break;
  2133. }
  2134. }
  2135. else if(device->Type == Capture)
  2136. {
  2137. switch(param)
  2138. {
  2139. case ALC_CAPTURE_SAMPLES:
  2140. ALCdevice_Lock(device);
  2141. *data = V0(device->Backend,availableSamples)();
  2142. ALCdevice_Unlock(device);
  2143. break;
  2144. case ALC_CONNECTED:
  2145. *data = device->Connected;
  2146. break;
  2147. default:
  2148. alcSetError(device, ALC_INVALID_ENUM);
  2149. break;
  2150. }
  2151. }
  2152. else /* render device */
  2153. {
  2154. switch(param)
  2155. {
  2156. case ALC_MAJOR_VERSION:
  2157. *data = alcMajorVersion;
  2158. break;
  2159. case ALC_MINOR_VERSION:
  2160. *data = alcMinorVersion;
  2161. break;
  2162. case ALC_EFX_MAJOR_VERSION:
  2163. *data = alcEFXMajorVersion;
  2164. break;
  2165. case ALC_EFX_MINOR_VERSION:
  2166. *data = alcEFXMinorVersion;
  2167. break;
  2168. case ALC_ATTRIBUTES_SIZE:
  2169. *data = 15;
  2170. break;
  2171. case ALC_ALL_ATTRIBUTES:
  2172. if(size < 15)
  2173. alcSetError(device, ALC_INVALID_VALUE);
  2174. else
  2175. {
  2176. int i = 0;
  2177. data[i++] = ALC_FREQUENCY;
  2178. data[i++] = device->Frequency;
  2179. if(device->Type != Loopback)
  2180. {
  2181. data[i++] = ALC_REFRESH;
  2182. data[i++] = device->Frequency / device->UpdateSize;
  2183. data[i++] = ALC_SYNC;
  2184. data[i++] = ALC_FALSE;
  2185. }
  2186. else
  2187. {
  2188. data[i++] = ALC_FORMAT_CHANNELS_SOFT;
  2189. data[i++] = device->FmtChans;
  2190. data[i++] = ALC_FORMAT_TYPE_SOFT;
  2191. data[i++] = device->FmtType;
  2192. }
  2193. data[i++] = ALC_MONO_SOURCES;
  2194. data[i++] = device->NumMonoSources;
  2195. data[i++] = ALC_STEREO_SOURCES;
  2196. data[i++] = device->NumStereoSources;
  2197. data[i++] = ALC_MAX_AUXILIARY_SENDS;
  2198. data[i++] = device->NumAuxSends;
  2199. data[i++] = ALC_HRTF_SOFT;
  2200. data[i++] = (device->Hrtf ? ALC_TRUE : ALC_FALSE);
  2201. data[i++] = 0;
  2202. }
  2203. break;
  2204. case ALC_FREQUENCY:
  2205. *data = device->Frequency;
  2206. break;
  2207. case ALC_REFRESH:
  2208. if(device->Type == Loopback)
  2209. alcSetError(device, ALC_INVALID_DEVICE);
  2210. else
  2211. *data = device->Frequency / device->UpdateSize;
  2212. break;
  2213. case ALC_SYNC:
  2214. if(device->Type == Loopback)
  2215. alcSetError(device, ALC_INVALID_DEVICE);
  2216. else
  2217. *data = ALC_FALSE;
  2218. break;
  2219. case ALC_FORMAT_CHANNELS_SOFT:
  2220. if(device->Type != Loopback)
  2221. alcSetError(device, ALC_INVALID_DEVICE);
  2222. else
  2223. *data = device->FmtChans;
  2224. break;
  2225. case ALC_FORMAT_TYPE_SOFT:
  2226. if(device->Type != Loopback)
  2227. alcSetError(device, ALC_INVALID_DEVICE);
  2228. else
  2229. *data = device->FmtType;
  2230. break;
  2231. case ALC_MONO_SOURCES:
  2232. *data = device->NumMonoSources;
  2233. break;
  2234. case ALC_STEREO_SOURCES:
  2235. *data = device->NumStereoSources;
  2236. break;
  2237. case ALC_MAX_AUXILIARY_SENDS:
  2238. *data = device->NumAuxSends;
  2239. break;
  2240. case ALC_CONNECTED:
  2241. *data = device->Connected;
  2242. break;
  2243. case ALC_HRTF_SOFT:
  2244. *data = (device->Hrtf ? ALC_TRUE : ALC_FALSE);
  2245. break;
  2246. default:
  2247. alcSetError(device, ALC_INVALID_ENUM);
  2248. break;
  2249. }
  2250. }
  2251. if(device)
  2252. ALCdevice_DecRef(device);
  2253. }
  2254. /* alcIsExtensionPresent
  2255. *
  2256. * Determines if there is support for a particular extension
  2257. */
  2258. ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
  2259. {
  2260. ALCboolean bResult = ALC_FALSE;
  2261. device = VerifyDevice(device);
  2262. if(!extName)
  2263. alcSetError(device, ALC_INVALID_VALUE);
  2264. else
  2265. {
  2266. size_t len = strlen(extName);
  2267. const char *ptr = (device ? alcExtensionList : alcNoDeviceExtList);
  2268. while(ptr && *ptr)
  2269. {
  2270. if(strncasecmp(ptr, extName, len) == 0 &&
  2271. (ptr[len] == '\0' || isspace(ptr[len])))
  2272. {
  2273. bResult = ALC_TRUE;
  2274. break;
  2275. }
  2276. if((ptr=strchr(ptr, ' ')) != NULL)
  2277. {
  2278. do {
  2279. ++ptr;
  2280. } while(isspace(*ptr));
  2281. }
  2282. }
  2283. }
  2284. if(device)
  2285. ALCdevice_DecRef(device);
  2286. return bResult;
  2287. }
  2288. /* alcGetProcAddress
  2289. *
  2290. * Retrieves the function address for a particular extension function
  2291. */
  2292. ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
  2293. {
  2294. ALCvoid *ptr = NULL;
  2295. if(!funcName)
  2296. {
  2297. device = VerifyDevice(device);
  2298. alcSetError(device, ALC_INVALID_VALUE);
  2299. if(device) ALCdevice_DecRef(device);
  2300. }
  2301. else
  2302. {
  2303. ALsizei i = 0;
  2304. while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName, funcName) != 0)
  2305. i++;
  2306. ptr = alcFunctions[i].address;
  2307. }
  2308. return ptr;
  2309. }
  2310. /* alcGetEnumValue
  2311. *
  2312. * Get the value for a particular ALC enumeration name
  2313. */
  2314. ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
  2315. {
  2316. ALCenum val = 0;
  2317. if(!enumName)
  2318. {
  2319. device = VerifyDevice(device);
  2320. alcSetError(device, ALC_INVALID_VALUE);
  2321. if(device) ALCdevice_DecRef(device);
  2322. }
  2323. else
  2324. {
  2325. ALsizei i = 0;
  2326. while(enumeration[i].enumName && strcmp(enumeration[i].enumName, enumName) != 0)
  2327. i++;
  2328. val = enumeration[i].value;
  2329. }
  2330. return val;
  2331. }
  2332. /* alcCreateContext
  2333. *
  2334. * Create and attach a context to the given device.
  2335. */
  2336. ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
  2337. {
  2338. ALCcontext *ALContext;
  2339. ALCenum err;
  2340. LockLists();
  2341. if(!(device=VerifyDevice(device)) || device->Type == Capture || !device->Connected)
  2342. {
  2343. UnlockLists();
  2344. alcSetError(device, ALC_INVALID_DEVICE);
  2345. if(device) ALCdevice_DecRef(device);
  2346. return NULL;
  2347. }
  2348. device->LastError = ALC_NO_ERROR;
  2349. if((err=UpdateDeviceParams(device, attrList)) != ALC_NO_ERROR)
  2350. {
  2351. UnlockLists();
  2352. alcSetError(device, err);
  2353. if(err == ALC_INVALID_DEVICE)
  2354. {
  2355. ALCdevice_Lock(device);
  2356. aluHandleDisconnect(device);
  2357. ALCdevice_Unlock(device);
  2358. }
  2359. ALCdevice_DecRef(device);
  2360. return NULL;
  2361. }
  2362. ALContext = calloc(1, sizeof(ALCcontext)+15+sizeof(ALlistener));
  2363. if(ALContext)
  2364. {
  2365. ALContext->ref = 1;
  2366. ALContext->Listener = (ALlistener*)(((ALintptrEXT)(ALContext+1)+15)&~15);
  2367. ALContext->MaxActiveSources = 256;
  2368. ALContext->ActiveSources = malloc(sizeof(ALContext->ActiveSources[0]) *
  2369. ALContext->MaxActiveSources);
  2370. }
  2371. if(!ALContext || !ALContext->ActiveSources)
  2372. {
  2373. if(!device->ContextList)
  2374. {
  2375. V0(device->Backend,stop)();
  2376. device->Flags &= ~DEVICE_RUNNING;
  2377. }
  2378. UnlockLists();
  2379. free(ALContext);
  2380. ALContext = NULL;
  2381. alcSetError(device, ALC_OUT_OF_MEMORY);
  2382. ALCdevice_DecRef(device);
  2383. return NULL;
  2384. }
  2385. ALContext->Device = device;
  2386. ALCdevice_IncRef(device);
  2387. InitContext(ALContext);
  2388. do {
  2389. ALContext->next = device->ContextList;
  2390. } while(!CompExchangePtr((XchgPtr*)&device->ContextList, ALContext->next, ALContext));
  2391. UnlockLists();
  2392. ALCdevice_DecRef(device);
  2393. TRACE("Created context %p\n", ALContext);
  2394. return ALContext;
  2395. }
  2396. /* alcDestroyContext
  2397. *
  2398. * Remove a context from its device
  2399. */
  2400. ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
  2401. {
  2402. ALCdevice *Device;
  2403. LockLists();
  2404. /* alcGetContextsDevice sets an error for invalid contexts */
  2405. Device = alcGetContextsDevice(context);
  2406. if(Device)
  2407. {
  2408. ReleaseContext(context, Device);
  2409. if(!Device->ContextList)
  2410. {
  2411. V0(Device->Backend,stop)();
  2412. Device->Flags &= ~DEVICE_RUNNING;
  2413. }
  2414. }
  2415. UnlockLists();
  2416. }
  2417. /* alcGetCurrentContext
  2418. *
  2419. * Returns the currently active context on the calling thread
  2420. */
  2421. ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(void)
  2422. {
  2423. ALCcontext *Context;
  2424. Context = althread_getspecific(LocalContext);
  2425. if(!Context) Context = GlobalContext;
  2426. return Context;
  2427. }
  2428. /* alcGetThreadContext
  2429. *
  2430. * Returns the currently active thread-local context
  2431. */
  2432. ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
  2433. {
  2434. ALCcontext *Context;
  2435. Context = althread_getspecific(LocalContext);
  2436. return Context;
  2437. }
  2438. /* alcMakeContextCurrent
  2439. *
  2440. * Makes the given context the active process-wide context, and removes the
  2441. * thread-local context for the calling thread.
  2442. */
  2443. ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
  2444. {
  2445. /* context must be valid or NULL */
  2446. if(context && !(context=VerifyContext(context)))
  2447. {
  2448. alcSetError(NULL, ALC_INVALID_CONTEXT);
  2449. return ALC_FALSE;
  2450. }
  2451. /* context's reference count is already incremented */
  2452. context = ExchangePtr((XchgPtr*)&GlobalContext, context);
  2453. if(context) ALCcontext_DecRef(context);
  2454. if((context=althread_getspecific(LocalContext)) != NULL)
  2455. {
  2456. althread_setspecific(LocalContext, NULL);
  2457. ALCcontext_DecRef(context);
  2458. }
  2459. return ALC_TRUE;
  2460. }
  2461. /* alcSetThreadContext
  2462. *
  2463. * Makes the given context the active context for the current thread
  2464. */
  2465. ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
  2466. {
  2467. ALCcontext *old;
  2468. /* context must be valid or NULL */
  2469. if(context && !(context=VerifyContext(context)))
  2470. {
  2471. alcSetError(NULL, ALC_INVALID_CONTEXT);
  2472. return ALC_FALSE;
  2473. }
  2474. /* context's reference count is already incremented */
  2475. old = althread_getspecific(LocalContext);
  2476. althread_setspecific(LocalContext, context);
  2477. if(old) ALCcontext_DecRef(old);
  2478. return ALC_TRUE;
  2479. }
  2480. /* alcGetContextsDevice
  2481. *
  2482. * Returns the device that a particular context is attached to
  2483. */
  2484. ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context)
  2485. {
  2486. ALCdevice *Device;
  2487. if(!(Context=VerifyContext(Context)))
  2488. {
  2489. alcSetError(NULL, ALC_INVALID_CONTEXT);
  2490. return NULL;
  2491. }
  2492. Device = Context->Device;
  2493. ALCcontext_DecRef(Context);
  2494. return Device;
  2495. }
  2496. /* alcOpenDevice
  2497. *
  2498. * Opens the named device.
  2499. */
  2500. ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
  2501. {
  2502. const ALCchar *fmt;
  2503. ALCdevice *device;
  2504. ALCenum err;
  2505. DO_INITCONFIG();
  2506. if(!PlaybackBackend.name)
  2507. {
  2508. alcSetError(NULL, ALC_INVALID_VALUE);
  2509. return NULL;
  2510. }
  2511. if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
  2512. deviceName = NULL;
  2513. device = al_calloc(16, sizeof(ALCdevice)+15+sizeof(ALeffectslot));
  2514. if(!device)
  2515. {
  2516. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2517. return NULL;
  2518. }
  2519. //Validate device
  2520. device->ref = 1;
  2521. device->Connected = ALC_TRUE;
  2522. device->Type = Playback;
  2523. device->LastError = ALC_NO_ERROR;
  2524. device->Flags = 0;
  2525. device->Bs2b = NULL;
  2526. device->Bs2bLevel = 0;
  2527. device->DeviceName = NULL;
  2528. device->ContextList = NULL;
  2529. device->MaxNoOfSources = 256;
  2530. device->AuxiliaryEffectSlotMax = 4;
  2531. device->NumAuxSends = MAX_SENDS;
  2532. InitUIntMap(&device->BufferMap, ~0);
  2533. InitUIntMap(&device->EffectMap, ~0);
  2534. InitUIntMap(&device->FilterMap, ~0);
  2535. InitUIntMap(&device->SfontMap, ~0);
  2536. InitUIntMap(&device->PresetMap, ~0);
  2537. InitUIntMap(&device->FontsoundMap, ~0);
  2538. //Set output format
  2539. device->FmtChans = DevFmtChannelsDefault;
  2540. device->FmtType = DevFmtTypeDefault;
  2541. device->Frequency = DEFAULT_OUTPUT_RATE;
  2542. device->NumUpdates = 4;
  2543. device->UpdateSize = 1024;
  2544. if(!PlaybackBackend.getFactory)
  2545. {
  2546. device->Funcs = &PlaybackBackend.Funcs;
  2547. device->Backend = create_backend_wrapper(device, ALCbackend_Playback);
  2548. }
  2549. else
  2550. {
  2551. ALCbackendFactory *factory = PlaybackBackend.getFactory();
  2552. device->Backend = V(factory,createBackend)(device, ALCbackend_Playback);
  2553. }
  2554. if(!device->Backend)
  2555. {
  2556. al_free(device);
  2557. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2558. return NULL;
  2559. }
  2560. if(ConfigValueStr(NULL, "channels", &fmt))
  2561. {
  2562. static const struct {
  2563. const char name[16];
  2564. enum DevFmtChannels chans;
  2565. } chanlist[] = {
  2566. { "mono", DevFmtMono },
  2567. { "stereo", DevFmtStereo },
  2568. { "quad", DevFmtQuad },
  2569. { "surround51", DevFmtX51 },
  2570. { "surround61", DevFmtX61 },
  2571. { "surround71", DevFmtX71 },
  2572. };
  2573. size_t i;
  2574. for(i = 0;i < COUNTOF(chanlist);i++)
  2575. {
  2576. if(strcasecmp(chanlist[i].name, fmt) == 0)
  2577. {
  2578. device->FmtChans = chanlist[i].chans;
  2579. device->Flags |= DEVICE_CHANNELS_REQUEST;
  2580. break;
  2581. }
  2582. }
  2583. if(i == COUNTOF(chanlist))
  2584. ERR("Unsupported channels: %s\n", fmt);
  2585. }
  2586. if(ConfigValueStr(NULL, "sample-type", &fmt))
  2587. {
  2588. static const struct {
  2589. const char name[16];
  2590. enum DevFmtType type;
  2591. } typelist[] = {
  2592. { "int8", DevFmtByte },
  2593. { "uint8", DevFmtUByte },
  2594. { "int16", DevFmtShort },
  2595. { "uint16", DevFmtUShort },
  2596. { "int32", DevFmtInt },
  2597. { "uint32", DevFmtUInt },
  2598. { "float32", DevFmtFloat },
  2599. };
  2600. size_t i;
  2601. for(i = 0;i < COUNTOF(typelist);i++)
  2602. {
  2603. if(strcasecmp(typelist[i].name, fmt) == 0)
  2604. {
  2605. device->FmtType = typelist[i].type;
  2606. device->Flags |= DEVICE_SAMPLE_TYPE_REQUEST;
  2607. break;
  2608. }
  2609. }
  2610. if(i == COUNTOF(typelist))
  2611. ERR("Unsupported sample-type: %s\n", fmt);
  2612. }
  2613. #define DEVICE_FORMAT_REQUEST (DEVICE_CHANNELS_REQUEST|DEVICE_SAMPLE_TYPE_REQUEST)
  2614. if((device->Flags&DEVICE_FORMAT_REQUEST) != DEVICE_FORMAT_REQUEST &&
  2615. ConfigValueStr(NULL, "format", &fmt))
  2616. {
  2617. static const struct {
  2618. const char name[32];
  2619. enum DevFmtChannels channels;
  2620. enum DevFmtType type;
  2621. } formats[] = {
  2622. { "AL_FORMAT_MONO32", DevFmtMono, DevFmtFloat },
  2623. { "AL_FORMAT_STEREO32", DevFmtStereo, DevFmtFloat },
  2624. { "AL_FORMAT_QUAD32", DevFmtQuad, DevFmtFloat },
  2625. { "AL_FORMAT_51CHN32", DevFmtX51, DevFmtFloat },
  2626. { "AL_FORMAT_61CHN32", DevFmtX61, DevFmtFloat },
  2627. { "AL_FORMAT_71CHN32", DevFmtX71, DevFmtFloat },
  2628. { "AL_FORMAT_MONO16", DevFmtMono, DevFmtShort },
  2629. { "AL_FORMAT_STEREO16", DevFmtStereo, DevFmtShort },
  2630. { "AL_FORMAT_QUAD16", DevFmtQuad, DevFmtShort },
  2631. { "AL_FORMAT_51CHN16", DevFmtX51, DevFmtShort },
  2632. { "AL_FORMAT_61CHN16", DevFmtX61, DevFmtShort },
  2633. { "AL_FORMAT_71CHN16", DevFmtX71, DevFmtShort },
  2634. { "AL_FORMAT_MONO8", DevFmtMono, DevFmtByte },
  2635. { "AL_FORMAT_STEREO8", DevFmtStereo, DevFmtByte },
  2636. { "AL_FORMAT_QUAD8", DevFmtQuad, DevFmtByte },
  2637. { "AL_FORMAT_51CHN8", DevFmtX51, DevFmtByte },
  2638. { "AL_FORMAT_61CHN8", DevFmtX61, DevFmtByte },
  2639. { "AL_FORMAT_71CHN8", DevFmtX71, DevFmtByte }
  2640. };
  2641. size_t i;
  2642. ERR("Option 'format' is deprecated, please use 'channels' and 'sample-type'\n");
  2643. for(i = 0;i < COUNTOF(formats);i++)
  2644. {
  2645. if(strcasecmp(fmt, formats[i].name) == 0)
  2646. {
  2647. if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
  2648. device->FmtChans = formats[i].channels;
  2649. if(!(device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
  2650. device->FmtType = formats[i].type;
  2651. device->Flags |= DEVICE_FORMAT_REQUEST;
  2652. break;
  2653. }
  2654. }
  2655. if(i == COUNTOF(formats))
  2656. ERR("Unsupported format: %s\n", fmt);
  2657. }
  2658. #undef DEVICE_FORMAT_REQUEST
  2659. if(ConfigValueUInt(NULL, "frequency", &device->Frequency))
  2660. {
  2661. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  2662. if(device->Frequency < MIN_OUTPUT_RATE)
  2663. ERR("%uhz request clamped to %uhz minimum\n", device->Frequency, MIN_OUTPUT_RATE);
  2664. device->Frequency = maxu(device->Frequency, MIN_OUTPUT_RATE);
  2665. }
  2666. ConfigValueUInt(NULL, "periods", &device->NumUpdates);
  2667. device->NumUpdates = clampu(device->NumUpdates, 2, 16);
  2668. ConfigValueUInt(NULL, "period_size", &device->UpdateSize);
  2669. device->UpdateSize = clampu(device->UpdateSize, 64, 8192);
  2670. if((CPUCapFlags&CPU_CAP_SSE))
  2671. device->UpdateSize = (device->UpdateSize+3)&~3;
  2672. ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
  2673. if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
  2674. ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
  2675. if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
  2676. ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
  2677. if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
  2678. ConfigValueInt(NULL, "cf_level", &device->Bs2bLevel);
  2679. device->NumStereoSources = 1;
  2680. device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
  2681. device->Synth = SynthCreate(device);
  2682. if(!device->Synth)
  2683. {
  2684. DELETE_OBJ(device->Backend);
  2685. al_free(device);
  2686. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2687. return NULL;
  2688. }
  2689. // Find a playback device to open
  2690. if((err=V(device->Backend,open)(deviceName)) != ALC_NO_ERROR)
  2691. {
  2692. DELETE_OBJ(device->Synth);
  2693. DELETE_OBJ(device->Backend);
  2694. al_free(device);
  2695. alcSetError(NULL, err);
  2696. return NULL;
  2697. }
  2698. if(DefaultEffect.type != AL_EFFECT_NULL)
  2699. {
  2700. device->DefaultSlot = (ALeffectslot*)(((ALintptrEXT)(device+1)+15)&~15);
  2701. if(InitEffectSlot(device->DefaultSlot) != AL_NO_ERROR)
  2702. {
  2703. device->DefaultSlot = NULL;
  2704. ERR("Failed to initialize the default effect slot\n");
  2705. }
  2706. else if(InitializeEffect(device, device->DefaultSlot, &DefaultEffect) != AL_NO_ERROR)
  2707. {
  2708. ALeffectState *state = device->DefaultSlot->EffectState;
  2709. device->DefaultSlot = NULL;
  2710. DELETE_OBJ(state);
  2711. ERR("Failed to initialize the default effect\n");
  2712. }
  2713. }
  2714. do {
  2715. device->next = DeviceList;
  2716. } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
  2717. TRACE("Created device %p, \"%s\"\n", device, device->DeviceName);
  2718. return device;
  2719. }
  2720. /* alcCloseDevice
  2721. *
  2722. * Closes the given device.
  2723. */
  2724. ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *Device)
  2725. {
  2726. ALCdevice *volatile*list;
  2727. ALCcontext *ctx;
  2728. LockLists();
  2729. list = &DeviceList;
  2730. while(*list && *list != Device)
  2731. list = &(*list)->next;
  2732. if(!*list || (*list)->Type == Capture)
  2733. {
  2734. alcSetError(*list, ALC_INVALID_DEVICE);
  2735. UnlockLists();
  2736. return ALC_FALSE;
  2737. }
  2738. *list = (*list)->next;
  2739. UnlockLists();
  2740. while((ctx=Device->ContextList) != NULL)
  2741. {
  2742. WARN("Releasing context %p\n", ctx);
  2743. ReleaseContext(ctx, Device);
  2744. }
  2745. if((Device->Flags&DEVICE_RUNNING))
  2746. V0(Device->Backend,stop)();
  2747. Device->Flags &= ~DEVICE_RUNNING;
  2748. ALCdevice_DecRef(Device);
  2749. return ALC_TRUE;
  2750. }
  2751. /************************************************
  2752. * ALC capture functions
  2753. ************************************************/
  2754. ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei samples)
  2755. {
  2756. ALCdevice *device = NULL;
  2757. ALCenum err;
  2758. DO_INITCONFIG();
  2759. if(!CaptureBackend.name)
  2760. {
  2761. alcSetError(NULL, ALC_INVALID_VALUE);
  2762. return NULL;
  2763. }
  2764. if(samples <= 0)
  2765. {
  2766. alcSetError(NULL, ALC_INVALID_VALUE);
  2767. return NULL;
  2768. }
  2769. if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
  2770. deviceName = NULL;
  2771. device = al_calloc(16, sizeof(ALCdevice));
  2772. if(!device)
  2773. {
  2774. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2775. return NULL;
  2776. }
  2777. //Validate device
  2778. device->ref = 1;
  2779. device->Connected = ALC_TRUE;
  2780. device->Type = Capture;
  2781. InitUIntMap(&device->BufferMap, ~0);
  2782. InitUIntMap(&device->EffectMap, ~0);
  2783. InitUIntMap(&device->FilterMap, ~0);
  2784. InitUIntMap(&device->SfontMap, ~0);
  2785. InitUIntMap(&device->PresetMap, ~0);
  2786. InitUIntMap(&device->FontsoundMap, ~0);
  2787. device->DeviceName = NULL;
  2788. if(!CaptureBackend.getFactory)
  2789. {
  2790. device->Funcs = &CaptureBackend.Funcs;
  2791. device->Backend = create_backend_wrapper(device, ALCbackend_Capture);
  2792. }
  2793. else
  2794. {
  2795. ALCbackendFactory *factory = CaptureBackend.getFactory();
  2796. device->Backend = V(factory,createBackend)(device, ALCbackend_Capture);
  2797. }
  2798. if(!device->Backend)
  2799. {
  2800. al_free(device);
  2801. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2802. return NULL;
  2803. }
  2804. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  2805. device->Frequency = frequency;
  2806. device->Flags |= DEVICE_CHANNELS_REQUEST | DEVICE_SAMPLE_TYPE_REQUEST;
  2807. if(DecomposeDevFormat(format, &device->FmtChans, &device->FmtType) == AL_FALSE)
  2808. {
  2809. al_free(device);
  2810. alcSetError(NULL, ALC_INVALID_ENUM);
  2811. return NULL;
  2812. }
  2813. device->UpdateSize = samples;
  2814. device->NumUpdates = 1;
  2815. if((err=V(device->Backend,open)(deviceName)) != ALC_NO_ERROR)
  2816. {
  2817. al_free(device);
  2818. alcSetError(NULL, err);
  2819. return NULL;
  2820. }
  2821. do {
  2822. device->next = DeviceList;
  2823. } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
  2824. TRACE("Created device %p, \"%s\"\n", device, device->DeviceName);
  2825. return device;
  2826. }
  2827. ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *Device)
  2828. {
  2829. ALCdevice *volatile*list;
  2830. LockLists();
  2831. list = &DeviceList;
  2832. while(*list && *list != Device)
  2833. list = &(*list)->next;
  2834. if(!*list || (*list)->Type != Capture)
  2835. {
  2836. alcSetError(*list, ALC_INVALID_DEVICE);
  2837. UnlockLists();
  2838. return ALC_FALSE;
  2839. }
  2840. *list = (*list)->next;
  2841. UnlockLists();
  2842. ALCdevice_DecRef(Device);
  2843. return ALC_TRUE;
  2844. }
  2845. ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
  2846. {
  2847. if(!(device=VerifyDevice(device)) || device->Type != Capture)
  2848. alcSetError(device, ALC_INVALID_DEVICE);
  2849. else
  2850. {
  2851. ALCdevice_Lock(device);
  2852. if(device->Connected)
  2853. {
  2854. if(!(device->Flags&DEVICE_RUNNING))
  2855. V0(device->Backend,start)();
  2856. device->Flags |= DEVICE_RUNNING;
  2857. }
  2858. ALCdevice_Unlock(device);
  2859. }
  2860. if(device) ALCdevice_DecRef(device);
  2861. }
  2862. ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
  2863. {
  2864. if(!(device=VerifyDevice(device)) || device->Type != Capture)
  2865. alcSetError(device, ALC_INVALID_DEVICE);
  2866. else
  2867. {
  2868. ALCdevice_Lock(device);
  2869. if((device->Flags&DEVICE_RUNNING))
  2870. V0(device->Backend,stop)();
  2871. device->Flags &= ~DEVICE_RUNNING;
  2872. ALCdevice_Unlock(device);
  2873. }
  2874. if(device) ALCdevice_DecRef(device);
  2875. }
  2876. ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
  2877. {
  2878. if(!(device=VerifyDevice(device)) || device->Type != Capture)
  2879. alcSetError(device, ALC_INVALID_DEVICE);
  2880. else
  2881. {
  2882. ALCenum err = ALC_INVALID_VALUE;
  2883. ALCdevice_Lock(device);
  2884. if(samples >= 0 && V0(device->Backend,availableSamples)() >= (ALCuint)samples)
  2885. err = V(device->Backend,captureSamples)(buffer, samples);
  2886. ALCdevice_Unlock(device);
  2887. if(err != ALC_NO_ERROR)
  2888. alcSetError(device, err);
  2889. }
  2890. if(device) ALCdevice_DecRef(device);
  2891. }
  2892. /************************************************
  2893. * ALC loopback functions
  2894. ************************************************/
  2895. /* alcLoopbackOpenDeviceSOFT
  2896. *
  2897. * Open a loopback device, for manual rendering.
  2898. */
  2899. ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceName)
  2900. {
  2901. ALCbackendFactory *factory;
  2902. ALCdevice *device;
  2903. DO_INITCONFIG();
  2904. /* Make sure the device name, if specified, is us. */
  2905. if(deviceName && strcmp(deviceName, alcDefaultName) != 0)
  2906. {
  2907. alcSetError(NULL, ALC_INVALID_VALUE);
  2908. return NULL;
  2909. }
  2910. device = al_calloc(16, sizeof(ALCdevice));
  2911. if(!device)
  2912. {
  2913. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2914. return NULL;
  2915. }
  2916. //Validate device
  2917. device->ref = 1;
  2918. device->Connected = ALC_TRUE;
  2919. device->Type = Loopback;
  2920. device->LastError = ALC_NO_ERROR;
  2921. device->Flags = 0;
  2922. device->Bs2b = NULL;
  2923. device->Bs2bLevel = 0;
  2924. device->DeviceName = NULL;
  2925. device->ContextList = NULL;
  2926. device->MaxNoOfSources = 256;
  2927. device->AuxiliaryEffectSlotMax = 4;
  2928. device->NumAuxSends = MAX_SENDS;
  2929. InitUIntMap(&device->BufferMap, ~0);
  2930. InitUIntMap(&device->EffectMap, ~0);
  2931. InitUIntMap(&device->FilterMap, ~0);
  2932. InitUIntMap(&device->SfontMap, ~0);
  2933. InitUIntMap(&device->PresetMap, ~0);
  2934. InitUIntMap(&device->FontsoundMap, ~0);
  2935. factory = ALCloopbackFactory_getFactory();
  2936. device->Backend = V(factory,createBackend)(device, ALCbackend_Loopback);
  2937. if(!device->Backend)
  2938. {
  2939. al_free(device);
  2940. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2941. return NULL;
  2942. }
  2943. //Set output format
  2944. device->NumUpdates = 0;
  2945. device->UpdateSize = 0;
  2946. device->Frequency = DEFAULT_OUTPUT_RATE;
  2947. device->FmtChans = DevFmtChannelsDefault;
  2948. device->FmtType = DevFmtTypeDefault;
  2949. ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
  2950. if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
  2951. ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
  2952. if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
  2953. ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
  2954. if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
  2955. device->NumStereoSources = 1;
  2956. device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
  2957. device->Synth = SynthCreate(device);
  2958. if(!device->Synth)
  2959. {
  2960. DELETE_OBJ(device->Backend);
  2961. al_free(device);
  2962. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2963. return NULL;
  2964. }
  2965. // Open the "backend"
  2966. V(device->Backend,open)("Loopback");
  2967. do {
  2968. device->next = DeviceList;
  2969. } while(!CompExchangePtr((XchgPtr*)&DeviceList, device->next, device));
  2970. TRACE("Created device %p\n", device);
  2971. return device;
  2972. }
  2973. /* alcIsRenderFormatSupportedSOFT
  2974. *
  2975. * Determines if the loopback device supports the given format for rendering.
  2976. */
  2977. ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type)
  2978. {
  2979. ALCboolean ret = ALC_FALSE;
  2980. if(!(device=VerifyDevice(device)) || device->Type != Loopback)
  2981. alcSetError(device, ALC_INVALID_DEVICE);
  2982. else if(freq <= 0)
  2983. alcSetError(device, ALC_INVALID_VALUE);
  2984. else
  2985. {
  2986. if(IsValidALCType(type) && BytesFromDevFmt(type) > 0 &&
  2987. IsValidALCChannels(channels) && ChannelsFromDevFmt(channels) > 0 &&
  2988. freq >= MIN_OUTPUT_RATE)
  2989. ret = ALC_TRUE;
  2990. }
  2991. if(device) ALCdevice_DecRef(device);
  2992. return ret;
  2993. }
  2994. /* alcRenderSamplesSOFT
  2995. *
  2996. * Renders some samples into a buffer, using the format last set by the
  2997. * attributes given to alcCreateContext.
  2998. */
  2999. FORCE_ALIGN ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
  3000. {
  3001. if(!(device=VerifyDevice(device)) || device->Type != Loopback)
  3002. alcSetError(device, ALC_INVALID_DEVICE);
  3003. else if(samples < 0 || (samples > 0 && buffer == NULL))
  3004. alcSetError(device, ALC_INVALID_VALUE);
  3005. else
  3006. aluMixData(device, buffer, samples);
  3007. if(device) ALCdevice_DecRef(device);
  3008. }
  3009. /************************************************
  3010. * ALC DSP pause/resume functions
  3011. ************************************************/
  3012. /* alcDevicePauseSOFT
  3013. *
  3014. * Pause the DSP to stop audio processing.
  3015. */
  3016. ALC_API void ALC_APIENTRY alcDevicePauseSOFT(ALCdevice *device)
  3017. {
  3018. if(!(device=VerifyDevice(device)) || device->Type != Playback)
  3019. alcSetError(device, ALC_INVALID_DEVICE);
  3020. else
  3021. {
  3022. LockLists();
  3023. if((device->Flags&DEVICE_RUNNING))
  3024. V0(device->Backend,stop)();
  3025. device->Flags &= ~DEVICE_RUNNING;
  3026. device->Flags |= DEVICE_PAUSED;
  3027. UnlockLists();
  3028. }
  3029. if(device) ALCdevice_DecRef(device);
  3030. }
  3031. /* alcDeviceResumeSOFT
  3032. *
  3033. * Resume the DSP to restart audio processing.
  3034. */
  3035. ALC_API void ALC_APIENTRY alcDeviceResumeSOFT(ALCdevice *device)
  3036. {
  3037. if(!(device=VerifyDevice(device)) || device->Type != Playback)
  3038. alcSetError(device, ALC_INVALID_DEVICE);
  3039. else
  3040. {
  3041. LockLists();
  3042. if((device->Flags&DEVICE_PAUSED))
  3043. {
  3044. if(V0(device->Backend,start)() != ALC_FALSE)
  3045. {
  3046. device->Flags |= DEVICE_RUNNING;
  3047. device->Flags &= ~DEVICE_PAUSED;
  3048. }
  3049. else
  3050. {
  3051. alcSetError(device, ALC_INVALID_DEVICE);
  3052. ALCdevice_Lock(device);
  3053. aluHandleDisconnect(device);
  3054. ALCdevice_Unlock(device);
  3055. }
  3056. }
  3057. UnlockLists();
  3058. }
  3059. if(device) ALCdevice_DecRef(device);
  3060. }