ALc.c 134 KB

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