ALc.c 112 KB

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