ALc.c 118 KB

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