123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462 |
- /**
- * OpenAL cross platform audio library
- * Copyright (C) 1999-2007 by authors.
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- * Or go to http://www.gnu.org/copyleft/lgpl.html
- */
- #include "config.h"
- #include "source.h"
- #include <algorithm>
- #include <array>
- #include <atomic>
- #include <cassert>
- #include <chrono>
- #include <climits>
- #include <cmath>
- #include <cstdint>
- #include <functional>
- #include <iterator>
- #include <limits>
- #include <memory>
- #include <mutex>
- #include <new>
- #include <numeric>
- #include <thread>
- #include <utility>
- #include "AL/al.h"
- #include "AL/alc.h"
- #include "AL/alext.h"
- #include "AL/efx.h"
- #include "albit.h"
- #include "alcmain.h"
- #include "alcontext.h"
- #include "almalloc.h"
- #include "alnumeric.h"
- #include "aloptional.h"
- #include "alspan.h"
- #include "alu.h"
- #include "atomic.h"
- #include "auxeffectslot.h"
- #include "backends/base.h"
- #include "bformatdec.h"
- #include "buffer.h"
- #include "core/ambidefs.h"
- #include "core/except.h"
- #include "core/filters/nfc.h"
- #include "core/filters/splitter.h"
- #include "core/logging.h"
- #include "event.h"
- #include "filter.h"
- #include "inprogext.h"
- #include "math_defs.h"
- #include "opthelpers.h"
- #include "ringbuffer.h"
- #include "threads.h"
- #include "voice_change.h"
- namespace {
- using namespace std::placeholders;
- using std::chrono::nanoseconds;
- Voice *GetSourceVoice(ALsource *source, ALCcontext *context)
- {
- auto voicelist = context->getVoicesSpan();
- ALuint idx{source->VoiceIdx};
- if(idx < voicelist.size())
- {
- ALuint sid{source->id};
- Voice *voice = voicelist[idx];
- if(voice->mSourceID.load(std::memory_order_acquire) == sid)
- return voice;
- }
- source->VoiceIdx = INVALID_VOICE_IDX;
- return nullptr;
- }
- void UpdateSourceProps(const ALsource *source, Voice *voice, ALCcontext *context)
- {
- /* Get an unused property container, or allocate a new one as needed. */
- VoicePropsItem *props{context->mFreeVoiceProps.load(std::memory_order_acquire)};
- if(!props)
- props = new VoicePropsItem{};
- else
- {
- VoicePropsItem *next;
- do {
- next = props->next.load(std::memory_order_relaxed);
- } while(context->mFreeVoiceProps.compare_exchange_weak(props, next,
- std::memory_order_acq_rel, std::memory_order_acquire) == 0);
- }
- props->Pitch = source->Pitch;
- props->Gain = source->Gain;
- props->OuterGain = source->OuterGain;
- props->MinGain = source->MinGain;
- props->MaxGain = source->MaxGain;
- props->InnerAngle = source->InnerAngle;
- props->OuterAngle = source->OuterAngle;
- props->RefDistance = source->RefDistance;
- props->MaxDistance = source->MaxDistance;
- props->RolloffFactor = source->RolloffFactor;
- props->Position = source->Position;
- props->Velocity = source->Velocity;
- props->Direction = source->Direction;
- props->OrientAt = source->OrientAt;
- props->OrientUp = source->OrientUp;
- props->HeadRelative = source->HeadRelative;
- props->mDistanceModel = source->mDistanceModel;
- props->mResampler = source->mResampler;
- props->DirectChannels = source->DirectChannels;
- props->mSpatializeMode = source->mSpatialize;
- props->DryGainHFAuto = source->DryGainHFAuto;
- props->WetGainAuto = source->WetGainAuto;
- props->WetGainHFAuto = source->WetGainHFAuto;
- props->OuterGainHF = source->OuterGainHF;
- props->AirAbsorptionFactor = source->AirAbsorptionFactor;
- props->RoomRolloffFactor = source->RoomRolloffFactor;
- props->DopplerFactor = source->DopplerFactor;
- props->StereoPan = source->StereoPan;
- props->Radius = source->Radius;
- props->Direct.Gain = source->Direct.Gain;
- props->Direct.GainHF = source->Direct.GainHF;
- props->Direct.HFReference = source->Direct.HFReference;
- props->Direct.GainLF = source->Direct.GainLF;
- props->Direct.LFReference = source->Direct.LFReference;
- auto copy_send = [](const ALsource::SendData &srcsend) noexcept -> VoiceProps::SendData
- {
- VoiceProps::SendData ret{};
- ret.Slot = srcsend.Slot ? &srcsend.Slot->mSlot : nullptr;
- ret.Gain = srcsend.Gain;
- ret.GainHF = srcsend.GainHF;
- ret.HFReference = srcsend.HFReference;
- ret.GainLF = srcsend.GainLF;
- ret.LFReference = srcsend.LFReference;
- return ret;
- };
- std::transform(source->Send.cbegin(), source->Send.cend(), props->Send, copy_send);
- if(!props->Send[0].Slot && context->mDefaultSlot)
- props->Send[0].Slot = &context->mDefaultSlot->mSlot;
- /* Set the new container for updating internal parameters. */
- props = voice->mUpdate.exchange(props, std::memory_order_acq_rel);
- if(props)
- {
- /* If there was an unused update container, put it back in the
- * freelist.
- */
- AtomicReplaceHead(context->mFreeVoiceProps, props);
- }
- }
- /* GetSourceSampleOffset
- *
- * Gets the current read offset for the given Source, in 32.32 fixed-point
- * samples. The offset is relative to the start of the queue (not the start of
- * the current buffer).
- */
- int64_t GetSourceSampleOffset(ALsource *Source, ALCcontext *context, nanoseconds *clocktime)
- {
- ALCdevice *device{context->mDevice.get()};
- const VoiceBufferItem *Current{};
- uint64_t readPos{};
- ALuint refcount;
- Voice *voice;
- do {
- refcount = device->waitForMix();
- *clocktime = GetDeviceClockTime(device);
- voice = GetSourceVoice(Source, context);
- if(voice)
- {
- Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
- readPos = uint64_t{voice->mPosition.load(std::memory_order_relaxed)} << 32;
- readPos |= uint64_t{voice->mPositionFrac.load(std::memory_order_relaxed)} <<
- (32-MixerFracBits);
- }
- std::atomic_thread_fence(std::memory_order_acquire);
- } while(refcount != device->MixCount.load(std::memory_order_relaxed));
- if(!voice)
- return 0;
- for(auto &item : Source->mQueue)
- {
- if(&item == Current) break;
- readPos += uint64_t{item.mSampleLen} << 32;
- }
- return static_cast<int64_t>(minu64(readPos, 0x7fffffffffffffff_u64));
- }
- /* GetSourceSecOffset
- *
- * Gets the current read offset for the given Source, in seconds. The offset is
- * relative to the start of the queue (not the start of the current buffer).
- */
- double GetSourceSecOffset(ALsource *Source, ALCcontext *context, nanoseconds *clocktime)
- {
- ALCdevice *device{context->mDevice.get()};
- const VoiceBufferItem *Current{};
- uint64_t readPos{};
- ALuint refcount;
- Voice *voice;
- do {
- refcount = device->waitForMix();
- *clocktime = GetDeviceClockTime(device);
- voice = GetSourceVoice(Source, context);
- if(voice)
- {
- Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
- readPos = uint64_t{voice->mPosition.load(std::memory_order_relaxed)} << MixerFracBits;
- readPos |= voice->mPositionFrac.load(std::memory_order_relaxed);
- }
- std::atomic_thread_fence(std::memory_order_acquire);
- } while(refcount != device->MixCount.load(std::memory_order_relaxed));
- if(!voice)
- return 0.0f;
- const ALbuffer *BufferFmt{nullptr};
- auto BufferList = Source->mQueue.cbegin();
- while(BufferList != Source->mQueue.cend() && std::addressof(*BufferList) != Current)
- {
- if(!BufferFmt) BufferFmt = BufferList->mBuffer;
- readPos += uint64_t{BufferList->mSampleLen} << MixerFracBits;
- ++BufferList;
- }
- while(BufferList != Source->mQueue.cend() && !BufferFmt)
- {
- BufferFmt = BufferList->mBuffer;
- ++BufferList;
- }
- assert(BufferFmt != nullptr);
- return static_cast<double>(readPos) / double{MixerFracOne} / BufferFmt->mSampleRate;
- }
- /* GetSourceOffset
- *
- * Gets the current read offset for the given Source, in the appropriate format
- * (Bytes, Samples or Seconds). The offset is relative to the start of the
- * queue (not the start of the current buffer).
- */
- double GetSourceOffset(ALsource *Source, ALenum name, ALCcontext *context)
- {
- ALCdevice *device{context->mDevice.get()};
- const VoiceBufferItem *Current{};
- ALuint readPos{};
- ALuint readPosFrac{};
- ALuint refcount;
- Voice *voice;
- do {
- refcount = device->waitForMix();
- voice = GetSourceVoice(Source, context);
- if(voice)
- {
- Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
- readPos = voice->mPosition.load(std::memory_order_relaxed);
- readPosFrac = voice->mPositionFrac.load(std::memory_order_relaxed);
- }
- std::atomic_thread_fence(std::memory_order_acquire);
- } while(refcount != device->MixCount.load(std::memory_order_relaxed));
- if(!voice)
- return 0.0;
- const ALbuffer *BufferFmt{nullptr};
- auto BufferList = Source->mQueue.cbegin();
- while(BufferList != Source->mQueue.cend() && std::addressof(*BufferList) != Current)
- {
- if(!BufferFmt) BufferFmt = BufferList->mBuffer;
- readPos += BufferList->mSampleLen;
- ++BufferList;
- }
- while(BufferList != Source->mQueue.cend() && !BufferFmt)
- {
- BufferFmt = BufferList->mBuffer;
- ++BufferList;
- }
- assert(BufferFmt != nullptr);
- double offset{};
- switch(name)
- {
- case AL_SEC_OFFSET:
- offset = (readPos + readPosFrac/double{MixerFracOne}) / BufferFmt->mSampleRate;
- break;
- case AL_SAMPLE_OFFSET:
- offset = readPos + readPosFrac/double{MixerFracOne};
- break;
- case AL_BYTE_OFFSET:
- if(BufferFmt->OriginalType == UserFmtIMA4)
- {
- ALuint FrameBlockSize{BufferFmt->OriginalAlign};
- ALuint align{(BufferFmt->OriginalAlign-1)/2 + 4};
- ALuint BlockSize{align * BufferFmt->channelsFromFmt()};
- /* Round down to nearest ADPCM block */
- offset = static_cast<double>(readPos / FrameBlockSize * BlockSize);
- }
- else if(BufferFmt->OriginalType == UserFmtMSADPCM)
- {
- ALuint FrameBlockSize{BufferFmt->OriginalAlign};
- ALuint align{(FrameBlockSize-2)/2 + 7};
- ALuint BlockSize{align * BufferFmt->channelsFromFmt()};
- /* Round down to nearest ADPCM block */
- offset = static_cast<double>(readPos / FrameBlockSize * BlockSize);
- }
- else
- {
- const ALuint FrameSize{BufferFmt->frameSizeFromFmt()};
- offset = static_cast<double>(readPos * FrameSize);
- }
- break;
- }
- return offset;
- }
- struct VoicePos {
- ALuint pos, frac;
- VoiceBufferItem *bufferitem;
- };
- /**
- * GetSampleOffset
- *
- * Retrieves the voice position, fixed-point fraction, and bufferlist item
- * using the givem offset type and offset. If the offset is out of range,
- * returns an empty optional.
- */
- al::optional<VoicePos> GetSampleOffset(al::deque<ALbufferQueueItem> &BufferList, ALenum OffsetType,
- double Offset)
- {
- /* Find the first valid Buffer in the Queue */
- const ALbuffer *BufferFmt{nullptr};
- for(auto &item : BufferList)
- {
- BufferFmt = item.mBuffer;
- if(BufferFmt) break;
- }
- if(!BufferFmt)
- return al::nullopt;
- /* Get sample frame offset */
- ALuint offset{0u}, frac{0u};
- double dbloff, dblfrac;
- switch(OffsetType)
- {
- case AL_SEC_OFFSET:
- dblfrac = std::modf(Offset*BufferFmt->mSampleRate, &dbloff);
- offset = static_cast<ALuint>(mind(dbloff, std::numeric_limits<ALuint>::max()));
- frac = static_cast<ALuint>(mind(dblfrac*MixerFracOne, MixerFracOne-1.0));
- break;
- case AL_SAMPLE_OFFSET:
- dblfrac = std::modf(Offset, &dbloff);
- offset = static_cast<ALuint>(mind(dbloff, std::numeric_limits<ALuint>::max()));
- frac = static_cast<ALuint>(mind(dblfrac*MixerFracOne, MixerFracOne-1.0));
- break;
- case AL_BYTE_OFFSET:
- /* Determine the ByteOffset (and ensure it is block aligned) */
- offset = static_cast<ALuint>(Offset);
- if(BufferFmt->OriginalType == UserFmtIMA4)
- {
- const ALuint align{(BufferFmt->OriginalAlign-1)/2 + 4};
- offset /= align * BufferFmt->channelsFromFmt();
- offset *= BufferFmt->OriginalAlign;
- }
- else if(BufferFmt->OriginalType == UserFmtMSADPCM)
- {
- const ALuint align{(BufferFmt->OriginalAlign-2)/2 + 7};
- offset /= align * BufferFmt->channelsFromFmt();
- offset *= BufferFmt->OriginalAlign;
- }
- else
- offset /= BufferFmt->frameSizeFromFmt();
- frac = 0;
- break;
- }
- /* Find the bufferlist item this offset belongs to. */
- ALuint totalBufferLen{0u};
- for(auto &item : BufferList)
- {
- if(totalBufferLen > offset)
- break;
- if(item.mSampleLen > offset-totalBufferLen)
- {
- /* Offset is in this buffer */
- return al::make_optional(VoicePos{offset-totalBufferLen, frac, &item});
- }
- totalBufferLen += item.mSampleLen;
- }
- /* Offset is out of range of the queue */
- return al::nullopt;
- }
- void InitVoice(Voice *voice, ALsource *source, ALbufferQueueItem *BufferList, ALCcontext *context,
- ALCdevice *device)
- {
- voice->mLoopBuffer.store(source->Looping ? &source->mQueue.front() : nullptr,
- std::memory_order_relaxed);
- ALbuffer *buffer{BufferList->mBuffer};
- ALuint num_channels{buffer->channelsFromFmt()};
- voice->mFrequency = buffer->mSampleRate;
- voice->mFmtChannels = buffer->mChannels;
- voice->mFmtType = buffer->mType;
- voice->mSampleSize = buffer->bytesFromFmt();
- voice->mAmbiLayout = buffer->mAmbiLayout;
- voice->mAmbiScaling = buffer->mAmbiScaling;
- voice->mAmbiOrder = buffer->mAmbiOrder;
- if(buffer->mCallback) voice->mFlags |= VoiceIsCallback;
- else if(source->SourceType == AL_STATIC) voice->mFlags |= VoiceIsStatic;
- voice->mNumCallbackSamples = 0;
- /* Clear the stepping value explicitly so the mixer knows not to mix this
- * until the update gets applied.
- */
- voice->mStep = 0;
- if(voice->mChans.capacity() > 2 && num_channels < voice->mChans.capacity())
- al::vector<Voice::ChannelData>{}.swap(voice->mChans);
- voice->mChans.reserve(maxu(2, num_channels));
- voice->mChans.resize(num_channels);
- /* Don't need to set the VOICE_IS_AMBISONIC flag if the device is not
- * higher order than the voice. No HF scaling is necessary to mix it.
- */
- if(voice->mAmbiOrder && device->mAmbiOrder > voice->mAmbiOrder)
- {
- const uint8_t *OrderFromChan{(voice->mFmtChannels == FmtBFormat2D) ?
- AmbiIndex::OrderFrom2DChannel().data() :
- AmbiIndex::OrderFromChannel().data()};
- const auto scales = BFormatDec::GetHFOrderScales(voice->mAmbiOrder, device->mAmbiOrder);
- const BandSplitter splitter{device->mXOverFreq / static_cast<float>(device->Frequency)};
- for(auto &chandata : voice->mChans)
- {
- chandata.mPrevSamples.fill(0.0f);
- chandata.mAmbiScale = scales[*(OrderFromChan++)];
- chandata.mAmbiSplitter = splitter;
- chandata.mDryParams = DirectParams{};
- std::fill_n(chandata.mWetParams.begin(), device->NumAuxSends, SendParams{});
- }
- voice->mFlags |= VoiceIsAmbisonic;
- }
- else
- {
- /* Clear previous samples. */
- for(auto &chandata : voice->mChans)
- {
- chandata.mPrevSamples.fill(0.0f);
- chandata.mDryParams = DirectParams{};
- std::fill_n(chandata.mWetParams.begin(), device->NumAuxSends, SendParams{});
- }
- }
- if(device->AvgSpeakerDist > 0.0f)
- {
- const float w1{SpeedOfSoundMetersPerSec /
- (device->AvgSpeakerDist * static_cast<float>(device->Frequency))};
- for(auto &chandata : voice->mChans)
- chandata.mDryParams.NFCtrlFilter.init(w1);
- }
- source->PropsClean.test_and_set(std::memory_order_acq_rel);
- UpdateSourceProps(source, voice, context);
- voice->mSourceID.store(source->id, std::memory_order_release);
- }
- VoiceChange *GetVoiceChanger(ALCcontext *ctx)
- {
- VoiceChange *vchg{ctx->mVoiceChangeTail};
- if UNLIKELY(vchg == ctx->mCurrentVoiceChange.load(std::memory_order_acquire))
- {
- ctx->allocVoiceChanges(1);
- vchg = ctx->mVoiceChangeTail;
- }
- ctx->mVoiceChangeTail = vchg->mNext.exchange(nullptr, std::memory_order_relaxed);
- return vchg;
- }
- void SendVoiceChanges(ALCcontext *ctx, VoiceChange *tail)
- {
- ALCdevice *device{ctx->mDevice.get()};
- VoiceChange *oldhead{ctx->mCurrentVoiceChange.load(std::memory_order_acquire)};
- while(VoiceChange *next{oldhead->mNext.load(std::memory_order_relaxed)})
- oldhead = next;
- oldhead->mNext.store(tail, std::memory_order_release);
- const bool connected{device->Connected.load(std::memory_order_acquire)};
- device->waitForMix();
- if UNLIKELY(!connected)
- {
- /* If the device is disconnected, just ignore all pending changes. */
- VoiceChange *cur{ctx->mCurrentVoiceChange.load(std::memory_order_acquire)};
- while(VoiceChange *next{cur->mNext.load(std::memory_order_acquire)})
- {
- cur = next;
- if(Voice *voice{cur->mVoice})
- voice->mSourceID.store(0, std::memory_order_relaxed);
- }
- ctx->mCurrentVoiceChange.store(cur, std::memory_order_release);
- }
- }
- bool SetVoiceOffset(Voice *oldvoice, const VoicePos &vpos, ALsource *source, ALCcontext *context,
- ALCdevice *device)
- {
- /* First, get a free voice to start at the new offset. */
- auto voicelist = context->getVoicesSpan();
- Voice *newvoice{};
- ALuint vidx{0};
- for(Voice *voice : voicelist)
- {
- if(voice->mPlayState.load(std::memory_order_acquire) == Voice::Stopped
- && voice->mSourceID.load(std::memory_order_relaxed) == 0u
- && voice->mPendingChange.load(std::memory_order_relaxed) == false)
- {
- newvoice = voice;
- break;
- }
- ++vidx;
- }
- if UNLIKELY(!newvoice)
- {
- auto &allvoices = *context->mVoices.load(std::memory_order_relaxed);
- if(allvoices.size() == voicelist.size())
- context->allocVoices(1);
- context->mActiveVoiceCount.fetch_add(1, std::memory_order_release);
- voicelist = context->getVoicesSpan();
- vidx = 0;
- for(Voice *voice : voicelist)
- {
- if(voice->mPlayState.load(std::memory_order_acquire) == Voice::Stopped
- && voice->mSourceID.load(std::memory_order_relaxed) == 0u
- && voice->mPendingChange.load(std::memory_order_relaxed) == false)
- {
- newvoice = voice;
- break;
- }
- ++vidx;
- }
- }
- /* Initialize the new voice and set its starting offset.
- * TODO: It might be better to have the VoiceChange processing copy the old
- * voice's mixing parameters (and pending update) insead of initializing it
- * all here. This would just need to set the minimum properties to link the
- * voice to the source and its position-dependent properties (including the
- * fading flag).
- */
- newvoice->mPlayState.store(Voice::Pending, std::memory_order_relaxed);
- newvoice->mPosition.store(vpos.pos, std::memory_order_relaxed);
- newvoice->mPositionFrac.store(vpos.frac, std::memory_order_relaxed);
- newvoice->mCurrentBuffer.store(vpos.bufferitem, std::memory_order_relaxed);
- newvoice->mFlags = 0u;
- if(vpos.pos > 0 || vpos.frac > 0 || vpos.bufferitem != &source->mQueue.front())
- newvoice->mFlags |= VoiceIsFading;
- InitVoice(newvoice, source, &source->mQueue.front(), context, device);
- source->VoiceIdx = vidx;
- /* Set the old voice as having a pending change, and send it off with the
- * new one with a new offset voice change.
- */
- oldvoice->mPendingChange.store(true, std::memory_order_relaxed);
- VoiceChange *vchg{GetVoiceChanger(context)};
- vchg->mOldVoice = oldvoice;
- vchg->mVoice = newvoice;
- vchg->mSourceID = source->id;
- vchg->mState = VChangeState::Restart;
- SendVoiceChanges(context, vchg);
- /* If the old voice still has a sourceID, it's still active and the change-
- * over will work on the next update.
- */
- if LIKELY(oldvoice->mSourceID.load(std::memory_order_acquire) != 0u)
- return true;
- /* Otherwise, if the new voice's state is not pending, the change-over
- * already happened.
- */
- if(newvoice->mPlayState.load(std::memory_order_acquire) != Voice::Pending)
- return true;
- /* Otherwise, wait for any current mix to finish and check one last time. */
- device->waitForMix();
- if(newvoice->mPlayState.load(std::memory_order_acquire) != Voice::Pending)
- return true;
- /* The change-over failed because the old voice stopped before the new
- * voice could start at the new offset. Let go of the new voice and have
- * the caller store the source offset since it's stopped.
- */
- newvoice->mCurrentBuffer.store(nullptr, std::memory_order_relaxed);
- newvoice->mLoopBuffer.store(nullptr, std::memory_order_relaxed);
- newvoice->mSourceID.store(0u, std::memory_order_relaxed);
- newvoice->mPlayState.store(Voice::Stopped, std::memory_order_relaxed);
- return false;
- }
- /**
- * Returns if the last known state for the source was playing or paused. Does
- * not sync with the mixer voice.
- */
- inline bool IsPlayingOrPaused(ALsource *source)
- { return source->state == AL_PLAYING || source->state == AL_PAUSED; }
- /**
- * Returns an updated source state using the matching voice's status (or lack
- * thereof).
- */
- inline ALenum GetSourceState(ALsource *source, Voice *voice)
- {
- if(!voice && source->state == AL_PLAYING)
- source->state = AL_STOPPED;
- return source->state;
- }
- /**
- * Returns if the source should specify an update, given the context's
- * deferring state and the source's last known state.
- */
- inline bool SourceShouldUpdate(ALsource *source, ALCcontext *context)
- {
- return !context->mDeferUpdates.load(std::memory_order_acquire) &&
- IsPlayingOrPaused(source);
- }
- bool EnsureSources(ALCcontext *context, size_t needed)
- {
- size_t count{std::accumulate(context->mSourceList.cbegin(), context->mSourceList.cend(),
- size_t{0},
- [](size_t cur, const SourceSubList &sublist) noexcept -> size_t
- { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })};
- while(needed > count)
- {
- if UNLIKELY(context->mSourceList.size() >= 1<<25)
- return false;
- context->mSourceList.emplace_back();
- auto sublist = context->mSourceList.end() - 1;
- sublist->FreeMask = ~0_u64;
- sublist->Sources = static_cast<ALsource*>(al_calloc(alignof(ALsource), sizeof(ALsource)*64));
- if UNLIKELY(!sublist->Sources)
- {
- context->mSourceList.pop_back();
- return false;
- }
- count += 64;
- }
- return true;
- }
- ALsource *AllocSource(ALCcontext *context)
- {
- auto sublist = std::find_if(context->mSourceList.begin(), context->mSourceList.end(),
- [](const SourceSubList &entry) noexcept -> bool
- { return entry.FreeMask != 0; }
- );
- auto lidx = static_cast<ALuint>(std::distance(context->mSourceList.begin(), sublist));
- auto slidx = static_cast<ALuint>(al::countr_zero(sublist->FreeMask));
- ALsource *source{::new(sublist->Sources + slidx) ALsource{}};
- /* Add 1 to avoid source ID 0. */
- source->id = ((lidx<<6) | slidx) + 1;
- context->mNumSources += 1;
- sublist->FreeMask &= ~(1_u64 << slidx);
- return source;
- }
- void FreeSource(ALCcontext *context, ALsource *source)
- {
- const ALuint id{source->id - 1};
- const size_t lidx{id >> 6};
- const ALuint slidx{id & 0x3f};
- if(IsPlayingOrPaused(source))
- {
- if(Voice *voice{GetSourceVoice(source, context)})
- {
- VoiceChange *vchg{GetVoiceChanger(context)};
- voice->mPendingChange.store(true, std::memory_order_relaxed);
- vchg->mVoice = voice;
- vchg->mSourceID = source->id;
- vchg->mState = VChangeState::Stop;
- SendVoiceChanges(context, vchg);
- }
- }
- al::destroy_at(source);
- context->mSourceList[lidx].FreeMask |= 1_u64 << slidx;
- context->mNumSources--;
- }
- inline ALsource *LookupSource(ALCcontext *context, ALuint id) noexcept
- {
- const size_t lidx{(id-1) >> 6};
- const ALuint slidx{(id-1) & 0x3f};
- if UNLIKELY(lidx >= context->mSourceList.size())
- return nullptr;
- SourceSubList &sublist{context->mSourceList[lidx]};
- if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
- return nullptr;
- return sublist.Sources + slidx;
- }
- inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id) noexcept
- {
- const size_t lidx{(id-1) >> 6};
- const ALuint slidx{(id-1) & 0x3f};
- if UNLIKELY(lidx >= device->BufferList.size())
- return nullptr;
- BufferSubList &sublist = device->BufferList[lidx];
- if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
- return nullptr;
- return sublist.Buffers + slidx;
- }
- inline ALfilter *LookupFilter(ALCdevice *device, ALuint id) noexcept
- {
- const size_t lidx{(id-1) >> 6};
- const ALuint slidx{(id-1) & 0x3f};
- if UNLIKELY(lidx >= device->FilterList.size())
- return nullptr;
- FilterSubList &sublist = device->FilterList[lidx];
- if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
- return nullptr;
- return sublist.Filters + slidx;
- }
- inline ALeffectslot *LookupEffectSlot(ALCcontext *context, ALuint id) noexcept
- {
- const size_t lidx{(id-1) >> 6};
- const ALuint slidx{(id-1) & 0x3f};
- if UNLIKELY(lidx >= context->mEffectSlotList.size())
- return nullptr;
- EffectSlotSubList &sublist{context->mEffectSlotList[lidx]};
- if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
- return nullptr;
- return sublist.EffectSlots + slidx;
- }
- al::optional<SpatializeMode> SpatializeModeFromEnum(ALenum mode)
- {
- switch(mode)
- {
- case AL_FALSE: return al::make_optional(SpatializeMode::Off);
- case AL_TRUE: return al::make_optional(SpatializeMode::On);
- case AL_AUTO_SOFT: return al::make_optional(SpatializeMode::Auto);
- }
- WARN("Unsupported spatialize mode: 0x%04x\n", mode);
- return al::nullopt;
- }
- ALenum EnumFromSpatializeMode(SpatializeMode mode)
- {
- switch(mode)
- {
- case SpatializeMode::Off: return AL_FALSE;
- case SpatializeMode::On: return AL_TRUE;
- case SpatializeMode::Auto: return AL_AUTO_SOFT;
- }
- throw std::runtime_error{"Invalid SpatializeMode: "+std::to_string(int(mode))};
- }
- al::optional<DirectMode> DirectModeFromEnum(ALenum mode)
- {
- switch(mode)
- {
- case AL_FALSE: return al::make_optional(DirectMode::Off);
- case AL_DROP_UNMATCHED_SOFT: return al::make_optional(DirectMode::DropMismatch);
- case AL_REMIX_UNMATCHED_SOFT: return al::make_optional(DirectMode::RemixMismatch);
- }
- WARN("Unsupported direct mode: 0x%04x\n", mode);
- return al::nullopt;
- }
- ALenum EnumFromDirectMode(DirectMode mode)
- {
- switch(mode)
- {
- case DirectMode::Off: return AL_FALSE;
- case DirectMode::DropMismatch: return AL_DROP_UNMATCHED_SOFT;
- case DirectMode::RemixMismatch: return AL_REMIX_UNMATCHED_SOFT;
- }
- throw std::runtime_error{"Invalid DirectMode: "+std::to_string(int(mode))};
- }
- al::optional<DistanceModel> DistanceModelFromALenum(ALenum model)
- {
- switch(model)
- {
- case AL_NONE: return al::make_optional(DistanceModel::Disable);
- case AL_INVERSE_DISTANCE: return al::make_optional(DistanceModel::Inverse);
- case AL_INVERSE_DISTANCE_CLAMPED: return al::make_optional(DistanceModel::InverseClamped);
- case AL_LINEAR_DISTANCE: return al::make_optional(DistanceModel::Linear);
- case AL_LINEAR_DISTANCE_CLAMPED: return al::make_optional(DistanceModel::LinearClamped);
- case AL_EXPONENT_DISTANCE: return al::make_optional(DistanceModel::Exponent);
- case AL_EXPONENT_DISTANCE_CLAMPED: return al::make_optional(DistanceModel::ExponentClamped);
- }
- return al::nullopt;
- }
- ALenum ALenumFromDistanceModel(DistanceModel model)
- {
- switch(model)
- {
- case DistanceModel::Disable: return AL_NONE;
- case DistanceModel::Inverse: return AL_INVERSE_DISTANCE;
- case DistanceModel::InverseClamped: return AL_INVERSE_DISTANCE_CLAMPED;
- case DistanceModel::Linear: return AL_LINEAR_DISTANCE;
- case DistanceModel::LinearClamped: return AL_LINEAR_DISTANCE_CLAMPED;
- case DistanceModel::Exponent: return AL_EXPONENT_DISTANCE;
- case DistanceModel::ExponentClamped: return AL_EXPONENT_DISTANCE_CLAMPED;
- }
- throw std::runtime_error{"Unexpected distance model "+std::to_string(static_cast<int>(model))};
- }
- enum SourceProp : ALenum {
- srcPitch = AL_PITCH,
- srcGain = AL_GAIN,
- srcMinGain = AL_MIN_GAIN,
- srcMaxGain = AL_MAX_GAIN,
- srcMaxDistance = AL_MAX_DISTANCE,
- srcRolloffFactor = AL_ROLLOFF_FACTOR,
- srcDopplerFactor = AL_DOPPLER_FACTOR,
- srcConeOuterGain = AL_CONE_OUTER_GAIN,
- srcSecOffset = AL_SEC_OFFSET,
- srcSampleOffset = AL_SAMPLE_OFFSET,
- srcByteOffset = AL_BYTE_OFFSET,
- srcConeInnerAngle = AL_CONE_INNER_ANGLE,
- srcConeOuterAngle = AL_CONE_OUTER_ANGLE,
- srcRefDistance = AL_REFERENCE_DISTANCE,
- srcPosition = AL_POSITION,
- srcVelocity = AL_VELOCITY,
- srcDirection = AL_DIRECTION,
- srcSourceRelative = AL_SOURCE_RELATIVE,
- srcLooping = AL_LOOPING,
- srcBuffer = AL_BUFFER,
- srcSourceState = AL_SOURCE_STATE,
- srcBuffersQueued = AL_BUFFERS_QUEUED,
- srcBuffersProcessed = AL_BUFFERS_PROCESSED,
- srcSourceType = AL_SOURCE_TYPE,
- /* ALC_EXT_EFX */
- srcConeOuterGainHF = AL_CONE_OUTER_GAINHF,
- srcAirAbsorptionFactor = AL_AIR_ABSORPTION_FACTOR,
- srcRoomRolloffFactor = AL_ROOM_ROLLOFF_FACTOR,
- srcDirectFilterGainHFAuto = AL_DIRECT_FILTER_GAINHF_AUTO,
- srcAuxSendFilterGainAuto = AL_AUXILIARY_SEND_FILTER_GAIN_AUTO,
- srcAuxSendFilterGainHFAuto = AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO,
- srcDirectFilter = AL_DIRECT_FILTER,
- srcAuxSendFilter = AL_AUXILIARY_SEND_FILTER,
- /* AL_SOFT_direct_channels */
- srcDirectChannelsSOFT = AL_DIRECT_CHANNELS_SOFT,
- /* AL_EXT_source_distance_model */
- srcDistanceModel = AL_DISTANCE_MODEL,
- /* AL_SOFT_source_latency */
- srcSampleOffsetLatencySOFT = AL_SAMPLE_OFFSET_LATENCY_SOFT,
- srcSecOffsetLatencySOFT = AL_SEC_OFFSET_LATENCY_SOFT,
- /* AL_EXT_STEREO_ANGLES */
- srcAngles = AL_STEREO_ANGLES,
- /* AL_EXT_SOURCE_RADIUS */
- srcRadius = AL_SOURCE_RADIUS,
- /* AL_EXT_BFORMAT */
- srcOrientation = AL_ORIENTATION,
- /* AL_SOFT_source_resampler */
- srcResampler = AL_SOURCE_RESAMPLER_SOFT,
- /* AL_SOFT_source_spatialize */
- srcSpatialize = AL_SOURCE_SPATIALIZE_SOFT,
- /* ALC_SOFT_device_clock */
- srcSampleOffsetClockSOFT = AL_SAMPLE_OFFSET_CLOCK_SOFT,
- srcSecOffsetClockSOFT = AL_SEC_OFFSET_CLOCK_SOFT,
- };
- constexpr size_t MaxValues{6u};
- ALuint FloatValsByProp(ALenum prop)
- {
- switch(static_cast<SourceProp>(prop))
- {
- case AL_PITCH:
- case AL_GAIN:
- case AL_MIN_GAIN:
- case AL_MAX_GAIN:
- case AL_MAX_DISTANCE:
- case AL_ROLLOFF_FACTOR:
- case AL_DOPPLER_FACTOR:
- case AL_CONE_OUTER_GAIN:
- case AL_SEC_OFFSET:
- case AL_SAMPLE_OFFSET:
- case AL_BYTE_OFFSET:
- case AL_CONE_INNER_ANGLE:
- case AL_CONE_OUTER_ANGLE:
- case AL_REFERENCE_DISTANCE:
- case AL_CONE_OUTER_GAINHF:
- case AL_AIR_ABSORPTION_FACTOR:
- case AL_ROOM_ROLLOFF_FACTOR:
- case AL_DIRECT_FILTER_GAINHF_AUTO:
- case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
- case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
- case AL_DIRECT_CHANNELS_SOFT:
- case AL_DISTANCE_MODEL:
- case AL_SOURCE_RELATIVE:
- case AL_LOOPING:
- case AL_SOURCE_STATE:
- case AL_BUFFERS_QUEUED:
- case AL_BUFFERS_PROCESSED:
- case AL_SOURCE_TYPE:
- case AL_SOURCE_RADIUS:
- case AL_SOURCE_RESAMPLER_SOFT:
- case AL_SOURCE_SPATIALIZE_SOFT:
- return 1;
- case AL_STEREO_ANGLES:
- return 2;
- case AL_POSITION:
- case AL_VELOCITY:
- case AL_DIRECTION:
- return 3;
- case AL_ORIENTATION:
- return 6;
- case AL_SEC_OFFSET_LATENCY_SOFT:
- case AL_SEC_OFFSET_CLOCK_SOFT:
- break; /* Double only */
- case AL_BUFFER:
- case AL_DIRECT_FILTER:
- case AL_AUXILIARY_SEND_FILTER:
- break; /* i/i64 only */
- case AL_SAMPLE_OFFSET_LATENCY_SOFT:
- case AL_SAMPLE_OFFSET_CLOCK_SOFT:
- break; /* i64 only */
- }
- return 0;
- }
- ALuint DoubleValsByProp(ALenum prop)
- {
- switch(static_cast<SourceProp>(prop))
- {
- case AL_PITCH:
- case AL_GAIN:
- case AL_MIN_GAIN:
- case AL_MAX_GAIN:
- case AL_MAX_DISTANCE:
- case AL_ROLLOFF_FACTOR:
- case AL_DOPPLER_FACTOR:
- case AL_CONE_OUTER_GAIN:
- case AL_SEC_OFFSET:
- case AL_SAMPLE_OFFSET:
- case AL_BYTE_OFFSET:
- case AL_CONE_INNER_ANGLE:
- case AL_CONE_OUTER_ANGLE:
- case AL_REFERENCE_DISTANCE:
- case AL_CONE_OUTER_GAINHF:
- case AL_AIR_ABSORPTION_FACTOR:
- case AL_ROOM_ROLLOFF_FACTOR:
- case AL_DIRECT_FILTER_GAINHF_AUTO:
- case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
- case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
- case AL_DIRECT_CHANNELS_SOFT:
- case AL_DISTANCE_MODEL:
- case AL_SOURCE_RELATIVE:
- case AL_LOOPING:
- case AL_SOURCE_STATE:
- case AL_BUFFERS_QUEUED:
- case AL_BUFFERS_PROCESSED:
- case AL_SOURCE_TYPE:
- case AL_SOURCE_RADIUS:
- case AL_SOURCE_RESAMPLER_SOFT:
- case AL_SOURCE_SPATIALIZE_SOFT:
- return 1;
- case AL_SEC_OFFSET_LATENCY_SOFT:
- case AL_SEC_OFFSET_CLOCK_SOFT:
- case AL_STEREO_ANGLES:
- return 2;
- case AL_POSITION:
- case AL_VELOCITY:
- case AL_DIRECTION:
- return 3;
- case AL_ORIENTATION:
- return 6;
- case AL_BUFFER:
- case AL_DIRECT_FILTER:
- case AL_AUXILIARY_SEND_FILTER:
- break; /* i/i64 only */
- case AL_SAMPLE_OFFSET_LATENCY_SOFT:
- case AL_SAMPLE_OFFSET_CLOCK_SOFT:
- break; /* i64 only */
- }
- return 0;
- }
- bool SetSourcefv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<const float> values);
- bool SetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<const int> values);
- bool SetSourcei64v(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<const int64_t> values);
- #define CHECKSIZE(v, s) do { \
- if LIKELY((v).size() == (s) || (v).size() == MaxValues) break; \
- Context->setError(AL_INVALID_ENUM, \
- "Property 0x%04x expects %d value(s), got %zu", prop, (s), \
- (v).size()); \
- return false; \
- } while(0)
- #define CHECKVAL(x) do { \
- if LIKELY(x) break; \
- Context->setError(AL_INVALID_VALUE, "Value out of range"); \
- return false; \
- } while(0)
- bool UpdateSourceProps(ALsource *source, ALCcontext *context)
- {
- Voice *voice;
- if(SourceShouldUpdate(source, context) && (voice=GetSourceVoice(source, context)) != nullptr)
- UpdateSourceProps(source, voice, context);
- else
- source->PropsClean.clear(std::memory_order_release);
- return true;
- }
- bool SetSourcefv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<const float> values)
- {
- int ival;
- switch(prop)
- {
- case AL_SEC_OFFSET_LATENCY_SOFT:
- case AL_SEC_OFFSET_CLOCK_SOFT:
- /* Query only */
- SETERR_RETURN(Context, AL_INVALID_OPERATION, false,
- "Setting read-only source property 0x%04x", prop);
- case AL_PITCH:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f);
- Source->Pitch = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_CONE_INNER_ANGLE:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f && values[0] <= 360.0f);
- Source->InnerAngle = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_CONE_OUTER_ANGLE:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f && values[0] <= 360.0f);
- Source->OuterAngle = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_GAIN:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f);
- Source->Gain = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_MAX_DISTANCE:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f);
- Source->MaxDistance = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_ROLLOFF_FACTOR:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f);
- Source->RolloffFactor = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_REFERENCE_DISTANCE:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f);
- Source->RefDistance = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_MIN_GAIN:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f);
- Source->MinGain = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_MAX_GAIN:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f);
- Source->MaxGain = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_CONE_OUTER_GAIN:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f && values[0] <= 1.0f);
- Source->OuterGain = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_CONE_OUTER_GAINHF:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f && values[0] <= 1.0f);
- Source->OuterGainHF = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_AIR_ABSORPTION_FACTOR:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f && values[0] <= 10.0f);
- Source->AirAbsorptionFactor = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_ROOM_ROLLOFF_FACTOR:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f && values[0] <= 10.0f);
- Source->RoomRolloffFactor = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_DOPPLER_FACTOR:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f && values[0] <= 1.0f);
- Source->DopplerFactor = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_SEC_OFFSET:
- case AL_SAMPLE_OFFSET:
- case AL_BYTE_OFFSET:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f);
- if(Voice *voice{GetSourceVoice(Source, Context)})
- {
- if((voice->mFlags&VoiceIsCallback))
- SETERR_RETURN(Context, AL_INVALID_VALUE, false,
- "Source offset for callback is invalid");
- auto vpos = GetSampleOffset(Source->mQueue, prop, values[0]);
- if(!vpos) SETERR_RETURN(Context, AL_INVALID_VALUE, false, "Invalid offset");
- if(SetVoiceOffset(voice, *vpos, Source, Context, Context->mDevice.get()))
- return true;
- }
- Source->OffsetType = prop;
- Source->Offset = values[0];
- return true;
- case AL_SOURCE_RADIUS:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0.0f && std::isfinite(values[0]));
- Source->Radius = values[0];
- return UpdateSourceProps(Source, Context);
- case AL_STEREO_ANGLES:
- CHECKSIZE(values, 2);
- CHECKVAL(std::isfinite(values[0]) && std::isfinite(values[1]));
- Source->StereoPan[0] = values[0];
- Source->StereoPan[1] = values[1];
- return UpdateSourceProps(Source, Context);
- case AL_POSITION:
- CHECKSIZE(values, 3);
- CHECKVAL(std::isfinite(values[0]) && std::isfinite(values[1]) && std::isfinite(values[2]));
- Source->Position[0] = values[0];
- Source->Position[1] = values[1];
- Source->Position[2] = values[2];
- return UpdateSourceProps(Source, Context);
- case AL_VELOCITY:
- CHECKSIZE(values, 3);
- CHECKVAL(std::isfinite(values[0]) && std::isfinite(values[1]) && std::isfinite(values[2]));
- Source->Velocity[0] = values[0];
- Source->Velocity[1] = values[1];
- Source->Velocity[2] = values[2];
- return UpdateSourceProps(Source, Context);
- case AL_DIRECTION:
- CHECKSIZE(values, 3);
- CHECKVAL(std::isfinite(values[0]) && std::isfinite(values[1]) && std::isfinite(values[2]));
- Source->Direction[0] = values[0];
- Source->Direction[1] = values[1];
- Source->Direction[2] = values[2];
- return UpdateSourceProps(Source, Context);
- case AL_ORIENTATION:
- CHECKSIZE(values, 6);
- CHECKVAL(std::isfinite(values[0]) && std::isfinite(values[1]) && std::isfinite(values[2])
- && std::isfinite(values[3]) && std::isfinite(values[4]) && std::isfinite(values[5]));
- Source->OrientAt[0] = values[0];
- Source->OrientAt[1] = values[1];
- Source->OrientAt[2] = values[2];
- Source->OrientUp[0] = values[3];
- Source->OrientUp[1] = values[4];
- Source->OrientUp[2] = values[5];
- return UpdateSourceProps(Source, Context);
- case AL_SOURCE_RELATIVE:
- case AL_LOOPING:
- case AL_SOURCE_STATE:
- case AL_SOURCE_TYPE:
- case AL_DISTANCE_MODEL:
- case AL_DIRECT_FILTER_GAINHF_AUTO:
- case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
- case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
- case AL_DIRECT_CHANNELS_SOFT:
- case AL_SOURCE_RESAMPLER_SOFT:
- case AL_SOURCE_SPATIALIZE_SOFT:
- CHECKSIZE(values, 1);
- ival = static_cast<int>(values[0]);
- return SetSourceiv(Source, Context, prop, {&ival, 1u});
- case AL_BUFFERS_QUEUED:
- case AL_BUFFERS_PROCESSED:
- CHECKSIZE(values, 1);
- ival = static_cast<int>(static_cast<ALuint>(values[0]));
- return SetSourceiv(Source, Context, prop, {&ival, 1u});
- case AL_BUFFER:
- case AL_DIRECT_FILTER:
- case AL_AUXILIARY_SEND_FILTER:
- case AL_SAMPLE_OFFSET_LATENCY_SOFT:
- case AL_SAMPLE_OFFSET_CLOCK_SOFT:
- break;
- }
- ERR("Unexpected property: 0x%04x\n", prop);
- Context->setError(AL_INVALID_ENUM, "Invalid source float property 0x%04x", prop);
- return false;
- }
- bool SetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<const int> values)
- {
- ALCdevice *device{Context->mDevice.get()};
- ALeffectslot *slot{nullptr};
- al::deque<ALbufferQueueItem> oldlist;
- std::unique_lock<std::mutex> slotlock;
- float fvals[6];
- switch(prop)
- {
- case AL_SOURCE_STATE:
- case AL_SOURCE_TYPE:
- case AL_BUFFERS_QUEUED:
- case AL_BUFFERS_PROCESSED:
- /* Query only */
- SETERR_RETURN(Context, AL_INVALID_OPERATION, false,
- "Setting read-only source property 0x%04x", prop);
- case AL_SOURCE_RELATIVE:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] == AL_FALSE || values[0] == AL_TRUE);
- Source->HeadRelative = values[0] != AL_FALSE;
- return UpdateSourceProps(Source, Context);
- case AL_LOOPING:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] == AL_FALSE || values[0] == AL_TRUE);
- Source->Looping = values[0] != AL_FALSE;
- if(IsPlayingOrPaused(Source))
- {
- if(Voice *voice{GetSourceVoice(Source, Context)})
- {
- if(Source->Looping)
- voice->mLoopBuffer.store(&Source->mQueue.front(), std::memory_order_release);
- else
- voice->mLoopBuffer.store(nullptr, std::memory_order_release);
- /* If the source is playing, wait for the current mix to finish
- * to ensure it isn't currently looping back or reaching the
- * end.
- */
- device->waitForMix();
- }
- }
- return true;
- case AL_BUFFER:
- CHECKSIZE(values, 1);
- {
- const ALenum state{GetSourceState(Source, GetSourceVoice(Source, Context))};
- if(state == AL_PLAYING || state == AL_PAUSED)
- SETERR_RETURN(Context, AL_INVALID_OPERATION, false,
- "Setting buffer on playing or paused source %u", Source->id);
- }
- if(values[0])
- {
- std::lock_guard<std::mutex> _{device->BufferLock};
- ALbuffer *buffer{LookupBuffer(device, static_cast<ALuint>(values[0]))};
- if(!buffer)
- SETERR_RETURN(Context, AL_INVALID_VALUE, false, "Invalid buffer ID %u",
- static_cast<ALuint>(values[0]));
- if(buffer->MappedAccess && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT))
- SETERR_RETURN(Context, AL_INVALID_OPERATION, false,
- "Setting non-persistently mapped buffer %u", buffer->id);
- if(buffer->mCallback && ReadRef(buffer->ref) != 0)
- SETERR_RETURN(Context, AL_INVALID_OPERATION, false,
- "Setting already-set callback buffer %u", buffer->id);
- /* Add the selected buffer to a one-item queue */
- al::deque<ALbufferQueueItem> newlist;
- newlist.emplace_back();
- newlist.back().mCallback = buffer->mCallback;
- newlist.back().mUserData = buffer->mUserData;
- newlist.back().mSampleLen = buffer->mSampleLen;
- newlist.back().mLoopStart = buffer->mLoopStart;
- newlist.back().mLoopEnd = buffer->mLoopEnd;
- newlist.back().mSamples = buffer->mData.data();
- newlist.back().mBuffer = buffer;
- IncrementRef(buffer->ref);
- /* Source is now Static */
- Source->SourceType = AL_STATIC;
- Source->mQueue.swap(oldlist);
- Source->mQueue.swap(newlist);
- }
- else
- {
- /* Source is now Undetermined */
- Source->SourceType = AL_UNDETERMINED;
- Source->mQueue.swap(oldlist);
- }
- /* Delete all elements in the previous queue */
- for(auto &item : oldlist)
- {
- if(ALbuffer *buffer{item.mBuffer})
- DecrementRef(buffer->ref);
- }
- return true;
- case AL_SEC_OFFSET:
- case AL_SAMPLE_OFFSET:
- case AL_BYTE_OFFSET:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0);
- if(Voice *voice{GetSourceVoice(Source, Context)})
- {
- if((voice->mFlags&VoiceIsCallback))
- SETERR_RETURN(Context, AL_INVALID_VALUE, false,
- "Source offset for callback is invalid");
- auto vpos = GetSampleOffset(Source->mQueue, prop, values[0]);
- if(!vpos) SETERR_RETURN(Context, AL_INVALID_VALUE, false, "Invalid source offset");
- if(SetVoiceOffset(voice, *vpos, Source, Context, device))
- return true;
- }
- Source->OffsetType = prop;
- Source->Offset = values[0];
- return true;
- case AL_DIRECT_FILTER:
- CHECKSIZE(values, 1);
- if(values[0])
- {
- std::lock_guard<std::mutex> _{device->FilterLock};
- ALfilter *filter{LookupFilter(device, static_cast<ALuint>(values[0]))};
- if(!filter)
- SETERR_RETURN(Context, AL_INVALID_VALUE, false, "Invalid filter ID %u",
- static_cast<ALuint>(values[0]));
- Source->Direct.Gain = filter->Gain;
- Source->Direct.GainHF = filter->GainHF;
- Source->Direct.HFReference = filter->HFReference;
- Source->Direct.GainLF = filter->GainLF;
- Source->Direct.LFReference = filter->LFReference;
- }
- else
- {
- Source->Direct.Gain = 1.0f;
- Source->Direct.GainHF = 1.0f;
- Source->Direct.HFReference = LOWPASSFREQREF;
- Source->Direct.GainLF = 1.0f;
- Source->Direct.LFReference = HIGHPASSFREQREF;
- }
- return UpdateSourceProps(Source, Context);
- case AL_DIRECT_FILTER_GAINHF_AUTO:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] == AL_FALSE || values[0] == AL_TRUE);
- Source->DryGainHFAuto = values[0] != AL_FALSE;
- return UpdateSourceProps(Source, Context);
- case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] == AL_FALSE || values[0] == AL_TRUE);
- Source->WetGainAuto = values[0] != AL_FALSE;
- return UpdateSourceProps(Source, Context);
- case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] == AL_FALSE || values[0] == AL_TRUE);
- Source->WetGainHFAuto = values[0] != AL_FALSE;
- return UpdateSourceProps(Source, Context);
- case AL_DIRECT_CHANNELS_SOFT:
- CHECKSIZE(values, 1);
- if(auto mode = DirectModeFromEnum(values[0]))
- {
- Source->DirectChannels = *mode;
- return UpdateSourceProps(Source, Context);
- }
- Context->setError(AL_INVALID_VALUE, "Unsupported AL_DIRECT_CHANNELS_SOFT: 0x%04x\n",
- values[0]);
- return false;
- case AL_DISTANCE_MODEL:
- CHECKSIZE(values, 1);
- if(auto model = DistanceModelFromALenum(values[0]))
- {
- Source->mDistanceModel = *model;
- if(Context->mSourceDistanceModel)
- return UpdateSourceProps(Source, Context);
- return true;
- }
- Context->setError(AL_INVALID_VALUE, "Distance model out of range: 0x%04x", values[0]);
- return false;
- case AL_SOURCE_RESAMPLER_SOFT:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] >= 0 && values[0] <= static_cast<int>(Resampler::Max));
- Source->mResampler = static_cast<Resampler>(values[0]);
- return UpdateSourceProps(Source, Context);
- case AL_SOURCE_SPATIALIZE_SOFT:
- CHECKSIZE(values, 1);
- if(auto mode = SpatializeModeFromEnum(values[0]))
- {
- Source->mSpatialize = *mode;
- return UpdateSourceProps(Source, Context);
- }
- Context->setError(AL_INVALID_VALUE, "Unsupported AL_SOURCE_SPATIALIZE_SOFT: 0x%04x\n",
- values[0]);
- return false;
- case AL_AUXILIARY_SEND_FILTER:
- CHECKSIZE(values, 3);
- slotlock = std::unique_lock<std::mutex>{Context->mEffectSlotLock};
- if(values[0] && (slot=LookupEffectSlot(Context, static_cast<ALuint>(values[0]))) == nullptr)
- SETERR_RETURN(Context, AL_INVALID_VALUE, false, "Invalid effect ID %u", values[0]);
- if(static_cast<ALuint>(values[1]) >= device->NumAuxSends)
- SETERR_RETURN(Context, AL_INVALID_VALUE, false, "Invalid send %u", values[1]);
- if(values[2])
- {
- std::lock_guard<std::mutex> _{device->FilterLock};
- ALfilter *filter{LookupFilter(device, static_cast<ALuint>(values[2]))};
- if(!filter)
- SETERR_RETURN(Context, AL_INVALID_VALUE, false, "Invalid filter ID %u", values[2]);
- auto &send = Source->Send[static_cast<ALuint>(values[1])];
- send.Gain = filter->Gain;
- send.GainHF = filter->GainHF;
- send.HFReference = filter->HFReference;
- send.GainLF = filter->GainLF;
- send.LFReference = filter->LFReference;
- }
- else
- {
- /* Disable filter */
- auto &send = Source->Send[static_cast<ALuint>(values[1])];
- send.Gain = 1.0f;
- send.GainHF = 1.0f;
- send.HFReference = LOWPASSFREQREF;
- send.GainLF = 1.0f;
- send.LFReference = HIGHPASSFREQREF;
- }
- if(slot != Source->Send[static_cast<ALuint>(values[1])].Slot && IsPlayingOrPaused(Source))
- {
- /* Add refcount on the new slot, and release the previous slot */
- if(slot) IncrementRef(slot->ref);
- if(auto *oldslot = Source->Send[static_cast<ALuint>(values[1])].Slot)
- DecrementRef(oldslot->ref);
- Source->Send[static_cast<ALuint>(values[1])].Slot = slot;
- /* We must force an update if the auxiliary slot changed on an
- * active source, in case the slot is about to be deleted.
- */
- Voice *voice{GetSourceVoice(Source, Context)};
- if(voice) UpdateSourceProps(Source, voice, Context);
- else Source->PropsClean.clear(std::memory_order_release);
- }
- else
- {
- if(slot) IncrementRef(slot->ref);
- if(auto *oldslot = Source->Send[static_cast<ALuint>(values[1])].Slot)
- DecrementRef(oldslot->ref);
- Source->Send[static_cast<ALuint>(values[1])].Slot = slot;
- UpdateSourceProps(Source, Context);
- }
- return true;
- /* 1x float */
- case AL_CONE_INNER_ANGLE:
- case AL_CONE_OUTER_ANGLE:
- case AL_PITCH:
- case AL_GAIN:
- case AL_MIN_GAIN:
- case AL_MAX_GAIN:
- case AL_REFERENCE_DISTANCE:
- case AL_ROLLOFF_FACTOR:
- case AL_CONE_OUTER_GAIN:
- case AL_MAX_DISTANCE:
- case AL_DOPPLER_FACTOR:
- case AL_CONE_OUTER_GAINHF:
- case AL_AIR_ABSORPTION_FACTOR:
- case AL_ROOM_ROLLOFF_FACTOR:
- case AL_SOURCE_RADIUS:
- CHECKSIZE(values, 1);
- fvals[0] = static_cast<float>(values[0]);
- return SetSourcefv(Source, Context, prop, {fvals, 1u});
- /* 3x float */
- case AL_POSITION:
- case AL_VELOCITY:
- case AL_DIRECTION:
- CHECKSIZE(values, 3);
- fvals[0] = static_cast<float>(values[0]);
- fvals[1] = static_cast<float>(values[1]);
- fvals[2] = static_cast<float>(values[2]);
- return SetSourcefv(Source, Context, prop, {fvals, 3u});
- /* 6x float */
- case AL_ORIENTATION:
- CHECKSIZE(values, 6);
- fvals[0] = static_cast<float>(values[0]);
- fvals[1] = static_cast<float>(values[1]);
- fvals[2] = static_cast<float>(values[2]);
- fvals[3] = static_cast<float>(values[3]);
- fvals[4] = static_cast<float>(values[4]);
- fvals[5] = static_cast<float>(values[5]);
- return SetSourcefv(Source, Context, prop, {fvals, 6u});
- case AL_SAMPLE_OFFSET_LATENCY_SOFT:
- case AL_SEC_OFFSET_LATENCY_SOFT:
- case AL_SEC_OFFSET_CLOCK_SOFT:
- case AL_SAMPLE_OFFSET_CLOCK_SOFT:
- case AL_STEREO_ANGLES:
- break;
- }
- ERR("Unexpected property: 0x%04x\n", prop);
- Context->setError(AL_INVALID_ENUM, "Invalid source integer property 0x%04x", prop);
- return false;
- }
- bool SetSourcei64v(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<const int64_t> values)
- {
- float fvals[MaxValues];
- int ivals[MaxValues];
- switch(prop)
- {
- case AL_SOURCE_TYPE:
- case AL_BUFFERS_QUEUED:
- case AL_BUFFERS_PROCESSED:
- case AL_SOURCE_STATE:
- case AL_SAMPLE_OFFSET_LATENCY_SOFT:
- case AL_SAMPLE_OFFSET_CLOCK_SOFT:
- /* Query only */
- SETERR_RETURN(Context, AL_INVALID_OPERATION, false,
- "Setting read-only source property 0x%04x", prop);
- /* 1x int */
- case AL_SOURCE_RELATIVE:
- case AL_LOOPING:
- case AL_SEC_OFFSET:
- case AL_SAMPLE_OFFSET:
- case AL_BYTE_OFFSET:
- case AL_DIRECT_FILTER_GAINHF_AUTO:
- case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
- case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
- case AL_DIRECT_CHANNELS_SOFT:
- case AL_DISTANCE_MODEL:
- case AL_SOURCE_RESAMPLER_SOFT:
- case AL_SOURCE_SPATIALIZE_SOFT:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] <= INT_MAX && values[0] >= INT_MIN);
- ivals[0] = static_cast<int>(values[0]);
- return SetSourceiv(Source, Context, prop, {ivals, 1u});
- /* 1x uint */
- case AL_BUFFER:
- case AL_DIRECT_FILTER:
- CHECKSIZE(values, 1);
- CHECKVAL(values[0] <= UINT_MAX && values[0] >= 0);
- ivals[0] = static_cast<int>(values[0]);
- return SetSourceiv(Source, Context, prop, {ivals, 1u});
- /* 3x uint */
- case AL_AUXILIARY_SEND_FILTER:
- CHECKSIZE(values, 3);
- CHECKVAL(values[0] <= UINT_MAX && values[0] >= 0 && values[1] <= UINT_MAX && values[1] >= 0
- && values[2] <= UINT_MAX && values[2] >= 0);
- ivals[0] = static_cast<int>(values[0]);
- ivals[1] = static_cast<int>(values[1]);
- ivals[2] = static_cast<int>(values[2]);
- return SetSourceiv(Source, Context, prop, {ivals, 3u});
- /* 1x float */
- case AL_CONE_INNER_ANGLE:
- case AL_CONE_OUTER_ANGLE:
- case AL_PITCH:
- case AL_GAIN:
- case AL_MIN_GAIN:
- case AL_MAX_GAIN:
- case AL_REFERENCE_DISTANCE:
- case AL_ROLLOFF_FACTOR:
- case AL_CONE_OUTER_GAIN:
- case AL_MAX_DISTANCE:
- case AL_DOPPLER_FACTOR:
- case AL_CONE_OUTER_GAINHF:
- case AL_AIR_ABSORPTION_FACTOR:
- case AL_ROOM_ROLLOFF_FACTOR:
- case AL_SOURCE_RADIUS:
- CHECKSIZE(values, 1);
- fvals[0] = static_cast<float>(values[0]);
- return SetSourcefv(Source, Context, prop, {fvals, 1u});
- /* 3x float */
- case AL_POSITION:
- case AL_VELOCITY:
- case AL_DIRECTION:
- CHECKSIZE(values, 3);
- fvals[0] = static_cast<float>(values[0]);
- fvals[1] = static_cast<float>(values[1]);
- fvals[2] = static_cast<float>(values[2]);
- return SetSourcefv(Source, Context, prop, {fvals, 3u});
- /* 6x float */
- case AL_ORIENTATION:
- CHECKSIZE(values, 6);
- fvals[0] = static_cast<float>(values[0]);
- fvals[1] = static_cast<float>(values[1]);
- fvals[2] = static_cast<float>(values[2]);
- fvals[3] = static_cast<float>(values[3]);
- fvals[4] = static_cast<float>(values[4]);
- fvals[5] = static_cast<float>(values[5]);
- return SetSourcefv(Source, Context, prop, {fvals, 6u});
- case AL_SEC_OFFSET_LATENCY_SOFT:
- case AL_SEC_OFFSET_CLOCK_SOFT:
- case AL_STEREO_ANGLES:
- break;
- }
- ERR("Unexpected property: 0x%04x\n", prop);
- Context->setError(AL_INVALID_ENUM, "Invalid source integer64 property 0x%04x", prop);
- return false;
- }
- #undef CHECKVAL
- bool GetSourcedv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<double> values);
- bool GetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<int> values);
- bool GetSourcei64v(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<int64_t> values);
- bool GetSourcedv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<double> values)
- {
- ALCdevice *device{Context->mDevice.get()};
- ClockLatency clocktime;
- nanoseconds srcclock;
- int ivals[MaxValues];
- bool err;
- switch(prop)
- {
- case AL_GAIN:
- CHECKSIZE(values, 1);
- values[0] = Source->Gain;
- return true;
- case AL_PITCH:
- CHECKSIZE(values, 1);
- values[0] = Source->Pitch;
- return true;
- case AL_MAX_DISTANCE:
- CHECKSIZE(values, 1);
- values[0] = Source->MaxDistance;
- return true;
- case AL_ROLLOFF_FACTOR:
- CHECKSIZE(values, 1);
- values[0] = Source->RolloffFactor;
- return true;
- case AL_REFERENCE_DISTANCE:
- CHECKSIZE(values, 1);
- values[0] = Source->RefDistance;
- return true;
- case AL_CONE_INNER_ANGLE:
- CHECKSIZE(values, 1);
- values[0] = Source->InnerAngle;
- return true;
- case AL_CONE_OUTER_ANGLE:
- CHECKSIZE(values, 1);
- values[0] = Source->OuterAngle;
- return true;
- case AL_MIN_GAIN:
- CHECKSIZE(values, 1);
- values[0] = Source->MinGain;
- return true;
- case AL_MAX_GAIN:
- CHECKSIZE(values, 1);
- values[0] = Source->MaxGain;
- return true;
- case AL_CONE_OUTER_GAIN:
- CHECKSIZE(values, 1);
- values[0] = Source->OuterGain;
- return true;
- case AL_SEC_OFFSET:
- case AL_SAMPLE_OFFSET:
- case AL_BYTE_OFFSET:
- CHECKSIZE(values, 1);
- values[0] = GetSourceOffset(Source, prop, Context);
- return true;
- case AL_CONE_OUTER_GAINHF:
- CHECKSIZE(values, 1);
- values[0] = Source->OuterGainHF;
- return true;
- case AL_AIR_ABSORPTION_FACTOR:
- CHECKSIZE(values, 1);
- values[0] = Source->AirAbsorptionFactor;
- return true;
- case AL_ROOM_ROLLOFF_FACTOR:
- CHECKSIZE(values, 1);
- values[0] = Source->RoomRolloffFactor;
- return true;
- case AL_DOPPLER_FACTOR:
- CHECKSIZE(values, 1);
- values[0] = Source->DopplerFactor;
- return true;
- case AL_SOURCE_RADIUS:
- CHECKSIZE(values, 1);
- values[0] = Source->Radius;
- return true;
- case AL_STEREO_ANGLES:
- CHECKSIZE(values, 2);
- values[0] = Source->StereoPan[0];
- values[1] = Source->StereoPan[1];
- return true;
- case AL_SEC_OFFSET_LATENCY_SOFT:
- CHECKSIZE(values, 2);
- /* Get the source offset with the clock time first. Then get the clock
- * time with the device latency. Order is important.
- */
- values[0] = GetSourceSecOffset(Source, Context, &srcclock);
- {
- std::lock_guard<std::mutex> _{device->StateLock};
- clocktime = GetClockLatency(device);
- }
- if(srcclock == clocktime.ClockTime)
- values[1] = static_cast<double>(clocktime.Latency.count()) / 1000000000.0;
- else
- {
- /* If the clock time incremented, reduce the latency by that much
- * since it's that much closer to the source offset it got earlier.
- */
- const nanoseconds diff{clocktime.ClockTime - srcclock};
- const nanoseconds latency{clocktime.Latency - std::min(clocktime.Latency, diff)};
- values[1] = static_cast<double>(latency.count()) / 1000000000.0;
- }
- return true;
- case AL_SEC_OFFSET_CLOCK_SOFT:
- CHECKSIZE(values, 2);
- values[0] = GetSourceSecOffset(Source, Context, &srcclock);
- values[1] = static_cast<double>(srcclock.count()) / 1000000000.0;
- return true;
- case AL_POSITION:
- CHECKSIZE(values, 3);
- values[0] = Source->Position[0];
- values[1] = Source->Position[1];
- values[2] = Source->Position[2];
- return true;
- case AL_VELOCITY:
- CHECKSIZE(values, 3);
- values[0] = Source->Velocity[0];
- values[1] = Source->Velocity[1];
- values[2] = Source->Velocity[2];
- return true;
- case AL_DIRECTION:
- CHECKSIZE(values, 3);
- values[0] = Source->Direction[0];
- values[1] = Source->Direction[1];
- values[2] = Source->Direction[2];
- return true;
- case AL_ORIENTATION:
- CHECKSIZE(values, 6);
- values[0] = Source->OrientAt[0];
- values[1] = Source->OrientAt[1];
- values[2] = Source->OrientAt[2];
- values[3] = Source->OrientUp[0];
- values[4] = Source->OrientUp[1];
- values[5] = Source->OrientUp[2];
- return true;
- /* 1x int */
- case AL_SOURCE_RELATIVE:
- case AL_LOOPING:
- case AL_SOURCE_STATE:
- case AL_BUFFERS_QUEUED:
- case AL_BUFFERS_PROCESSED:
- case AL_SOURCE_TYPE:
- case AL_DIRECT_FILTER_GAINHF_AUTO:
- case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
- case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
- case AL_DIRECT_CHANNELS_SOFT:
- case AL_DISTANCE_MODEL:
- case AL_SOURCE_RESAMPLER_SOFT:
- case AL_SOURCE_SPATIALIZE_SOFT:
- CHECKSIZE(values, 1);
- if((err=GetSourceiv(Source, Context, prop, {ivals, 1u})) != false)
- values[0] = static_cast<double>(ivals[0]);
- return err;
- case AL_BUFFER:
- case AL_DIRECT_FILTER:
- case AL_AUXILIARY_SEND_FILTER:
- case AL_SAMPLE_OFFSET_LATENCY_SOFT:
- case AL_SAMPLE_OFFSET_CLOCK_SOFT:
- break;
- }
- ERR("Unexpected property: 0x%04x\n", prop);
- Context->setError(AL_INVALID_ENUM, "Invalid source double property 0x%04x", prop);
- return false;
- }
- bool GetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<int> values)
- {
- double dvals[MaxValues];
- bool err;
- switch(prop)
- {
- case AL_SOURCE_RELATIVE:
- CHECKSIZE(values, 1);
- values[0] = Source->HeadRelative;
- return true;
- case AL_LOOPING:
- CHECKSIZE(values, 1);
- values[0] = Source->Looping;
- return true;
- case AL_BUFFER:
- CHECKSIZE(values, 1);
- {
- ALbufferQueueItem *BufferList{(Source->SourceType == AL_STATIC)
- ? &Source->mQueue.front() : nullptr};
- ALbuffer *buffer{BufferList ? BufferList->mBuffer : nullptr};
- values[0] = buffer ? static_cast<int>(buffer->id) : 0;
- }
- return true;
- case AL_SOURCE_STATE:
- CHECKSIZE(values, 1);
- values[0] = GetSourceState(Source, GetSourceVoice(Source, Context));
- return true;
- case AL_BUFFERS_QUEUED:
- CHECKSIZE(values, 1);
- values[0] = static_cast<int>(Source->mQueue.size());
- return true;
- case AL_BUFFERS_PROCESSED:
- CHECKSIZE(values, 1);
- if(Source->Looping || Source->SourceType != AL_STREAMING)
- {
- /* Buffers on a looping source are in a perpetual state of PENDING,
- * so don't report any as PROCESSED
- */
- values[0] = 0;
- }
- else
- {
- int played{0};
- if(Source->state != AL_INITIAL)
- {
- const VoiceBufferItem *Current{nullptr};
- if(Voice *voice{GetSourceVoice(Source, Context)})
- Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
- for(auto &item : Source->mQueue)
- {
- if(&item == Current)
- break;
- ++played;
- }
- }
- values[0] = played;
- }
- return true;
- case AL_SOURCE_TYPE:
- CHECKSIZE(values, 1);
- values[0] = Source->SourceType;
- return true;
- case AL_DIRECT_FILTER_GAINHF_AUTO:
- CHECKSIZE(values, 1);
- values[0] = Source->DryGainHFAuto;
- return true;
- case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
- CHECKSIZE(values, 1);
- values[0] = Source->WetGainAuto;
- return true;
- case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
- CHECKSIZE(values, 1);
- values[0] = Source->WetGainHFAuto;
- return true;
- case AL_DIRECT_CHANNELS_SOFT:
- CHECKSIZE(values, 1);
- values[0] = EnumFromDirectMode(Source->DirectChannels);
- return true;
- case AL_DISTANCE_MODEL:
- CHECKSIZE(values, 1);
- values[0] = ALenumFromDistanceModel(Source->mDistanceModel);
- return true;
- case AL_SOURCE_RESAMPLER_SOFT:
- CHECKSIZE(values, 1);
- values[0] = static_cast<int>(Source->mResampler);
- return true;
- case AL_SOURCE_SPATIALIZE_SOFT:
- CHECKSIZE(values, 1);
- values[0] = EnumFromSpatializeMode(Source->mSpatialize);
- return true;
- /* 1x float/double */
- case AL_CONE_INNER_ANGLE:
- case AL_CONE_OUTER_ANGLE:
- case AL_PITCH:
- case AL_GAIN:
- case AL_MIN_GAIN:
- case AL_MAX_GAIN:
- case AL_REFERENCE_DISTANCE:
- case AL_ROLLOFF_FACTOR:
- case AL_CONE_OUTER_GAIN:
- case AL_MAX_DISTANCE:
- case AL_SEC_OFFSET:
- case AL_SAMPLE_OFFSET:
- case AL_BYTE_OFFSET:
- case AL_DOPPLER_FACTOR:
- case AL_AIR_ABSORPTION_FACTOR:
- case AL_ROOM_ROLLOFF_FACTOR:
- case AL_CONE_OUTER_GAINHF:
- case AL_SOURCE_RADIUS:
- CHECKSIZE(values, 1);
- if((err=GetSourcedv(Source, Context, prop, {dvals, 1u})) != false)
- values[0] = static_cast<int>(dvals[0]);
- return err;
- /* 3x float/double */
- case AL_POSITION:
- case AL_VELOCITY:
- case AL_DIRECTION:
- CHECKSIZE(values, 3);
- if((err=GetSourcedv(Source, Context, prop, {dvals, 3u})) != false)
- {
- values[0] = static_cast<int>(dvals[0]);
- values[1] = static_cast<int>(dvals[1]);
- values[2] = static_cast<int>(dvals[2]);
- }
- return err;
- /* 6x float/double */
- case AL_ORIENTATION:
- CHECKSIZE(values, 6);
- if((err=GetSourcedv(Source, Context, prop, {dvals, 6u})) != false)
- {
- values[0] = static_cast<int>(dvals[0]);
- values[1] = static_cast<int>(dvals[1]);
- values[2] = static_cast<int>(dvals[2]);
- values[3] = static_cast<int>(dvals[3]);
- values[4] = static_cast<int>(dvals[4]);
- values[5] = static_cast<int>(dvals[5]);
- }
- return err;
- case AL_SAMPLE_OFFSET_LATENCY_SOFT:
- case AL_SAMPLE_OFFSET_CLOCK_SOFT:
- break; /* i64 only */
- case AL_SEC_OFFSET_LATENCY_SOFT:
- case AL_SEC_OFFSET_CLOCK_SOFT:
- break; /* Double only */
- case AL_STEREO_ANGLES:
- break; /* Float/double only */
- case AL_DIRECT_FILTER:
- case AL_AUXILIARY_SEND_FILTER:
- break; /* ??? */
- }
- ERR("Unexpected property: 0x%04x\n", prop);
- Context->setError(AL_INVALID_ENUM, "Invalid source integer property 0x%04x", prop);
- return false;
- }
- bool GetSourcei64v(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<int64_t> values)
- {
- ALCdevice *device = Context->mDevice.get();
- ClockLatency clocktime;
- nanoseconds srcclock;
- double dvals[MaxValues];
- int ivals[MaxValues];
- bool err;
- switch(prop)
- {
- case AL_SAMPLE_OFFSET_LATENCY_SOFT:
- CHECKSIZE(values, 2);
- /* Get the source offset with the clock time first. Then get the clock
- * time with the device latency. Order is important.
- */
- values[0] = GetSourceSampleOffset(Source, Context, &srcclock);
- {
- std::lock_guard<std::mutex> _{device->StateLock};
- clocktime = GetClockLatency(device);
- }
- if(srcclock == clocktime.ClockTime)
- values[1] = clocktime.Latency.count();
- else
- {
- /* If the clock time incremented, reduce the latency by that much
- * since it's that much closer to the source offset it got earlier.
- */
- const nanoseconds diff{clocktime.ClockTime - srcclock};
- values[1] = nanoseconds{clocktime.Latency - std::min(clocktime.Latency, diff)}.count();
- }
- return true;
- case AL_SAMPLE_OFFSET_CLOCK_SOFT:
- CHECKSIZE(values, 2);
- values[0] = GetSourceSampleOffset(Source, Context, &srcclock);
- values[1] = srcclock.count();
- return true;
- /* 1x float/double */
- case AL_CONE_INNER_ANGLE:
- case AL_CONE_OUTER_ANGLE:
- case AL_PITCH:
- case AL_GAIN:
- case AL_MIN_GAIN:
- case AL_MAX_GAIN:
- case AL_REFERENCE_DISTANCE:
- case AL_ROLLOFF_FACTOR:
- case AL_CONE_OUTER_GAIN:
- case AL_MAX_DISTANCE:
- case AL_SEC_OFFSET:
- case AL_SAMPLE_OFFSET:
- case AL_BYTE_OFFSET:
- case AL_DOPPLER_FACTOR:
- case AL_AIR_ABSORPTION_FACTOR:
- case AL_ROOM_ROLLOFF_FACTOR:
- case AL_CONE_OUTER_GAINHF:
- case AL_SOURCE_RADIUS:
- CHECKSIZE(values, 1);
- if((err=GetSourcedv(Source, Context, prop, {dvals, 1u})) != false)
- values[0] = static_cast<int64_t>(dvals[0]);
- return err;
- /* 3x float/double */
- case AL_POSITION:
- case AL_VELOCITY:
- case AL_DIRECTION:
- CHECKSIZE(values, 3);
- if((err=GetSourcedv(Source, Context, prop, {dvals, 3u})) != false)
- {
- values[0] = static_cast<int64_t>(dvals[0]);
- values[1] = static_cast<int64_t>(dvals[1]);
- values[2] = static_cast<int64_t>(dvals[2]);
- }
- return err;
- /* 6x float/double */
- case AL_ORIENTATION:
- CHECKSIZE(values, 6);
- if((err=GetSourcedv(Source, Context, prop, {dvals, 6u})) != false)
- {
- values[0] = static_cast<int64_t>(dvals[0]);
- values[1] = static_cast<int64_t>(dvals[1]);
- values[2] = static_cast<int64_t>(dvals[2]);
- values[3] = static_cast<int64_t>(dvals[3]);
- values[4] = static_cast<int64_t>(dvals[4]);
- values[5] = static_cast<int64_t>(dvals[5]);
- }
- return err;
- /* 1x int */
- case AL_SOURCE_RELATIVE:
- case AL_LOOPING:
- case AL_SOURCE_STATE:
- case AL_BUFFERS_QUEUED:
- case AL_BUFFERS_PROCESSED:
- case AL_SOURCE_TYPE:
- case AL_DIRECT_FILTER_GAINHF_AUTO:
- case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
- case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
- case AL_DIRECT_CHANNELS_SOFT:
- case AL_DISTANCE_MODEL:
- case AL_SOURCE_RESAMPLER_SOFT:
- case AL_SOURCE_SPATIALIZE_SOFT:
- CHECKSIZE(values, 1);
- if((err=GetSourceiv(Source, Context, prop, {ivals, 1u})) != false)
- values[0] = ivals[0];
- return err;
- /* 1x uint */
- case AL_BUFFER:
- case AL_DIRECT_FILTER:
- CHECKSIZE(values, 1);
- if((err=GetSourceiv(Source, Context, prop, {ivals, 1u})) != false)
- values[0] = static_cast<ALuint>(ivals[0]);
- return err;
- /* 3x uint */
- case AL_AUXILIARY_SEND_FILTER:
- CHECKSIZE(values, 3);
- if((err=GetSourceiv(Source, Context, prop, {ivals, 3u})) != false)
- {
- values[0] = static_cast<ALuint>(ivals[0]);
- values[1] = static_cast<ALuint>(ivals[1]);
- values[2] = static_cast<ALuint>(ivals[2]);
- }
- return err;
- case AL_SEC_OFFSET_LATENCY_SOFT:
- case AL_SEC_OFFSET_CLOCK_SOFT:
- break; /* Double only */
- case AL_STEREO_ANGLES:
- break; /* Float/double only */
- }
- ERR("Unexpected property: 0x%04x\n", prop);
- Context->setError(AL_INVALID_ENUM, "Invalid source integer64 property 0x%04x", prop);
- return false;
- }
- } // namespace
- AL_API void AL_APIENTRY alGenSources(ALsizei n, ALuint *sources)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- if UNLIKELY(n < 0)
- context->setError(AL_INVALID_VALUE, "Generating %d sources", n);
- if UNLIKELY(n <= 0) return;
- std::unique_lock<std::mutex> srclock{context->mSourceLock};
- ALCdevice *device{context->mDevice.get()};
- if(static_cast<ALuint>(n) > device->SourcesMax-context->mNumSources)
- {
- context->setError(AL_OUT_OF_MEMORY, "Exceeding %u source limit (%u + %d)",
- device->SourcesMax, context->mNumSources, n);
- return;
- }
- if(!EnsureSources(context.get(), static_cast<ALuint>(n)))
- {
- context->setError(AL_OUT_OF_MEMORY, "Failed to allocate %d source%s", n, (n==1)?"":"s");
- return;
- }
- if(n == 1)
- {
- ALsource *source{AllocSource(context.get())};
- sources[0] = source->id;
- }
- else
- {
- al::vector<ALuint> ids;
- ids.reserve(static_cast<ALuint>(n));
- do {
- ALsource *source{AllocSource(context.get())};
- ids.emplace_back(source->id);
- } while(--n);
- std::copy(ids.cbegin(), ids.cend(), sources);
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alDeleteSources(ALsizei n, const ALuint *sources)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- if UNLIKELY(n < 0)
- SETERR_RETURN(context, AL_INVALID_VALUE,, "Deleting %d sources", n);
- std::lock_guard<std::mutex> _{context->mSourceLock};
- /* Check that all Sources are valid */
- auto validate_source = [&context](const ALuint sid) -> bool
- { return LookupSource(context.get(), sid) != nullptr; };
- const ALuint *sources_end = sources + n;
- auto invsrc = std::find_if_not(sources, sources_end, validate_source);
- if UNLIKELY(invsrc != sources_end)
- {
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", *invsrc);
- return;
- }
- /* All good. Delete source IDs. */
- auto delete_source = [&context](const ALuint sid) -> void
- {
- ALsource *src{LookupSource(context.get(), sid)};
- if(src) FreeSource(context.get(), src);
- };
- std::for_each(sources, sources_end, delete_source);
- }
- END_API_FUNC
- AL_API ALboolean AL_APIENTRY alIsSource(ALuint source)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if LIKELY(context)
- {
- std::lock_guard<std::mutex> _{context->mSourceLock};
- if(LookupSource(context.get(), source) != nullptr)
- return AL_TRUE;
- }
- return AL_FALSE;
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourcef(ALuint source, ALenum param, ALfloat value)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mPropLock};
- std::lock_guard<std::mutex> __{context->mSourceLock};
- ALsource *Source = LookupSource(context.get(), source);
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else
- SetSourcefv(Source, context.get(), static_cast<SourceProp>(param), {&value, 1u});
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSource3f(ALuint source, ALenum param, ALfloat value1, ALfloat value2, ALfloat value3)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mPropLock};
- std::lock_guard<std::mutex> __{context->mSourceLock};
- ALsource *Source = LookupSource(context.get(), source);
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else
- {
- const float fvals[3]{ value1, value2, value3 };
- SetSourcefv(Source, context.get(), static_cast<SourceProp>(param), fvals);
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourcefv(ALuint source, ALenum param, const ALfloat *values)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mPropLock};
- std::lock_guard<std::mutex> __{context->mSourceLock};
- ALsource *Source = LookupSource(context.get(), source);
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!values)
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- SetSourcefv(Source, context.get(), static_cast<SourceProp>(param), {values, MaxValues});
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourcedSOFT(ALuint source, ALenum param, ALdouble value)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mPropLock};
- std::lock_guard<std::mutex> __{context->mSourceLock};
- ALsource *Source = LookupSource(context.get(), source);
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else
- {
- const float fval[1]{static_cast<float>(value)};
- SetSourcefv(Source, context.get(), static_cast<SourceProp>(param), fval);
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSource3dSOFT(ALuint source, ALenum param, ALdouble value1, ALdouble value2, ALdouble value3)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mPropLock};
- std::lock_guard<std::mutex> __{context->mSourceLock};
- ALsource *Source = LookupSource(context.get(), source);
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else
- {
- const float fvals[3]{static_cast<float>(value1), static_cast<float>(value2),
- static_cast<float>(value3)};
- SetSourcefv(Source, context.get(), static_cast<SourceProp>(param), fvals);
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourcedvSOFT(ALuint source, ALenum param, const ALdouble *values)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mPropLock};
- std::lock_guard<std::mutex> __{context->mSourceLock};
- ALsource *Source = LookupSource(context.get(), source);
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!values)
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- {
- const ALuint count{DoubleValsByProp(param)};
- float fvals[MaxValues];
- for(ALuint i{0};i < count;i++)
- fvals[i] = static_cast<float>(values[i]);
- SetSourcefv(Source, context.get(), static_cast<SourceProp>(param), {fvals, count});
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourcei(ALuint source, ALenum param, ALint value)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mPropLock};
- std::lock_guard<std::mutex> __{context->mSourceLock};
- ALsource *Source = LookupSource(context.get(), source);
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else
- SetSourceiv(Source, context.get(), static_cast<SourceProp>(param), {&value, 1u});
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSource3i(ALuint source, ALenum param, ALint value1, ALint value2, ALint value3)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mPropLock};
- std::lock_guard<std::mutex> __{context->mSourceLock};
- ALsource *Source = LookupSource(context.get(), source);
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else
- {
- const int ivals[3]{ value1, value2, value3 };
- SetSourceiv(Source, context.get(), static_cast<SourceProp>(param), ivals);
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourceiv(ALuint source, ALenum param, const ALint *values)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mPropLock};
- std::lock_guard<std::mutex> __{context->mSourceLock};
- ALsource *Source = LookupSource(context.get(), source);
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!values)
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- SetSourceiv(Source, context.get(), static_cast<SourceProp>(param), {values, MaxValues});
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourcei64SOFT(ALuint source, ALenum param, ALint64SOFT value)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mPropLock};
- std::lock_guard<std::mutex> __{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else
- SetSourcei64v(Source, context.get(), static_cast<SourceProp>(param), {&value, 1u});
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSource3i64SOFT(ALuint source, ALenum param, ALint64SOFT value1, ALint64SOFT value2, ALint64SOFT value3)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mPropLock};
- std::lock_guard<std::mutex> __{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else
- {
- const int64_t i64vals[3]{ value1, value2, value3 };
- SetSourcei64v(Source, context.get(), static_cast<SourceProp>(param), i64vals);
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourcei64vSOFT(ALuint source, ALenum param, const ALint64SOFT *values)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mPropLock};
- std::lock_guard<std::mutex> __{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!values)
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- SetSourcei64v(Source, context.get(), static_cast<SourceProp>(param), {values, MaxValues});
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alGetSourcef(ALuint source, ALenum param, ALfloat *value)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!value)
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- {
- double dval[1];
- if(GetSourcedv(Source, context.get(), static_cast<SourceProp>(param), dval))
- *value = static_cast<float>(dval[0]);
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alGetSource3f(ALuint source, ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!(value1 && value2 && value3))
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- {
- double dvals[3];
- if(GetSourcedv(Source, context.get(), static_cast<SourceProp>(param), dvals))
- {
- *value1 = static_cast<float>(dvals[0]);
- *value2 = static_cast<float>(dvals[1]);
- *value3 = static_cast<float>(dvals[2]);
- }
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alGetSourcefv(ALuint source, ALenum param, ALfloat *values)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!values)
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- {
- const ALuint count{FloatValsByProp(param)};
- double dvals[MaxValues];
- if(GetSourcedv(Source, context.get(), static_cast<SourceProp>(param), {dvals, count}))
- {
- for(ALuint i{0};i < count;i++)
- values[i] = static_cast<float>(dvals[i]);
- }
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alGetSourcedSOFT(ALuint source, ALenum param, ALdouble *value)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!value)
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- GetSourcedv(Source, context.get(), static_cast<SourceProp>(param), {value, 1u});
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alGetSource3dSOFT(ALuint source, ALenum param, ALdouble *value1, ALdouble *value2, ALdouble *value3)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!(value1 && value2 && value3))
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- {
- double dvals[3];
- if(GetSourcedv(Source, context.get(), static_cast<SourceProp>(param), dvals))
- {
- *value1 = dvals[0];
- *value2 = dvals[1];
- *value3 = dvals[2];
- }
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alGetSourcedvSOFT(ALuint source, ALenum param, ALdouble *values)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!values)
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- GetSourcedv(Source, context.get(), static_cast<SourceProp>(param), {values, MaxValues});
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alGetSourcei(ALuint source, ALenum param, ALint *value)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!value)
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- GetSourceiv(Source, context.get(), static_cast<SourceProp>(param), {value, 1u});
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alGetSource3i(ALuint source, ALenum param, ALint *value1, ALint *value2, ALint *value3)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!(value1 && value2 && value3))
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- {
- int ivals[3];
- if(GetSourceiv(Source, context.get(), static_cast<SourceProp>(param), ivals))
- {
- *value1 = ivals[0];
- *value2 = ivals[1];
- *value3 = ivals[2];
- }
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alGetSourceiv(ALuint source, ALenum param, ALint *values)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!values)
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- GetSourceiv(Source, context.get(), static_cast<SourceProp>(param), {values, MaxValues});
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alGetSourcei64SOFT(ALuint source, ALenum param, ALint64SOFT *value)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!value)
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- GetSourcei64v(Source, context.get(), static_cast<SourceProp>(param), {value, 1u});
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alGetSource3i64SOFT(ALuint source, ALenum param, ALint64SOFT *value1, ALint64SOFT *value2, ALint64SOFT *value3)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!(value1 && value2 && value3))
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- {
- int64_t i64vals[3];
- if(GetSourcei64v(Source, context.get(), static_cast<SourceProp>(param), i64vals))
- {
- *value1 = i64vals[0];
- *value2 = i64vals[1];
- *value3 = i64vals[2];
- }
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alGetSourcei64vSOFT(ALuint source, ALenum param, ALint64SOFT *values)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- std::lock_guard<std::mutex> _{context->mSourceLock};
- ALsource *Source{LookupSource(context.get(), source)};
- if UNLIKELY(!Source)
- context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
- else if UNLIKELY(!values)
- context->setError(AL_INVALID_VALUE, "NULL pointer");
- else
- GetSourcei64v(Source, context.get(), static_cast<SourceProp>(param), {values, MaxValues});
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourcePlay(ALuint source)
- START_API_FUNC
- { alSourcePlayv(1, &source); }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourcePlayv(ALsizei n, const ALuint *sources)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- if UNLIKELY(n < 0)
- context->setError(AL_INVALID_VALUE, "Playing %d sources", n);
- if UNLIKELY(n <= 0) return;
- al::vector<ALsource*> extra_sources;
- std::array<ALsource*,8> source_storage;
- al::span<ALsource*> srchandles;
- if LIKELY(static_cast<ALuint>(n) <= source_storage.size())
- srchandles = {source_storage.data(), static_cast<ALuint>(n)};
- else
- {
- extra_sources.resize(static_cast<ALuint>(n));
- srchandles = {extra_sources.data(), extra_sources.size()};
- }
- std::lock_guard<std::mutex> _{context->mSourceLock};
- for(auto &srchdl : srchandles)
- {
- srchdl = LookupSource(context.get(), *sources);
- if(!srchdl)
- SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", *sources);
- ++sources;
- }
- ALCdevice *device{context->mDevice.get()};
- /* If the device is disconnected, go right to stopped. */
- if UNLIKELY(!device->Connected.load(std::memory_order_acquire))
- {
- /* TODO: Send state change event? */
- for(ALsource *source : srchandles)
- {
- source->Offset = 0.0;
- source->OffsetType = AL_NONE;
- source->state = AL_STOPPED;
- }
- return;
- }
- /* Count the number of reusable voices. */
- auto voicelist = context->getVoicesSpan();
- size_t free_voices{0};
- for(const Voice *voice : voicelist)
- {
- free_voices += (voice->mPlayState.load(std::memory_order_acquire) == Voice::Stopped
- && voice->mSourceID.load(std::memory_order_relaxed) == 0u
- && voice->mPendingChange.load(std::memory_order_relaxed) == false);
- if(free_voices == srchandles.size())
- break;
- }
- if UNLIKELY(srchandles.size() != free_voices)
- {
- const size_t inc_amount{srchandles.size() - free_voices};
- auto &allvoices = *context->mVoices.load(std::memory_order_relaxed);
- if(inc_amount > allvoices.size() - voicelist.size())
- {
- /* Increase the number of voices to handle the request. */
- context->allocVoices(inc_amount - (allvoices.size() - voicelist.size()));
- }
- context->mActiveVoiceCount.fetch_add(inc_amount, std::memory_order_release);
- voicelist = context->getVoicesSpan();
- }
- auto voiceiter = voicelist.begin();
- ALuint vidx{0};
- VoiceChange *tail{}, *cur{};
- for(ALsource *source : srchandles)
- {
- /* Check that there is a queue containing at least one valid, non zero
- * length buffer.
- */
- auto BufferList = source->mQueue.begin();
- for(;BufferList != source->mQueue.end();++BufferList)
- {
- if(BufferList->mSampleLen != 0 || BufferList->mCallback)
- break;
- }
- /* If there's nothing to play, go right to stopped. */
- if UNLIKELY(BufferList == source->mQueue.end())
- {
- /* NOTE: A source without any playable buffers should not have a
- * Voice since it shouldn't be in a playing or paused state. So
- * there's no need to look up its voice and clear the source.
- */
- source->Offset = 0.0;
- source->OffsetType = AL_NONE;
- source->state = AL_STOPPED;
- continue;
- }
- if(!cur)
- cur = tail = GetVoiceChanger(context.get());
- else
- {
- cur->mNext.store(GetVoiceChanger(context.get()), std::memory_order_relaxed);
- cur = cur->mNext.load(std::memory_order_relaxed);
- }
- Voice *voice{GetSourceVoice(source, context.get())};
- switch(GetSourceState(source, voice))
- {
- case AL_PAUSED:
- /* A source that's paused simply resumes. If there's no voice, it
- * was lost from a disconnect, so just start over with a new one.
- */
- cur->mOldVoice = nullptr;
- if(!voice) break;
- cur->mVoice = voice;
- cur->mSourceID = source->id;
- cur->mState = VChangeState::Play;
- source->state = AL_PLAYING;
- continue;
- case AL_PLAYING:
- /* A source that's already playing is restarted from the beginning.
- * Stop the current voice and start a new one so it properly cross-
- * fades back to the beginning.
- */
- if(voice)
- voice->mPendingChange.store(true, std::memory_order_relaxed);
- cur->mOldVoice = voice;
- voice = nullptr;
- break;
- default:
- assert(voice == nullptr);
- cur->mOldVoice = nullptr;
- break;
- }
- /* Find the next unused voice to play this source with. */
- for(;voiceiter != voicelist.end();++voiceiter,++vidx)
- {
- Voice *v{*voiceiter};
- if(v->mPlayState.load(std::memory_order_acquire) == Voice::Stopped
- && v->mSourceID.load(std::memory_order_relaxed) == 0u
- && v->mPendingChange.load(std::memory_order_relaxed) == false)
- {
- voice = v;
- break;
- }
- }
- voice->mPosition.store(0u, std::memory_order_relaxed);
- voice->mPositionFrac.store(0, std::memory_order_relaxed);
- voice->mCurrentBuffer.store(&source->mQueue.front(), std::memory_order_relaxed);
- voice->mFlags = 0;
- /* A source that's not playing or paused has any offset applied when it
- * starts playing.
- */
- if(const ALenum offsettype{source->OffsetType})
- {
- const double offset{source->Offset};
- source->OffsetType = AL_NONE;
- source->Offset = 0.0;
- if(auto vpos = GetSampleOffset(source->mQueue, offsettype, offset))
- {
- voice->mPosition.store(vpos->pos, std::memory_order_relaxed);
- voice->mPositionFrac.store(vpos->frac, std::memory_order_relaxed);
- voice->mCurrentBuffer.store(vpos->bufferitem, std::memory_order_relaxed);
- if(vpos->pos!=0 || vpos->frac!=0 || vpos->bufferitem!=&source->mQueue.front())
- voice->mFlags |= VoiceIsFading;
- }
- }
- InitVoice(voice, source, std::addressof(*BufferList), context.get(), device);
- source->VoiceIdx = vidx;
- source->state = AL_PLAYING;
- cur->mVoice = voice;
- cur->mSourceID = source->id;
- cur->mState = VChangeState::Play;
- }
- if LIKELY(tail)
- SendVoiceChanges(context.get(), tail);
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourcePause(ALuint source)
- START_API_FUNC
- { alSourcePausev(1, &source); }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourcePausev(ALsizei n, const ALuint *sources)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- if UNLIKELY(n < 0)
- context->setError(AL_INVALID_VALUE, "Pausing %d sources", n);
- if UNLIKELY(n <= 0) return;
- al::vector<ALsource*> extra_sources;
- std::array<ALsource*,8> source_storage;
- al::span<ALsource*> srchandles;
- if LIKELY(static_cast<ALuint>(n) <= source_storage.size())
- srchandles = {source_storage.data(), static_cast<ALuint>(n)};
- else
- {
- extra_sources.resize(static_cast<ALuint>(n));
- srchandles = {extra_sources.data(), extra_sources.size()};
- }
- std::lock_guard<std::mutex> _{context->mSourceLock};
- for(auto &srchdl : srchandles)
- {
- srchdl = LookupSource(context.get(), *sources);
- if(!srchdl)
- SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", *sources);
- ++sources;
- }
- /* Pausing has to be done in two steps. First, for each source that's
- * detected to be playing, chamge the voice (asynchronously) to
- * stopping/paused.
- */
- VoiceChange *tail{}, *cur{};
- for(ALsource *source : srchandles)
- {
- Voice *voice{GetSourceVoice(source, context.get())};
- if(GetSourceState(source, voice) == AL_PLAYING)
- {
- if(!cur)
- cur = tail = GetVoiceChanger(context.get());
- else
- {
- cur->mNext.store(GetVoiceChanger(context.get()), std::memory_order_relaxed);
- cur = cur->mNext.load(std::memory_order_relaxed);
- }
- cur->mVoice = voice;
- cur->mSourceID = source->id;
- cur->mState = VChangeState::Pause;
- }
- }
- if LIKELY(tail)
- {
- SendVoiceChanges(context.get(), tail);
- /* Second, now that the voice changes have been sent, because it's
- * possible that the voice stopped after it was detected playing and
- * before the voice got paused, recheck that the source is still
- * considered playing and set it to paused if so.
- */
- for(ALsource *source : srchandles)
- {
- Voice *voice{GetSourceVoice(source, context.get())};
- if(GetSourceState(source, voice) == AL_PLAYING)
- source->state = AL_PAUSED;
- }
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourceStop(ALuint source)
- START_API_FUNC
- { alSourceStopv(1, &source); }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourceStopv(ALsizei n, const ALuint *sources)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- if UNLIKELY(n < 0)
- context->setError(AL_INVALID_VALUE, "Stopping %d sources", n);
- if UNLIKELY(n <= 0) return;
- al::vector<ALsource*> extra_sources;
- std::array<ALsource*,8> source_storage;
- al::span<ALsource*> srchandles;
- if LIKELY(static_cast<ALuint>(n) <= source_storage.size())
- srchandles = {source_storage.data(), static_cast<ALuint>(n)};
- else
- {
- extra_sources.resize(static_cast<ALuint>(n));
- srchandles = {extra_sources.data(), extra_sources.size()};
- }
- std::lock_guard<std::mutex> _{context->mSourceLock};
- for(auto &srchdl : srchandles)
- {
- srchdl = LookupSource(context.get(), *sources);
- if(!srchdl)
- SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", *sources);
- ++sources;
- }
- VoiceChange *tail{}, *cur{};
- for(ALsource *source : srchandles)
- {
- if(Voice *voice{GetSourceVoice(source, context.get())})
- {
- if(!cur)
- cur = tail = GetVoiceChanger(context.get());
- else
- {
- cur->mNext.store(GetVoiceChanger(context.get()), std::memory_order_relaxed);
- cur = cur->mNext.load(std::memory_order_relaxed);
- }
- voice->mPendingChange.store(true, std::memory_order_relaxed);
- cur->mVoice = voice;
- cur->mSourceID = source->id;
- cur->mState = VChangeState::Stop;
- source->state = AL_STOPPED;
- }
- source->Offset = 0.0;
- source->OffsetType = AL_NONE;
- source->VoiceIdx = INVALID_VOICE_IDX;
- }
- if LIKELY(tail)
- SendVoiceChanges(context.get(), tail);
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourceRewind(ALuint source)
- START_API_FUNC
- { alSourceRewindv(1, &source); }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourceRewindv(ALsizei n, const ALuint *sources)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- if UNLIKELY(n < 0)
- context->setError(AL_INVALID_VALUE, "Rewinding %d sources", n);
- if UNLIKELY(n <= 0) return;
- al::vector<ALsource*> extra_sources;
- std::array<ALsource*,8> source_storage;
- al::span<ALsource*> srchandles;
- if LIKELY(static_cast<ALuint>(n) <= source_storage.size())
- srchandles = {source_storage.data(), static_cast<ALuint>(n)};
- else
- {
- extra_sources.resize(static_cast<ALuint>(n));
- srchandles = {extra_sources.data(), extra_sources.size()};
- }
- std::lock_guard<std::mutex> _{context->mSourceLock};
- for(auto &srchdl : srchandles)
- {
- srchdl = LookupSource(context.get(), *sources);
- if(!srchdl)
- SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", *sources);
- ++sources;
- }
- VoiceChange *tail{}, *cur{};
- for(ALsource *source : srchandles)
- {
- Voice *voice{GetSourceVoice(source, context.get())};
- if(source->state != AL_INITIAL)
- {
- if(!cur)
- cur = tail = GetVoiceChanger(context.get());
- else
- {
- cur->mNext.store(GetVoiceChanger(context.get()), std::memory_order_relaxed);
- cur = cur->mNext.load(std::memory_order_relaxed);
- }
- if(voice)
- voice->mPendingChange.store(true, std::memory_order_relaxed);
- cur->mVoice = voice;
- cur->mSourceID = source->id;
- cur->mState = VChangeState::Reset;
- source->state = AL_INITIAL;
- }
- source->Offset = 0.0;
- source->OffsetType = AL_NONE;
- source->VoiceIdx = INVALID_VOICE_IDX;
- }
- if LIKELY(tail)
- SendVoiceChanges(context.get(), tail);
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourceQueueBuffers(ALuint src, ALsizei nb, const ALuint *buffers)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- if UNLIKELY(nb < 0)
- context->setError(AL_INVALID_VALUE, "Queueing %d buffers", nb);
- if UNLIKELY(nb <= 0) return;
- std::lock_guard<std::mutex> _{context->mSourceLock};
- ALsource *source{LookupSource(context.get(),src)};
- if UNLIKELY(!source)
- SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", src);
- /* Can't queue on a Static Source */
- if UNLIKELY(source->SourceType == AL_STATIC)
- SETERR_RETURN(context, AL_INVALID_OPERATION,, "Queueing onto static source %u", src);
- /* Check for a valid Buffer, for its frequency and format */
- ALCdevice *device{context->mDevice.get()};
- ALbuffer *BufferFmt{nullptr};
- for(auto &item : source->mQueue)
- {
- BufferFmt = item.mBuffer;
- if(BufferFmt) break;
- }
- std::unique_lock<std::mutex> buflock{device->BufferLock};
- const size_t NewListStart{source->mQueue.size()};
- ALbufferQueueItem *BufferList{nullptr};
- for(ALsizei i{0};i < nb;i++)
- {
- bool fmt_mismatch{false};
- ALbuffer *buffer{nullptr};
- if(buffers[i] && (buffer=LookupBuffer(device, buffers[i])) == nullptr)
- {
- context->setError(AL_INVALID_NAME, "Queueing invalid buffer ID %u", buffers[i]);
- goto buffer_error;
- }
- if(buffer && buffer->mCallback)
- {
- context->setError(AL_INVALID_OPERATION, "Queueing callback buffer %u", buffers[i]);
- goto buffer_error;
- }
- source->mQueue.emplace_back();
- if(!BufferList)
- BufferList = &source->mQueue.back();
- else
- {
- auto &item = source->mQueue.back();
- BufferList->mNext.store(&item, std::memory_order_relaxed);
- BufferList = &item;
- }
- if(!buffer) continue;
- BufferList->mSampleLen = buffer->mSampleLen;
- BufferList->mLoopEnd = buffer->mSampleLen;
- BufferList->mSamples = buffer->mData.data();
- BufferList->mBuffer = buffer;
- IncrementRef(buffer->ref);
- if(buffer->MappedAccess != 0 && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT))
- {
- context->setError(AL_INVALID_OPERATION, "Queueing non-persistently mapped buffer %u",
- buffer->id);
- goto buffer_error;
- }
- if(BufferFmt == nullptr)
- BufferFmt = buffer;
- else
- {
- fmt_mismatch |= BufferFmt->mSampleRate != buffer->mSampleRate;
- fmt_mismatch |= BufferFmt->mChannels != buffer->mChannels;
- if(BufferFmt->isBFormat())
- {
- fmt_mismatch |= BufferFmt->mAmbiLayout != buffer->mAmbiLayout;
- fmt_mismatch |= BufferFmt->mAmbiScaling != buffer->mAmbiScaling;
- }
- fmt_mismatch |= BufferFmt->mAmbiOrder != buffer->mAmbiOrder;
- fmt_mismatch |= BufferFmt->OriginalType != buffer->OriginalType;
- }
- if UNLIKELY(fmt_mismatch)
- {
- context->setError(AL_INVALID_OPERATION, "Queueing buffer with mismatched format");
- buffer_error:
- /* A buffer failed (invalid ID or format), so unlock and release
- * each buffer we had.
- */
- auto iter = source->mQueue.begin() + ptrdiff_t(NewListStart);
- for(;iter != source->mQueue.end();++iter)
- {
- if(ALbuffer *buf{iter->mBuffer})
- DecrementRef(buf->ref);
- }
- source->mQueue.resize(NewListStart);
- return;
- }
- }
- /* All buffers good. */
- buflock.unlock();
- /* Source is now streaming */
- source->SourceType = AL_STREAMING;
- if(NewListStart != 0)
- {
- auto iter = source->mQueue.begin() + ptrdiff_t(NewListStart);
- (iter-1)->mNext.store(std::addressof(*iter), std::memory_order_release);
- }
- }
- END_API_FUNC
- AL_API void AL_APIENTRY alSourceUnqueueBuffers(ALuint src, ALsizei nb, ALuint *buffers)
- START_API_FUNC
- {
- ContextRef context{GetContextRef()};
- if UNLIKELY(!context) return;
- if UNLIKELY(nb < 0)
- context->setError(AL_INVALID_VALUE, "Unqueueing %d buffers", nb);
- if UNLIKELY(nb <= 0) return;
- std::lock_guard<std::mutex> _{context->mSourceLock};
- ALsource *source{LookupSource(context.get(),src)};
- if UNLIKELY(!source)
- SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", src);
- if UNLIKELY(source->SourceType != AL_STREAMING)
- SETERR_RETURN(context, AL_INVALID_VALUE,, "Unqueueing from a non-streaming source %u",
- src);
- if UNLIKELY(source->Looping)
- SETERR_RETURN(context, AL_INVALID_VALUE,, "Unqueueing from looping source %u", src);
- /* Make sure enough buffers have been processed to unqueue. */
- uint processed{0u};
- if LIKELY(source->state != AL_INITIAL)
- {
- VoiceBufferItem *Current{nullptr};
- if(Voice *voice{GetSourceVoice(source, context.get())})
- Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
- for(auto &item : source->mQueue)
- {
- if(&item == Current)
- break;
- ++processed;
- }
- }
- if UNLIKELY(processed < static_cast<ALuint>(nb))
- SETERR_RETURN(context, AL_INVALID_VALUE,, "Unqueueing %d buffer%s (only %u processed)",
- nb, (nb==1)?"":"s", processed);
- do {
- auto &head = source->mQueue.front();
- if(ALbuffer *buffer{head.mBuffer})
- {
- *(buffers++) = buffer->id;
- DecrementRef(buffer->ref);
- }
- else
- *(buffers++) = 0;
- source->mQueue.pop_front();
- } while(--nb);
- }
- END_API_FUNC
- ALsource::ALsource()
- {
- Direct.Gain = 1.0f;
- Direct.GainHF = 1.0f;
- Direct.HFReference = LOWPASSFREQREF;
- Direct.GainLF = 1.0f;
- Direct.LFReference = HIGHPASSFREQREF;
- for(auto &send : Send)
- {
- send.Slot = nullptr;
- send.Gain = 1.0f;
- send.GainHF = 1.0f;
- send.HFReference = LOWPASSFREQREF;
- send.GainLF = 1.0f;
- send.LFReference = HIGHPASSFREQREF;
- }
- PropsClean.test_and_set(std::memory_order_relaxed);
- }
- ALsource::~ALsource()
- {
- for(auto &item : mQueue)
- {
- if(ALbuffer *buffer{item.mBuffer})
- DecrementRef(buffer->ref);
- }
- auto clear_send = [](ALsource::SendData &send) -> void
- { if(send.Slot) DecrementRef(send.Slot->ref); };
- std::for_each(Send.begin(), Send.end(), clear_send);
- }
- void UpdateAllSourceProps(ALCcontext *context)
- {
- std::lock_guard<std::mutex> _{context->mSourceLock};
- auto voicelist = context->getVoicesSpan();
- ALuint vidx{0u};
- for(Voice *voice : voicelist)
- {
- ALuint sid{voice->mSourceID.load(std::memory_order_acquire)};
- ALsource *source = sid ? LookupSource(context, sid) : nullptr;
- if(source && source->VoiceIdx == vidx)
- {
- if(!source->PropsClean.test_and_set(std::memory_order_acq_rel))
- UpdateSourceProps(source, voice, context);
- }
- ++vidx;
- }
- }
- SourceSubList::~SourceSubList()
- {
- uint64_t usemask{~FreeMask};
- while(usemask)
- {
- const int idx{al::countr_zero(usemask)};
- al::destroy_at(Sources+idx);
- usemask &= ~(1_u64 << idx);
- }
- FreeMask = ~usemask;
- al_free(Sources);
- Sources = nullptr;
- }
|