| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731 |
- /**
- * 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 "buffer.h"
- #include <algorithm>
- #include <array>
- #include <atomic>
- #include <cassert>
- #include <cstddef>
- #include <cstdint>
- #include <cstring>
- #include <iterator>
- #include <limits>
- #include <memory>
- #include <mutex>
- #include <numeric>
- #include <optional>
- #include <stdexcept>
- #include <unordered_map>
- #include <utility>
- #include <vector>
- #include "AL/al.h"
- #include "AL/alc.h"
- #include "AL/alext.h"
- #include "albit.h"
- #include "alc/context.h"
- #include "alc/device.h"
- #include "alc/inprogext.h"
- #include "almalloc.h"
- #include "alnumeric.h"
- #include "alspan.h"
- #include "core/device.h"
- #include "core/except.h"
- #include "core/logging.h"
- #include "core/resampler_limits.h"
- #include "core/voice.h"
- #include "direct_defs.h"
- #include "intrusive_ptr.h"
- #include "opthelpers.h"
- #if ALSOFT_EAX
- #include <unordered_set>
- #include "eax/globals.h"
- #include "eax/x_ram.h"
- #endif // ALSOFT_EAX
- namespace {
- using SubListAllocator = al::allocator<std::array<ALbuffer,64>>;
- constexpr auto AmbiLayoutFromEnum(ALenum layout) noexcept -> std::optional<AmbiLayout>
- {
- switch(layout)
- {
- case AL_FUMA_SOFT: return AmbiLayout::FuMa;
- case AL_ACN_SOFT: return AmbiLayout::ACN;
- }
- return std::nullopt;
- }
- constexpr auto EnumFromAmbiLayout(AmbiLayout layout) -> ALenum
- {
- switch(layout)
- {
- case AmbiLayout::FuMa: return AL_FUMA_SOFT;
- case AmbiLayout::ACN: return AL_ACN_SOFT;
- }
- throw std::runtime_error{fmt::format("Invalid AmbiLayout: {}",
- int{al::to_underlying(layout)})};
- }
- constexpr auto AmbiScalingFromEnum(ALenum scale) noexcept -> std::optional<AmbiScaling>
- {
- switch(scale)
- {
- case AL_FUMA_SOFT: return AmbiScaling::FuMa;
- case AL_SN3D_SOFT: return AmbiScaling::SN3D;
- case AL_N3D_SOFT: return AmbiScaling::N3D;
- }
- return std::nullopt;
- }
- constexpr auto EnumFromAmbiScaling(AmbiScaling scale) -> ALenum
- {
- switch(scale)
- {
- case AmbiScaling::FuMa: return AL_FUMA_SOFT;
- case AmbiScaling::SN3D: return AL_SN3D_SOFT;
- case AmbiScaling::N3D: return AL_N3D_SOFT;
- case AmbiScaling::UHJ: break;
- }
- throw std::runtime_error{fmt::format("Invalid AmbiScaling: {}",
- int{al::to_underlying(scale)})};
- }
- #if ALSOFT_EAX
- constexpr auto EaxStorageFromEnum(ALenum scale) noexcept -> std::optional<EaxStorage>
- {
- switch(scale)
- {
- case AL_STORAGE_AUTOMATIC: return EaxStorage::Automatic;
- case AL_STORAGE_ACCESSIBLE: return EaxStorage::Accessible;
- case AL_STORAGE_HARDWARE: return EaxStorage::Hardware;
- }
- return std::nullopt;
- }
- constexpr auto EnumFromEaxStorage(EaxStorage storage) -> ALenum
- {
- switch(storage)
- {
- case EaxStorage::Automatic: return AL_STORAGE_AUTOMATIC;
- case EaxStorage::Accessible: return AL_STORAGE_ACCESSIBLE;
- case EaxStorage::Hardware: return AL_STORAGE_HARDWARE;
- }
- throw std::runtime_error{fmt::format("Invalid EaxStorage: {}",
- int{al::to_underlying(storage)})};
- }
- auto eax_x_ram_check_availability(const al::Device &device, const ALbuffer &buffer,
- const ALuint newsize) noexcept -> bool
- {
- ALuint freemem{device.eax_x_ram_free_size};
- /* If the buffer is currently in "hardware", add its memory to the free
- * pool since it'll be "replaced".
- */
- if(buffer.eax_x_ram_is_hardware)
- freemem += buffer.OriginalSize;
- return freemem >= newsize;
- }
- void eax_x_ram_apply(al::Device &device, ALbuffer &buffer) noexcept
- {
- if(buffer.eax_x_ram_is_hardware)
- return;
- if(device.eax_x_ram_free_size >= buffer.OriginalSize)
- {
- device.eax_x_ram_free_size -= buffer.OriginalSize;
- buffer.eax_x_ram_is_hardware = true;
- }
- }
- void eax_x_ram_clear(al::Device& al_device, ALbuffer& al_buffer) noexcept
- {
- if(al_buffer.eax_x_ram_is_hardware)
- al_device.eax_x_ram_free_size += al_buffer.OriginalSize;
- al_buffer.eax_x_ram_is_hardware = false;
- }
- #endif // ALSOFT_EAX
- constexpr ALbitfieldSOFT INVALID_STORAGE_MASK{~unsigned(AL_MAP_READ_BIT_SOFT |
- AL_MAP_WRITE_BIT_SOFT | AL_MAP_PERSISTENT_BIT_SOFT | AL_PRESERVE_DATA_BIT_SOFT)};
- constexpr ALbitfieldSOFT MAP_READ_WRITE_FLAGS{AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT};
- constexpr ALbitfieldSOFT INVALID_MAP_FLAGS{~unsigned(AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT |
- AL_MAP_PERSISTENT_BIT_SOFT)};
- [[nodiscard]]
- auto EnsureBuffers(al::Device *device, size_t needed) noexcept -> bool
- try {
- size_t count{std::accumulate(device->BufferList.cbegin(), device->BufferList.cend(), 0_uz,
- [](size_t cur, const BufferSubList &sublist) noexcept -> size_t
- { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })};
- while(needed > count)
- {
- if(device->BufferList.size() >= 1<<25) UNLIKELY
- return false;
- BufferSubList sublist{};
- sublist.FreeMask = ~0_u64;
- sublist.Buffers = SubListAllocator{}.allocate(1);
- device->BufferList.emplace_back(std::move(sublist));
- count += std::tuple_size_v<SubListAllocator::value_type>;
- }
- return true;
- }
- catch(...) {
- return false;
- }
- [[nodiscard]]
- auto AllocBuffer(al::Device *device) noexcept -> ALbuffer*
- {
- auto sublist = std::find_if(device->BufferList.begin(), device->BufferList.end(),
- [](const BufferSubList &entry) noexcept -> bool
- { return entry.FreeMask != 0; });
- auto lidx = static_cast<ALuint>(std::distance(device->BufferList.begin(), sublist));
- auto slidx = static_cast<ALuint>(al::countr_zero(sublist->FreeMask));
- ASSUME(slidx < 64);
- ALbuffer *buffer{al::construct_at(al::to_address(sublist->Buffers->begin() + slidx))};
- /* Add 1 to avoid buffer ID 0. */
- buffer->id = ((lidx<<6) | slidx) + 1;
- sublist->FreeMask &= ~(1_u64 << slidx);
- return buffer;
- }
- void FreeBuffer(al::Device *device, ALbuffer *buffer)
- {
- #if ALSOFT_EAX
- eax_x_ram_clear(*device, *buffer);
- #endif // ALSOFT_EAX
- device->mBufferNames.erase(buffer->id);
- const ALuint id{buffer->id - 1};
- const size_t lidx{id >> 6};
- const ALuint slidx{id & 0x3f};
- std::destroy_at(buffer);
- device->BufferList[lidx].FreeMask |= 1_u64 << slidx;
- }
- [[nodiscard]]
- auto LookupBuffer(al::Device *device, ALuint id) noexcept -> ALbuffer*
- {
- const size_t lidx{(id-1) >> 6};
- const ALuint slidx{(id-1) & 0x3f};
- if(lidx >= device->BufferList.size()) UNLIKELY
- return nullptr;
- BufferSubList &sublist = device->BufferList[lidx];
- if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY
- return nullptr;
- return al::to_address(sublist.Buffers->begin() + slidx);
- }
- [[nodiscard]]
- constexpr auto SanitizeAlignment(FmtType type, ALuint align) noexcept -> ALuint
- {
- if(align == 0)
- {
- if(type == FmtIMA4)
- {
- /* Here is where things vary:
- * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
- * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
- */
- return 65;
- }
- if(type == FmtMSADPCM)
- return 64;
- return 1;
- }
- if(type == FmtIMA4)
- {
- /* IMA4 block alignment must be a multiple of 8, plus 1. */
- if((align&7) == 1) return align;
- return 0;
- }
- if(type == FmtMSADPCM)
- {
- /* MSADPCM block alignment must be a multiple of 2. */
- if((align&1) == 0) return align;
- return 0;
- }
- return align;
- }
- /** Loads the specified data into the buffer, using the specified format. */
- void LoadData(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq, ALuint size,
- const FmtChannels DstChannels, const FmtType DstType, const al::span<const std::byte> SrcData,
- ALbitfieldSOFT access)
- {
- if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0)
- context->throw_error(AL_INVALID_OPERATION, "Modifying storage for in-use buffer {}",
- ALBuf->id);
- const ALuint unpackalign{ALBuf->UnpackAlign};
- const ALuint align{SanitizeAlignment(DstType, unpackalign)};
- if(align < 1)
- context->throw_error(AL_INVALID_VALUE, "Invalid unpack alignment {} for {} samples",
- unpackalign, NameFromFormat(DstType));
- const ALuint ambiorder{IsBFormat(DstChannels) ? ALBuf->UnpackAmbiOrder :
- (IsUHJ(DstChannels) ? 1 : 0)};
- if(ambiorder > 3)
- {
- if(ALBuf->mAmbiLayout == AmbiLayout::FuMa)
- context->throw_error(AL_INVALID_OPERATION,
- "Cannot load {}{} order B-Format data with FuMa layout", ALBuf->mAmbiOrder,
- GetCounterSuffix(ALBuf->mAmbiOrder));
- if(ALBuf->mAmbiScaling == AmbiScaling::FuMa)
- context->throw_error(AL_INVALID_OPERATION,
- "Cannot load {}{} order B-Format data with FuMa scaling", ALBuf->mAmbiOrder,
- GetCounterSuffix(ALBuf->mAmbiOrder));
- }
- if((access&AL_PRESERVE_DATA_BIT_SOFT))
- {
- /* Can only preserve data with the same format and alignment. */
- if(ALBuf->mChannels != DstChannels || ALBuf->mType != DstType)
- context->throw_error(AL_INVALID_VALUE, "Preserving data of mismatched format");
- if(ALBuf->mBlockAlign != align)
- context->throw_error(AL_INVALID_VALUE, "Preserving data of mismatched alignment");
- if(ALBuf->mAmbiOrder != ambiorder)
- context->throw_error(AL_INVALID_VALUE, "Preserving data of mismatched order");
- }
- /* Convert the size in bytes to blocks using the unpack block alignment. */
- const ALuint NumChannels{ChannelsFromFmt(DstChannels, ambiorder)};
- const ALuint BlockSize{NumChannels *
- ((DstType == FmtIMA4) ? (align-1)/2 + 4 :
- (DstType == FmtMSADPCM) ? (align-2)/2 + 7 :
- (align * BytesFromFmt(DstType)))};
- if((size%BlockSize) != 0)
- context->throw_error(AL_INVALID_VALUE,
- "Data size {} is not a multiple of frame size {} ({} unpack alignment)",
- size, BlockSize, align);
- const ALuint blocks{size / BlockSize};
- if(blocks > std::numeric_limits<ALsizei>::max()/align)
- context->throw_error(AL_OUT_OF_MEMORY,
- "Buffer size overflow, {} blocks x {} samples per block", blocks, align);
- if(blocks > std::numeric_limits<size_t>::max()/BlockSize)
- context->throw_error(AL_OUT_OF_MEMORY,
- "Buffer size overflow, {} frames x {} bytes per frame", blocks, BlockSize);
- const size_t newsize{static_cast<size_t>(blocks) * BlockSize};
- #if ALSOFT_EAX
- if(ALBuf->eax_x_ram_mode == EaxStorage::Hardware)
- {
- auto &device = *context->mALDevice;
- if(!eax_x_ram_check_availability(device, *ALBuf, size))
- context->throw_error(AL_OUT_OF_MEMORY, "Out of X-RAM memory (avail: {}, needed: {})",
- device.eax_x_ram_free_size, size);
- }
- #endif
- /* This could reallocate only when increasing the size or the new size is
- * less than half the current, but then the buffer's AL_SIZE would not be
- * very reliable for accounting buffer memory usage, and reporting the real
- * size could cause problems for apps that use AL_SIZE to try to get the
- * buffer's play length.
- */
- if(newsize != ALBuf->mDataStorage.size())
- {
- auto newdata = decltype(ALBuf->mDataStorage)(newsize, std::byte{});
- if((access&AL_PRESERVE_DATA_BIT_SOFT))
- {
- const size_t tocopy{std::min(newdata.size(), ALBuf->mDataStorage.size())};
- std::copy_n(ALBuf->mDataStorage.begin(), tocopy, newdata.begin());
- }
- newdata.swap(ALBuf->mDataStorage);
- }
- ALBuf->mData = ALBuf->mDataStorage;
- #if ALSOFT_EAX
- eax_x_ram_clear(*context->mALDevice, *ALBuf);
- #endif
- if(!SrcData.empty() && !ALBuf->mData.empty())
- std::copy_n(SrcData.begin(), blocks*BlockSize, ALBuf->mData.begin());
- ALBuf->mBlockAlign = (DstType == FmtIMA4 || DstType == FmtMSADPCM) ? align : 1;
- ALBuf->OriginalSize = size;
- ALBuf->Access = access;
- ALBuf->mSampleRate = static_cast<ALuint>(freq);
- ALBuf->mChannels = DstChannels;
- ALBuf->mType = DstType;
- ALBuf->mAmbiOrder = ambiorder;
- ALBuf->mCallback = nullptr;
- ALBuf->mUserData = nullptr;
- ALBuf->mSampleLen = blocks * align;
- ALBuf->mLoopStart = 0;
- ALBuf->mLoopEnd = ALBuf->mSampleLen;
- #if ALSOFT_EAX
- if(eax_g_is_enabled && ALBuf->eax_x_ram_mode == EaxStorage::Hardware)
- eax_x_ram_apply(*context->mALDevice, *ALBuf);
- #endif
- }
- /** Prepares the buffer to use the specified callback, using the specified format. */
- void PrepareCallback(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq,
- const FmtChannels DstChannels, const FmtType DstType, ALBUFFERCALLBACKTYPESOFT callback,
- void *userptr)
- {
- if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0)
- context->throw_error(AL_INVALID_OPERATION, "Modifying callback for in-use buffer {}",
- ALBuf->id);
- const ALuint ambiorder{IsBFormat(DstChannels) ? ALBuf->UnpackAmbiOrder :
- (IsUHJ(DstChannels) ? 1 : 0)};
- const ALuint unpackalign{ALBuf->UnpackAlign};
- const ALuint align{SanitizeAlignment(DstType, unpackalign)};
- if(align < 1)
- context->throw_error(AL_INVALID_VALUE, "Invalid unpack alignment {} for {} samples",
- unpackalign, NameFromFormat(DstType));
- const ALuint BlockSize{ChannelsFromFmt(DstChannels, ambiorder) *
- ((DstType == FmtIMA4) ? (align-1)/2 + 4 :
- (DstType == FmtMSADPCM) ? (align-2)/2 + 7 :
- (align * BytesFromFmt(DstType)))};
- /* The maximum number of samples a callback buffer may need to store is a
- * full mixing line * max pitch * channel count, since it may need to hold
- * a full line's worth of sample frames before downsampling. An additional
- * MaxResamplerEdge is needed for "future" samples during resampling (the
- * voice will hold a history for the past samples).
- */
- static constexpr size_t line_size{DeviceBase::MixerLineSize*MaxPitch + MaxResamplerEdge};
- const size_t line_blocks{(line_size + align-1) / align};
- using BufferVectorType = decltype(ALBuf->mDataStorage);
- BufferVectorType(line_blocks*BlockSize).swap(ALBuf->mDataStorage);
- ALBuf->mData = ALBuf->mDataStorage;
- #if ALSOFT_EAX
- eax_x_ram_clear(*context->mALDevice, *ALBuf);
- #endif
- ALBuf->mCallback = callback;
- ALBuf->mUserData = userptr;
- ALBuf->OriginalSize = 0;
- ALBuf->Access = 0;
- ALBuf->mBlockAlign = (DstType == FmtIMA4 || DstType == FmtMSADPCM) ? align : 1;
- ALBuf->mSampleRate = static_cast<ALuint>(freq);
- ALBuf->mChannels = DstChannels;
- ALBuf->mType = DstType;
- ALBuf->mAmbiOrder = ambiorder;
- ALBuf->mSampleLen = 0;
- ALBuf->mLoopStart = 0;
- ALBuf->mLoopEnd = ALBuf->mSampleLen;
- }
- /** Prepares the buffer to use caller-specified storage. */
- void PrepareUserPtr(ALCcontext *context [[maybe_unused]], ALbuffer *ALBuf, ALsizei freq,
- const FmtChannels DstChannels, const FmtType DstType, std::byte *sdata, const ALuint sdatalen)
- {
- if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0)
- context->throw_error(AL_INVALID_OPERATION, "Modifying storage for in-use buffer {}",
- ALBuf->id);
- const ALuint unpackalign{ALBuf->UnpackAlign};
- const ALuint align{SanitizeAlignment(DstType, unpackalign)};
- if(align < 1)
- context->throw_error(AL_INVALID_VALUE, "Invalid unpack alignment {} for {} samples",
- unpackalign, NameFromFormat(DstType));
- auto get_type_alignment = [](const FmtType type) noexcept -> ALuint
- {
- /* NOTE: This only needs to be the required alignment for the CPU to
- * read/write the given sample type in the mixer.
- */
- switch(type)
- {
- case FmtUByte: return alignof(ALubyte);
- case FmtShort: return alignof(ALshort);
- case FmtInt: return alignof(ALint);
- case FmtFloat: return alignof(ALfloat);
- case FmtDouble: return alignof(ALdouble);
- case FmtMulaw: return alignof(ALubyte);
- case FmtAlaw: return alignof(ALubyte);
- case FmtIMA4: break;
- case FmtMSADPCM: break;
- }
- return 1;
- };
- const auto typealign = get_type_alignment(DstType);
- if((reinterpret_cast<uintptr_t>(sdata) & (typealign-1)) != 0)
- context->throw_error(AL_INVALID_VALUE, "Pointer {} is misaligned for {} samples ({})",
- static_cast<void*>(sdata), NameFromFormat(DstType), typealign);
- const ALuint ambiorder{IsBFormat(DstChannels) ? ALBuf->UnpackAmbiOrder :
- (IsUHJ(DstChannels) ? 1 : 0)};
- /* Convert the size in bytes to blocks using the unpack block alignment. */
- const ALuint NumChannels{ChannelsFromFmt(DstChannels, ambiorder)};
- const ALuint BlockSize{NumChannels *
- ((DstType == FmtIMA4) ? (align-1)/2 + 4 :
- (DstType == FmtMSADPCM) ? (align-2)/2 + 7 :
- (align * BytesFromFmt(DstType)))};
- if((sdatalen%BlockSize) != 0)
- context->throw_error(AL_INVALID_VALUE,
- "Data size {} is not a multiple of frame size {} ({} unpack alignment)",
- sdatalen, BlockSize, align);
- const ALuint blocks{sdatalen / BlockSize};
- if(blocks > std::numeric_limits<ALsizei>::max()/align)
- context->throw_error(AL_OUT_OF_MEMORY,
- "Buffer size overflow, {} blocks x {} samples per block", blocks, align);
- if(blocks > std::numeric_limits<size_t>::max()/BlockSize)
- context->throw_error(AL_OUT_OF_MEMORY,
- "Buffer size overflow, {} frames x {} bytes per frame", blocks, BlockSize);
- #if ALSOFT_EAX
- if(ALBuf->eax_x_ram_mode == EaxStorage::Hardware)
- {
- auto &device = *context->mALDevice;
- if(!eax_x_ram_check_availability(device, *ALBuf, sdatalen))
- context->throw_error(AL_OUT_OF_MEMORY, "Out of X-RAM memory (avail: {}, needed: {})",
- device.eax_x_ram_free_size, sdatalen);
- }
- #endif
- decltype(ALBuf->mDataStorage){}.swap(ALBuf->mDataStorage);
- ALBuf->mData = al::span{sdata, sdatalen};
- #if ALSOFT_EAX
- eax_x_ram_clear(*context->mALDevice, *ALBuf);
- #endif
- ALBuf->mCallback = nullptr;
- ALBuf->mUserData = nullptr;
- ALBuf->OriginalSize = sdatalen;
- ALBuf->Access = 0;
- ALBuf->mBlockAlign = (DstType == FmtIMA4 || DstType == FmtMSADPCM) ? align : 1;
- ALBuf->mSampleRate = static_cast<ALuint>(freq);
- ALBuf->mChannels = DstChannels;
- ALBuf->mType = DstType;
- ALBuf->mAmbiOrder = ambiorder;
- ALBuf->mSampleLen = blocks * align;
- ALBuf->mLoopStart = 0;
- ALBuf->mLoopEnd = ALBuf->mSampleLen;
- #if ALSOFT_EAX
- if(ALBuf->eax_x_ram_mode == EaxStorage::Hardware)
- eax_x_ram_apply(*context->mALDevice, *ALBuf);
- #endif
- }
- struct DecompResult { FmtChannels channels; FmtType type; };
- auto DecomposeUserFormat(ALenum format) noexcept -> std::optional<DecompResult>
- {
- struct FormatMap {
- ALenum format;
- DecompResult result;
- };
- static constexpr std::array UserFmtList{
- FormatMap{AL_FORMAT_MONO8, {FmtMono, FmtUByte} },
- FormatMap{AL_FORMAT_MONO16, {FmtMono, FmtShort} },
- FormatMap{AL_FORMAT_MONO_I32, {FmtMono, FmtInt} },
- FormatMap{AL_FORMAT_MONO_FLOAT32, {FmtMono, FmtFloat} },
- FormatMap{AL_FORMAT_MONO_DOUBLE_EXT, {FmtMono, FmtDouble} },
- FormatMap{AL_FORMAT_MONO_IMA4, {FmtMono, FmtIMA4} },
- FormatMap{AL_FORMAT_MONO_MSADPCM_SOFT, {FmtMono, FmtMSADPCM}},
- FormatMap{AL_FORMAT_MONO_MULAW, {FmtMono, FmtMulaw} },
- FormatMap{AL_FORMAT_MONO_ALAW_EXT, {FmtMono, FmtAlaw} },
- FormatMap{AL_FORMAT_STEREO8, {FmtStereo, FmtUByte} },
- FormatMap{AL_FORMAT_STEREO16, {FmtStereo, FmtShort} },
- FormatMap{AL_FORMAT_STEREO_I32, {FmtStereo, FmtInt} },
- FormatMap{AL_FORMAT_STEREO_FLOAT32, {FmtStereo, FmtFloat} },
- FormatMap{AL_FORMAT_STEREO_DOUBLE_EXT, {FmtStereo, FmtDouble} },
- FormatMap{AL_FORMAT_STEREO_IMA4, {FmtStereo, FmtIMA4} },
- FormatMap{AL_FORMAT_STEREO_MSADPCM_SOFT, {FmtStereo, FmtMSADPCM}},
- FormatMap{AL_FORMAT_STEREO_MULAW, {FmtStereo, FmtMulaw} },
- FormatMap{AL_FORMAT_STEREO_ALAW_EXT, {FmtStereo, FmtAlaw} },
- FormatMap{AL_FORMAT_REAR8, {FmtRear, FmtUByte}},
- FormatMap{AL_FORMAT_REAR16, {FmtRear, FmtShort}},
- FormatMap{AL_FORMAT_REAR32, {FmtRear, FmtFloat}},
- FormatMap{AL_FORMAT_REAR_I32, {FmtRear, FmtInt} },
- FormatMap{AL_FORMAT_REAR_FLOAT32, {FmtRear, FmtFloat}},
- FormatMap{AL_FORMAT_REAR_MULAW, {FmtRear, FmtMulaw}},
- FormatMap{AL_FORMAT_QUAD8_LOKI, {FmtQuad, FmtUByte}},
- FormatMap{AL_FORMAT_QUAD16_LOKI, {FmtQuad, FmtShort}},
- FormatMap{AL_FORMAT_QUAD8, {FmtQuad, FmtUByte}},
- FormatMap{AL_FORMAT_QUAD16, {FmtQuad, FmtShort}},
- FormatMap{AL_FORMAT_QUAD32, {FmtQuad, FmtFloat}},
- FormatMap{AL_FORMAT_QUAD_I32, {FmtQuad, FmtInt} },
- FormatMap{AL_FORMAT_QUAD_FLOAT32, {FmtQuad, FmtFloat}},
- FormatMap{AL_FORMAT_QUAD_MULAW, {FmtQuad, FmtMulaw}},
- FormatMap{AL_FORMAT_51CHN8, {FmtX51, FmtUByte}},
- FormatMap{AL_FORMAT_51CHN16, {FmtX51, FmtShort}},
- FormatMap{AL_FORMAT_51CHN32, {FmtX51, FmtFloat}},
- FormatMap{AL_FORMAT_51CHN_I32, {FmtX51, FmtInt} },
- FormatMap{AL_FORMAT_51CHN_FLOAT32, {FmtX51, FmtFloat}},
- FormatMap{AL_FORMAT_51CHN_MULAW, {FmtX51, FmtMulaw}},
- FormatMap{AL_FORMAT_61CHN8, {FmtX61, FmtUByte}},
- FormatMap{AL_FORMAT_61CHN16, {FmtX61, FmtShort}},
- FormatMap{AL_FORMAT_61CHN32, {FmtX61, FmtFloat}},
- FormatMap{AL_FORMAT_61CHN_I32, {FmtX61, FmtInt} },
- FormatMap{AL_FORMAT_61CHN_FLOAT32, {FmtX61, FmtFloat}},
- FormatMap{AL_FORMAT_61CHN_MULAW, {FmtX61, FmtMulaw}},
- FormatMap{AL_FORMAT_71CHN8, {FmtX71, FmtUByte}},
- FormatMap{AL_FORMAT_71CHN16, {FmtX71, FmtShort}},
- FormatMap{AL_FORMAT_71CHN32, {FmtX71, FmtFloat}},
- FormatMap{AL_FORMAT_71CHN_I32, {FmtX71, FmtInt} },
- FormatMap{AL_FORMAT_71CHN_FLOAT32, {FmtX71, FmtFloat}},
- FormatMap{AL_FORMAT_71CHN_MULAW, {FmtX71, FmtMulaw}},
- FormatMap{AL_FORMAT_BFORMAT2D_8, {FmtBFormat2D, FmtUByte}},
- FormatMap{AL_FORMAT_BFORMAT2D_16, {FmtBFormat2D, FmtShort}},
- FormatMap{AL_FORMAT_BFORMAT2D_I32, {FmtBFormat2D, FmtInt} },
- FormatMap{AL_FORMAT_BFORMAT2D_FLOAT32, {FmtBFormat2D, FmtFloat}},
- FormatMap{AL_FORMAT_BFORMAT2D_MULAW, {FmtBFormat2D, FmtMulaw}},
- FormatMap{AL_FORMAT_BFORMAT3D_8, {FmtBFormat3D, FmtUByte}},
- FormatMap{AL_FORMAT_BFORMAT3D_16, {FmtBFormat3D, FmtShort}},
- FormatMap{AL_FORMAT_BFORMAT3D_I32, {FmtBFormat3D, FmtInt} },
- FormatMap{AL_FORMAT_BFORMAT3D_FLOAT32, {FmtBFormat3D, FmtFloat}},
- FormatMap{AL_FORMAT_BFORMAT3D_MULAW, {FmtBFormat3D, FmtMulaw}},
- FormatMap{AL_FORMAT_UHJ2CHN8_SOFT, {FmtUHJ2, FmtUByte} },
- FormatMap{AL_FORMAT_UHJ2CHN16_SOFT, {FmtUHJ2, FmtShort} },
- FormatMap{AL_FORMAT_UHJ2CHN_I32_SOFT, {FmtUHJ2, FmtInt} },
- FormatMap{AL_FORMAT_UHJ2CHN_FLOAT32_SOFT, {FmtUHJ2, FmtFloat} },
- FormatMap{AL_FORMAT_UHJ2CHN_MULAW_SOFT, {FmtUHJ2, FmtMulaw} },
- FormatMap{AL_FORMAT_UHJ2CHN_ALAW_SOFT, {FmtUHJ2, FmtAlaw} },
- FormatMap{AL_FORMAT_UHJ2CHN_IMA4_SOFT, {FmtUHJ2, FmtIMA4} },
- FormatMap{AL_FORMAT_UHJ2CHN_MSADPCM_SOFT, {FmtUHJ2, FmtMSADPCM}},
- FormatMap{AL_FORMAT_UHJ3CHN8_SOFT, {FmtUHJ3, FmtUByte}},
- FormatMap{AL_FORMAT_UHJ3CHN16_SOFT, {FmtUHJ3, FmtShort}},
- FormatMap{AL_FORMAT_UHJ3CHN_I32_SOFT, {FmtUHJ3, FmtInt} },
- FormatMap{AL_FORMAT_UHJ3CHN_FLOAT32_SOFT, {FmtUHJ3, FmtFloat}},
- FormatMap{AL_FORMAT_UHJ3CHN_MULAW_SOFT, {FmtUHJ3, FmtMulaw}},
- FormatMap{AL_FORMAT_UHJ3CHN_ALAW_SOFT, {FmtUHJ3, FmtAlaw} },
- FormatMap{AL_FORMAT_UHJ4CHN8_SOFT, {FmtUHJ4, FmtUByte}},
- FormatMap{AL_FORMAT_UHJ4CHN16_SOFT, {FmtUHJ4, FmtShort}},
- FormatMap{AL_FORMAT_UHJ4CHN_I32_SOFT, {FmtUHJ4, FmtInt} },
- FormatMap{AL_FORMAT_UHJ4CHN_FLOAT32_SOFT, {FmtUHJ4, FmtFloat}},
- FormatMap{AL_FORMAT_UHJ4CHN_MULAW_SOFT, {FmtUHJ4, FmtMulaw}},
- FormatMap{AL_FORMAT_UHJ4CHN_ALAW_SOFT, {FmtUHJ4, FmtAlaw} },
- };
- auto iter = std::find_if(UserFmtList.cbegin(), UserFmtList.cend(),
- [format](const FormatMap &fmt) noexcept { return fmt.format == format; });
- if(iter != UserFmtList.cend())
- return iter->result;
- return std::nullopt;
- }
- } // namespace
- AL_API DECL_FUNC2(void, alGenBuffers, ALsizei,n, ALuint*,buffers)
- FORCE_ALIGN void AL_APIENTRY alGenBuffersDirect(ALCcontext *context, ALsizei n, ALuint *buffers) noexcept
- try {
- if(n < 0)
- context->throw_error(AL_INVALID_VALUE, "Generating {} buffers", n);
- if(n <= 0) UNLIKELY return;
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- const al::span bids{buffers, static_cast<ALuint>(n)};
- if(!EnsureBuffers(device, bids.size()))
- context->throw_error(AL_OUT_OF_MEMORY, "Failed to allocate {} buffer{}", n,
- (n==1) ? "" : "s");
- std::generate(bids.begin(), bids.end(), [device]{ return AllocBuffer(device)->id; });
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNC2(void, alDeleteBuffers, ALsizei,n, const ALuint*,buffers)
- FORCE_ALIGN void AL_APIENTRY alDeleteBuffersDirect(ALCcontext *context, ALsizei n,
- const ALuint *buffers) noexcept
- try {
- if(n < 0)
- context->throw_error(AL_INVALID_VALUE, "Deleting {} buffers", n);
- if(n <= 0) UNLIKELY return;
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- /* First try to find any buffers that are invalid or in-use. */
- auto validate_buffer = [context,device](const ALuint bid)
- {
- if(!bid) return;
- ALbuffer *ALBuf{LookupBuffer(device, bid)};
- if(!ALBuf)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", bid);
- if(ALBuf->ref.load(std::memory_order_relaxed) != 0)
- context->throw_error(AL_INVALID_OPERATION, "Deleting in-use buffer {}", bid);
- };
- const al::span bids{buffers, static_cast<ALuint>(n)};
- std::for_each(bids.begin(), bids.end(), validate_buffer);
- /* All good. Delete non-0 buffer IDs. */
- auto delete_buffer = [device](const ALuint bid) -> void
- {
- if(ALbuffer *buffer{bid ? LookupBuffer(device, bid) : nullptr})
- FreeBuffer(device, buffer);
- };
- std::for_each(bids.begin(), bids.end(), delete_buffer);
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNC1(ALboolean, alIsBuffer, ALuint,buffer)
- FORCE_ALIGN ALboolean AL_APIENTRY alIsBufferDirect(ALCcontext *context, ALuint buffer) noexcept
- {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- if(!buffer || LookupBuffer(device, buffer))
- return AL_TRUE;
- return AL_FALSE;
- }
- AL_API void AL_APIENTRY alBufferData(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq) noexcept
- {
- auto context = GetContextRef();
- if(!context) UNLIKELY return;
- alBufferStorageDirectSOFT(context.get(), buffer, format, data, size, freq, 0);
- }
- FORCE_ALIGN void AL_APIENTRY alBufferDataDirect(ALCcontext *context, ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq) noexcept
- { alBufferStorageDirectSOFT(context, buffer, format, data, size, freq, 0); }
- AL_API DECL_FUNCEXT6(void, alBufferStorage,SOFT, ALuint,buffer, ALenum,format, const ALvoid*,data, ALsizei,size, ALsizei,freq, ALbitfieldSOFT,flags)
- FORCE_ALIGN void AL_APIENTRY alBufferStorageDirectSOFT(ALCcontext *context, ALuint buffer,
- ALenum format, const ALvoid *data, ALsizei size, ALsizei freq, ALbitfieldSOFT flags) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- ALbuffer *albuf{LookupBuffer(device, buffer)};
- if(!albuf)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(size < 0)
- context->throw_error(AL_INVALID_VALUE, "Negative storage size {}", size);
- if(freq < 1)
- context->throw_error(AL_INVALID_VALUE, "Invalid sample rate {}", freq);
- if((flags&INVALID_STORAGE_MASK) != 0)
- context->throw_error(AL_INVALID_VALUE, "Invalid storage flags {:#x}",
- flags&INVALID_STORAGE_MASK);
- if((flags&AL_MAP_PERSISTENT_BIT_SOFT) && !(flags&MAP_READ_WRITE_FLAGS))
- context->throw_error(AL_INVALID_VALUE,
- "Declaring persistently mapped storage without read or write access");
- auto usrfmt = DecomposeUserFormat(format);
- if(!usrfmt)
- context->throw_error(AL_INVALID_ENUM, "Invalid format {:#04x}", as_unsigned(format));
- auto bdata = static_cast<const std::byte*>(data);
- LoadData(context, albuf, freq, static_cast<ALuint>(size), usrfmt->channels, usrfmt->type,
- al::span{bdata, bdata ? static_cast<ALuint>(size) : 0u}, flags);
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- FORCE_ALIGN DECL_FUNC5(void, alBufferDataStatic, ALuint,buffer, ALenum,format, ALvoid*,data, ALsizei,size, ALsizei,freq)
- FORCE_ALIGN void AL_APIENTRY alBufferDataStaticDirect(ALCcontext *context, const ALuint buffer,
- ALenum format, ALvoid *data, ALsizei size, ALsizei freq) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- ALbuffer *albuf{LookupBuffer(device, buffer)};
- if(!albuf)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(size < 0)
- context->throw_error(AL_INVALID_VALUE, "Negative storage size {}", size);
- if(freq < 1)
- context->throw_error(AL_INVALID_VALUE, "Invalid sample rate {}", freq);
- auto usrfmt = DecomposeUserFormat(format);
- if(!usrfmt)
- context->throw_error(AL_INVALID_ENUM, "Invalid format {:#04x}", as_unsigned(format));
- PrepareUserPtr(context, albuf, freq, usrfmt->channels, usrfmt->type,
- static_cast<std::byte*>(data), static_cast<ALuint>(size));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNCEXT4(void*, alMapBuffer,SOFT, ALuint,buffer, ALsizei,offset, ALsizei,length, ALbitfieldSOFT,access)
- FORCE_ALIGN void* AL_APIENTRY alMapBufferDirectSOFT(ALCcontext *context, ALuint buffer,
- ALsizei offset, ALsizei length, ALbitfieldSOFT access) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- ALbuffer *albuf{LookupBuffer(device, buffer)};
- if(!albuf)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if((access&INVALID_MAP_FLAGS) != 0)
- context->throw_error(AL_INVALID_VALUE, "Invalid map flags {:#x}",
- access&INVALID_MAP_FLAGS);
- if(!(access&MAP_READ_WRITE_FLAGS))
- context->throw_error(AL_INVALID_VALUE, "Mapping buffer {} without read or write access",
- buffer);
- const ALbitfieldSOFT unavailable{(albuf->Access^access) & access};
- if(albuf->ref.load(std::memory_order_relaxed) != 0 && !(access&AL_MAP_PERSISTENT_BIT_SOFT))
- context->throw_error(AL_INVALID_OPERATION,
- "Mapping in-use buffer {} without persistent mapping", buffer);
- if(albuf->MappedAccess != 0)
- context->throw_error(AL_INVALID_OPERATION, "Mapping already-mapped buffer {}", buffer);
- if((unavailable&AL_MAP_READ_BIT_SOFT))
- context->throw_error(AL_INVALID_VALUE, "Mapping buffer {} for reading without read access",
- buffer);
- if((unavailable&AL_MAP_WRITE_BIT_SOFT))
- context->throw_error(AL_INVALID_VALUE,
- "Mapping buffer {} for writing without write access", buffer);
- if((unavailable&AL_MAP_PERSISTENT_BIT_SOFT))
- context->throw_error(AL_INVALID_VALUE,
- "Mapping buffer {} persistently without persistent access", buffer);
- if(offset < 0 || length <= 0 || static_cast<ALuint>(offset) >= albuf->OriginalSize
- || static_cast<ALuint>(length) > albuf->OriginalSize - static_cast<ALuint>(offset))
- context->throw_error(AL_INVALID_VALUE, "Mapping invalid range {}+{} for buffer {}", offset,
- length, buffer);
- void *retval{albuf->mData.data() + offset};
- albuf->MappedAccess = access;
- albuf->MappedOffset = offset;
- albuf->MappedSize = length;
- return retval;
- }
- catch(al::base_exception&) {
- return nullptr;
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- return nullptr;
- }
- AL_API DECL_FUNCEXT1(void, alUnmapBuffer,SOFT, ALuint,buffer)
- FORCE_ALIGN void AL_APIENTRY alUnmapBufferDirectSOFT(ALCcontext *context, ALuint buffer) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- ALbuffer *albuf{LookupBuffer(device, buffer)};
- if(!albuf)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(albuf->MappedAccess == 0)
- context->throw_error(AL_INVALID_OPERATION, "Unmapping unmapped buffer {}", buffer);
- albuf->MappedAccess = 0;
- albuf->MappedOffset = 0;
- albuf->MappedSize = 0;
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNCEXT3(void, alFlushMappedBuffer,SOFT, ALuint,buffer, ALsizei,offset, ALsizei,length)
- FORCE_ALIGN void AL_APIENTRY alFlushMappedBufferDirectSOFT(ALCcontext *context, ALuint buffer,
- ALsizei offset, ALsizei length) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- ALbuffer *albuf{LookupBuffer(device, buffer)};
- if(!albuf)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(!(albuf->MappedAccess&AL_MAP_WRITE_BIT_SOFT))
- context->throw_error(AL_INVALID_OPERATION,
- "Flushing buffer {} while not mapped for writing", buffer);
- if(offset < albuf->MappedOffset || length <= 0
- || offset >= albuf->MappedOffset+albuf->MappedSize
- || length > albuf->MappedOffset+albuf->MappedSize-offset)
- context->throw_error(AL_INVALID_VALUE, "Flushing invalid range {}+{} on buffer {}", offset,
- length, buffer);
- /* FIXME: Need to use some method of double-buffering for the mixer and app
- * to hold separate memory, which can be safely transferred asynchronously.
- * Currently we just say the app shouldn't write where OpenAL's reading,
- * and hope for the best...
- */
- std::atomic_thread_fence(std::memory_order_seq_cst);
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNCEXT5(void, alBufferSubData,SOFT, ALuint,buffer, ALenum,format, const ALvoid*,data, ALsizei,offset, ALsizei,length)
- FORCE_ALIGN void AL_APIENTRY alBufferSubDataDirectSOFT(ALCcontext *context, ALuint buffer,
- ALenum format, const ALvoid *data, ALsizei offset, ALsizei length) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- ALbuffer *albuf{LookupBuffer(device, buffer)};
- if(!albuf)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- auto usrfmt = DecomposeUserFormat(format);
- if(!usrfmt)
- context->throw_error(AL_INVALID_ENUM, "Invalid format {:#04x}", as_unsigned(format));
- const ALuint unpack_align{albuf->UnpackAlign};
- const ALuint align{SanitizeAlignment(usrfmt->type, unpack_align)};
- if(align < 1)
- context->throw_error(AL_INVALID_VALUE, "Invalid unpack alignment {}", unpack_align);
- if(usrfmt->channels != albuf->mChannels || usrfmt->type != albuf->mType)
- context->throw_error(AL_INVALID_ENUM, "Unpacking data with mismatched format");
- if(align != albuf->mBlockAlign)
- context->throw_error(AL_INVALID_VALUE,
- "Unpacking data with alignment {} does not match original alignment {}", align,
- albuf->mBlockAlign);
- if(albuf->isBFormat() && albuf->UnpackAmbiOrder != albuf->mAmbiOrder)
- context->throw_error(AL_INVALID_VALUE, "Unpacking data with mismatched ambisonic order");
- if(albuf->MappedAccess != 0)
- context->throw_error(AL_INVALID_OPERATION, "Unpacking data into mapped buffer {}", buffer);
- const ALuint num_chans{albuf->channelsFromFmt()};
- const ALuint byte_align{
- (albuf->mType == FmtIMA4) ? ((align-1)/2 + 4) * num_chans :
- (albuf->mType == FmtMSADPCM) ? ((align-2)/2 + 7) * num_chans :
- (align * albuf->bytesFromFmt() * num_chans)};
- if(offset < 0 || length < 0 || static_cast<ALuint>(offset) > albuf->OriginalSize
- || static_cast<ALuint>(length) > albuf->OriginalSize-static_cast<ALuint>(offset))
- context->throw_error(AL_INVALID_VALUE, "Invalid data sub-range {}+{} on buffer {}", offset,
- length, buffer);
- if((static_cast<ALuint>(offset)%byte_align) != 0)
- context->throw_error(AL_INVALID_VALUE,
- "Sub-range offset {} is not a multiple of frame size {} ({} unpack alignment)",
- offset, byte_align, align);
- if((static_cast<ALuint>(length)%byte_align) != 0)
- context->throw_error(AL_INVALID_VALUE,
- "Sub-range length {} is not a multiple of frame size {} ({} unpack alignment)",
- length, byte_align, align);
- std::memcpy(albuf->mData.data()+offset, data, static_cast<ALuint>(length));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNC3(void, alBufferf, ALuint,buffer, ALenum,param, ALfloat,value)
- FORCE_ALIGN void AL_APIENTRY alBufferfDirect(ALCcontext *context, ALuint buffer, ALenum param,
- ALfloat value [[maybe_unused]]) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- if(LookupBuffer(device, buffer) == nullptr)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer float property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNC5(void, alBuffer3f, ALuint,buffer, ALenum,param, ALfloat,value1, ALfloat,value2, ALfloat,value3)
- FORCE_ALIGN void AL_APIENTRY alBuffer3fDirect(ALCcontext *context, ALuint buffer, ALenum param,
- ALfloat value1 [[maybe_unused]], ALfloat value2 [[maybe_unused]],
- ALfloat value3 [[maybe_unused]]) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- if(LookupBuffer(device, buffer) == nullptr)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer 3-float property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNC3(void, alBufferfv, ALuint,buffer, ALenum,param, const ALfloat*,values)
- FORCE_ALIGN void AL_APIENTRY alBufferfvDirect(ALCcontext *context, ALuint buffer, ALenum param,
- const ALfloat *values) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- if(LookupBuffer(device, buffer) == nullptr)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(!values)
- context->throw_error(AL_INVALID_VALUE, "NULL pointer");
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer float-vector property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNC3(void, alBufferi, ALuint,buffer, ALenum,param, ALint,value)
- FORCE_ALIGN void AL_APIENTRY alBufferiDirect(ALCcontext *context, ALuint buffer, ALenum param,
- ALint value) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- ALbuffer *albuf{LookupBuffer(device, buffer)};
- if(!albuf)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- switch(param)
- {
- case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
- if(value < 0)
- context->throw_error(AL_INVALID_VALUE, "Invalid unpack block alignment {}", value);
- albuf->UnpackAlign = static_cast<ALuint>(value);
- return;
- case AL_PACK_BLOCK_ALIGNMENT_SOFT:
- if(value < 0)
- context->throw_error(AL_INVALID_VALUE, "Invalid pack block alignment {}", value);
- albuf->PackAlign = static_cast<ALuint>(value);
- return;
- case AL_AMBISONIC_LAYOUT_SOFT:
- if(albuf->ref.load(std::memory_order_relaxed) != 0)
- context->throw_error(AL_INVALID_OPERATION,
- "Modifying in-use buffer {}'s ambisonic layout", buffer);
- if(const auto layout = AmbiLayoutFromEnum(value))
- {
- if(layout.value() == AmbiLayout::FuMa && albuf->mAmbiOrder > 3)
- context->throw_error(AL_INVALID_OPERATION,
- "Cannot set FuMa layout for {}{} order B-Format data", albuf->mAmbiOrder,
- GetCounterSuffix(albuf->mAmbiOrder));
- albuf->mAmbiLayout = layout.value();
- return;
- }
- context->throw_error(AL_INVALID_VALUE, "Invalid unpack ambisonic layout {:#04x}",
- as_unsigned(value));
- case AL_AMBISONIC_SCALING_SOFT:
- if(albuf->ref.load(std::memory_order_relaxed) != 0)
- context->throw_error(AL_INVALID_OPERATION,
- "Modifying in-use buffer {}'s ambisonic scaling", buffer);
- if(const auto scaling = AmbiScalingFromEnum(value))
- {
- if(scaling.value() == AmbiScaling::FuMa && albuf->mAmbiOrder > 3)
- context->throw_error(AL_INVALID_OPERATION,
- "Cannot set FuMa scaling for {}{} order B-Format data", albuf->mAmbiOrder,
- GetCounterSuffix(albuf->mAmbiOrder));
- albuf->mAmbiScaling = scaling.value();
- return;
- }
- context->throw_error(AL_INVALID_VALUE, "Invalid unpack ambisonic scaling {:#04x}",
- as_unsigned(value));
- case AL_UNPACK_AMBISONIC_ORDER_SOFT:
- if(value < 1 || value > 14)
- context->throw_error(AL_INVALID_VALUE, "Invalid unpack ambisonic order {}", value);
- albuf->UnpackAmbiOrder = static_cast<ALuint>(value);
- return;
- }
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer integer property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNC5(void, alBuffer3i, ALuint,buffer, ALenum,param, ALint,value1, ALint,value2, ALint,value3)
- FORCE_ALIGN void AL_APIENTRY alBuffer3iDirect(ALCcontext *context, ALuint buffer, ALenum param,
- ALint value1 [[maybe_unused]], ALint value2 [[maybe_unused]], ALint value3 [[maybe_unused]]) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- if(LookupBuffer(device, buffer) == nullptr)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer 3-integer property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNC3(void, alBufferiv, ALuint,buffer, ALenum,param, const ALint*,values)
- FORCE_ALIGN void AL_APIENTRY alBufferivDirect(ALCcontext *context, ALuint buffer, ALenum param,
- const ALint *values) noexcept
- try {
- if(!values)
- context->throw_error(AL_INVALID_VALUE, "NULL pointer");
- switch(param)
- {
- case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
- case AL_PACK_BLOCK_ALIGNMENT_SOFT:
- case AL_AMBISONIC_LAYOUT_SOFT:
- case AL_AMBISONIC_SCALING_SOFT:
- case AL_UNPACK_AMBISONIC_ORDER_SOFT:
- alBufferiDirect(context, buffer, param, *values);
- return;
- }
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- ALbuffer *albuf{LookupBuffer(device, buffer)};
- if(!albuf)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- switch(param)
- {
- case AL_LOOP_POINTS_SOFT:
- auto vals = al::span{values, 2_uz};
- if(albuf->ref.load(std::memory_order_relaxed) != 0)
- context->throw_error(AL_INVALID_OPERATION, "Modifying in-use buffer {}'s loop points",
- buffer);
- if(vals[0] < 0 || vals[0] >= vals[1] || static_cast<ALuint>(vals[1]) > albuf->mSampleLen)
- context->throw_error(AL_INVALID_VALUE,
- "Invalid loop point range {} -> {} on buffer {}", vals[0], vals[1], buffer);
- albuf->mLoopStart = static_cast<ALuint>(vals[0]);
- albuf->mLoopEnd = static_cast<ALuint>(vals[1]);
- return;
- }
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer integer-vector property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNC3(void, alGetBufferf, ALuint,buffer, ALenum,param, ALfloat*,value)
- FORCE_ALIGN void AL_APIENTRY alGetBufferfDirect(ALCcontext *context, ALuint buffer, ALenum param,
- ALfloat *value) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- ALbuffer *albuf{LookupBuffer(device, buffer)};
- if(!albuf)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(!value)
- context->throw_error(AL_INVALID_VALUE, "NULL pointer");
- switch(param)
- {
- case AL_SEC_LENGTH_SOFT:
- *value = (albuf->mSampleRate < 1) ? 0.0f :
- (static_cast<float>(albuf->mSampleLen) / static_cast<float>(albuf->mSampleRate));
- return;
- }
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer float property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNC5(void, alGetBuffer3f, ALuint,buffer, ALenum,param, ALfloat*,value1, ALfloat*,value2, ALfloat*,value3)
- FORCE_ALIGN void AL_APIENTRY alGetBuffer3fDirect(ALCcontext *context, ALuint buffer, ALenum param,
- ALfloat *value1, ALfloat *value2, ALfloat *value3) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- if(LookupBuffer(device, buffer) == nullptr)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(!value1 || !value2 || !value3)
- context->throw_error(AL_INVALID_VALUE, "NULL pointer");
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer 3-float property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNC3(void, alGetBufferfv, ALuint,buffer, ALenum,param, ALfloat*,values)
- FORCE_ALIGN void AL_APIENTRY alGetBufferfvDirect(ALCcontext *context, ALuint buffer, ALenum param,
- ALfloat *values) noexcept
- try {
- switch(param)
- {
- case AL_SEC_LENGTH_SOFT:
- alGetBufferfDirect(context, buffer, param, values);
- return;
- }
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- if(LookupBuffer(device, buffer) == nullptr)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(!values)
- context->throw_error(AL_INVALID_VALUE, "NULL pointer");
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer float-vector property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNC3(void, alGetBufferi, ALuint,buffer, ALenum,param, ALint*,value)
- FORCE_ALIGN void AL_APIENTRY alGetBufferiDirect(ALCcontext *context, ALuint buffer, ALenum param,
- ALint *value) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- ALbuffer *albuf{LookupBuffer(device, buffer)};
- if(!albuf)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(!value)
- context->throw_error(AL_INVALID_VALUE, "NULL pointer");
- switch(param)
- {
- case AL_FREQUENCY:
- *value = static_cast<ALint>(albuf->mSampleRate);
- return;
- case AL_BITS:
- *value = (albuf->mType == FmtIMA4 || albuf->mType == FmtMSADPCM) ? 4
- : static_cast<ALint>(albuf->bytesFromFmt() * 8);
- return;
- case AL_CHANNELS:
- *value = static_cast<ALint>(albuf->channelsFromFmt());
- return;
- case AL_SIZE:
- *value = albuf->mCallback ? 0 : static_cast<ALint>(albuf->mData.size());
- return;
- case AL_BYTE_LENGTH_SOFT:
- *value = static_cast<ALint>(albuf->mSampleLen / albuf->mBlockAlign
- * albuf->blockSizeFromFmt());
- return;
- case AL_SAMPLE_LENGTH_SOFT:
- *value = static_cast<ALint>(albuf->mSampleLen);
- return;
- case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
- *value = static_cast<ALint>(albuf->UnpackAlign);
- return;
- case AL_PACK_BLOCK_ALIGNMENT_SOFT:
- *value = static_cast<ALint>(albuf->PackAlign);
- return;
- case AL_AMBISONIC_LAYOUT_SOFT:
- *value = EnumFromAmbiLayout(albuf->mAmbiLayout);
- return;
- case AL_AMBISONIC_SCALING_SOFT:
- *value = EnumFromAmbiScaling(albuf->mAmbiScaling);
- return;
- case AL_UNPACK_AMBISONIC_ORDER_SOFT:
- *value = static_cast<int>(albuf->UnpackAmbiOrder);
- return;
- }
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer integer property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNC5(void, alGetBuffer3i, ALuint,buffer, ALenum,param, ALint*,value1, ALint*,value2, ALint*,value3)
- FORCE_ALIGN void AL_APIENTRY alGetBuffer3iDirect(ALCcontext *context, ALuint buffer, ALenum param,
- ALint *value1, ALint *value2, ALint *value3) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- if(LookupBuffer(device, buffer) == nullptr)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(!value1 || !value2 || !value3)
- context->throw_error(AL_INVALID_VALUE, "NULL pointer");
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer 3-integer property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNC3(void, alGetBufferiv, ALuint,buffer, ALenum,param, ALint*,values)
- FORCE_ALIGN void AL_APIENTRY alGetBufferivDirect(ALCcontext *context, ALuint buffer, ALenum param,
- ALint *values) noexcept
- try {
- switch(param)
- {
- case AL_FREQUENCY:
- case AL_BITS:
- case AL_CHANNELS:
- case AL_SIZE:
- case AL_INTERNAL_FORMAT_SOFT:
- case AL_BYTE_LENGTH_SOFT:
- case AL_SAMPLE_LENGTH_SOFT:
- case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
- case AL_PACK_BLOCK_ALIGNMENT_SOFT:
- case AL_AMBISONIC_LAYOUT_SOFT:
- case AL_AMBISONIC_SCALING_SOFT:
- case AL_UNPACK_AMBISONIC_ORDER_SOFT:
- alGetBufferiDirect(context, buffer, param, values);
- return;
- }
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- ALbuffer *albuf{LookupBuffer(device, buffer)};
- if(!albuf)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(!values)
- context->throw_error(AL_INVALID_VALUE, "NULL pointer");
- switch(param)
- {
- case AL_LOOP_POINTS_SOFT:
- auto vals = al::span{values, 2_uz};
- vals[0] = static_cast<ALint>(albuf->mLoopStart);
- vals[1] = static_cast<ALint>(albuf->mLoopEnd);
- return;
- }
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer integer-vector property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNCEXT5(void, alBufferCallback,SOFT, ALuint,buffer, ALenum,format, ALsizei,freq, ALBUFFERCALLBACKTYPESOFT,callback, ALvoid*,userptr)
- FORCE_ALIGN void AL_APIENTRY alBufferCallbackDirectSOFT(ALCcontext *context, ALuint buffer,
- ALenum format, ALsizei freq, ALBUFFERCALLBACKTYPESOFT callback, ALvoid *userptr) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- ALbuffer *albuf{LookupBuffer(device, buffer)};
- if(!albuf)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(freq < 1)
- context->throw_error(AL_INVALID_VALUE, "Invalid sample rate {}", freq);
- if(callback == nullptr)
- context->throw_error(AL_INVALID_VALUE, "NULL callback");
- auto usrfmt = DecomposeUserFormat(format);
- if(!usrfmt)
- context->throw_error(AL_INVALID_ENUM, "Invalid format {:#04x}", as_unsigned(format));
- PrepareCallback(context, albuf, freq, usrfmt->channels, usrfmt->type, callback, userptr);
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNCEXT3(void, alGetBufferPtr,SOFT, ALuint,buffer, ALenum,param, ALvoid**,value)
- FORCE_ALIGN void AL_APIENTRY alGetBufferPtrDirectSOFT(ALCcontext *context, ALuint buffer,
- ALenum param, ALvoid **value) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- ALbuffer *albuf{LookupBuffer(device, buffer)};
- if(!albuf)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(!value)
- context->throw_error(AL_INVALID_VALUE, "NULL pointer");
- switch(param)
- {
- case AL_BUFFER_CALLBACK_FUNCTION_SOFT:
- *value = reinterpret_cast<void*>(albuf->mCallback);
- return;
- case AL_BUFFER_CALLBACK_USER_PARAM_SOFT:
- *value = albuf->mUserData;
- return;
- }
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer pointer property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNCEXT5(void, alGetBuffer3Ptr,SOFT, ALuint,buffer, ALenum,param, ALvoid**,value1, ALvoid**,value2, ALvoid**,value3)
- FORCE_ALIGN void AL_APIENTRY alGetBuffer3PtrDirectSOFT(ALCcontext *context, ALuint buffer,
- ALenum param, ALvoid **value1, ALvoid **value2, ALvoid **value3) noexcept
- try {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- if(LookupBuffer(device, buffer) == nullptr)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(!value1 || !value2 || !value3)
- context->throw_error(AL_INVALID_VALUE, "NULL pointer");
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer 3-pointer property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API DECL_FUNCEXT3(void, alGetBufferPtrv,SOFT, ALuint,buffer, ALenum,param, ALvoid**,values)
- FORCE_ALIGN void AL_APIENTRY alGetBufferPtrvDirectSOFT(ALCcontext *context, ALuint buffer,
- ALenum param, ALvoid **values) noexcept
- try {
- switch(param)
- {
- case AL_BUFFER_CALLBACK_FUNCTION_SOFT:
- case AL_BUFFER_CALLBACK_USER_PARAM_SOFT:
- alGetBufferPtrDirectSOFT(context, buffer, param, values);
- return;
- }
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- if(LookupBuffer(device, buffer) == nullptr)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- if(!values)
- context->throw_error(AL_INVALID_VALUE, "NULL pointer");
- context->throw_error(AL_INVALID_ENUM, "Invalid buffer pointer-vector property {:#04x}",
- as_unsigned(param));
- }
- catch(al::base_exception&) {
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- }
- AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint /*buffer*/, ALuint /*samplerate*/,
- ALenum /*internalformat*/, ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/,
- const ALvoid* /*data*/) noexcept
- {
- ContextRef context{GetContextRef()};
- if(!context) UNLIKELY return;
- context->setError(AL_INVALID_OPERATION, "alBufferSamplesSOFT not supported");
- }
- AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
- ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, const ALvoid* /*data*/) noexcept
- {
- ContextRef context{GetContextRef()};
- if(!context) UNLIKELY return;
- context->setError(AL_INVALID_OPERATION, "alBufferSubSamplesSOFT not supported");
- }
- AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
- ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, ALvoid* /*data*/) noexcept
- {
- ContextRef context{GetContextRef()};
- if(!context) UNLIKELY return;
- context->setError(AL_INVALID_OPERATION, "alGetBufferSamplesSOFT not supported");
- }
- AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum /*format*/) noexcept
- {
- ContextRef context{GetContextRef()};
- if(!context) UNLIKELY return AL_FALSE;
- context->setError(AL_INVALID_OPERATION, "alIsBufferFormatSupportedSOFT not supported");
- return AL_FALSE;
- }
- void ALbuffer::SetName(ALCcontext *context, ALuint id, std::string_view name)
- {
- auto *device = context->mALDevice.get();
- auto buflock = std::lock_guard{device->BufferLock};
- auto buffer = LookupBuffer(device, id);
- if(!buffer)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", id);
- device->mBufferNames.insert_or_assign(id, name);
- }
- BufferSubList::~BufferSubList()
- {
- if(!Buffers)
- return;
- uint64_t usemask{~FreeMask};
- while(usemask)
- {
- const int idx{al::countr_zero(usemask)};
- std::destroy_at(al::to_address(Buffers->begin() + idx));
- usemask &= ~(1_u64 << idx);
- }
- FreeMask = ~usemask;
- SubListAllocator{}.deallocate(Buffers, 1);
- Buffers = nullptr;
- }
- #if ALSOFT_EAX
- FORCE_ALIGN DECL_FUNC3(ALboolean, EAXSetBufferMode, ALsizei,n, const ALuint*,buffers, ALint,value)
- FORCE_ALIGN ALboolean AL_APIENTRY EAXSetBufferModeDirect(ALCcontext *context, ALsizei n,
- const ALuint *buffers, ALint value) noexcept
- try {
- if(!eax_g_is_enabled)
- context->throw_error(AL_INVALID_OPERATION, "EAX not enabled");
- const auto storage = EaxStorageFromEnum(value);
- if(!storage)
- context->throw_error(AL_INVALID_ENUM, "Unsupported X-RAM mode {:#x}", as_unsigned(value));
- if(n == 0)
- return AL_TRUE;
- if(n < 0)
- context->throw_error(AL_INVALID_VALUE, "Buffer count {} out of range", n);
- if(!buffers)
- context->throw_error(AL_INVALID_VALUE, "Null AL buffers");
- auto device = context->mALDevice.get();
- std::lock_guard<std::mutex> devlock{device->BufferLock};
- /* Special-case setting a single buffer, to avoid extraneous allocations. */
- if(n == 1)
- {
- const auto bufid = *buffers;
- if(bufid == AL_NONE)
- return AL_TRUE;
- const auto buffer = LookupBuffer(device, bufid);
- if(!buffer)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", bufid);
- /* TODO: Is the store location allowed to change for in-use buffers, or
- * only when not set/queued on a source?
- */
- if(*storage == EaxStorage::Hardware)
- {
- if(!buffer->eax_x_ram_is_hardware
- && buffer->OriginalSize > device->eax_x_ram_free_size)
- context->throw_error(AL_OUT_OF_MEMORY,
- "Out of X-RAM memory (need: {}, avail: {})", buffer->OriginalSize,
- device->eax_x_ram_free_size);
- eax_x_ram_apply(*device, *buffer);
- }
- else
- eax_x_ram_clear(*device, *buffer);
- buffer->eax_x_ram_mode = *storage;
- return AL_TRUE;
- }
- /* Validate the buffers. */
- std::unordered_set<ALbuffer*> buflist;
- for(const ALuint bufid : al::span{buffers, static_cast<ALuint>(n)})
- {
- if(bufid == AL_NONE)
- continue;
- const auto buffer = LookupBuffer(device, bufid);
- if(!buffer)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", bufid);
- /* TODO: Is the store location allowed to change for in-use buffers, or
- * only when not set/queued on a source?
- */
- buflist.emplace(buffer);
- }
- if(*storage == EaxStorage::Hardware)
- {
- size_t total_needed{0};
- for(ALbuffer *buffer : buflist)
- {
- if(!buffer->eax_x_ram_is_hardware)
- {
- if(std::numeric_limits<size_t>::max() - buffer->OriginalSize < total_needed)
- context->throw_error(AL_OUT_OF_MEMORY, "Size overflow ({} + {})",
- buffer->OriginalSize, total_needed);
- total_needed += buffer->OriginalSize;
- }
- }
- if(total_needed > device->eax_x_ram_free_size)
- context->throw_error(AL_OUT_OF_MEMORY, "Out of X-RAM memory (need: {}, avail: {})",
- total_needed, device->eax_x_ram_free_size);
- }
- /* Update the mode. */
- for(ALbuffer *buffer : buflist)
- {
- if(*storage == EaxStorage::Hardware)
- eax_x_ram_apply(*device, *buffer);
- else
- eax_x_ram_clear(*device, *buffer);
- buffer->eax_x_ram_mode = *storage;
- }
- return AL_TRUE;
- }
- catch(al::base_exception&) {
- return AL_FALSE;
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- return AL_FALSE;
- }
- FORCE_ALIGN DECL_FUNC2(ALenum, EAXGetBufferMode, ALuint,buffer, ALint*,pReserved)
- FORCE_ALIGN ALenum AL_APIENTRY EAXGetBufferModeDirect(ALCcontext *context, ALuint buffer,
- ALint *pReserved) noexcept
- try {
- if(!eax_g_is_enabled)
- context->throw_error(AL_INVALID_OPERATION, "EAX not enabled.");
- if(pReserved)
- context->throw_error(AL_INVALID_VALUE, "Non-null reserved parameter");
- auto device = context->mALDevice.get();
- std::lock_guard<std::mutex> devlock{device->BufferLock};
- const auto al_buffer = LookupBuffer(device, buffer);
- if(!al_buffer)
- context->throw_error(AL_INVALID_NAME, "Invalid buffer ID {}", buffer);
- return EnumFromEaxStorage(al_buffer->eax_x_ram_mode);
- }
- catch(al::base_exception&) {
- return AL_NONE;
- }
- catch(std::exception &e) {
- ERR("Caught exception: {}", e.what());
- return AL_NONE;
- }
- #endif // ALSOFT_EAX
|