Assume 'frac' is less than 1
[openal-soft.git] / al / buffer.cpp
blob6bf3ecbcfa5bd6ef01187cde0ea5e150d3ecc1ce
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include "buffer.h"
25 #include <algorithm>
26 #include <array>
27 #include <atomic>
28 #include <cassert>
29 #include <cstdint>
30 #include <cstdlib>
31 #include <cstring>
32 #include <iterator>
33 #include <limits>
34 #include <memory>
35 #include <mutex>
36 #include <new>
37 #include <numeric>
38 #include <stdexcept>
39 #include <utility>
41 #include "AL/al.h"
42 #include "AL/alc.h"
43 #include "AL/alext.h"
45 #include "albit.h"
46 #include "albyte.h"
47 #include "alc/context.h"
48 #include "alc/device.h"
49 #include "alc/inprogext.h"
50 #include "almalloc.h"
51 #include "alnumeric.h"
52 #include "aloptional.h"
53 #include "atomic.h"
54 #include "core/except.h"
55 #include "core/logging.h"
56 #include "core/voice.h"
57 #include "opthelpers.h"
59 #ifdef ALSOFT_EAX
60 #include "eax/globals.h"
61 #include "eax/x_ram.h"
62 #endif // ALSOFT_EAX
65 namespace {
67 constexpr int MaxAdpcmChannels{2};
69 /* IMA ADPCM Stepsize table */
70 constexpr int IMAStep_size[89] = {
71 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19,
72 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
73 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157,
74 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
75 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
76 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660,
77 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,
78 11487,12635,13899,15289,16818,18500,20350,22358,24633,27086,29794,
79 32767
82 /* IMA4 ADPCM Codeword decode table */
83 constexpr int IMA4Codeword[16] = {
84 1, 3, 5, 7, 9, 11, 13, 15,
85 -1,-3,-5,-7,-9,-11,-13,-15,
88 /* IMA4 ADPCM Step index adjust decode table */
89 constexpr int IMA4Index_adjust[16] = {
90 -1,-1,-1,-1, 2, 4, 6, 8,
91 -1,-1,-1,-1, 2, 4, 6, 8
95 /* MSADPCM Adaption table */
96 constexpr int MSADPCMAdaption[16] = {
97 230, 230, 230, 230, 307, 409, 512, 614,
98 768, 614, 512, 409, 307, 230, 230, 230
101 /* MSADPCM Adaption Coefficient tables */
102 constexpr int MSADPCMAdaptionCoeff[7][2] = {
103 { 256, 0 },
104 { 512, -256 },
105 { 0, 0 },
106 { 192, 64 },
107 { 240, 0 },
108 { 460, -208 },
109 { 392, -232 }
113 void DecodeIMA4Block(int16_t *dst, const al::byte *src, size_t numchans, size_t align)
115 int sample[MaxAdpcmChannels]{};
116 int index[MaxAdpcmChannels]{};
117 ALuint code[MaxAdpcmChannels]{};
119 for(size_t c{0};c < numchans;c++)
121 sample[c] = src[0] | (src[1]<<8);
122 sample[c] = (sample[c]^0x8000) - 32768;
123 src += 2;
124 index[c] = src[0] | (src[1]<<8);
125 index[c] = clampi((index[c]^0x8000) - 32768, 0, 88);
126 src += 2;
128 *(dst++) = static_cast<int16_t>(sample[c]);
131 for(size_t i{1};i < align;i++)
133 if((i&7) == 1)
135 for(size_t c{0};c < numchans;c++)
137 code[c] = ALuint{src[0]} | (ALuint{src[1]}<< 8) | (ALuint{src[2]}<<16)
138 | (ALuint{src[3]}<<24);
139 src += 4;
143 for(size_t c{0};c < numchans;c++)
145 const ALuint nibble{code[c]&0xf};
146 code[c] >>= 4;
148 sample[c] += IMA4Codeword[nibble] * IMAStep_size[index[c]] / 8;
149 sample[c] = clampi(sample[c], -32768, 32767);
151 index[c] += IMA4Index_adjust[nibble];
152 index[c] = clampi(index[c], 0, 88);
154 *(dst++) = static_cast<int16_t>(sample[c]);
159 void DecodeMSADPCMBlock(int16_t *dst, const al::byte *src, size_t numchans, size_t align)
161 uint8_t blockpred[MaxAdpcmChannels]{};
162 int delta[MaxAdpcmChannels]{};
163 int16_t samples[MaxAdpcmChannels][2]{};
165 for(size_t c{0};c < numchans;c++)
167 blockpred[c] = std::min<ALubyte>(src[0], 6);
168 ++src;
170 for(size_t c{0};c < numchans;c++)
172 delta[c] = src[0] | (src[1]<<8);
173 delta[c] = (delta[c]^0x8000) - 32768;
174 src += 2;
176 for(size_t c{0};c < numchans;c++)
178 samples[c][0] = static_cast<ALshort>(src[0] | (src[1]<<8));
179 src += 2;
181 for(size_t c{0};c < numchans;c++)
183 samples[c][1] = static_cast<ALshort>(src[0] | (src[1]<<8));
184 src += 2;
187 /* Second sample is written first. */
188 for(size_t c{0};c < numchans;c++)
189 *(dst++) = samples[c][1];
190 for(size_t c{0};c < numchans;c++)
191 *(dst++) = samples[c][0];
193 int num{0};
194 for(size_t i{2};i < align;i++)
196 for(size_t c{0};c < numchans;c++)
198 /* Read the nibble (first is in the upper bits). */
199 al::byte nibble;
200 if(!(num++ & 1))
201 nibble = *src >> 4;
202 else
203 nibble = *(src++) & 0x0f;
205 int pred{(samples[c][0]*MSADPCMAdaptionCoeff[blockpred[c]][0] +
206 samples[c][1]*MSADPCMAdaptionCoeff[blockpred[c]][1]) / 256};
207 pred += ((nibble^0x08) - 0x08) * delta[c];
208 pred = clampi(pred, -32768, 32767);
210 samples[c][1] = samples[c][0];
211 samples[c][0] = static_cast<int16_t>(pred);
213 delta[c] = (MSADPCMAdaption[nibble] * delta[c]) / 256;
214 delta[c] = maxi(16, delta[c]);
216 *(dst++) = static_cast<int16_t>(pred);
221 void Convert_int16_ima4(int16_t *dst, const al::byte *src, size_t numchans, size_t len,
222 size_t align)
224 assert(numchans <= MaxAdpcmChannels);
225 const size_t byte_align{((align-1)/2 + 4) * numchans};
227 len /= align;
228 while(len--)
230 DecodeIMA4Block(dst, src, numchans, align);
231 src += byte_align;
232 dst += align*numchans;
236 void Convert_int16_msadpcm(int16_t *dst, const al::byte *src, size_t numchans, size_t len,
237 size_t align)
239 assert(numchans <= MaxAdpcmChannels);
240 const size_t byte_align{((align-2)/2 + 7) * numchans};
242 len /= align;
243 while(len--)
245 DecodeMSADPCMBlock(dst, src, numchans, align);
246 src += byte_align;
247 dst += align*numchans;
252 ALuint BytesFromUserFmt(UserFmtType type) noexcept
254 switch(type)
256 case UserFmtUByte: return sizeof(uint8_t);
257 case UserFmtShort: return sizeof(int16_t);
258 case UserFmtFloat: return sizeof(float);
259 case UserFmtDouble: return sizeof(double);
260 case UserFmtMulaw: return sizeof(uint8_t);
261 case UserFmtAlaw: return sizeof(uint8_t);
262 case UserFmtIMA4: break; /* not handled here */
263 case UserFmtMSADPCM: break; /* not handled here */
265 return 0;
267 ALuint ChannelsFromUserFmt(UserFmtChannels chans, ALuint ambiorder) noexcept
269 switch(chans)
271 case UserFmtMono: return 1;
272 case UserFmtStereo: return 2;
273 case UserFmtRear: return 2;
274 case UserFmtQuad: return 4;
275 case UserFmtX51: return 6;
276 case UserFmtX61: return 7;
277 case UserFmtX71: return 8;
278 case UserFmtBFormat2D: return (ambiorder*2) + 1;
279 case UserFmtBFormat3D: return (ambiorder+1) * (ambiorder+1);
280 case UserFmtUHJ2: return 2;
281 case UserFmtUHJ3: return 3;
282 case UserFmtUHJ4: return 4;
284 return 0;
287 al::optional<AmbiLayout> AmbiLayoutFromEnum(ALenum layout)
289 switch(layout)
291 case AL_FUMA_SOFT: return AmbiLayout::FuMa;
292 case AL_ACN_SOFT: return AmbiLayout::ACN;
294 return al::nullopt;
296 ALenum EnumFromAmbiLayout(AmbiLayout layout)
298 switch(layout)
300 case AmbiLayout::FuMa: return AL_FUMA_SOFT;
301 case AmbiLayout::ACN: return AL_ACN_SOFT;
303 throw std::runtime_error{"Invalid AmbiLayout: "+std::to_string(int(layout))};
306 al::optional<AmbiScaling> AmbiScalingFromEnum(ALenum scale)
308 switch(scale)
310 case AL_FUMA_SOFT: return AmbiScaling::FuMa;
311 case AL_SN3D_SOFT: return AmbiScaling::SN3D;
312 case AL_N3D_SOFT: return AmbiScaling::N3D;
314 return al::nullopt;
316 ALenum EnumFromAmbiScaling(AmbiScaling scale)
318 switch(scale)
320 case AmbiScaling::FuMa: return AL_FUMA_SOFT;
321 case AmbiScaling::SN3D: return AL_SN3D_SOFT;
322 case AmbiScaling::N3D: return AL_N3D_SOFT;
323 case AmbiScaling::UHJ: break;
325 throw std::runtime_error{"Invalid AmbiScaling: "+std::to_string(int(scale))};
328 al::optional<FmtChannels> FmtFromUserFmt(UserFmtChannels chans)
330 switch(chans)
332 case UserFmtMono: return FmtMono;
333 case UserFmtStereo: return FmtStereo;
334 case UserFmtRear: return FmtRear;
335 case UserFmtQuad: return FmtQuad;
336 case UserFmtX51: return FmtX51;
337 case UserFmtX61: return FmtX61;
338 case UserFmtX71: return FmtX71;
339 case UserFmtBFormat2D: return FmtBFormat2D;
340 case UserFmtBFormat3D: return FmtBFormat3D;
341 case UserFmtUHJ2: return FmtUHJ2;
342 case UserFmtUHJ3: return FmtUHJ3;
343 case UserFmtUHJ4: return FmtUHJ4;
345 return al::nullopt;
347 al::optional<FmtType> FmtFromUserFmt(UserFmtType type)
349 switch(type)
351 case UserFmtUByte: return FmtUByte;
352 case UserFmtShort: return FmtShort;
353 case UserFmtFloat: return FmtFloat;
354 case UserFmtDouble: return FmtDouble;
355 case UserFmtMulaw: return FmtMulaw;
356 case UserFmtAlaw: return FmtAlaw;
357 /* ADPCM not handled here. */
358 case UserFmtIMA4: break;
359 case UserFmtMSADPCM: break;
361 return al::nullopt;
365 #ifdef ALSOFT_EAX
366 bool eax_x_ram_check_availability(const ALCdevice &device, const ALbuffer &buffer,
367 const ALuint newsize) noexcept
369 ALuint freemem{device.eax_x_ram_free_size};
370 /* If the buffer is currently in "hardware", add its memory to the free
371 * pool since it'll be "replaced".
373 if(buffer.eax_x_ram_is_hardware)
374 freemem += buffer.OriginalSize;
375 return freemem >= newsize;
378 void eax_x_ram_apply(ALCdevice &device, ALbuffer &buffer) noexcept
380 if(buffer.eax_x_ram_is_hardware)
381 return;
383 if(device.eax_x_ram_free_size >= buffer.OriginalSize)
385 device.eax_x_ram_free_size -= buffer.OriginalSize;
386 buffer.eax_x_ram_is_hardware = true;
390 void eax_x_ram_clear(ALCdevice& al_device, ALbuffer& al_buffer)
392 if(al_buffer.eax_x_ram_is_hardware)
393 al_device.eax_x_ram_free_size += al_buffer.OriginalSize;
394 al_buffer.eax_x_ram_is_hardware = false;
396 #endif // ALSOFT_EAX
399 constexpr ALbitfieldSOFT INVALID_STORAGE_MASK{~unsigned(AL_MAP_READ_BIT_SOFT |
400 AL_MAP_WRITE_BIT_SOFT | AL_MAP_PERSISTENT_BIT_SOFT | AL_PRESERVE_DATA_BIT_SOFT)};
401 constexpr ALbitfieldSOFT MAP_READ_WRITE_FLAGS{AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT};
402 constexpr ALbitfieldSOFT INVALID_MAP_FLAGS{~unsigned(AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT |
403 AL_MAP_PERSISTENT_BIT_SOFT)};
406 bool EnsureBuffers(ALCdevice *device, size_t needed)
408 size_t count{std::accumulate(device->BufferList.cbegin(), device->BufferList.cend(), size_t{0},
409 [](size_t cur, const BufferSubList &sublist) noexcept -> size_t
410 { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })};
412 while(needed > count)
414 if(device->BufferList.size() >= 1<<25) [[unlikely]]
415 return false;
417 device->BufferList.emplace_back();
418 auto sublist = device->BufferList.end() - 1;
419 sublist->FreeMask = ~0_u64;
420 sublist->Buffers = static_cast<ALbuffer*>(al_calloc(alignof(ALbuffer), sizeof(ALbuffer)*64));
421 if(!sublist->Buffers) [[unlikely]]
423 device->BufferList.pop_back();
424 return false;
426 count += 64;
428 return true;
431 ALbuffer *AllocBuffer(ALCdevice *device)
433 auto sublist = std::find_if(device->BufferList.begin(), device->BufferList.end(),
434 [](const BufferSubList &entry) noexcept -> bool
435 { return entry.FreeMask != 0; });
436 auto lidx = static_cast<ALuint>(std::distance(device->BufferList.begin(), sublist));
437 auto slidx = static_cast<ALuint>(al::countr_zero(sublist->FreeMask));
438 ASSUME(slidx < 64);
440 ALbuffer *buffer{al::construct_at(sublist->Buffers + slidx)};
442 /* Add 1 to avoid buffer ID 0. */
443 buffer->id = ((lidx<<6) | slidx) + 1;
445 sublist->FreeMask &= ~(1_u64 << slidx);
447 return buffer;
450 void FreeBuffer(ALCdevice *device, ALbuffer *buffer)
452 #ifdef ALSOFT_EAX
453 eax_x_ram_clear(*device, *buffer);
454 #endif // ALSOFT_EAX
456 const ALuint id{buffer->id - 1};
457 const size_t lidx{id >> 6};
458 const ALuint slidx{id & 0x3f};
460 al::destroy_at(buffer);
462 device->BufferList[lidx].FreeMask |= 1_u64 << slidx;
465 inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id)
467 const size_t lidx{(id-1) >> 6};
468 const ALuint slidx{(id-1) & 0x3f};
470 if(lidx >= device->BufferList.size()) [[unlikely]]
471 return nullptr;
472 BufferSubList &sublist = device->BufferList[lidx];
473 if(sublist.FreeMask & (1_u64 << slidx)) [[unlikely]]
474 return nullptr;
475 return sublist.Buffers + slidx;
479 ALuint SanitizeAlignment(UserFmtType type, ALuint align)
481 if(align == 0)
483 if(type == UserFmtIMA4)
485 /* Here is where things vary:
486 * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
487 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
489 return 65;
491 if(type == UserFmtMSADPCM)
492 return 64;
493 return 1;
496 if(type == UserFmtIMA4)
498 /* IMA4 block alignment must be a multiple of 8, plus 1. */
499 if((align&7) == 1) return static_cast<ALuint>(align);
500 return 0;
502 if(type == UserFmtMSADPCM)
504 /* MSADPCM block alignment must be a multiple of 2. */
505 if((align&1) == 0) return static_cast<ALuint>(align);
506 return 0;
509 return static_cast<ALuint>(align);
513 const ALchar *NameFromUserFmtType(UserFmtType type)
515 switch(type)
517 case UserFmtUByte: return "UInt8";
518 case UserFmtShort: return "Int16";
519 case UserFmtFloat: return "Float32";
520 case UserFmtDouble: return "Float64";
521 case UserFmtMulaw: return "muLaw";
522 case UserFmtAlaw: return "aLaw";
523 case UserFmtIMA4: return "IMA4 ADPCM";
524 case UserFmtMSADPCM: return "MSADPCM";
526 return "<internal type error>";
529 /** Loads the specified data into the buffer, using the specified format. */
530 void LoadData(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq, ALuint size,
531 UserFmtChannels SrcChannels, UserFmtType SrcType, const al::byte *SrcData,
532 ALbitfieldSOFT access)
534 if(ReadRef(ALBuf->ref) != 0 || ALBuf->MappedAccess != 0) [[unlikely]]
535 return context->setError(AL_INVALID_OPERATION, "Modifying storage for in-use buffer %u",
536 ALBuf->id);
538 /* Currently no channel configurations need to be converted. */
539 auto DstChannels = FmtFromUserFmt(SrcChannels);
540 if(!DstChannels) [[unlikely]]
541 return context->setError(AL_INVALID_ENUM, "Invalid format");
543 /* IMA4 and MSADPCM convert to 16-bit short.
545 * TODO: Currently we can only map samples when they're not converted. To
546 * allow it would need some kind of double-buffering to hold onto a copy of
547 * the original data.
549 if((access&MAP_READ_WRITE_FLAGS))
551 if(SrcType == UserFmtIMA4 || SrcType == UserFmtMSADPCM) [[unlikely]]
552 return context->setError(AL_INVALID_VALUE, "%s samples cannot be mapped",
553 NameFromUserFmtType(SrcType));
555 auto DstType = (SrcType == UserFmtIMA4 || SrcType == UserFmtMSADPCM)
556 ? al::make_optional(FmtShort) : FmtFromUserFmt(SrcType);
557 if(!DstType) [[unlikely]]
558 return context->setError(AL_INVALID_ENUM, "Invalid format");
560 const ALuint unpackalign{ALBuf->UnpackAlign};
561 const ALuint align{SanitizeAlignment(SrcType, unpackalign)};
562 if(align < 1) [[unlikely]]
563 return context->setError(AL_INVALID_VALUE, "Invalid unpack alignment %u for %s samples",
564 unpackalign, NameFromUserFmtType(SrcType));
566 const ALuint ambiorder{IsBFormat(*DstChannels) ? ALBuf->UnpackAmbiOrder :
567 (IsUHJ(*DstChannels) ? 1 : 0)};
569 if((access&AL_PRESERVE_DATA_BIT_SOFT))
571 /* Can only preserve data with the same format and alignment. */
572 if(ALBuf->mChannels != *DstChannels || ALBuf->OriginalType != SrcType) [[unlikely]]
573 return context->setError(AL_INVALID_VALUE, "Preserving data of mismatched format");
574 if(ALBuf->OriginalAlign != align) [[unlikely]]
575 return context->setError(AL_INVALID_VALUE, "Preserving data of mismatched alignment");
576 if(ALBuf->mAmbiOrder != ambiorder) [[unlikely]]
577 return context->setError(AL_INVALID_VALUE, "Preserving data of mismatched order");
580 /* Convert the input/source size in bytes to sample frames using the unpack
581 * block alignment.
583 const ALuint SrcByteAlign{ChannelsFromUserFmt(SrcChannels, ambiorder) *
584 ((SrcType == UserFmtIMA4) ? (align-1)/2 + 4 :
585 (SrcType == UserFmtMSADPCM) ? (align-2)/2 + 7 :
586 (align * BytesFromUserFmt(SrcType)))};
587 if((size%SrcByteAlign) != 0) [[unlikely]]
588 return context->setError(AL_INVALID_VALUE,
589 "Data size %d is not a multiple of frame size %d (%d unpack alignment)",
590 size, SrcByteAlign, align);
592 if(size/SrcByteAlign > std::numeric_limits<ALsizei>::max()/align) [[unlikely]]
593 return context->setError(AL_OUT_OF_MEMORY,
594 "Buffer size overflow, %d blocks x %d samples per block", size/SrcByteAlign, align);
595 const ALuint frames{size / SrcByteAlign * align};
597 /* Convert the sample frames to the number of bytes needed for internal
598 * storage.
600 ALuint NumChannels{ChannelsFromFmt(*DstChannels, ambiorder)};
601 ALuint FrameSize{NumChannels * BytesFromFmt(*DstType)};
602 if(frames > std::numeric_limits<size_t>::max()/FrameSize) [[unlikely]]
603 return context->setError(AL_OUT_OF_MEMORY,
604 "Buffer size overflow, %d frames x %d bytes per frame", frames, FrameSize);
605 size_t newsize{static_cast<size_t>(frames) * FrameSize};
607 #ifdef ALSOFT_EAX
608 if(ALBuf->eax_x_ram_mode == AL_STORAGE_HARDWARE)
610 ALCdevice &device = *context->mALDevice;
611 if(!eax_x_ram_check_availability(device, *ALBuf, size))
612 return context->setError(AL_OUT_OF_MEMORY,
613 "Out of X-RAM memory (avail: %u, needed: %u)", device.eax_x_ram_free_size, size);
615 #endif
617 /* Round up to the next 16-byte multiple. This could reallocate only when
618 * increasing or the new size is less than half the current, but then the
619 * buffer's AL_SIZE would not be very reliable for accounting buffer memory
620 * usage, and reporting the real size could cause problems for apps that
621 * use AL_SIZE to try to get the buffer's play length.
623 newsize = RoundUp(newsize, 16);
624 if(newsize != ALBuf->mData.size())
626 auto newdata = al::vector<al::byte,16>(newsize, al::byte{});
627 if((access&AL_PRESERVE_DATA_BIT_SOFT))
629 const size_t tocopy{minz(newdata.size(), ALBuf->mData.size())};
630 std::copy_n(ALBuf->mData.begin(), tocopy, newdata.begin());
632 newdata.swap(ALBuf->mData);
634 #ifdef ALSOFT_EAX
635 eax_x_ram_clear(*context->mALDevice, *ALBuf);
636 #endif
638 if(SrcType == UserFmtIMA4)
640 assert(*DstType == FmtShort);
641 if(SrcData != nullptr && !ALBuf->mData.empty())
642 Convert_int16_ima4(reinterpret_cast<int16_t*>(ALBuf->mData.data()), SrcData,
643 NumChannels, frames, align);
644 ALBuf->OriginalAlign = align;
646 else if(SrcType == UserFmtMSADPCM)
648 assert(*DstType == FmtShort);
649 if(SrcData != nullptr && !ALBuf->mData.empty())
650 Convert_int16_msadpcm(reinterpret_cast<int16_t*>(ALBuf->mData.data()), SrcData,
651 NumChannels, frames, align);
652 ALBuf->OriginalAlign = align;
654 else
656 assert(DstType.has_value());
657 if(SrcData != nullptr && !ALBuf->mData.empty())
658 std::copy_n(SrcData, frames*FrameSize, ALBuf->mData.begin());
659 ALBuf->OriginalAlign = 1;
661 ALBuf->OriginalSize = size;
662 ALBuf->OriginalType = SrcType;
664 ALBuf->Access = access;
666 ALBuf->mSampleRate = static_cast<ALuint>(freq);
667 ALBuf->mChannels = *DstChannels;
668 ALBuf->mType = *DstType;
669 ALBuf->mAmbiOrder = ambiorder;
671 ALBuf->mCallback = nullptr;
672 ALBuf->mUserData = nullptr;
674 ALBuf->mSampleLen = frames;
675 ALBuf->mLoopStart = 0;
676 ALBuf->mLoopEnd = ALBuf->mSampleLen;
678 #ifdef ALSOFT_EAX
679 if(eax_g_is_enabled && ALBuf->eax_x_ram_mode != AL_STORAGE_ACCESSIBLE)
680 eax_x_ram_apply(*context->mALDevice, *ALBuf);
681 #endif
684 /** Prepares the buffer to use the specified callback, using the specified format. */
685 void PrepareCallback(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq,
686 UserFmtChannels SrcChannels, UserFmtType SrcType, ALBUFFERCALLBACKTYPESOFT callback,
687 void *userptr)
689 if(ReadRef(ALBuf->ref) != 0 || ALBuf->MappedAccess != 0) [[unlikely]]
690 return context->setError(AL_INVALID_OPERATION, "Modifying callback for in-use buffer %u",
691 ALBuf->id);
693 /* Currently no channel configurations need to be converted. */
694 auto DstChannels = FmtFromUserFmt(SrcChannels);
695 if(!DstChannels) [[unlikely]]
696 return context->setError(AL_INVALID_ENUM, "Invalid format");
698 /* IMA4 and MSADPCM convert to 16-bit short. Not supported with callbacks. */
699 auto DstType = FmtFromUserFmt(SrcType);
700 if(!DstType) [[unlikely]]
701 return context->setError(AL_INVALID_ENUM, "Unsupported callback format");
703 const ALuint ambiorder{IsBFormat(*DstChannels) ? ALBuf->UnpackAmbiOrder :
704 (IsUHJ(*DstChannels) ? 1 : 0)};
706 static constexpr uint line_size{BufferLineSize + MaxPostVoiceLoad};
707 al::vector<al::byte,16>(FrameSizeFromFmt(*DstChannels, *DstType, ambiorder) *
708 size_t{line_size}).swap(ALBuf->mData);
710 #ifdef ALSOFT_EAX
711 eax_x_ram_clear(*context->mALDevice, *ALBuf);
712 #endif
714 ALBuf->mCallback = callback;
715 ALBuf->mUserData = userptr;
717 ALBuf->OriginalType = SrcType;
718 ALBuf->OriginalSize = 0;
719 ALBuf->OriginalAlign = 1;
720 ALBuf->Access = 0;
722 ALBuf->mSampleRate = static_cast<ALuint>(freq);
723 ALBuf->mChannels = *DstChannels;
724 ALBuf->mType = *DstType;
725 ALBuf->mAmbiOrder = ambiorder;
727 ALBuf->mSampleLen = 0;
728 ALBuf->mLoopStart = 0;
729 ALBuf->mLoopEnd = ALBuf->mSampleLen;
733 struct DecompResult { UserFmtChannels channels; UserFmtType type; };
734 al::optional<DecompResult> DecomposeUserFormat(ALenum format)
736 struct FormatMap {
737 ALenum format;
738 UserFmtChannels channels;
739 UserFmtType type;
741 static const std::array<FormatMap,55> UserFmtList{{
742 { AL_FORMAT_MONO8, UserFmtMono, UserFmtUByte },
743 { AL_FORMAT_MONO16, UserFmtMono, UserFmtShort },
744 { AL_FORMAT_MONO_FLOAT32, UserFmtMono, UserFmtFloat },
745 { AL_FORMAT_MONO_DOUBLE_EXT, UserFmtMono, UserFmtDouble },
746 { AL_FORMAT_MONO_IMA4, UserFmtMono, UserFmtIMA4 },
747 { AL_FORMAT_MONO_MSADPCM_SOFT, UserFmtMono, UserFmtMSADPCM },
748 { AL_FORMAT_MONO_MULAW, UserFmtMono, UserFmtMulaw },
749 { AL_FORMAT_MONO_ALAW_EXT, UserFmtMono, UserFmtAlaw },
751 { AL_FORMAT_STEREO8, UserFmtStereo, UserFmtUByte },
752 { AL_FORMAT_STEREO16, UserFmtStereo, UserFmtShort },
753 { AL_FORMAT_STEREO_FLOAT32, UserFmtStereo, UserFmtFloat },
754 { AL_FORMAT_STEREO_DOUBLE_EXT, UserFmtStereo, UserFmtDouble },
755 { AL_FORMAT_STEREO_IMA4, UserFmtStereo, UserFmtIMA4 },
756 { AL_FORMAT_STEREO_MSADPCM_SOFT, UserFmtStereo, UserFmtMSADPCM },
757 { AL_FORMAT_STEREO_MULAW, UserFmtStereo, UserFmtMulaw },
758 { AL_FORMAT_STEREO_ALAW_EXT, UserFmtStereo, UserFmtAlaw },
760 { AL_FORMAT_REAR8, UserFmtRear, UserFmtUByte },
761 { AL_FORMAT_REAR16, UserFmtRear, UserFmtShort },
762 { AL_FORMAT_REAR32, UserFmtRear, UserFmtFloat },
763 { AL_FORMAT_REAR_MULAW, UserFmtRear, UserFmtMulaw },
765 { AL_FORMAT_QUAD8_LOKI, UserFmtQuad, UserFmtUByte },
766 { AL_FORMAT_QUAD16_LOKI, UserFmtQuad, UserFmtShort },
768 { AL_FORMAT_QUAD8, UserFmtQuad, UserFmtUByte },
769 { AL_FORMAT_QUAD16, UserFmtQuad, UserFmtShort },
770 { AL_FORMAT_QUAD32, UserFmtQuad, UserFmtFloat },
771 { AL_FORMAT_QUAD_MULAW, UserFmtQuad, UserFmtMulaw },
773 { AL_FORMAT_51CHN8, UserFmtX51, UserFmtUByte },
774 { AL_FORMAT_51CHN16, UserFmtX51, UserFmtShort },
775 { AL_FORMAT_51CHN32, UserFmtX51, UserFmtFloat },
776 { AL_FORMAT_51CHN_MULAW, UserFmtX51, UserFmtMulaw },
778 { AL_FORMAT_61CHN8, UserFmtX61, UserFmtUByte },
779 { AL_FORMAT_61CHN16, UserFmtX61, UserFmtShort },
780 { AL_FORMAT_61CHN32, UserFmtX61, UserFmtFloat },
781 { AL_FORMAT_61CHN_MULAW, UserFmtX61, UserFmtMulaw },
783 { AL_FORMAT_71CHN8, UserFmtX71, UserFmtUByte },
784 { AL_FORMAT_71CHN16, UserFmtX71, UserFmtShort },
785 { AL_FORMAT_71CHN32, UserFmtX71, UserFmtFloat },
786 { AL_FORMAT_71CHN_MULAW, UserFmtX71, UserFmtMulaw },
788 { AL_FORMAT_BFORMAT2D_8, UserFmtBFormat2D, UserFmtUByte },
789 { AL_FORMAT_BFORMAT2D_16, UserFmtBFormat2D, UserFmtShort },
790 { AL_FORMAT_BFORMAT2D_FLOAT32, UserFmtBFormat2D, UserFmtFloat },
791 { AL_FORMAT_BFORMAT2D_MULAW, UserFmtBFormat2D, UserFmtMulaw },
793 { AL_FORMAT_BFORMAT3D_8, UserFmtBFormat3D, UserFmtUByte },
794 { AL_FORMAT_BFORMAT3D_16, UserFmtBFormat3D, UserFmtShort },
795 { AL_FORMAT_BFORMAT3D_FLOAT32, UserFmtBFormat3D, UserFmtFloat },
796 { AL_FORMAT_BFORMAT3D_MULAW, UserFmtBFormat3D, UserFmtMulaw },
798 { AL_FORMAT_UHJ2CHN8_SOFT, UserFmtUHJ2, UserFmtUByte },
799 { AL_FORMAT_UHJ2CHN16_SOFT, UserFmtUHJ2, UserFmtShort },
800 { AL_FORMAT_UHJ2CHN_FLOAT32_SOFT, UserFmtUHJ2, UserFmtFloat },
802 { AL_FORMAT_UHJ3CHN8_SOFT, UserFmtUHJ3, UserFmtUByte },
803 { AL_FORMAT_UHJ3CHN16_SOFT, UserFmtUHJ3, UserFmtShort },
804 { AL_FORMAT_UHJ3CHN_FLOAT32_SOFT, UserFmtUHJ3, UserFmtFloat },
806 { AL_FORMAT_UHJ4CHN8_SOFT, UserFmtUHJ4, UserFmtUByte },
807 { AL_FORMAT_UHJ4CHN16_SOFT, UserFmtUHJ4, UserFmtShort },
808 { AL_FORMAT_UHJ4CHN_FLOAT32_SOFT, UserFmtUHJ4, UserFmtFloat },
811 for(const auto &fmt : UserFmtList)
813 if(fmt.format == format)
814 return al::make_optional<DecompResult>({fmt.channels, fmt.type});
816 return al::nullopt;
819 } // namespace
822 AL_API void AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
823 START_API_FUNC
825 ContextRef context{GetContextRef()};
826 if(!context) [[unlikely]] return;
828 if(n < 0) [[unlikely]]
829 context->setError(AL_INVALID_VALUE, "Generating %d buffers", n);
830 if(n <= 0) [[unlikely]] return;
832 ALCdevice *device{context->mALDevice.get()};
833 std::lock_guard<std::mutex> _{device->BufferLock};
834 if(!EnsureBuffers(device, static_cast<ALuint>(n)))
836 context->setError(AL_OUT_OF_MEMORY, "Failed to allocate %d buffer%s", n, (n==1)?"":"s");
837 return;
840 if(n == 1) [[likely]]
842 /* Special handling for the easy and normal case. */
843 ALbuffer *buffer{AllocBuffer(device)};
844 buffers[0] = buffer->id;
846 else
848 /* Store the allocated buffer IDs in a separate local list, to avoid
849 * modifying the user storage in case of failure.
851 al::vector<ALuint> ids;
852 ids.reserve(static_cast<ALuint>(n));
853 do {
854 ALbuffer *buffer{AllocBuffer(device)};
855 ids.emplace_back(buffer->id);
856 } while(--n);
857 std::copy(ids.begin(), ids.end(), buffers);
860 END_API_FUNC
862 AL_API void AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
863 START_API_FUNC
865 ContextRef context{GetContextRef()};
866 if(!context) [[unlikely]] return;
868 if(n < 0) [[unlikely]]
869 context->setError(AL_INVALID_VALUE, "Deleting %d buffers", n);
870 if(n <= 0) [[unlikely]] return;
872 ALCdevice *device{context->mALDevice.get()};
873 std::lock_guard<std::mutex> _{device->BufferLock};
875 /* First try to find any buffers that are invalid or in-use. */
876 auto validate_buffer = [device, &context](const ALuint bid) -> bool
878 if(!bid) return true;
879 ALbuffer *ALBuf{LookupBuffer(device, bid)};
880 if(!ALBuf) [[unlikely]]
882 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", bid);
883 return false;
885 if(ReadRef(ALBuf->ref) != 0) [[unlikely]]
887 context->setError(AL_INVALID_OPERATION, "Deleting in-use buffer %u", bid);
888 return false;
890 return true;
892 const ALuint *buffers_end = buffers + n;
893 auto invbuf = std::find_if_not(buffers, buffers_end, validate_buffer);
894 if(invbuf != buffers_end) [[unlikely]] return;
896 /* All good. Delete non-0 buffer IDs. */
897 auto delete_buffer = [device](const ALuint bid) -> void
899 ALbuffer *buffer{bid ? LookupBuffer(device, bid) : nullptr};
900 if(buffer) FreeBuffer(device, buffer);
902 std::for_each(buffers, buffers_end, delete_buffer);
904 END_API_FUNC
906 AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
907 START_API_FUNC
909 ContextRef context{GetContextRef()};
910 if(context) [[likely]]
912 ALCdevice *device{context->mALDevice.get()};
913 std::lock_guard<std::mutex> _{device->BufferLock};
914 if(!buffer || LookupBuffer(device, buffer))
915 return AL_TRUE;
917 return AL_FALSE;
919 END_API_FUNC
922 AL_API void AL_APIENTRY alBufferData(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq)
923 START_API_FUNC
924 { alBufferStorageSOFT(buffer, format, data, size, freq, 0); }
925 END_API_FUNC
927 AL_API void AL_APIENTRY alBufferStorageSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq, ALbitfieldSOFT flags)
928 START_API_FUNC
930 ContextRef context{GetContextRef()};
931 if(!context) [[unlikely]] return;
933 ALCdevice *device{context->mALDevice.get()};
934 std::lock_guard<std::mutex> _{device->BufferLock};
936 ALbuffer *albuf = LookupBuffer(device, buffer);
937 if(!albuf) [[unlikely]]
938 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
939 else if(size < 0) [[unlikely]]
940 context->setError(AL_INVALID_VALUE, "Negative storage size %d", size);
941 else if(freq < 1) [[unlikely]]
942 context->setError(AL_INVALID_VALUE, "Invalid sample rate %d", freq);
943 else if((flags&INVALID_STORAGE_MASK) != 0) [[unlikely]]
944 context->setError(AL_INVALID_VALUE, "Invalid storage flags 0x%x",
945 flags&INVALID_STORAGE_MASK);
946 else if((flags&AL_MAP_PERSISTENT_BIT_SOFT) && !(flags&MAP_READ_WRITE_FLAGS)) [[unlikely]]
947 context->setError(AL_INVALID_VALUE,
948 "Declaring persistently mapped storage without read or write access");
949 else
951 auto usrfmt = DecomposeUserFormat(format);
952 if(!usrfmt) [[unlikely]]
953 context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
954 else
956 LoadData(context.get(), albuf, freq, static_cast<ALuint>(size), usrfmt->channels,
957 usrfmt->type, static_cast<const al::byte*>(data), flags);
961 END_API_FUNC
963 AL_API void* AL_APIENTRY alMapBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length, ALbitfieldSOFT access)
964 START_API_FUNC
966 ContextRef context{GetContextRef()};
967 if(!context) [[unlikely]] return nullptr;
969 ALCdevice *device{context->mALDevice.get()};
970 std::lock_guard<std::mutex> _{device->BufferLock};
972 ALbuffer *albuf = LookupBuffer(device, buffer);
973 if(!albuf) [[unlikely]]
974 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
975 else if((access&INVALID_MAP_FLAGS) != 0) [[unlikely]]
976 context->setError(AL_INVALID_VALUE, "Invalid map flags 0x%x", access&INVALID_MAP_FLAGS);
977 else if(!(access&MAP_READ_WRITE_FLAGS)) [[unlikely]]
978 context->setError(AL_INVALID_VALUE, "Mapping buffer %u without read or write access",
979 buffer);
980 else
982 ALbitfieldSOFT unavailable = (albuf->Access^access) & access;
983 if(ReadRef(albuf->ref) != 0 && !(access&AL_MAP_PERSISTENT_BIT_SOFT)) [[unlikely]]
984 context->setError(AL_INVALID_OPERATION,
985 "Mapping in-use buffer %u without persistent mapping", buffer);
986 else if(albuf->MappedAccess != 0) [[unlikely]]
987 context->setError(AL_INVALID_OPERATION, "Mapping already-mapped buffer %u", buffer);
988 else if((unavailable&AL_MAP_READ_BIT_SOFT)) [[unlikely]]
989 context->setError(AL_INVALID_VALUE,
990 "Mapping buffer %u for reading without read access", buffer);
991 else if((unavailable&AL_MAP_WRITE_BIT_SOFT)) [[unlikely]]
992 context->setError(AL_INVALID_VALUE,
993 "Mapping buffer %u for writing without write access", buffer);
994 else if((unavailable&AL_MAP_PERSISTENT_BIT_SOFT)) [[unlikely]]
995 context->setError(AL_INVALID_VALUE,
996 "Mapping buffer %u persistently without persistent access", buffer);
997 else if(offset < 0 || length <= 0
998 || static_cast<ALuint>(offset) >= albuf->OriginalSize
999 || static_cast<ALuint>(length) > albuf->OriginalSize - static_cast<ALuint>(offset))
1000 [[unlikely]]
1001 context->setError(AL_INVALID_VALUE, "Mapping invalid range %d+%d for buffer %u",
1002 offset, length, buffer);
1003 else
1005 void *retval{albuf->mData.data() + offset};
1006 albuf->MappedAccess = access;
1007 albuf->MappedOffset = offset;
1008 albuf->MappedSize = length;
1009 return retval;
1013 return nullptr;
1015 END_API_FUNC
1017 AL_API void AL_APIENTRY alUnmapBufferSOFT(ALuint buffer)
1018 START_API_FUNC
1020 ContextRef context{GetContextRef()};
1021 if(!context) [[unlikely]] return;
1023 ALCdevice *device{context->mALDevice.get()};
1024 std::lock_guard<std::mutex> _{device->BufferLock};
1026 ALbuffer *albuf = LookupBuffer(device, buffer);
1027 if(!albuf) [[unlikely]]
1028 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1029 else if(albuf->MappedAccess == 0) [[unlikely]]
1030 context->setError(AL_INVALID_OPERATION, "Unmapping unmapped buffer %u", buffer);
1031 else
1033 albuf->MappedAccess = 0;
1034 albuf->MappedOffset = 0;
1035 albuf->MappedSize = 0;
1038 END_API_FUNC
1040 AL_API void AL_APIENTRY alFlushMappedBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length)
1041 START_API_FUNC
1043 ContextRef context{GetContextRef()};
1044 if(!context) [[unlikely]] return;
1046 ALCdevice *device{context->mALDevice.get()};
1047 std::lock_guard<std::mutex> _{device->BufferLock};
1049 ALbuffer *albuf = LookupBuffer(device, buffer);
1050 if(!albuf) [[unlikely]]
1051 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1052 else if(!(albuf->MappedAccess&AL_MAP_WRITE_BIT_SOFT)) [[unlikely]]
1053 context->setError(AL_INVALID_OPERATION, "Flushing buffer %u while not mapped for writing",
1054 buffer);
1055 else if(offset < albuf->MappedOffset || length <= 0
1056 || offset >= albuf->MappedOffset+albuf->MappedSize
1057 || length > albuf->MappedOffset+albuf->MappedSize-offset) [[unlikely]]
1058 context->setError(AL_INVALID_VALUE, "Flushing invalid range %d+%d on buffer %u", offset,
1059 length, buffer);
1060 else
1062 /* FIXME: Need to use some method of double-buffering for the mixer and
1063 * app to hold separate memory, which can be safely transfered
1064 * asynchronously. Currently we just say the app shouldn't write where
1065 * OpenAL's reading, and hope for the best...
1067 std::atomic_thread_fence(std::memory_order_seq_cst);
1070 END_API_FUNC
1072 AL_API void AL_APIENTRY alBufferSubDataSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei offset, ALsizei length)
1073 START_API_FUNC
1075 ContextRef context{GetContextRef()};
1076 if(!context) [[unlikely]] return;
1078 ALCdevice *device{context->mALDevice.get()};
1079 std::lock_guard<std::mutex> _{device->BufferLock};
1081 ALbuffer *albuf = LookupBuffer(device, buffer);
1082 if(!albuf) [[unlikely]]
1084 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1085 return;
1088 auto usrfmt = DecomposeUserFormat(format);
1089 if(!usrfmt) [[unlikely]]
1091 context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
1092 return;
1095 ALuint unpack_align{albuf->UnpackAlign};
1096 ALuint align{SanitizeAlignment(usrfmt->type, unpack_align)};
1097 if(align < 1) [[unlikely]]
1098 context->setError(AL_INVALID_VALUE, "Invalid unpack alignment %u", unpack_align);
1099 else if(al::to_underlying(usrfmt->channels) != al::to_underlying(albuf->mChannels)
1100 || usrfmt->type != albuf->OriginalType) [[unlikely]]
1101 context->setError(AL_INVALID_ENUM, "Unpacking data with mismatched format");
1102 else if(align != albuf->OriginalAlign) [[unlikely]]
1103 context->setError(AL_INVALID_VALUE,
1104 "Unpacking data with alignment %u does not match original alignment %u", align,
1105 albuf->OriginalAlign);
1106 else if(albuf->isBFormat() && albuf->UnpackAmbiOrder != albuf->mAmbiOrder) [[unlikely]]
1107 context->setError(AL_INVALID_VALUE, "Unpacking data with mismatched ambisonic order");
1108 else if(albuf->MappedAccess != 0) [[unlikely]]
1109 context->setError(AL_INVALID_OPERATION, "Unpacking data into mapped buffer %u", buffer);
1110 else
1112 ALuint num_chans{albuf->channelsFromFmt()};
1113 ALuint frame_size{num_chans * albuf->bytesFromFmt()};
1114 ALuint byte_align{
1115 (albuf->OriginalType == UserFmtIMA4) ? ((align-1)/2 + 4) * num_chans :
1116 (albuf->OriginalType == UserFmtMSADPCM) ? ((align-2)/2 + 7) * num_chans :
1117 (align * frame_size)
1120 if(offset < 0 || length < 0 || static_cast<ALuint>(offset) > albuf->OriginalSize
1121 || static_cast<ALuint>(length) > albuf->OriginalSize-static_cast<ALuint>(offset))
1122 [[unlikely]]
1123 context->setError(AL_INVALID_VALUE, "Invalid data sub-range %d+%d on buffer %u",
1124 offset, length, buffer);
1125 else if((static_cast<ALuint>(offset)%byte_align) != 0) [[unlikely]]
1126 context->setError(AL_INVALID_VALUE,
1127 "Sub-range offset %d is not a multiple of frame size %d (%d unpack alignment)",
1128 offset, byte_align, align);
1129 else if((static_cast<ALuint>(length)%byte_align) != 0) [[unlikely]]
1130 context->setError(AL_INVALID_VALUE,
1131 "Sub-range length %d is not a multiple of frame size %d (%d unpack alignment)",
1132 length, byte_align, align);
1133 else
1135 /* offset -> byte offset, length -> sample count */
1136 size_t byteoff{static_cast<ALuint>(offset)/byte_align * align * frame_size};
1137 size_t samplen{static_cast<ALuint>(length)/byte_align * align};
1139 void *dst = albuf->mData.data() + byteoff;
1140 if(usrfmt->type == UserFmtIMA4 && albuf->mType == FmtShort)
1141 Convert_int16_ima4(static_cast<int16_t*>(dst), static_cast<const al::byte*>(data),
1142 num_chans, samplen, align);
1143 else if(usrfmt->type == UserFmtMSADPCM && albuf->mType == FmtShort)
1144 Convert_int16_msadpcm(static_cast<int16_t*>(dst),
1145 static_cast<const al::byte*>(data), num_chans, samplen, align);
1146 else
1148 assert(al::to_underlying(usrfmt->type) == al::to_underlying(albuf->mType));
1149 memcpy(dst, data, size_t{samplen} * frame_size);
1154 END_API_FUNC
1157 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint /*buffer*/, ALuint /*samplerate*/,
1158 ALenum /*internalformat*/, ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/,
1159 const ALvoid* /*data*/)
1160 START_API_FUNC
1162 ContextRef context{GetContextRef()};
1163 if(!context) [[unlikely]] return;
1165 context->setError(AL_INVALID_OPERATION, "alBufferSamplesSOFT not supported");
1167 END_API_FUNC
1169 AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
1170 ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, const ALvoid* /*data*/)
1171 START_API_FUNC
1173 ContextRef context{GetContextRef()};
1174 if(!context) [[unlikely]] return;
1176 context->setError(AL_INVALID_OPERATION, "alBufferSubSamplesSOFT not supported");
1178 END_API_FUNC
1180 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
1181 ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, ALvoid* /*data*/)
1182 START_API_FUNC
1184 ContextRef context{GetContextRef()};
1185 if(!context) [[unlikely]] return;
1187 context->setError(AL_INVALID_OPERATION, "alGetBufferSamplesSOFT not supported");
1189 END_API_FUNC
1191 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum /*format*/)
1192 START_API_FUNC
1194 ContextRef context{GetContextRef()};
1195 if(!context) [[unlikely]] return AL_FALSE;
1197 context->setError(AL_INVALID_OPERATION, "alIsBufferFormatSupportedSOFT not supported");
1198 return AL_FALSE;
1200 END_API_FUNC
1203 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum param, ALfloat /*value*/)
1204 START_API_FUNC
1206 ContextRef context{GetContextRef()};
1207 if(!context) [[unlikely]] return;
1209 ALCdevice *device{context->mALDevice.get()};
1210 std::lock_guard<std::mutex> _{device->BufferLock};
1212 if(LookupBuffer(device, buffer) == nullptr) [[unlikely]]
1213 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1214 else switch(param)
1216 default:
1217 context->setError(AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
1220 END_API_FUNC
1222 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum param,
1223 ALfloat /*value1*/, ALfloat /*value2*/, ALfloat /*value3*/)
1224 START_API_FUNC
1226 ContextRef context{GetContextRef()};
1227 if(!context) [[unlikely]] return;
1229 ALCdevice *device{context->mALDevice.get()};
1230 std::lock_guard<std::mutex> _{device->BufferLock};
1232 if(LookupBuffer(device, buffer) == nullptr) [[unlikely]]
1233 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1234 else switch(param)
1236 default:
1237 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
1240 END_API_FUNC
1242 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum param, const ALfloat *values)
1243 START_API_FUNC
1245 ContextRef context{GetContextRef()};
1246 if(!context) [[unlikely]] return;
1248 ALCdevice *device{context->mALDevice.get()};
1249 std::lock_guard<std::mutex> _{device->BufferLock};
1251 if(LookupBuffer(device, buffer) == nullptr) [[unlikely]]
1252 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1253 else if(!values) [[unlikely]]
1254 context->setError(AL_INVALID_VALUE, "NULL pointer");
1255 else switch(param)
1257 default:
1258 context->setError(AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
1261 END_API_FUNC
1264 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum param, ALint value)
1265 START_API_FUNC
1267 ContextRef context{GetContextRef()};
1268 if(!context) [[unlikely]] return;
1270 ALCdevice *device{context->mALDevice.get()};
1271 std::lock_guard<std::mutex> _{device->BufferLock};
1273 ALbuffer *albuf = LookupBuffer(device, buffer);
1274 if(!albuf) [[unlikely]]
1275 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1276 else switch(param)
1278 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1279 if(value < 0) [[unlikely]]
1280 context->setError(AL_INVALID_VALUE, "Invalid unpack block alignment %d", value);
1281 else
1282 albuf->UnpackAlign = static_cast<ALuint>(value);
1283 break;
1285 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1286 if(value < 0) [[unlikely]]
1287 context->setError(AL_INVALID_VALUE, "Invalid pack block alignment %d", value);
1288 else
1289 albuf->PackAlign = static_cast<ALuint>(value);
1290 break;
1292 case AL_AMBISONIC_LAYOUT_SOFT:
1293 if(ReadRef(albuf->ref) != 0) [[unlikely]]
1294 context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's ambisonic layout",
1295 buffer);
1296 else if(value != AL_FUMA_SOFT && value != AL_ACN_SOFT) [[unlikely]]
1297 context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic layout %04x", value);
1298 else
1299 albuf->mAmbiLayout = AmbiLayoutFromEnum(value).value();
1300 break;
1302 case AL_AMBISONIC_SCALING_SOFT:
1303 if(ReadRef(albuf->ref) != 0) [[unlikely]]
1304 context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's ambisonic scaling",
1305 buffer);
1306 else if(value != AL_FUMA_SOFT && value != AL_SN3D_SOFT && value != AL_N3D_SOFT)
1307 [[unlikely]]
1308 context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic scaling %04x", value);
1309 else
1310 albuf->mAmbiScaling = AmbiScalingFromEnum(value).value();
1311 break;
1313 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1314 if(value < 1 || value > 14) [[unlikely]]
1315 context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic order %d", value);
1316 else
1317 albuf->UnpackAmbiOrder = static_cast<ALuint>(value);
1318 break;
1320 default:
1321 context->setError(AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
1324 END_API_FUNC
1326 AL_API void AL_APIENTRY alBuffer3i(ALuint buffer, ALenum param,
1327 ALint /*value1*/, ALint /*value2*/, ALint /*value3*/)
1328 START_API_FUNC
1330 ContextRef context{GetContextRef()};
1331 if(!context) [[unlikely]] return;
1333 ALCdevice *device{context->mALDevice.get()};
1334 std::lock_guard<std::mutex> _{device->BufferLock};
1336 if(LookupBuffer(device, buffer) == nullptr) [[unlikely]]
1337 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1338 else switch(param)
1340 default:
1341 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
1344 END_API_FUNC
1346 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum param, const ALint *values)
1347 START_API_FUNC
1349 if(values)
1351 switch(param)
1353 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1354 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1355 case AL_AMBISONIC_LAYOUT_SOFT:
1356 case AL_AMBISONIC_SCALING_SOFT:
1357 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1358 alBufferi(buffer, param, values[0]);
1359 return;
1363 ContextRef context{GetContextRef()};
1364 if(!context) [[unlikely]] return;
1366 ALCdevice *device{context->mALDevice.get()};
1367 std::lock_guard<std::mutex> _{device->BufferLock};
1369 ALbuffer *albuf = LookupBuffer(device, buffer);
1370 if(!albuf) [[unlikely]]
1371 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1372 else if(!values) [[unlikely]]
1373 context->setError(AL_INVALID_VALUE, "NULL pointer");
1374 else switch(param)
1376 case AL_LOOP_POINTS_SOFT:
1377 if(ReadRef(albuf->ref) != 0) [[unlikely]]
1378 context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's loop points",
1379 buffer);
1380 else if(values[0] < 0 || values[0] >= values[1]
1381 || static_cast<ALuint>(values[1]) > albuf->mSampleLen) [[unlikely]]
1382 context->setError(AL_INVALID_VALUE, "Invalid loop point range %d -> %d on buffer %u",
1383 values[0], values[1], buffer);
1384 else
1386 albuf->mLoopStart = static_cast<ALuint>(values[0]);
1387 albuf->mLoopEnd = static_cast<ALuint>(values[1]);
1389 break;
1391 default:
1392 context->setError(AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x", param);
1395 END_API_FUNC
1398 AL_API void AL_APIENTRY alGetBufferf(ALuint buffer, ALenum param, ALfloat *value)
1399 START_API_FUNC
1401 ContextRef context{GetContextRef()};
1402 if(!context) [[unlikely]] return;
1404 ALCdevice *device{context->mALDevice.get()};
1405 std::lock_guard<std::mutex> _{device->BufferLock};
1407 ALbuffer *albuf = LookupBuffer(device, buffer);
1408 if(!albuf) [[unlikely]]
1409 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1410 else if(!value) [[unlikely]]
1411 context->setError(AL_INVALID_VALUE, "NULL pointer");
1412 else switch(param)
1414 default:
1415 context->setError(AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
1418 END_API_FUNC
1420 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3)
1421 START_API_FUNC
1423 ContextRef context{GetContextRef()};
1424 if(!context) [[unlikely]] return;
1426 ALCdevice *device{context->mALDevice.get()};
1427 std::lock_guard<std::mutex> _{device->BufferLock};
1429 if(LookupBuffer(device, buffer) == nullptr) [[unlikely]]
1430 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1431 else if(!value1 || !value2 || !value3) [[unlikely]]
1432 context->setError(AL_INVALID_VALUE, "NULL pointer");
1433 else switch(param)
1435 default:
1436 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
1439 END_API_FUNC
1441 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum param, ALfloat *values)
1442 START_API_FUNC
1444 switch(param)
1446 case AL_SEC_LENGTH_SOFT:
1447 alGetBufferf(buffer, param, values);
1448 return;
1451 ContextRef context{GetContextRef()};
1452 if(!context) [[unlikely]] return;
1454 ALCdevice *device{context->mALDevice.get()};
1455 std::lock_guard<std::mutex> _{device->BufferLock};
1457 if(LookupBuffer(device, buffer) == nullptr) [[unlikely]]
1458 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1459 else if(!values) [[unlikely]]
1460 context->setError(AL_INVALID_VALUE, "NULL pointer");
1461 else switch(param)
1463 default:
1464 context->setError(AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
1467 END_API_FUNC
1470 AL_API void AL_APIENTRY alGetBufferi(ALuint buffer, ALenum param, ALint *value)
1471 START_API_FUNC
1473 ContextRef context{GetContextRef()};
1474 if(!context) [[unlikely]] return;
1476 ALCdevice *device{context->mALDevice.get()};
1477 std::lock_guard<std::mutex> _{device->BufferLock};
1478 ALbuffer *albuf = LookupBuffer(device, buffer);
1479 if(!albuf) [[unlikely]]
1480 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1481 else if(!value) [[unlikely]]
1482 context->setError(AL_INVALID_VALUE, "NULL pointer");
1483 else switch(param)
1485 case AL_FREQUENCY:
1486 *value = static_cast<ALint>(albuf->mSampleRate);
1487 break;
1489 case AL_BITS:
1490 *value = static_cast<ALint>(albuf->bytesFromFmt() * 8);
1491 break;
1493 case AL_CHANNELS:
1494 *value = static_cast<ALint>(albuf->channelsFromFmt());
1495 break;
1497 case AL_SIZE:
1498 *value = static_cast<ALint>(albuf->mSampleLen * albuf->frameSizeFromFmt());
1499 break;
1501 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1502 *value = static_cast<ALint>(albuf->UnpackAlign);
1503 break;
1505 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1506 *value = static_cast<ALint>(albuf->PackAlign);
1507 break;
1509 case AL_AMBISONIC_LAYOUT_SOFT:
1510 *value = EnumFromAmbiLayout(albuf->mAmbiLayout);
1511 break;
1513 case AL_AMBISONIC_SCALING_SOFT:
1514 *value = EnumFromAmbiScaling(albuf->mAmbiScaling);
1515 break;
1517 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1518 *value = static_cast<int>(albuf->UnpackAmbiOrder);
1519 break;
1521 default:
1522 context->setError(AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
1525 END_API_FUNC
1527 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum param, ALint *value1, ALint *value2, ALint *value3)
1528 START_API_FUNC
1530 ContextRef context{GetContextRef()};
1531 if(!context) [[unlikely]] return;
1533 ALCdevice *device{context->mALDevice.get()};
1534 std::lock_guard<std::mutex> _{device->BufferLock};
1535 if(LookupBuffer(device, buffer) == nullptr) [[unlikely]]
1536 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1537 else if(!value1 || !value2 || !value3) [[unlikely]]
1538 context->setError(AL_INVALID_VALUE, "NULL pointer");
1539 else switch(param)
1541 default:
1542 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
1545 END_API_FUNC
1547 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum param, ALint *values)
1548 START_API_FUNC
1550 switch(param)
1552 case AL_FREQUENCY:
1553 case AL_BITS:
1554 case AL_CHANNELS:
1555 case AL_SIZE:
1556 case AL_INTERNAL_FORMAT_SOFT:
1557 case AL_BYTE_LENGTH_SOFT:
1558 case AL_SAMPLE_LENGTH_SOFT:
1559 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1560 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1561 case AL_AMBISONIC_LAYOUT_SOFT:
1562 case AL_AMBISONIC_SCALING_SOFT:
1563 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1564 alGetBufferi(buffer, param, values);
1565 return;
1568 ContextRef context{GetContextRef()};
1569 if(!context) [[unlikely]] return;
1571 ALCdevice *device{context->mALDevice.get()};
1572 std::lock_guard<std::mutex> _{device->BufferLock};
1573 ALbuffer *albuf = LookupBuffer(device, buffer);
1574 if(!albuf) [[unlikely]]
1575 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1576 else if(!values) [[unlikely]]
1577 context->setError(AL_INVALID_VALUE, "NULL pointer");
1578 else switch(param)
1580 case AL_LOOP_POINTS_SOFT:
1581 values[0] = static_cast<ALint>(albuf->mLoopStart);
1582 values[1] = static_cast<ALint>(albuf->mLoopEnd);
1583 break;
1585 default:
1586 context->setError(AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x", param);
1589 END_API_FUNC
1592 AL_API void AL_APIENTRY alBufferCallbackSOFT(ALuint buffer, ALenum format, ALsizei freq,
1593 ALBUFFERCALLBACKTYPESOFT callback, ALvoid *userptr)
1594 START_API_FUNC
1596 ContextRef context{GetContextRef()};
1597 if(!context) [[unlikely]] return;
1599 ALCdevice *device{context->mALDevice.get()};
1600 std::lock_guard<std::mutex> _{device->BufferLock};
1602 ALbuffer *albuf = LookupBuffer(device, buffer);
1603 if(!albuf) [[unlikely]]
1604 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1605 else if(freq < 1) [[unlikely]]
1606 context->setError(AL_INVALID_VALUE, "Invalid sample rate %d", freq);
1607 else if(callback == nullptr) [[unlikely]]
1608 context->setError(AL_INVALID_VALUE, "NULL callback");
1609 else
1611 auto usrfmt = DecomposeUserFormat(format);
1612 if(!usrfmt) [[unlikely]]
1613 context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
1614 else
1615 PrepareCallback(context.get(), albuf, freq, usrfmt->channels, usrfmt->type, callback,
1616 userptr);
1619 END_API_FUNC
1621 AL_API void AL_APIENTRY alGetBufferPtrSOFT(ALuint buffer, ALenum param, ALvoid **value)
1622 START_API_FUNC
1624 ContextRef context{GetContextRef()};
1625 if(!context) [[unlikely]] return;
1627 ALCdevice *device{context->mALDevice.get()};
1628 std::lock_guard<std::mutex> _{device->BufferLock};
1629 ALbuffer *albuf = LookupBuffer(device, buffer);
1630 if(!albuf) [[unlikely]]
1631 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1632 else if(!value) [[unlikely]]
1633 context->setError(AL_INVALID_VALUE, "NULL pointer");
1634 else switch(param)
1636 case AL_BUFFER_CALLBACK_FUNCTION_SOFT:
1637 *value = reinterpret_cast<void*>(albuf->mCallback);
1638 break;
1639 case AL_BUFFER_CALLBACK_USER_PARAM_SOFT:
1640 *value = albuf->mUserData;
1641 break;
1643 default:
1644 context->setError(AL_INVALID_ENUM, "Invalid buffer pointer property 0x%04x", param);
1647 END_API_FUNC
1649 AL_API void AL_APIENTRY alGetBuffer3PtrSOFT(ALuint buffer, ALenum param, ALvoid **value1, ALvoid **value2, ALvoid **value3)
1650 START_API_FUNC
1652 ContextRef context{GetContextRef()};
1653 if(!context) [[unlikely]] return;
1655 ALCdevice *device{context->mALDevice.get()};
1656 std::lock_guard<std::mutex> _{device->BufferLock};
1657 if(LookupBuffer(device, buffer) == nullptr) [[unlikely]]
1658 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1659 else if(!value1 || !value2 || !value3) [[unlikely]]
1660 context->setError(AL_INVALID_VALUE, "NULL pointer");
1661 else switch(param)
1663 default:
1664 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-pointer property 0x%04x", param);
1667 END_API_FUNC
1669 AL_API void AL_APIENTRY alGetBufferPtrvSOFT(ALuint buffer, ALenum param, ALvoid **values)
1670 START_API_FUNC
1672 switch(param)
1674 case AL_BUFFER_CALLBACK_FUNCTION_SOFT:
1675 case AL_BUFFER_CALLBACK_USER_PARAM_SOFT:
1676 alGetBufferPtrSOFT(buffer, param, values);
1677 return;
1680 ContextRef context{GetContextRef()};
1681 if(!context) [[unlikely]] return;
1683 ALCdevice *device{context->mALDevice.get()};
1684 std::lock_guard<std::mutex> _{device->BufferLock};
1685 if(LookupBuffer(device, buffer) == nullptr) [[unlikely]]
1686 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1687 else if(!values) [[unlikely]]
1688 context->setError(AL_INVALID_VALUE, "NULL pointer");
1689 else switch(param)
1691 default:
1692 context->setError(AL_INVALID_ENUM, "Invalid buffer pointer-vector property 0x%04x", param);
1695 END_API_FUNC
1698 BufferSubList::~BufferSubList()
1700 uint64_t usemask{~FreeMask};
1701 while(usemask)
1703 const int idx{al::countr_zero(usemask)};
1704 al::destroy_at(Buffers+idx);
1705 usemask &= ~(1_u64 << idx);
1707 FreeMask = ~usemask;
1708 al_free(Buffers);
1709 Buffers = nullptr;
1713 #ifdef ALSOFT_EAX
1714 FORCE_ALIGN ALboolean AL_APIENTRY EAXSetBufferMode(ALsizei n, const ALuint* buffers, ALint value)
1715 START_API_FUNC
1717 #define EAX_PREFIX "[EAXSetBufferMode] "
1719 const auto context = ContextRef{GetContextRef()};
1720 if(!context)
1722 ERR(EAX_PREFIX "%s\n", "No current context.");
1723 return ALC_FALSE;
1726 if(!eax_g_is_enabled)
1728 context->setError(AL_INVALID_OPERATION, EAX_PREFIX "%s", "EAX not enabled.");
1729 return ALC_FALSE;
1732 switch(value)
1734 case AL_STORAGE_AUTOMATIC:
1735 case AL_STORAGE_HARDWARE:
1736 case AL_STORAGE_ACCESSIBLE:
1737 break;
1739 default:
1740 context->setError(AL_INVALID_ENUM, EAX_PREFIX "Unsupported X-RAM mode 0x%x", value);
1741 return ALC_FALSE;
1744 if(n == 0)
1745 return ALC_TRUE;
1747 if(n < 0)
1749 context->setError(AL_INVALID_VALUE, EAX_PREFIX "Buffer count %d out of range", n);
1750 return ALC_FALSE;
1753 if(!buffers)
1755 context->setError(AL_INVALID_VALUE, EAX_PREFIX "%s", "Null AL buffers");
1756 return ALC_FALSE;
1759 auto device = context->mALDevice.get();
1760 std::lock_guard<std::mutex> device_lock{device->BufferLock};
1761 size_t total_needed{0};
1763 // Validate the buffers.
1765 for(auto i = 0;i < n;++i)
1767 const auto buffer = buffers[i];
1768 if(buffer == AL_NONE)
1769 continue;
1771 const auto al_buffer = LookupBuffer(device, buffer);
1772 if(!al_buffer) [[unlikely]]
1774 ERR(EAX_PREFIX "Invalid buffer ID %u.\n", buffer);
1775 return ALC_FALSE;
1778 /* TODO: Is the store location allowed to change for in-use buffers, or
1779 * only when not set/queued on a source?
1782 if(value == AL_STORAGE_HARDWARE && !al_buffer->eax_x_ram_is_hardware)
1784 /* FIXME: This doesn't account for duplicate buffers. When the same
1785 * buffer ID is specified multiple times in the provided list, it
1786 * counts each instance as more memory that needs to fit in X-RAM.
1788 if(std::numeric_limits<size_t>::max()-al_buffer->OriginalSize < total_needed) [[unlikely]]
1790 context->setError(AL_OUT_OF_MEMORY, EAX_PREFIX "Buffer size overflow (%u + %zu)\n",
1791 al_buffer->OriginalSize, total_needed);
1792 return ALC_FALSE;
1794 total_needed += al_buffer->OriginalSize;
1797 if(total_needed > device->eax_x_ram_free_size)
1799 context->setError(AL_INVALID_ENUM, EAX_PREFIX "Out of X-RAM memory (need: %zu, avail: %u)",
1800 total_needed, device->eax_x_ram_free_size);
1801 return ALC_FALSE;
1804 // Update the mode.
1806 for(auto i = 0;i < n;++i)
1808 const auto buffer = buffers[i];
1809 if(buffer == AL_NONE)
1810 continue;
1812 const auto al_buffer = LookupBuffer(device, buffer);
1813 assert(al_buffer);
1815 if(value != AL_STORAGE_ACCESSIBLE)
1816 eax_x_ram_apply(*device, *al_buffer);
1817 else
1818 eax_x_ram_clear(*device, *al_buffer);
1819 al_buffer->eax_x_ram_mode = value;
1822 return AL_TRUE;
1824 #undef EAX_PREFIX
1826 END_API_FUNC
1828 FORCE_ALIGN ALenum AL_APIENTRY EAXGetBufferMode(ALuint buffer, ALint* pReserved)
1829 START_API_FUNC
1831 #define EAX_PREFIX "[EAXGetBufferMode] "
1833 const auto context = ContextRef{GetContextRef()};
1834 if(!context)
1836 ERR(EAX_PREFIX "%s\n", "No current context.");
1837 return AL_NONE;
1840 if(!eax_g_is_enabled)
1842 context->setError(AL_INVALID_OPERATION, EAX_PREFIX "%s", "EAX not enabled.");
1843 return AL_NONE;
1846 if(pReserved)
1848 context->setError(AL_INVALID_VALUE, EAX_PREFIX "%s", "Non-null reserved parameter");
1849 return AL_NONE;
1852 auto device = context->mALDevice.get();
1853 std::lock_guard<std::mutex> device_lock{device->BufferLock};
1855 const auto al_buffer = LookupBuffer(device, buffer);
1856 if(!al_buffer)
1858 context->setError(AL_INVALID_NAME, EAX_PREFIX "Invalid buffer ID %u", buffer);
1859 return AL_NONE;
1862 return al_buffer->eax_x_ram_mode;
1864 #undef EAX_PREFIX
1866 END_API_FUNC
1868 #endif // ALSOFT_EAX