ALc.c 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703
  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 "version.h"
  22. #include <math.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <memory.h>
  26. #include <ctype.h>
  27. #include <signal.h>
  28. #include "alMain.h"
  29. #include "alSource.h"
  30. #include "alListener.h"
  31. #include "alSource.h"
  32. #include "alBuffer.h"
  33. #include "alFilter.h"
  34. #include "alEffect.h"
  35. #include "alAuxEffectSlot.h"
  36. #include "alError.h"
  37. #include "mastering.h"
  38. #include "bformatdec.h"
  39. #include "alu.h"
  40. #include "alconfig.h"
  41. #include "ringbuffer.h"
  42. #include "fpu_modes.h"
  43. #include "cpu_caps.h"
  44. #include "compat.h"
  45. #include "threads.h"
  46. #include "alstring.h"
  47. #include "almalloc.h"
  48. #include "backends/base.h"
  49. /************************************************
  50. * Backends
  51. ************************************************/
  52. struct BackendInfo {
  53. const char *name;
  54. ALCbackendFactory* (*getFactory)(void);
  55. };
  56. static struct BackendInfo BackendList[] = {
  57. #ifdef HAVE_JACK
  58. { "jack", ALCjackBackendFactory_getFactory },
  59. #endif
  60. #ifdef HAVE_PULSEAUDIO
  61. { "pulse", ALCpulseBackendFactory_getFactory },
  62. #endif
  63. #ifdef HAVE_ALSA
  64. { "alsa", ALCalsaBackendFactory_getFactory },
  65. #endif
  66. #ifdef HAVE_COREAUDIO
  67. { "core", ALCcoreAudioBackendFactory_getFactory },
  68. #endif
  69. #ifdef HAVE_SOLARIS
  70. { "solaris", ALCsolarisBackendFactory_getFactory },
  71. #endif
  72. #ifdef HAVE_SNDIO
  73. { "sndio", SndioBackendFactory_getFactory },
  74. #endif
  75. #ifdef HAVE_OSS
  76. { "oss", ALCossBackendFactory_getFactory },
  77. #endif
  78. #ifdef HAVE_QSA
  79. { "qsa", ALCqsaBackendFactory_getFactory },
  80. #endif
  81. #ifdef HAVE_WASAPI
  82. { "wasapi", ALCwasapiBackendFactory_getFactory },
  83. #endif
  84. #ifdef HAVE_DSOUND
  85. { "dsound", ALCdsoundBackendFactory_getFactory },
  86. #endif
  87. #ifdef HAVE_WINMM
  88. { "winmm", ALCwinmmBackendFactory_getFactory },
  89. #endif
  90. #ifdef HAVE_PORTAUDIO
  91. { "port", ALCportBackendFactory_getFactory },
  92. #endif
  93. #ifdef HAVE_OPENSL
  94. { "opensl", ALCopenslBackendFactory_getFactory },
  95. #endif
  96. #ifdef HAVE_SDL2
  97. { "sdl2", ALCsdl2BackendFactory_getFactory },
  98. #endif
  99. { "null", ALCnullBackendFactory_getFactory },
  100. #ifdef HAVE_WAVE
  101. { "wave", ALCwaveBackendFactory_getFactory },
  102. #endif
  103. };
  104. static ALsizei BackendListSize = COUNTOF(BackendList);
  105. #undef EmptyFuncs
  106. static struct BackendInfo PlaybackBackend;
  107. static struct BackendInfo CaptureBackend;
  108. /************************************************
  109. * Functions, enums, and errors
  110. ************************************************/
  111. #define DECL(x) { #x, (ALCvoid*)(x) }
  112. static const struct {
  113. const ALCchar *funcName;
  114. ALCvoid *address;
  115. } alcFunctions[] = {
  116. DECL(alcCreateContext),
  117. DECL(alcMakeContextCurrent),
  118. DECL(alcProcessContext),
  119. DECL(alcSuspendContext),
  120. DECL(alcDestroyContext),
  121. DECL(alcGetCurrentContext),
  122. DECL(alcGetContextsDevice),
  123. DECL(alcOpenDevice),
  124. DECL(alcCloseDevice),
  125. DECL(alcGetError),
  126. DECL(alcIsExtensionPresent),
  127. DECL(alcGetProcAddress),
  128. DECL(alcGetEnumValue),
  129. DECL(alcGetString),
  130. DECL(alcGetIntegerv),
  131. DECL(alcCaptureOpenDevice),
  132. DECL(alcCaptureCloseDevice),
  133. DECL(alcCaptureStart),
  134. DECL(alcCaptureStop),
  135. DECL(alcCaptureSamples),
  136. DECL(alcSetThreadContext),
  137. DECL(alcGetThreadContext),
  138. DECL(alcLoopbackOpenDeviceSOFT),
  139. DECL(alcIsRenderFormatSupportedSOFT),
  140. DECL(alcRenderSamplesSOFT),
  141. DECL(alcDevicePauseSOFT),
  142. DECL(alcDeviceResumeSOFT),
  143. DECL(alcGetStringiSOFT),
  144. DECL(alcResetDeviceSOFT),
  145. DECL(alcGetInteger64vSOFT),
  146. DECL(alEnable),
  147. DECL(alDisable),
  148. DECL(alIsEnabled),
  149. DECL(alGetString),
  150. DECL(alGetBooleanv),
  151. DECL(alGetIntegerv),
  152. DECL(alGetFloatv),
  153. DECL(alGetDoublev),
  154. DECL(alGetBoolean),
  155. DECL(alGetInteger),
  156. DECL(alGetFloat),
  157. DECL(alGetDouble),
  158. DECL(alGetError),
  159. DECL(alIsExtensionPresent),
  160. DECL(alGetProcAddress),
  161. DECL(alGetEnumValue),
  162. DECL(alListenerf),
  163. DECL(alListener3f),
  164. DECL(alListenerfv),
  165. DECL(alListeneri),
  166. DECL(alListener3i),
  167. DECL(alListeneriv),
  168. DECL(alGetListenerf),
  169. DECL(alGetListener3f),
  170. DECL(alGetListenerfv),
  171. DECL(alGetListeneri),
  172. DECL(alGetListener3i),
  173. DECL(alGetListeneriv),
  174. DECL(alGenSources),
  175. DECL(alDeleteSources),
  176. DECL(alIsSource),
  177. DECL(alSourcef),
  178. DECL(alSource3f),
  179. DECL(alSourcefv),
  180. DECL(alSourcei),
  181. DECL(alSource3i),
  182. DECL(alSourceiv),
  183. DECL(alGetSourcef),
  184. DECL(alGetSource3f),
  185. DECL(alGetSourcefv),
  186. DECL(alGetSourcei),
  187. DECL(alGetSource3i),
  188. DECL(alGetSourceiv),
  189. DECL(alSourcePlayv),
  190. DECL(alSourceStopv),
  191. DECL(alSourceRewindv),
  192. DECL(alSourcePausev),
  193. DECL(alSourcePlay),
  194. DECL(alSourceStop),
  195. DECL(alSourceRewind),
  196. DECL(alSourcePause),
  197. DECL(alSourceQueueBuffers),
  198. DECL(alSourceUnqueueBuffers),
  199. DECL(alGenBuffers),
  200. DECL(alDeleteBuffers),
  201. DECL(alIsBuffer),
  202. DECL(alBufferData),
  203. DECL(alBufferf),
  204. DECL(alBuffer3f),
  205. DECL(alBufferfv),
  206. DECL(alBufferi),
  207. DECL(alBuffer3i),
  208. DECL(alBufferiv),
  209. DECL(alGetBufferf),
  210. DECL(alGetBuffer3f),
  211. DECL(alGetBufferfv),
  212. DECL(alGetBufferi),
  213. DECL(alGetBuffer3i),
  214. DECL(alGetBufferiv),
  215. DECL(alDopplerFactor),
  216. DECL(alDopplerVelocity),
  217. DECL(alSpeedOfSound),
  218. DECL(alDistanceModel),
  219. DECL(alGenFilters),
  220. DECL(alDeleteFilters),
  221. DECL(alIsFilter),
  222. DECL(alFilteri),
  223. DECL(alFilteriv),
  224. DECL(alFilterf),
  225. DECL(alFilterfv),
  226. DECL(alGetFilteri),
  227. DECL(alGetFilteriv),
  228. DECL(alGetFilterf),
  229. DECL(alGetFilterfv),
  230. DECL(alGenEffects),
  231. DECL(alDeleteEffects),
  232. DECL(alIsEffect),
  233. DECL(alEffecti),
  234. DECL(alEffectiv),
  235. DECL(alEffectf),
  236. DECL(alEffectfv),
  237. DECL(alGetEffecti),
  238. DECL(alGetEffectiv),
  239. DECL(alGetEffectf),
  240. DECL(alGetEffectfv),
  241. DECL(alGenAuxiliaryEffectSlots),
  242. DECL(alDeleteAuxiliaryEffectSlots),
  243. DECL(alIsAuxiliaryEffectSlot),
  244. DECL(alAuxiliaryEffectSloti),
  245. DECL(alAuxiliaryEffectSlotiv),
  246. DECL(alAuxiliaryEffectSlotf),
  247. DECL(alAuxiliaryEffectSlotfv),
  248. DECL(alGetAuxiliaryEffectSloti),
  249. DECL(alGetAuxiliaryEffectSlotiv),
  250. DECL(alGetAuxiliaryEffectSlotf),
  251. DECL(alGetAuxiliaryEffectSlotfv),
  252. DECL(alDeferUpdatesSOFT),
  253. DECL(alProcessUpdatesSOFT),
  254. DECL(alSourcedSOFT),
  255. DECL(alSource3dSOFT),
  256. DECL(alSourcedvSOFT),
  257. DECL(alGetSourcedSOFT),
  258. DECL(alGetSource3dSOFT),
  259. DECL(alGetSourcedvSOFT),
  260. DECL(alSourcei64SOFT),
  261. DECL(alSource3i64SOFT),
  262. DECL(alSourcei64vSOFT),
  263. DECL(alGetSourcei64SOFT),
  264. DECL(alGetSource3i64SOFT),
  265. DECL(alGetSourcei64vSOFT),
  266. DECL(alGetStringiSOFT),
  267. DECL(alBufferStorageSOFT),
  268. DECL(alMapBufferSOFT),
  269. DECL(alUnmapBufferSOFT),
  270. DECL(alFlushMappedBufferSOFT),
  271. DECL(alEventControlSOFT),
  272. DECL(alEventCallbackSOFT),
  273. DECL(alGetPointerSOFT),
  274. DECL(alGetPointervSOFT),
  275. };
  276. #undef DECL
  277. #define DECL(x) { #x, (x) }
  278. static const struct {
  279. const ALCchar *enumName;
  280. ALCenum value;
  281. } alcEnumerations[] = {
  282. DECL(ALC_INVALID),
  283. DECL(ALC_FALSE),
  284. DECL(ALC_TRUE),
  285. DECL(ALC_MAJOR_VERSION),
  286. DECL(ALC_MINOR_VERSION),
  287. DECL(ALC_ATTRIBUTES_SIZE),
  288. DECL(ALC_ALL_ATTRIBUTES),
  289. DECL(ALC_DEFAULT_DEVICE_SPECIFIER),
  290. DECL(ALC_DEVICE_SPECIFIER),
  291. DECL(ALC_ALL_DEVICES_SPECIFIER),
  292. DECL(ALC_DEFAULT_ALL_DEVICES_SPECIFIER),
  293. DECL(ALC_EXTENSIONS),
  294. DECL(ALC_FREQUENCY),
  295. DECL(ALC_REFRESH),
  296. DECL(ALC_SYNC),
  297. DECL(ALC_MONO_SOURCES),
  298. DECL(ALC_STEREO_SOURCES),
  299. DECL(ALC_CAPTURE_DEVICE_SPECIFIER),
  300. DECL(ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER),
  301. DECL(ALC_CAPTURE_SAMPLES),
  302. DECL(ALC_CONNECTED),
  303. DECL(ALC_EFX_MAJOR_VERSION),
  304. DECL(ALC_EFX_MINOR_VERSION),
  305. DECL(ALC_MAX_AUXILIARY_SENDS),
  306. DECL(ALC_FORMAT_CHANNELS_SOFT),
  307. DECL(ALC_FORMAT_TYPE_SOFT),
  308. DECL(ALC_MONO_SOFT),
  309. DECL(ALC_STEREO_SOFT),
  310. DECL(ALC_QUAD_SOFT),
  311. DECL(ALC_5POINT1_SOFT),
  312. DECL(ALC_6POINT1_SOFT),
  313. DECL(ALC_7POINT1_SOFT),
  314. DECL(ALC_BFORMAT3D_SOFT),
  315. DECL(ALC_BYTE_SOFT),
  316. DECL(ALC_UNSIGNED_BYTE_SOFT),
  317. DECL(ALC_SHORT_SOFT),
  318. DECL(ALC_UNSIGNED_SHORT_SOFT),
  319. DECL(ALC_INT_SOFT),
  320. DECL(ALC_UNSIGNED_INT_SOFT),
  321. DECL(ALC_FLOAT_SOFT),
  322. DECL(ALC_HRTF_SOFT),
  323. DECL(ALC_DONT_CARE_SOFT),
  324. DECL(ALC_HRTF_STATUS_SOFT),
  325. DECL(ALC_HRTF_DISABLED_SOFT),
  326. DECL(ALC_HRTF_ENABLED_SOFT),
  327. DECL(ALC_HRTF_DENIED_SOFT),
  328. DECL(ALC_HRTF_REQUIRED_SOFT),
  329. DECL(ALC_HRTF_HEADPHONES_DETECTED_SOFT),
  330. DECL(ALC_HRTF_UNSUPPORTED_FORMAT_SOFT),
  331. DECL(ALC_NUM_HRTF_SPECIFIERS_SOFT),
  332. DECL(ALC_HRTF_SPECIFIER_SOFT),
  333. DECL(ALC_HRTF_ID_SOFT),
  334. DECL(ALC_AMBISONIC_LAYOUT_SOFT),
  335. DECL(ALC_AMBISONIC_SCALING_SOFT),
  336. DECL(ALC_AMBISONIC_ORDER_SOFT),
  337. DECL(ALC_ACN_SOFT),
  338. DECL(ALC_FUMA_SOFT),
  339. DECL(ALC_N3D_SOFT),
  340. DECL(ALC_SN3D_SOFT),
  341. DECL(ALC_OUTPUT_LIMITER_SOFT),
  342. DECL(ALC_NO_ERROR),
  343. DECL(ALC_INVALID_DEVICE),
  344. DECL(ALC_INVALID_CONTEXT),
  345. DECL(ALC_INVALID_ENUM),
  346. DECL(ALC_INVALID_VALUE),
  347. DECL(ALC_OUT_OF_MEMORY),
  348. DECL(AL_INVALID),
  349. DECL(AL_NONE),
  350. DECL(AL_FALSE),
  351. DECL(AL_TRUE),
  352. DECL(AL_SOURCE_RELATIVE),
  353. DECL(AL_CONE_INNER_ANGLE),
  354. DECL(AL_CONE_OUTER_ANGLE),
  355. DECL(AL_PITCH),
  356. DECL(AL_POSITION),
  357. DECL(AL_DIRECTION),
  358. DECL(AL_VELOCITY),
  359. DECL(AL_LOOPING),
  360. DECL(AL_BUFFER),
  361. DECL(AL_GAIN),
  362. DECL(AL_MIN_GAIN),
  363. DECL(AL_MAX_GAIN),
  364. DECL(AL_ORIENTATION),
  365. DECL(AL_REFERENCE_DISTANCE),
  366. DECL(AL_ROLLOFF_FACTOR),
  367. DECL(AL_CONE_OUTER_GAIN),
  368. DECL(AL_MAX_DISTANCE),
  369. DECL(AL_SEC_OFFSET),
  370. DECL(AL_SAMPLE_OFFSET),
  371. DECL(AL_BYTE_OFFSET),
  372. DECL(AL_SOURCE_TYPE),
  373. DECL(AL_STATIC),
  374. DECL(AL_STREAMING),
  375. DECL(AL_UNDETERMINED),
  376. DECL(AL_METERS_PER_UNIT),
  377. DECL(AL_LOOP_POINTS_SOFT),
  378. DECL(AL_DIRECT_CHANNELS_SOFT),
  379. DECL(AL_DIRECT_FILTER),
  380. DECL(AL_AUXILIARY_SEND_FILTER),
  381. DECL(AL_AIR_ABSORPTION_FACTOR),
  382. DECL(AL_ROOM_ROLLOFF_FACTOR),
  383. DECL(AL_CONE_OUTER_GAINHF),
  384. DECL(AL_DIRECT_FILTER_GAINHF_AUTO),
  385. DECL(AL_AUXILIARY_SEND_FILTER_GAIN_AUTO),
  386. DECL(AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO),
  387. DECL(AL_SOURCE_STATE),
  388. DECL(AL_INITIAL),
  389. DECL(AL_PLAYING),
  390. DECL(AL_PAUSED),
  391. DECL(AL_STOPPED),
  392. DECL(AL_BUFFERS_QUEUED),
  393. DECL(AL_BUFFERS_PROCESSED),
  394. DECL(AL_FORMAT_MONO8),
  395. DECL(AL_FORMAT_MONO16),
  396. DECL(AL_FORMAT_MONO_FLOAT32),
  397. DECL(AL_FORMAT_MONO_DOUBLE_EXT),
  398. DECL(AL_FORMAT_STEREO8),
  399. DECL(AL_FORMAT_STEREO16),
  400. DECL(AL_FORMAT_STEREO_FLOAT32),
  401. DECL(AL_FORMAT_STEREO_DOUBLE_EXT),
  402. DECL(AL_FORMAT_MONO_IMA4),
  403. DECL(AL_FORMAT_STEREO_IMA4),
  404. DECL(AL_FORMAT_MONO_MSADPCM_SOFT),
  405. DECL(AL_FORMAT_STEREO_MSADPCM_SOFT),
  406. DECL(AL_FORMAT_QUAD8_LOKI),
  407. DECL(AL_FORMAT_QUAD16_LOKI),
  408. DECL(AL_FORMAT_QUAD8),
  409. DECL(AL_FORMAT_QUAD16),
  410. DECL(AL_FORMAT_QUAD32),
  411. DECL(AL_FORMAT_51CHN8),
  412. DECL(AL_FORMAT_51CHN16),
  413. DECL(AL_FORMAT_51CHN32),
  414. DECL(AL_FORMAT_61CHN8),
  415. DECL(AL_FORMAT_61CHN16),
  416. DECL(AL_FORMAT_61CHN32),
  417. DECL(AL_FORMAT_71CHN8),
  418. DECL(AL_FORMAT_71CHN16),
  419. DECL(AL_FORMAT_71CHN32),
  420. DECL(AL_FORMAT_REAR8),
  421. DECL(AL_FORMAT_REAR16),
  422. DECL(AL_FORMAT_REAR32),
  423. DECL(AL_FORMAT_MONO_MULAW),
  424. DECL(AL_FORMAT_MONO_MULAW_EXT),
  425. DECL(AL_FORMAT_STEREO_MULAW),
  426. DECL(AL_FORMAT_STEREO_MULAW_EXT),
  427. DECL(AL_FORMAT_QUAD_MULAW),
  428. DECL(AL_FORMAT_51CHN_MULAW),
  429. DECL(AL_FORMAT_61CHN_MULAW),
  430. DECL(AL_FORMAT_71CHN_MULAW),
  431. DECL(AL_FORMAT_REAR_MULAW),
  432. DECL(AL_FORMAT_MONO_ALAW_EXT),
  433. DECL(AL_FORMAT_STEREO_ALAW_EXT),
  434. DECL(AL_FORMAT_BFORMAT2D_8),
  435. DECL(AL_FORMAT_BFORMAT2D_16),
  436. DECL(AL_FORMAT_BFORMAT2D_FLOAT32),
  437. DECL(AL_FORMAT_BFORMAT2D_MULAW),
  438. DECL(AL_FORMAT_BFORMAT3D_8),
  439. DECL(AL_FORMAT_BFORMAT3D_16),
  440. DECL(AL_FORMAT_BFORMAT3D_FLOAT32),
  441. DECL(AL_FORMAT_BFORMAT3D_MULAW),
  442. DECL(AL_FREQUENCY),
  443. DECL(AL_BITS),
  444. DECL(AL_CHANNELS),
  445. DECL(AL_SIZE),
  446. DECL(AL_UNPACK_BLOCK_ALIGNMENT_SOFT),
  447. DECL(AL_PACK_BLOCK_ALIGNMENT_SOFT),
  448. DECL(AL_SOURCE_RADIUS),
  449. DECL(AL_STEREO_ANGLES),
  450. DECL(AL_UNUSED),
  451. DECL(AL_PENDING),
  452. DECL(AL_PROCESSED),
  453. DECL(AL_NO_ERROR),
  454. DECL(AL_INVALID_NAME),
  455. DECL(AL_INVALID_ENUM),
  456. DECL(AL_INVALID_VALUE),
  457. DECL(AL_INVALID_OPERATION),
  458. DECL(AL_OUT_OF_MEMORY),
  459. DECL(AL_VENDOR),
  460. DECL(AL_VERSION),
  461. DECL(AL_RENDERER),
  462. DECL(AL_EXTENSIONS),
  463. DECL(AL_DOPPLER_FACTOR),
  464. DECL(AL_DOPPLER_VELOCITY),
  465. DECL(AL_DISTANCE_MODEL),
  466. DECL(AL_SPEED_OF_SOUND),
  467. DECL(AL_SOURCE_DISTANCE_MODEL),
  468. DECL(AL_DEFERRED_UPDATES_SOFT),
  469. DECL(AL_GAIN_LIMIT_SOFT),
  470. DECL(AL_INVERSE_DISTANCE),
  471. DECL(AL_INVERSE_DISTANCE_CLAMPED),
  472. DECL(AL_LINEAR_DISTANCE),
  473. DECL(AL_LINEAR_DISTANCE_CLAMPED),
  474. DECL(AL_EXPONENT_DISTANCE),
  475. DECL(AL_EXPONENT_DISTANCE_CLAMPED),
  476. DECL(AL_FILTER_TYPE),
  477. DECL(AL_FILTER_NULL),
  478. DECL(AL_FILTER_LOWPASS),
  479. DECL(AL_FILTER_HIGHPASS),
  480. DECL(AL_FILTER_BANDPASS),
  481. DECL(AL_LOWPASS_GAIN),
  482. DECL(AL_LOWPASS_GAINHF),
  483. DECL(AL_HIGHPASS_GAIN),
  484. DECL(AL_HIGHPASS_GAINLF),
  485. DECL(AL_BANDPASS_GAIN),
  486. DECL(AL_BANDPASS_GAINHF),
  487. DECL(AL_BANDPASS_GAINLF),
  488. DECL(AL_EFFECT_TYPE),
  489. DECL(AL_EFFECT_NULL),
  490. DECL(AL_EFFECT_REVERB),
  491. DECL(AL_EFFECT_EAXREVERB),
  492. DECL(AL_EFFECT_CHORUS),
  493. DECL(AL_EFFECT_DISTORTION),
  494. DECL(AL_EFFECT_ECHO),
  495. DECL(AL_EFFECT_FLANGER),
  496. DECL(AL_EFFECT_PITCH_SHIFTER),
  497. DECL(AL_EFFECT_FREQUENCY_SHIFTER),
  498. #if 0
  499. DECL(AL_EFFECT_VOCAL_MORPHER),
  500. #endif
  501. DECL(AL_EFFECT_RING_MODULATOR),
  502. DECL(AL_EFFECT_AUTOWAH),
  503. DECL(AL_EFFECT_COMPRESSOR),
  504. DECL(AL_EFFECT_EQUALIZER),
  505. DECL(AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT),
  506. DECL(AL_EFFECT_DEDICATED_DIALOGUE),
  507. DECL(AL_EFFECTSLOT_EFFECT),
  508. DECL(AL_EFFECTSLOT_GAIN),
  509. DECL(AL_EFFECTSLOT_AUXILIARY_SEND_AUTO),
  510. DECL(AL_EFFECTSLOT_NULL),
  511. DECL(AL_EAXREVERB_DENSITY),
  512. DECL(AL_EAXREVERB_DIFFUSION),
  513. DECL(AL_EAXREVERB_GAIN),
  514. DECL(AL_EAXREVERB_GAINHF),
  515. DECL(AL_EAXREVERB_GAINLF),
  516. DECL(AL_EAXREVERB_DECAY_TIME),
  517. DECL(AL_EAXREVERB_DECAY_HFRATIO),
  518. DECL(AL_EAXREVERB_DECAY_LFRATIO),
  519. DECL(AL_EAXREVERB_REFLECTIONS_GAIN),
  520. DECL(AL_EAXREVERB_REFLECTIONS_DELAY),
  521. DECL(AL_EAXREVERB_REFLECTIONS_PAN),
  522. DECL(AL_EAXREVERB_LATE_REVERB_GAIN),
  523. DECL(AL_EAXREVERB_LATE_REVERB_DELAY),
  524. DECL(AL_EAXREVERB_LATE_REVERB_PAN),
  525. DECL(AL_EAXREVERB_ECHO_TIME),
  526. DECL(AL_EAXREVERB_ECHO_DEPTH),
  527. DECL(AL_EAXREVERB_MODULATION_TIME),
  528. DECL(AL_EAXREVERB_MODULATION_DEPTH),
  529. DECL(AL_EAXREVERB_AIR_ABSORPTION_GAINHF),
  530. DECL(AL_EAXREVERB_HFREFERENCE),
  531. DECL(AL_EAXREVERB_LFREFERENCE),
  532. DECL(AL_EAXREVERB_ROOM_ROLLOFF_FACTOR),
  533. DECL(AL_EAXREVERB_DECAY_HFLIMIT),
  534. DECL(AL_REVERB_DENSITY),
  535. DECL(AL_REVERB_DIFFUSION),
  536. DECL(AL_REVERB_GAIN),
  537. DECL(AL_REVERB_GAINHF),
  538. DECL(AL_REVERB_DECAY_TIME),
  539. DECL(AL_REVERB_DECAY_HFRATIO),
  540. DECL(AL_REVERB_REFLECTIONS_GAIN),
  541. DECL(AL_REVERB_REFLECTIONS_DELAY),
  542. DECL(AL_REVERB_LATE_REVERB_GAIN),
  543. DECL(AL_REVERB_LATE_REVERB_DELAY),
  544. DECL(AL_REVERB_AIR_ABSORPTION_GAINHF),
  545. DECL(AL_REVERB_ROOM_ROLLOFF_FACTOR),
  546. DECL(AL_REVERB_DECAY_HFLIMIT),
  547. DECL(AL_CHORUS_WAVEFORM),
  548. DECL(AL_CHORUS_PHASE),
  549. DECL(AL_CHORUS_RATE),
  550. DECL(AL_CHORUS_DEPTH),
  551. DECL(AL_CHORUS_FEEDBACK),
  552. DECL(AL_CHORUS_DELAY),
  553. DECL(AL_DISTORTION_EDGE),
  554. DECL(AL_DISTORTION_GAIN),
  555. DECL(AL_DISTORTION_LOWPASS_CUTOFF),
  556. DECL(AL_DISTORTION_EQCENTER),
  557. DECL(AL_DISTORTION_EQBANDWIDTH),
  558. DECL(AL_ECHO_DELAY),
  559. DECL(AL_ECHO_LRDELAY),
  560. DECL(AL_ECHO_DAMPING),
  561. DECL(AL_ECHO_FEEDBACK),
  562. DECL(AL_ECHO_SPREAD),
  563. DECL(AL_FLANGER_WAVEFORM),
  564. DECL(AL_FLANGER_PHASE),
  565. DECL(AL_FLANGER_RATE),
  566. DECL(AL_FLANGER_DEPTH),
  567. DECL(AL_FLANGER_FEEDBACK),
  568. DECL(AL_FLANGER_DELAY),
  569. DECL(AL_FREQUENCY_SHIFTER_FREQUENCY),
  570. DECL(AL_FREQUENCY_SHIFTER_LEFT_DIRECTION),
  571. DECL(AL_FREQUENCY_SHIFTER_RIGHT_DIRECTION),
  572. DECL(AL_RING_MODULATOR_FREQUENCY),
  573. DECL(AL_RING_MODULATOR_HIGHPASS_CUTOFF),
  574. DECL(AL_RING_MODULATOR_WAVEFORM),
  575. DECL(AL_PITCH_SHIFTER_COARSE_TUNE),
  576. DECL(AL_PITCH_SHIFTER_FINE_TUNE),
  577. DECL(AL_COMPRESSOR_ONOFF),
  578. DECL(AL_EQUALIZER_LOW_GAIN),
  579. DECL(AL_EQUALIZER_LOW_CUTOFF),
  580. DECL(AL_EQUALIZER_MID1_GAIN),
  581. DECL(AL_EQUALIZER_MID1_CENTER),
  582. DECL(AL_EQUALIZER_MID1_WIDTH),
  583. DECL(AL_EQUALIZER_MID2_GAIN),
  584. DECL(AL_EQUALIZER_MID2_CENTER),
  585. DECL(AL_EQUALIZER_MID2_WIDTH),
  586. DECL(AL_EQUALIZER_HIGH_GAIN),
  587. DECL(AL_EQUALIZER_HIGH_CUTOFF),
  588. DECL(AL_DEDICATED_GAIN),
  589. DECL(AL_AUTOWAH_ATTACK_TIME),
  590. DECL(AL_AUTOWAH_RELEASE_TIME),
  591. DECL(AL_AUTOWAH_RESONANCE),
  592. DECL(AL_AUTOWAH_PEAK_GAIN),
  593. DECL(AL_NUM_RESAMPLERS_SOFT),
  594. DECL(AL_DEFAULT_RESAMPLER_SOFT),
  595. DECL(AL_SOURCE_RESAMPLER_SOFT),
  596. DECL(AL_RESAMPLER_NAME_SOFT),
  597. DECL(AL_SOURCE_SPATIALIZE_SOFT),
  598. DECL(AL_AUTO_SOFT),
  599. DECL(AL_MAP_READ_BIT_SOFT),
  600. DECL(AL_MAP_WRITE_BIT_SOFT),
  601. DECL(AL_MAP_PERSISTENT_BIT_SOFT),
  602. DECL(AL_PRESERVE_DATA_BIT_SOFT),
  603. DECL(AL_EVENT_CALLBACK_FUNCTION_SOFT),
  604. DECL(AL_EVENT_CALLBACK_USER_PARAM_SOFT),
  605. DECL(AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT),
  606. DECL(AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT),
  607. DECL(AL_EVENT_TYPE_ERROR_SOFT),
  608. DECL(AL_EVENT_TYPE_PERFORMANCE_SOFT),
  609. DECL(AL_EVENT_TYPE_DEPRECATED_SOFT),
  610. };
  611. #undef DECL
  612. static const ALCchar alcNoError[] = "No Error";
  613. static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
  614. static const ALCchar alcErrInvalidContext[] = "Invalid Context";
  615. static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
  616. static const ALCchar alcErrInvalidValue[] = "Invalid Value";
  617. static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
  618. /************************************************
  619. * Global variables
  620. ************************************************/
  621. /* Enumerated device names */
  622. static const ALCchar alcDefaultName[] = "OpenAL Soft\0";
  623. static al_string alcAllDevicesList;
  624. static al_string alcCaptureDeviceList;
  625. /* Default is always the first in the list */
  626. static ALCchar *alcDefaultAllDevicesSpecifier;
  627. static ALCchar *alcCaptureDefaultDeviceSpecifier;
  628. /* Default context extensions */
  629. static const ALchar alExtList[] =
  630. "AL_EXT_ALAW "
  631. "AL_EXT_BFORMAT "
  632. "AL_EXT_DOUBLE "
  633. "AL_EXT_EXPONENT_DISTANCE "
  634. "AL_EXT_FLOAT32 "
  635. "AL_EXT_IMA4 "
  636. "AL_EXT_LINEAR_DISTANCE "
  637. "AL_EXT_MCFORMATS "
  638. "AL_EXT_MULAW "
  639. "AL_EXT_MULAW_BFORMAT "
  640. "AL_EXT_MULAW_MCFORMATS "
  641. "AL_EXT_OFFSET "
  642. "AL_EXT_source_distance_model "
  643. "AL_EXT_SOURCE_RADIUS "
  644. "AL_EXT_STEREO_ANGLES "
  645. "AL_LOKI_quadriphonic "
  646. "AL_SOFT_block_alignment "
  647. "AL_SOFT_deferred_updates "
  648. "AL_SOFT_direct_channels "
  649. "AL_SOFTX_events "
  650. "AL_SOFTX_filter_gain_ex "
  651. "AL_SOFT_gain_clamp_ex "
  652. "AL_SOFT_loop_points "
  653. "AL_SOFTX_map_buffer "
  654. "AL_SOFT_MSADPCM "
  655. "AL_SOFT_source_latency "
  656. "AL_SOFT_source_length "
  657. "AL_SOFT_source_resampler "
  658. "AL_SOFT_source_spatialize";
  659. static ATOMIC(ALCenum) LastNullDeviceError = ATOMIC_INIT_STATIC(ALC_NO_ERROR);
  660. /* Thread-local current context */
  661. static altss_t LocalContext;
  662. /* Process-wide current context */
  663. static ATOMIC(ALCcontext*) GlobalContext = ATOMIC_INIT_STATIC(NULL);
  664. /* Mixing thread piority level */
  665. ALint RTPrioLevel;
  666. FILE *LogFile;
  667. #ifdef _DEBUG
  668. enum LogLevel LogLevel = LogWarning;
  669. #else
  670. enum LogLevel LogLevel = LogError;
  671. #endif
  672. /* Flag to trap ALC device errors */
  673. static ALCboolean TrapALCError = ALC_FALSE;
  674. /* One-time configuration init control */
  675. static alonce_flag alc_config_once = AL_ONCE_FLAG_INIT;
  676. /* Default effect that applies to sources that don't have an effect on send 0 */
  677. static ALeffect DefaultEffect;
  678. /* Flag to specify if alcSuspendContext/alcProcessContext should defer/process
  679. * updates.
  680. */
  681. static ALCboolean SuspendDefers = ALC_TRUE;
  682. /************************************************
  683. * ALC information
  684. ************************************************/
  685. static const ALCchar alcNoDeviceExtList[] =
  686. "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
  687. "ALC_EXT_thread_local_context ALC_SOFT_loopback";
  688. static const ALCchar alcExtensionList[] =
  689. "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
  690. "ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX "
  691. "ALC_EXT_thread_local_context ALC_SOFT_device_clock ALC_SOFT_HRTF "
  692. "ALC_SOFT_loopback ALC_SOFT_output_limiter ALC_SOFT_pause_device";
  693. static const ALCint alcMajorVersion = 1;
  694. static const ALCint alcMinorVersion = 1;
  695. static const ALCint alcEFXMajorVersion = 1;
  696. static const ALCint alcEFXMinorVersion = 0;
  697. /************************************************
  698. * Device lists
  699. ************************************************/
  700. static ATOMIC(ALCdevice*) DeviceList = ATOMIC_INIT_STATIC(NULL);
  701. static almtx_t ListLock;
  702. static inline void LockLists(void)
  703. {
  704. int ret = almtx_lock(&ListLock);
  705. assert(ret == althrd_success);
  706. }
  707. static inline void UnlockLists(void)
  708. {
  709. int ret = almtx_unlock(&ListLock);
  710. assert(ret == althrd_success);
  711. }
  712. /************************************************
  713. * Library initialization
  714. ************************************************/
  715. #if defined(_WIN32)
  716. static void alc_init(void);
  717. static void alc_deinit(void);
  718. static void alc_deinit_safe(void);
  719. #ifndef AL_LIBTYPE_STATIC
  720. BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD reason, LPVOID lpReserved)
  721. {
  722. switch(reason)
  723. {
  724. case DLL_PROCESS_ATTACH:
  725. /* Pin the DLL so we won't get unloaded until the process terminates */
  726. GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
  727. (WCHAR*)hModule, &hModule);
  728. alc_init();
  729. break;
  730. case DLL_THREAD_DETACH:
  731. althrd_thread_detach();
  732. break;
  733. case DLL_PROCESS_DETACH:
  734. if(!lpReserved)
  735. alc_deinit();
  736. else
  737. alc_deinit_safe();
  738. break;
  739. }
  740. return TRUE;
  741. }
  742. #elif defined(_MSC_VER)
  743. #pragma section(".CRT$XCU",read)
  744. static void alc_constructor(void);
  745. static void alc_destructor(void);
  746. __declspec(allocate(".CRT$XCU")) void (__cdecl* alc_constructor_)(void) = alc_constructor;
  747. static void alc_constructor(void)
  748. {
  749. atexit(alc_destructor);
  750. alc_init();
  751. }
  752. static void alc_destructor(void)
  753. {
  754. alc_deinit();
  755. }
  756. #elif defined(HAVE_GCC_DESTRUCTOR)
  757. static void alc_init(void) __attribute__((constructor));
  758. static void alc_deinit(void) __attribute__((destructor));
  759. #else
  760. #error "No static initialization available on this platform!"
  761. #endif
  762. #elif defined(HAVE_GCC_DESTRUCTOR)
  763. static void alc_init(void) __attribute__((constructor));
  764. static void alc_deinit(void) __attribute__((destructor));
  765. #else
  766. #error "No global initialization available on this platform!"
  767. #endif
  768. static void ReleaseThreadCtx(void *ptr);
  769. static void alc_init(void)
  770. {
  771. const char *str;
  772. int ret;
  773. LogFile = stderr;
  774. AL_STRING_INIT(alcAllDevicesList);
  775. AL_STRING_INIT(alcCaptureDeviceList);
  776. str = getenv("__ALSOFT_HALF_ANGLE_CONES");
  777. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  778. ConeScale *= 0.5f;
  779. str = getenv("__ALSOFT_REVERSE_Z");
  780. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  781. ZScale *= -1.0f;
  782. str = getenv("__ALSOFT_REVERB_IGNORES_SOUND_SPEED");
  783. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  784. OverrideReverbSpeedOfSound = AL_TRUE;
  785. ret = altss_create(&LocalContext, ReleaseThreadCtx);
  786. assert(ret == althrd_success);
  787. ret = almtx_init(&ListLock, almtx_recursive);
  788. assert(ret == althrd_success);
  789. }
  790. static void alc_initconfig(void)
  791. {
  792. const char *devs, *str;
  793. int capfilter;
  794. float valf;
  795. int i, n;
  796. str = getenv("ALSOFT_LOGLEVEL");
  797. if(str)
  798. {
  799. long lvl = strtol(str, NULL, 0);
  800. if(lvl >= NoLog && lvl <= LogRef)
  801. LogLevel = lvl;
  802. }
  803. str = getenv("ALSOFT_LOGFILE");
  804. if(str && str[0])
  805. {
  806. FILE *logfile = al_fopen(str, "wt");
  807. if(logfile) LogFile = logfile;
  808. else ERR("Failed to open log file '%s'\n", str);
  809. }
  810. TRACE("Initializing library v%s-%s %s\n", ALSOFT_VERSION,
  811. ALSOFT_GIT_COMMIT_HASH, ALSOFT_GIT_BRANCH);
  812. {
  813. char buf[1024] = "";
  814. int len = 0;
  815. if(BackendListSize > 0)
  816. len += snprintf(buf, sizeof(buf), "%s", BackendList[0].name);
  817. for(i = 1;i < BackendListSize;i++)
  818. len += snprintf(buf+len, sizeof(buf)-len, ", %s", BackendList[i].name);
  819. TRACE("Supported backends: %s\n", buf);
  820. }
  821. ReadALConfig();
  822. str = getenv("__ALSOFT_SUSPEND_CONTEXT");
  823. if(str && *str)
  824. {
  825. if(strcasecmp(str, "ignore") == 0)
  826. {
  827. SuspendDefers = ALC_FALSE;
  828. TRACE("Selected context suspend behavior, \"ignore\"\n");
  829. }
  830. else
  831. ERR("Unhandled context suspend behavior setting: \"%s\"\n", str);
  832. }
  833. capfilter = 0;
  834. #if defined(HAVE_SSE4_1)
  835. capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3 | CPU_CAP_SSE4_1;
  836. #elif defined(HAVE_SSE3)
  837. capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3;
  838. #elif defined(HAVE_SSE2)
  839. capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2;
  840. #elif defined(HAVE_SSE)
  841. capfilter |= CPU_CAP_SSE;
  842. #endif
  843. #ifdef HAVE_NEON
  844. capfilter |= CPU_CAP_NEON;
  845. #endif
  846. if(ConfigValueStr(NULL, NULL, "disable-cpu-exts", &str))
  847. {
  848. if(strcasecmp(str, "all") == 0)
  849. capfilter = 0;
  850. else
  851. {
  852. size_t len;
  853. const char *next = str;
  854. do {
  855. str = next;
  856. while(isspace(str[0]))
  857. str++;
  858. next = strchr(str, ',');
  859. if(!str[0] || str[0] == ',')
  860. continue;
  861. len = (next ? ((size_t)(next-str)) : strlen(str));
  862. while(len > 0 && isspace(str[len-1]))
  863. len--;
  864. if(len == 3 && strncasecmp(str, "sse", len) == 0)
  865. capfilter &= ~CPU_CAP_SSE;
  866. else if(len == 4 && strncasecmp(str, "sse2", len) == 0)
  867. capfilter &= ~CPU_CAP_SSE2;
  868. else if(len == 4 && strncasecmp(str, "sse3", len) == 0)
  869. capfilter &= ~CPU_CAP_SSE3;
  870. else if(len == 6 && strncasecmp(str, "sse4.1", len) == 0)
  871. capfilter &= ~CPU_CAP_SSE4_1;
  872. else if(len == 4 && strncasecmp(str, "neon", len) == 0)
  873. capfilter &= ~CPU_CAP_NEON;
  874. else
  875. WARN("Invalid CPU extension \"%s\"\n", str);
  876. } while(next++);
  877. }
  878. }
  879. FillCPUCaps(capfilter);
  880. #ifdef _WIN32
  881. RTPrioLevel = 1;
  882. #else
  883. RTPrioLevel = 0;
  884. #endif
  885. ConfigValueInt(NULL, NULL, "rt-prio", &RTPrioLevel);
  886. aluInit();
  887. aluInitMixer();
  888. str = getenv("ALSOFT_TRAP_ERROR");
  889. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  890. {
  891. TrapALError = AL_TRUE;
  892. TrapALCError = AL_TRUE;
  893. }
  894. else
  895. {
  896. str = getenv("ALSOFT_TRAP_AL_ERROR");
  897. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  898. TrapALError = AL_TRUE;
  899. TrapALError = GetConfigValueBool(NULL, NULL, "trap-al-error", TrapALError);
  900. str = getenv("ALSOFT_TRAP_ALC_ERROR");
  901. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  902. TrapALCError = ALC_TRUE;
  903. TrapALCError = GetConfigValueBool(NULL, NULL, "trap-alc-error", TrapALCError);
  904. }
  905. if(ConfigValueFloat(NULL, "reverb", "boost", &valf))
  906. ReverbBoost *= powf(10.0f, valf / 20.0f);
  907. if(((devs=getenv("ALSOFT_DRIVERS")) && devs[0]) ||
  908. ConfigValueStr(NULL, NULL, "drivers", &devs))
  909. {
  910. int n;
  911. size_t len;
  912. const char *next = devs;
  913. int endlist, delitem;
  914. i = 0;
  915. do {
  916. devs = next;
  917. while(isspace(devs[0]))
  918. devs++;
  919. next = strchr(devs, ',');
  920. delitem = (devs[0] == '-');
  921. if(devs[0] == '-') devs++;
  922. if(!devs[0] || devs[0] == ',')
  923. {
  924. endlist = 0;
  925. continue;
  926. }
  927. endlist = 1;
  928. len = (next ? ((size_t)(next-devs)) : strlen(devs));
  929. while(len > 0 && isspace(devs[len-1]))
  930. len--;
  931. #ifdef HAVE_WASAPI
  932. /* HACK: For backwards compatibility, convert backend references of
  933. * mmdevapi to wasapi. This should eventually be removed.
  934. */
  935. if(len == 8 && strncmp(devs, "mmdevapi", len) == 0)
  936. {
  937. devs = "wasapi";
  938. len = 6;
  939. }
  940. #endif
  941. for(n = i;n < BackendListSize;n++)
  942. {
  943. if(len == strlen(BackendList[n].name) &&
  944. strncmp(BackendList[n].name, devs, len) == 0)
  945. {
  946. if(delitem)
  947. {
  948. for(;n+1 < BackendListSize;n++)
  949. BackendList[n] = BackendList[n+1];
  950. BackendListSize--;
  951. }
  952. else
  953. {
  954. struct BackendInfo Bkp = BackendList[n];
  955. for(;n > i;n--)
  956. BackendList[n] = BackendList[n-1];
  957. BackendList[n] = Bkp;
  958. i++;
  959. }
  960. break;
  961. }
  962. }
  963. } while(next++);
  964. if(endlist)
  965. BackendListSize = i;
  966. }
  967. for(n = i = 0;i < BackendListSize && (!PlaybackBackend.name || !CaptureBackend.name);i++)
  968. {
  969. ALCbackendFactory *factory;
  970. BackendList[n] = BackendList[i];
  971. factory = BackendList[n].getFactory();
  972. if(!V0(factory,init)())
  973. {
  974. WARN("Failed to initialize backend \"%s\"\n", BackendList[n].name);
  975. continue;
  976. }
  977. TRACE("Initialized backend \"%s\"\n", BackendList[n].name);
  978. if(!PlaybackBackend.name && V(factory,querySupport)(ALCbackend_Playback))
  979. {
  980. PlaybackBackend = BackendList[n];
  981. TRACE("Added \"%s\" for playback\n", PlaybackBackend.name);
  982. }
  983. if(!CaptureBackend.name && V(factory,querySupport)(ALCbackend_Capture))
  984. {
  985. CaptureBackend = BackendList[n];
  986. TRACE("Added \"%s\" for capture\n", CaptureBackend.name);
  987. }
  988. n++;
  989. }
  990. BackendListSize = n;
  991. {
  992. ALCbackendFactory *factory = ALCloopbackFactory_getFactory();
  993. V0(factory,init)();
  994. }
  995. if(!PlaybackBackend.name)
  996. WARN("No playback backend available!\n");
  997. if(!CaptureBackend.name)
  998. WARN("No capture backend available!\n");
  999. if(ConfigValueStr(NULL, NULL, "excludefx", &str))
  1000. {
  1001. size_t len;
  1002. const char *next = str;
  1003. do {
  1004. str = next;
  1005. next = strchr(str, ',');
  1006. if(!str[0] || next == str)
  1007. continue;
  1008. len = (next ? ((size_t)(next-str)) : strlen(str));
  1009. for(n = 0;n < EFFECTLIST_SIZE;n++)
  1010. {
  1011. if(len == strlen(EffectList[n].name) &&
  1012. strncmp(EffectList[n].name, str, len) == 0)
  1013. DisabledEffects[EffectList[n].type] = AL_TRUE;
  1014. }
  1015. } while(next++);
  1016. }
  1017. InitEffect(&DefaultEffect);
  1018. str = getenv("ALSOFT_DEFAULT_REVERB");
  1019. if((str && str[0]) || ConfigValueStr(NULL, NULL, "default-reverb", &str))
  1020. LoadReverbPreset(str, &DefaultEffect);
  1021. }
  1022. #define DO_INITCONFIG() alcall_once(&alc_config_once, alc_initconfig)
  1023. /************************************************
  1024. * Library deinitialization
  1025. ************************************************/
  1026. static void alc_cleanup(void)
  1027. {
  1028. ALCdevice *dev;
  1029. AL_STRING_DEINIT(alcAllDevicesList);
  1030. AL_STRING_DEINIT(alcCaptureDeviceList);
  1031. free(alcDefaultAllDevicesSpecifier);
  1032. alcDefaultAllDevicesSpecifier = NULL;
  1033. free(alcCaptureDefaultDeviceSpecifier);
  1034. alcCaptureDefaultDeviceSpecifier = NULL;
  1035. if((dev=ATOMIC_EXCHANGE_PTR_SEQ(&DeviceList, NULL)) != NULL)
  1036. {
  1037. ALCuint num = 0;
  1038. do {
  1039. num++;
  1040. dev = ATOMIC_LOAD(&dev->next, almemory_order_relaxed);
  1041. } while(dev != NULL);
  1042. ERR("%u device%s not closed\n", num, (num>1)?"s":"");
  1043. }
  1044. }
  1045. static void alc_deinit_safe(void)
  1046. {
  1047. alc_cleanup();
  1048. FreeHrtfs();
  1049. FreeALConfig();
  1050. almtx_destroy(&ListLock);
  1051. altss_delete(LocalContext);
  1052. if(LogFile != stderr)
  1053. fclose(LogFile);
  1054. LogFile = NULL;
  1055. althrd_deinit();
  1056. }
  1057. static void alc_deinit(void)
  1058. {
  1059. int i;
  1060. alc_cleanup();
  1061. memset(&PlaybackBackend, 0, sizeof(PlaybackBackend));
  1062. memset(&CaptureBackend, 0, sizeof(CaptureBackend));
  1063. for(i = 0;i < BackendListSize;i++)
  1064. {
  1065. ALCbackendFactory *factory = BackendList[i].getFactory();
  1066. V0(factory,deinit)();
  1067. }
  1068. {
  1069. ALCbackendFactory *factory = ALCloopbackFactory_getFactory();
  1070. V0(factory,deinit)();
  1071. }
  1072. alc_deinit_safe();
  1073. }
  1074. /************************************************
  1075. * Device enumeration
  1076. ************************************************/
  1077. static void ProbeDevices(al_string *list, struct BackendInfo *backendinfo, enum DevProbe type)
  1078. {
  1079. DO_INITCONFIG();
  1080. LockLists();
  1081. alstr_clear(list);
  1082. if(backendinfo->getFactory)
  1083. {
  1084. ALCbackendFactory *factory = backendinfo->getFactory();
  1085. V(factory,probe)(type, list);
  1086. }
  1087. UnlockLists();
  1088. }
  1089. static void ProbeAllDevicesList(void)
  1090. { ProbeDevices(&alcAllDevicesList, &PlaybackBackend, ALL_DEVICE_PROBE); }
  1091. static void ProbeCaptureDeviceList(void)
  1092. { ProbeDevices(&alcCaptureDeviceList, &CaptureBackend, CAPTURE_DEVICE_PROBE); }
  1093. /************************************************
  1094. * Device format information
  1095. ************************************************/
  1096. const ALCchar *DevFmtTypeString(enum DevFmtType type)
  1097. {
  1098. switch(type)
  1099. {
  1100. case DevFmtByte: return "Signed Byte";
  1101. case DevFmtUByte: return "Unsigned Byte";
  1102. case DevFmtShort: return "Signed Short";
  1103. case DevFmtUShort: return "Unsigned Short";
  1104. case DevFmtInt: return "Signed Int";
  1105. case DevFmtUInt: return "Unsigned Int";
  1106. case DevFmtFloat: return "Float";
  1107. }
  1108. return "(unknown type)";
  1109. }
  1110. const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans)
  1111. {
  1112. switch(chans)
  1113. {
  1114. case DevFmtMono: return "Mono";
  1115. case DevFmtStereo: return "Stereo";
  1116. case DevFmtQuad: return "Quadraphonic";
  1117. case DevFmtX51: return "5.1 Surround";
  1118. case DevFmtX51Rear: return "5.1 Surround (Rear)";
  1119. case DevFmtX61: return "6.1 Surround";
  1120. case DevFmtX71: return "7.1 Surround";
  1121. case DevFmtAmbi3D: return "Ambisonic 3D";
  1122. }
  1123. return "(unknown channels)";
  1124. }
  1125. extern inline ALsizei FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type, ALsizei ambiorder);
  1126. ALsizei BytesFromDevFmt(enum DevFmtType type)
  1127. {
  1128. switch(type)
  1129. {
  1130. case DevFmtByte: return sizeof(ALbyte);
  1131. case DevFmtUByte: return sizeof(ALubyte);
  1132. case DevFmtShort: return sizeof(ALshort);
  1133. case DevFmtUShort: return sizeof(ALushort);
  1134. case DevFmtInt: return sizeof(ALint);
  1135. case DevFmtUInt: return sizeof(ALuint);
  1136. case DevFmtFloat: return sizeof(ALfloat);
  1137. }
  1138. return 0;
  1139. }
  1140. ALsizei ChannelsFromDevFmt(enum DevFmtChannels chans, ALsizei ambiorder)
  1141. {
  1142. switch(chans)
  1143. {
  1144. case DevFmtMono: return 1;
  1145. case DevFmtStereo: return 2;
  1146. case DevFmtQuad: return 4;
  1147. case DevFmtX51: return 6;
  1148. case DevFmtX51Rear: return 6;
  1149. case DevFmtX61: return 7;
  1150. case DevFmtX71: return 8;
  1151. case DevFmtAmbi3D: return (ambiorder >= 3) ? 16 :
  1152. (ambiorder == 2) ? 9 :
  1153. (ambiorder == 1) ? 4 : 1;
  1154. }
  1155. return 0;
  1156. }
  1157. static ALboolean DecomposeDevFormat(ALenum format, enum DevFmtChannels *chans,
  1158. enum DevFmtType *type)
  1159. {
  1160. static const struct {
  1161. ALenum format;
  1162. enum DevFmtChannels channels;
  1163. enum DevFmtType type;
  1164. } list[] = {
  1165. { AL_FORMAT_MONO8, DevFmtMono, DevFmtUByte },
  1166. { AL_FORMAT_MONO16, DevFmtMono, DevFmtShort },
  1167. { AL_FORMAT_MONO_FLOAT32, DevFmtMono, DevFmtFloat },
  1168. { AL_FORMAT_STEREO8, DevFmtStereo, DevFmtUByte },
  1169. { AL_FORMAT_STEREO16, DevFmtStereo, DevFmtShort },
  1170. { AL_FORMAT_STEREO_FLOAT32, DevFmtStereo, DevFmtFloat },
  1171. { AL_FORMAT_QUAD8, DevFmtQuad, DevFmtUByte },
  1172. { AL_FORMAT_QUAD16, DevFmtQuad, DevFmtShort },
  1173. { AL_FORMAT_QUAD32, DevFmtQuad, DevFmtFloat },
  1174. { AL_FORMAT_51CHN8, DevFmtX51, DevFmtUByte },
  1175. { AL_FORMAT_51CHN16, DevFmtX51, DevFmtShort },
  1176. { AL_FORMAT_51CHN32, DevFmtX51, DevFmtFloat },
  1177. { AL_FORMAT_61CHN8, DevFmtX61, DevFmtUByte },
  1178. { AL_FORMAT_61CHN16, DevFmtX61, DevFmtShort },
  1179. { AL_FORMAT_61CHN32, DevFmtX61, DevFmtFloat },
  1180. { AL_FORMAT_71CHN8, DevFmtX71, DevFmtUByte },
  1181. { AL_FORMAT_71CHN16, DevFmtX71, DevFmtShort },
  1182. { AL_FORMAT_71CHN32, DevFmtX71, DevFmtFloat },
  1183. };
  1184. ALuint i;
  1185. for(i = 0;i < COUNTOF(list);i++)
  1186. {
  1187. if(list[i].format == format)
  1188. {
  1189. *chans = list[i].channels;
  1190. *type = list[i].type;
  1191. return AL_TRUE;
  1192. }
  1193. }
  1194. return AL_FALSE;
  1195. }
  1196. static ALCboolean IsValidALCType(ALCenum type)
  1197. {
  1198. switch(type)
  1199. {
  1200. case ALC_BYTE_SOFT:
  1201. case ALC_UNSIGNED_BYTE_SOFT:
  1202. case ALC_SHORT_SOFT:
  1203. case ALC_UNSIGNED_SHORT_SOFT:
  1204. case ALC_INT_SOFT:
  1205. case ALC_UNSIGNED_INT_SOFT:
  1206. case ALC_FLOAT_SOFT:
  1207. return ALC_TRUE;
  1208. }
  1209. return ALC_FALSE;
  1210. }
  1211. static ALCboolean IsValidALCChannels(ALCenum channels)
  1212. {
  1213. switch(channels)
  1214. {
  1215. case ALC_MONO_SOFT:
  1216. case ALC_STEREO_SOFT:
  1217. case ALC_QUAD_SOFT:
  1218. case ALC_5POINT1_SOFT:
  1219. case ALC_6POINT1_SOFT:
  1220. case ALC_7POINT1_SOFT:
  1221. case ALC_BFORMAT3D_SOFT:
  1222. return ALC_TRUE;
  1223. }
  1224. return ALC_FALSE;
  1225. }
  1226. static ALCboolean IsValidAmbiLayout(ALCenum layout)
  1227. {
  1228. switch(layout)
  1229. {
  1230. case ALC_ACN_SOFT:
  1231. case ALC_FUMA_SOFT:
  1232. return ALC_TRUE;
  1233. }
  1234. return ALC_FALSE;
  1235. }
  1236. static ALCboolean IsValidAmbiScaling(ALCenum scaling)
  1237. {
  1238. switch(scaling)
  1239. {
  1240. case ALC_N3D_SOFT:
  1241. case ALC_SN3D_SOFT:
  1242. case ALC_FUMA_SOFT:
  1243. return ALC_TRUE;
  1244. }
  1245. return ALC_FALSE;
  1246. }
  1247. /************************************************
  1248. * Miscellaneous ALC helpers
  1249. ************************************************/
  1250. /* SetDefaultWFXChannelOrder
  1251. *
  1252. * Sets the default channel order used by WaveFormatEx.
  1253. */
  1254. void SetDefaultWFXChannelOrder(ALCdevice *device)
  1255. {
  1256. ALsizei i;
  1257. for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
  1258. device->RealOut.ChannelName[i] = InvalidChannel;
  1259. switch(device->FmtChans)
  1260. {
  1261. case DevFmtMono:
  1262. device->RealOut.ChannelName[0] = FrontCenter;
  1263. break;
  1264. case DevFmtStereo:
  1265. device->RealOut.ChannelName[0] = FrontLeft;
  1266. device->RealOut.ChannelName[1] = FrontRight;
  1267. break;
  1268. case DevFmtQuad:
  1269. device->RealOut.ChannelName[0] = FrontLeft;
  1270. device->RealOut.ChannelName[1] = FrontRight;
  1271. device->RealOut.ChannelName[2] = BackLeft;
  1272. device->RealOut.ChannelName[3] = BackRight;
  1273. break;
  1274. case DevFmtX51:
  1275. device->RealOut.ChannelName[0] = FrontLeft;
  1276. device->RealOut.ChannelName[1] = FrontRight;
  1277. device->RealOut.ChannelName[2] = FrontCenter;
  1278. device->RealOut.ChannelName[3] = LFE;
  1279. device->RealOut.ChannelName[4] = SideLeft;
  1280. device->RealOut.ChannelName[5] = SideRight;
  1281. break;
  1282. case DevFmtX51Rear:
  1283. device->RealOut.ChannelName[0] = FrontLeft;
  1284. device->RealOut.ChannelName[1] = FrontRight;
  1285. device->RealOut.ChannelName[2] = FrontCenter;
  1286. device->RealOut.ChannelName[3] = LFE;
  1287. device->RealOut.ChannelName[4] = BackLeft;
  1288. device->RealOut.ChannelName[5] = BackRight;
  1289. break;
  1290. case DevFmtX61:
  1291. device->RealOut.ChannelName[0] = FrontLeft;
  1292. device->RealOut.ChannelName[1] = FrontRight;
  1293. device->RealOut.ChannelName[2] = FrontCenter;
  1294. device->RealOut.ChannelName[3] = LFE;
  1295. device->RealOut.ChannelName[4] = BackCenter;
  1296. device->RealOut.ChannelName[5] = SideLeft;
  1297. device->RealOut.ChannelName[6] = SideRight;
  1298. break;
  1299. case DevFmtX71:
  1300. device->RealOut.ChannelName[0] = FrontLeft;
  1301. device->RealOut.ChannelName[1] = FrontRight;
  1302. device->RealOut.ChannelName[2] = FrontCenter;
  1303. device->RealOut.ChannelName[3] = LFE;
  1304. device->RealOut.ChannelName[4] = BackLeft;
  1305. device->RealOut.ChannelName[5] = BackRight;
  1306. device->RealOut.ChannelName[6] = SideLeft;
  1307. device->RealOut.ChannelName[7] = SideRight;
  1308. break;
  1309. case DevFmtAmbi3D:
  1310. device->RealOut.ChannelName[0] = Aux0;
  1311. if(device->AmbiOrder > 0)
  1312. {
  1313. device->RealOut.ChannelName[1] = Aux1;
  1314. device->RealOut.ChannelName[2] = Aux2;
  1315. device->RealOut.ChannelName[3] = Aux3;
  1316. }
  1317. if(device->AmbiOrder > 1)
  1318. {
  1319. device->RealOut.ChannelName[4] = Aux4;
  1320. device->RealOut.ChannelName[5] = Aux5;
  1321. device->RealOut.ChannelName[6] = Aux6;
  1322. device->RealOut.ChannelName[7] = Aux7;
  1323. device->RealOut.ChannelName[8] = Aux8;
  1324. }
  1325. if(device->AmbiOrder > 2)
  1326. {
  1327. device->RealOut.ChannelName[9] = Aux9;
  1328. device->RealOut.ChannelName[10] = Aux10;
  1329. device->RealOut.ChannelName[11] = Aux11;
  1330. device->RealOut.ChannelName[12] = Aux12;
  1331. device->RealOut.ChannelName[13] = Aux13;
  1332. device->RealOut.ChannelName[14] = Aux14;
  1333. device->RealOut.ChannelName[15] = Aux15;
  1334. }
  1335. break;
  1336. }
  1337. }
  1338. /* SetDefaultChannelOrder
  1339. *
  1340. * Sets the default channel order used by most non-WaveFormatEx-based APIs.
  1341. */
  1342. void SetDefaultChannelOrder(ALCdevice *device)
  1343. {
  1344. ALsizei i;
  1345. for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
  1346. device->RealOut.ChannelName[i] = InvalidChannel;
  1347. switch(device->FmtChans)
  1348. {
  1349. case DevFmtX51Rear:
  1350. device->RealOut.ChannelName[0] = FrontLeft;
  1351. device->RealOut.ChannelName[1] = FrontRight;
  1352. device->RealOut.ChannelName[2] = BackLeft;
  1353. device->RealOut.ChannelName[3] = BackRight;
  1354. device->RealOut.ChannelName[4] = FrontCenter;
  1355. device->RealOut.ChannelName[5] = LFE;
  1356. return;
  1357. case DevFmtX71:
  1358. device->RealOut.ChannelName[0] = FrontLeft;
  1359. device->RealOut.ChannelName[1] = FrontRight;
  1360. device->RealOut.ChannelName[2] = BackLeft;
  1361. device->RealOut.ChannelName[3] = BackRight;
  1362. device->RealOut.ChannelName[4] = FrontCenter;
  1363. device->RealOut.ChannelName[5] = LFE;
  1364. device->RealOut.ChannelName[6] = SideLeft;
  1365. device->RealOut.ChannelName[7] = SideRight;
  1366. return;
  1367. /* Same as WFX order */
  1368. case DevFmtMono:
  1369. case DevFmtStereo:
  1370. case DevFmtQuad:
  1371. case DevFmtX51:
  1372. case DevFmtX61:
  1373. case DevFmtAmbi3D:
  1374. SetDefaultWFXChannelOrder(device);
  1375. break;
  1376. }
  1377. }
  1378. extern inline ALint GetChannelIndex(const enum Channel names[MAX_OUTPUT_CHANNELS], enum Channel chan);
  1379. extern inline ALint GetChannelIdxByName(const RealMixParams *real, enum Channel chan);
  1380. /* ALCcontext_DeferUpdates
  1381. *
  1382. * Defers/suspends updates for the given context's listener and sources. This
  1383. * does *NOT* stop mixing, but rather prevents certain property changes from
  1384. * taking effect.
  1385. */
  1386. void ALCcontext_DeferUpdates(ALCcontext *context)
  1387. {
  1388. ATOMIC_STORE_SEQ(&context->DeferUpdates, AL_TRUE);
  1389. }
  1390. /* ALCcontext_ProcessUpdates
  1391. *
  1392. * Resumes update processing after being deferred.
  1393. */
  1394. void ALCcontext_ProcessUpdates(ALCcontext *context)
  1395. {
  1396. almtx_lock(&context->PropLock);
  1397. if(ATOMIC_EXCHANGE_SEQ(&context->DeferUpdates, AL_FALSE))
  1398. {
  1399. /* Tell the mixer to stop applying updates, then wait for any active
  1400. * updating to finish, before providing updates.
  1401. */
  1402. ATOMIC_STORE_SEQ(&context->HoldUpdates, AL_TRUE);
  1403. while((ATOMIC_LOAD(&context->UpdateCount, almemory_order_acquire)&1) != 0)
  1404. althrd_yield();
  1405. if(!ATOMIC_FLAG_TEST_AND_SET(&context->PropsClean, almemory_order_acq_rel))
  1406. UpdateContextProps(context);
  1407. if(!ATOMIC_FLAG_TEST_AND_SET(&context->Listener->PropsClean, almemory_order_acq_rel))
  1408. UpdateListenerProps(context);
  1409. UpdateAllEffectSlotProps(context);
  1410. UpdateAllSourceProps(context);
  1411. /* Now with all updates declared, let the mixer continue applying them
  1412. * so they all happen at once.
  1413. */
  1414. ATOMIC_STORE_SEQ(&context->HoldUpdates, AL_FALSE);
  1415. }
  1416. almtx_unlock(&context->PropLock);
  1417. }
  1418. /* alcSetError
  1419. *
  1420. * Stores the latest ALC device error
  1421. */
  1422. static void alcSetError(ALCdevice *device, ALCenum errorCode)
  1423. {
  1424. WARN("Error generated on device %p, code 0x%04x\n", device, errorCode);
  1425. if(TrapALCError)
  1426. {
  1427. #ifdef _WIN32
  1428. /* DebugBreak() will cause an exception if there is no debugger */
  1429. if(IsDebuggerPresent())
  1430. DebugBreak();
  1431. #elif defined(SIGTRAP)
  1432. raise(SIGTRAP);
  1433. #endif
  1434. }
  1435. if(device)
  1436. ATOMIC_STORE_SEQ(&device->LastError, errorCode);
  1437. else
  1438. ATOMIC_STORE_SEQ(&LastNullDeviceError, errorCode);
  1439. }
  1440. static struct Compressor *CreateDeviceLimiter(const ALCdevice *device, const ALfloat threshold)
  1441. {
  1442. return CompressorInit(device->RealOut.NumChannels, device->Frequency,
  1443. AL_TRUE, AL_TRUE, AL_TRUE, AL_TRUE, AL_TRUE, 0.001f, 0.002f,
  1444. 0.0f, 0.0f, threshold, INFINITY, 0.0f, 0.020f, 0.200f);
  1445. }
  1446. /* UpdateClockBase
  1447. *
  1448. * Updates the device's base clock time with however many samples have been
  1449. * done. This is used so frequency changes on the device don't cause the time
  1450. * to jump forward or back. Must not be called while the device is running/
  1451. * mixing.
  1452. */
  1453. static inline void UpdateClockBase(ALCdevice *device)
  1454. {
  1455. IncrementRef(&device->MixCount);
  1456. device->ClockBase += device->SamplesDone * DEVICE_CLOCK_RES / device->Frequency;
  1457. device->SamplesDone = 0;
  1458. IncrementRef(&device->MixCount);
  1459. }
  1460. /* UpdateDeviceParams
  1461. *
  1462. * Updates device parameters according to the attribute list (caller is
  1463. * responsible for holding the list lock).
  1464. */
  1465. static ALCenum UpdateDeviceParams(ALCdevice *device, const ALCint *attrList)
  1466. {
  1467. enum HrtfRequestMode hrtf_userreq = Hrtf_Default;
  1468. enum HrtfRequestMode hrtf_appreq = Hrtf_Default;
  1469. ALCenum gainLimiter = device->LimiterState;
  1470. const ALsizei old_sends = device->NumAuxSends;
  1471. ALsizei new_sends = device->NumAuxSends;
  1472. enum DevFmtChannels oldChans;
  1473. enum DevFmtType oldType;
  1474. ALboolean update_failed;
  1475. ALCsizei hrtf_id = -1;
  1476. ALCcontext *context;
  1477. ALCuint oldFreq;
  1478. size_t size;
  1479. ALCsizei i;
  1480. int val;
  1481. // Check for attributes
  1482. if(device->Type == Loopback)
  1483. {
  1484. ALCsizei numMono, numStereo, numSends;
  1485. ALCenum alayout = AL_NONE;
  1486. ALCenum ascale = AL_NONE;
  1487. ALCenum schans = AL_NONE;
  1488. ALCenum stype = AL_NONE;
  1489. ALCsizei attrIdx = 0;
  1490. ALCsizei aorder = 0;
  1491. ALCuint freq = 0;
  1492. if(!attrList)
  1493. {
  1494. WARN("Missing attributes for loopback device\n");
  1495. return ALC_INVALID_VALUE;
  1496. }
  1497. numMono = device->NumMonoSources;
  1498. numStereo = device->NumStereoSources;
  1499. numSends = old_sends;
  1500. #define TRACE_ATTR(a, v) TRACE("Loopback %s = %d\n", #a, v)
  1501. while(attrList[attrIdx])
  1502. {
  1503. switch(attrList[attrIdx])
  1504. {
  1505. case ALC_FORMAT_CHANNELS_SOFT:
  1506. schans = attrList[attrIdx + 1];
  1507. TRACE_ATTR(ALC_FORMAT_CHANNELS_SOFT, schans);
  1508. if(!IsValidALCChannels(schans))
  1509. return ALC_INVALID_VALUE;
  1510. break;
  1511. case ALC_FORMAT_TYPE_SOFT:
  1512. stype = attrList[attrIdx + 1];
  1513. TRACE_ATTR(ALC_FORMAT_TYPE_SOFT, stype);
  1514. if(!IsValidALCType(stype))
  1515. return ALC_INVALID_VALUE;
  1516. break;
  1517. case ALC_FREQUENCY:
  1518. freq = attrList[attrIdx + 1];
  1519. TRACE_ATTR(ALC_FREQUENCY, freq);
  1520. if(freq < MIN_OUTPUT_RATE)
  1521. return ALC_INVALID_VALUE;
  1522. break;
  1523. case ALC_AMBISONIC_LAYOUT_SOFT:
  1524. alayout = attrList[attrIdx + 1];
  1525. TRACE_ATTR(ALC_AMBISONIC_LAYOUT_SOFT, alayout);
  1526. if(!IsValidAmbiLayout(alayout))
  1527. return ALC_INVALID_VALUE;
  1528. break;
  1529. case ALC_AMBISONIC_SCALING_SOFT:
  1530. ascale = attrList[attrIdx + 1];
  1531. TRACE_ATTR(ALC_AMBISONIC_SCALING_SOFT, ascale);
  1532. if(!IsValidAmbiScaling(ascale))
  1533. return ALC_INVALID_VALUE;
  1534. break;
  1535. case ALC_AMBISONIC_ORDER_SOFT:
  1536. aorder = attrList[attrIdx + 1];
  1537. TRACE_ATTR(ALC_AMBISONIC_ORDER_SOFT, aorder);
  1538. if(aorder < 1 || aorder > MAX_AMBI_ORDER)
  1539. return ALC_INVALID_VALUE;
  1540. break;
  1541. case ALC_MONO_SOURCES:
  1542. numMono = attrList[attrIdx + 1];
  1543. TRACE_ATTR(ALC_MONO_SOURCES, numMono);
  1544. numMono = maxi(numMono, 0);
  1545. break;
  1546. case ALC_STEREO_SOURCES:
  1547. numStereo = attrList[attrIdx + 1];
  1548. TRACE_ATTR(ALC_STEREO_SOURCES, numStereo);
  1549. numStereo = maxi(numStereo, 0);
  1550. break;
  1551. case ALC_MAX_AUXILIARY_SENDS:
  1552. numSends = attrList[attrIdx + 1];
  1553. TRACE_ATTR(ALC_MAX_AUXILIARY_SENDS, numSends);
  1554. numSends = clampi(numSends, 0, MAX_SENDS);
  1555. break;
  1556. case ALC_HRTF_SOFT:
  1557. TRACE_ATTR(ALC_HRTF_SOFT, attrList[attrIdx + 1]);
  1558. if(attrList[attrIdx + 1] == ALC_FALSE)
  1559. hrtf_appreq = Hrtf_Disable;
  1560. else if(attrList[attrIdx + 1] == ALC_TRUE)
  1561. hrtf_appreq = Hrtf_Enable;
  1562. else
  1563. hrtf_appreq = Hrtf_Default;
  1564. break;
  1565. case ALC_HRTF_ID_SOFT:
  1566. hrtf_id = attrList[attrIdx + 1];
  1567. TRACE_ATTR(ALC_HRTF_ID_SOFT, hrtf_id);
  1568. break;
  1569. case ALC_OUTPUT_LIMITER_SOFT:
  1570. gainLimiter = attrList[attrIdx + 1];
  1571. TRACE_ATTR(ALC_OUTPUT_LIMITER_SOFT, gainLimiter);
  1572. break;
  1573. default:
  1574. TRACE("Loopback 0x%04X = %d (0x%x)\n", attrList[attrIdx],
  1575. attrList[attrIdx + 1], attrList[attrIdx + 1]);
  1576. break;
  1577. }
  1578. attrIdx += 2;
  1579. }
  1580. #undef TRACE_ATTR
  1581. if(!schans || !stype || !freq)
  1582. {
  1583. WARN("Missing format for loopback device\n");
  1584. return ALC_INVALID_VALUE;
  1585. }
  1586. if(schans == ALC_BFORMAT3D_SOFT && (!alayout || !ascale || !aorder))
  1587. {
  1588. WARN("Missing ambisonic info for loopback device\n");
  1589. return ALC_INVALID_VALUE;
  1590. }
  1591. if((device->Flags&DEVICE_RUNNING))
  1592. V0(device->Backend,stop)();
  1593. device->Flags &= ~DEVICE_RUNNING;
  1594. UpdateClockBase(device);
  1595. device->Frequency = freq;
  1596. device->FmtChans = schans;
  1597. device->FmtType = stype;
  1598. if(schans == ALC_BFORMAT3D_SOFT)
  1599. {
  1600. device->AmbiOrder = aorder;
  1601. device->AmbiLayout = alayout;
  1602. device->AmbiScale = ascale;
  1603. }
  1604. if(numMono > INT_MAX-numStereo)
  1605. numMono = INT_MAX-numStereo;
  1606. numMono += numStereo;
  1607. if(ConfigValueInt(NULL, NULL, "sources", &numMono))
  1608. {
  1609. if(numMono <= 0)
  1610. numMono = 256;
  1611. }
  1612. else
  1613. numMono = maxi(numMono, 256);
  1614. numStereo = mini(numStereo, numMono);
  1615. numMono -= numStereo;
  1616. device->SourcesMax = numMono + numStereo;
  1617. device->NumMonoSources = numMono;
  1618. device->NumStereoSources = numStereo;
  1619. if(ConfigValueInt(NULL, NULL, "sends", &new_sends))
  1620. new_sends = mini(numSends, clampi(new_sends, 0, MAX_SENDS));
  1621. else
  1622. new_sends = numSends;
  1623. }
  1624. else if(attrList && attrList[0])
  1625. {
  1626. ALCsizei numMono, numStereo, numSends;
  1627. ALCsizei attrIdx = 0;
  1628. ALCuint freq;
  1629. /* If a context is already running on the device, stop playback so the
  1630. * device attributes can be updated. */
  1631. if((device->Flags&DEVICE_RUNNING))
  1632. V0(device->Backend,stop)();
  1633. device->Flags &= ~DEVICE_RUNNING;
  1634. UpdateClockBase(device);
  1635. freq = device->Frequency;
  1636. numMono = device->NumMonoSources;
  1637. numStereo = device->NumStereoSources;
  1638. numSends = old_sends;
  1639. #define TRACE_ATTR(a, v) TRACE("%s = %d\n", #a, v)
  1640. while(attrList[attrIdx])
  1641. {
  1642. switch(attrList[attrIdx])
  1643. {
  1644. case ALC_FREQUENCY:
  1645. freq = attrList[attrIdx + 1];
  1646. TRACE_ATTR(ALC_FREQUENCY, freq);
  1647. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  1648. break;
  1649. case ALC_MONO_SOURCES:
  1650. numMono = attrList[attrIdx + 1];
  1651. TRACE_ATTR(ALC_MONO_SOURCES, numMono);
  1652. numMono = maxi(numMono, 0);
  1653. break;
  1654. case ALC_STEREO_SOURCES:
  1655. numStereo = attrList[attrIdx + 1];
  1656. TRACE_ATTR(ALC_STEREO_SOURCES, numStereo);
  1657. numStereo = maxi(numStereo, 0);
  1658. break;
  1659. case ALC_MAX_AUXILIARY_SENDS:
  1660. numSends = attrList[attrIdx + 1];
  1661. TRACE_ATTR(ALC_MAX_AUXILIARY_SENDS, numSends);
  1662. numSends = clampi(numSends, 0, MAX_SENDS);
  1663. break;
  1664. case ALC_HRTF_SOFT:
  1665. TRACE_ATTR(ALC_HRTF_SOFT, attrList[attrIdx + 1]);
  1666. if(attrList[attrIdx + 1] == ALC_FALSE)
  1667. hrtf_appreq = Hrtf_Disable;
  1668. else if(attrList[attrIdx + 1] == ALC_TRUE)
  1669. hrtf_appreq = Hrtf_Enable;
  1670. else
  1671. hrtf_appreq = Hrtf_Default;
  1672. break;
  1673. case ALC_HRTF_ID_SOFT:
  1674. hrtf_id = attrList[attrIdx + 1];
  1675. TRACE_ATTR(ALC_HRTF_ID_SOFT, hrtf_id);
  1676. break;
  1677. case ALC_OUTPUT_LIMITER_SOFT:
  1678. gainLimiter = attrList[attrIdx + 1];
  1679. TRACE_ATTR(ALC_OUTPUT_LIMITER_SOFT, gainLimiter);
  1680. break;
  1681. default:
  1682. TRACE("0x%04X = %d (0x%x)\n", attrList[attrIdx],
  1683. attrList[attrIdx + 1], attrList[attrIdx + 1]);
  1684. break;
  1685. }
  1686. attrIdx += 2;
  1687. }
  1688. #undef TRACE_ATTR
  1689. ConfigValueUInt(alstr_get_cstr(device->DeviceName), NULL, "frequency", &freq);
  1690. freq = maxu(freq, MIN_OUTPUT_RATE);
  1691. device->UpdateSize = (ALuint64)device->UpdateSize * freq /
  1692. device->Frequency;
  1693. /* SSE and Neon do best with the update size being a multiple of 4 */
  1694. if((CPUCapFlags&(CPU_CAP_SSE|CPU_CAP_NEON)) != 0)
  1695. device->UpdateSize = (device->UpdateSize+3)&~3;
  1696. device->Frequency = freq;
  1697. if(numMono > INT_MAX-numStereo)
  1698. numMono = INT_MAX-numStereo;
  1699. numMono += numStereo;
  1700. if(ConfigValueInt(alstr_get_cstr(device->DeviceName), NULL, "sources", &numMono))
  1701. {
  1702. if(numMono <= 0)
  1703. numMono = 256;
  1704. }
  1705. else
  1706. numMono = maxi(numMono, 256);
  1707. numStereo = mini(numStereo, numMono);
  1708. numMono -= numStereo;
  1709. device->SourcesMax = numMono + numStereo;
  1710. device->NumMonoSources = numMono;
  1711. device->NumStereoSources = numStereo;
  1712. if(ConfigValueInt(alstr_get_cstr(device->DeviceName), NULL, "sends", &new_sends))
  1713. new_sends = mini(numSends, clampi(new_sends, 0, MAX_SENDS));
  1714. else
  1715. new_sends = numSends;
  1716. }
  1717. if((device->Flags&DEVICE_RUNNING))
  1718. return ALC_NO_ERROR;
  1719. al_free(device->Uhj_Encoder);
  1720. device->Uhj_Encoder = NULL;
  1721. al_free(device->Bs2b);
  1722. device->Bs2b = NULL;
  1723. al_free(device->ChannelDelay[0].Buffer);
  1724. for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
  1725. {
  1726. device->ChannelDelay[i].Length = 0;
  1727. device->ChannelDelay[i].Buffer = NULL;
  1728. }
  1729. al_free(device->Dry.Buffer);
  1730. device->Dry.Buffer = NULL;
  1731. device->Dry.NumChannels = 0;
  1732. device->FOAOut.Buffer = NULL;
  1733. device->FOAOut.NumChannels = 0;
  1734. device->RealOut.Buffer = NULL;
  1735. device->RealOut.NumChannels = 0;
  1736. UpdateClockBase(device);
  1737. device->FixedLatency = 0;
  1738. device->DitherSeed = DITHER_RNG_SEED;
  1739. /*************************************************************************
  1740. * Update device format request if HRTF is requested
  1741. */
  1742. device->HrtfStatus = ALC_HRTF_DISABLED_SOFT;
  1743. if(device->Type != Loopback)
  1744. {
  1745. const char *hrtf;
  1746. if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "hrtf", &hrtf))
  1747. {
  1748. if(strcasecmp(hrtf, "true") == 0)
  1749. hrtf_userreq = Hrtf_Enable;
  1750. else if(strcasecmp(hrtf, "false") == 0)
  1751. hrtf_userreq = Hrtf_Disable;
  1752. else if(strcasecmp(hrtf, "auto") != 0)
  1753. ERR("Unexpected hrtf value: %s\n", hrtf);
  1754. }
  1755. if(hrtf_userreq == Hrtf_Enable || (hrtf_userreq != Hrtf_Disable && hrtf_appreq == Hrtf_Enable))
  1756. {
  1757. struct Hrtf *hrtf = NULL;
  1758. if(VECTOR_SIZE(device->HrtfList) == 0)
  1759. {
  1760. VECTOR_DEINIT(device->HrtfList);
  1761. device->HrtfList = EnumerateHrtf(device->DeviceName);
  1762. }
  1763. if(VECTOR_SIZE(device->HrtfList) > 0)
  1764. {
  1765. if(hrtf_id >= 0 && (size_t)hrtf_id < VECTOR_SIZE(device->HrtfList))
  1766. hrtf = GetLoadedHrtf(VECTOR_ELEM(device->HrtfList, hrtf_id).hrtf);
  1767. else
  1768. hrtf = GetLoadedHrtf(VECTOR_ELEM(device->HrtfList, 0).hrtf);
  1769. }
  1770. if(hrtf)
  1771. {
  1772. device->FmtChans = DevFmtStereo;
  1773. device->Frequency = hrtf->sampleRate;
  1774. device->Flags |= DEVICE_CHANNELS_REQUEST | DEVICE_FREQUENCY_REQUEST;
  1775. if(device->HrtfHandle)
  1776. Hrtf_DecRef(device->HrtfHandle);
  1777. device->HrtfHandle = hrtf;
  1778. }
  1779. else
  1780. {
  1781. hrtf_userreq = Hrtf_Default;
  1782. hrtf_appreq = Hrtf_Disable;
  1783. device->HrtfStatus = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT;
  1784. }
  1785. }
  1786. }
  1787. oldFreq = device->Frequency;
  1788. oldChans = device->FmtChans;
  1789. oldType = device->FmtType;
  1790. TRACE("Pre-reset: %s%s, %s%s, %s%uhz, %u update size x%d\n",
  1791. (device->Flags&DEVICE_CHANNELS_REQUEST)?"*":"", DevFmtChannelsString(device->FmtChans),
  1792. (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST)?"*":"", DevFmtTypeString(device->FmtType),
  1793. (device->Flags&DEVICE_FREQUENCY_REQUEST)?"*":"", device->Frequency,
  1794. device->UpdateSize, device->NumUpdates
  1795. );
  1796. if(V0(device->Backend,reset)() == ALC_FALSE)
  1797. return ALC_INVALID_DEVICE;
  1798. if(device->FmtChans != oldChans && (device->Flags&DEVICE_CHANNELS_REQUEST))
  1799. {
  1800. ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans),
  1801. DevFmtChannelsString(device->FmtChans));
  1802. device->Flags &= ~DEVICE_CHANNELS_REQUEST;
  1803. }
  1804. if(device->FmtType != oldType && (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
  1805. {
  1806. ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType),
  1807. DevFmtTypeString(device->FmtType));
  1808. device->Flags &= ~DEVICE_SAMPLE_TYPE_REQUEST;
  1809. }
  1810. if(device->Frequency != oldFreq && (device->Flags&DEVICE_FREQUENCY_REQUEST))
  1811. {
  1812. ERR("Failed to set %uhz, got %uhz instead\n", oldFreq, device->Frequency);
  1813. device->Flags &= ~DEVICE_FREQUENCY_REQUEST;
  1814. }
  1815. if((device->UpdateSize&3) != 0)
  1816. {
  1817. if((CPUCapFlags&CPU_CAP_SSE))
  1818. WARN("SSE performs best with multiple of 4 update sizes (%u)\n", device->UpdateSize);
  1819. if((CPUCapFlags&CPU_CAP_NEON))
  1820. WARN("NEON performs best with multiple of 4 update sizes (%u)\n", device->UpdateSize);
  1821. }
  1822. TRACE("Post-reset: %s, %s, %uhz, %u update size x%d\n",
  1823. DevFmtChannelsString(device->FmtChans), DevFmtTypeString(device->FmtType),
  1824. device->Frequency, device->UpdateSize, device->NumUpdates
  1825. );
  1826. aluInitRenderer(device, hrtf_id, hrtf_appreq, hrtf_userreq);
  1827. TRACE("Channel config, Dry: %d, FOA: %d, Real: %d\n", device->Dry.NumChannels,
  1828. device->FOAOut.NumChannels, device->RealOut.NumChannels);
  1829. /* Allocate extra channels for any post-filter output. */
  1830. size = (device->Dry.NumChannels + device->FOAOut.NumChannels +
  1831. device->RealOut.NumChannels)*sizeof(device->Dry.Buffer[0]);
  1832. TRACE("Allocating "SZFMT" channels, "SZFMT" bytes\n", size/sizeof(device->Dry.Buffer[0]), size);
  1833. device->Dry.Buffer = al_calloc(16, size);
  1834. if(!device->Dry.Buffer)
  1835. {
  1836. ERR("Failed to allocate "SZFMT" bytes for mix buffer\n", size);
  1837. return ALC_INVALID_DEVICE;
  1838. }
  1839. if(device->RealOut.NumChannels != 0)
  1840. device->RealOut.Buffer = device->Dry.Buffer + device->Dry.NumChannels +
  1841. device->FOAOut.NumChannels;
  1842. else
  1843. {
  1844. device->RealOut.Buffer = device->Dry.Buffer;
  1845. device->RealOut.NumChannels = device->Dry.NumChannels;
  1846. }
  1847. if(device->FOAOut.NumChannels != 0)
  1848. device->FOAOut.Buffer = device->Dry.Buffer + device->Dry.NumChannels;
  1849. else
  1850. {
  1851. device->FOAOut.Buffer = device->Dry.Buffer;
  1852. device->FOAOut.NumChannels = device->Dry.NumChannels;
  1853. }
  1854. device->NumAuxSends = new_sends;
  1855. TRACE("Max sources: %d (%d + %d), effect slots: %d, sends: %d\n",
  1856. device->SourcesMax, device->NumMonoSources, device->NumStereoSources,
  1857. device->AuxiliaryEffectSlotMax, device->NumAuxSends);
  1858. device->DitherDepth = 0.0f;
  1859. if(GetConfigValueBool(alstr_get_cstr(device->DeviceName), NULL, "dither", 1))
  1860. {
  1861. ALint depth = 0;
  1862. ConfigValueInt(alstr_get_cstr(device->DeviceName), NULL, "dither-depth", &depth);
  1863. if(depth <= 0)
  1864. {
  1865. switch(device->FmtType)
  1866. {
  1867. case DevFmtByte:
  1868. case DevFmtUByte:
  1869. depth = 8;
  1870. break;
  1871. case DevFmtShort:
  1872. case DevFmtUShort:
  1873. depth = 16;
  1874. break;
  1875. case DevFmtInt:
  1876. case DevFmtUInt:
  1877. case DevFmtFloat:
  1878. break;
  1879. }
  1880. }
  1881. if(depth > 0)
  1882. {
  1883. depth = clampi(depth, 2, 24);
  1884. device->DitherDepth = powf(2.0f, (ALfloat)(depth-1));
  1885. }
  1886. }
  1887. if(!(device->DitherDepth > 0.0f))
  1888. TRACE("Dithering disabled\n");
  1889. else
  1890. TRACE("Dithering enabled (%g-bit, %g)\n", log2f(device->DitherDepth)+1.0f,
  1891. device->DitherDepth);
  1892. device->LimiterState = gainLimiter;
  1893. if(ConfigValueBool(alstr_get_cstr(device->DeviceName), NULL, "output-limiter", &val))
  1894. gainLimiter = val ? ALC_TRUE : ALC_FALSE;
  1895. /* Valid values for gainLimiter are ALC_DONT_CARE_SOFT, ALC_TRUE, and
  1896. * ALC_FALSE. For ALC_DONT_CARE_SOFT, use the limiter for integer-based
  1897. * output (where samples must be clamped), and don't for floating-point
  1898. * (which can take unclamped samples).
  1899. */
  1900. if(gainLimiter == ALC_DONT_CARE_SOFT)
  1901. {
  1902. switch(device->FmtType)
  1903. {
  1904. case DevFmtByte:
  1905. case DevFmtUByte:
  1906. case DevFmtShort:
  1907. case DevFmtUShort:
  1908. case DevFmtInt:
  1909. case DevFmtUInt:
  1910. gainLimiter = ALC_TRUE;
  1911. break;
  1912. case DevFmtFloat:
  1913. gainLimiter = ALC_FALSE;
  1914. break;
  1915. }
  1916. }
  1917. if(gainLimiter != ALC_FALSE)
  1918. {
  1919. ALfloat thrshld = 1.0f;
  1920. switch(device->FmtType)
  1921. {
  1922. case DevFmtByte:
  1923. case DevFmtUByte:
  1924. thrshld = 127.0f / 128.0f;
  1925. break;
  1926. case DevFmtShort:
  1927. case DevFmtUShort:
  1928. thrshld = 32767.0f / 32768.0f;
  1929. break;
  1930. case DevFmtInt:
  1931. case DevFmtUInt:
  1932. case DevFmtFloat:
  1933. break;
  1934. }
  1935. if(device->DitherDepth > 0.0f)
  1936. thrshld -= 1.0f / device->DitherDepth;
  1937. al_free(device->Limiter);
  1938. device->Limiter = CreateDeviceLimiter(device, log10f(thrshld) * 20.0f);
  1939. device->FixedLatency += (ALuint)(GetCompressorLookAhead(device->Limiter) *
  1940. DEVICE_CLOCK_RES / device->Frequency);
  1941. }
  1942. else
  1943. {
  1944. al_free(device->Limiter);
  1945. device->Limiter = NULL;
  1946. }
  1947. TRACE("Output limiter %s\n", device->Limiter ? "enabled" : "disabled");
  1948. aluSelectPostProcess(device);
  1949. TRACE("Fixed device latency: %uns\n", device->FixedLatency);
  1950. /* Need to delay returning failure until replacement Send arrays have been
  1951. * allocated with the appropriate size.
  1952. */
  1953. update_failed = AL_FALSE;
  1954. START_MIXER_MODE();
  1955. context = ATOMIC_LOAD_SEQ(&device->ContextList);
  1956. while(context)
  1957. {
  1958. SourceSubList *sublist, *subend;
  1959. struct ALvoiceProps *vprops;
  1960. ALsizei pos;
  1961. if(context->DefaultSlot)
  1962. {
  1963. ALeffectslot *slot = context->DefaultSlot;
  1964. ALeffectState *state = slot->Effect.State;
  1965. state->OutBuffer = device->Dry.Buffer;
  1966. state->OutChannels = device->Dry.NumChannels;
  1967. if(V(state,deviceUpdate)(device) == AL_FALSE)
  1968. update_failed = AL_TRUE;
  1969. else
  1970. UpdateEffectSlotProps(slot, context);
  1971. }
  1972. almtx_lock(&context->PropLock);
  1973. almtx_lock(&context->EffectSlotLock);
  1974. for(pos = 0;pos < (ALsizei)VECTOR_SIZE(context->EffectSlotList);pos++)
  1975. {
  1976. ALeffectslot *slot = VECTOR_ELEM(context->EffectSlotList, pos);
  1977. ALeffectState *state = slot->Effect.State;
  1978. state->OutBuffer = device->Dry.Buffer;
  1979. state->OutChannels = device->Dry.NumChannels;
  1980. if(V(state,deviceUpdate)(device) == AL_FALSE)
  1981. update_failed = AL_TRUE;
  1982. else
  1983. UpdateEffectSlotProps(slot, context);
  1984. }
  1985. almtx_unlock(&context->EffectSlotLock);
  1986. almtx_lock(&context->SourceLock);
  1987. sublist = VECTOR_BEGIN(context->SourceList);
  1988. subend = VECTOR_END(context->SourceList);
  1989. for(;sublist != subend;++sublist)
  1990. {
  1991. ALuint64 usemask = ~sublist->FreeMask;
  1992. while(usemask)
  1993. {
  1994. ALsizei idx = CTZ64(usemask);
  1995. ALsource *source = sublist->Sources + idx;
  1996. usemask &= ~(U64(1) << idx);
  1997. if(old_sends != device->NumAuxSends)
  1998. {
  1999. ALvoid *sends = al_calloc(16, device->NumAuxSends*sizeof(source->Send[0]));
  2000. ALsizei s;
  2001. memcpy(sends, source->Send,
  2002. mini(device->NumAuxSends, old_sends)*sizeof(source->Send[0])
  2003. );
  2004. for(s = device->NumAuxSends;s < old_sends;s++)
  2005. {
  2006. if(source->Send[s].Slot)
  2007. DecrementRef(&source->Send[s].Slot->ref);
  2008. source->Send[s].Slot = NULL;
  2009. }
  2010. al_free(source->Send);
  2011. source->Send = sends;
  2012. for(s = old_sends;s < device->NumAuxSends;s++)
  2013. {
  2014. source->Send[s].Slot = NULL;
  2015. source->Send[s].Gain = 1.0f;
  2016. source->Send[s].GainHF = 1.0f;
  2017. source->Send[s].HFReference = LOWPASSFREQREF;
  2018. source->Send[s].GainLF = 1.0f;
  2019. source->Send[s].LFReference = HIGHPASSFREQREF;
  2020. }
  2021. }
  2022. ATOMIC_FLAG_CLEAR(&source->PropsClean, almemory_order_release);
  2023. }
  2024. }
  2025. /* Clear any pre-existing voice property structs, in case the number of
  2026. * auxiliary sends is changing. Active sources will have updates
  2027. * respecified in UpdateAllSourceProps.
  2028. */
  2029. vprops = ATOMIC_EXCHANGE_PTR(&context->FreeVoiceProps, NULL, almemory_order_acq_rel);
  2030. while(vprops)
  2031. {
  2032. struct ALvoiceProps *next = ATOMIC_LOAD(&vprops->next, almemory_order_relaxed);
  2033. al_free(vprops);
  2034. vprops = next;
  2035. }
  2036. AllocateVoices(context, context->MaxVoices, old_sends);
  2037. for(pos = 0;pos < context->VoiceCount;pos++)
  2038. {
  2039. ALvoice *voice = context->Voices[pos];
  2040. al_free(ATOMIC_EXCHANGE_PTR(&voice->Update, NULL, almemory_order_acq_rel));
  2041. if(ATOMIC_LOAD(&voice->Source, almemory_order_acquire) == NULL)
  2042. continue;
  2043. if(device->AvgSpeakerDist > 0.0f)
  2044. {
  2045. /* Reinitialize the NFC filters for new parameters. */
  2046. ALfloat w1 = SPEEDOFSOUNDMETRESPERSEC /
  2047. (device->AvgSpeakerDist * device->Frequency);
  2048. for(i = 0;i < voice->NumChannels;i++)
  2049. NfcFilterCreate(&voice->Direct.Params[i].NFCtrlFilter, 0.0f, w1);
  2050. }
  2051. }
  2052. almtx_unlock(&context->SourceLock);
  2053. ATOMIC_FLAG_TEST_AND_SET(&context->PropsClean, almemory_order_release);
  2054. UpdateContextProps(context);
  2055. ATOMIC_FLAG_TEST_AND_SET(&context->Listener->PropsClean, almemory_order_release);
  2056. UpdateListenerProps(context);
  2057. UpdateAllSourceProps(context);
  2058. almtx_unlock(&context->PropLock);
  2059. context = ATOMIC_LOAD(&context->next, almemory_order_relaxed);
  2060. }
  2061. END_MIXER_MODE();
  2062. if(update_failed)
  2063. return ALC_INVALID_DEVICE;
  2064. if(!(device->Flags&DEVICE_PAUSED))
  2065. {
  2066. if(V0(device->Backend,start)() == ALC_FALSE)
  2067. return ALC_INVALID_DEVICE;
  2068. device->Flags |= DEVICE_RUNNING;
  2069. }
  2070. return ALC_NO_ERROR;
  2071. }
  2072. static void InitDevice(ALCdevice *device, enum DeviceType type)
  2073. {
  2074. ALsizei i;
  2075. InitRef(&device->ref, 1);
  2076. ATOMIC_INIT(&device->Connected, ALC_TRUE);
  2077. device->Type = type;
  2078. ATOMIC_INIT(&device->LastError, ALC_NO_ERROR);
  2079. device->Flags = 0;
  2080. device->Render_Mode = NormalRender;
  2081. device->AvgSpeakerDist = 0.0f;
  2082. device->LimiterState = ALC_DONT_CARE_SOFT;
  2083. ATOMIC_INIT(&device->ContextList, NULL);
  2084. device->ClockBase = 0;
  2085. device->SamplesDone = 0;
  2086. device->FixedLatency = 0;
  2087. device->SourcesMax = 0;
  2088. device->AuxiliaryEffectSlotMax = 0;
  2089. device->NumAuxSends = 0;
  2090. device->Dry.Buffer = NULL;
  2091. device->Dry.NumChannels = 0;
  2092. device->FOAOut.Buffer = NULL;
  2093. device->FOAOut.NumChannels = 0;
  2094. device->RealOut.Buffer = NULL;
  2095. device->RealOut.NumChannels = 0;
  2096. AL_STRING_INIT(device->DeviceName);
  2097. for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
  2098. {
  2099. device->ChannelDelay[i].Gain = 1.0f;
  2100. device->ChannelDelay[i].Length = 0;
  2101. device->ChannelDelay[i].Buffer = NULL;
  2102. }
  2103. AL_STRING_INIT(device->HrtfName);
  2104. VECTOR_INIT(device->HrtfList);
  2105. device->HrtfHandle = NULL;
  2106. device->Hrtf = NULL;
  2107. device->Bs2b = NULL;
  2108. device->Uhj_Encoder = NULL;
  2109. device->AmbiDecoder = NULL;
  2110. device->AmbiUp = NULL;
  2111. device->Stablizer = NULL;
  2112. device->Limiter = NULL;
  2113. VECTOR_INIT(device->BufferList);
  2114. almtx_init(&device->BufferLock, almtx_plain);
  2115. VECTOR_INIT(device->EffectList);
  2116. almtx_init(&device->EffectLock, almtx_plain);
  2117. VECTOR_INIT(device->FilterList);
  2118. almtx_init(&device->FilterLock, almtx_plain);
  2119. almtx_init(&device->BackendLock, almtx_plain);
  2120. device->Backend = NULL;
  2121. ATOMIC_INIT(&device->next, NULL);
  2122. }
  2123. /* FreeDevice
  2124. *
  2125. * Frees the device structure, and destroys any objects the app failed to
  2126. * delete. Called once there's no more references on the device.
  2127. */
  2128. static ALCvoid FreeDevice(ALCdevice *device)
  2129. {
  2130. ALsizei i;
  2131. TRACE("%p\n", device);
  2132. if(device->Backend)
  2133. DELETE_OBJ(device->Backend);
  2134. device->Backend = NULL;
  2135. almtx_destroy(&device->BackendLock);
  2136. ReleaseALBuffers(device);
  2137. #define FREE_BUFFERSUBLIST(x) al_free((x)->Buffers)
  2138. VECTOR_FOR_EACH(BufferSubList, device->BufferList, FREE_BUFFERSUBLIST);
  2139. #undef FREE_BUFFERSUBLIST
  2140. VECTOR_DEINIT(device->BufferList);
  2141. almtx_destroy(&device->BufferLock);
  2142. ReleaseALEffects(device);
  2143. #define FREE_EFFECTSUBLIST(x) al_free((x)->Effects)
  2144. VECTOR_FOR_EACH(EffectSubList, device->EffectList, FREE_EFFECTSUBLIST);
  2145. #undef FREE_EFFECTSUBLIST
  2146. VECTOR_DEINIT(device->EffectList);
  2147. almtx_destroy(&device->EffectLock);
  2148. ReleaseALFilters(device);
  2149. #define FREE_FILTERSUBLIST(x) al_free((x)->Filters)
  2150. VECTOR_FOR_EACH(FilterSubList, device->FilterList, FREE_FILTERSUBLIST);
  2151. #undef FREE_FILTERSUBLIST
  2152. VECTOR_DEINIT(device->FilterList);
  2153. almtx_destroy(&device->FilterLock);
  2154. AL_STRING_DEINIT(device->HrtfName);
  2155. FreeHrtfList(&device->HrtfList);
  2156. if(device->HrtfHandle)
  2157. Hrtf_DecRef(device->HrtfHandle);
  2158. device->HrtfHandle = NULL;
  2159. al_free(device->Hrtf);
  2160. device->Hrtf = NULL;
  2161. al_free(device->Bs2b);
  2162. device->Bs2b = NULL;
  2163. al_free(device->Uhj_Encoder);
  2164. device->Uhj_Encoder = NULL;
  2165. bformatdec_free(&device->AmbiDecoder);
  2166. ambiup_free(&device->AmbiUp);
  2167. al_free(device->Stablizer);
  2168. device->Stablizer = NULL;
  2169. al_free(device->Limiter);
  2170. device->Limiter = NULL;
  2171. al_free(device->ChannelDelay[0].Buffer);
  2172. for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
  2173. {
  2174. device->ChannelDelay[i].Gain = 1.0f;
  2175. device->ChannelDelay[i].Length = 0;
  2176. device->ChannelDelay[i].Buffer = NULL;
  2177. }
  2178. AL_STRING_DEINIT(device->DeviceName);
  2179. al_free(device->Dry.Buffer);
  2180. device->Dry.Buffer = NULL;
  2181. device->Dry.NumChannels = 0;
  2182. device->FOAOut.Buffer = NULL;
  2183. device->FOAOut.NumChannels = 0;
  2184. device->RealOut.Buffer = NULL;
  2185. device->RealOut.NumChannels = 0;
  2186. al_free(device);
  2187. }
  2188. void ALCdevice_IncRef(ALCdevice *device)
  2189. {
  2190. uint ref;
  2191. ref = IncrementRef(&device->ref);
  2192. TRACEREF("%p increasing refcount to %u\n", device, ref);
  2193. }
  2194. void ALCdevice_DecRef(ALCdevice *device)
  2195. {
  2196. uint ref;
  2197. ref = DecrementRef(&device->ref);
  2198. TRACEREF("%p decreasing refcount to %u\n", device, ref);
  2199. if(ref == 0) FreeDevice(device);
  2200. }
  2201. /* VerifyDevice
  2202. *
  2203. * Checks if the device handle is valid, and increments its ref count if so.
  2204. */
  2205. static ALCboolean VerifyDevice(ALCdevice **device)
  2206. {
  2207. ALCdevice *tmpDevice;
  2208. LockLists();
  2209. tmpDevice = ATOMIC_LOAD_SEQ(&DeviceList);
  2210. while(tmpDevice)
  2211. {
  2212. if(tmpDevice == *device)
  2213. {
  2214. ALCdevice_IncRef(tmpDevice);
  2215. UnlockLists();
  2216. return ALC_TRUE;
  2217. }
  2218. tmpDevice = ATOMIC_LOAD(&tmpDevice->next, almemory_order_relaxed);
  2219. }
  2220. UnlockLists();
  2221. *device = NULL;
  2222. return ALC_FALSE;
  2223. }
  2224. /* InitContext
  2225. *
  2226. * Initializes context fields
  2227. */
  2228. static ALvoid InitContext(ALCcontext *Context)
  2229. {
  2230. ALlistener *listener = Context->Listener;
  2231. struct ALeffectslotArray *auxslots;
  2232. //Initialise listener
  2233. listener->Gain = 1.0f;
  2234. listener->Position[0] = 0.0f;
  2235. listener->Position[1] = 0.0f;
  2236. listener->Position[2] = 0.0f;
  2237. listener->Velocity[0] = 0.0f;
  2238. listener->Velocity[1] = 0.0f;
  2239. listener->Velocity[2] = 0.0f;
  2240. listener->Forward[0] = 0.0f;
  2241. listener->Forward[1] = 0.0f;
  2242. listener->Forward[2] = -1.0f;
  2243. listener->Up[0] = 0.0f;
  2244. listener->Up[1] = 1.0f;
  2245. listener->Up[2] = 0.0f;
  2246. ATOMIC_FLAG_TEST_AND_SET(&listener->PropsClean, almemory_order_relaxed);
  2247. ATOMIC_INIT(&listener->Update, NULL);
  2248. //Validate Context
  2249. InitRef(&Context->UpdateCount, 0);
  2250. ATOMIC_INIT(&Context->HoldUpdates, AL_FALSE);
  2251. Context->GainBoost = 1.0f;
  2252. almtx_init(&Context->PropLock, almtx_plain);
  2253. ATOMIC_INIT(&Context->LastError, AL_NO_ERROR);
  2254. VECTOR_INIT(Context->SourceList);
  2255. Context->NumSources = 0;
  2256. almtx_init(&Context->SourceLock, almtx_plain);
  2257. VECTOR_INIT(Context->EffectSlotList);
  2258. almtx_init(&Context->EffectSlotLock, almtx_plain);
  2259. if(Context->DefaultSlot)
  2260. {
  2261. auxslots = al_calloc(DEF_ALIGN, FAM_SIZE(struct ALeffectslotArray, slot, 1));
  2262. auxslots->count = 1;
  2263. auxslots->slot[0] = Context->DefaultSlot;
  2264. }
  2265. else
  2266. {
  2267. auxslots = al_calloc(DEF_ALIGN, sizeof(struct ALeffectslotArray));
  2268. auxslots->count = 0;
  2269. }
  2270. ATOMIC_INIT(&Context->ActiveAuxSlots, auxslots);
  2271. //Set globals
  2272. Context->DistanceModel = DefaultDistanceModel;
  2273. Context->SourceDistanceModel = AL_FALSE;
  2274. Context->DopplerFactor = 1.0f;
  2275. Context->DopplerVelocity = 1.0f;
  2276. Context->SpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
  2277. Context->MetersPerUnit = AL_DEFAULT_METERS_PER_UNIT;
  2278. ATOMIC_FLAG_TEST_AND_SET(&Context->PropsClean, almemory_order_relaxed);
  2279. ATOMIC_INIT(&Context->DeferUpdates, AL_FALSE);
  2280. alsem_init(&Context->EventSem, 0);
  2281. Context->AsyncEvents = NULL;
  2282. ATOMIC_INIT(&Context->EnabledEvts, 0);
  2283. almtx_init(&Context->EventCbLock, almtx_plain);
  2284. Context->EventCb = NULL;
  2285. Context->EventParam = NULL;
  2286. ATOMIC_INIT(&Context->Update, NULL);
  2287. ATOMIC_INIT(&Context->FreeContextProps, NULL);
  2288. ATOMIC_INIT(&Context->FreeListenerProps, NULL);
  2289. ATOMIC_INIT(&Context->FreeVoiceProps, NULL);
  2290. ATOMIC_INIT(&Context->FreeEffectslotProps, NULL);
  2291. Context->ExtensionList = alExtList;
  2292. listener->Params.Matrix = IdentityMatrixf;
  2293. aluVectorSet(&listener->Params.Velocity, 0.0f, 0.0f, 0.0f, 0.0f);
  2294. listener->Params.Gain = listener->Gain;
  2295. listener->Params.MetersPerUnit = Context->MetersPerUnit;
  2296. listener->Params.DopplerFactor = Context->DopplerFactor;
  2297. listener->Params.SpeedOfSound = Context->SpeedOfSound * Context->DopplerVelocity;
  2298. listener->Params.ReverbSpeedOfSound = listener->Params.SpeedOfSound *
  2299. listener->Params.MetersPerUnit;
  2300. listener->Params.SourceDistanceModel = Context->SourceDistanceModel;
  2301. listener->Params.DistanceModel = Context->DistanceModel;
  2302. Context->AsyncEvents = ll_ringbuffer_create(63, sizeof(AsyncEvent), false);
  2303. if(althrd_create(&Context->EventThread, EventThread, Context) != althrd_success)
  2304. ERR("Failed to start event thread! Expect problems.\n");
  2305. }
  2306. /* FreeContext
  2307. *
  2308. * Cleans up the context, and destroys any remaining objects the app failed to
  2309. * delete. Called once there's no more references on the context.
  2310. */
  2311. static void FreeContext(ALCcontext *context)
  2312. {
  2313. ALlistener *listener = context->Listener;
  2314. struct ALeffectslotArray *auxslots;
  2315. struct ALeffectslotProps *eprops;
  2316. struct ALlistenerProps *lprops;
  2317. struct ALcontextProps *cprops;
  2318. struct ALvoiceProps *vprops;
  2319. size_t count;
  2320. ALsizei i;
  2321. TRACE("%p\n", context);
  2322. if((cprops=ATOMIC_LOAD(&context->Update, almemory_order_acquire)) != NULL)
  2323. {
  2324. TRACE("Freed unapplied context update %p\n", cprops);
  2325. al_free(cprops);
  2326. }
  2327. count = 0;
  2328. cprops = ATOMIC_LOAD(&context->FreeContextProps, almemory_order_acquire);
  2329. while(cprops)
  2330. {
  2331. struct ALcontextProps *next = ATOMIC_LOAD(&cprops->next, almemory_order_acquire);
  2332. al_free(cprops);
  2333. cprops = next;
  2334. ++count;
  2335. }
  2336. TRACE("Freed "SZFMT" context property object%s\n", count, (count==1)?"":"s");
  2337. if(context->DefaultSlot)
  2338. {
  2339. DeinitEffectSlot(context->DefaultSlot);
  2340. context->DefaultSlot = NULL;
  2341. }
  2342. auxslots = ATOMIC_EXCHANGE_PTR(&context->ActiveAuxSlots, NULL, almemory_order_relaxed);
  2343. al_free(auxslots);
  2344. ReleaseALSources(context);
  2345. #define FREE_SOURCESUBLIST(x) al_free((x)->Sources)
  2346. VECTOR_FOR_EACH(SourceSubList, context->SourceList, FREE_SOURCESUBLIST);
  2347. #undef FREE_SOURCESUBLIST
  2348. VECTOR_DEINIT(context->SourceList);
  2349. context->NumSources = 0;
  2350. almtx_destroy(&context->SourceLock);
  2351. count = 0;
  2352. eprops = ATOMIC_LOAD(&context->FreeEffectslotProps, almemory_order_relaxed);
  2353. while(eprops)
  2354. {
  2355. struct ALeffectslotProps *next = ATOMIC_LOAD(&eprops->next, almemory_order_relaxed);
  2356. if(eprops->State) ALeffectState_DecRef(eprops->State);
  2357. al_free(eprops);
  2358. eprops = next;
  2359. ++count;
  2360. }
  2361. TRACE("Freed "SZFMT" AuxiliaryEffectSlot property object%s\n", count, (count==1)?"":"s");
  2362. ReleaseALAuxiliaryEffectSlots(context);
  2363. #define FREE_EFFECTSLOTPTR(x) al_free(*(x))
  2364. VECTOR_FOR_EACH(ALeffectslotPtr, context->EffectSlotList, FREE_EFFECTSLOTPTR);
  2365. #undef FREE_EFFECTSLOTPTR
  2366. VECTOR_DEINIT(context->EffectSlotList);
  2367. almtx_destroy(&context->EffectSlotLock);
  2368. count = 0;
  2369. vprops = ATOMIC_LOAD(&context->FreeVoiceProps, almemory_order_relaxed);
  2370. while(vprops)
  2371. {
  2372. struct ALvoiceProps *next = ATOMIC_LOAD(&vprops->next, almemory_order_relaxed);
  2373. al_free(vprops);
  2374. vprops = next;
  2375. ++count;
  2376. }
  2377. TRACE("Freed "SZFMT" voice property object%s\n", count, (count==1)?"":"s");
  2378. for(i = 0;i < context->VoiceCount;i++)
  2379. DeinitVoice(context->Voices[i]);
  2380. al_free(context->Voices);
  2381. context->Voices = NULL;
  2382. context->VoiceCount = 0;
  2383. context->MaxVoices = 0;
  2384. if((lprops=ATOMIC_LOAD(&listener->Update, almemory_order_acquire)) != NULL)
  2385. {
  2386. TRACE("Freed unapplied listener update %p\n", lprops);
  2387. al_free(lprops);
  2388. }
  2389. count = 0;
  2390. lprops = ATOMIC_LOAD(&context->FreeListenerProps, almemory_order_acquire);
  2391. while(lprops)
  2392. {
  2393. struct ALlistenerProps *next = ATOMIC_LOAD(&lprops->next, almemory_order_acquire);
  2394. al_free(lprops);
  2395. lprops = next;
  2396. ++count;
  2397. }
  2398. TRACE("Freed "SZFMT" listener property object%s\n", count, (count==1)?"":"s");
  2399. almtx_destroy(&context->EventCbLock);
  2400. alsem_destroy(&context->EventSem);
  2401. ll_ringbuffer_free(context->AsyncEvents);
  2402. context->AsyncEvents = NULL;
  2403. almtx_destroy(&context->PropLock);
  2404. ALCdevice_DecRef(context->Device);
  2405. context->Device = NULL;
  2406. //Invalidate context
  2407. memset(context, 0, sizeof(ALCcontext));
  2408. al_free(context);
  2409. }
  2410. /* ReleaseContext
  2411. *
  2412. * Removes the context reference from the given device and removes it from
  2413. * being current on the running thread or globally. Returns true if other
  2414. * contexts still exist on the device.
  2415. */
  2416. static bool ReleaseContext(ALCcontext *context, ALCdevice *device)
  2417. {
  2418. static const AsyncEvent kill_evt = ASYNC_EVENT(EventType_KillThread);
  2419. ALCcontext *origctx, *newhead;
  2420. bool ret = true;
  2421. if(altss_get(LocalContext) == context)
  2422. {
  2423. WARN("%p released while current on thread\n", context);
  2424. altss_set(LocalContext, NULL);
  2425. ALCcontext_DecRef(context);
  2426. }
  2427. origctx = context;
  2428. if(ATOMIC_COMPARE_EXCHANGE_PTR_STRONG_SEQ(&GlobalContext, &origctx, NULL))
  2429. ALCcontext_DecRef(context);
  2430. V0(device->Backend,lock)();
  2431. origctx = context;
  2432. newhead = ATOMIC_LOAD(&context->next, almemory_order_relaxed);
  2433. if(!ATOMIC_COMPARE_EXCHANGE_PTR_STRONG_SEQ(&device->ContextList, &origctx, newhead))
  2434. {
  2435. ALCcontext *list;
  2436. do {
  2437. /* origctx is what the desired context failed to match. Try
  2438. * swapping out the next one in the list.
  2439. */
  2440. list = origctx;
  2441. origctx = context;
  2442. } while(!ATOMIC_COMPARE_EXCHANGE_PTR_STRONG_SEQ(&list->next, &origctx, newhead));
  2443. }
  2444. else
  2445. ret = !!newhead;
  2446. V0(device->Backend,unlock)();
  2447. /* Make sure the context is finished and no longer processing in the mixer
  2448. * before sending the message queue kill event. The backend's lock does
  2449. * this, although waiting for a non-odd mix count would work too.
  2450. */
  2451. while(ll_ringbuffer_write(context->AsyncEvents, (const char*)&kill_evt, 1) == 0)
  2452. althrd_yield();
  2453. alsem_post(&context->EventSem);
  2454. althrd_join(context->EventThread, NULL);
  2455. ALCcontext_DecRef(context);
  2456. return ret;
  2457. }
  2458. static void ALCcontext_IncRef(ALCcontext *context)
  2459. {
  2460. uint ref = IncrementRef(&context->ref);
  2461. TRACEREF("%p increasing refcount to %u\n", context, ref);
  2462. }
  2463. void ALCcontext_DecRef(ALCcontext *context)
  2464. {
  2465. uint ref = DecrementRef(&context->ref);
  2466. TRACEREF("%p decreasing refcount to %u\n", context, ref);
  2467. if(ref == 0) FreeContext(context);
  2468. }
  2469. static void ReleaseThreadCtx(void *ptr)
  2470. {
  2471. ALCcontext *context = ptr;
  2472. uint ref = DecrementRef(&context->ref);
  2473. TRACEREF("%p decreasing refcount to %u\n", context, ref);
  2474. ERR("Context %p current for thread being destroyed, possible leak!\n", context);
  2475. }
  2476. /* VerifyContext
  2477. *
  2478. * Checks that the given context is valid, and increments its reference count.
  2479. */
  2480. static ALCboolean VerifyContext(ALCcontext **context)
  2481. {
  2482. ALCdevice *dev;
  2483. LockLists();
  2484. dev = ATOMIC_LOAD_SEQ(&DeviceList);
  2485. while(dev)
  2486. {
  2487. ALCcontext *ctx = ATOMIC_LOAD(&dev->ContextList, almemory_order_acquire);
  2488. while(ctx)
  2489. {
  2490. if(ctx == *context)
  2491. {
  2492. ALCcontext_IncRef(ctx);
  2493. UnlockLists();
  2494. return ALC_TRUE;
  2495. }
  2496. ctx = ATOMIC_LOAD(&ctx->next, almemory_order_relaxed);
  2497. }
  2498. dev = ATOMIC_LOAD(&dev->next, almemory_order_relaxed);
  2499. }
  2500. UnlockLists();
  2501. *context = NULL;
  2502. return ALC_FALSE;
  2503. }
  2504. /* GetContextRef
  2505. *
  2506. * Returns the currently active context for this thread, and adds a reference
  2507. * without locking it.
  2508. */
  2509. ALCcontext *GetContextRef(void)
  2510. {
  2511. ALCcontext *context;
  2512. context = altss_get(LocalContext);
  2513. if(context)
  2514. ALCcontext_IncRef(context);
  2515. else
  2516. {
  2517. LockLists();
  2518. context = ATOMIC_LOAD_SEQ(&GlobalContext);
  2519. if(context)
  2520. ALCcontext_IncRef(context);
  2521. UnlockLists();
  2522. }
  2523. return context;
  2524. }
  2525. void AllocateVoices(ALCcontext *context, ALsizei num_voices, ALsizei old_sends)
  2526. {
  2527. ALCdevice *device = context->Device;
  2528. ALsizei num_sends = device->NumAuxSends;
  2529. struct ALvoiceProps *props;
  2530. size_t sizeof_props;
  2531. size_t sizeof_voice;
  2532. ALvoice **voices;
  2533. ALvoice *voice;
  2534. ALsizei v = 0;
  2535. size_t size;
  2536. if(num_voices == context->MaxVoices && num_sends == old_sends)
  2537. return;
  2538. /* Allocate the voice pointers, voices, and the voices' stored source
  2539. * property set (including the dynamically-sized Send[] array) in one
  2540. * chunk.
  2541. */
  2542. sizeof_voice = RoundUp(FAM_SIZE(ALvoice, Send, num_sends), 16);
  2543. sizeof_props = RoundUp(FAM_SIZE(struct ALvoiceProps, Send, num_sends), 16);
  2544. size = sizeof(ALvoice*) + sizeof_voice + sizeof_props;
  2545. voices = al_calloc(16, RoundUp(size*num_voices, 16));
  2546. /* The voice and property objects are stored interleaved since they're
  2547. * paired together.
  2548. */
  2549. voice = (ALvoice*)((char*)voices + RoundUp(num_voices*sizeof(ALvoice*), 16));
  2550. props = (struct ALvoiceProps*)((char*)voice + sizeof_voice);
  2551. if(context->Voices)
  2552. {
  2553. const ALsizei v_count = mini(context->VoiceCount, num_voices);
  2554. const ALsizei s_count = mini(old_sends, num_sends);
  2555. for(;v < v_count;v++)
  2556. {
  2557. ALvoice *old_voice = context->Voices[v];
  2558. ALsizei i;
  2559. /* Copy the old voice data and source property set to the new
  2560. * storage.
  2561. */
  2562. *voice = *old_voice;
  2563. for(i = 0;i < s_count;i++)
  2564. voice->Send[i] = old_voice->Send[i];
  2565. *props = *(old_voice->Props);
  2566. for(i = 0;i < s_count;i++)
  2567. props->Send[i] = old_voice->Props->Send[i];
  2568. /* Set this voice's property set pointer and voice reference. */
  2569. voice->Props = props;
  2570. voices[v] = voice;
  2571. /* Increment pointers to the next storage space. */
  2572. voice = (ALvoice*)((char*)props + sizeof_props);
  2573. props = (struct ALvoiceProps*)((char*)voice + sizeof_voice);
  2574. }
  2575. /* Deinit any left over voices that weren't copied over to the new
  2576. * array. NOTE: If this does anything, v equals num_voices and
  2577. * num_voices is less than VoiceCount, so the following loop won't do
  2578. * anything.
  2579. */
  2580. for(;v < context->VoiceCount;v++)
  2581. DeinitVoice(context->Voices[v]);
  2582. }
  2583. /* Finish setting the voices' property set pointers and references. */
  2584. for(;v < num_voices;v++)
  2585. {
  2586. ATOMIC_INIT(&voice->Update, NULL);
  2587. voice->Props = props;
  2588. voices[v] = voice;
  2589. voice = (ALvoice*)((char*)props + sizeof_props);
  2590. props = (struct ALvoiceProps*)((char*)voice + sizeof_voice);
  2591. }
  2592. al_free(context->Voices);
  2593. context->Voices = voices;
  2594. context->MaxVoices = num_voices;
  2595. context->VoiceCount = mini(context->VoiceCount, num_voices);
  2596. }
  2597. /************************************************
  2598. * Standard ALC functions
  2599. ************************************************/
  2600. /* alcGetError
  2601. *
  2602. * Return last ALC generated error code for the given device
  2603. */
  2604. ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
  2605. {
  2606. ALCenum errorCode;
  2607. if(VerifyDevice(&device))
  2608. {
  2609. errorCode = ATOMIC_EXCHANGE_SEQ(&device->LastError, ALC_NO_ERROR);
  2610. ALCdevice_DecRef(device);
  2611. }
  2612. else
  2613. errorCode = ATOMIC_EXCHANGE_SEQ(&LastNullDeviceError, ALC_NO_ERROR);
  2614. return errorCode;
  2615. }
  2616. /* alcSuspendContext
  2617. *
  2618. * Suspends updates for the given context
  2619. */
  2620. ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *context)
  2621. {
  2622. if(!SuspendDefers)
  2623. return;
  2624. if(!VerifyContext(&context))
  2625. alcSetError(NULL, ALC_INVALID_CONTEXT);
  2626. else
  2627. {
  2628. ALCcontext_DeferUpdates(context);
  2629. ALCcontext_DecRef(context);
  2630. }
  2631. }
  2632. /* alcProcessContext
  2633. *
  2634. * Resumes processing updates for the given context
  2635. */
  2636. ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *context)
  2637. {
  2638. if(!SuspendDefers)
  2639. return;
  2640. if(!VerifyContext(&context))
  2641. alcSetError(NULL, ALC_INVALID_CONTEXT);
  2642. else
  2643. {
  2644. ALCcontext_ProcessUpdates(context);
  2645. ALCcontext_DecRef(context);
  2646. }
  2647. }
  2648. /* alcGetString
  2649. *
  2650. * Returns information about the device, and error strings
  2651. */
  2652. ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *Device, ALCenum param)
  2653. {
  2654. const ALCchar *value = NULL;
  2655. switch(param)
  2656. {
  2657. case ALC_NO_ERROR:
  2658. value = alcNoError;
  2659. break;
  2660. case ALC_INVALID_ENUM:
  2661. value = alcErrInvalidEnum;
  2662. break;
  2663. case ALC_INVALID_VALUE:
  2664. value = alcErrInvalidValue;
  2665. break;
  2666. case ALC_INVALID_DEVICE:
  2667. value = alcErrInvalidDevice;
  2668. break;
  2669. case ALC_INVALID_CONTEXT:
  2670. value = alcErrInvalidContext;
  2671. break;
  2672. case ALC_OUT_OF_MEMORY:
  2673. value = alcErrOutOfMemory;
  2674. break;
  2675. case ALC_DEVICE_SPECIFIER:
  2676. value = alcDefaultName;
  2677. break;
  2678. case ALC_ALL_DEVICES_SPECIFIER:
  2679. if(VerifyDevice(&Device))
  2680. {
  2681. value = alstr_get_cstr(Device->DeviceName);
  2682. ALCdevice_DecRef(Device);
  2683. }
  2684. else
  2685. {
  2686. ProbeAllDevicesList();
  2687. value = alstr_get_cstr(alcAllDevicesList);
  2688. }
  2689. break;
  2690. case ALC_CAPTURE_DEVICE_SPECIFIER:
  2691. if(VerifyDevice(&Device))
  2692. {
  2693. value = alstr_get_cstr(Device->DeviceName);
  2694. ALCdevice_DecRef(Device);
  2695. }
  2696. else
  2697. {
  2698. ProbeCaptureDeviceList();
  2699. value = alstr_get_cstr(alcCaptureDeviceList);
  2700. }
  2701. break;
  2702. /* Default devices are always first in the list */
  2703. case ALC_DEFAULT_DEVICE_SPECIFIER:
  2704. value = alcDefaultName;
  2705. break;
  2706. case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
  2707. if(alstr_empty(alcAllDevicesList))
  2708. ProbeAllDevicesList();
  2709. VerifyDevice(&Device);
  2710. free(alcDefaultAllDevicesSpecifier);
  2711. alcDefaultAllDevicesSpecifier = strdup(alstr_get_cstr(alcAllDevicesList));
  2712. if(!alcDefaultAllDevicesSpecifier)
  2713. alcSetError(Device, ALC_OUT_OF_MEMORY);
  2714. value = alcDefaultAllDevicesSpecifier;
  2715. if(Device) ALCdevice_DecRef(Device);
  2716. break;
  2717. case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
  2718. if(alstr_empty(alcCaptureDeviceList))
  2719. ProbeCaptureDeviceList();
  2720. VerifyDevice(&Device);
  2721. free(alcCaptureDefaultDeviceSpecifier);
  2722. alcCaptureDefaultDeviceSpecifier = strdup(alstr_get_cstr(alcCaptureDeviceList));
  2723. if(!alcCaptureDefaultDeviceSpecifier)
  2724. alcSetError(Device, ALC_OUT_OF_MEMORY);
  2725. value = alcCaptureDefaultDeviceSpecifier;
  2726. if(Device) ALCdevice_DecRef(Device);
  2727. break;
  2728. case ALC_EXTENSIONS:
  2729. if(!VerifyDevice(&Device))
  2730. value = alcNoDeviceExtList;
  2731. else
  2732. {
  2733. value = alcExtensionList;
  2734. ALCdevice_DecRef(Device);
  2735. }
  2736. break;
  2737. case ALC_HRTF_SPECIFIER_SOFT:
  2738. if(!VerifyDevice(&Device))
  2739. alcSetError(NULL, ALC_INVALID_DEVICE);
  2740. else
  2741. {
  2742. almtx_lock(&Device->BackendLock);
  2743. value = (Device->HrtfHandle ? alstr_get_cstr(Device->HrtfName) : "");
  2744. almtx_unlock(&Device->BackendLock);
  2745. ALCdevice_DecRef(Device);
  2746. }
  2747. break;
  2748. default:
  2749. VerifyDevice(&Device);
  2750. alcSetError(Device, ALC_INVALID_ENUM);
  2751. if(Device) ALCdevice_DecRef(Device);
  2752. break;
  2753. }
  2754. return value;
  2755. }
  2756. static inline ALCsizei NumAttrsForDevice(ALCdevice *device)
  2757. {
  2758. if(device->Type == Capture) return 9;
  2759. if(device->Type != Loopback) return 29;
  2760. if(device->FmtChans == DevFmtAmbi3D)
  2761. return 35;
  2762. return 29;
  2763. }
  2764. static ALCsizei GetIntegerv(ALCdevice *device, ALCenum param, ALCsizei size, ALCint *values)
  2765. {
  2766. ALCsizei i;
  2767. if(size <= 0 || values == NULL)
  2768. {
  2769. alcSetError(device, ALC_INVALID_VALUE);
  2770. return 0;
  2771. }
  2772. if(!device)
  2773. {
  2774. switch(param)
  2775. {
  2776. case ALC_MAJOR_VERSION:
  2777. values[0] = alcMajorVersion;
  2778. return 1;
  2779. case ALC_MINOR_VERSION:
  2780. values[0] = alcMinorVersion;
  2781. return 1;
  2782. case ALC_ATTRIBUTES_SIZE:
  2783. case ALC_ALL_ATTRIBUTES:
  2784. case ALC_FREQUENCY:
  2785. case ALC_REFRESH:
  2786. case ALC_SYNC:
  2787. case ALC_MONO_SOURCES:
  2788. case ALC_STEREO_SOURCES:
  2789. case ALC_CAPTURE_SAMPLES:
  2790. case ALC_FORMAT_CHANNELS_SOFT:
  2791. case ALC_FORMAT_TYPE_SOFT:
  2792. case ALC_AMBISONIC_LAYOUT_SOFT:
  2793. case ALC_AMBISONIC_SCALING_SOFT:
  2794. case ALC_AMBISONIC_ORDER_SOFT:
  2795. case ALC_MAX_AMBISONIC_ORDER_SOFT:
  2796. alcSetError(NULL, ALC_INVALID_DEVICE);
  2797. return 0;
  2798. default:
  2799. alcSetError(NULL, ALC_INVALID_ENUM);
  2800. return 0;
  2801. }
  2802. return 0;
  2803. }
  2804. if(device->Type == Capture)
  2805. {
  2806. switch(param)
  2807. {
  2808. case ALC_ATTRIBUTES_SIZE:
  2809. values[0] = NumAttrsForDevice(device);
  2810. return 1;
  2811. case ALC_ALL_ATTRIBUTES:
  2812. if(size < NumAttrsForDevice(device))
  2813. {
  2814. alcSetError(device, ALC_INVALID_VALUE);
  2815. return 0;
  2816. }
  2817. i = 0;
  2818. almtx_lock(&device->BackendLock);
  2819. values[i++] = ALC_MAJOR_VERSION;
  2820. values[i++] = alcMajorVersion;
  2821. values[i++] = ALC_MINOR_VERSION;
  2822. values[i++] = alcMinorVersion;
  2823. values[i++] = ALC_CAPTURE_SAMPLES;
  2824. values[i++] = V0(device->Backend,availableSamples)();
  2825. values[i++] = ALC_CONNECTED;
  2826. values[i++] = ATOMIC_LOAD(&device->Connected, almemory_order_relaxed);
  2827. almtx_unlock(&device->BackendLock);
  2828. values[i++] = 0;
  2829. return i;
  2830. case ALC_MAJOR_VERSION:
  2831. values[0] = alcMajorVersion;
  2832. return 1;
  2833. case ALC_MINOR_VERSION:
  2834. values[0] = alcMinorVersion;
  2835. return 1;
  2836. case ALC_CAPTURE_SAMPLES:
  2837. almtx_lock(&device->BackendLock);
  2838. values[0] = V0(device->Backend,availableSamples)();
  2839. almtx_unlock(&device->BackendLock);
  2840. return 1;
  2841. case ALC_CONNECTED:
  2842. values[0] = ATOMIC_LOAD(&device->Connected, almemory_order_acquire);
  2843. return 1;
  2844. default:
  2845. alcSetError(device, ALC_INVALID_ENUM);
  2846. return 0;
  2847. }
  2848. return 0;
  2849. }
  2850. /* render device */
  2851. switch(param)
  2852. {
  2853. case ALC_ATTRIBUTES_SIZE:
  2854. values[0] = NumAttrsForDevice(device);
  2855. return 1;
  2856. case ALC_ALL_ATTRIBUTES:
  2857. if(size < NumAttrsForDevice(device))
  2858. {
  2859. alcSetError(device, ALC_INVALID_VALUE);
  2860. return 0;
  2861. }
  2862. i = 0;
  2863. almtx_lock(&device->BackendLock);
  2864. values[i++] = ALC_MAJOR_VERSION;
  2865. values[i++] = alcMajorVersion;
  2866. values[i++] = ALC_MINOR_VERSION;
  2867. values[i++] = alcMinorVersion;
  2868. values[i++] = ALC_EFX_MAJOR_VERSION;
  2869. values[i++] = alcEFXMajorVersion;
  2870. values[i++] = ALC_EFX_MINOR_VERSION;
  2871. values[i++] = alcEFXMinorVersion;
  2872. values[i++] = ALC_FREQUENCY;
  2873. values[i++] = device->Frequency;
  2874. if(device->Type != Loopback)
  2875. {
  2876. values[i++] = ALC_REFRESH;
  2877. values[i++] = device->Frequency / device->UpdateSize;
  2878. values[i++] = ALC_SYNC;
  2879. values[i++] = ALC_FALSE;
  2880. }
  2881. else
  2882. {
  2883. if(device->FmtChans == DevFmtAmbi3D)
  2884. {
  2885. values[i++] = ALC_AMBISONIC_LAYOUT_SOFT;
  2886. values[i++] = device->AmbiLayout;
  2887. values[i++] = ALC_AMBISONIC_SCALING_SOFT;
  2888. values[i++] = device->AmbiScale;
  2889. values[i++] = ALC_AMBISONIC_ORDER_SOFT;
  2890. values[i++] = device->AmbiOrder;
  2891. }
  2892. values[i++] = ALC_FORMAT_CHANNELS_SOFT;
  2893. values[i++] = device->FmtChans;
  2894. values[i++] = ALC_FORMAT_TYPE_SOFT;
  2895. values[i++] = device->FmtType;
  2896. }
  2897. values[i++] = ALC_MONO_SOURCES;
  2898. values[i++] = device->NumMonoSources;
  2899. values[i++] = ALC_STEREO_SOURCES;
  2900. values[i++] = device->NumStereoSources;
  2901. values[i++] = ALC_MAX_AUXILIARY_SENDS;
  2902. values[i++] = device->NumAuxSends;
  2903. values[i++] = ALC_HRTF_SOFT;
  2904. values[i++] = (device->HrtfHandle ? ALC_TRUE : ALC_FALSE);
  2905. values[i++] = ALC_HRTF_STATUS_SOFT;
  2906. values[i++] = device->HrtfStatus;
  2907. values[i++] = ALC_OUTPUT_LIMITER_SOFT;
  2908. values[i++] = device->Limiter ? ALC_TRUE : ALC_FALSE;
  2909. values[i++] = ALC_MAX_AMBISONIC_ORDER_SOFT;
  2910. values[i++] = MAX_AMBI_ORDER;
  2911. almtx_unlock(&device->BackendLock);
  2912. values[i++] = 0;
  2913. return i;
  2914. case ALC_MAJOR_VERSION:
  2915. values[0] = alcMajorVersion;
  2916. return 1;
  2917. case ALC_MINOR_VERSION:
  2918. values[0] = alcMinorVersion;
  2919. return 1;
  2920. case ALC_EFX_MAJOR_VERSION:
  2921. values[0] = alcEFXMajorVersion;
  2922. return 1;
  2923. case ALC_EFX_MINOR_VERSION:
  2924. values[0] = alcEFXMinorVersion;
  2925. return 1;
  2926. case ALC_FREQUENCY:
  2927. values[0] = device->Frequency;
  2928. return 1;
  2929. case ALC_REFRESH:
  2930. if(device->Type == Loopback)
  2931. {
  2932. alcSetError(device, ALC_INVALID_DEVICE);
  2933. return 0;
  2934. }
  2935. almtx_lock(&device->BackendLock);
  2936. values[0] = device->Frequency / device->UpdateSize;
  2937. almtx_unlock(&device->BackendLock);
  2938. return 1;
  2939. case ALC_SYNC:
  2940. if(device->Type == Loopback)
  2941. {
  2942. alcSetError(device, ALC_INVALID_DEVICE);
  2943. return 0;
  2944. }
  2945. values[0] = ALC_FALSE;
  2946. return 1;
  2947. case ALC_FORMAT_CHANNELS_SOFT:
  2948. if(device->Type != Loopback)
  2949. {
  2950. alcSetError(device, ALC_INVALID_DEVICE);
  2951. return 0;
  2952. }
  2953. values[0] = device->FmtChans;
  2954. return 1;
  2955. case ALC_FORMAT_TYPE_SOFT:
  2956. if(device->Type != Loopback)
  2957. {
  2958. alcSetError(device, ALC_INVALID_DEVICE);
  2959. return 0;
  2960. }
  2961. values[0] = device->FmtType;
  2962. return 1;
  2963. case ALC_AMBISONIC_LAYOUT_SOFT:
  2964. if(device->Type != Loopback || device->FmtChans != DevFmtAmbi3D)
  2965. {
  2966. alcSetError(device, ALC_INVALID_DEVICE);
  2967. return 0;
  2968. }
  2969. values[0] = device->AmbiLayout;
  2970. return 1;
  2971. case ALC_AMBISONIC_SCALING_SOFT:
  2972. if(device->Type != Loopback || device->FmtChans != DevFmtAmbi3D)
  2973. {
  2974. alcSetError(device, ALC_INVALID_DEVICE);
  2975. return 0;
  2976. }
  2977. values[0] = device->AmbiScale;
  2978. return 1;
  2979. case ALC_AMBISONIC_ORDER_SOFT:
  2980. if(device->Type != Loopback || device->FmtChans != DevFmtAmbi3D)
  2981. {
  2982. alcSetError(device, ALC_INVALID_DEVICE);
  2983. return 0;
  2984. }
  2985. values[0] = device->AmbiOrder;
  2986. return 1;
  2987. case ALC_MONO_SOURCES:
  2988. values[0] = device->NumMonoSources;
  2989. return 1;
  2990. case ALC_STEREO_SOURCES:
  2991. values[0] = device->NumStereoSources;
  2992. return 1;
  2993. case ALC_MAX_AUXILIARY_SENDS:
  2994. values[0] = device->NumAuxSends;
  2995. return 1;
  2996. case ALC_CONNECTED:
  2997. values[0] = ATOMIC_LOAD(&device->Connected, almemory_order_acquire);
  2998. return 1;
  2999. case ALC_HRTF_SOFT:
  3000. values[0] = (device->HrtfHandle ? ALC_TRUE : ALC_FALSE);
  3001. return 1;
  3002. case ALC_HRTF_STATUS_SOFT:
  3003. values[0] = device->HrtfStatus;
  3004. return 1;
  3005. case ALC_NUM_HRTF_SPECIFIERS_SOFT:
  3006. almtx_lock(&device->BackendLock);
  3007. FreeHrtfList(&device->HrtfList);
  3008. device->HrtfList = EnumerateHrtf(device->DeviceName);
  3009. values[0] = (ALCint)VECTOR_SIZE(device->HrtfList);
  3010. almtx_unlock(&device->BackendLock);
  3011. return 1;
  3012. case ALC_OUTPUT_LIMITER_SOFT:
  3013. values[0] = device->Limiter ? ALC_TRUE : ALC_FALSE;
  3014. return 1;
  3015. case ALC_MAX_AMBISONIC_ORDER_SOFT:
  3016. values[0] = MAX_AMBI_ORDER;
  3017. return 1;
  3018. default:
  3019. alcSetError(device, ALC_INVALID_ENUM);
  3020. return 0;
  3021. }
  3022. return 0;
  3023. }
  3024. /* alcGetIntegerv
  3025. *
  3026. * Returns information about the device and the version of OpenAL
  3027. */
  3028. ALC_API void ALC_APIENTRY alcGetIntegerv(ALCdevice *device, ALCenum param, ALCsizei size, ALCint *values)
  3029. {
  3030. VerifyDevice(&device);
  3031. if(size <= 0 || values == NULL)
  3032. alcSetError(device, ALC_INVALID_VALUE);
  3033. else
  3034. GetIntegerv(device, param, size, values);
  3035. if(device) ALCdevice_DecRef(device);
  3036. }
  3037. ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname, ALCsizei size, ALCint64SOFT *values)
  3038. {
  3039. ALCint *ivals;
  3040. ALsizei i;
  3041. VerifyDevice(&device);
  3042. if(size <= 0 || values == NULL)
  3043. alcSetError(device, ALC_INVALID_VALUE);
  3044. else if(!device || device->Type == Capture)
  3045. {
  3046. ivals = malloc(size * sizeof(ALCint));
  3047. size = GetIntegerv(device, pname, size, ivals);
  3048. for(i = 0;i < size;i++)
  3049. values[i] = ivals[i];
  3050. free(ivals);
  3051. }
  3052. else /* render device */
  3053. {
  3054. ClockLatency clock;
  3055. ALuint64 basecount;
  3056. ALuint samplecount;
  3057. ALuint refcount;
  3058. switch(pname)
  3059. {
  3060. case ALC_ATTRIBUTES_SIZE:
  3061. *values = NumAttrsForDevice(device)+4;
  3062. break;
  3063. case ALC_ALL_ATTRIBUTES:
  3064. if(size < NumAttrsForDevice(device)+4)
  3065. alcSetError(device, ALC_INVALID_VALUE);
  3066. else
  3067. {
  3068. i = 0;
  3069. almtx_lock(&device->BackendLock);
  3070. values[i++] = ALC_FREQUENCY;
  3071. values[i++] = device->Frequency;
  3072. if(device->Type != Loopback)
  3073. {
  3074. values[i++] = ALC_REFRESH;
  3075. values[i++] = device->Frequency / device->UpdateSize;
  3076. values[i++] = ALC_SYNC;
  3077. values[i++] = ALC_FALSE;
  3078. }
  3079. else
  3080. {
  3081. if(device->FmtChans == DevFmtAmbi3D)
  3082. {
  3083. values[i++] = ALC_AMBISONIC_LAYOUT_SOFT;
  3084. values[i++] = device->AmbiLayout;
  3085. values[i++] = ALC_AMBISONIC_SCALING_SOFT;
  3086. values[i++] = device->AmbiScale;
  3087. values[i++] = ALC_AMBISONIC_ORDER_SOFT;
  3088. values[i++] = device->AmbiOrder;
  3089. }
  3090. values[i++] = ALC_FORMAT_CHANNELS_SOFT;
  3091. values[i++] = device->FmtChans;
  3092. values[i++] = ALC_FORMAT_TYPE_SOFT;
  3093. values[i++] = device->FmtType;
  3094. }
  3095. values[i++] = ALC_MONO_SOURCES;
  3096. values[i++] = device->NumMonoSources;
  3097. values[i++] = ALC_STEREO_SOURCES;
  3098. values[i++] = device->NumStereoSources;
  3099. values[i++] = ALC_MAX_AUXILIARY_SENDS;
  3100. values[i++] = device->NumAuxSends;
  3101. values[i++] = ALC_HRTF_SOFT;
  3102. values[i++] = (device->HrtfHandle ? ALC_TRUE : ALC_FALSE);
  3103. values[i++] = ALC_HRTF_STATUS_SOFT;
  3104. values[i++] = device->HrtfStatus;
  3105. values[i++] = ALC_OUTPUT_LIMITER_SOFT;
  3106. values[i++] = device->Limiter ? ALC_TRUE : ALC_FALSE;
  3107. clock = GetClockLatency(device);
  3108. values[i++] = ALC_DEVICE_CLOCK_SOFT;
  3109. values[i++] = clock.ClockTime;
  3110. values[i++] = ALC_DEVICE_LATENCY_SOFT;
  3111. values[i++] = clock.Latency;
  3112. almtx_unlock(&device->BackendLock);
  3113. values[i++] = 0;
  3114. }
  3115. break;
  3116. case ALC_DEVICE_CLOCK_SOFT:
  3117. almtx_lock(&device->BackendLock);
  3118. do {
  3119. while(((refcount=ReadRef(&device->MixCount))&1) != 0)
  3120. althrd_yield();
  3121. basecount = device->ClockBase;
  3122. samplecount = device->SamplesDone;
  3123. } while(refcount != ReadRef(&device->MixCount));
  3124. *values = basecount + (samplecount*DEVICE_CLOCK_RES/device->Frequency);
  3125. almtx_unlock(&device->BackendLock);
  3126. break;
  3127. case ALC_DEVICE_LATENCY_SOFT:
  3128. almtx_lock(&device->BackendLock);
  3129. clock = GetClockLatency(device);
  3130. almtx_unlock(&device->BackendLock);
  3131. *values = clock.Latency;
  3132. break;
  3133. case ALC_DEVICE_CLOCK_LATENCY_SOFT:
  3134. if(size < 2)
  3135. alcSetError(device, ALC_INVALID_VALUE);
  3136. else
  3137. {
  3138. almtx_lock(&device->BackendLock);
  3139. clock = GetClockLatency(device);
  3140. almtx_unlock(&device->BackendLock);
  3141. values[0] = clock.ClockTime;
  3142. values[1] = clock.Latency;
  3143. }
  3144. break;
  3145. default:
  3146. ivals = malloc(size * sizeof(ALCint));
  3147. size = GetIntegerv(device, pname, size, ivals);
  3148. for(i = 0;i < size;i++)
  3149. values[i] = ivals[i];
  3150. free(ivals);
  3151. break;
  3152. }
  3153. }
  3154. if(device)
  3155. ALCdevice_DecRef(device);
  3156. }
  3157. /* alcIsExtensionPresent
  3158. *
  3159. * Determines if there is support for a particular extension
  3160. */
  3161. ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
  3162. {
  3163. ALCboolean bResult = ALC_FALSE;
  3164. VerifyDevice(&device);
  3165. if(!extName)
  3166. alcSetError(device, ALC_INVALID_VALUE);
  3167. else
  3168. {
  3169. size_t len = strlen(extName);
  3170. const char *ptr = (device ? alcExtensionList : alcNoDeviceExtList);
  3171. while(ptr && *ptr)
  3172. {
  3173. if(strncasecmp(ptr, extName, len) == 0 &&
  3174. (ptr[len] == '\0' || isspace(ptr[len])))
  3175. {
  3176. bResult = ALC_TRUE;
  3177. break;
  3178. }
  3179. if((ptr=strchr(ptr, ' ')) != NULL)
  3180. {
  3181. do {
  3182. ++ptr;
  3183. } while(isspace(*ptr));
  3184. }
  3185. }
  3186. }
  3187. if(device)
  3188. ALCdevice_DecRef(device);
  3189. return bResult;
  3190. }
  3191. /* alcGetProcAddress
  3192. *
  3193. * Retrieves the function address for a particular extension function
  3194. */
  3195. ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
  3196. {
  3197. ALCvoid *ptr = NULL;
  3198. if(!funcName)
  3199. {
  3200. VerifyDevice(&device);
  3201. alcSetError(device, ALC_INVALID_VALUE);
  3202. if(device) ALCdevice_DecRef(device);
  3203. }
  3204. else
  3205. {
  3206. size_t i = 0;
  3207. for(i = 0;i < COUNTOF(alcFunctions);i++)
  3208. {
  3209. if(strcmp(alcFunctions[i].funcName, funcName) == 0)
  3210. {
  3211. ptr = alcFunctions[i].address;
  3212. break;
  3213. }
  3214. }
  3215. }
  3216. return ptr;
  3217. }
  3218. /* alcGetEnumValue
  3219. *
  3220. * Get the value for a particular ALC enumeration name
  3221. */
  3222. ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
  3223. {
  3224. ALCenum val = 0;
  3225. if(!enumName)
  3226. {
  3227. VerifyDevice(&device);
  3228. alcSetError(device, ALC_INVALID_VALUE);
  3229. if(device) ALCdevice_DecRef(device);
  3230. }
  3231. else
  3232. {
  3233. size_t i = 0;
  3234. for(i = 0;i < COUNTOF(alcEnumerations);i++)
  3235. {
  3236. if(strcmp(alcEnumerations[i].enumName, enumName) == 0)
  3237. {
  3238. val = alcEnumerations[i].value;
  3239. break;
  3240. }
  3241. }
  3242. }
  3243. return val;
  3244. }
  3245. /* alcCreateContext
  3246. *
  3247. * Create and attach a context to the given device.
  3248. */
  3249. ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
  3250. {
  3251. ALCcontext *ALContext;
  3252. ALfloat valf;
  3253. ALCenum err;
  3254. /* Explicitly hold the list lock while taking the BackendLock in case the
  3255. * device is asynchronously destropyed, to ensure this new context is
  3256. * properly cleaned up after being made.
  3257. */
  3258. LockLists();
  3259. if(!VerifyDevice(&device) || device->Type == Capture ||
  3260. !ATOMIC_LOAD(&device->Connected, almemory_order_relaxed))
  3261. {
  3262. UnlockLists();
  3263. alcSetError(device, ALC_INVALID_DEVICE);
  3264. if(device) ALCdevice_DecRef(device);
  3265. return NULL;
  3266. }
  3267. almtx_lock(&device->BackendLock);
  3268. UnlockLists();
  3269. ATOMIC_STORE_SEQ(&device->LastError, ALC_NO_ERROR);
  3270. if(device->Type == Playback && DefaultEffect.type != AL_EFFECT_NULL)
  3271. ALContext = al_calloc(16, sizeof(ALCcontext)+sizeof(ALlistener)+sizeof(ALeffectslot));
  3272. else
  3273. ALContext = al_calloc(16, sizeof(ALCcontext)+sizeof(ALlistener));
  3274. if(!ALContext)
  3275. {
  3276. almtx_unlock(&device->BackendLock);
  3277. alcSetError(device, ALC_OUT_OF_MEMORY);
  3278. ALCdevice_DecRef(device);
  3279. return NULL;
  3280. }
  3281. InitRef(&ALContext->ref, 1);
  3282. ALContext->Listener = (ALlistener*)ALContext->_listener_mem;
  3283. ALContext->DefaultSlot = NULL;
  3284. ALContext->Voices = NULL;
  3285. ALContext->VoiceCount = 0;
  3286. ALContext->MaxVoices = 0;
  3287. ATOMIC_INIT(&ALContext->ActiveAuxSlots, NULL);
  3288. ALContext->Device = device;
  3289. ATOMIC_INIT(&ALContext->next, NULL);
  3290. if((err=UpdateDeviceParams(device, attrList)) != ALC_NO_ERROR)
  3291. {
  3292. almtx_unlock(&device->BackendLock);
  3293. al_free(ALContext);
  3294. ALContext = NULL;
  3295. alcSetError(device, err);
  3296. if(err == ALC_INVALID_DEVICE)
  3297. {
  3298. V0(device->Backend,lock)();
  3299. aluHandleDisconnect(device, "Device update failure");
  3300. V0(device->Backend,unlock)();
  3301. }
  3302. ALCdevice_DecRef(device);
  3303. return NULL;
  3304. }
  3305. AllocateVoices(ALContext, 256, device->NumAuxSends);
  3306. if(DefaultEffect.type != AL_EFFECT_NULL && device->Type == Playback)
  3307. {
  3308. ALContext->DefaultSlot = (ALeffectslot*)(ALContext->_listener_mem + sizeof(ALlistener));
  3309. if(InitEffectSlot(ALContext->DefaultSlot) == AL_NO_ERROR)
  3310. aluInitEffectPanning(ALContext->DefaultSlot);
  3311. else
  3312. {
  3313. ALContext->DefaultSlot = NULL;
  3314. ERR("Failed to initialize the default effect slot\n");
  3315. }
  3316. }
  3317. ALCdevice_IncRef(ALContext->Device);
  3318. InitContext(ALContext);
  3319. if(ConfigValueFloat(alstr_get_cstr(device->DeviceName), NULL, "volume-adjust", &valf))
  3320. {
  3321. if(!isfinite(valf))
  3322. ERR("volume-adjust must be finite: %f\n", valf);
  3323. else
  3324. {
  3325. ALfloat db = clampf(valf, -24.0f, 24.0f);
  3326. if(db != valf)
  3327. WARN("volume-adjust clamped: %f, range: +/-%f\n", valf, 24.0f);
  3328. ALContext->GainBoost = powf(10.0f, db/20.0f);
  3329. TRACE("volume-adjust gain: %f\n", ALContext->GainBoost);
  3330. }
  3331. }
  3332. UpdateListenerProps(ALContext);
  3333. {
  3334. ALCcontext *head = ATOMIC_LOAD_SEQ(&device->ContextList);
  3335. do {
  3336. ATOMIC_STORE(&ALContext->next, head, almemory_order_relaxed);
  3337. } while(ATOMIC_COMPARE_EXCHANGE_PTR_WEAK_SEQ(&device->ContextList, &head,
  3338. ALContext) == 0);
  3339. }
  3340. almtx_unlock(&device->BackendLock);
  3341. if(ALContext->DefaultSlot)
  3342. {
  3343. if(InitializeEffect(ALContext, ALContext->DefaultSlot, &DefaultEffect) == AL_NO_ERROR)
  3344. UpdateEffectSlotProps(ALContext->DefaultSlot, ALContext);
  3345. else
  3346. ERR("Failed to initialize the default effect\n");
  3347. }
  3348. ALCdevice_DecRef(device);
  3349. TRACE("Created context %p\n", ALContext);
  3350. return ALContext;
  3351. }
  3352. /* alcDestroyContext
  3353. *
  3354. * Remove a context from its device
  3355. */
  3356. ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
  3357. {
  3358. ALCdevice *Device;
  3359. LockLists();
  3360. if(!VerifyContext(&context))
  3361. {
  3362. UnlockLists();
  3363. alcSetError(NULL, ALC_INVALID_CONTEXT);
  3364. return;
  3365. }
  3366. Device = context->Device;
  3367. if(Device)
  3368. {
  3369. almtx_lock(&Device->BackendLock);
  3370. if(!ReleaseContext(context, Device))
  3371. {
  3372. V0(Device->Backend,stop)();
  3373. Device->Flags &= ~DEVICE_RUNNING;
  3374. }
  3375. almtx_unlock(&Device->BackendLock);
  3376. }
  3377. UnlockLists();
  3378. ALCcontext_DecRef(context);
  3379. }
  3380. /* alcGetCurrentContext
  3381. *
  3382. * Returns the currently active context on the calling thread
  3383. */
  3384. ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(void)
  3385. {
  3386. ALCcontext *Context = altss_get(LocalContext);
  3387. if(!Context) Context = ATOMIC_LOAD_SEQ(&GlobalContext);
  3388. return Context;
  3389. }
  3390. /* alcGetThreadContext
  3391. *
  3392. * Returns the currently active thread-local context
  3393. */
  3394. ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
  3395. {
  3396. return altss_get(LocalContext);
  3397. }
  3398. /* alcMakeContextCurrent
  3399. *
  3400. * Makes the given context the active process-wide context, and removes the
  3401. * thread-local context for the calling thread.
  3402. */
  3403. ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
  3404. {
  3405. /* context must be valid or NULL */
  3406. if(context && !VerifyContext(&context))
  3407. {
  3408. alcSetError(NULL, ALC_INVALID_CONTEXT);
  3409. return ALC_FALSE;
  3410. }
  3411. /* context's reference count is already incremented */
  3412. context = ATOMIC_EXCHANGE_PTR_SEQ(&GlobalContext, context);
  3413. if(context) ALCcontext_DecRef(context);
  3414. if((context=altss_get(LocalContext)) != NULL)
  3415. {
  3416. altss_set(LocalContext, NULL);
  3417. ALCcontext_DecRef(context);
  3418. }
  3419. return ALC_TRUE;
  3420. }
  3421. /* alcSetThreadContext
  3422. *
  3423. * Makes the given context the active context for the current thread
  3424. */
  3425. ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
  3426. {
  3427. ALCcontext *old;
  3428. /* context must be valid or NULL */
  3429. if(context && !VerifyContext(&context))
  3430. {
  3431. alcSetError(NULL, ALC_INVALID_CONTEXT);
  3432. return ALC_FALSE;
  3433. }
  3434. /* context's reference count is already incremented */
  3435. old = altss_get(LocalContext);
  3436. altss_set(LocalContext, context);
  3437. if(old) ALCcontext_DecRef(old);
  3438. return ALC_TRUE;
  3439. }
  3440. /* alcGetContextsDevice
  3441. *
  3442. * Returns the device that a particular context is attached to
  3443. */
  3444. ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context)
  3445. {
  3446. ALCdevice *Device;
  3447. if(!VerifyContext(&Context))
  3448. {
  3449. alcSetError(NULL, ALC_INVALID_CONTEXT);
  3450. return NULL;
  3451. }
  3452. Device = Context->Device;
  3453. ALCcontext_DecRef(Context);
  3454. return Device;
  3455. }
  3456. /* alcOpenDevice
  3457. *
  3458. * Opens the named device.
  3459. */
  3460. ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
  3461. {
  3462. ALCbackendFactory *factory;
  3463. const ALCchar *fmt;
  3464. ALCdevice *device;
  3465. ALCenum err;
  3466. DO_INITCONFIG();
  3467. if(!PlaybackBackend.name)
  3468. {
  3469. alcSetError(NULL, ALC_INVALID_VALUE);
  3470. return NULL;
  3471. }
  3472. if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0
  3473. #ifdef _WIN32
  3474. /* Some old Windows apps hardcode these expecting OpenAL to use a
  3475. * specific audio API, even when they're not enumerated. Creative's
  3476. * router effectively ignores them too.
  3477. */
  3478. || strcasecmp(deviceName, "DirectSound3D") == 0 || strcasecmp(deviceName, "DirectSound") == 0
  3479. || strcasecmp(deviceName, "MMSYSTEM") == 0
  3480. #endif
  3481. ))
  3482. deviceName = NULL;
  3483. device = al_calloc(16, sizeof(ALCdevice));
  3484. if(!device)
  3485. {
  3486. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  3487. return NULL;
  3488. }
  3489. //Validate device
  3490. InitDevice(device, Playback);
  3491. //Set output format
  3492. device->FmtChans = DevFmtChannelsDefault;
  3493. device->FmtType = DevFmtTypeDefault;
  3494. device->Frequency = DEFAULT_OUTPUT_RATE;
  3495. device->IsHeadphones = AL_FALSE;
  3496. device->AmbiLayout = AmbiLayout_Default;
  3497. device->AmbiScale = AmbiNorm_Default;
  3498. device->LimiterState = ALC_TRUE;
  3499. device->NumUpdates = 3;
  3500. device->UpdateSize = 1024;
  3501. device->SourcesMax = 256;
  3502. device->AuxiliaryEffectSlotMax = 64;
  3503. device->NumAuxSends = DEFAULT_SENDS;
  3504. if(ConfigValueStr(deviceName, NULL, "channels", &fmt))
  3505. {
  3506. static const struct {
  3507. const char name[16];
  3508. enum DevFmtChannels chans;
  3509. ALsizei order;
  3510. } chanlist[] = {
  3511. { "mono", DevFmtMono, 0 },
  3512. { "stereo", DevFmtStereo, 0 },
  3513. { "quad", DevFmtQuad, 0 },
  3514. { "surround51", DevFmtX51, 0 },
  3515. { "surround61", DevFmtX61, 0 },
  3516. { "surround71", DevFmtX71, 0 },
  3517. { "surround51rear", DevFmtX51Rear, 0 },
  3518. { "ambi1", DevFmtAmbi3D, 1 },
  3519. { "ambi2", DevFmtAmbi3D, 2 },
  3520. { "ambi3", DevFmtAmbi3D, 3 },
  3521. };
  3522. size_t i;
  3523. for(i = 0;i < COUNTOF(chanlist);i++)
  3524. {
  3525. if(strcasecmp(chanlist[i].name, fmt) == 0)
  3526. {
  3527. device->FmtChans = chanlist[i].chans;
  3528. device->AmbiOrder = chanlist[i].order;
  3529. device->Flags |= DEVICE_CHANNELS_REQUEST;
  3530. break;
  3531. }
  3532. }
  3533. if(i == COUNTOF(chanlist))
  3534. ERR("Unsupported channels: %s\n", fmt);
  3535. }
  3536. if(ConfigValueStr(deviceName, NULL, "sample-type", &fmt))
  3537. {
  3538. static const struct {
  3539. const char name[16];
  3540. enum DevFmtType type;
  3541. } typelist[] = {
  3542. { "int8", DevFmtByte },
  3543. { "uint8", DevFmtUByte },
  3544. { "int16", DevFmtShort },
  3545. { "uint16", DevFmtUShort },
  3546. { "int32", DevFmtInt },
  3547. { "uint32", DevFmtUInt },
  3548. { "float32", DevFmtFloat },
  3549. };
  3550. size_t i;
  3551. for(i = 0;i < COUNTOF(typelist);i++)
  3552. {
  3553. if(strcasecmp(typelist[i].name, fmt) == 0)
  3554. {
  3555. device->FmtType = typelist[i].type;
  3556. device->Flags |= DEVICE_SAMPLE_TYPE_REQUEST;
  3557. break;
  3558. }
  3559. }
  3560. if(i == COUNTOF(typelist))
  3561. ERR("Unsupported sample-type: %s\n", fmt);
  3562. }
  3563. if(ConfigValueUInt(deviceName, NULL, "frequency", &device->Frequency))
  3564. {
  3565. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  3566. if(device->Frequency < MIN_OUTPUT_RATE)
  3567. ERR("%uhz request clamped to %uhz minimum\n", device->Frequency, MIN_OUTPUT_RATE);
  3568. device->Frequency = maxu(device->Frequency, MIN_OUTPUT_RATE);
  3569. }
  3570. ConfigValueUInt(deviceName, NULL, "periods", &device->NumUpdates);
  3571. device->NumUpdates = clampu(device->NumUpdates, 2, 16);
  3572. ConfigValueUInt(deviceName, NULL, "period_size", &device->UpdateSize);
  3573. device->UpdateSize = clampu(device->UpdateSize, 64, 8192);
  3574. if((CPUCapFlags&(CPU_CAP_SSE|CPU_CAP_NEON)) != 0)
  3575. device->UpdateSize = (device->UpdateSize+3)&~3;
  3576. ConfigValueUInt(deviceName, NULL, "sources", &device->SourcesMax);
  3577. if(device->SourcesMax == 0) device->SourcesMax = 256;
  3578. ConfigValueUInt(deviceName, NULL, "slots", &device->AuxiliaryEffectSlotMax);
  3579. if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 64;
  3580. else device->AuxiliaryEffectSlotMax = minu(device->AuxiliaryEffectSlotMax, INT_MAX);
  3581. if(ConfigValueInt(deviceName, NULL, "sends", &device->NumAuxSends))
  3582. device->NumAuxSends = clampi(
  3583. DEFAULT_SENDS, 0, clampi(device->NumAuxSends, 0, MAX_SENDS)
  3584. );
  3585. device->NumStereoSources = 1;
  3586. device->NumMonoSources = device->SourcesMax - device->NumStereoSources;
  3587. factory = PlaybackBackend.getFactory();
  3588. device->Backend = V(factory,createBackend)(device, ALCbackend_Playback);
  3589. if(!device->Backend)
  3590. {
  3591. FreeDevice(device);
  3592. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  3593. return NULL;
  3594. }
  3595. // Find a playback device to open
  3596. if((err=V(device->Backend,open)(deviceName)) != ALC_NO_ERROR)
  3597. {
  3598. FreeDevice(device);
  3599. alcSetError(NULL, err);
  3600. return NULL;
  3601. }
  3602. if(ConfigValueStr(alstr_get_cstr(device->DeviceName), NULL, "ambi-format", &fmt))
  3603. {
  3604. if(strcasecmp(fmt, "fuma") == 0)
  3605. {
  3606. device->AmbiLayout = AmbiLayout_FuMa;
  3607. device->AmbiScale = AmbiNorm_FuMa;
  3608. }
  3609. else if(strcasecmp(fmt, "acn+sn3d") == 0)
  3610. {
  3611. device->AmbiLayout = AmbiLayout_ACN;
  3612. device->AmbiScale = AmbiNorm_SN3D;
  3613. }
  3614. else if(strcasecmp(fmt, "acn+n3d") == 0)
  3615. {
  3616. device->AmbiLayout = AmbiLayout_ACN;
  3617. device->AmbiScale = AmbiNorm_N3D;
  3618. }
  3619. else
  3620. ERR("Unsupported ambi-format: %s\n", fmt);
  3621. }
  3622. {
  3623. ALCdevice *head = ATOMIC_LOAD_SEQ(&DeviceList);
  3624. do {
  3625. ATOMIC_STORE(&device->next, head, almemory_order_relaxed);
  3626. } while(!ATOMIC_COMPARE_EXCHANGE_PTR_WEAK_SEQ(&DeviceList, &head, device));
  3627. }
  3628. TRACE("Created device %p, \"%s\"\n", device, alstr_get_cstr(device->DeviceName));
  3629. return device;
  3630. }
  3631. /* alcCloseDevice
  3632. *
  3633. * Closes the given device.
  3634. */
  3635. ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *device)
  3636. {
  3637. ALCdevice *iter, *origdev, *nextdev;
  3638. ALCcontext *ctx;
  3639. LockLists();
  3640. iter = ATOMIC_LOAD_SEQ(&DeviceList);
  3641. do {
  3642. if(iter == device)
  3643. break;
  3644. iter = ATOMIC_LOAD(&iter->next, almemory_order_relaxed);
  3645. } while(iter != NULL);
  3646. if(!iter || iter->Type == Capture)
  3647. {
  3648. alcSetError(iter, ALC_INVALID_DEVICE);
  3649. UnlockLists();
  3650. return ALC_FALSE;
  3651. }
  3652. almtx_lock(&device->BackendLock);
  3653. origdev = device;
  3654. nextdev = ATOMIC_LOAD(&device->next, almemory_order_relaxed);
  3655. if(!ATOMIC_COMPARE_EXCHANGE_PTR_STRONG_SEQ(&DeviceList, &origdev, nextdev))
  3656. {
  3657. ALCdevice *list;
  3658. do {
  3659. list = origdev;
  3660. origdev = device;
  3661. } while(!ATOMIC_COMPARE_EXCHANGE_PTR_STRONG_SEQ(&list->next, &origdev, nextdev));
  3662. }
  3663. UnlockLists();
  3664. ctx = ATOMIC_LOAD_SEQ(&device->ContextList);
  3665. while(ctx != NULL)
  3666. {
  3667. ALCcontext *next = ATOMIC_LOAD(&ctx->next, almemory_order_relaxed);
  3668. WARN("Releasing context %p\n", ctx);
  3669. ReleaseContext(ctx, device);
  3670. ctx = next;
  3671. }
  3672. if((device->Flags&DEVICE_RUNNING))
  3673. V0(device->Backend,stop)();
  3674. device->Flags &= ~DEVICE_RUNNING;
  3675. almtx_unlock(&device->BackendLock);
  3676. ALCdevice_DecRef(device);
  3677. return ALC_TRUE;
  3678. }
  3679. /************************************************
  3680. * ALC capture functions
  3681. ************************************************/
  3682. ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei samples)
  3683. {
  3684. ALCbackendFactory *factory;
  3685. ALCdevice *device = NULL;
  3686. ALCenum err;
  3687. DO_INITCONFIG();
  3688. if(!CaptureBackend.name)
  3689. {
  3690. alcSetError(NULL, ALC_INVALID_VALUE);
  3691. return NULL;
  3692. }
  3693. if(samples <= 0)
  3694. {
  3695. alcSetError(NULL, ALC_INVALID_VALUE);
  3696. return NULL;
  3697. }
  3698. if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
  3699. deviceName = NULL;
  3700. device = al_calloc(16, sizeof(ALCdevice));
  3701. if(!device)
  3702. {
  3703. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  3704. return NULL;
  3705. }
  3706. //Validate device
  3707. InitDevice(device, Capture);
  3708. device->Frequency = frequency;
  3709. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  3710. if(DecomposeDevFormat(format, &device->FmtChans, &device->FmtType) == AL_FALSE)
  3711. {
  3712. FreeDevice(device);
  3713. alcSetError(NULL, ALC_INVALID_ENUM);
  3714. return NULL;
  3715. }
  3716. device->Flags |= DEVICE_CHANNELS_REQUEST | DEVICE_SAMPLE_TYPE_REQUEST;
  3717. device->IsHeadphones = AL_FALSE;
  3718. device->AmbiOrder = 0;
  3719. device->AmbiLayout = AmbiLayout_Default;
  3720. device->AmbiScale = AmbiNorm_Default;
  3721. device->UpdateSize = samples;
  3722. device->NumUpdates = 1;
  3723. factory = CaptureBackend.getFactory();
  3724. device->Backend = V(factory,createBackend)(device, ALCbackend_Capture);
  3725. if(!device->Backend)
  3726. {
  3727. FreeDevice(device);
  3728. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  3729. return NULL;
  3730. }
  3731. TRACE("Capture format: %s, %s, %uhz, %u update size x%d\n",
  3732. DevFmtChannelsString(device->FmtChans), DevFmtTypeString(device->FmtType),
  3733. device->Frequency, device->UpdateSize, device->NumUpdates
  3734. );
  3735. if((err=V(device->Backend,open)(deviceName)) != ALC_NO_ERROR)
  3736. {
  3737. FreeDevice(device);
  3738. alcSetError(NULL, err);
  3739. return NULL;
  3740. }
  3741. {
  3742. ALCdevice *head = ATOMIC_LOAD_SEQ(&DeviceList);
  3743. do {
  3744. ATOMIC_STORE(&device->next, head, almemory_order_relaxed);
  3745. } while(!ATOMIC_COMPARE_EXCHANGE_PTR_WEAK_SEQ(&DeviceList, &head, device));
  3746. }
  3747. TRACE("Created device %p, \"%s\"\n", device, alstr_get_cstr(device->DeviceName));
  3748. return device;
  3749. }
  3750. ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *device)
  3751. {
  3752. ALCdevice *iter, *origdev, *nextdev;
  3753. LockLists();
  3754. iter = ATOMIC_LOAD_SEQ(&DeviceList);
  3755. do {
  3756. if(iter == device)
  3757. break;
  3758. iter = ATOMIC_LOAD(&iter->next, almemory_order_relaxed);
  3759. } while(iter != NULL);
  3760. if(!iter || iter->Type != Capture)
  3761. {
  3762. alcSetError(iter, ALC_INVALID_DEVICE);
  3763. UnlockLists();
  3764. return ALC_FALSE;
  3765. }
  3766. origdev = device;
  3767. nextdev = ATOMIC_LOAD(&device->next, almemory_order_relaxed);
  3768. if(!ATOMIC_COMPARE_EXCHANGE_PTR_STRONG_SEQ(&DeviceList, &origdev, nextdev))
  3769. {
  3770. ALCdevice *list;
  3771. do {
  3772. list = origdev;
  3773. origdev = device;
  3774. } while(!ATOMIC_COMPARE_EXCHANGE_PTR_STRONG_SEQ(&list->next, &origdev, nextdev));
  3775. }
  3776. UnlockLists();
  3777. almtx_lock(&device->BackendLock);
  3778. if((device->Flags&DEVICE_RUNNING))
  3779. V0(device->Backend,stop)();
  3780. device->Flags &= ~DEVICE_RUNNING;
  3781. almtx_unlock(&device->BackendLock);
  3782. ALCdevice_DecRef(device);
  3783. return ALC_TRUE;
  3784. }
  3785. ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
  3786. {
  3787. if(!VerifyDevice(&device) || device->Type != Capture)
  3788. alcSetError(device, ALC_INVALID_DEVICE);
  3789. else
  3790. {
  3791. almtx_lock(&device->BackendLock);
  3792. if(!ATOMIC_LOAD(&device->Connected, almemory_order_acquire))
  3793. alcSetError(device, ALC_INVALID_DEVICE);
  3794. else if(!(device->Flags&DEVICE_RUNNING))
  3795. {
  3796. if(V0(device->Backend,start)())
  3797. device->Flags |= DEVICE_RUNNING;
  3798. else
  3799. {
  3800. aluHandleDisconnect(device, "Device start failure");
  3801. alcSetError(device, ALC_INVALID_DEVICE);
  3802. }
  3803. }
  3804. almtx_unlock(&device->BackendLock);
  3805. }
  3806. if(device) ALCdevice_DecRef(device);
  3807. }
  3808. ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
  3809. {
  3810. if(!VerifyDevice(&device) || device->Type != Capture)
  3811. alcSetError(device, ALC_INVALID_DEVICE);
  3812. else
  3813. {
  3814. almtx_lock(&device->BackendLock);
  3815. if((device->Flags&DEVICE_RUNNING))
  3816. V0(device->Backend,stop)();
  3817. device->Flags &= ~DEVICE_RUNNING;
  3818. almtx_unlock(&device->BackendLock);
  3819. }
  3820. if(device) ALCdevice_DecRef(device);
  3821. }
  3822. ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
  3823. {
  3824. if(!VerifyDevice(&device) || device->Type != Capture)
  3825. alcSetError(device, ALC_INVALID_DEVICE);
  3826. else
  3827. {
  3828. ALCenum err = ALC_INVALID_VALUE;
  3829. almtx_lock(&device->BackendLock);
  3830. if(samples >= 0 && V0(device->Backend,availableSamples)() >= (ALCuint)samples)
  3831. err = V(device->Backend,captureSamples)(buffer, samples);
  3832. almtx_unlock(&device->BackendLock);
  3833. if(err != ALC_NO_ERROR)
  3834. alcSetError(device, err);
  3835. }
  3836. if(device) ALCdevice_DecRef(device);
  3837. }
  3838. /************************************************
  3839. * ALC loopback functions
  3840. ************************************************/
  3841. /* alcLoopbackOpenDeviceSOFT
  3842. *
  3843. * Open a loopback device, for manual rendering.
  3844. */
  3845. ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceName)
  3846. {
  3847. ALCbackendFactory *factory;
  3848. ALCdevice *device;
  3849. DO_INITCONFIG();
  3850. /* Make sure the device name, if specified, is us. */
  3851. if(deviceName && strcmp(deviceName, alcDefaultName) != 0)
  3852. {
  3853. alcSetError(NULL, ALC_INVALID_VALUE);
  3854. return NULL;
  3855. }
  3856. device = al_calloc(16, sizeof(ALCdevice));
  3857. if(!device)
  3858. {
  3859. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  3860. return NULL;
  3861. }
  3862. //Validate device
  3863. InitDevice(device, Loopback);
  3864. device->SourcesMax = 256;
  3865. device->AuxiliaryEffectSlotMax = 64;
  3866. device->NumAuxSends = DEFAULT_SENDS;
  3867. //Set output format
  3868. device->NumUpdates = 0;
  3869. device->UpdateSize = 0;
  3870. device->Frequency = DEFAULT_OUTPUT_RATE;
  3871. device->FmtChans = DevFmtChannelsDefault;
  3872. device->FmtType = DevFmtTypeDefault;
  3873. device->IsHeadphones = AL_FALSE;
  3874. device->AmbiLayout = AmbiLayout_Default;
  3875. device->AmbiScale = AmbiNorm_Default;
  3876. ConfigValueUInt(NULL, NULL, "sources", &device->SourcesMax);
  3877. if(device->SourcesMax == 0) device->SourcesMax = 256;
  3878. ConfigValueUInt(NULL, NULL, "slots", &device->AuxiliaryEffectSlotMax);
  3879. if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 64;
  3880. else device->AuxiliaryEffectSlotMax = minu(device->AuxiliaryEffectSlotMax, INT_MAX);
  3881. if(ConfigValueInt(NULL, NULL, "sends", &device->NumAuxSends))
  3882. device->NumAuxSends = clampi(
  3883. DEFAULT_SENDS, 0, clampi(device->NumAuxSends, 0, MAX_SENDS)
  3884. );
  3885. device->NumStereoSources = 1;
  3886. device->NumMonoSources = device->SourcesMax - device->NumStereoSources;
  3887. factory = ALCloopbackFactory_getFactory();
  3888. device->Backend = V(factory,createBackend)(device, ALCbackend_Loopback);
  3889. if(!device->Backend)
  3890. {
  3891. al_free(device);
  3892. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  3893. return NULL;
  3894. }
  3895. // Open the "backend"
  3896. V(device->Backend,open)("Loopback");
  3897. {
  3898. ALCdevice *head = ATOMIC_LOAD_SEQ(&DeviceList);
  3899. do {
  3900. ATOMIC_STORE(&device->next, head, almemory_order_relaxed);
  3901. } while(!ATOMIC_COMPARE_EXCHANGE_PTR_WEAK_SEQ(&DeviceList, &head, device));
  3902. }
  3903. TRACE("Created device %p\n", device);
  3904. return device;
  3905. }
  3906. /* alcIsRenderFormatSupportedSOFT
  3907. *
  3908. * Determines if the loopback device supports the given format for rendering.
  3909. */
  3910. ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type)
  3911. {
  3912. ALCboolean ret = ALC_FALSE;
  3913. if(!VerifyDevice(&device) || device->Type != Loopback)
  3914. alcSetError(device, ALC_INVALID_DEVICE);
  3915. else if(freq <= 0)
  3916. alcSetError(device, ALC_INVALID_VALUE);
  3917. else
  3918. {
  3919. if(IsValidALCType(type) && IsValidALCChannels(channels) && freq >= MIN_OUTPUT_RATE)
  3920. ret = ALC_TRUE;
  3921. }
  3922. if(device) ALCdevice_DecRef(device);
  3923. return ret;
  3924. }
  3925. /* alcRenderSamplesSOFT
  3926. *
  3927. * Renders some samples into a buffer, using the format last set by the
  3928. * attributes given to alcCreateContext.
  3929. */
  3930. FORCE_ALIGN ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
  3931. {
  3932. if(!VerifyDevice(&device) || device->Type != Loopback)
  3933. alcSetError(device, ALC_INVALID_DEVICE);
  3934. else if(samples < 0 || (samples > 0 && buffer == NULL))
  3935. alcSetError(device, ALC_INVALID_VALUE);
  3936. else
  3937. {
  3938. V0(device->Backend,lock)();
  3939. aluMixData(device, buffer, samples);
  3940. V0(device->Backend,unlock)();
  3941. }
  3942. if(device) ALCdevice_DecRef(device);
  3943. }
  3944. /************************************************
  3945. * ALC DSP pause/resume functions
  3946. ************************************************/
  3947. /* alcDevicePauseSOFT
  3948. *
  3949. * Pause the DSP to stop audio processing.
  3950. */
  3951. ALC_API void ALC_APIENTRY alcDevicePauseSOFT(ALCdevice *device)
  3952. {
  3953. if(!VerifyDevice(&device) || device->Type != Playback)
  3954. alcSetError(device, ALC_INVALID_DEVICE);
  3955. else
  3956. {
  3957. almtx_lock(&device->BackendLock);
  3958. if((device->Flags&DEVICE_RUNNING))
  3959. V0(device->Backend,stop)();
  3960. device->Flags &= ~DEVICE_RUNNING;
  3961. device->Flags |= DEVICE_PAUSED;
  3962. almtx_unlock(&device->BackendLock);
  3963. }
  3964. if(device) ALCdevice_DecRef(device);
  3965. }
  3966. /* alcDeviceResumeSOFT
  3967. *
  3968. * Resume the DSP to restart audio processing.
  3969. */
  3970. ALC_API void ALC_APIENTRY alcDeviceResumeSOFT(ALCdevice *device)
  3971. {
  3972. if(!VerifyDevice(&device) || device->Type != Playback)
  3973. alcSetError(device, ALC_INVALID_DEVICE);
  3974. else
  3975. {
  3976. almtx_lock(&device->BackendLock);
  3977. if((device->Flags&DEVICE_PAUSED))
  3978. {
  3979. device->Flags &= ~DEVICE_PAUSED;
  3980. if(ATOMIC_LOAD_SEQ(&device->ContextList) != NULL)
  3981. {
  3982. if(V0(device->Backend,start)() != ALC_FALSE)
  3983. device->Flags |= DEVICE_RUNNING;
  3984. else
  3985. {
  3986. V0(device->Backend,lock)();
  3987. aluHandleDisconnect(device, "Device start failure");
  3988. V0(device->Backend,unlock)();
  3989. alcSetError(device, ALC_INVALID_DEVICE);
  3990. }
  3991. }
  3992. }
  3993. almtx_unlock(&device->BackendLock);
  3994. }
  3995. if(device) ALCdevice_DecRef(device);
  3996. }
  3997. /************************************************
  3998. * ALC HRTF functions
  3999. ************************************************/
  4000. /* alcGetStringiSOFT
  4001. *
  4002. * Gets a string parameter at the given index.
  4003. */
  4004. ALC_API const ALCchar* ALC_APIENTRY alcGetStringiSOFT(ALCdevice *device, ALCenum paramName, ALCsizei index)
  4005. {
  4006. const ALCchar *str = NULL;
  4007. if(!VerifyDevice(&device) || device->Type == Capture)
  4008. alcSetError(device, ALC_INVALID_DEVICE);
  4009. else switch(paramName)
  4010. {
  4011. case ALC_HRTF_SPECIFIER_SOFT:
  4012. if(index >= 0 && (size_t)index < VECTOR_SIZE(device->HrtfList))
  4013. str = alstr_get_cstr(VECTOR_ELEM(device->HrtfList, index).name);
  4014. else
  4015. alcSetError(device, ALC_INVALID_VALUE);
  4016. break;
  4017. default:
  4018. alcSetError(device, ALC_INVALID_ENUM);
  4019. break;
  4020. }
  4021. if(device) ALCdevice_DecRef(device);
  4022. return str;
  4023. }
  4024. /* alcResetDeviceSOFT
  4025. *
  4026. * Resets the given device output, using the specified attribute list.
  4027. */
  4028. ALC_API ALCboolean ALC_APIENTRY alcResetDeviceSOFT(ALCdevice *device, const ALCint *attribs)
  4029. {
  4030. ALCenum err;
  4031. LockLists();
  4032. if(!VerifyDevice(&device) || device->Type == Capture ||
  4033. !ATOMIC_LOAD(&device->Connected, almemory_order_relaxed))
  4034. {
  4035. UnlockLists();
  4036. alcSetError(device, ALC_INVALID_DEVICE);
  4037. if(device) ALCdevice_DecRef(device);
  4038. return ALC_FALSE;
  4039. }
  4040. almtx_lock(&device->BackendLock);
  4041. UnlockLists();
  4042. err = UpdateDeviceParams(device, attribs);
  4043. almtx_unlock(&device->BackendLock);
  4044. if(err != ALC_NO_ERROR)
  4045. {
  4046. alcSetError(device, err);
  4047. if(err == ALC_INVALID_DEVICE)
  4048. {
  4049. V0(device->Backend,lock)();
  4050. aluHandleDisconnect(device, "Device start failure");
  4051. V0(device->Backend,unlock)();
  4052. }
  4053. ALCdevice_DecRef(device);
  4054. return ALC_FALSE;
  4055. }
  4056. ALCdevice_DecRef(device);
  4057. return ALC_TRUE;
  4058. }