ALc.c 138 KB

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