source.cpp 167 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032
  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 "source.h"
  22. #include <algorithm>
  23. #include <array>
  24. #include <atomic>
  25. #include <bitset>
  26. #include <cassert>
  27. #include <chrono>
  28. #include <cinttypes>
  29. #include <cmath>
  30. #include <cstdint>
  31. #include <cstdio>
  32. #include <exception>
  33. #include <iterator>
  34. #include <limits>
  35. #include <memory>
  36. #include <mutex>
  37. #include <numeric>
  38. #include <optional>
  39. #include <ratio>
  40. #include <stdexcept>
  41. #include <string>
  42. #include <tuple>
  43. #include <type_traits>
  44. #include <unordered_map>
  45. #include <utility>
  46. #include <vector>
  47. #include "AL/al.h"
  48. #include "AL/alc.h"
  49. #include "AL/alext.h"
  50. #include "AL/efx.h"
  51. #include "albit.h"
  52. #include "alc/backends/base.h"
  53. #include "alc/context.h"
  54. #include "alc/device.h"
  55. #include "alc/inprogext.h"
  56. #include "almalloc.h"
  57. #include "alnumeric.h"
  58. #include "alspan.h"
  59. #include "atomic.h"
  60. #include "auxeffectslot.h"
  61. #include "buffer.h"
  62. #include "core/buffer_storage.h"
  63. #include "core/logging.h"
  64. #include "core/mixer/defs.h"
  65. #include "core/voice_change.h"
  66. #include "direct_defs.h"
  67. #include "error.h"
  68. #include "filter.h"
  69. #include "flexarray.h"
  70. #include "intrusive_ptr.h"
  71. #include "opthelpers.h"
  72. #ifdef ALSOFT_EAX
  73. #include "eax/api.h"
  74. #include "eax/call.h"
  75. #include "eax/fx_slot_index.h"
  76. #include "eax/utils.h"
  77. #endif
  78. namespace {
  79. using SubListAllocator = al::allocator<std::array<ALsource,64>>;
  80. using std::chrono::nanoseconds;
  81. using seconds_d = std::chrono::duration<double>;
  82. using source_store_array = std::array<ALsource*,3>;
  83. using source_store_vector = std::vector<ALsource*>;
  84. using source_store_variant = std::variant<std::monostate,source_store_array,source_store_vector>;
  85. Voice *GetSourceVoice(ALsource *source, ALCcontext *context)
  86. {
  87. auto voicelist = context->getVoicesSpan();
  88. ALuint idx{source->VoiceIdx};
  89. if(idx < voicelist.size())
  90. {
  91. ALuint sid{source->id};
  92. Voice *voice = voicelist[idx];
  93. if(voice->mSourceID.load(std::memory_order_acquire) == sid)
  94. return voice;
  95. }
  96. source->VoiceIdx = InvalidVoiceIndex;
  97. return nullptr;
  98. }
  99. void UpdateSourceProps(const ALsource *source, Voice *voice, ALCcontext *context)
  100. {
  101. /* Get an unused property container, or allocate a new one as needed. */
  102. VoicePropsItem *props{context->mFreeVoiceProps.load(std::memory_order_acquire)};
  103. if(!props)
  104. {
  105. context->allocVoiceProps();
  106. props = context->mFreeVoiceProps.load(std::memory_order_acquire);
  107. }
  108. VoicePropsItem *next;
  109. do {
  110. next = props->next.load(std::memory_order_relaxed);
  111. } while(context->mFreeVoiceProps.compare_exchange_weak(props, next,
  112. std::memory_order_acq_rel, std::memory_order_acquire) == false);
  113. props->Pitch = source->Pitch;
  114. props->Gain = source->Gain;
  115. props->OuterGain = source->OuterGain;
  116. props->MinGain = source->MinGain;
  117. props->MaxGain = source->MaxGain;
  118. props->InnerAngle = source->InnerAngle;
  119. props->OuterAngle = source->OuterAngle;
  120. props->RefDistance = source->RefDistance;
  121. props->MaxDistance = source->MaxDistance;
  122. props->RolloffFactor = source->RolloffFactor
  123. #ifdef ALSOFT_EAX
  124. + source->RolloffFactor2
  125. #endif
  126. ;
  127. props->Position = source->Position;
  128. props->Velocity = source->Velocity;
  129. props->Direction = source->Direction;
  130. props->OrientAt = source->OrientAt;
  131. props->OrientUp = source->OrientUp;
  132. props->HeadRelative = source->HeadRelative;
  133. props->mDistanceModel = source->mDistanceModel;
  134. props->mResampler = source->mResampler;
  135. props->DirectChannels = source->DirectChannels;
  136. props->mSpatializeMode = source->mSpatialize;
  137. props->DryGainHFAuto = source->DryGainHFAuto;
  138. props->WetGainAuto = source->WetGainAuto;
  139. props->WetGainHFAuto = source->WetGainHFAuto;
  140. props->OuterGainHF = source->OuterGainHF;
  141. props->AirAbsorptionFactor = source->AirAbsorptionFactor;
  142. props->RoomRolloffFactor = source->RoomRolloffFactor;
  143. props->DopplerFactor = source->DopplerFactor;
  144. props->StereoPan = source->StereoPan;
  145. props->Radius = source->Radius;
  146. props->EnhWidth = source->EnhWidth;
  147. props->Panning = source->mPanningEnabled ? source->mPan : 0.0f;
  148. props->Direct.Gain = source->Direct.Gain;
  149. props->Direct.GainHF = source->Direct.GainHF;
  150. props->Direct.HFReference = source->Direct.HFReference;
  151. props->Direct.GainLF = source->Direct.GainLF;
  152. props->Direct.LFReference = source->Direct.LFReference;
  153. auto copy_send = [](const ALsource::SendData &srcsend) noexcept -> VoiceProps::SendData
  154. {
  155. VoiceProps::SendData ret{};
  156. ret.Slot = srcsend.Slot ? srcsend.Slot->mSlot : nullptr;
  157. ret.Gain = srcsend.Gain;
  158. ret.GainHF = srcsend.GainHF;
  159. ret.HFReference = srcsend.HFReference;
  160. ret.GainLF = srcsend.GainLF;
  161. ret.LFReference = srcsend.LFReference;
  162. return ret;
  163. };
  164. std::transform(source->Send.cbegin(), source->Send.cend(), props->Send.begin(), copy_send);
  165. if(!props->Send[0].Slot && context->mDefaultSlot)
  166. props->Send[0].Slot = context->mDefaultSlot->mSlot;
  167. /* Set the new container for updating internal parameters. */
  168. props = voice->mUpdate.exchange(props, std::memory_order_acq_rel);
  169. if(props)
  170. {
  171. /* If there was an unused update container, put it back in the
  172. * freelist.
  173. */
  174. AtomicReplaceHead(context->mFreeVoiceProps, props);
  175. }
  176. }
  177. /* GetSourceSampleOffset
  178. *
  179. * Gets the current read offset for the given Source, in 32.32 fixed-point
  180. * samples. The offset is relative to the start of the queue (not the start of
  181. * the current buffer).
  182. */
  183. int64_t GetSourceSampleOffset(ALsource *Source, ALCcontext *context, nanoseconds *clocktime)
  184. {
  185. ALCdevice *device{context->mALDevice.get()};
  186. const VoiceBufferItem *Current{};
  187. int64_t readPos{};
  188. uint refcount{};
  189. Voice *voice{};
  190. do {
  191. refcount = device->waitForMix();
  192. *clocktime = device->getClockTime();
  193. voice = GetSourceVoice(Source, context);
  194. if(voice)
  195. {
  196. Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
  197. readPos = int64_t{voice->mPosition.load(std::memory_order_relaxed)} << MixerFracBits;
  198. readPos += voice->mPositionFrac.load(std::memory_order_relaxed);
  199. }
  200. std::atomic_thread_fence(std::memory_order_acquire);
  201. } while(refcount != device->mMixCount.load(std::memory_order_relaxed));
  202. if(!voice)
  203. return 0;
  204. for(auto &item : Source->mQueue)
  205. {
  206. if(&item == Current) break;
  207. readPos += int64_t{item.mSampleLen} << MixerFracBits;
  208. }
  209. if(readPos > std::numeric_limits<int64_t>::max() >> (32-MixerFracBits))
  210. return std::numeric_limits<int64_t>::max();
  211. return readPos << (32-MixerFracBits);
  212. }
  213. /* GetSourceSecOffset
  214. *
  215. * Gets the current read offset for the given Source, in seconds. The offset is
  216. * relative to the start of the queue (not the start of the current buffer).
  217. */
  218. double GetSourceSecOffset(ALsource *Source, ALCcontext *context, nanoseconds *clocktime)
  219. {
  220. ALCdevice *device{context->mALDevice.get()};
  221. const VoiceBufferItem *Current{};
  222. int64_t readPos{};
  223. uint refcount{};
  224. Voice *voice{};
  225. do {
  226. refcount = device->waitForMix();
  227. *clocktime = device->getClockTime();
  228. voice = GetSourceVoice(Source, context);
  229. if(voice)
  230. {
  231. Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
  232. readPos = int64_t{voice->mPosition.load(std::memory_order_relaxed)} << MixerFracBits;
  233. readPos += voice->mPositionFrac.load(std::memory_order_relaxed);
  234. }
  235. std::atomic_thread_fence(std::memory_order_acquire);
  236. } while(refcount != device->mMixCount.load(std::memory_order_relaxed));
  237. if(!voice)
  238. return 0.0f;
  239. const ALbuffer *BufferFmt{nullptr};
  240. auto BufferList = Source->mQueue.cbegin();
  241. while(BufferList != Source->mQueue.cend() && al::to_address(BufferList) != Current)
  242. {
  243. if(!BufferFmt) BufferFmt = BufferList->mBuffer;
  244. readPos += int64_t{BufferList->mSampleLen} << MixerFracBits;
  245. ++BufferList;
  246. }
  247. while(BufferList != Source->mQueue.cend() && !BufferFmt)
  248. {
  249. BufferFmt = BufferList->mBuffer;
  250. ++BufferList;
  251. }
  252. ASSUME(BufferFmt != nullptr);
  253. return static_cast<double>(readPos) / double{MixerFracOne} / BufferFmt->mSampleRate;
  254. }
  255. /* GetSourceOffset
  256. *
  257. * Gets the current read offset for the given Source, in the appropriate format
  258. * (Bytes, Samples or Seconds). The offset is relative to the start of the
  259. * queue (not the start of the current buffer).
  260. */
  261. template<typename T>
  262. NOINLINE T GetSourceOffset(ALsource *Source, ALenum name, ALCcontext *context)
  263. {
  264. ALCdevice *device{context->mALDevice.get()};
  265. const VoiceBufferItem *Current{};
  266. int64_t readPos{};
  267. uint readPosFrac{};
  268. uint refcount;
  269. Voice *voice;
  270. do {
  271. refcount = device->waitForMix();
  272. voice = GetSourceVoice(Source, context);
  273. if(voice)
  274. {
  275. Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
  276. readPos = voice->mPosition.load(std::memory_order_relaxed);
  277. readPosFrac = voice->mPositionFrac.load(std::memory_order_relaxed);
  278. }
  279. std::atomic_thread_fence(std::memory_order_acquire);
  280. } while(refcount != device->mMixCount.load(std::memory_order_relaxed));
  281. if(!voice)
  282. return T{0};
  283. const ALbuffer *BufferFmt{nullptr};
  284. auto BufferList = Source->mQueue.cbegin();
  285. while(BufferList != Source->mQueue.cend() && al::to_address(BufferList) != Current)
  286. {
  287. if(!BufferFmt) BufferFmt = BufferList->mBuffer;
  288. readPos += BufferList->mSampleLen;
  289. ++BufferList;
  290. }
  291. while(BufferList != Source->mQueue.cend() && !BufferFmt)
  292. {
  293. BufferFmt = BufferList->mBuffer;
  294. ++BufferList;
  295. }
  296. ASSUME(BufferFmt != nullptr);
  297. T offset{};
  298. switch(name)
  299. {
  300. case AL_SEC_OFFSET:
  301. if constexpr(std::is_floating_point_v<T>)
  302. {
  303. offset = static_cast<T>(readPos) + static_cast<T>(readPosFrac)/T{MixerFracOne};
  304. offset /= static_cast<T>(BufferFmt->mSampleRate);
  305. }
  306. else
  307. {
  308. readPos /= BufferFmt->mSampleRate;
  309. offset = static_cast<T>(std::clamp<int64_t>(readPos, std::numeric_limits<T>::min(),
  310. std::numeric_limits<T>::max()));
  311. }
  312. break;
  313. case AL_SAMPLE_OFFSET:
  314. if constexpr(std::is_floating_point_v<T>)
  315. offset = static_cast<T>(readPos) + static_cast<T>(readPosFrac)/T{MixerFracOne};
  316. else
  317. offset = static_cast<T>(std::clamp<int64_t>(readPos, std::numeric_limits<T>::min(),
  318. std::numeric_limits<T>::max()));
  319. break;
  320. case AL_BYTE_OFFSET:
  321. const ALuint BlockSamples{BufferFmt->mBlockAlign};
  322. const ALuint BlockSize{BufferFmt->blockSizeFromFmt()};
  323. /* Round down to the block boundary. */
  324. readPos = readPos / BlockSamples * BlockSize;
  325. if constexpr(std::is_floating_point_v<T>)
  326. offset = static_cast<T>(readPos);
  327. else
  328. {
  329. if(readPos > std::numeric_limits<T>::max())
  330. offset = RoundDown(std::numeric_limits<T>::max(), static_cast<T>(BlockSize));
  331. else if(readPos < std::numeric_limits<T>::min())
  332. offset = RoundUp(std::numeric_limits<T>::min(), static_cast<T>(BlockSize));
  333. else
  334. offset = static_cast<T>(readPos);
  335. }
  336. break;
  337. }
  338. return offset;
  339. }
  340. /* GetSourceLength
  341. *
  342. * Gets the length of the given Source's buffer queue, in the appropriate
  343. * format (Bytes, Samples or Seconds).
  344. */
  345. template<typename T>
  346. NOINLINE T GetSourceLength(const ALsource *source, ALenum name)
  347. {
  348. uint64_t length{0};
  349. const ALbuffer *BufferFmt{nullptr};
  350. for(auto &listitem : source->mQueue)
  351. {
  352. if(!BufferFmt)
  353. BufferFmt = listitem.mBuffer;
  354. length += listitem.mSampleLen;
  355. }
  356. if(length == 0)
  357. return T{0};
  358. ASSUME(BufferFmt != nullptr);
  359. switch(name)
  360. {
  361. case AL_SEC_LENGTH_SOFT:
  362. if constexpr(std::is_floating_point_v<T>)
  363. return static_cast<T>(length) / static_cast<T>(BufferFmt->mSampleRate);
  364. else
  365. return static_cast<T>(std::min<uint64_t>(length/BufferFmt->mSampleRate,
  366. std::numeric_limits<T>::max()));
  367. case AL_SAMPLE_LENGTH_SOFT:
  368. if constexpr(std::is_floating_point_v<T>)
  369. return static_cast<T>(length);
  370. else
  371. return static_cast<T>(std::min<uint64_t>(length, std::numeric_limits<T>::max()));
  372. case AL_BYTE_LENGTH_SOFT:
  373. const ALuint BlockSamples{BufferFmt->mBlockAlign};
  374. const ALuint BlockSize{BufferFmt->blockSizeFromFmt()};
  375. /* Round down to the block boundary. */
  376. length = length / BlockSamples * BlockSize;
  377. if constexpr(std::is_floating_point_v<T>)
  378. return static_cast<T>(length);
  379. else
  380. {
  381. if(length > uint64_t{std::numeric_limits<T>::max()})
  382. return RoundDown(std::numeric_limits<T>::max(), static_cast<T>(BlockSize));
  383. return static_cast<T>(length);
  384. }
  385. }
  386. return T{0};
  387. }
  388. struct VoicePos {
  389. int pos;
  390. uint frac;
  391. ALbufferQueueItem *bufferitem;
  392. };
  393. /**
  394. * GetSampleOffset
  395. *
  396. * Retrieves the voice position, fixed-point fraction, and bufferlist item
  397. * using the given offset type and offset. If the offset is out of range,
  398. * returns an empty optional.
  399. */
  400. std::optional<VoicePos> GetSampleOffset(std::deque<ALbufferQueueItem> &BufferList,
  401. ALenum OffsetType, double Offset)
  402. {
  403. /* Find the first valid Buffer in the Queue */
  404. const ALbuffer *BufferFmt{nullptr};
  405. for(auto &item : BufferList)
  406. {
  407. BufferFmt = item.mBuffer;
  408. if(BufferFmt) break;
  409. }
  410. if(!BufferFmt) UNLIKELY
  411. return std::nullopt;
  412. /* Get sample frame offset */
  413. int64_t offset{};
  414. uint frac{};
  415. double dbloff, dblfrac;
  416. switch(OffsetType)
  417. {
  418. case AL_SEC_OFFSET:
  419. dblfrac = std::modf(Offset*BufferFmt->mSampleRate, &dbloff);
  420. if(dblfrac < 0.0)
  421. {
  422. /* If there's a negative fraction, reduce the offset to "floor" it,
  423. * and convert the fraction to a percentage to the next value (e.g.
  424. * -2.75 -> -3 + 0.25).
  425. */
  426. dbloff -= 1.0;
  427. dblfrac += 1.0;
  428. }
  429. offset = static_cast<int64_t>(dbloff);
  430. frac = static_cast<uint>(std::min(dblfrac*MixerFracOne, MixerFracOne-1.0));
  431. break;
  432. case AL_SAMPLE_OFFSET:
  433. dblfrac = std::modf(Offset, &dbloff);
  434. if(dblfrac < 0.0)
  435. {
  436. dbloff -= 1.0;
  437. dblfrac += 1.0;
  438. }
  439. offset = static_cast<int64_t>(dbloff);
  440. frac = static_cast<uint>(std::min(dblfrac*MixerFracOne, MixerFracOne-1.0));
  441. break;
  442. case AL_BYTE_OFFSET:
  443. /* Determine the ByteOffset (and ensure it is block aligned) */
  444. Offset = std::floor(Offset / BufferFmt->blockSizeFromFmt());
  445. offset = static_cast<int64_t>(Offset) * BufferFmt->mBlockAlign;
  446. frac = 0;
  447. break;
  448. }
  449. /* Find the bufferlist item this offset belongs to. */
  450. if(offset < 0)
  451. {
  452. if(offset < std::numeric_limits<int>::min())
  453. return std::nullopt;
  454. return VoicePos{static_cast<int>(offset), frac, &BufferList.front()};
  455. }
  456. if(BufferFmt->mCallback)
  457. return std::nullopt;
  458. int64_t totalBufferLen{0};
  459. for(auto &item : BufferList)
  460. {
  461. if(totalBufferLen > offset)
  462. break;
  463. if(item.mSampleLen > offset-totalBufferLen)
  464. {
  465. /* Offset is in this buffer */
  466. return VoicePos{static_cast<int>(offset-totalBufferLen), frac, &item};
  467. }
  468. totalBufferLen += item.mSampleLen;
  469. }
  470. /* Offset is out of range of the queue */
  471. return std::nullopt;
  472. }
  473. void InitVoice(Voice *voice, ALsource *source, ALbufferQueueItem *BufferList, ALCcontext *context,
  474. ALCdevice *device)
  475. {
  476. voice->mLoopBuffer.store(source->Looping ? &source->mQueue.front() : nullptr,
  477. std::memory_order_relaxed);
  478. ALbuffer *buffer{BufferList->mBuffer};
  479. voice->mFrequency = buffer->mSampleRate;
  480. if(buffer->mChannels == FmtMono && source->mPanningEnabled)
  481. voice->mFmtChannels = FmtMonoDup;
  482. else if(buffer->mChannels == FmtStereo && source->mStereoMode == SourceStereo::Enhanced)
  483. voice->mFmtChannels = FmtSuperStereo;
  484. else
  485. voice->mFmtChannels = buffer->mChannels;
  486. voice->mFmtType = buffer->mType;
  487. voice->mFrameStep = buffer->channelsFromFmt();
  488. voice->mBytesPerBlock = buffer->blockSizeFromFmt();
  489. voice->mSamplesPerBlock = buffer->mBlockAlign;
  490. voice->mAmbiLayout = IsUHJ(voice->mFmtChannels) ? AmbiLayout::FuMa : buffer->mAmbiLayout;
  491. voice->mAmbiScaling = IsUHJ(voice->mFmtChannels) ? AmbiScaling::UHJ : buffer->mAmbiScaling;
  492. voice->mAmbiOrder = (voice->mFmtChannels == FmtSuperStereo) ? 1 : buffer->mAmbiOrder;
  493. if(buffer->mCallback) voice->mFlags.set(VoiceIsCallback);
  494. else if(source->SourceType == AL_STATIC) voice->mFlags.set(VoiceIsStatic);
  495. voice->mNumCallbackBlocks = 0;
  496. voice->mCallbackBlockBase = 0;
  497. voice->prepare(device);
  498. source->mPropsDirty = false;
  499. UpdateSourceProps(source, voice, context);
  500. voice->mSourceID.store(source->id, std::memory_order_release);
  501. }
  502. VoiceChange *GetVoiceChanger(ALCcontext *ctx)
  503. {
  504. VoiceChange *vchg{ctx->mVoiceChangeTail};
  505. if(vchg == ctx->mCurrentVoiceChange.load(std::memory_order_acquire)) UNLIKELY
  506. {
  507. ctx->allocVoiceChanges();
  508. vchg = ctx->mVoiceChangeTail;
  509. }
  510. ctx->mVoiceChangeTail = vchg->mNext.exchange(nullptr, std::memory_order_relaxed);
  511. return vchg;
  512. }
  513. void SendVoiceChanges(ALCcontext *ctx, VoiceChange *tail)
  514. {
  515. ALCdevice *device{ctx->mALDevice.get()};
  516. VoiceChange *oldhead{ctx->mCurrentVoiceChange.load(std::memory_order_acquire)};
  517. while(VoiceChange *next{oldhead->mNext.load(std::memory_order_relaxed)})
  518. oldhead = next;
  519. oldhead->mNext.store(tail, std::memory_order_release);
  520. const bool connected{device->Connected.load(std::memory_order_acquire)};
  521. std::ignore = device->waitForMix();
  522. if(!connected) UNLIKELY
  523. {
  524. if(ctx->mStopVoicesOnDisconnect.load(std::memory_order_acquire))
  525. {
  526. /* If the device is disconnected and voices are stopped, just
  527. * ignore all pending changes.
  528. */
  529. VoiceChange *cur{ctx->mCurrentVoiceChange.load(std::memory_order_acquire)};
  530. while(VoiceChange *next{cur->mNext.load(std::memory_order_acquire)})
  531. {
  532. cur = next;
  533. if(Voice *voice{cur->mVoice})
  534. voice->mSourceID.store(0, std::memory_order_relaxed);
  535. }
  536. ctx->mCurrentVoiceChange.store(cur, std::memory_order_release);
  537. }
  538. }
  539. }
  540. bool SetVoiceOffset(Voice *oldvoice, const VoicePos &vpos, ALsource *source, ALCcontext *context,
  541. ALCdevice *device)
  542. {
  543. /* First, get a free voice to start at the new offset. */
  544. auto voicelist = context->getVoicesSpan();
  545. Voice *newvoice{};
  546. ALuint vidx{0};
  547. for(Voice *voice : voicelist)
  548. {
  549. if(voice->mPlayState.load(std::memory_order_acquire) == Voice::Stopped
  550. && voice->mSourceID.load(std::memory_order_relaxed) == 0u
  551. && voice->mPendingChange.load(std::memory_order_relaxed) == false)
  552. {
  553. newvoice = voice;
  554. break;
  555. }
  556. ++vidx;
  557. }
  558. if(!newvoice) UNLIKELY
  559. {
  560. auto &allvoices = *context->mVoices.load(std::memory_order_relaxed);
  561. if(allvoices.size() == voicelist.size())
  562. context->allocVoices(1);
  563. context->mActiveVoiceCount.fetch_add(1, std::memory_order_release);
  564. voicelist = context->getVoicesSpan();
  565. vidx = 0;
  566. for(Voice *voice : voicelist)
  567. {
  568. if(voice->mPlayState.load(std::memory_order_acquire) == Voice::Stopped
  569. && voice->mSourceID.load(std::memory_order_relaxed) == 0u
  570. && voice->mPendingChange.load(std::memory_order_relaxed) == false)
  571. {
  572. newvoice = voice;
  573. break;
  574. }
  575. ++vidx;
  576. }
  577. ASSUME(newvoice != nullptr);
  578. }
  579. /* Initialize the new voice and set its starting offset.
  580. * TODO: It might be better to have the VoiceChange processing copy the old
  581. * voice's mixing parameters (and pending update) insead of initializing it
  582. * all here. This would just need to set the minimum properties to link the
  583. * voice to the source and its position-dependent properties (including the
  584. * fading flag).
  585. */
  586. newvoice->mPlayState.store(Voice::Pending, std::memory_order_relaxed);
  587. newvoice->mPosition.store(vpos.pos, std::memory_order_relaxed);
  588. newvoice->mPositionFrac.store(vpos.frac, std::memory_order_relaxed);
  589. newvoice->mCurrentBuffer.store(vpos.bufferitem, std::memory_order_relaxed);
  590. newvoice->mStartTime = oldvoice->mStartTime;
  591. newvoice->mFlags.reset();
  592. if(vpos.pos > 0 || (vpos.pos == 0 && vpos.frac > 0)
  593. || vpos.bufferitem != &source->mQueue.front())
  594. newvoice->mFlags.set(VoiceIsFading);
  595. InitVoice(newvoice, source, vpos.bufferitem, context, device);
  596. source->VoiceIdx = vidx;
  597. /* Set the old voice as having a pending change, and send it off with the
  598. * new one with a new offset voice change.
  599. */
  600. oldvoice->mPendingChange.store(true, std::memory_order_relaxed);
  601. VoiceChange *vchg{GetVoiceChanger(context)};
  602. vchg->mOldVoice = oldvoice;
  603. vchg->mVoice = newvoice;
  604. vchg->mSourceID = source->id;
  605. vchg->mState = VChangeState::Restart;
  606. SendVoiceChanges(context, vchg);
  607. /* If the old voice still has a sourceID, it's still active and the change-
  608. * over will work on the next update.
  609. */
  610. if(oldvoice->mSourceID.load(std::memory_order_acquire) != 0u) LIKELY
  611. return true;
  612. /* Otherwise, if the new voice's state is not pending, the change-over
  613. * already happened.
  614. */
  615. if(newvoice->mPlayState.load(std::memory_order_acquire) != Voice::Pending)
  616. return true;
  617. /* Otherwise, wait for any current mix to finish and check one last time. */
  618. std::ignore = device->waitForMix();
  619. if(newvoice->mPlayState.load(std::memory_order_acquire) != Voice::Pending)
  620. return true;
  621. /* The change-over failed because the old voice stopped before the new
  622. * voice could start at the new offset. Let go of the new voice and have
  623. * the caller store the source offset since it's stopped.
  624. */
  625. newvoice->mCurrentBuffer.store(nullptr, std::memory_order_relaxed);
  626. newvoice->mLoopBuffer.store(nullptr, std::memory_order_relaxed);
  627. newvoice->mSourceID.store(0u, std::memory_order_relaxed);
  628. newvoice->mPlayState.store(Voice::Stopped, std::memory_order_relaxed);
  629. return false;
  630. }
  631. /**
  632. * Returns if the last known state for the source was playing or paused. Does
  633. * not sync with the mixer voice.
  634. */
  635. inline bool IsPlayingOrPaused(ALsource *source)
  636. { return source->state == AL_PLAYING || source->state == AL_PAUSED; }
  637. /**
  638. * Returns an updated source state using the matching voice's status (or lack
  639. * thereof).
  640. */
  641. inline ALenum GetSourceState(ALsource *source, Voice *voice)
  642. {
  643. if(!voice && source->state == AL_PLAYING)
  644. source->state = AL_STOPPED;
  645. return source->state;
  646. }
  647. bool EnsureSources(ALCcontext *context, size_t needed)
  648. {
  649. size_t count{std::accumulate(context->mSourceList.cbegin(), context->mSourceList.cend(), 0_uz,
  650. [](size_t cur, const SourceSubList &sublist) noexcept -> size_t
  651. { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })};
  652. try {
  653. while(needed > count)
  654. {
  655. if(context->mSourceList.size() >= 1<<25) UNLIKELY
  656. return false;
  657. SourceSubList sublist{};
  658. sublist.FreeMask = ~0_u64;
  659. sublist.Sources = SubListAllocator{}.allocate(1);
  660. context->mSourceList.emplace_back(std::move(sublist));
  661. count += std::tuple_size_v<SubListAllocator::value_type>;
  662. }
  663. }
  664. catch(...) {
  665. return false;
  666. }
  667. return true;
  668. }
  669. ALsource *AllocSource(ALCcontext *context) noexcept
  670. {
  671. auto sublist = std::find_if(context->mSourceList.begin(), context->mSourceList.end(),
  672. [](const SourceSubList &entry) noexcept -> bool
  673. { return entry.FreeMask != 0; });
  674. auto lidx = static_cast<ALuint>(std::distance(context->mSourceList.begin(), sublist));
  675. auto slidx = static_cast<ALuint>(al::countr_zero(sublist->FreeMask));
  676. ASSUME(slidx < 64);
  677. ALsource *source{al::construct_at(al::to_address(sublist->Sources->begin() + slidx))};
  678. #ifdef ALSOFT_EAX
  679. source->eaxInitialize(context);
  680. #endif // ALSOFT_EAX
  681. /* Add 1 to avoid source ID 0. */
  682. source->id = ((lidx<<6) | slidx) + 1;
  683. context->mNumSources += 1;
  684. sublist->FreeMask &= ~(1_u64 << slidx);
  685. return source;
  686. }
  687. void FreeSource(ALCcontext *context, ALsource *source)
  688. {
  689. context->mSourceNames.erase(source->id);
  690. const ALuint id{source->id - 1};
  691. const size_t lidx{id >> 6};
  692. const ALuint slidx{id & 0x3f};
  693. if(Voice *voice{GetSourceVoice(source, context)})
  694. {
  695. VoiceChange *vchg{GetVoiceChanger(context)};
  696. voice->mPendingChange.store(true, std::memory_order_relaxed);
  697. vchg->mVoice = voice;
  698. vchg->mSourceID = source->id;
  699. vchg->mState = VChangeState::Stop;
  700. SendVoiceChanges(context, vchg);
  701. }
  702. std::destroy_at(source);
  703. context->mSourceList[lidx].FreeMask |= 1_u64 << slidx;
  704. context->mNumSources--;
  705. }
  706. inline ALsource *LookupSource(ALCcontext *context, ALuint id) noexcept
  707. {
  708. const size_t lidx{(id-1) >> 6};
  709. const ALuint slidx{(id-1) & 0x3f};
  710. if(lidx >= context->mSourceList.size()) UNLIKELY
  711. return nullptr;
  712. SourceSubList &sublist{context->mSourceList[lidx]};
  713. if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY
  714. return nullptr;
  715. return al::to_address(sublist.Sources->begin() + slidx);
  716. }
  717. auto LookupBuffer = [](ALCdevice *device, auto id) noexcept -> ALbuffer*
  718. {
  719. const auto lidx{(id-1) >> 6};
  720. const auto slidx{(id-1) & 0x3f};
  721. if(lidx >= device->BufferList.size()) UNLIKELY
  722. return nullptr;
  723. BufferSubList &sublist = device->BufferList[static_cast<size_t>(lidx)];
  724. if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY
  725. return nullptr;
  726. return al::to_address(sublist.Buffers->begin() + static_cast<size_t>(slidx));
  727. };
  728. auto LookupFilter = [](ALCdevice *device, auto id) noexcept -> ALfilter*
  729. {
  730. const auto lidx{(id-1) >> 6};
  731. const auto slidx{(id-1) & 0x3f};
  732. if(lidx >= device->FilterList.size()) UNLIKELY
  733. return nullptr;
  734. FilterSubList &sublist = device->FilterList[static_cast<size_t>(lidx)];
  735. if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY
  736. return nullptr;
  737. return al::to_address(sublist.Filters->begin() + static_cast<size_t>(slidx));
  738. };
  739. auto LookupEffectSlot = [](ALCcontext *context, auto id) noexcept -> ALeffectslot*
  740. {
  741. const auto lidx{(id-1) >> 6};
  742. const auto slidx{(id-1) & 0x3f};
  743. if(lidx >= context->mEffectSlotList.size()) UNLIKELY
  744. return nullptr;
  745. EffectSlotSubList &sublist{context->mEffectSlotList[static_cast<size_t>(lidx)]};
  746. if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY
  747. return nullptr;
  748. return al::to_address(sublist.EffectSlots->begin() + static_cast<size_t>(slidx));
  749. };
  750. auto StereoModeFromEnum = [](auto mode) noexcept -> std::optional<SourceStereo>
  751. {
  752. switch(mode)
  753. {
  754. case AL_NORMAL_SOFT: return SourceStereo::Normal;
  755. case AL_SUPER_STEREO_SOFT: return SourceStereo::Enhanced;
  756. }
  757. return std::nullopt;
  758. };
  759. ALenum EnumFromStereoMode(SourceStereo mode)
  760. {
  761. switch(mode)
  762. {
  763. case SourceStereo::Normal: return AL_NORMAL_SOFT;
  764. case SourceStereo::Enhanced: return AL_SUPER_STEREO_SOFT;
  765. }
  766. throw std::runtime_error{"Invalid SourceStereo: "+std::to_string(int(mode))};
  767. }
  768. auto SpatializeModeFromEnum = [](auto mode) noexcept -> std::optional<SpatializeMode>
  769. {
  770. switch(mode)
  771. {
  772. case AL_FALSE: return SpatializeMode::Off;
  773. case AL_TRUE: return SpatializeMode::On;
  774. case AL_AUTO_SOFT: return SpatializeMode::Auto;
  775. }
  776. return std::nullopt;
  777. };
  778. ALenum EnumFromSpatializeMode(SpatializeMode mode)
  779. {
  780. switch(mode)
  781. {
  782. case SpatializeMode::Off: return AL_FALSE;
  783. case SpatializeMode::On: return AL_TRUE;
  784. case SpatializeMode::Auto: return AL_AUTO_SOFT;
  785. }
  786. throw std::runtime_error{"Invalid SpatializeMode: "+std::to_string(int(mode))};
  787. }
  788. auto DirectModeFromEnum = [](auto mode) noexcept -> std::optional<DirectMode>
  789. {
  790. switch(mode)
  791. {
  792. case AL_FALSE: return DirectMode::Off;
  793. case AL_DROP_UNMATCHED_SOFT: return DirectMode::DropMismatch;
  794. case AL_REMIX_UNMATCHED_SOFT: return DirectMode::RemixMismatch;
  795. }
  796. return std::nullopt;
  797. };
  798. ALenum EnumFromDirectMode(DirectMode mode)
  799. {
  800. switch(mode)
  801. {
  802. case DirectMode::Off: return AL_FALSE;
  803. case DirectMode::DropMismatch: return AL_DROP_UNMATCHED_SOFT;
  804. case DirectMode::RemixMismatch: return AL_REMIX_UNMATCHED_SOFT;
  805. }
  806. throw std::runtime_error{"Invalid DirectMode: "+std::to_string(int(mode))};
  807. }
  808. auto DistanceModelFromALenum = [](auto model) noexcept -> std::optional<DistanceModel>
  809. {
  810. switch(model)
  811. {
  812. case AL_NONE: return DistanceModel::Disable;
  813. case AL_INVERSE_DISTANCE: return DistanceModel::Inverse;
  814. case AL_INVERSE_DISTANCE_CLAMPED: return DistanceModel::InverseClamped;
  815. case AL_LINEAR_DISTANCE: return DistanceModel::Linear;
  816. case AL_LINEAR_DISTANCE_CLAMPED: return DistanceModel::LinearClamped;
  817. case AL_EXPONENT_DISTANCE: return DistanceModel::Exponent;
  818. case AL_EXPONENT_DISTANCE_CLAMPED: return DistanceModel::ExponentClamped;
  819. }
  820. return std::nullopt;
  821. };
  822. ALenum ALenumFromDistanceModel(DistanceModel model)
  823. {
  824. switch(model)
  825. {
  826. case DistanceModel::Disable: return AL_NONE;
  827. case DistanceModel::Inverse: return AL_INVERSE_DISTANCE;
  828. case DistanceModel::InverseClamped: return AL_INVERSE_DISTANCE_CLAMPED;
  829. case DistanceModel::Linear: return AL_LINEAR_DISTANCE;
  830. case DistanceModel::LinearClamped: return AL_LINEAR_DISTANCE_CLAMPED;
  831. case DistanceModel::Exponent: return AL_EXPONENT_DISTANCE;
  832. case DistanceModel::ExponentClamped: return AL_EXPONENT_DISTANCE_CLAMPED;
  833. }
  834. throw std::runtime_error{"Unexpected distance model "+std::to_string(static_cast<int>(model))};
  835. }
  836. enum SourceProp : ALenum {
  837. srcPitch = AL_PITCH,
  838. srcGain = AL_GAIN,
  839. srcMinGain = AL_MIN_GAIN,
  840. srcMaxGain = AL_MAX_GAIN,
  841. srcMaxDistance = AL_MAX_DISTANCE,
  842. srcRolloffFactor = AL_ROLLOFF_FACTOR,
  843. srcDopplerFactor = AL_DOPPLER_FACTOR,
  844. srcConeOuterGain = AL_CONE_OUTER_GAIN,
  845. srcSecOffset = AL_SEC_OFFSET,
  846. srcSampleOffset = AL_SAMPLE_OFFSET,
  847. srcByteOffset = AL_BYTE_OFFSET,
  848. srcConeInnerAngle = AL_CONE_INNER_ANGLE,
  849. srcConeOuterAngle = AL_CONE_OUTER_ANGLE,
  850. srcRefDistance = AL_REFERENCE_DISTANCE,
  851. srcPosition = AL_POSITION,
  852. srcVelocity = AL_VELOCITY,
  853. srcDirection = AL_DIRECTION,
  854. srcSourceRelative = AL_SOURCE_RELATIVE,
  855. srcLooping = AL_LOOPING,
  856. srcBuffer = AL_BUFFER,
  857. srcSourceState = AL_SOURCE_STATE,
  858. srcBuffersQueued = AL_BUFFERS_QUEUED,
  859. srcBuffersProcessed = AL_BUFFERS_PROCESSED,
  860. srcSourceType = AL_SOURCE_TYPE,
  861. /* ALC_EXT_EFX */
  862. srcConeOuterGainHF = AL_CONE_OUTER_GAINHF,
  863. srcAirAbsorptionFactor = AL_AIR_ABSORPTION_FACTOR,
  864. srcRoomRolloffFactor = AL_ROOM_ROLLOFF_FACTOR,
  865. srcDirectFilterGainHFAuto = AL_DIRECT_FILTER_GAINHF_AUTO,
  866. srcAuxSendFilterGainAuto = AL_AUXILIARY_SEND_FILTER_GAIN_AUTO,
  867. srcAuxSendFilterGainHFAuto = AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO,
  868. srcDirectFilter = AL_DIRECT_FILTER,
  869. srcAuxSendFilter = AL_AUXILIARY_SEND_FILTER,
  870. /* AL_SOFT_direct_channels */
  871. srcDirectChannelsSOFT = AL_DIRECT_CHANNELS_SOFT,
  872. /* AL_EXT_source_distance_model */
  873. srcDistanceModel = AL_DISTANCE_MODEL,
  874. /* AL_SOFT_source_latency */
  875. srcSampleOffsetLatencySOFT = AL_SAMPLE_OFFSET_LATENCY_SOFT,
  876. srcSecOffsetLatencySOFT = AL_SEC_OFFSET_LATENCY_SOFT,
  877. /* AL_EXT_STEREO_ANGLES */
  878. srcAngles = AL_STEREO_ANGLES,
  879. /* AL_EXT_SOURCE_RADIUS */
  880. srcRadius = AL_SOURCE_RADIUS,
  881. /* AL_EXT_BFORMAT */
  882. srcOrientation = AL_ORIENTATION,
  883. /* AL_SOFT_source_length */
  884. srcByteLength = AL_BYTE_LENGTH_SOFT,
  885. srcSampleLength = AL_SAMPLE_LENGTH_SOFT,
  886. srcSecLength = AL_SEC_LENGTH_SOFT,
  887. /* AL_SOFT_source_resampler */
  888. srcResampler = AL_SOURCE_RESAMPLER_SOFT,
  889. /* AL_SOFT_source_spatialize */
  890. srcSpatialize = AL_SOURCE_SPATIALIZE_SOFT,
  891. /* ALC_SOFT_device_clock */
  892. srcSampleOffsetClockSOFT = AL_SAMPLE_OFFSET_CLOCK_SOFT,
  893. srcSecOffsetClockSOFT = AL_SEC_OFFSET_CLOCK_SOFT,
  894. /* AL_SOFT_UHJ */
  895. srcStereoMode = AL_STEREO_MODE_SOFT,
  896. srcSuperStereoWidth = AL_SUPER_STEREO_WIDTH_SOFT,
  897. /* AL_SOFT_buffer_sub_data */
  898. srcByteRWOffsetsSOFT = AL_BYTE_RW_OFFSETS_SOFT,
  899. srcSampleRWOffsetsSOFT = AL_SAMPLE_RW_OFFSETS_SOFT,
  900. /* AL_SOFT_source_panning */
  901. srcPanningEnabledSOFT = AL_PANNING_ENABLED_SOFT,
  902. srcPanSOFT = AL_PAN_SOFT,
  903. };
  904. constexpr ALuint IntValsByProp(ALenum prop)
  905. {
  906. switch(static_cast<SourceProp>(prop))
  907. {
  908. case AL_SOURCE_STATE:
  909. case AL_SOURCE_TYPE:
  910. case AL_BUFFERS_QUEUED:
  911. case AL_BUFFERS_PROCESSED:
  912. case AL_BYTE_LENGTH_SOFT:
  913. case AL_SAMPLE_LENGTH_SOFT:
  914. case AL_SOURCE_RELATIVE:
  915. case AL_LOOPING:
  916. case AL_BUFFER:
  917. case AL_SAMPLE_OFFSET:
  918. case AL_BYTE_OFFSET:
  919. case AL_DIRECT_FILTER:
  920. case AL_DIRECT_FILTER_GAINHF_AUTO:
  921. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  922. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  923. case AL_DIRECT_CHANNELS_SOFT:
  924. case AL_DISTANCE_MODEL:
  925. case AL_SOURCE_RESAMPLER_SOFT:
  926. case AL_SOURCE_SPATIALIZE_SOFT:
  927. case AL_STEREO_MODE_SOFT:
  928. case AL_PANNING_ENABLED_SOFT:
  929. case AL_PAN_SOFT:
  930. return 1;
  931. case AL_SOURCE_RADIUS: /*AL_BYTE_RW_OFFSETS_SOFT:*/
  932. if(sBufferSubDataCompat)
  933. return 2;
  934. /*fall-through*/
  935. case AL_CONE_INNER_ANGLE:
  936. case AL_CONE_OUTER_ANGLE:
  937. case AL_PITCH:
  938. case AL_GAIN:
  939. case AL_MIN_GAIN:
  940. case AL_MAX_GAIN:
  941. case AL_REFERENCE_DISTANCE:
  942. case AL_ROLLOFF_FACTOR:
  943. case AL_CONE_OUTER_GAIN:
  944. case AL_MAX_DISTANCE:
  945. case AL_SEC_OFFSET:
  946. case AL_DOPPLER_FACTOR:
  947. case AL_CONE_OUTER_GAINHF:
  948. case AL_AIR_ABSORPTION_FACTOR:
  949. case AL_ROOM_ROLLOFF_FACTOR:
  950. case AL_SEC_LENGTH_SOFT:
  951. case AL_SUPER_STEREO_WIDTH_SOFT:
  952. return 1; /* 1x float */
  953. case AL_SAMPLE_RW_OFFSETS_SOFT:
  954. if(sBufferSubDataCompat)
  955. return 2;
  956. break;
  957. case AL_AUXILIARY_SEND_FILTER:
  958. return 3;
  959. case AL_POSITION:
  960. case AL_VELOCITY:
  961. case AL_DIRECTION:
  962. return 3; /* 3x float */
  963. case AL_ORIENTATION:
  964. return 6; /* 6x float */
  965. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  966. case AL_SAMPLE_OFFSET_CLOCK_SOFT:
  967. case AL_STEREO_ANGLES:
  968. break; /* i64 only */
  969. case AL_SEC_OFFSET_LATENCY_SOFT:
  970. case AL_SEC_OFFSET_CLOCK_SOFT:
  971. break; /* double only */
  972. }
  973. return 0;
  974. }
  975. constexpr ALuint Int64ValsByProp(ALenum prop)
  976. {
  977. switch(static_cast<SourceProp>(prop))
  978. {
  979. case AL_SOURCE_STATE:
  980. case AL_SOURCE_TYPE:
  981. case AL_BUFFERS_QUEUED:
  982. case AL_BUFFERS_PROCESSED:
  983. case AL_BYTE_LENGTH_SOFT:
  984. case AL_SAMPLE_LENGTH_SOFT:
  985. case AL_SOURCE_RELATIVE:
  986. case AL_LOOPING:
  987. case AL_BUFFER:
  988. case AL_SAMPLE_OFFSET:
  989. case AL_BYTE_OFFSET:
  990. case AL_DIRECT_FILTER:
  991. case AL_DIRECT_FILTER_GAINHF_AUTO:
  992. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  993. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  994. case AL_DIRECT_CHANNELS_SOFT:
  995. case AL_DISTANCE_MODEL:
  996. case AL_SOURCE_RESAMPLER_SOFT:
  997. case AL_SOURCE_SPATIALIZE_SOFT:
  998. case AL_STEREO_MODE_SOFT:
  999. case AL_PANNING_ENABLED_SOFT:
  1000. case AL_PAN_SOFT:
  1001. return 1;
  1002. case AL_SOURCE_RADIUS: /*AL_BYTE_RW_OFFSETS_SOFT:*/
  1003. if(sBufferSubDataCompat)
  1004. return 2;
  1005. /*fall-through*/
  1006. case AL_CONE_INNER_ANGLE:
  1007. case AL_CONE_OUTER_ANGLE:
  1008. case AL_PITCH:
  1009. case AL_GAIN:
  1010. case AL_MIN_GAIN:
  1011. case AL_MAX_GAIN:
  1012. case AL_REFERENCE_DISTANCE:
  1013. case AL_ROLLOFF_FACTOR:
  1014. case AL_CONE_OUTER_GAIN:
  1015. case AL_MAX_DISTANCE:
  1016. case AL_SEC_OFFSET:
  1017. case AL_DOPPLER_FACTOR:
  1018. case AL_CONE_OUTER_GAINHF:
  1019. case AL_AIR_ABSORPTION_FACTOR:
  1020. case AL_ROOM_ROLLOFF_FACTOR:
  1021. case AL_SEC_LENGTH_SOFT:
  1022. case AL_SUPER_STEREO_WIDTH_SOFT:
  1023. return 1; /* 1x float */
  1024. case AL_SAMPLE_RW_OFFSETS_SOFT:
  1025. if(sBufferSubDataCompat)
  1026. return 2;
  1027. break;
  1028. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  1029. case AL_SAMPLE_OFFSET_CLOCK_SOFT:
  1030. case AL_STEREO_ANGLES:
  1031. return 2;
  1032. case AL_AUXILIARY_SEND_FILTER:
  1033. return 3;
  1034. case AL_POSITION:
  1035. case AL_VELOCITY:
  1036. case AL_DIRECTION:
  1037. return 3; /* 3x float */
  1038. case AL_ORIENTATION:
  1039. return 6; /* 6x float */
  1040. case AL_SEC_OFFSET_LATENCY_SOFT:
  1041. case AL_SEC_OFFSET_CLOCK_SOFT:
  1042. break; /* double only */
  1043. }
  1044. return 0;
  1045. }
  1046. constexpr ALuint FloatValsByProp(ALenum prop)
  1047. {
  1048. switch(static_cast<SourceProp>(prop))
  1049. {
  1050. case AL_PITCH:
  1051. case AL_GAIN:
  1052. case AL_MIN_GAIN:
  1053. case AL_MAX_GAIN:
  1054. case AL_MAX_DISTANCE:
  1055. case AL_ROLLOFF_FACTOR:
  1056. case AL_DOPPLER_FACTOR:
  1057. case AL_CONE_OUTER_GAIN:
  1058. case AL_SEC_OFFSET:
  1059. case AL_SAMPLE_OFFSET:
  1060. case AL_BYTE_OFFSET:
  1061. case AL_CONE_INNER_ANGLE:
  1062. case AL_CONE_OUTER_ANGLE:
  1063. case AL_REFERENCE_DISTANCE:
  1064. case AL_CONE_OUTER_GAINHF:
  1065. case AL_AIR_ABSORPTION_FACTOR:
  1066. case AL_ROOM_ROLLOFF_FACTOR:
  1067. case AL_DIRECT_FILTER_GAINHF_AUTO:
  1068. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  1069. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  1070. case AL_DIRECT_CHANNELS_SOFT:
  1071. case AL_DISTANCE_MODEL:
  1072. case AL_SOURCE_RELATIVE:
  1073. case AL_LOOPING:
  1074. case AL_SOURCE_STATE:
  1075. case AL_BUFFERS_QUEUED:
  1076. case AL_BUFFERS_PROCESSED:
  1077. case AL_SOURCE_TYPE:
  1078. case AL_SOURCE_RESAMPLER_SOFT:
  1079. case AL_SOURCE_SPATIALIZE_SOFT:
  1080. case AL_BYTE_LENGTH_SOFT:
  1081. case AL_SAMPLE_LENGTH_SOFT:
  1082. case AL_SEC_LENGTH_SOFT:
  1083. case AL_STEREO_MODE_SOFT:
  1084. case AL_SUPER_STEREO_WIDTH_SOFT:
  1085. case AL_PANNING_ENABLED_SOFT:
  1086. case AL_PAN_SOFT:
  1087. return 1;
  1088. case AL_SOURCE_RADIUS: /*AL_BYTE_RW_OFFSETS_SOFT:*/
  1089. if(!sBufferSubDataCompat)
  1090. return 1;
  1091. /*fall-through*/
  1092. case AL_SAMPLE_RW_OFFSETS_SOFT:
  1093. break;
  1094. case AL_STEREO_ANGLES:
  1095. return 2;
  1096. case AL_POSITION:
  1097. case AL_VELOCITY:
  1098. case AL_DIRECTION:
  1099. return 3;
  1100. case AL_ORIENTATION:
  1101. return 6;
  1102. case AL_SEC_OFFSET_LATENCY_SOFT:
  1103. case AL_SEC_OFFSET_CLOCK_SOFT:
  1104. break; /* Double only */
  1105. case AL_BUFFER:
  1106. case AL_DIRECT_FILTER:
  1107. case AL_AUXILIARY_SEND_FILTER:
  1108. break; /* i/i64 only */
  1109. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  1110. case AL_SAMPLE_OFFSET_CLOCK_SOFT:
  1111. break; /* i64 only */
  1112. }
  1113. return 0;
  1114. }
  1115. constexpr ALuint DoubleValsByProp(ALenum prop)
  1116. {
  1117. switch(static_cast<SourceProp>(prop))
  1118. {
  1119. case AL_PITCH:
  1120. case AL_GAIN:
  1121. case AL_MIN_GAIN:
  1122. case AL_MAX_GAIN:
  1123. case AL_MAX_DISTANCE:
  1124. case AL_ROLLOFF_FACTOR:
  1125. case AL_DOPPLER_FACTOR:
  1126. case AL_CONE_OUTER_GAIN:
  1127. case AL_SEC_OFFSET:
  1128. case AL_SAMPLE_OFFSET:
  1129. case AL_BYTE_OFFSET:
  1130. case AL_CONE_INNER_ANGLE:
  1131. case AL_CONE_OUTER_ANGLE:
  1132. case AL_REFERENCE_DISTANCE:
  1133. case AL_CONE_OUTER_GAINHF:
  1134. case AL_AIR_ABSORPTION_FACTOR:
  1135. case AL_ROOM_ROLLOFF_FACTOR:
  1136. case AL_DIRECT_FILTER_GAINHF_AUTO:
  1137. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  1138. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  1139. case AL_DIRECT_CHANNELS_SOFT:
  1140. case AL_DISTANCE_MODEL:
  1141. case AL_SOURCE_RELATIVE:
  1142. case AL_LOOPING:
  1143. case AL_SOURCE_STATE:
  1144. case AL_BUFFERS_QUEUED:
  1145. case AL_BUFFERS_PROCESSED:
  1146. case AL_SOURCE_TYPE:
  1147. case AL_SOURCE_RESAMPLER_SOFT:
  1148. case AL_SOURCE_SPATIALIZE_SOFT:
  1149. case AL_BYTE_LENGTH_SOFT:
  1150. case AL_SAMPLE_LENGTH_SOFT:
  1151. case AL_SEC_LENGTH_SOFT:
  1152. case AL_STEREO_MODE_SOFT:
  1153. case AL_SUPER_STEREO_WIDTH_SOFT:
  1154. case AL_PANNING_ENABLED_SOFT:
  1155. case AL_PAN_SOFT:
  1156. return 1;
  1157. case AL_SOURCE_RADIUS: /*AL_BYTE_RW_OFFSETS_SOFT:*/
  1158. if(!sBufferSubDataCompat)
  1159. return 1;
  1160. /*fall-through*/
  1161. case AL_SAMPLE_RW_OFFSETS_SOFT:
  1162. break;
  1163. case AL_SEC_OFFSET_LATENCY_SOFT:
  1164. case AL_SEC_OFFSET_CLOCK_SOFT:
  1165. case AL_STEREO_ANGLES:
  1166. return 2;
  1167. case AL_POSITION:
  1168. case AL_VELOCITY:
  1169. case AL_DIRECTION:
  1170. return 3;
  1171. case AL_ORIENTATION:
  1172. return 6;
  1173. case AL_BUFFER:
  1174. case AL_DIRECT_FILTER:
  1175. case AL_AUXILIARY_SEND_FILTER:
  1176. break; /* i/i64 only */
  1177. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  1178. case AL_SAMPLE_OFFSET_CLOCK_SOFT:
  1179. break; /* i64 only */
  1180. }
  1181. return 0;
  1182. }
  1183. void UpdateSourceProps(ALsource *source, ALCcontext *context)
  1184. {
  1185. if(!context->mDeferUpdates)
  1186. {
  1187. if(Voice *voice{GetSourceVoice(source, context)})
  1188. {
  1189. UpdateSourceProps(source, voice, context);
  1190. return;
  1191. }
  1192. }
  1193. source->mPropsDirty = true;
  1194. }
  1195. #ifdef ALSOFT_EAX
  1196. void CommitAndUpdateSourceProps(ALsource *source, ALCcontext *context)
  1197. {
  1198. if(!context->mDeferUpdates)
  1199. {
  1200. if(context->hasEax())
  1201. source->eaxCommit();
  1202. if(Voice *voice{GetSourceVoice(source, context)})
  1203. {
  1204. UpdateSourceProps(source, voice, context);
  1205. return;
  1206. }
  1207. }
  1208. source->mPropsDirty = true;
  1209. }
  1210. #else
  1211. inline void CommitAndUpdateSourceProps(ALsource *source, ALCcontext *context)
  1212. { UpdateSourceProps(source, context); }
  1213. #endif
  1214. template<typename T>
  1215. struct PropType { };
  1216. template<>
  1217. struct PropType<ALint> { static const char *Name() { return "integer"; } };
  1218. template<>
  1219. struct PropType<ALint64SOFT> { static const char *Name() { return "int64"; } };
  1220. template<>
  1221. struct PropType<ALfloat> { static const char *Name() { return "float"; } };
  1222. template<>
  1223. struct PropType<ALdouble> { static const char *Name() { return "double"; } };
  1224. struct HexPrinter {
  1225. std::array<char,32> mStr{};
  1226. template<typename T>
  1227. HexPrinter(T value)
  1228. {
  1229. using ST = std::make_signed_t<std::remove_cv_t<T>>;
  1230. if constexpr(std::is_same_v<ST,int>)
  1231. std::snprintf(mStr.data(), mStr.size(), "0x%x", value);
  1232. else if constexpr(std::is_same_v<ST,long>)
  1233. std::snprintf(mStr.data(), mStr.size(), "0x%lx", value);
  1234. else if constexpr(std::is_same_v<ST,long long>)
  1235. std::snprintf(mStr.data(), mStr.size(), "0x%llx", value);
  1236. }
  1237. [[nodiscard]] auto c_str() const noexcept -> const char* { return mStr.data(); }
  1238. };
  1239. /**
  1240. * Returns a pair of lambdas to check the following setter.
  1241. *
  1242. * The first lambda checks the size of the span is valid for the required size,
  1243. * setting the proper context error and throwing a check_size_exception if it
  1244. * fails.
  1245. *
  1246. * The second lambda tests the validity of the value check, setting the proper
  1247. * context error and throwing a check_value_exception if it failed.
  1248. */
  1249. template<typename T, size_t N>
  1250. auto GetCheckers(const SourceProp prop, const al::span<T,N> values)
  1251. {
  1252. return std::make_pair(
  1253. [=](size_t expect) -> void
  1254. {
  1255. if(values.size() == expect) return;
  1256. throw al::context_error{AL_INVALID_ENUM,
  1257. "Property 0x%04x expects %zu value(s), got %zu", prop, expect, values.size()};
  1258. },
  1259. [](bool passed) -> void
  1260. {
  1261. if(passed) return;
  1262. throw al::context_error{AL_INVALID_VALUE, "Value out of range"};
  1263. }
  1264. );
  1265. }
  1266. template<typename T>
  1267. NOINLINE void SetProperty(ALsource *const Source, ALCcontext *const Context, const SourceProp prop,
  1268. const al::span<const T> values)
  1269. {
  1270. auto [CheckSize, CheckValue] = GetCheckers(prop, values);
  1271. ALCdevice *device{Context->mALDevice.get()};
  1272. switch(prop)
  1273. {
  1274. case AL_SOURCE_STATE:
  1275. case AL_SOURCE_TYPE:
  1276. case AL_BUFFERS_QUEUED:
  1277. case AL_BUFFERS_PROCESSED:
  1278. if constexpr(std::is_integral_v<T>)
  1279. {
  1280. /* Query only */
  1281. throw al::context_error{AL_INVALID_OPERATION,
  1282. "Setting read-only source property 0x%04x", prop};
  1283. }
  1284. break;
  1285. case AL_BYTE_LENGTH_SOFT:
  1286. case AL_SAMPLE_LENGTH_SOFT:
  1287. case AL_SEC_LENGTH_SOFT:
  1288. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  1289. case AL_SEC_OFFSET_LATENCY_SOFT:
  1290. case AL_SAMPLE_OFFSET_CLOCK_SOFT:
  1291. case AL_SEC_OFFSET_CLOCK_SOFT:
  1292. /* Query only */
  1293. throw al::context_error{AL_INVALID_OPERATION, "Setting read-only source property 0x%04x",
  1294. prop};
  1295. case AL_PITCH:
  1296. CheckSize(1);
  1297. CheckValue(values[0] >= T{0});
  1298. Source->Pitch = static_cast<float>(values[0]);
  1299. return UpdateSourceProps(Source, Context);
  1300. case AL_CONE_INNER_ANGLE:
  1301. CheckSize(1);
  1302. CheckValue(values[0] >= T{0} && values[0] <= T{360});
  1303. Source->InnerAngle = static_cast<float>(values[0]);
  1304. return CommitAndUpdateSourceProps(Source, Context);
  1305. case AL_CONE_OUTER_ANGLE:
  1306. CheckSize(1);
  1307. CheckValue(values[0] >= T{0} && values[0] <= T{360});
  1308. Source->OuterAngle = static_cast<float>(values[0]);
  1309. return CommitAndUpdateSourceProps(Source, Context);
  1310. case AL_GAIN:
  1311. CheckSize(1);
  1312. CheckValue(values[0] >= T{0});
  1313. Source->Gain = static_cast<float>(values[0]);
  1314. return UpdateSourceProps(Source, Context);
  1315. case AL_MAX_DISTANCE:
  1316. CheckSize(1);
  1317. CheckValue(values[0] >= T{0});
  1318. Source->MaxDistance = static_cast<float>(values[0]);
  1319. return CommitAndUpdateSourceProps(Source, Context);
  1320. case AL_ROLLOFF_FACTOR:
  1321. CheckSize(1);
  1322. CheckValue(values[0] >= T{0});
  1323. Source->RolloffFactor = static_cast<float>(values[0]);
  1324. return CommitAndUpdateSourceProps(Source, Context);
  1325. case AL_REFERENCE_DISTANCE:
  1326. CheckSize(1);
  1327. CheckValue(values[0] >= T{0});
  1328. Source->RefDistance = static_cast<float>(values[0]);
  1329. return CommitAndUpdateSourceProps(Source, Context);
  1330. case AL_MIN_GAIN:
  1331. CheckSize(1);
  1332. CheckValue(values[0] >= T{0});
  1333. Source->MinGain = static_cast<float>(values[0]);
  1334. return UpdateSourceProps(Source, Context);
  1335. case AL_MAX_GAIN:
  1336. CheckSize(1);
  1337. CheckValue(values[0] >= T{0});
  1338. Source->MaxGain = static_cast<float>(values[0]);
  1339. return UpdateSourceProps(Source, Context);
  1340. case AL_CONE_OUTER_GAIN:
  1341. CheckSize(1);
  1342. CheckValue(values[0] >= T{0} && values[0] <= T{1});
  1343. Source->OuterGain = static_cast<float>(values[0]);
  1344. return UpdateSourceProps(Source, Context);
  1345. case AL_CONE_OUTER_GAINHF:
  1346. CheckSize(1);
  1347. CheckValue(values[0] >= T{0} && values[0] <= T{1});
  1348. Source->OuterGainHF = static_cast<float>(values[0]);
  1349. return UpdateSourceProps(Source, Context);
  1350. case AL_AIR_ABSORPTION_FACTOR:
  1351. CheckSize(1);
  1352. CheckValue(values[0] >= T{0} && values[0] <= T{10});
  1353. Source->AirAbsorptionFactor = static_cast<float>(values[0]);
  1354. return UpdateSourceProps(Source, Context);
  1355. case AL_ROOM_ROLLOFF_FACTOR:
  1356. CheckSize(1);
  1357. CheckValue(values[0] >= T{0} && values[0] <= T{1});
  1358. Source->RoomRolloffFactor = static_cast<float>(values[0]);
  1359. return UpdateSourceProps(Source, Context);
  1360. case AL_DOPPLER_FACTOR:
  1361. CheckSize(1);
  1362. CheckValue(values[0] >= T{0} && values[0] <= T{1});
  1363. Source->DopplerFactor = static_cast<float>(values[0]);
  1364. return UpdateSourceProps(Source, Context);
  1365. case AL_SOURCE_RELATIVE:
  1366. if constexpr(std::is_integral_v<T>)
  1367. {
  1368. CheckSize(1);
  1369. CheckValue(values[0] == AL_FALSE || values[0] == AL_TRUE);
  1370. Source->HeadRelative = values[0] != AL_FALSE;
  1371. return CommitAndUpdateSourceProps(Source, Context);
  1372. }
  1373. break;
  1374. case AL_LOOPING:
  1375. if constexpr(std::is_integral_v<T>)
  1376. {
  1377. CheckSize(1);
  1378. CheckValue(values[0] == AL_FALSE || values[0] == AL_TRUE);
  1379. Source->Looping = values[0] != AL_FALSE;
  1380. if(Voice *voice{GetSourceVoice(Source, Context)})
  1381. {
  1382. if(Source->Looping)
  1383. voice->mLoopBuffer.store(&Source->mQueue.front(), std::memory_order_release);
  1384. else
  1385. voice->mLoopBuffer.store(nullptr, std::memory_order_release);
  1386. /* If the source is playing, wait for the current mix to finish
  1387. * to ensure it isn't currently looping back or reaching the
  1388. * end.
  1389. */
  1390. std::ignore = device->waitForMix();
  1391. }
  1392. return;
  1393. }
  1394. break;
  1395. case AL_BUFFER:
  1396. if constexpr(std::is_integral_v<T>)
  1397. {
  1398. CheckSize(1);
  1399. if(const ALenum state{GetSourceState(Source, GetSourceVoice(Source, Context))};
  1400. state == AL_PLAYING || state == AL_PAUSED)
  1401. throw al::context_error{AL_INVALID_OPERATION,
  1402. "Setting buffer on playing or paused source %u", Source->id};
  1403. std::deque<ALbufferQueueItem> oldlist;
  1404. if(values[0])
  1405. {
  1406. using UT = std::make_unsigned_t<T>;
  1407. std::lock_guard<std::mutex> buflock{device->BufferLock};
  1408. ALbuffer *buffer{LookupBuffer(device, static_cast<UT>(values[0]))};
  1409. if(!buffer)
  1410. throw al::context_error{AL_INVALID_VALUE, "Invalid buffer ID %s",
  1411. std::to_string(values[0]).c_str()};
  1412. if(buffer->MappedAccess && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT))
  1413. throw al::context_error{AL_INVALID_OPERATION,
  1414. "Setting non-persistently mapped buffer %u", buffer->id};
  1415. if(buffer->mCallback && buffer->ref.load(std::memory_order_relaxed) != 0)
  1416. throw al::context_error{AL_INVALID_OPERATION,
  1417. "Setting already-set callback buffer %u", buffer->id};
  1418. /* Add the selected buffer to a one-item queue */
  1419. std::deque<ALbufferQueueItem> newlist;
  1420. newlist.emplace_back();
  1421. newlist.back().mCallback = buffer->mCallback;
  1422. newlist.back().mUserData = buffer->mUserData;
  1423. newlist.back().mBlockAlign = buffer->mBlockAlign;
  1424. newlist.back().mSampleLen = buffer->mSampleLen;
  1425. newlist.back().mLoopStart = buffer->mLoopStart;
  1426. newlist.back().mLoopEnd = buffer->mLoopEnd;
  1427. newlist.back().mSamples = buffer->mData;
  1428. newlist.back().mBuffer = buffer;
  1429. IncrementRef(buffer->ref);
  1430. /* Source is now Static */
  1431. Source->SourceType = AL_STATIC;
  1432. Source->mQueue.swap(oldlist);
  1433. Source->mQueue.swap(newlist);
  1434. }
  1435. else
  1436. {
  1437. /* Source is now Undetermined */
  1438. Source->SourceType = AL_UNDETERMINED;
  1439. Source->mQueue.swap(oldlist);
  1440. }
  1441. /* Delete all elements in the previous queue */
  1442. for(auto &item : oldlist)
  1443. {
  1444. if(ALbuffer *buffer{item.mBuffer})
  1445. DecrementRef(buffer->ref);
  1446. }
  1447. return;
  1448. }
  1449. break;
  1450. case AL_SEC_OFFSET:
  1451. case AL_SAMPLE_OFFSET:
  1452. case AL_BYTE_OFFSET:
  1453. CheckSize(1);
  1454. if constexpr(std::is_floating_point_v<T>)
  1455. CheckValue(std::isfinite(values[0]));
  1456. if(Voice *voice{GetSourceVoice(Source, Context)})
  1457. {
  1458. auto vpos = GetSampleOffset(Source->mQueue, prop, static_cast<double>(values[0]));
  1459. if(!vpos) throw al::context_error{AL_INVALID_VALUE, "Invalid offset"};
  1460. if(SetVoiceOffset(voice, *vpos, Source, Context, Context->mALDevice.get()))
  1461. return;
  1462. }
  1463. Source->OffsetType = prop;
  1464. Source->Offset = static_cast<double>(values[0]);
  1465. return;
  1466. case AL_SAMPLE_RW_OFFSETS_SOFT:
  1467. if(sBufferSubDataCompat)
  1468. {
  1469. if constexpr(std::is_integral_v<T>)
  1470. {
  1471. /* Query only */
  1472. throw al::context_error{AL_INVALID_OPERATION,
  1473. "Setting read-only source property 0x%04x", prop};
  1474. }
  1475. }
  1476. break;
  1477. case AL_SOURCE_RADIUS: /*AL_BYTE_RW_OFFSETS_SOFT:*/
  1478. if(sBufferSubDataCompat)
  1479. {
  1480. if constexpr(std::is_integral_v<T>)
  1481. {
  1482. /* Query only */
  1483. throw al::context_error{AL_INVALID_OPERATION,
  1484. "Setting read-only source property 0x%04x", prop};
  1485. }
  1486. break;
  1487. }
  1488. CheckSize(1);
  1489. if constexpr(std::is_floating_point_v<T>)
  1490. CheckValue(values[0] >= T{0} && std::isfinite(static_cast<float>(values[0])));
  1491. else
  1492. CheckValue(values[0] >= T{0});
  1493. Source->Radius = static_cast<float>(values[0]);
  1494. return UpdateSourceProps(Source, Context);
  1495. case AL_SUPER_STEREO_WIDTH_SOFT:
  1496. CheckSize(1);
  1497. CheckValue(values[0] >= T{0} && values[0] <= T{1});
  1498. Source->EnhWidth = static_cast<float>(values[0]);
  1499. return UpdateSourceProps(Source, Context);
  1500. case AL_PANNING_ENABLED_SOFT:
  1501. CheckSize(1);
  1502. if(const ALenum state{GetSourceState(Source, GetSourceVoice(Source, Context))};
  1503. state == AL_PLAYING || state == AL_PAUSED)
  1504. throw al::context_error{AL_INVALID_OPERATION,
  1505. "Modifying panning enabled on playing or paused source %u", Source->id};
  1506. CheckValue(values[0] == AL_FALSE || values[0] == AL_TRUE);
  1507. Source->mPanningEnabled = values[0] != AL_FALSE;
  1508. return UpdateSourceProps(Source, Context);
  1509. case AL_PAN_SOFT:
  1510. CheckSize(1);
  1511. CheckValue(values[0] >= T{-1} && values[0] <= T{1});
  1512. Source->mPan = static_cast<float>(values[0]);
  1513. return UpdateSourceProps(Source, Context);
  1514. case AL_STEREO_ANGLES:
  1515. CheckSize(2);
  1516. if constexpr(std::is_floating_point_v<T>)
  1517. CheckValue(std::isfinite(static_cast<float>(values[0]))
  1518. && std::isfinite(static_cast<float>(values[1])));
  1519. Source->StereoPan[0] = static_cast<float>(values[0]);
  1520. Source->StereoPan[1] = static_cast<float>(values[1]);
  1521. return UpdateSourceProps(Source, Context);
  1522. case AL_POSITION:
  1523. CheckSize(3);
  1524. if constexpr(std::is_floating_point_v<T>)
  1525. CheckValue(std::isfinite(static_cast<float>(values[0]))
  1526. && std::isfinite(static_cast<float>(values[1]))
  1527. && std::isfinite(static_cast<float>(values[2])));
  1528. Source->Position[0] = static_cast<float>(values[0]);
  1529. Source->Position[1] = static_cast<float>(values[1]);
  1530. Source->Position[2] = static_cast<float>(values[2]);
  1531. return CommitAndUpdateSourceProps(Source, Context);
  1532. case AL_VELOCITY:
  1533. CheckSize(3);
  1534. if constexpr(std::is_floating_point_v<T>)
  1535. CheckValue(std::isfinite(static_cast<float>(values[0]))
  1536. && std::isfinite(static_cast<float>(values[1]))
  1537. && std::isfinite(static_cast<float>(values[2])));
  1538. Source->Velocity[0] = static_cast<float>(values[0]);
  1539. Source->Velocity[1] = static_cast<float>(values[1]);
  1540. Source->Velocity[2] = static_cast<float>(values[2]);
  1541. return CommitAndUpdateSourceProps(Source, Context);
  1542. case AL_DIRECTION:
  1543. CheckSize(3);
  1544. if constexpr(std::is_floating_point_v<T>)
  1545. CheckValue(std::isfinite(static_cast<float>(values[0]))
  1546. && std::isfinite(static_cast<float>(values[1]))
  1547. && std::isfinite(static_cast<float>(values[2])));
  1548. Source->Direction[0] = static_cast<float>(values[0]);
  1549. Source->Direction[1] = static_cast<float>(values[1]);
  1550. Source->Direction[2] = static_cast<float>(values[2]);
  1551. return CommitAndUpdateSourceProps(Source, Context);
  1552. case AL_ORIENTATION:
  1553. CheckSize(6);
  1554. if constexpr(std::is_floating_point_v<T>)
  1555. CheckValue(std::isfinite(static_cast<float>(values[0]))
  1556. && std::isfinite(static_cast<float>(values[1]))
  1557. && std::isfinite(static_cast<float>(values[2]))
  1558. && std::isfinite(static_cast<float>(values[3]))
  1559. && std::isfinite(static_cast<float>(values[4]))
  1560. && std::isfinite(static_cast<float>(values[5])));
  1561. Source->OrientAt[0] = static_cast<float>(values[0]);
  1562. Source->OrientAt[1] = static_cast<float>(values[1]);
  1563. Source->OrientAt[2] = static_cast<float>(values[2]);
  1564. Source->OrientUp[0] = static_cast<float>(values[3]);
  1565. Source->OrientUp[1] = static_cast<float>(values[4]);
  1566. Source->OrientUp[2] = static_cast<float>(values[5]);
  1567. return UpdateSourceProps(Source, Context);
  1568. case AL_DIRECT_FILTER:
  1569. if constexpr(std::is_integral_v<T>)
  1570. {
  1571. CheckSize(1);
  1572. const auto filterid = static_cast<std::make_unsigned_t<T>>(values[0]);
  1573. if(values[0])
  1574. {
  1575. std::lock_guard<std::mutex> filterlock{device->FilterLock};
  1576. ALfilter *filter{LookupFilter(device, filterid)};
  1577. if(!filter)
  1578. throw al::context_error{AL_INVALID_VALUE, "Invalid filter ID %s",
  1579. std::to_string(filterid).c_str()};
  1580. Source->Direct.Gain = filter->Gain;
  1581. Source->Direct.GainHF = filter->GainHF;
  1582. Source->Direct.HFReference = filter->HFReference;
  1583. Source->Direct.GainLF = filter->GainLF;
  1584. Source->Direct.LFReference = filter->LFReference;
  1585. }
  1586. else
  1587. {
  1588. Source->Direct.Gain = 1.0f;
  1589. Source->Direct.GainHF = 1.0f;
  1590. Source->Direct.HFReference = LowPassFreqRef;
  1591. Source->Direct.GainLF = 1.0f;
  1592. Source->Direct.LFReference = HighPassFreqRef;
  1593. }
  1594. return UpdateSourceProps(Source, Context);
  1595. }
  1596. break;
  1597. case AL_DIRECT_FILTER_GAINHF_AUTO:
  1598. if constexpr(std::is_integral_v<T>)
  1599. {
  1600. CheckSize(1);
  1601. CheckValue(values[0] == AL_FALSE || values[0] == AL_TRUE);
  1602. Source->DryGainHFAuto = values[0] != AL_FALSE;
  1603. return UpdateSourceProps(Source, Context);
  1604. }
  1605. break;
  1606. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  1607. if constexpr(std::is_integral_v<T>)
  1608. {
  1609. CheckSize(1);
  1610. CheckValue(values[0] == AL_FALSE || values[0] == AL_TRUE);
  1611. Source->WetGainAuto = values[0] != AL_FALSE;
  1612. return UpdateSourceProps(Source, Context);
  1613. }
  1614. break;
  1615. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  1616. if constexpr(std::is_integral_v<T>)
  1617. {
  1618. CheckSize(1);
  1619. CheckValue(values[0] == AL_FALSE || values[0] == AL_TRUE);
  1620. Source->WetGainHFAuto = values[0] != AL_FALSE;
  1621. return UpdateSourceProps(Source, Context);
  1622. }
  1623. break;
  1624. case AL_DIRECT_CHANNELS_SOFT:
  1625. if constexpr(std::is_integral_v<T>)
  1626. {
  1627. CheckSize(1);
  1628. if(auto mode = DirectModeFromEnum(values[0]))
  1629. {
  1630. Source->DirectChannels = *mode;
  1631. return UpdateSourceProps(Source, Context);
  1632. }
  1633. throw al::context_error{AL_INVALID_VALUE, "Invalid direct channels mode: %s\n",
  1634. HexPrinter{values[0]}.c_str()};
  1635. }
  1636. break;
  1637. case AL_DISTANCE_MODEL:
  1638. if constexpr(std::is_integral_v<T>)
  1639. {
  1640. CheckSize(1);
  1641. if(auto model = DistanceModelFromALenum(values[0]))
  1642. {
  1643. Source->mDistanceModel = *model;
  1644. if(Context->mSourceDistanceModel)
  1645. UpdateSourceProps(Source, Context);
  1646. return;
  1647. }
  1648. throw al::context_error{AL_INVALID_VALUE, "Invalid distance model: %s\n",
  1649. HexPrinter{values[0]}.c_str()};
  1650. }
  1651. break;
  1652. case AL_SOURCE_RESAMPLER_SOFT:
  1653. if constexpr(std::is_integral_v<T>)
  1654. {
  1655. CheckSize(1);
  1656. CheckValue(values[0] >= 0 && values[0] <= static_cast<int>(Resampler::Max));
  1657. Source->mResampler = static_cast<Resampler>(values[0]);
  1658. return UpdateSourceProps(Source, Context);
  1659. }
  1660. break;
  1661. case AL_SOURCE_SPATIALIZE_SOFT:
  1662. if constexpr(std::is_integral_v<T>)
  1663. {
  1664. CheckSize(1);
  1665. if(auto mode = SpatializeModeFromEnum(values[0]))
  1666. {
  1667. Source->mSpatialize = *mode;
  1668. return UpdateSourceProps(Source, Context);
  1669. }
  1670. throw al::context_error{AL_INVALID_VALUE, "Invalid source spatialize mode: %s\n",
  1671. HexPrinter{values[0]}.c_str()};
  1672. }
  1673. break;
  1674. case AL_STEREO_MODE_SOFT:
  1675. if constexpr(std::is_integral_v<T>)
  1676. {
  1677. CheckSize(1);
  1678. if(const ALenum state{GetSourceState(Source, GetSourceVoice(Source, Context))};
  1679. state == AL_PLAYING || state == AL_PAUSED)
  1680. throw al::context_error{AL_INVALID_OPERATION,
  1681. "Modifying stereo mode on playing or paused source %u", Source->id};
  1682. if(auto mode = StereoModeFromEnum(values[0]))
  1683. {
  1684. Source->mStereoMode = *mode;
  1685. return;
  1686. }
  1687. throw al::context_error{AL_INVALID_VALUE, "Invalid stereo mode: %s\n",
  1688. HexPrinter{values[0]}.c_str()};
  1689. }
  1690. break;
  1691. case AL_AUXILIARY_SEND_FILTER:
  1692. if constexpr(std::is_integral_v<T>)
  1693. {
  1694. CheckSize(3);
  1695. const auto slotid = static_cast<std::make_unsigned_t<T>>(values[0]);
  1696. const auto sendidx = static_cast<std::make_unsigned_t<T>>(values[1]);
  1697. const auto filterid = static_cast<std::make_unsigned_t<T>>(values[2]);
  1698. std::unique_lock slotlock{Context->mEffectSlotLock};
  1699. ALeffectslot *slot{};
  1700. if(values[0])
  1701. {
  1702. slot = LookupEffectSlot(Context, slotid);
  1703. if(!slot)
  1704. throw al::context_error{AL_INVALID_VALUE, "Invalid effect ID %s",
  1705. std::to_string(slotid).c_str()};
  1706. }
  1707. if(sendidx >= device->NumAuxSends)
  1708. throw al::context_error{AL_INVALID_VALUE, "Invalid send %s",
  1709. std::to_string(sendidx).c_str()};
  1710. auto &send = Source->Send[static_cast<size_t>(sendidx)];
  1711. if(values[2])
  1712. {
  1713. std::lock_guard<std::mutex> filterlock{device->FilterLock};
  1714. ALfilter *filter{LookupFilter(device, filterid)};
  1715. if(!filter)
  1716. throw al::context_error{AL_INVALID_VALUE, "Invalid filter ID %s",
  1717. std::to_string(filterid).c_str()};
  1718. send.Gain = filter->Gain;
  1719. send.GainHF = filter->GainHF;
  1720. send.HFReference = filter->HFReference;
  1721. send.GainLF = filter->GainLF;
  1722. send.LFReference = filter->LFReference;
  1723. }
  1724. else
  1725. {
  1726. /* Disable filter */
  1727. send.Gain = 1.0f;
  1728. send.GainHF = 1.0f;
  1729. send.HFReference = LowPassFreqRef;
  1730. send.GainLF = 1.0f;
  1731. send.LFReference = HighPassFreqRef;
  1732. }
  1733. /* We must force an update if the current auxiliary slot is valid
  1734. * and about to be changed on an active source, in case the old
  1735. * slot is about to be deleted.
  1736. */
  1737. if(send.Slot && slot != send.Slot && IsPlayingOrPaused(Source))
  1738. {
  1739. /* Add refcount on the new slot, and release the previous slot */
  1740. if(slot) IncrementRef(slot->ref);
  1741. if(auto *oldslot = send.Slot)
  1742. DecrementRef(oldslot->ref);
  1743. send.Slot = slot;
  1744. Voice *voice{GetSourceVoice(Source, Context)};
  1745. if(voice) UpdateSourceProps(Source, voice, Context);
  1746. else Source->mPropsDirty = true;
  1747. }
  1748. else
  1749. {
  1750. if(slot) IncrementRef(slot->ref);
  1751. if(auto *oldslot = send.Slot)
  1752. DecrementRef(oldslot->ref);
  1753. send.Slot = slot;
  1754. UpdateSourceProps(Source, Context);
  1755. }
  1756. return;
  1757. }
  1758. break;
  1759. }
  1760. ERR("Unexpected %s property: 0x%04x\n", PropType<T>::Name(), prop);
  1761. throw al::context_error{AL_INVALID_ENUM, "Invalid source %s property 0x%04x",
  1762. PropType<T>::Name(), prop};
  1763. }
  1764. template<typename T, size_t N>
  1765. auto GetSizeChecker(const SourceProp prop, const al::span<T,N> values)
  1766. {
  1767. return [=](size_t expect) -> void
  1768. {
  1769. if(values.size() == expect) LIKELY return;
  1770. throw al::context_error{AL_INVALID_ENUM, "Property 0x%04x expects %zu value(s), got %zu",
  1771. prop, expect, values.size()};
  1772. };
  1773. }
  1774. template<typename T>
  1775. NOINLINE void GetProperty(ALsource *const Source, ALCcontext *const Context, const SourceProp prop,
  1776. const al::span<T> values)
  1777. {
  1778. using std::chrono::duration_cast;
  1779. auto CheckSize = GetSizeChecker(prop, values);
  1780. ALCdevice *device{Context->mALDevice.get()};
  1781. switch(prop)
  1782. {
  1783. case AL_GAIN:
  1784. CheckSize(1);
  1785. values[0] = static_cast<T>(Source->Gain);
  1786. return;
  1787. case AL_PITCH:
  1788. CheckSize(1);
  1789. values[0] = static_cast<T>(Source->Pitch);
  1790. return;
  1791. case AL_MAX_DISTANCE:
  1792. CheckSize(1);
  1793. values[0] = static_cast<T>(Source->MaxDistance);
  1794. return;
  1795. case AL_ROLLOFF_FACTOR:
  1796. CheckSize(1);
  1797. values[0] = static_cast<T>(Source->RolloffFactor);
  1798. return;
  1799. case AL_REFERENCE_DISTANCE:
  1800. CheckSize(1);
  1801. values[0] = static_cast<T>(Source->RefDistance);
  1802. return;
  1803. case AL_CONE_INNER_ANGLE:
  1804. CheckSize(1);
  1805. values[0] = static_cast<T>(Source->InnerAngle);
  1806. return;
  1807. case AL_CONE_OUTER_ANGLE:
  1808. CheckSize(1);
  1809. values[0] = static_cast<T>(Source->OuterAngle);
  1810. return;
  1811. case AL_MIN_GAIN:
  1812. CheckSize(1);
  1813. values[0] = static_cast<T>(Source->MinGain);
  1814. return;
  1815. case AL_MAX_GAIN:
  1816. CheckSize(1);
  1817. values[0] = static_cast<T>(Source->MaxGain);
  1818. return;
  1819. case AL_CONE_OUTER_GAIN:
  1820. CheckSize(1);
  1821. values[0] = static_cast<T>(Source->OuterGain);
  1822. return;
  1823. case AL_SEC_OFFSET:
  1824. case AL_SAMPLE_OFFSET:
  1825. case AL_BYTE_OFFSET:
  1826. CheckSize(1);
  1827. values[0] = GetSourceOffset<T>(Source, prop, Context);
  1828. return;
  1829. case AL_CONE_OUTER_GAINHF:
  1830. CheckSize(1);
  1831. values[0] = static_cast<T>(Source->OuterGainHF);
  1832. return;
  1833. case AL_AIR_ABSORPTION_FACTOR:
  1834. CheckSize(1);
  1835. values[0] = static_cast<T>(Source->AirAbsorptionFactor);
  1836. return;
  1837. case AL_ROOM_ROLLOFF_FACTOR:
  1838. CheckSize(1);
  1839. values[0] = static_cast<T>(Source->RoomRolloffFactor);
  1840. return;
  1841. case AL_DOPPLER_FACTOR:
  1842. CheckSize(1);
  1843. values[0] = static_cast<T>(Source->DopplerFactor);
  1844. return;
  1845. case AL_SAMPLE_RW_OFFSETS_SOFT:
  1846. if constexpr(std::is_integral_v<T>)
  1847. {
  1848. if(sBufferSubDataCompat)
  1849. {
  1850. CheckSize(2);
  1851. values[0] = GetSourceOffset<T>(Source, AL_SAMPLE_OFFSET, Context);
  1852. /* FIXME: values[1] should be ahead of values[0] by the device
  1853. * update time. It needs to clamp or wrap the length of the
  1854. * buffer queue.
  1855. */
  1856. values[1] = values[0];
  1857. return;
  1858. }
  1859. }
  1860. break;
  1861. case AL_SOURCE_RADIUS: /*AL_BYTE_RW_OFFSETS_SOFT:*/
  1862. if constexpr(std::is_floating_point_v<T>)
  1863. {
  1864. if(sBufferSubDataCompat)
  1865. break;
  1866. CheckSize(1);
  1867. values[0] = static_cast<T>(Source->Radius);
  1868. return;
  1869. }
  1870. else
  1871. {
  1872. if(sBufferSubDataCompat)
  1873. {
  1874. CheckSize(2);
  1875. values[0] = GetSourceOffset<T>(Source, AL_BYTE_OFFSET, Context);
  1876. /* FIXME: values[1] should be ahead of values[0] by the device
  1877. * update time. It needs to clamp or wrap the length of the
  1878. * buffer queue.
  1879. */
  1880. values[1] = values[0];
  1881. return;
  1882. }
  1883. break;
  1884. }
  1885. case AL_SUPER_STEREO_WIDTH_SOFT:
  1886. CheckSize(1);
  1887. values[0] = static_cast<T>(Source->EnhWidth);
  1888. return;
  1889. case AL_BYTE_LENGTH_SOFT:
  1890. case AL_SAMPLE_LENGTH_SOFT:
  1891. case AL_SEC_LENGTH_SOFT:
  1892. CheckSize(1);
  1893. values[0] = GetSourceLength<T>(Source, prop);
  1894. return;
  1895. case AL_PANNING_ENABLED_SOFT:
  1896. CheckSize(1);
  1897. values[0] = Source->mPanningEnabled;
  1898. return;
  1899. case AL_PAN_SOFT:
  1900. CheckSize(1);
  1901. values[0] = static_cast<T>(Source->mPan);
  1902. return;
  1903. case AL_STEREO_ANGLES:
  1904. if constexpr(std::is_floating_point_v<T>)
  1905. {
  1906. CheckSize(2);
  1907. values[0] = static_cast<T>(Source->StereoPan[0]);
  1908. values[1] = static_cast<T>(Source->StereoPan[1]);
  1909. return;
  1910. }
  1911. break;
  1912. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  1913. if constexpr(std::is_same_v<T,int64_t>)
  1914. {
  1915. CheckSize(2);
  1916. /* Get the source offset with the clock time first. Then get the
  1917. * clock time with the device latency. Order is important.
  1918. */
  1919. ClockLatency clocktime{};
  1920. nanoseconds srcclock{};
  1921. values[0] = GetSourceSampleOffset(Source, Context, &srcclock);
  1922. {
  1923. std::lock_guard<std::mutex> statelock{device->StateLock};
  1924. clocktime = GetClockLatency(device, device->Backend.get());
  1925. }
  1926. if(srcclock == clocktime.ClockTime)
  1927. values[1] = nanoseconds{clocktime.Latency}.count();
  1928. else
  1929. {
  1930. /* If the clock time incremented, reduce the latency by that
  1931. * much since it's that much closer to the source offset it got
  1932. * earlier.
  1933. */
  1934. const auto diff = std::min(clocktime.Latency, clocktime.ClockTime-srcclock);
  1935. values[1] = nanoseconds{clocktime.Latency - diff}.count();
  1936. }
  1937. return;
  1938. }
  1939. break;
  1940. case AL_SAMPLE_OFFSET_CLOCK_SOFT:
  1941. if constexpr(std::is_same_v<T,int64_t>)
  1942. {
  1943. CheckSize(2);
  1944. nanoseconds srcclock{};
  1945. values[0] = GetSourceSampleOffset(Source, Context, &srcclock);
  1946. values[1] = srcclock.count();
  1947. return;
  1948. }
  1949. break;
  1950. case AL_SEC_OFFSET_LATENCY_SOFT:
  1951. if constexpr(std::is_same_v<T,double>)
  1952. {
  1953. CheckSize(2);
  1954. /* Get the source offset with the clock time first. Then get the
  1955. * clock time with the device latency. Order is important.
  1956. */
  1957. ClockLatency clocktime{};
  1958. nanoseconds srcclock{};
  1959. values[0] = GetSourceSecOffset(Source, Context, &srcclock);
  1960. {
  1961. std::lock_guard<std::mutex> statelock{device->StateLock};
  1962. clocktime = GetClockLatency(device, device->Backend.get());
  1963. }
  1964. if(srcclock == clocktime.ClockTime)
  1965. values[1] = duration_cast<seconds_d>(clocktime.Latency).count();
  1966. else
  1967. {
  1968. /* If the clock time incremented, reduce the latency by that
  1969. * much since it's that much closer to the source offset it got
  1970. * earlier.
  1971. */
  1972. const auto diff = std::min(clocktime.Latency, clocktime.ClockTime-srcclock);
  1973. values[1] = duration_cast<seconds_d>(clocktime.Latency - diff).count();
  1974. }
  1975. return;
  1976. }
  1977. break;
  1978. case AL_SEC_OFFSET_CLOCK_SOFT:
  1979. if constexpr(std::is_same_v<T,double>)
  1980. {
  1981. CheckSize(2);
  1982. nanoseconds srcclock{};
  1983. values[0] = GetSourceSecOffset(Source, Context, &srcclock);
  1984. values[1] = duration_cast<seconds_d>(srcclock).count();
  1985. return;
  1986. }
  1987. break;
  1988. case AL_POSITION:
  1989. CheckSize(3);
  1990. values[0] = static_cast<T>(Source->Position[0]);
  1991. values[1] = static_cast<T>(Source->Position[1]);
  1992. values[2] = static_cast<T>(Source->Position[2]);
  1993. return;
  1994. case AL_VELOCITY:
  1995. CheckSize(3);
  1996. values[0] = static_cast<T>(Source->Velocity[0]);
  1997. values[1] = static_cast<T>(Source->Velocity[1]);
  1998. values[2] = static_cast<T>(Source->Velocity[2]);
  1999. return;
  2000. case AL_DIRECTION:
  2001. CheckSize(3);
  2002. values[0] = static_cast<T>(Source->Direction[0]);
  2003. values[1] = static_cast<T>(Source->Direction[1]);
  2004. values[2] = static_cast<T>(Source->Direction[2]);
  2005. return;
  2006. case AL_ORIENTATION:
  2007. CheckSize(6);
  2008. values[0] = static_cast<T>(Source->OrientAt[0]);
  2009. values[1] = static_cast<T>(Source->OrientAt[1]);
  2010. values[2] = static_cast<T>(Source->OrientAt[2]);
  2011. values[3] = static_cast<T>(Source->OrientUp[0]);
  2012. values[4] = static_cast<T>(Source->OrientUp[1]);
  2013. values[5] = static_cast<T>(Source->OrientUp[2]);
  2014. return;
  2015. case AL_SOURCE_RELATIVE:
  2016. if constexpr(std::is_integral_v<T>)
  2017. {
  2018. CheckSize(1);
  2019. values[0] = Source->HeadRelative;
  2020. return;
  2021. }
  2022. break;
  2023. case AL_LOOPING:
  2024. if constexpr(std::is_integral_v<T>)
  2025. {
  2026. CheckSize(1);
  2027. values[0] = Source->Looping;
  2028. return;
  2029. }
  2030. break;
  2031. case AL_BUFFER:
  2032. if constexpr(std::is_integral_v<T>)
  2033. {
  2034. CheckSize(1);
  2035. ALbufferQueueItem *BufferList{};
  2036. /* HACK: This query should technically only return the buffer set
  2037. * on a static source. However, some apps had used it to detect
  2038. * when a streaming source changed buffers, so report the current
  2039. * buffer's ID when playing.
  2040. */
  2041. if(Source->SourceType == AL_STATIC || Source->state == AL_INITIAL)
  2042. {
  2043. if(!Source->mQueue.empty())
  2044. BufferList = &Source->mQueue.front();
  2045. }
  2046. else if(Voice *voice{GetSourceVoice(Source, Context)})
  2047. {
  2048. VoiceBufferItem *Current{voice->mCurrentBuffer.load(std::memory_order_relaxed)};
  2049. BufferList = static_cast<ALbufferQueueItem*>(Current);
  2050. }
  2051. ALbuffer *buffer{BufferList ? BufferList->mBuffer : nullptr};
  2052. values[0] = buffer ? static_cast<T>(buffer->id) : T{0};
  2053. return;
  2054. }
  2055. break;
  2056. case AL_SOURCE_STATE:
  2057. if constexpr(std::is_integral_v<T>)
  2058. {
  2059. CheckSize(1);
  2060. values[0] = GetSourceState(Source, GetSourceVoice(Source, Context));
  2061. return;
  2062. }
  2063. break;
  2064. case AL_BUFFERS_QUEUED:
  2065. if constexpr(std::is_integral_v<T>)
  2066. {
  2067. CheckSize(1);
  2068. values[0] = static_cast<T>(Source->mQueue.size());
  2069. return;
  2070. }
  2071. break;
  2072. case AL_BUFFERS_PROCESSED:
  2073. if constexpr(std::is_integral_v<T>)
  2074. {
  2075. CheckSize(1);
  2076. if(Source->Looping || Source->SourceType != AL_STREAMING)
  2077. {
  2078. /* Buffers on a looping source are in a perpetual state of
  2079. * PENDING, so don't report any as PROCESSED
  2080. */
  2081. values[0] = 0;
  2082. }
  2083. else
  2084. {
  2085. int played{0};
  2086. if(Source->state != AL_INITIAL)
  2087. {
  2088. const VoiceBufferItem *Current{nullptr};
  2089. if(Voice *voice{GetSourceVoice(Source, Context)})
  2090. Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
  2091. for(auto &item : Source->mQueue)
  2092. {
  2093. if(&item == Current)
  2094. break;
  2095. ++played;
  2096. }
  2097. }
  2098. values[0] = played;
  2099. }
  2100. return;
  2101. }
  2102. break;
  2103. case AL_SOURCE_TYPE:
  2104. if constexpr(std::is_integral_v<T>)
  2105. {
  2106. CheckSize(1);
  2107. values[0] = Source->SourceType;
  2108. return;
  2109. }
  2110. break;
  2111. case AL_DIRECT_FILTER_GAINHF_AUTO:
  2112. if constexpr(std::is_integral_v<T>)
  2113. {
  2114. CheckSize(1);
  2115. values[0] = Source->DryGainHFAuto;
  2116. return;
  2117. }
  2118. break;
  2119. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  2120. if constexpr(std::is_integral_v<T>)
  2121. {
  2122. CheckSize(1);
  2123. values[0] = Source->WetGainAuto;
  2124. return;
  2125. }
  2126. break;
  2127. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  2128. if constexpr(std::is_integral_v<T>)
  2129. {
  2130. CheckSize(1);
  2131. values[0] = Source->WetGainHFAuto;
  2132. return;
  2133. }
  2134. break;
  2135. case AL_DIRECT_CHANNELS_SOFT:
  2136. if constexpr(std::is_integral_v<T>)
  2137. {
  2138. CheckSize(1);
  2139. values[0] = EnumFromDirectMode(Source->DirectChannels);
  2140. return;
  2141. }
  2142. break;
  2143. case AL_DISTANCE_MODEL:
  2144. if constexpr(std::is_integral_v<T>)
  2145. {
  2146. CheckSize(1);
  2147. values[0] = ALenumFromDistanceModel(Source->mDistanceModel);
  2148. return;
  2149. }
  2150. break;
  2151. case AL_SOURCE_RESAMPLER_SOFT:
  2152. if constexpr(std::is_integral_v<T>)
  2153. {
  2154. CheckSize(1);
  2155. values[0] = static_cast<T>(Source->mResampler);
  2156. return;
  2157. }
  2158. break;
  2159. case AL_SOURCE_SPATIALIZE_SOFT:
  2160. if constexpr(std::is_integral_v<T>)
  2161. {
  2162. CheckSize(1);
  2163. values[0] = EnumFromSpatializeMode(Source->mSpatialize);
  2164. return;
  2165. }
  2166. break;
  2167. case AL_STEREO_MODE_SOFT:
  2168. if constexpr(std::is_integral_v<T>)
  2169. {
  2170. CheckSize(1);
  2171. values[0] = EnumFromStereoMode(Source->mStereoMode);
  2172. return;
  2173. }
  2174. break;
  2175. case AL_DIRECT_FILTER:
  2176. case AL_AUXILIARY_SEND_FILTER:
  2177. break;
  2178. }
  2179. ERR("Unexpected %s query property: 0x%04x\n", PropType<T>::Name(), prop);
  2180. throw al::context_error{AL_INVALID_ENUM, "Invalid source %s query property 0x%04x",
  2181. PropType<T>::Name(), prop};
  2182. }
  2183. void StartSources(ALCcontext *const context, const al::span<ALsource*> srchandles,
  2184. const nanoseconds start_time=nanoseconds::min())
  2185. {
  2186. ALCdevice *device{context->mALDevice.get()};
  2187. /* If the device is disconnected, and voices stop on disconnect, go right
  2188. * to stopped.
  2189. */
  2190. if(!device->Connected.load(std::memory_order_acquire)) UNLIKELY
  2191. {
  2192. if(context->mStopVoicesOnDisconnect.load(std::memory_order_acquire))
  2193. {
  2194. for(ALsource *source : srchandles)
  2195. {
  2196. /* TODO: Send state change event? */
  2197. source->Offset = 0.0;
  2198. source->OffsetType = AL_NONE;
  2199. source->state = AL_STOPPED;
  2200. }
  2201. return;
  2202. }
  2203. }
  2204. /* Count the number of reusable voices. */
  2205. auto voicelist = context->getVoicesSpan();
  2206. size_t free_voices{0};
  2207. for(const Voice *voice : voicelist)
  2208. {
  2209. free_voices += (voice->mPlayState.load(std::memory_order_acquire) == Voice::Stopped
  2210. && voice->mSourceID.load(std::memory_order_relaxed) == 0u
  2211. && voice->mPendingChange.load(std::memory_order_relaxed) == false);
  2212. if(free_voices == srchandles.size())
  2213. break;
  2214. }
  2215. if(srchandles.size() != free_voices) UNLIKELY
  2216. {
  2217. const size_t inc_amount{srchandles.size() - free_voices};
  2218. auto &allvoices = *context->mVoices.load(std::memory_order_relaxed);
  2219. if(inc_amount > allvoices.size() - voicelist.size())
  2220. {
  2221. /* Increase the number of voices to handle the request. */
  2222. context->allocVoices(inc_amount - (allvoices.size() - voicelist.size()));
  2223. }
  2224. context->mActiveVoiceCount.fetch_add(inc_amount, std::memory_order_release);
  2225. voicelist = context->getVoicesSpan();
  2226. }
  2227. auto voiceiter = voicelist.begin();
  2228. ALuint vidx{0};
  2229. VoiceChange *tail{}, *cur{};
  2230. for(ALsource *source : srchandles)
  2231. {
  2232. /* Check that there is a queue containing at least one valid, non zero
  2233. * length buffer.
  2234. */
  2235. auto find_buffer = [](ALbufferQueueItem &entry) noexcept
  2236. { return entry.mSampleLen != 0 || entry.mCallback != nullptr; };
  2237. auto BufferList = std::find_if(source->mQueue.begin(), source->mQueue.end(), find_buffer);
  2238. /* If there's nothing to play, go right to stopped. */
  2239. if(BufferList == source->mQueue.end()) UNLIKELY
  2240. {
  2241. /* NOTE: A source without any playable buffers should not have a
  2242. * Voice since it shouldn't be in a playing or paused state. So
  2243. * there's no need to look up its voice and clear the source.
  2244. */
  2245. source->Offset = 0.0;
  2246. source->OffsetType = AL_NONE;
  2247. source->state = AL_STOPPED;
  2248. continue;
  2249. }
  2250. if(!cur)
  2251. cur = tail = GetVoiceChanger(context);
  2252. else
  2253. {
  2254. cur->mNext.store(GetVoiceChanger(context), std::memory_order_relaxed);
  2255. cur = cur->mNext.load(std::memory_order_relaxed);
  2256. }
  2257. Voice *voice{GetSourceVoice(source, context)};
  2258. switch(GetSourceState(source, voice))
  2259. {
  2260. case AL_PAUSED:
  2261. /* A source that's paused simply resumes. If there's no voice, it
  2262. * was lost from a disconnect, so just start over with a new one.
  2263. */
  2264. cur->mOldVoice = nullptr;
  2265. if(!voice) break;
  2266. cur->mVoice = voice;
  2267. cur->mSourceID = source->id;
  2268. cur->mState = VChangeState::Play;
  2269. source->state = AL_PLAYING;
  2270. #ifdef ALSOFT_EAX
  2271. if(context->hasEax())
  2272. source->eaxCommit();
  2273. #endif // ALSOFT_EAX
  2274. continue;
  2275. case AL_PLAYING:
  2276. /* A source that's already playing is restarted from the beginning.
  2277. * Stop the current voice and start a new one so it properly cross-
  2278. * fades back to the beginning.
  2279. */
  2280. if(voice)
  2281. voice->mPendingChange.store(true, std::memory_order_relaxed);
  2282. cur->mOldVoice = voice;
  2283. voice = nullptr;
  2284. break;
  2285. default:
  2286. assert(voice == nullptr);
  2287. cur->mOldVoice = nullptr;
  2288. #ifdef ALSOFT_EAX
  2289. if(context->hasEax())
  2290. source->eaxCommit();
  2291. #endif // ALSOFT_EAX
  2292. break;
  2293. }
  2294. /* Find the next unused voice to play this source with. */
  2295. for(;voiceiter != voicelist.end();++voiceiter,++vidx)
  2296. {
  2297. Voice *v{*voiceiter};
  2298. if(v->mPlayState.load(std::memory_order_acquire) == Voice::Stopped
  2299. && v->mSourceID.load(std::memory_order_relaxed) == 0u
  2300. && v->mPendingChange.load(std::memory_order_relaxed) == false)
  2301. {
  2302. voice = v;
  2303. break;
  2304. }
  2305. }
  2306. ASSUME(voice != nullptr);
  2307. voice->mPosition.store(0, std::memory_order_relaxed);
  2308. voice->mPositionFrac.store(0, std::memory_order_relaxed);
  2309. voice->mCurrentBuffer.store(&source->mQueue.front(), std::memory_order_relaxed);
  2310. voice->mStartTime = start_time;
  2311. voice->mFlags.reset();
  2312. /* A source that's not playing or paused has any offset applied when it
  2313. * starts playing.
  2314. */
  2315. if(const ALenum offsettype{source->OffsetType})
  2316. {
  2317. const double offset{source->Offset};
  2318. source->OffsetType = AL_NONE;
  2319. source->Offset = 0.0;
  2320. if(auto vpos = GetSampleOffset(source->mQueue, offsettype, offset))
  2321. {
  2322. voice->mPosition.store(vpos->pos, std::memory_order_relaxed);
  2323. voice->mPositionFrac.store(vpos->frac, std::memory_order_relaxed);
  2324. voice->mCurrentBuffer.store(vpos->bufferitem, std::memory_order_relaxed);
  2325. if(vpos->pos > 0 || (vpos->pos == 0 && vpos->frac > 0)
  2326. || vpos->bufferitem != &source->mQueue.front())
  2327. voice->mFlags.set(VoiceIsFading);
  2328. }
  2329. }
  2330. InitVoice(voice, source, al::to_address(BufferList), context, device);
  2331. source->VoiceIdx = vidx;
  2332. source->state = AL_PLAYING;
  2333. cur->mVoice = voice;
  2334. cur->mSourceID = source->id;
  2335. cur->mState = VChangeState::Play;
  2336. }
  2337. if(tail) LIKELY
  2338. SendVoiceChanges(context, tail);
  2339. }
  2340. } // namespace
  2341. AL_API DECL_FUNC2(void, alGenSources, ALsizei,n, ALuint*,sources)
  2342. FORCE_ALIGN void AL_APIENTRY alGenSourcesDirect(ALCcontext *context, ALsizei n, ALuint *sources) noexcept
  2343. try {
  2344. if(n < 0)
  2345. throw al::context_error{AL_INVALID_VALUE, "Generating %d sources", n};
  2346. if(n <= 0) UNLIKELY return;
  2347. std::unique_lock<std::mutex> srclock{context->mSourceLock};
  2348. ALCdevice *device{context->mALDevice.get()};
  2349. const al::span sids{sources, static_cast<ALuint>(n)};
  2350. if(sids.size() > device->SourcesMax-context->mNumSources)
  2351. throw al::context_error{AL_OUT_OF_MEMORY, "Exceeding %u source limit (%u + %d)",
  2352. device->SourcesMax, context->mNumSources, n};
  2353. if(!EnsureSources(context, sids.size()))
  2354. throw al::context_error{AL_OUT_OF_MEMORY, "Failed to allocate %d source%s", n,
  2355. (n == 1) ? "" : "s"};
  2356. std::generate(sids.begin(), sids.end(), [context]{ return AllocSource(context)->id; });
  2357. }
  2358. catch(al::context_error& e) {
  2359. context->setError(e.errorCode(), "%s", e.what());
  2360. }
  2361. AL_API DECL_FUNC2(void, alDeleteSources, ALsizei,n, const ALuint*,sources)
  2362. FORCE_ALIGN void AL_APIENTRY alDeleteSourcesDirect(ALCcontext *context, ALsizei n,
  2363. const ALuint *sources) noexcept
  2364. try {
  2365. if(n < 0)
  2366. throw al::context_error{AL_INVALID_VALUE, "Deleting %d sources", n};
  2367. if(n <= 0) UNLIKELY return;
  2368. std::lock_guard<std::mutex> srclock{context->mSourceLock};
  2369. /* Check that all Sources are valid */
  2370. auto validate_source = [context](const ALuint sid) -> bool
  2371. { return LookupSource(context, sid) != nullptr; };
  2372. const al::span sids{sources, static_cast<ALuint>(n)};
  2373. auto invsrc = std::find_if_not(sids.begin(), sids.end(), validate_source);
  2374. if(invsrc != sids.end())
  2375. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", *invsrc};
  2376. /* All good. Delete source IDs. */
  2377. auto delete_source = [&context](const ALuint sid) -> void
  2378. {
  2379. if(ALsource *src{LookupSource(context, sid)})
  2380. FreeSource(context, src);
  2381. };
  2382. std::for_each(sids.begin(), sids.end(), delete_source);
  2383. }
  2384. catch(al::context_error& e) {
  2385. context->setError(e.errorCode(), "%s", e.what());
  2386. }
  2387. AL_API DECL_FUNC1(ALboolean, alIsSource, ALuint,source)
  2388. FORCE_ALIGN ALboolean AL_APIENTRY alIsSourceDirect(ALCcontext *context, ALuint source) noexcept
  2389. {
  2390. std::lock_guard<std::mutex> srclock{context->mSourceLock};
  2391. if(LookupSource(context, source) != nullptr)
  2392. return AL_TRUE;
  2393. return AL_FALSE;
  2394. }
  2395. AL_API DECL_FUNC3(void, alSourcef, ALuint,source, ALenum,param, ALfloat,value)
  2396. FORCE_ALIGN void AL_APIENTRY alSourcefDirect(ALCcontext *context, ALuint source, ALenum param,
  2397. ALfloat value) noexcept
  2398. try {
  2399. std::lock_guard<std::mutex> proplock{context->mPropLock};
  2400. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2401. ALsource *Source{LookupSource(context, source)};
  2402. if(!Source)
  2403. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2404. SetProperty<float>(Source, context, static_cast<SourceProp>(param), {&value, 1u});
  2405. }
  2406. catch(al::context_error& e) {
  2407. context->setError(e.errorCode(), "%s", e.what());
  2408. }
  2409. AL_API DECL_FUNC5(void, alSource3f, ALuint,source, ALenum,param, ALfloat,value1, ALfloat,value2, ALfloat,value3)
  2410. FORCE_ALIGN void AL_APIENTRY alSource3fDirect(ALCcontext *context, ALuint source, ALenum param,
  2411. ALfloat value1, ALfloat value2, ALfloat value3) noexcept
  2412. try {
  2413. std::lock_guard<std::mutex> proplock{context->mPropLock};
  2414. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2415. ALsource *Source{LookupSource(context, source)};
  2416. if(!Source)
  2417. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2418. const std::array fvals{value1, value2, value3};
  2419. SetProperty<float>(Source, context, static_cast<SourceProp>(param), fvals);
  2420. }
  2421. catch(al::context_error& e) {
  2422. context->setError(e.errorCode(), "%s", e.what());
  2423. }
  2424. AL_API DECL_FUNC3(void, alSourcefv, ALuint,source, ALenum,param, const ALfloat*,values)
  2425. FORCE_ALIGN void AL_APIENTRY alSourcefvDirect(ALCcontext *context, ALuint source, ALenum param,
  2426. const ALfloat *values) noexcept
  2427. try {
  2428. std::lock_guard<std::mutex> proplock{context->mPropLock};
  2429. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2430. ALsource *Source{LookupSource(context, source)};
  2431. if(!Source)
  2432. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2433. if(!values)
  2434. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2435. const ALuint count{FloatValsByProp(param)};
  2436. SetProperty(Source, context, static_cast<SourceProp>(param), al::span{values, count});
  2437. }
  2438. catch(al::context_error& e) {
  2439. context->setError(e.errorCode(), "%s", e.what());
  2440. }
  2441. AL_API DECL_FUNCEXT3(void, alSourced,SOFT, ALuint,source, ALenum,param, ALdouble,value)
  2442. FORCE_ALIGN void AL_APIENTRY alSourcedDirectSOFT(ALCcontext *context, ALuint source, ALenum param,
  2443. ALdouble value) noexcept
  2444. try {
  2445. std::lock_guard<std::mutex> proplock{context->mPropLock};
  2446. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2447. ALsource *Source{LookupSource(context, source)};
  2448. if(!Source)
  2449. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2450. SetProperty<double>(Source, context, static_cast<SourceProp>(param), {&value, 1});
  2451. }
  2452. catch(al::context_error& e) {
  2453. context->setError(e.errorCode(), "%s", e.what());
  2454. }
  2455. AL_API DECL_FUNCEXT5(void, alSource3d,SOFT, ALuint,source, ALenum,param, ALdouble,value1, ALdouble,value2, ALdouble,value3)
  2456. FORCE_ALIGN void AL_APIENTRY alSource3dDirectSOFT(ALCcontext *context, ALuint source, ALenum param,
  2457. ALdouble value1, ALdouble value2, ALdouble value3) noexcept
  2458. try {
  2459. std::lock_guard<std::mutex> proplock{context->mPropLock};
  2460. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2461. ALsource *Source{LookupSource(context, source)};
  2462. if(!Source)
  2463. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2464. const std::array dvals{value1, value2, value3};
  2465. SetProperty<double>(Source, context, static_cast<SourceProp>(param), dvals);
  2466. }
  2467. catch(al::context_error& e) {
  2468. context->setError(e.errorCode(), "%s", e.what());
  2469. }
  2470. AL_API DECL_FUNCEXT3(void, alSourcedv,SOFT, ALuint,source, ALenum,param, const ALdouble*,values)
  2471. FORCE_ALIGN void AL_APIENTRY alSourcedvDirectSOFT(ALCcontext *context, ALuint source, ALenum param,
  2472. const ALdouble *values) noexcept
  2473. try {
  2474. std::lock_guard<std::mutex> proplock{context->mPropLock};
  2475. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2476. ALsource *Source{LookupSource(context, source)};
  2477. if(!Source)
  2478. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2479. if(!values)
  2480. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2481. const ALuint count{DoubleValsByProp(param)};
  2482. SetProperty(Source, context, static_cast<SourceProp>(param), al::span{values, count});
  2483. }
  2484. catch(al::context_error& e) {
  2485. context->setError(e.errorCode(), "%s", e.what());
  2486. }
  2487. AL_API DECL_FUNC3(void, alSourcei, ALuint,source, ALenum,param, ALint,value)
  2488. FORCE_ALIGN void AL_APIENTRY alSourceiDirect(ALCcontext *context, ALuint source, ALenum param,
  2489. ALint value) noexcept
  2490. try {
  2491. std::lock_guard<std::mutex> proplock{context->mPropLock};
  2492. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2493. ALsource *Source{LookupSource(context, source)};
  2494. if(!Source)
  2495. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2496. SetProperty<int>(Source, context, static_cast<SourceProp>(param), {&value, 1u});
  2497. }
  2498. catch(al::context_error& e) {
  2499. context->setError(e.errorCode(), "%s", e.what());
  2500. }
  2501. AL_API DECL_FUNC5(void, alSource3i, ALuint,buffer, ALenum,param, ALint,value1, ALint,value2, ALint,value3)
  2502. FORCE_ALIGN void AL_APIENTRY alSource3iDirect(ALCcontext *context, ALuint source, ALenum param,
  2503. ALint value1, ALint value2, ALint value3) noexcept
  2504. try {
  2505. std::lock_guard<std::mutex> proplock{context->mPropLock};
  2506. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2507. ALsource *Source{LookupSource(context, source)};
  2508. if(!Source)
  2509. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2510. const std::array ivals{value1, value2, value3};
  2511. SetProperty<int>(Source, context, static_cast<SourceProp>(param), ivals);
  2512. }
  2513. catch(al::context_error& e) {
  2514. context->setError(e.errorCode(), "%s", e.what());
  2515. }
  2516. AL_API DECL_FUNC3(void, alSourceiv, ALuint,source, ALenum,param, const ALint*,values)
  2517. FORCE_ALIGN void AL_APIENTRY alSourceivDirect(ALCcontext *context, ALuint source, ALenum param,
  2518. const ALint *values) noexcept
  2519. try {
  2520. std::lock_guard<std::mutex> proplock{context->mPropLock};
  2521. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2522. ALsource *Source{LookupSource(context, source)};
  2523. if(!Source)
  2524. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2525. if(!values)
  2526. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2527. const ALuint count{IntValsByProp(param)};
  2528. SetProperty(Source, context, static_cast<SourceProp>(param), al::span{values, count});
  2529. }
  2530. catch(al::context_error& e) {
  2531. context->setError(e.errorCode(), "%s", e.what());
  2532. }
  2533. AL_API DECL_FUNCEXT3(void, alSourcei64,SOFT, ALuint,source, ALenum,param, ALint64SOFT,value)
  2534. FORCE_ALIGN void AL_APIENTRY alSourcei64DirectSOFT(ALCcontext *context, ALuint source,
  2535. ALenum param, ALint64SOFT value) noexcept
  2536. try {
  2537. std::lock_guard<std::mutex> proplock{context->mPropLock};
  2538. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2539. ALsource *Source{LookupSource(context, source)};
  2540. if(!Source)
  2541. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2542. SetProperty<int64_t>(Source, context, static_cast<SourceProp>(param), {&value, 1u});
  2543. }
  2544. catch(al::context_error& e) {
  2545. context->setError(e.errorCode(), "%s", e.what());
  2546. }
  2547. AL_API DECL_FUNCEXT5(void, alSource3i64,SOFT, ALuint,source, ALenum,param, ALint64SOFT,value1, ALint64SOFT,value2, ALint64SOFT,value3)
  2548. FORCE_ALIGN void AL_APIENTRY alSource3i64DirectSOFT(ALCcontext *context, ALuint source,
  2549. ALenum param, ALint64SOFT value1, ALint64SOFT value2, ALint64SOFT value3) noexcept
  2550. try {
  2551. std::lock_guard<std::mutex> proplock{context->mPropLock};
  2552. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2553. ALsource *Source{LookupSource(context, source)};
  2554. if(!Source)
  2555. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2556. const std::array i64vals{value1, value2, value3};
  2557. SetProperty<int64_t>(Source, context, static_cast<SourceProp>(param), i64vals);
  2558. }
  2559. catch(al::context_error& e) {
  2560. context->setError(e.errorCode(), "%s", e.what());
  2561. }
  2562. AL_API DECL_FUNCEXT3(void, alSourcei64v,SOFT, ALuint,source, ALenum,param, const ALint64SOFT*,values)
  2563. FORCE_ALIGN void AL_APIENTRY alSourcei64vDirectSOFT(ALCcontext *context, ALuint source,
  2564. ALenum param, const ALint64SOFT *values) noexcept
  2565. try {
  2566. std::lock_guard<std::mutex> proplock{context->mPropLock};
  2567. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2568. ALsource *Source{LookupSource(context, source)};
  2569. if(!Source)
  2570. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2571. if(!values)
  2572. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2573. const ALuint count{Int64ValsByProp(param)};
  2574. SetProperty(Source, context, static_cast<SourceProp>(param), al::span{values, count});
  2575. }
  2576. catch(al::context_error& e) {
  2577. context->setError(e.errorCode(), "%s", e.what());
  2578. }
  2579. AL_API DECL_FUNC3(void, alGetSourcef, ALuint,source, ALenum,param, ALfloat*,value)
  2580. FORCE_ALIGN void AL_APIENTRY alGetSourcefDirect(ALCcontext *context, ALuint source, ALenum param,
  2581. ALfloat *value) noexcept
  2582. try {
  2583. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2584. ALsource *Source{LookupSource(context, source)};
  2585. if(!Source)
  2586. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2587. if(!value)
  2588. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2589. GetProperty(Source, context, static_cast<SourceProp>(param), al::span{value, 1u});
  2590. }
  2591. catch(al::context_error& e) {
  2592. context->setError(e.errorCode(), "%s", e.what());
  2593. }
  2594. AL_API DECL_FUNC5(void, alGetSource3f, ALuint,source, ALenum,param, ALfloat*,value1, ALfloat*,value2, ALfloat*,value3)
  2595. FORCE_ALIGN void AL_APIENTRY alGetSource3fDirect(ALCcontext *context, ALuint source, ALenum param,
  2596. ALfloat *value1, ALfloat *value2, ALfloat *value3) noexcept
  2597. try {
  2598. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2599. ALsource *Source{LookupSource(context, source)};
  2600. if(!Source)
  2601. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2602. if(!(value1 && value2 && value3))
  2603. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2604. std::array<float,3> fvals{};
  2605. GetProperty<float>(Source, context, static_cast<SourceProp>(param), fvals);
  2606. *value1 = fvals[0];
  2607. *value2 = fvals[1];
  2608. *value3 = fvals[2];
  2609. }
  2610. catch(al::context_error& e) {
  2611. context->setError(e.errorCode(), "%s", e.what());
  2612. }
  2613. AL_API DECL_FUNC3(void, alGetSourcefv, ALuint,source, ALenum,param, ALfloat*,values)
  2614. FORCE_ALIGN void AL_APIENTRY alGetSourcefvDirect(ALCcontext *context, ALuint source, ALenum param,
  2615. ALfloat *values) noexcept
  2616. try {
  2617. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2618. ALsource *Source{LookupSource(context, source)};
  2619. if(!Source)
  2620. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2621. if(!values)
  2622. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2623. const ALuint count{FloatValsByProp(param)};
  2624. GetProperty(Source, context, static_cast<SourceProp>(param), al::span{values, count});
  2625. }
  2626. catch(al::context_error& e) {
  2627. context->setError(e.errorCode(), "%s", e.what());
  2628. }
  2629. AL_API DECL_FUNCEXT3(void, alGetSourced,SOFT, ALuint,source, ALenum,param, ALdouble*,value)
  2630. FORCE_ALIGN void AL_APIENTRY alGetSourcedDirectSOFT(ALCcontext *context, ALuint source,
  2631. ALenum param, ALdouble *value) noexcept
  2632. try {
  2633. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2634. ALsource *Source{LookupSource(context, source)};
  2635. if(!Source)
  2636. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2637. if(!value)
  2638. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2639. GetProperty(Source, context, static_cast<SourceProp>(param), al::span{value, 1u});
  2640. }
  2641. catch(al::context_error& e) {
  2642. context->setError(e.errorCode(), "%s", e.what());
  2643. }
  2644. AL_API DECL_FUNCEXT5(void, alGetSource3d,SOFT, ALuint,source, ALenum,param, ALdouble*,value1, ALdouble*,value2, ALdouble*,value3)
  2645. FORCE_ALIGN void AL_APIENTRY alGetSource3dDirectSOFT(ALCcontext *context, ALuint source,
  2646. ALenum param, ALdouble *value1, ALdouble *value2, ALdouble *value3) noexcept
  2647. try {
  2648. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2649. ALsource *Source{LookupSource(context, source)};
  2650. if(!Source)
  2651. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2652. if(!(value1 && value2 && value3))
  2653. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2654. std::array<double,3> dvals{};
  2655. GetProperty<double>(Source, context, static_cast<SourceProp>(param), dvals);
  2656. *value1 = dvals[0];
  2657. *value2 = dvals[1];
  2658. *value3 = dvals[2];
  2659. }
  2660. catch(al::context_error& e) {
  2661. context->setError(e.errorCode(), "%s", e.what());
  2662. }
  2663. AL_API DECL_FUNCEXT3(void, alGetSourcedv,SOFT, ALuint,source, ALenum,param, ALdouble*,values)
  2664. FORCE_ALIGN void AL_APIENTRY alGetSourcedvDirectSOFT(ALCcontext *context, ALuint source,
  2665. ALenum param, ALdouble *values) noexcept
  2666. try {
  2667. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2668. ALsource *Source{LookupSource(context, source)};
  2669. if(!Source)
  2670. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2671. if(!values)
  2672. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2673. const ALuint count{DoubleValsByProp(param)};
  2674. GetProperty(Source, context, static_cast<SourceProp>(param), al::span{values, count});
  2675. }
  2676. catch(al::context_error& e) {
  2677. context->setError(e.errorCode(), "%s", e.what());
  2678. }
  2679. AL_API DECL_FUNC3(void, alGetSourcei, ALuint,source, ALenum,param, ALint*,value)
  2680. FORCE_ALIGN void AL_APIENTRY alGetSourceiDirect(ALCcontext *context, ALuint source, ALenum param,
  2681. ALint *value) noexcept
  2682. try {
  2683. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2684. ALsource *Source{LookupSource(context, source)};
  2685. if(!Source)
  2686. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2687. if(!value)
  2688. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2689. GetProperty(Source, context, static_cast<SourceProp>(param), al::span{value, 1u});
  2690. }
  2691. catch(al::context_error& e) {
  2692. context->setError(e.errorCode(), "%s", e.what());
  2693. }
  2694. AL_API DECL_FUNC5(void, alGetSource3i, ALuint,source, ALenum,param, ALint*,value1, ALint*,value2, ALint*,value3)
  2695. FORCE_ALIGN void AL_APIENTRY alGetSource3iDirect(ALCcontext *context, ALuint source, ALenum param,
  2696. ALint *value1, ALint *value2, ALint *value3) noexcept
  2697. try {
  2698. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2699. ALsource *Source{LookupSource(context, source)};
  2700. if(!Source)
  2701. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2702. if(!(value1 && value2 && value3))
  2703. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2704. std::array<int,3> ivals{};
  2705. GetProperty<int>(Source, context, static_cast<SourceProp>(param), ivals);
  2706. *value1 = ivals[0];
  2707. *value2 = ivals[1];
  2708. *value3 = ivals[2];
  2709. }
  2710. catch(al::context_error& e) {
  2711. context->setError(e.errorCode(), "%s", e.what());
  2712. }
  2713. AL_API DECL_FUNC3(void, alGetSourceiv, ALuint,source, ALenum,param, ALint*,values)
  2714. FORCE_ALIGN void AL_APIENTRY alGetSourceivDirect(ALCcontext *context, ALuint source, ALenum param,
  2715. ALint *values) noexcept
  2716. try {
  2717. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2718. ALsource *Source{LookupSource(context, source)};
  2719. if(!Source)
  2720. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2721. if(!values)
  2722. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2723. const ALuint count{IntValsByProp(param)};
  2724. GetProperty(Source, context, static_cast<SourceProp>(param), al::span{values, count});
  2725. }
  2726. catch(al::context_error& e) {
  2727. context->setError(e.errorCode(), "%s", e.what());
  2728. }
  2729. AL_API DECL_FUNCEXT3(void, alGetSourcei64,SOFT, ALuint,source, ALenum,param, ALint64SOFT*,value)
  2730. FORCE_ALIGN void AL_APIENTRY alGetSourcei64DirectSOFT(ALCcontext *context, ALuint source, ALenum param, ALint64SOFT *value) noexcept
  2731. try {
  2732. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2733. ALsource *Source{LookupSource(context, source)};
  2734. if(!Source)
  2735. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2736. if(!value)
  2737. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2738. GetProperty(Source, context, static_cast<SourceProp>(param), al::span{value, 1u});
  2739. }
  2740. catch(al::context_error& e) {
  2741. context->setError(e.errorCode(), "%s", e.what());
  2742. }
  2743. AL_API DECL_FUNCEXT5(void, alGetSource3i64,SOFT, ALuint,source, ALenum,param, ALint64SOFT*,value1, ALint64SOFT*,value2, ALint64SOFT*,value3)
  2744. FORCE_ALIGN void AL_APIENTRY alGetSource3i64DirectSOFT(ALCcontext *context, ALuint source,
  2745. ALenum param, ALint64SOFT *value1, ALint64SOFT *value2, ALint64SOFT *value3) noexcept
  2746. try {
  2747. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2748. ALsource *Source{LookupSource(context, source)};
  2749. if(!Source)
  2750. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2751. if(!(value1 && value2 && value3))
  2752. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2753. std::array<int64_t,3> i64vals{};
  2754. GetProperty<int64_t>(Source, context, static_cast<SourceProp>(param), i64vals);
  2755. *value1 = i64vals[0];
  2756. *value2 = i64vals[1];
  2757. *value3 = i64vals[2];
  2758. }
  2759. catch(al::context_error& e) {
  2760. context->setError(e.errorCode(), "%s", e.what());
  2761. }
  2762. AL_API DECL_FUNCEXT3(void, alGetSourcei64v,SOFT, ALuint,source, ALenum,param, ALint64SOFT*,values)
  2763. FORCE_ALIGN void AL_APIENTRY alGetSourcei64vDirectSOFT(ALCcontext *context, ALuint source,
  2764. ALenum param, ALint64SOFT *values) noexcept
  2765. try {
  2766. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2767. ALsource *Source{LookupSource(context, source)};
  2768. if(!Source)
  2769. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2770. if(!values)
  2771. throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
  2772. const ALuint count{Int64ValsByProp(param)};
  2773. GetProperty(Source, context, static_cast<SourceProp>(param), al::span{values, count});
  2774. }
  2775. catch(al::context_error& e) {
  2776. context->setError(e.errorCode(), "%s", e.what());
  2777. }
  2778. AL_API DECL_FUNC1(void, alSourcePlay, ALuint,source)
  2779. FORCE_ALIGN void AL_APIENTRY alSourcePlayDirect(ALCcontext *context, ALuint source) noexcept
  2780. try {
  2781. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2782. ALsource *Source{LookupSource(context, source)};
  2783. if(!Source)
  2784. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2785. StartSources(context, {&Source, 1});
  2786. }
  2787. catch(al::context_error& e) {
  2788. context->setError(e.errorCode(), "%s", e.what());
  2789. }
  2790. FORCE_ALIGN DECL_FUNCEXT2(void, alSourcePlayAtTime,SOFT, ALuint,source, ALint64SOFT,start_time)
  2791. FORCE_ALIGN void AL_APIENTRY alSourcePlayAtTimeDirectSOFT(ALCcontext *context, ALuint source,
  2792. ALint64SOFT start_time) noexcept
  2793. try {
  2794. if(start_time < 0)
  2795. throw al::context_error{AL_INVALID_VALUE, "Invalid time point %" PRId64, start_time};
  2796. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2797. ALsource *Source{LookupSource(context, source)};
  2798. if(!Source)
  2799. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", source};
  2800. StartSources(context, {&Source, 1}, nanoseconds{start_time});
  2801. }
  2802. catch(al::context_error& e) {
  2803. context->setError(e.errorCode(), "%s", e.what());
  2804. }
  2805. AL_API DECL_FUNC2(void, alSourcePlayv, ALsizei,n, const ALuint*,sources)
  2806. FORCE_ALIGN void AL_APIENTRY alSourcePlayvDirect(ALCcontext *context, ALsizei n,
  2807. const ALuint *sources) noexcept
  2808. try {
  2809. if(n < 0)
  2810. throw al::context_error{AL_INVALID_VALUE, "Playing %d sources", n};
  2811. if(n <= 0) UNLIKELY return;
  2812. al::span sids{sources, static_cast<ALuint>(n)};
  2813. source_store_variant source_store;
  2814. const auto srchandles = [&source_store](size_t count) -> al::span<ALsource*>
  2815. {
  2816. if(count > std::tuple_size_v<source_store_array>)
  2817. return al::span{source_store.emplace<source_store_vector>(count)};
  2818. return al::span{source_store.emplace<source_store_array>()}.first(count);
  2819. }(sids.size());
  2820. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2821. auto lookup_src = [context](const ALuint sid) -> ALsource*
  2822. {
  2823. if(ALsource *src{LookupSource(context, sid)})
  2824. return src;
  2825. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", sid};
  2826. };
  2827. std::transform(sids.cbegin(), sids.cend(), srchandles.begin(), lookup_src);
  2828. StartSources(context, srchandles);
  2829. }
  2830. catch(al::context_error& e) {
  2831. context->setError(e.errorCode(), "%s", e.what());
  2832. }
  2833. FORCE_ALIGN DECL_FUNCEXT3(void, alSourcePlayAtTimev,SOFT, ALsizei,n, const ALuint*,sources, ALint64SOFT,start_time)
  2834. FORCE_ALIGN void AL_APIENTRY alSourcePlayAtTimevDirectSOFT(ALCcontext *context, ALsizei n,
  2835. const ALuint *sources, ALint64SOFT start_time) noexcept
  2836. try {
  2837. if(n < 0)
  2838. throw al::context_error{AL_INVALID_VALUE, "Playing %d sources", n};
  2839. if(n <= 0) UNLIKELY return;
  2840. if(start_time < 0)
  2841. throw al::context_error{AL_INVALID_VALUE, "Invalid time point %" PRId64, start_time};
  2842. al::span sids{sources, static_cast<ALuint>(n)};
  2843. source_store_variant source_store;
  2844. const auto srchandles = [&source_store](size_t count) -> al::span<ALsource*>
  2845. {
  2846. if(count > std::tuple_size_v<source_store_array>)
  2847. return al::span{source_store.emplace<source_store_vector>(count)};
  2848. return al::span{source_store.emplace<source_store_array>()}.first(count);
  2849. }(sids.size());
  2850. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2851. auto lookup_src = [context](const ALuint sid) -> ALsource*
  2852. {
  2853. if(ALsource *src{LookupSource(context, sid)})
  2854. return src;
  2855. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", sid};
  2856. };
  2857. std::transform(sids.cbegin(), sids.cend(), srchandles.begin(), lookup_src);
  2858. StartSources(context, srchandles, nanoseconds{start_time});
  2859. }
  2860. catch(al::context_error& e) {
  2861. context->setError(e.errorCode(), "%s", e.what());
  2862. }
  2863. AL_API DECL_FUNC1(void, alSourcePause, ALuint,source)
  2864. FORCE_ALIGN void AL_APIENTRY alSourcePauseDirect(ALCcontext *context, ALuint source) noexcept
  2865. { alSourcePausevDirect(context, 1, &source); }
  2866. AL_API DECL_FUNC2(void, alSourcePausev, ALsizei,n, const ALuint*,sources)
  2867. FORCE_ALIGN void AL_APIENTRY alSourcePausevDirect(ALCcontext *context, ALsizei n,
  2868. const ALuint *sources) noexcept
  2869. try {
  2870. if(n < 0)
  2871. throw al::context_error{AL_INVALID_VALUE, "Pausing %d sources", n};
  2872. if(n <= 0) UNLIKELY return;
  2873. al::span sids{sources, static_cast<ALuint>(n)};
  2874. source_store_variant source_store;
  2875. const auto srchandles = [&source_store](size_t count) -> al::span<ALsource*>
  2876. {
  2877. if(count > std::tuple_size_v<source_store_array>)
  2878. return al::span{source_store.emplace<source_store_vector>(count)};
  2879. return al::span{source_store.emplace<source_store_array>()}.first(count);
  2880. }(sids.size());
  2881. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2882. auto lookup_src = [context](const ALuint sid) -> ALsource*
  2883. {
  2884. if(ALsource *src{LookupSource(context, sid)})
  2885. return src;
  2886. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", sid};
  2887. };
  2888. std::transform(sids.cbegin(), sids.cend(), srchandles.begin(), lookup_src);
  2889. /* Pausing has to be done in two steps. First, for each source that's
  2890. * detected to be playing, chamge the voice (asynchronously) to
  2891. * stopping/paused.
  2892. */
  2893. VoiceChange *tail{}, *cur{};
  2894. for(ALsource *source : srchandles)
  2895. {
  2896. Voice *voice{GetSourceVoice(source, context)};
  2897. if(GetSourceState(source, voice) == AL_PLAYING)
  2898. {
  2899. if(!cur)
  2900. cur = tail = GetVoiceChanger(context);
  2901. else
  2902. {
  2903. cur->mNext.store(GetVoiceChanger(context), std::memory_order_relaxed);
  2904. cur = cur->mNext.load(std::memory_order_relaxed);
  2905. }
  2906. cur->mVoice = voice;
  2907. cur->mSourceID = source->id;
  2908. cur->mState = VChangeState::Pause;
  2909. }
  2910. }
  2911. if(tail) LIKELY
  2912. {
  2913. SendVoiceChanges(context, tail);
  2914. /* Second, now that the voice changes have been sent, because it's
  2915. * possible that the voice stopped after it was detected playing and
  2916. * before the voice got paused, recheck that the source is still
  2917. * considered playing and set it to paused if so.
  2918. */
  2919. for(ALsource *source : srchandles)
  2920. {
  2921. Voice *voice{GetSourceVoice(source, context)};
  2922. if(GetSourceState(source, voice) == AL_PLAYING)
  2923. source->state = AL_PAUSED;
  2924. }
  2925. }
  2926. }
  2927. catch(al::context_error& e) {
  2928. context->setError(e.errorCode(), "%s", e.what());
  2929. }
  2930. AL_API DECL_FUNC1(void, alSourceStop, ALuint,source)
  2931. FORCE_ALIGN void AL_APIENTRY alSourceStopDirect(ALCcontext *context, ALuint source) noexcept
  2932. { alSourceStopvDirect(context, 1, &source); }
  2933. AL_API DECL_FUNC2(void, alSourceStopv, ALsizei,n, const ALuint*,sources)
  2934. FORCE_ALIGN void AL_APIENTRY alSourceStopvDirect(ALCcontext *context, ALsizei n,
  2935. const ALuint *sources) noexcept
  2936. try {
  2937. if(n < 0)
  2938. throw al::context_error{AL_INVALID_VALUE, "Stopping %d sources", n};
  2939. if(n <= 0) UNLIKELY return;
  2940. al::span sids{sources, static_cast<ALuint>(n)};
  2941. source_store_variant source_store;
  2942. const auto srchandles = [&source_store](size_t count) -> al::span<ALsource*>
  2943. {
  2944. if(count > std::tuple_size_v<source_store_array>)
  2945. return al::span{source_store.emplace<source_store_vector>(count)};
  2946. return al::span{source_store.emplace<source_store_array>()}.first(count);
  2947. }(sids.size());
  2948. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  2949. auto lookup_src = [context](const ALuint sid) -> ALsource*
  2950. {
  2951. if(ALsource *src{LookupSource(context, sid)})
  2952. return src;
  2953. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", sid};
  2954. };
  2955. std::transform(sids.cbegin(), sids.cend(), srchandles.begin(), lookup_src);
  2956. VoiceChange *tail{}, *cur{};
  2957. for(ALsource *source : srchandles)
  2958. {
  2959. if(Voice *voice{GetSourceVoice(source, context)})
  2960. {
  2961. if(!cur)
  2962. cur = tail = GetVoiceChanger(context);
  2963. else
  2964. {
  2965. cur->mNext.store(GetVoiceChanger(context), std::memory_order_relaxed);
  2966. cur = cur->mNext.load(std::memory_order_relaxed);
  2967. }
  2968. voice->mPendingChange.store(true, std::memory_order_relaxed);
  2969. cur->mVoice = voice;
  2970. cur->mSourceID = source->id;
  2971. cur->mState = VChangeState::Stop;
  2972. source->state = AL_STOPPED;
  2973. }
  2974. source->Offset = 0.0;
  2975. source->OffsetType = AL_NONE;
  2976. source->VoiceIdx = InvalidVoiceIndex;
  2977. }
  2978. if(tail) LIKELY
  2979. SendVoiceChanges(context, tail);
  2980. }
  2981. catch(al::context_error& e) {
  2982. context->setError(e.errorCode(), "%s", e.what());
  2983. }
  2984. AL_API DECL_FUNC1(void, alSourceRewind, ALuint,source)
  2985. FORCE_ALIGN void AL_APIENTRY alSourceRewindDirect(ALCcontext *context, ALuint source) noexcept
  2986. { alSourceRewindvDirect(context, 1, &source); }
  2987. AL_API DECL_FUNC2(void, alSourceRewindv, ALsizei,n, const ALuint*,sources)
  2988. FORCE_ALIGN void AL_APIENTRY alSourceRewindvDirect(ALCcontext *context, ALsizei n,
  2989. const ALuint *sources) noexcept
  2990. try {
  2991. if(n < 0)
  2992. throw al::context_error{AL_INVALID_VALUE, "Rewinding %d sources", n};
  2993. if(n <= 0) UNLIKELY return;
  2994. al::span sids{sources, static_cast<ALuint>(n)};
  2995. source_store_variant source_store;
  2996. const auto srchandles = [&source_store](size_t count) -> al::span<ALsource*>
  2997. {
  2998. if(count > std::tuple_size_v<source_store_array>)
  2999. return al::span{source_store.emplace<source_store_vector>(count)};
  3000. return al::span{source_store.emplace<source_store_array>()}.first(count);
  3001. }(sids.size());
  3002. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  3003. auto lookup_src = [context](const ALuint sid) -> ALsource*
  3004. {
  3005. if(ALsource *src{LookupSource(context, sid)})
  3006. return src;
  3007. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", sid};
  3008. };
  3009. std::transform(sids.cbegin(), sids.cend(), srchandles.begin(), lookup_src);
  3010. VoiceChange *tail{}, *cur{};
  3011. for(ALsource *source : srchandles)
  3012. {
  3013. Voice *voice{GetSourceVoice(source, context)};
  3014. if(source->state != AL_INITIAL)
  3015. {
  3016. if(!cur)
  3017. cur = tail = GetVoiceChanger(context);
  3018. else
  3019. {
  3020. cur->mNext.store(GetVoiceChanger(context), std::memory_order_relaxed);
  3021. cur = cur->mNext.load(std::memory_order_relaxed);
  3022. }
  3023. if(voice)
  3024. voice->mPendingChange.store(true, std::memory_order_relaxed);
  3025. cur->mVoice = voice;
  3026. cur->mSourceID = source->id;
  3027. cur->mState = VChangeState::Reset;
  3028. source->state = AL_INITIAL;
  3029. }
  3030. source->Offset = 0.0;
  3031. source->OffsetType = AL_NONE;
  3032. source->VoiceIdx = InvalidVoiceIndex;
  3033. }
  3034. if(tail) LIKELY
  3035. SendVoiceChanges(context, tail);
  3036. }
  3037. catch(al::context_error& e) {
  3038. context->setError(e.errorCode(), "%s", e.what());
  3039. }
  3040. AL_API DECL_FUNC3(void, alSourceQueueBuffers, ALuint,source, ALsizei,nb, const ALuint*,buffers)
  3041. FORCE_ALIGN void AL_APIENTRY alSourceQueueBuffersDirect(ALCcontext *context, ALuint src,
  3042. ALsizei nb, const ALuint *buffers) noexcept
  3043. try {
  3044. if(nb < 0)
  3045. throw al::context_error{AL_INVALID_VALUE, "Queueing %d buffers", nb};
  3046. if(nb <= 0) UNLIKELY return;
  3047. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  3048. ALsource *source{LookupSource(context,src)};
  3049. if(!source)
  3050. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", src};
  3051. /* Can't queue on a Static Source */
  3052. if(source->SourceType == AL_STATIC)
  3053. throw al::context_error{AL_INVALID_OPERATION, "Queueing onto static source %u", src};
  3054. /* Check for a valid Buffer, for its frequency and format */
  3055. ALCdevice *device{context->mALDevice.get()};
  3056. ALbuffer *BufferFmt{nullptr};
  3057. for(auto &item : source->mQueue)
  3058. {
  3059. BufferFmt = item.mBuffer;
  3060. if(BufferFmt) break;
  3061. }
  3062. std::unique_lock<std::mutex> buflock{device->BufferLock};
  3063. const auto bids = al::span{buffers, static_cast<ALuint>(nb)};
  3064. const size_t NewListStart{source->mQueue.size()};
  3065. try {
  3066. ALbufferQueueItem *BufferList{nullptr};
  3067. std::for_each(bids.cbegin(), bids.cend(),
  3068. [source,device,&BufferFmt,&BufferList](const ALuint bid)
  3069. {
  3070. ALbuffer *buffer{bid ? LookupBuffer(device, bid) : nullptr};
  3071. if(bid && !buffer)
  3072. throw al::context_error{AL_INVALID_NAME, "Queueing invalid buffer ID %u", bid};
  3073. if(buffer)
  3074. {
  3075. if(buffer->mSampleRate < 1)
  3076. throw al::context_error{AL_INVALID_OPERATION,
  3077. "Queueing buffer %u with no format", buffer->id};
  3078. if(buffer->mCallback)
  3079. throw al::context_error{AL_INVALID_OPERATION, "Queueing callback buffer %u",
  3080. buffer->id};
  3081. if(buffer->MappedAccess != 0 && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT))
  3082. throw al::context_error{AL_INVALID_OPERATION,
  3083. "Queueing non-persistently mapped buffer %u", buffer->id};
  3084. }
  3085. source->mQueue.emplace_back();
  3086. if(!BufferList)
  3087. BufferList = &source->mQueue.back();
  3088. else
  3089. {
  3090. auto &item = source->mQueue.back();
  3091. BufferList->mNext.store(&item, std::memory_order_relaxed);
  3092. BufferList = &item;
  3093. }
  3094. if(!buffer) return;
  3095. BufferList->mBlockAlign = buffer->mBlockAlign;
  3096. BufferList->mSampleLen = buffer->mSampleLen;
  3097. BufferList->mLoopEnd = buffer->mSampleLen;
  3098. BufferList->mSamples = buffer->mData;
  3099. BufferList->mBuffer = buffer;
  3100. IncrementRef(buffer->ref);
  3101. bool fmt_mismatch{false};
  3102. if(BufferFmt == nullptr)
  3103. BufferFmt = buffer;
  3104. else
  3105. {
  3106. fmt_mismatch |= BufferFmt->mSampleRate != buffer->mSampleRate;
  3107. fmt_mismatch |= BufferFmt->mChannels != buffer->mChannels;
  3108. fmt_mismatch |= BufferFmt->mType != buffer->mType;
  3109. if(BufferFmt->isBFormat())
  3110. {
  3111. fmt_mismatch |= BufferFmt->mAmbiLayout != buffer->mAmbiLayout;
  3112. fmt_mismatch |= BufferFmt->mAmbiScaling != buffer->mAmbiScaling;
  3113. }
  3114. fmt_mismatch |= BufferFmt->mAmbiOrder != buffer->mAmbiOrder;
  3115. }
  3116. if(fmt_mismatch)
  3117. throw al::context_error{AL_INVALID_OPERATION,
  3118. "Queueing buffer with mismatched format\n"
  3119. " Expected: %uhz, %s, %s ; Got: %uhz, %s, %s\n", BufferFmt->mSampleRate,
  3120. NameFromFormat(BufferFmt->mType), NameFromFormat(BufferFmt->mChannels),
  3121. buffer->mSampleRate, NameFromFormat(buffer->mType),
  3122. NameFromFormat(buffer->mChannels)};
  3123. });
  3124. }
  3125. catch(...) {
  3126. /* A buffer failed (invalid ID or format), or there was some other
  3127. * unexpected error, so unlock and release each buffer we had.
  3128. */
  3129. auto iter = source->mQueue.begin() + ptrdiff_t(NewListStart);
  3130. for(;iter != source->mQueue.end();++iter)
  3131. {
  3132. if(ALbuffer *buf{iter->mBuffer})
  3133. DecrementRef(buf->ref);
  3134. }
  3135. source->mQueue.resize(NewListStart);
  3136. throw;
  3137. }
  3138. /* All buffers good. */
  3139. buflock.unlock();
  3140. /* Source is now streaming */
  3141. source->SourceType = AL_STREAMING;
  3142. if(NewListStart != 0)
  3143. {
  3144. auto iter = source->mQueue.begin() + ptrdiff_t(NewListStart);
  3145. (iter-1)->mNext.store(al::to_address(iter), std::memory_order_release);
  3146. }
  3147. }
  3148. catch(al::context_error& e) {
  3149. context->setError(e.errorCode(), "%s", e.what());
  3150. }
  3151. AL_API DECL_FUNC3(void, alSourceUnqueueBuffers, ALuint,source, ALsizei,nb, ALuint*,buffers)
  3152. FORCE_ALIGN void AL_APIENTRY alSourceUnqueueBuffersDirect(ALCcontext *context, ALuint src,
  3153. ALsizei nb, ALuint *buffers) noexcept
  3154. try {
  3155. if(nb < 0)
  3156. throw al::context_error{AL_INVALID_VALUE, "Unqueueing %d buffers", nb};
  3157. if(nb <= 0) UNLIKELY return;
  3158. std::lock_guard<std::mutex> sourcelock{context->mSourceLock};
  3159. ALsource *source{LookupSource(context,src)};
  3160. if(!source)
  3161. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", src};
  3162. if(source->SourceType != AL_STREAMING)
  3163. throw al::context_error{AL_INVALID_VALUE, "Unqueueing from a non-streaming source %u",src};
  3164. if(source->Looping)
  3165. throw al::context_error{AL_INVALID_VALUE, "Unqueueing from looping source %u", src};
  3166. /* Make sure enough buffers have been processed to unqueue. */
  3167. const al::span bids{buffers, static_cast<ALuint>(nb)};
  3168. size_t processed{0};
  3169. if(source->state != AL_INITIAL) LIKELY
  3170. {
  3171. VoiceBufferItem *Current{nullptr};
  3172. if(Voice *voice{GetSourceVoice(source, context)})
  3173. Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
  3174. for(auto &item : source->mQueue)
  3175. {
  3176. if(&item == Current)
  3177. break;
  3178. ++processed;
  3179. }
  3180. }
  3181. if(processed < bids.size())
  3182. throw al::context_error{AL_INVALID_VALUE, "Unqueueing %d buffer%s (only %zu processed)",
  3183. nb, (nb==1)?"":"s", processed};
  3184. std::generate(bids.begin(), bids.end(), [source]() noexcept -> ALuint
  3185. {
  3186. auto &head = source->mQueue.front();
  3187. ALuint bid{0};
  3188. if(ALbuffer *buffer{head.mBuffer})
  3189. {
  3190. bid = buffer->id;
  3191. DecrementRef(buffer->ref);
  3192. }
  3193. source->mQueue.pop_front();
  3194. return bid;
  3195. });
  3196. }
  3197. catch(al::context_error& e) {
  3198. context->setError(e.errorCode(), "%s", e.what());
  3199. }
  3200. AL_API void AL_APIENTRY alSourceQueueBufferLayersSOFT(ALuint, ALsizei, const ALuint*) noexcept
  3201. {
  3202. ContextRef context{GetContextRef()};
  3203. if(!context) UNLIKELY return;
  3204. context->setError(AL_INVALID_OPERATION, "alSourceQueueBufferLayersSOFT not supported");
  3205. }
  3206. ALsource::ALsource() noexcept
  3207. {
  3208. Direct.Gain = 1.0f;
  3209. Direct.GainHF = 1.0f;
  3210. Direct.HFReference = LowPassFreqRef;
  3211. Direct.GainLF = 1.0f;
  3212. Direct.LFReference = HighPassFreqRef;
  3213. for(auto &send : Send)
  3214. {
  3215. send.Slot = nullptr;
  3216. send.Gain = 1.0f;
  3217. send.GainHF = 1.0f;
  3218. send.HFReference = LowPassFreqRef;
  3219. send.GainLF = 1.0f;
  3220. send.LFReference = HighPassFreqRef;
  3221. }
  3222. }
  3223. ALsource::~ALsource()
  3224. {
  3225. for(auto &item : mQueue)
  3226. {
  3227. if(ALbuffer *buffer{item.mBuffer})
  3228. DecrementRef(buffer->ref);
  3229. }
  3230. auto clear_send = [](ALsource::SendData &send) -> void
  3231. { if(send.Slot) DecrementRef(send.Slot->ref); };
  3232. std::for_each(Send.begin(), Send.end(), clear_send);
  3233. }
  3234. void UpdateAllSourceProps(ALCcontext *context)
  3235. {
  3236. std::lock_guard<std::mutex> srclock{context->mSourceLock};
  3237. auto voicelist = context->getVoicesSpan();
  3238. ALuint vidx{0u};
  3239. for(Voice *voice : voicelist)
  3240. {
  3241. ALuint sid{voice->mSourceID.load(std::memory_order_acquire)};
  3242. ALsource *source{sid ? LookupSource(context, sid) : nullptr};
  3243. if(source && source->VoiceIdx == vidx)
  3244. {
  3245. if(std::exchange(source->mPropsDirty, false))
  3246. UpdateSourceProps(source, voice, context);
  3247. }
  3248. ++vidx;
  3249. }
  3250. }
  3251. void ALsource::SetName(ALCcontext *context, ALuint id, std::string_view name)
  3252. {
  3253. std::lock_guard<std::mutex> srclock{context->mSourceLock};
  3254. auto source = LookupSource(context, id);
  3255. if(!source)
  3256. throw al::context_error{AL_INVALID_NAME, "Invalid source ID %u", id};
  3257. context->mSourceNames.insert_or_assign(id, name);
  3258. }
  3259. SourceSubList::~SourceSubList()
  3260. {
  3261. if(!Sources)
  3262. return;
  3263. uint64_t usemask{~FreeMask};
  3264. while(usemask)
  3265. {
  3266. const int idx{al::countr_zero(usemask)};
  3267. usemask &= ~(1_u64 << idx);
  3268. std::destroy_at(al::to_address(Sources->begin() + idx));
  3269. }
  3270. FreeMask = ~usemask;
  3271. SubListAllocator{}.deallocate(Sources, 1);
  3272. Sources = nullptr;
  3273. }
  3274. #ifdef ALSOFT_EAX
  3275. void ALsource::eaxInitialize(ALCcontext *context) noexcept
  3276. {
  3277. assert(context != nullptr);
  3278. mEaxAlContext = context;
  3279. mEaxPrimaryFxSlotId = context->eaxGetPrimaryFxSlotIndex();
  3280. eax_set_defaults();
  3281. eax1_translate(mEax1.i, mEax);
  3282. mEaxVersion = 1;
  3283. mEaxChanged = true;
  3284. }
  3285. void ALsource::eaxDispatch(const EaxCall& call)
  3286. {
  3287. call.is_get() ? eax_get(call) : eax_set(call);
  3288. }
  3289. ALsource* ALsource::EaxLookupSource(ALCcontext& al_context, ALuint source_id) noexcept
  3290. {
  3291. return LookupSource(&al_context, source_id);
  3292. }
  3293. [[noreturn]] void ALsource::eax_fail(const char* message)
  3294. {
  3295. throw Exception{message};
  3296. }
  3297. [[noreturn]] void ALsource::eax_fail_unknown_property_id()
  3298. {
  3299. eax_fail("Unknown property id.");
  3300. }
  3301. [[noreturn]] void ALsource::eax_fail_unknown_version()
  3302. {
  3303. eax_fail("Unknown version.");
  3304. }
  3305. [[noreturn]] void ALsource::eax_fail_unknown_active_fx_slot_id()
  3306. {
  3307. eax_fail("Unknown active FX slot ID.");
  3308. }
  3309. [[noreturn]] void ALsource::eax_fail_unknown_receiving_fx_slot_id()
  3310. {
  3311. eax_fail("Unknown receiving FX slot ID.");
  3312. }
  3313. void ALsource::eax_set_sends_defaults(EaxSends& sends, const EaxFxSlotIds& ids) noexcept
  3314. {
  3315. for(size_t i{0};i < EAX_MAX_FXSLOTS;++i)
  3316. {
  3317. auto& send = sends[i];
  3318. send.guidReceivingFXSlotID = *(ids[i]);
  3319. send.lSend = EAXSOURCE_DEFAULTSEND;
  3320. send.lSendHF = EAXSOURCE_DEFAULTSENDHF;
  3321. send.lOcclusion = EAXSOURCE_DEFAULTOCCLUSION;
  3322. send.flOcclusionLFRatio = EAXSOURCE_DEFAULTOCCLUSIONLFRATIO;
  3323. send.flOcclusionRoomRatio = EAXSOURCE_DEFAULTOCCLUSIONROOMRATIO;
  3324. send.flOcclusionDirectRatio = EAXSOURCE_DEFAULTOCCLUSIONDIRECTRATIO;
  3325. send.lExclusion = EAXSOURCE_DEFAULTEXCLUSION;
  3326. send.flExclusionLFRatio = EAXSOURCE_DEFAULTEXCLUSIONLFRATIO;
  3327. }
  3328. }
  3329. void ALsource::eax1_set_defaults(Eax1Props& props) noexcept
  3330. {
  3331. props.fMix = EAX_REVERBMIX_USEDISTANCE;
  3332. }
  3333. void ALsource::eax1_set_defaults() noexcept
  3334. {
  3335. eax1_set_defaults(mEax1.i);
  3336. mEax1.d = mEax1.i;
  3337. }
  3338. void ALsource::eax2_set_defaults(Eax2Props& props) noexcept
  3339. {
  3340. props.lDirect = EAXSOURCE_DEFAULTDIRECT;
  3341. props.lDirectHF = EAXSOURCE_DEFAULTDIRECTHF;
  3342. props.lRoom = EAXSOURCE_DEFAULTROOM;
  3343. props.lRoomHF = EAXSOURCE_DEFAULTROOMHF;
  3344. props.flRoomRolloffFactor = EAXSOURCE_DEFAULTROOMROLLOFFFACTOR;
  3345. props.lObstruction = EAXSOURCE_DEFAULTOBSTRUCTION;
  3346. props.flObstructionLFRatio = EAXSOURCE_DEFAULTOBSTRUCTIONLFRATIO;
  3347. props.lOcclusion = EAXSOURCE_DEFAULTOCCLUSION;
  3348. props.flOcclusionLFRatio = EAXSOURCE_DEFAULTOCCLUSIONLFRATIO;
  3349. props.flOcclusionRoomRatio = EAXSOURCE_DEFAULTOCCLUSIONROOMRATIO;
  3350. props.lOutsideVolumeHF = EAXSOURCE_DEFAULTOUTSIDEVOLUMEHF;
  3351. props.flAirAbsorptionFactor = EAXSOURCE_DEFAULTAIRABSORPTIONFACTOR;
  3352. props.dwFlags = EAXSOURCE_DEFAULTFLAGS;
  3353. }
  3354. void ALsource::eax2_set_defaults() noexcept
  3355. {
  3356. eax2_set_defaults(mEax2.i);
  3357. mEax2.d = mEax2.i;
  3358. }
  3359. void ALsource::eax3_set_defaults(Eax3Props& props) noexcept
  3360. {
  3361. props.lDirect = EAXSOURCE_DEFAULTDIRECT;
  3362. props.lDirectHF = EAXSOURCE_DEFAULTDIRECTHF;
  3363. props.lRoom = EAXSOURCE_DEFAULTROOM;
  3364. props.lRoomHF = EAXSOURCE_DEFAULTROOMHF;
  3365. props.lObstruction = EAXSOURCE_DEFAULTOBSTRUCTION;
  3366. props.flObstructionLFRatio = EAXSOURCE_DEFAULTOBSTRUCTIONLFRATIO;
  3367. props.lOcclusion = EAXSOURCE_DEFAULTOCCLUSION;
  3368. props.flOcclusionLFRatio = EAXSOURCE_DEFAULTOCCLUSIONLFRATIO;
  3369. props.flOcclusionRoomRatio = EAXSOURCE_DEFAULTOCCLUSIONROOMRATIO;
  3370. props.flOcclusionDirectRatio = EAXSOURCE_DEFAULTOCCLUSIONDIRECTRATIO;
  3371. props.lExclusion = EAXSOURCE_DEFAULTEXCLUSION;
  3372. props.flExclusionLFRatio = EAXSOURCE_DEFAULTEXCLUSIONLFRATIO;
  3373. props.lOutsideVolumeHF = EAXSOURCE_DEFAULTOUTSIDEVOLUMEHF;
  3374. props.flDopplerFactor = EAXSOURCE_DEFAULTDOPPLERFACTOR;
  3375. props.flRolloffFactor = EAXSOURCE_DEFAULTROLLOFFFACTOR;
  3376. props.flRoomRolloffFactor = EAXSOURCE_DEFAULTROOMROLLOFFFACTOR;
  3377. props.flAirAbsorptionFactor = EAXSOURCE_DEFAULTAIRABSORPTIONFACTOR;
  3378. props.ulFlags = EAXSOURCE_DEFAULTFLAGS;
  3379. }
  3380. void ALsource::eax3_set_defaults() noexcept
  3381. {
  3382. eax3_set_defaults(mEax3.i);
  3383. mEax3.d = mEax3.i;
  3384. }
  3385. void ALsource::eax4_set_sends_defaults(EaxSends& sends) noexcept
  3386. {
  3387. eax_set_sends_defaults(sends, eax4_fx_slot_ids);
  3388. }
  3389. void ALsource::eax4_set_active_fx_slots_defaults(EAX40ACTIVEFXSLOTS& slots) noexcept
  3390. {
  3391. slots = EAX40SOURCE_DEFAULTACTIVEFXSLOTID;
  3392. }
  3393. void ALsource::eax4_set_defaults() noexcept
  3394. {
  3395. eax3_set_defaults(mEax4.i.source);
  3396. eax4_set_sends_defaults(mEax4.i.sends);
  3397. eax4_set_active_fx_slots_defaults(mEax4.i.active_fx_slots);
  3398. mEax4.d = mEax4.i;
  3399. }
  3400. void ALsource::eax5_set_source_defaults(EAX50SOURCEPROPERTIES& props) noexcept
  3401. {
  3402. eax3_set_defaults(static_cast<Eax3Props&>(props));
  3403. props.flMacroFXFactor = EAXSOURCE_DEFAULTMACROFXFACTOR;
  3404. }
  3405. void ALsource::eax5_set_sends_defaults(EaxSends& sends) noexcept
  3406. {
  3407. eax_set_sends_defaults(sends, eax5_fx_slot_ids);
  3408. }
  3409. void ALsource::eax5_set_active_fx_slots_defaults(EAX50ACTIVEFXSLOTS& slots) noexcept
  3410. {
  3411. slots = EAX50SOURCE_3DDEFAULTACTIVEFXSLOTID;
  3412. }
  3413. void ALsource::eax5_set_speaker_levels_defaults(EaxSpeakerLevels& speaker_levels) noexcept
  3414. {
  3415. for(size_t i{0};i < eax_max_speakers;++i)
  3416. {
  3417. auto& speaker_level = speaker_levels[i];
  3418. speaker_level.lSpeakerID = static_cast<long>(EAXSPEAKER_FRONT_LEFT + i);
  3419. speaker_level.lLevel = EAXSOURCE_DEFAULTSPEAKERLEVEL;
  3420. }
  3421. }
  3422. void ALsource::eax5_set_defaults(Eax5Props& props) noexcept
  3423. {
  3424. eax5_set_source_defaults(props.source);
  3425. eax5_set_sends_defaults(props.sends);
  3426. eax5_set_active_fx_slots_defaults(props.active_fx_slots);
  3427. eax5_set_speaker_levels_defaults(props.speaker_levels);
  3428. }
  3429. void ALsource::eax5_set_defaults() noexcept
  3430. {
  3431. eax5_set_defaults(mEax5.i);
  3432. mEax5.d = mEax5.i;
  3433. }
  3434. void ALsource::eax_set_defaults() noexcept
  3435. {
  3436. eax1_set_defaults();
  3437. eax2_set_defaults();
  3438. eax3_set_defaults();
  3439. eax4_set_defaults();
  3440. eax5_set_defaults();
  3441. }
  3442. void ALsource::eax1_translate(const Eax1Props& src, Eax5Props& dst) noexcept
  3443. {
  3444. eax5_set_defaults(dst);
  3445. if (src.fMix == EAX_REVERBMIX_USEDISTANCE)
  3446. {
  3447. dst.source.ulFlags |= EAXSOURCEFLAGS_ROOMAUTO;
  3448. dst.sends[0].lSend = 0;
  3449. }
  3450. else
  3451. {
  3452. dst.source.ulFlags &= ~EAXSOURCEFLAGS_ROOMAUTO;
  3453. dst.sends[0].lSend = std::clamp(static_cast<long>(gain_to_level_mb(src.fMix)),
  3454. EAXSOURCE_MINSEND, EAXSOURCE_MAXSEND);
  3455. }
  3456. }
  3457. void ALsource::eax2_translate(const Eax2Props& src, Eax5Props& dst) noexcept
  3458. {
  3459. // Source.
  3460. //
  3461. dst.source.lDirect = src.lDirect;
  3462. dst.source.lDirectHF = src.lDirectHF;
  3463. dst.source.lRoom = src.lRoom;
  3464. dst.source.lRoomHF = src.lRoomHF;
  3465. dst.source.lObstruction = src.lObstruction;
  3466. dst.source.flObstructionLFRatio = src.flObstructionLFRatio;
  3467. dst.source.lOcclusion = src.lOcclusion;
  3468. dst.source.flOcclusionLFRatio = src.flOcclusionLFRatio;
  3469. dst.source.flOcclusionRoomRatio = src.flOcclusionRoomRatio;
  3470. dst.source.flOcclusionDirectRatio = EAXSOURCE_DEFAULTOCCLUSIONDIRECTRATIO;
  3471. dst.source.lExclusion = EAXSOURCE_DEFAULTEXCLUSION;
  3472. dst.source.flExclusionLFRatio = EAXSOURCE_DEFAULTEXCLUSIONLFRATIO;
  3473. dst.source.lOutsideVolumeHF = src.lOutsideVolumeHF;
  3474. dst.source.flDopplerFactor = EAXSOURCE_DEFAULTDOPPLERFACTOR;
  3475. dst.source.flRolloffFactor = EAXSOURCE_DEFAULTROLLOFFFACTOR;
  3476. dst.source.flRoomRolloffFactor = src.flRoomRolloffFactor;
  3477. dst.source.flAirAbsorptionFactor = src.flAirAbsorptionFactor;
  3478. dst.source.ulFlags = src.dwFlags;
  3479. dst.source.flMacroFXFactor = EAXSOURCE_DEFAULTMACROFXFACTOR;
  3480. // Set everything else to defaults.
  3481. //
  3482. eax5_set_sends_defaults(dst.sends);
  3483. eax5_set_active_fx_slots_defaults(dst.active_fx_slots);
  3484. eax5_set_speaker_levels_defaults(dst.speaker_levels);
  3485. }
  3486. void ALsource::eax3_translate(const Eax3Props& src, Eax5Props& dst) noexcept
  3487. {
  3488. // Source.
  3489. //
  3490. static_cast<Eax3Props&>(dst.source) = src;
  3491. dst.source.flMacroFXFactor = EAXSOURCE_DEFAULTMACROFXFACTOR;
  3492. // Set everything else to defaults.
  3493. //
  3494. eax5_set_sends_defaults(dst.sends);
  3495. eax5_set_active_fx_slots_defaults(dst.active_fx_slots);
  3496. eax5_set_speaker_levels_defaults(dst.speaker_levels);
  3497. }
  3498. void ALsource::eax4_translate(const Eax4Props& src, Eax5Props& dst) noexcept
  3499. {
  3500. // Source.
  3501. //
  3502. static_cast<Eax3Props&>(dst.source) = src.source;
  3503. dst.source.flMacroFXFactor = EAXSOURCE_DEFAULTMACROFXFACTOR;
  3504. // Sends.
  3505. //
  3506. dst.sends = src.sends;
  3507. for(size_t i{0};i < EAX_MAX_FXSLOTS;++i)
  3508. dst.sends[i].guidReceivingFXSlotID = *(eax5_fx_slot_ids[i]);
  3509. // Active FX slots.
  3510. //
  3511. for(size_t i{0};i < EAX50_MAX_ACTIVE_FXSLOTS;++i)
  3512. {
  3513. auto& dst_id = dst.active_fx_slots.guidActiveFXSlots[i];
  3514. if(i < EAX40_MAX_ACTIVE_FXSLOTS)
  3515. {
  3516. const auto& src_id = src.active_fx_slots.guidActiveFXSlots[i];
  3517. if(src_id == EAX_NULL_GUID)
  3518. dst_id = EAX_NULL_GUID;
  3519. else if(src_id == EAX_PrimaryFXSlotID)
  3520. dst_id = EAX_PrimaryFXSlotID;
  3521. else if(src_id == EAXPROPERTYID_EAX40_FXSlot0)
  3522. dst_id = EAXPROPERTYID_EAX50_FXSlot0;
  3523. else if(src_id == EAXPROPERTYID_EAX40_FXSlot1)
  3524. dst_id = EAXPROPERTYID_EAX50_FXSlot1;
  3525. else if(src_id == EAXPROPERTYID_EAX40_FXSlot2)
  3526. dst_id = EAXPROPERTYID_EAX50_FXSlot2;
  3527. else if(src_id == EAXPROPERTYID_EAX40_FXSlot3)
  3528. dst_id = EAXPROPERTYID_EAX50_FXSlot3;
  3529. else
  3530. assert(false && "Unknown active FX slot ID.");
  3531. }
  3532. else
  3533. dst_id = EAX_NULL_GUID;
  3534. }
  3535. // Speaker levels.
  3536. //
  3537. eax5_set_speaker_levels_defaults(dst.speaker_levels);
  3538. }
  3539. float ALsource::eax_calculate_dst_occlusion_mb(
  3540. long src_occlusion_mb,
  3541. float path_ratio,
  3542. float lf_ratio) noexcept
  3543. {
  3544. const auto ratio_1 = path_ratio + lf_ratio - 1.0F;
  3545. const auto ratio_2 = path_ratio * lf_ratio;
  3546. const auto ratio = (ratio_2 > ratio_1) ? ratio_2 : ratio_1;
  3547. const auto dst_occlustion_mb = static_cast<float>(src_occlusion_mb) * ratio;
  3548. return dst_occlustion_mb;
  3549. }
  3550. EaxAlLowPassParam ALsource::eax_create_direct_filter_param() const noexcept
  3551. {
  3552. auto gain_mb =
  3553. static_cast<float>(mEax.source.lDirect) +
  3554. (static_cast<float>(mEax.source.lObstruction) * mEax.source.flObstructionLFRatio) +
  3555. eax_calculate_dst_occlusion_mb(
  3556. mEax.source.lOcclusion,
  3557. mEax.source.flOcclusionDirectRatio,
  3558. mEax.source.flOcclusionLFRatio);
  3559. const auto has_source_occlusion = (mEax.source.lOcclusion != 0);
  3560. auto gain_hf_mb =
  3561. static_cast<float>(mEax.source.lDirectHF) +
  3562. static_cast<float>(mEax.source.lObstruction);
  3563. for(size_t i{0};i < EAX_MAX_FXSLOTS;++i)
  3564. {
  3565. if(!mEaxActiveFxSlots[i])
  3566. continue;
  3567. if(has_source_occlusion)
  3568. {
  3569. const auto& fx_slot = mEaxAlContext->eaxGetFxSlot(i);
  3570. const auto& fx_slot_eax = fx_slot.eax_get_eax_fx_slot();
  3571. const auto is_environmental_fx = ((fx_slot_eax.ulFlags & EAXFXSLOTFLAGS_ENVIRONMENT) != 0);
  3572. const auto is_primary = (mEaxPrimaryFxSlotId.value_or(-1) == fx_slot.eax_get_index());
  3573. const auto is_listener_environment = (is_environmental_fx && is_primary);
  3574. if(is_listener_environment)
  3575. {
  3576. gain_mb += eax_calculate_dst_occlusion_mb(
  3577. mEax.source.lOcclusion,
  3578. mEax.source.flOcclusionDirectRatio,
  3579. mEax.source.flOcclusionLFRatio);
  3580. gain_hf_mb += static_cast<float>(mEax.source.lOcclusion) * mEax.source.flOcclusionDirectRatio;
  3581. }
  3582. }
  3583. const auto& send = mEax.sends[i];
  3584. if(send.lOcclusion != 0)
  3585. {
  3586. gain_mb += eax_calculate_dst_occlusion_mb(
  3587. send.lOcclusion,
  3588. send.flOcclusionDirectRatio,
  3589. send.flOcclusionLFRatio);
  3590. gain_hf_mb += static_cast<float>(send.lOcclusion) * send.flOcclusionDirectRatio;
  3591. }
  3592. }
  3593. return EaxAlLowPassParam{
  3594. level_mb_to_gain(gain_mb),
  3595. std::min(level_mb_to_gain(gain_hf_mb), 1.0f)};
  3596. }
  3597. EaxAlLowPassParam ALsource::eax_create_room_filter_param(
  3598. const ALeffectslot& fx_slot,
  3599. const EAXSOURCEALLSENDPROPERTIES& send) const noexcept
  3600. {
  3601. const auto& fx_slot_eax = fx_slot.eax_get_eax_fx_slot();
  3602. const auto is_environmental_fx = ((fx_slot_eax.ulFlags & EAXFXSLOTFLAGS_ENVIRONMENT) != 0);
  3603. const auto is_primary = (mEaxPrimaryFxSlotId.value_or(-1) == fx_slot.eax_get_index());
  3604. const auto is_listener_environment = (is_environmental_fx && is_primary);
  3605. const auto gain_mb =
  3606. (static_cast<float>(fx_slot_eax.lOcclusion) * fx_slot_eax.flOcclusionLFRatio) +
  3607. static_cast<float>((is_environmental_fx ? mEax.source.lRoom : 0) + send.lSend) +
  3608. (is_listener_environment ?
  3609. eax_calculate_dst_occlusion_mb(
  3610. mEax.source.lOcclusion,
  3611. mEax.source.flOcclusionRoomRatio,
  3612. mEax.source.flOcclusionLFRatio) :
  3613. 0.0f) +
  3614. eax_calculate_dst_occlusion_mb(
  3615. send.lOcclusion,
  3616. send.flOcclusionRoomRatio,
  3617. send.flOcclusionLFRatio) +
  3618. (is_listener_environment ?
  3619. (static_cast<float>(mEax.source.lExclusion) * mEax.source.flExclusionLFRatio) :
  3620. 0.0f) +
  3621. (static_cast<float>(send.lExclusion) * send.flExclusionLFRatio);
  3622. const auto gain_hf_mb =
  3623. static_cast<float>(fx_slot_eax.lOcclusion) +
  3624. static_cast<float>((is_environmental_fx ? mEax.source.lRoomHF : 0) + send.lSendHF) +
  3625. (is_listener_environment ?
  3626. ((static_cast<float>(mEax.source.lOcclusion) * mEax.source.flOcclusionRoomRatio)) :
  3627. 0.0f) +
  3628. (static_cast<float>(send.lOcclusion) * send.flOcclusionRoomRatio) +
  3629. (is_listener_environment ?
  3630. static_cast<float>(mEax.source.lExclusion + send.lExclusion) :
  3631. 0.0f);
  3632. return EaxAlLowPassParam{
  3633. level_mb_to_gain(gain_mb),
  3634. std::min(level_mb_to_gain(gain_hf_mb), 1.0f)};
  3635. }
  3636. void ALsource::eax_update_direct_filter()
  3637. {
  3638. const auto& direct_param = eax_create_direct_filter_param();
  3639. Direct.Gain = direct_param.gain;
  3640. Direct.GainHF = direct_param.gain_hf;
  3641. Direct.HFReference = LowPassFreqRef;
  3642. Direct.GainLF = 1.0f;
  3643. Direct.LFReference = HighPassFreqRef;
  3644. mPropsDirty = true;
  3645. }
  3646. void ALsource::eax_update_room_filters()
  3647. {
  3648. for(size_t i{0};i < EAX_MAX_FXSLOTS;++i)
  3649. {
  3650. if(!mEaxActiveFxSlots[i])
  3651. continue;
  3652. auto& fx_slot = mEaxAlContext->eaxGetFxSlot(i);
  3653. const auto& send = mEax.sends[i];
  3654. const auto& room_param = eax_create_room_filter_param(fx_slot, send);
  3655. eax_set_al_source_send(&fx_slot, i, room_param);
  3656. }
  3657. }
  3658. void ALsource::eax_set_efx_outer_gain_hf()
  3659. {
  3660. OuterGainHF = std::clamp(
  3661. level_mb_to_gain(static_cast<float>(mEax.source.lOutsideVolumeHF)),
  3662. AL_MIN_CONE_OUTER_GAINHF,
  3663. AL_MAX_CONE_OUTER_GAINHF);
  3664. }
  3665. void ALsource::eax_set_efx_doppler_factor()
  3666. {
  3667. DopplerFactor = mEax.source.flDopplerFactor;
  3668. }
  3669. void ALsource::eax_set_efx_rolloff_factor()
  3670. {
  3671. RolloffFactor2 = mEax.source.flRolloffFactor;
  3672. }
  3673. void ALsource::eax_set_efx_room_rolloff_factor()
  3674. {
  3675. RoomRolloffFactor = mEax.source.flRoomRolloffFactor;
  3676. }
  3677. void ALsource::eax_set_efx_air_absorption_factor()
  3678. {
  3679. AirAbsorptionFactor = mEax.source.flAirAbsorptionFactor;
  3680. }
  3681. void ALsource::eax_set_efx_dry_gain_hf_auto()
  3682. {
  3683. DryGainHFAuto = ((mEax.source.ulFlags & EAXSOURCEFLAGS_DIRECTHFAUTO) != 0);
  3684. }
  3685. void ALsource::eax_set_efx_wet_gain_auto()
  3686. {
  3687. WetGainAuto = ((mEax.source.ulFlags & EAXSOURCEFLAGS_ROOMAUTO) != 0);
  3688. }
  3689. void ALsource::eax_set_efx_wet_gain_hf_auto()
  3690. {
  3691. WetGainHFAuto = ((mEax.source.ulFlags & EAXSOURCEFLAGS_ROOMHFAUTO) != 0);
  3692. }
  3693. void ALsource::eax1_set(const EaxCall& call, Eax1Props& props)
  3694. {
  3695. switch (call.get_property_id()) {
  3696. case DSPROPERTY_EAXBUFFER_ALL:
  3697. eax_defer<Eax1SourceAllValidator>(call, props);
  3698. break;
  3699. case DSPROPERTY_EAXBUFFER_REVERBMIX:
  3700. eax_defer<Eax1SourceReverbMixValidator>(call, props.fMix);
  3701. break;
  3702. default:
  3703. eax_fail_unknown_property_id();
  3704. }
  3705. }
  3706. void ALsource::eax2_set(const EaxCall& call, Eax2Props& props)
  3707. {
  3708. switch (call.get_property_id()) {
  3709. case DSPROPERTY_EAX20BUFFER_NONE:
  3710. break;
  3711. case DSPROPERTY_EAX20BUFFER_ALLPARAMETERS:
  3712. eax_defer<Eax2SourceAllValidator>(call, props);
  3713. break;
  3714. case DSPROPERTY_EAX20BUFFER_DIRECT:
  3715. eax_defer<Eax2SourceDirectValidator>(call, props.lDirect);
  3716. break;
  3717. case DSPROPERTY_EAX20BUFFER_DIRECTHF:
  3718. eax_defer<Eax2SourceDirectHfValidator>(call, props.lDirectHF);
  3719. break;
  3720. case DSPROPERTY_EAX20BUFFER_ROOM:
  3721. eax_defer<Eax2SourceRoomValidator>(call, props.lRoom);
  3722. break;
  3723. case DSPROPERTY_EAX20BUFFER_ROOMHF:
  3724. eax_defer<Eax2SourceRoomHfValidator>(call, props.lRoomHF);
  3725. break;
  3726. case DSPROPERTY_EAX20BUFFER_ROOMROLLOFFFACTOR:
  3727. eax_defer<Eax2SourceRoomRolloffFactorValidator>(call, props.flRoomRolloffFactor);
  3728. break;
  3729. case DSPROPERTY_EAX20BUFFER_OBSTRUCTION:
  3730. eax_defer<Eax2SourceObstructionValidator>(call, props.lObstruction);
  3731. break;
  3732. case DSPROPERTY_EAX20BUFFER_OBSTRUCTIONLFRATIO:
  3733. eax_defer<Eax2SourceObstructionLfRatioValidator>(call, props.flObstructionLFRatio);
  3734. break;
  3735. case DSPROPERTY_EAX20BUFFER_OCCLUSION:
  3736. eax_defer<Eax2SourceOcclusionValidator>(call, props.lOcclusion);
  3737. break;
  3738. case DSPROPERTY_EAX20BUFFER_OCCLUSIONLFRATIO:
  3739. eax_defer<Eax2SourceOcclusionLfRatioValidator>(call, props.flOcclusionLFRatio);
  3740. break;
  3741. case DSPROPERTY_EAX20BUFFER_OCCLUSIONROOMRATIO:
  3742. eax_defer<Eax2SourceOcclusionRoomRatioValidator>(call, props.flOcclusionRoomRatio);
  3743. break;
  3744. case DSPROPERTY_EAX20BUFFER_OUTSIDEVOLUMEHF:
  3745. eax_defer<Eax2SourceOutsideVolumeHfValidator>(call, props.lOutsideVolumeHF);
  3746. break;
  3747. case DSPROPERTY_EAX20BUFFER_AIRABSORPTIONFACTOR:
  3748. eax_defer<Eax2SourceAirAbsorptionFactorValidator>(call, props.flAirAbsorptionFactor);
  3749. break;
  3750. case DSPROPERTY_EAX20BUFFER_FLAGS:
  3751. eax_defer<Eax2SourceFlagsValidator>(call, props.dwFlags);
  3752. break;
  3753. default:
  3754. eax_fail_unknown_property_id();
  3755. }
  3756. }
  3757. void ALsource::eax3_set(const EaxCall& call, Eax3Props& props)
  3758. {
  3759. switch (call.get_property_id()) {
  3760. case EAXSOURCE_NONE:
  3761. break;
  3762. case EAXSOURCE_ALLPARAMETERS:
  3763. eax_defer<Eax3SourceAllValidator>(call, props);
  3764. break;
  3765. case EAXSOURCE_OBSTRUCTIONPARAMETERS:
  3766. eax_defer_sub<Eax4ObstructionValidator, EAXOBSTRUCTIONPROPERTIES>(call, props.lObstruction);
  3767. break;
  3768. case EAXSOURCE_OCCLUSIONPARAMETERS:
  3769. eax_defer_sub<Eax4OcclusionValidator, EAXOCCLUSIONPROPERTIES>(call, props.lOcclusion);
  3770. break;
  3771. case EAXSOURCE_EXCLUSIONPARAMETERS:
  3772. eax_defer_sub<Eax4ExclusionValidator, EAXEXCLUSIONPROPERTIES>(call, props.lExclusion);
  3773. break;
  3774. case EAXSOURCE_DIRECT:
  3775. eax_defer<Eax2SourceDirectValidator>(call, props.lDirect);
  3776. break;
  3777. case EAXSOURCE_DIRECTHF:
  3778. eax_defer<Eax2SourceDirectHfValidator>(call, props.lDirectHF);
  3779. break;
  3780. case EAXSOURCE_ROOM:
  3781. eax_defer<Eax2SourceRoomValidator>(call, props.lRoom);
  3782. break;
  3783. case EAXSOURCE_ROOMHF:
  3784. eax_defer<Eax2SourceRoomHfValidator>(call, props.lRoomHF);
  3785. break;
  3786. case EAXSOURCE_OBSTRUCTION:
  3787. eax_defer<Eax2SourceObstructionValidator>(call, props.lObstruction);
  3788. break;
  3789. case EAXSOURCE_OBSTRUCTIONLFRATIO:
  3790. eax_defer<Eax2SourceObstructionLfRatioValidator>(call, props.flObstructionLFRatio);
  3791. break;
  3792. case EAXSOURCE_OCCLUSION:
  3793. eax_defer<Eax2SourceOcclusionValidator>(call, props.lOcclusion);
  3794. break;
  3795. case EAXSOURCE_OCCLUSIONLFRATIO:
  3796. eax_defer<Eax2SourceOcclusionLfRatioValidator>(call, props.flOcclusionLFRatio);
  3797. break;
  3798. case EAXSOURCE_OCCLUSIONROOMRATIO:
  3799. eax_defer<Eax2SourceOcclusionRoomRatioValidator>(call, props.flOcclusionRoomRatio);
  3800. break;
  3801. case EAXSOURCE_OCCLUSIONDIRECTRATIO:
  3802. eax_defer<Eax3SourceOcclusionDirectRatioValidator>(call, props.flOcclusionDirectRatio);
  3803. break;
  3804. case EAXSOURCE_EXCLUSION:
  3805. eax_defer<Eax3SourceExclusionValidator>(call, props.lExclusion);
  3806. break;
  3807. case EAXSOURCE_EXCLUSIONLFRATIO:
  3808. eax_defer<Eax3SourceExclusionLfRatioValidator>(call, props.flExclusionLFRatio);
  3809. break;
  3810. case EAXSOURCE_OUTSIDEVOLUMEHF:
  3811. eax_defer<Eax2SourceOutsideVolumeHfValidator>(call, props.lOutsideVolumeHF);
  3812. break;
  3813. case EAXSOURCE_DOPPLERFACTOR:
  3814. eax_defer<Eax3SourceDopplerFactorValidator>(call, props.flDopplerFactor);
  3815. break;
  3816. case EAXSOURCE_ROLLOFFFACTOR:
  3817. eax_defer<Eax3SourceRolloffFactorValidator>(call, props.flRolloffFactor);
  3818. break;
  3819. case EAXSOURCE_ROOMROLLOFFFACTOR:
  3820. eax_defer<Eax2SourceRoomRolloffFactorValidator>(call, props.flRoomRolloffFactor);
  3821. break;
  3822. case EAXSOURCE_AIRABSORPTIONFACTOR:
  3823. eax_defer<Eax2SourceAirAbsorptionFactorValidator>(call, props.flAirAbsorptionFactor);
  3824. break;
  3825. case EAXSOURCE_FLAGS:
  3826. eax_defer<Eax2SourceFlagsValidator>(call, props.ulFlags);
  3827. break;
  3828. default:
  3829. eax_fail_unknown_property_id();
  3830. }
  3831. }
  3832. void ALsource::eax4_set(const EaxCall& call, Eax4Props& props)
  3833. {
  3834. switch (call.get_property_id()) {
  3835. case EAXSOURCE_NONE:
  3836. case EAXSOURCE_ALLPARAMETERS:
  3837. case EAXSOURCE_OBSTRUCTIONPARAMETERS:
  3838. case EAXSOURCE_OCCLUSIONPARAMETERS:
  3839. case EAXSOURCE_EXCLUSIONPARAMETERS:
  3840. case EAXSOURCE_DIRECT:
  3841. case EAXSOURCE_DIRECTHF:
  3842. case EAXSOURCE_ROOM:
  3843. case EAXSOURCE_ROOMHF:
  3844. case EAXSOURCE_OBSTRUCTION:
  3845. case EAXSOURCE_OBSTRUCTIONLFRATIO:
  3846. case EAXSOURCE_OCCLUSION:
  3847. case EAXSOURCE_OCCLUSIONLFRATIO:
  3848. case EAXSOURCE_OCCLUSIONROOMRATIO:
  3849. case EAXSOURCE_OCCLUSIONDIRECTRATIO:
  3850. case EAXSOURCE_EXCLUSION:
  3851. case EAXSOURCE_EXCLUSIONLFRATIO:
  3852. case EAXSOURCE_OUTSIDEVOLUMEHF:
  3853. case EAXSOURCE_DOPPLERFACTOR:
  3854. case EAXSOURCE_ROLLOFFFACTOR:
  3855. case EAXSOURCE_ROOMROLLOFFFACTOR:
  3856. case EAXSOURCE_AIRABSORPTIONFACTOR:
  3857. case EAXSOURCE_FLAGS:
  3858. eax3_set(call, props.source);
  3859. break;
  3860. case EAXSOURCE_SENDPARAMETERS:
  3861. eax4_defer_sends<Eax4SendValidator, EAXSOURCESENDPROPERTIES>(call, props.sends);
  3862. break;
  3863. case EAXSOURCE_ALLSENDPARAMETERS:
  3864. eax4_defer_sends<Eax4AllSendValidator, EAXSOURCEALLSENDPROPERTIES>(call, props.sends);
  3865. break;
  3866. case EAXSOURCE_OCCLUSIONSENDPARAMETERS:
  3867. eax4_defer_sends<Eax4OcclusionSendValidator, EAXSOURCEOCCLUSIONSENDPROPERTIES>(call, props.sends);
  3868. break;
  3869. case EAXSOURCE_EXCLUSIONSENDPARAMETERS:
  3870. eax4_defer_sends<Eax4ExclusionSendValidator, EAXSOURCEEXCLUSIONSENDPROPERTIES>(call, props.sends);
  3871. break;
  3872. case EAXSOURCE_ACTIVEFXSLOTID:
  3873. eax4_defer_active_fx_slot_id(call, al::span{props.active_fx_slots.guidActiveFXSlots});
  3874. break;
  3875. default:
  3876. eax_fail_unknown_property_id();
  3877. }
  3878. }
  3879. void ALsource::eax5_defer_all_2d(const EaxCall& call, EAX50SOURCEPROPERTIES& props)
  3880. {
  3881. const auto& src_props = call.get_value<Exception, const EAXSOURCE2DPROPERTIES>();
  3882. Eax5SourceAll2dValidator{}(src_props);
  3883. props.lDirect = src_props.lDirect;
  3884. props.lDirectHF = src_props.lDirectHF;
  3885. props.lRoom = src_props.lRoom;
  3886. props.lRoomHF = src_props.lRoomHF;
  3887. props.ulFlags = src_props.ulFlags;
  3888. }
  3889. void ALsource::eax5_defer_speaker_levels(const EaxCall& call, EaxSpeakerLevels& props)
  3890. {
  3891. const auto values = call.get_values<const EAXSPEAKERLEVELPROPERTIES>(eax_max_speakers);
  3892. std::for_each(values.cbegin(), values.cend(), Eax5SpeakerAllValidator{});
  3893. for (const auto& value : values) {
  3894. const auto index = static_cast<size_t>(value.lSpeakerID - EAXSPEAKER_FRONT_LEFT);
  3895. props[index].lLevel = value.lLevel;
  3896. }
  3897. }
  3898. void ALsource::eax5_set(const EaxCall& call, Eax5Props& props)
  3899. {
  3900. switch (call.get_property_id()) {
  3901. case EAXSOURCE_NONE:
  3902. break;
  3903. case EAXSOURCE_ALLPARAMETERS:
  3904. eax_defer<Eax5SourceAllValidator>(call, props.source);
  3905. break;
  3906. case EAXSOURCE_OBSTRUCTIONPARAMETERS:
  3907. case EAXSOURCE_OCCLUSIONPARAMETERS:
  3908. case EAXSOURCE_EXCLUSIONPARAMETERS:
  3909. case EAXSOURCE_DIRECT:
  3910. case EAXSOURCE_DIRECTHF:
  3911. case EAXSOURCE_ROOM:
  3912. case EAXSOURCE_ROOMHF:
  3913. case EAXSOURCE_OBSTRUCTION:
  3914. case EAXSOURCE_OBSTRUCTIONLFRATIO:
  3915. case EAXSOURCE_OCCLUSION:
  3916. case EAXSOURCE_OCCLUSIONLFRATIO:
  3917. case EAXSOURCE_OCCLUSIONROOMRATIO:
  3918. case EAXSOURCE_OCCLUSIONDIRECTRATIO:
  3919. case EAXSOURCE_EXCLUSION:
  3920. case EAXSOURCE_EXCLUSIONLFRATIO:
  3921. case EAXSOURCE_OUTSIDEVOLUMEHF:
  3922. case EAXSOURCE_DOPPLERFACTOR:
  3923. case EAXSOURCE_ROLLOFFFACTOR:
  3924. case EAXSOURCE_ROOMROLLOFFFACTOR:
  3925. case EAXSOURCE_AIRABSORPTIONFACTOR:
  3926. eax3_set(call, props.source);
  3927. break;
  3928. case EAXSOURCE_FLAGS:
  3929. eax_defer<Eax5SourceFlagsValidator>(call, props.source.ulFlags);
  3930. break;
  3931. case EAXSOURCE_SENDPARAMETERS:
  3932. eax5_defer_sends<Eax5SendValidator, EAXSOURCESENDPROPERTIES>(call, props.sends);
  3933. break;
  3934. case EAXSOURCE_ALLSENDPARAMETERS:
  3935. eax5_defer_sends<Eax5AllSendValidator, EAXSOURCEALLSENDPROPERTIES>(call, props.sends);
  3936. break;
  3937. case EAXSOURCE_OCCLUSIONSENDPARAMETERS:
  3938. eax5_defer_sends<Eax5OcclusionSendValidator, EAXSOURCEOCCLUSIONSENDPROPERTIES>(call, props.sends);
  3939. break;
  3940. case EAXSOURCE_EXCLUSIONSENDPARAMETERS:
  3941. eax5_defer_sends<Eax5ExclusionSendValidator, EAXSOURCEEXCLUSIONSENDPROPERTIES>(call, props.sends);
  3942. break;
  3943. case EAXSOURCE_ACTIVEFXSLOTID:
  3944. eax5_defer_active_fx_slot_id(call, al::span{props.active_fx_slots.guidActiveFXSlots});
  3945. break;
  3946. case EAXSOURCE_MACROFXFACTOR:
  3947. eax_defer<Eax5SourceMacroFXFactorValidator>(call, props.source.flMacroFXFactor);
  3948. break;
  3949. case EAXSOURCE_SPEAKERLEVELS:
  3950. eax5_defer_speaker_levels(call, props.speaker_levels);
  3951. break;
  3952. case EAXSOURCE_ALL2DPARAMETERS:
  3953. eax5_defer_all_2d(call, props.source);
  3954. break;
  3955. default:
  3956. eax_fail_unknown_property_id();
  3957. }
  3958. }
  3959. void ALsource::eax_set(const EaxCall& call)
  3960. {
  3961. const auto eax_version = call.get_version();
  3962. switch(eax_version)
  3963. {
  3964. case 1: eax1_set(call, mEax1.d); break;
  3965. case 2: eax2_set(call, mEax2.d); break;
  3966. case 3: eax3_set(call, mEax3.d); break;
  3967. case 4: eax4_set(call, mEax4.d); break;
  3968. case 5: eax5_set(call, mEax5.d); break;
  3969. default: eax_fail_unknown_property_id();
  3970. }
  3971. mEaxChanged = true;
  3972. mEaxVersion = eax_version;
  3973. }
  3974. void ALsource::eax_get_active_fx_slot_id(const EaxCall& call, const al::span<const GUID> src_ids)
  3975. {
  3976. assert(src_ids.size()==EAX40_MAX_ACTIVE_FXSLOTS || src_ids.size()==EAX50_MAX_ACTIVE_FXSLOTS);
  3977. const auto dst_ids = call.get_values<GUID>(src_ids.size());
  3978. std::uninitialized_copy_n(src_ids.begin(), dst_ids.size(), dst_ids.begin());
  3979. }
  3980. void ALsource::eax1_get(const EaxCall& call, const Eax1Props& props)
  3981. {
  3982. switch (call.get_property_id()) {
  3983. case DSPROPERTY_EAXBUFFER_ALL:
  3984. case DSPROPERTY_EAXBUFFER_REVERBMIX:
  3985. call.set_value<Exception>(props.fMix);
  3986. break;
  3987. default:
  3988. eax_fail_unknown_property_id();
  3989. }
  3990. }
  3991. void ALsource::eax2_get(const EaxCall& call, const Eax2Props& props)
  3992. {
  3993. switch (call.get_property_id()) {
  3994. case DSPROPERTY_EAX20BUFFER_NONE:
  3995. break;
  3996. case DSPROPERTY_EAX20BUFFER_ALLPARAMETERS:
  3997. call.set_value<Exception>(props);
  3998. break;
  3999. case DSPROPERTY_EAX20BUFFER_DIRECT:
  4000. call.set_value<Exception>(props.lDirect);
  4001. break;
  4002. case DSPROPERTY_EAX20BUFFER_DIRECTHF:
  4003. call.set_value<Exception>(props.lDirectHF);
  4004. break;
  4005. case DSPROPERTY_EAX20BUFFER_ROOM:
  4006. call.set_value<Exception>(props.lRoom);
  4007. break;
  4008. case DSPROPERTY_EAX20BUFFER_ROOMHF:
  4009. call.set_value<Exception>(props.lRoomHF);
  4010. break;
  4011. case DSPROPERTY_EAX20BUFFER_ROOMROLLOFFFACTOR:
  4012. call.set_value<Exception>(props.flRoomRolloffFactor);
  4013. break;
  4014. case DSPROPERTY_EAX20BUFFER_OBSTRUCTION:
  4015. call.set_value<Exception>(props.lObstruction);
  4016. break;
  4017. case DSPROPERTY_EAX20BUFFER_OBSTRUCTIONLFRATIO:
  4018. call.set_value<Exception>(props.flObstructionLFRatio);
  4019. break;
  4020. case DSPROPERTY_EAX20BUFFER_OCCLUSION:
  4021. call.set_value<Exception>(props.lOcclusion);
  4022. break;
  4023. case DSPROPERTY_EAX20BUFFER_OCCLUSIONLFRATIO:
  4024. call.set_value<Exception>(props.flOcclusionLFRatio);
  4025. break;
  4026. case DSPROPERTY_EAX20BUFFER_OCCLUSIONROOMRATIO:
  4027. call.set_value<Exception>(props.flOcclusionRoomRatio);
  4028. break;
  4029. case DSPROPERTY_EAX20BUFFER_OUTSIDEVOLUMEHF:
  4030. call.set_value<Exception>(props.lOutsideVolumeHF);
  4031. break;
  4032. case DSPROPERTY_EAX20BUFFER_AIRABSORPTIONFACTOR:
  4033. call.set_value<Exception>(props.flAirAbsorptionFactor);
  4034. break;
  4035. case DSPROPERTY_EAX20BUFFER_FLAGS:
  4036. call.set_value<Exception>(props.dwFlags);
  4037. break;
  4038. default:
  4039. eax_fail_unknown_property_id();
  4040. }
  4041. }
  4042. void ALsource::eax3_get_obstruction(const EaxCall& call, const Eax3Props& props)
  4043. {
  4044. const auto& subprops = reinterpret_cast<const EAXOBSTRUCTIONPROPERTIES&>(props.lObstruction);
  4045. call.set_value<Exception>(subprops);
  4046. }
  4047. void ALsource::eax3_get_occlusion(const EaxCall& call, const Eax3Props& props)
  4048. {
  4049. const auto& subprops = reinterpret_cast<const EAXOCCLUSIONPROPERTIES&>(props.lOcclusion);
  4050. call.set_value<Exception>(subprops);
  4051. }
  4052. void ALsource::eax3_get_exclusion(const EaxCall& call, const Eax3Props& props)
  4053. {
  4054. const auto& subprops = reinterpret_cast<const EAXEXCLUSIONPROPERTIES&>(props.lExclusion);
  4055. call.set_value<Exception>(subprops);
  4056. }
  4057. void ALsource::eax3_get(const EaxCall& call, const Eax3Props& props)
  4058. {
  4059. switch (call.get_property_id()) {
  4060. case EAXSOURCE_NONE:
  4061. break;
  4062. case EAXSOURCE_ALLPARAMETERS:
  4063. call.set_value<Exception>(props);
  4064. break;
  4065. case EAXSOURCE_OBSTRUCTIONPARAMETERS:
  4066. eax3_get_obstruction(call, props);
  4067. break;
  4068. case EAXSOURCE_OCCLUSIONPARAMETERS:
  4069. eax3_get_occlusion(call, props);
  4070. break;
  4071. case EAXSOURCE_EXCLUSIONPARAMETERS:
  4072. eax3_get_exclusion(call, props);
  4073. break;
  4074. case EAXSOURCE_DIRECT:
  4075. call.set_value<Exception>(props.lDirect);
  4076. break;
  4077. case EAXSOURCE_DIRECTHF:
  4078. call.set_value<Exception>(props.lDirectHF);
  4079. break;
  4080. case EAXSOURCE_ROOM:
  4081. call.set_value<Exception>(props.lRoom);
  4082. break;
  4083. case EAXSOURCE_ROOMHF:
  4084. call.set_value<Exception>(props.lRoomHF);
  4085. break;
  4086. case EAXSOURCE_OBSTRUCTION:
  4087. call.set_value<Exception>(props.lObstruction);
  4088. break;
  4089. case EAXSOURCE_OBSTRUCTIONLFRATIO:
  4090. call.set_value<Exception>(props.flObstructionLFRatio);
  4091. break;
  4092. case EAXSOURCE_OCCLUSION:
  4093. call.set_value<Exception>(props.lOcclusion);
  4094. break;
  4095. case EAXSOURCE_OCCLUSIONLFRATIO:
  4096. call.set_value<Exception>(props.flOcclusionLFRatio);
  4097. break;
  4098. case EAXSOURCE_OCCLUSIONROOMRATIO:
  4099. call.set_value<Exception>(props.flOcclusionRoomRatio);
  4100. break;
  4101. case EAXSOURCE_OCCLUSIONDIRECTRATIO:
  4102. call.set_value<Exception>(props.flOcclusionDirectRatio);
  4103. break;
  4104. case EAXSOURCE_EXCLUSION:
  4105. call.set_value<Exception>(props.lExclusion);
  4106. break;
  4107. case EAXSOURCE_EXCLUSIONLFRATIO:
  4108. call.set_value<Exception>(props.flExclusionLFRatio);
  4109. break;
  4110. case EAXSOURCE_OUTSIDEVOLUMEHF:
  4111. call.set_value<Exception>(props.lOutsideVolumeHF);
  4112. break;
  4113. case EAXSOURCE_DOPPLERFACTOR:
  4114. call.set_value<Exception>(props.flDopplerFactor);
  4115. break;
  4116. case EAXSOURCE_ROLLOFFFACTOR:
  4117. call.set_value<Exception>(props.flRolloffFactor);
  4118. break;
  4119. case EAXSOURCE_ROOMROLLOFFFACTOR:
  4120. call.set_value<Exception>(props.flRoomRolloffFactor);
  4121. break;
  4122. case EAXSOURCE_AIRABSORPTIONFACTOR:
  4123. call.set_value<Exception>(props.flAirAbsorptionFactor);
  4124. break;
  4125. case EAXSOURCE_FLAGS:
  4126. call.set_value<Exception>(props.ulFlags);
  4127. break;
  4128. default:
  4129. eax_fail_unknown_property_id();
  4130. }
  4131. }
  4132. void ALsource::eax4_get(const EaxCall& call, const Eax4Props& props)
  4133. {
  4134. switch (call.get_property_id()) {
  4135. case EAXSOURCE_NONE:
  4136. break;
  4137. case EAXSOURCE_ALLPARAMETERS:
  4138. case EAXSOURCE_OBSTRUCTIONPARAMETERS:
  4139. case EAXSOURCE_OCCLUSIONPARAMETERS:
  4140. case EAXSOURCE_EXCLUSIONPARAMETERS:
  4141. case EAXSOURCE_DIRECT:
  4142. case EAXSOURCE_DIRECTHF:
  4143. case EAXSOURCE_ROOM:
  4144. case EAXSOURCE_ROOMHF:
  4145. case EAXSOURCE_OBSTRUCTION:
  4146. case EAXSOURCE_OBSTRUCTIONLFRATIO:
  4147. case EAXSOURCE_OCCLUSION:
  4148. case EAXSOURCE_OCCLUSIONLFRATIO:
  4149. case EAXSOURCE_OCCLUSIONROOMRATIO:
  4150. case EAXSOURCE_OCCLUSIONDIRECTRATIO:
  4151. case EAXSOURCE_EXCLUSION:
  4152. case EAXSOURCE_EXCLUSIONLFRATIO:
  4153. case EAXSOURCE_OUTSIDEVOLUMEHF:
  4154. case EAXSOURCE_DOPPLERFACTOR:
  4155. case EAXSOURCE_ROLLOFFFACTOR:
  4156. case EAXSOURCE_ROOMROLLOFFFACTOR:
  4157. case EAXSOURCE_AIRABSORPTIONFACTOR:
  4158. case EAXSOURCE_FLAGS:
  4159. eax3_get(call, props.source);
  4160. break;
  4161. case EAXSOURCE_SENDPARAMETERS:
  4162. eax_get_sends<EAXSOURCESENDPROPERTIES>(call, props.sends);
  4163. break;
  4164. case EAXSOURCE_ALLSENDPARAMETERS:
  4165. eax_get_sends<EAXSOURCEALLSENDPROPERTIES>(call, props.sends);
  4166. break;
  4167. case EAXSOURCE_OCCLUSIONSENDPARAMETERS:
  4168. eax_get_sends<EAXSOURCEOCCLUSIONSENDPROPERTIES>(call, props.sends);
  4169. break;
  4170. case EAXSOURCE_EXCLUSIONSENDPARAMETERS:
  4171. eax_get_sends<EAXSOURCEEXCLUSIONSENDPROPERTIES>(call, props.sends);
  4172. break;
  4173. case EAXSOURCE_ACTIVEFXSLOTID:
  4174. eax_get_active_fx_slot_id(call, props.active_fx_slots.guidActiveFXSlots);
  4175. break;
  4176. default:
  4177. eax_fail_unknown_property_id();
  4178. }
  4179. }
  4180. void ALsource::eax5_get_all_2d(const EaxCall& call, const EAX50SOURCEPROPERTIES& props)
  4181. {
  4182. auto& subprops = call.get_value<Exception, EAXSOURCE2DPROPERTIES>();
  4183. subprops.lDirect = props.lDirect;
  4184. subprops.lDirectHF = props.lDirectHF;
  4185. subprops.lRoom = props.lRoom;
  4186. subprops.lRoomHF = props.lRoomHF;
  4187. subprops.ulFlags = props.ulFlags;
  4188. }
  4189. void ALsource::eax5_get_speaker_levels(const EaxCall& call, const EaxSpeakerLevels& props)
  4190. {
  4191. const auto subprops = call.get_values<EAXSPEAKERLEVELPROPERTIES>(eax_max_speakers);
  4192. std::uninitialized_copy_n(props.cbegin(), subprops.size(), subprops.begin());
  4193. }
  4194. void ALsource::eax5_get(const EaxCall& call, const Eax5Props& props)
  4195. {
  4196. switch (call.get_property_id()) {
  4197. case EAXSOURCE_NONE:
  4198. break;
  4199. case EAXSOURCE_ALLPARAMETERS:
  4200. case EAXSOURCE_OBSTRUCTIONPARAMETERS:
  4201. case EAXSOURCE_OCCLUSIONPARAMETERS:
  4202. case EAXSOURCE_EXCLUSIONPARAMETERS:
  4203. case EAXSOURCE_DIRECT:
  4204. case EAXSOURCE_DIRECTHF:
  4205. case EAXSOURCE_ROOM:
  4206. case EAXSOURCE_ROOMHF:
  4207. case EAXSOURCE_OBSTRUCTION:
  4208. case EAXSOURCE_OBSTRUCTIONLFRATIO:
  4209. case EAXSOURCE_OCCLUSION:
  4210. case EAXSOURCE_OCCLUSIONLFRATIO:
  4211. case EAXSOURCE_OCCLUSIONROOMRATIO:
  4212. case EAXSOURCE_OCCLUSIONDIRECTRATIO:
  4213. case EAXSOURCE_EXCLUSION:
  4214. case EAXSOURCE_EXCLUSIONLFRATIO:
  4215. case EAXSOURCE_OUTSIDEVOLUMEHF:
  4216. case EAXSOURCE_DOPPLERFACTOR:
  4217. case EAXSOURCE_ROLLOFFFACTOR:
  4218. case EAXSOURCE_ROOMROLLOFFFACTOR:
  4219. case EAXSOURCE_AIRABSORPTIONFACTOR:
  4220. case EAXSOURCE_FLAGS:
  4221. eax3_get(call, props.source);
  4222. break;
  4223. case EAXSOURCE_SENDPARAMETERS:
  4224. eax_get_sends<EAXSOURCESENDPROPERTIES>(call, props.sends);
  4225. break;
  4226. case EAXSOURCE_ALLSENDPARAMETERS:
  4227. eax_get_sends<EAXSOURCEALLSENDPROPERTIES>(call, props.sends);
  4228. break;
  4229. case EAXSOURCE_OCCLUSIONSENDPARAMETERS:
  4230. eax_get_sends<EAXSOURCEOCCLUSIONSENDPROPERTIES>(call, props.sends);
  4231. break;
  4232. case EAXSOURCE_EXCLUSIONSENDPARAMETERS:
  4233. eax_get_sends<EAXSOURCEEXCLUSIONSENDPROPERTIES>(call, props.sends);
  4234. break;
  4235. case EAXSOURCE_ACTIVEFXSLOTID:
  4236. eax_get_active_fx_slot_id(call, props.active_fx_slots.guidActiveFXSlots);
  4237. break;
  4238. case EAXSOURCE_MACROFXFACTOR:
  4239. call.set_value<Exception>(props.source.flMacroFXFactor);
  4240. break;
  4241. case EAXSOURCE_SPEAKERLEVELS:
  4242. call.set_value<Exception>(props.speaker_levels);
  4243. break;
  4244. case EAXSOURCE_ALL2DPARAMETERS:
  4245. eax5_get_all_2d(call, props.source);
  4246. break;
  4247. default:
  4248. eax_fail_unknown_property_id();
  4249. }
  4250. }
  4251. void ALsource::eax_get(const EaxCall& call)
  4252. {
  4253. switch (call.get_version()) {
  4254. case 1: eax1_get(call, mEax1.i); break;
  4255. case 2: eax2_get(call, mEax2.i); break;
  4256. case 3: eax3_get(call, mEax3.i); break;
  4257. case 4: eax4_get(call, mEax4.i); break;
  4258. case 5: eax5_get(call, mEax5.i); break;
  4259. default: eax_fail_unknown_version();
  4260. }
  4261. }
  4262. void ALsource::eax_set_al_source_send(ALeffectslot *slot, size_t sendidx, const EaxAlLowPassParam &filter)
  4263. {
  4264. if(sendidx >= EAX_MAX_FXSLOTS)
  4265. return;
  4266. auto &send = Send[sendidx];
  4267. send.Gain = filter.gain;
  4268. send.GainHF = filter.gain_hf;
  4269. send.HFReference = LowPassFreqRef;
  4270. send.GainLF = 1.0f;
  4271. send.LFReference = HighPassFreqRef;
  4272. if(slot != nullptr)
  4273. IncrementRef(slot->ref);
  4274. if(auto *oldslot = send.Slot)
  4275. DecrementRef(oldslot->ref);
  4276. send.Slot = slot;
  4277. mPropsDirty = true;
  4278. }
  4279. void ALsource::eax_commit_active_fx_slots()
  4280. {
  4281. // Clear all slots to an inactive state.
  4282. mEaxActiveFxSlots.fill(false);
  4283. // Mark the set slots as active.
  4284. for(const auto& slot_id : mEax.active_fx_slots.guidActiveFXSlots)
  4285. {
  4286. if(slot_id == EAX_NULL_GUID)
  4287. {
  4288. }
  4289. else if(slot_id == EAX_PrimaryFXSlotID)
  4290. {
  4291. // Mark primary FX slot as active.
  4292. if(mEaxPrimaryFxSlotId.has_value())
  4293. mEaxActiveFxSlots[*mEaxPrimaryFxSlotId] = true;
  4294. }
  4295. else if(slot_id == EAXPROPERTYID_EAX50_FXSlot0)
  4296. mEaxActiveFxSlots[0] = true;
  4297. else if(slot_id == EAXPROPERTYID_EAX50_FXSlot1)
  4298. mEaxActiveFxSlots[1] = true;
  4299. else if(slot_id == EAXPROPERTYID_EAX50_FXSlot2)
  4300. mEaxActiveFxSlots[2] = true;
  4301. else if(slot_id == EAXPROPERTYID_EAX50_FXSlot3)
  4302. mEaxActiveFxSlots[3] = true;
  4303. }
  4304. // Deactivate EFX auxiliary effect slots for inactive slots. Active slots
  4305. // will be updated with the room filters.
  4306. for(size_t i{0};i < EAX_MAX_FXSLOTS;++i)
  4307. {
  4308. if(!mEaxActiveFxSlots[i])
  4309. eax_set_al_source_send(nullptr, i, EaxAlLowPassParam{1.0f, 1.0f});
  4310. }
  4311. }
  4312. void ALsource::eax_commit_filters()
  4313. {
  4314. eax_update_direct_filter();
  4315. eax_update_room_filters();
  4316. }
  4317. void ALsource::eaxCommit()
  4318. {
  4319. const auto primary_fx_slot_id = mEaxAlContext->eaxGetPrimaryFxSlotIndex();
  4320. const auto is_primary_fx_slot_id_changed = (mEaxPrimaryFxSlotId != primary_fx_slot_id);
  4321. if(!mEaxChanged && !is_primary_fx_slot_id_changed)
  4322. return;
  4323. mEaxPrimaryFxSlotId = primary_fx_slot_id;
  4324. mEaxChanged = false;
  4325. switch(mEaxVersion)
  4326. {
  4327. case 1:
  4328. mEax1.i = mEax1.d;
  4329. eax1_translate(mEax1.i, mEax);
  4330. break;
  4331. case 2:
  4332. mEax2.i = mEax2.d;
  4333. eax2_translate(mEax2.i, mEax);
  4334. break;
  4335. case 3:
  4336. mEax3.i = mEax3.d;
  4337. eax3_translate(mEax3.i, mEax);
  4338. break;
  4339. case 4:
  4340. mEax4.i = mEax4.d;
  4341. eax4_translate(mEax4.i, mEax);
  4342. break;
  4343. case 5:
  4344. mEax5.i = mEax5.d;
  4345. mEax = mEax5.d;
  4346. break;
  4347. }
  4348. eax_set_efx_outer_gain_hf();
  4349. eax_set_efx_doppler_factor();
  4350. eax_set_efx_rolloff_factor();
  4351. eax_set_efx_room_rolloff_factor();
  4352. eax_set_efx_air_absorption_factor();
  4353. eax_set_efx_dry_gain_hf_auto();
  4354. eax_set_efx_wet_gain_auto();
  4355. eax_set_efx_wet_gain_hf_auto();
  4356. eax_commit_active_fx_slots();
  4357. eax_commit_filters();
  4358. }
  4359. #endif // ALSOFT_EAX