Reject older versions of PipeWire than built against
[openal-soft.git] / al / buffer.cpp
blob13407103ba259a74fef6d9b257edc4d8bf327618
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 al::make_optional(AmbiLayout::FuMa);
292 case AL_ACN_SOFT: return al::make_optional(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 al::make_optional(AmbiScaling::FuMa);
311 case AL_SN3D_SOFT: return al::make_optional(AmbiScaling::SN3D);
312 case AL_N3D_SOFT: return al::make_optional(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 al::make_optional(FmtMono);
333 case UserFmtStereo: return al::make_optional(FmtStereo);
334 case UserFmtRear: return al::make_optional(FmtRear);
335 case UserFmtQuad: return al::make_optional(FmtQuad);
336 case UserFmtX51: return al::make_optional(FmtX51);
337 case UserFmtX61: return al::make_optional(FmtX61);
338 case UserFmtX71: return al::make_optional(FmtX71);
339 case UserFmtBFormat2D: return al::make_optional(FmtBFormat2D);
340 case UserFmtBFormat3D: return al::make_optional(FmtBFormat3D);
341 case UserFmtUHJ2: return al::make_optional(FmtUHJ2);
342 case UserFmtUHJ3: return al::make_optional(FmtUHJ3);
343 case UserFmtUHJ4: return al::make_optional(FmtUHJ4);
345 return al::nullopt;
347 al::optional<FmtType> FmtFromUserFmt(UserFmtType type)
349 switch(type)
351 case UserFmtUByte: return al::make_optional(FmtUByte);
352 case UserFmtShort: return al::make_optional(FmtShort);
353 case UserFmtFloat: return al::make_optional(FmtFloat);
354 case UserFmtDouble: return al::make_optional(FmtDouble);
355 case UserFmtMulaw: return al::make_optional(FmtMulaw);
356 case UserFmtAlaw: return al::make_optional(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 UNLIKELY(device->BufferList.size() >= 1<<25)
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 UNLIKELY(!sublist->Buffers)
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 UNLIKELY(lidx >= device->BufferList.size())
471 return nullptr;
472 BufferSubList &sublist = device->BufferList[lidx];
473 if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
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 UNLIKELY(ReadRef(ALBuf->ref) != 0 || ALBuf->MappedAccess != 0)
535 SETERR_RETURN(context, 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 UNLIKELY(!DstChannels)
541 SETERR_RETURN(context, 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 UNLIKELY(SrcType == UserFmtIMA4 || SrcType == UserFmtMSADPCM)
552 SETERR_RETURN(context, 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 UNLIKELY(!DstType)
558 SETERR_RETURN(context, AL_INVALID_ENUM, , "Invalid format");
560 const ALuint unpackalign{ALBuf->UnpackAlign};
561 const ALuint align{SanitizeAlignment(SrcType, unpackalign)};
562 if UNLIKELY(align < 1)
563 SETERR_RETURN(context, 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 UNLIKELY(ALBuf->mChannels != *DstChannels || ALBuf->OriginalType != SrcType)
573 SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched format");
574 if UNLIKELY(ALBuf->OriginalAlign != align)
575 SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched alignment");
576 if(ALBuf->mAmbiOrder != ambiorder)
577 SETERR_RETURN(context, 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 UNLIKELY((size%SrcByteAlign) != 0)
588 SETERR_RETURN(context, AL_INVALID_VALUE,,
589 "Data size %d is not a multiple of frame size %d (%d unpack alignment)",
590 size, SrcByteAlign, align);
592 if UNLIKELY(size/SrcByteAlign > std::numeric_limits<ALsizei>::max()/align)
593 SETERR_RETURN(context, 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 UNLIKELY(frames > std::numeric_limits<size_t>::max()/FrameSize)
603 SETERR_RETURN(context, 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 SETERR_RETURN(context, 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);
635 if(SrcType == UserFmtIMA4)
637 assert(*DstType == FmtShort);
638 if(SrcData != nullptr && !ALBuf->mData.empty())
639 Convert_int16_ima4(reinterpret_cast<int16_t*>(ALBuf->mData.data()), SrcData,
640 NumChannels, frames, align);
641 ALBuf->OriginalAlign = align;
643 else if(SrcType == UserFmtMSADPCM)
645 assert(*DstType == FmtShort);
646 if(SrcData != nullptr && !ALBuf->mData.empty())
647 Convert_int16_msadpcm(reinterpret_cast<int16_t*>(ALBuf->mData.data()), SrcData,
648 NumChannels, frames, align);
649 ALBuf->OriginalAlign = align;
651 else
653 assert(DstType.has_value());
654 if(SrcData != nullptr && !ALBuf->mData.empty())
655 std::copy_n(SrcData, frames*FrameSize, ALBuf->mData.begin());
656 ALBuf->OriginalAlign = 1;
658 ALBuf->OriginalSize = size;
659 ALBuf->OriginalType = SrcType;
661 ALBuf->Access = access;
663 ALBuf->mSampleRate = static_cast<ALuint>(freq);
664 ALBuf->mChannels = *DstChannels;
665 ALBuf->mType = *DstType;
666 ALBuf->mAmbiOrder = ambiorder;
668 ALBuf->mCallback = nullptr;
669 ALBuf->mUserData = nullptr;
671 ALBuf->mSampleLen = frames;
672 ALBuf->mLoopStart = 0;
673 ALBuf->mLoopEnd = ALBuf->mSampleLen;
675 #ifdef ALSOFT_EAX
676 if(eax_g_is_enabled && ALBuf->eax_x_ram_mode != AL_STORAGE_ACCESSIBLE)
677 eax_x_ram_apply(*context->mALDevice, *ALBuf);
678 #endif
681 /** Prepares the buffer to use the specified callback, using the specified format. */
682 void PrepareCallback(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq,
683 UserFmtChannels SrcChannels, UserFmtType SrcType, ALBUFFERCALLBACKTYPESOFT callback,
684 void *userptr)
686 if UNLIKELY(ReadRef(ALBuf->ref) != 0 || ALBuf->MappedAccess != 0)
687 SETERR_RETURN(context, AL_INVALID_OPERATION,, "Modifying callback for in-use buffer %u",
688 ALBuf->id);
690 /* Currently no channel configurations need to be converted. */
691 auto DstChannels = FmtFromUserFmt(SrcChannels);
692 if UNLIKELY(!DstChannels)
693 SETERR_RETURN(context, AL_INVALID_ENUM,, "Invalid format");
695 /* IMA4 and MSADPCM convert to 16-bit short. Not supported with callbacks. */
696 auto DstType = FmtFromUserFmt(SrcType);
697 if UNLIKELY(!DstType)
698 SETERR_RETURN(context, AL_INVALID_ENUM,, "Unsupported callback format");
700 const ALuint ambiorder{IsBFormat(*DstChannels) ? ALBuf->UnpackAmbiOrder :
701 (IsUHJ(*DstChannels) ? 1 : 0)};
703 static constexpr uint line_size{BufferLineSize + MaxPostVoiceLoad};
704 al::vector<al::byte,16>(FrameSizeFromFmt(*DstChannels, *DstType, ambiorder) *
705 size_t{line_size}).swap(ALBuf->mData);
707 #ifdef ALSOFT_EAX
708 eax_x_ram_clear(*context->mALDevice, *ALBuf);
709 #endif
711 ALBuf->mCallback = callback;
712 ALBuf->mUserData = userptr;
714 ALBuf->OriginalType = SrcType;
715 ALBuf->OriginalSize = 0;
716 ALBuf->OriginalAlign = 1;
717 ALBuf->Access = 0;
719 ALBuf->mSampleRate = static_cast<ALuint>(freq);
720 ALBuf->mChannels = *DstChannels;
721 ALBuf->mType = *DstType;
722 ALBuf->mAmbiOrder = ambiorder;
724 ALBuf->mSampleLen = 0;
725 ALBuf->mLoopStart = 0;
726 ALBuf->mLoopEnd = ALBuf->mSampleLen;
730 struct DecompResult { UserFmtChannels channels; UserFmtType type; };
731 al::optional<DecompResult> DecomposeUserFormat(ALenum format)
733 struct FormatMap {
734 ALenum format;
735 UserFmtChannels channels;
736 UserFmtType type;
738 static const std::array<FormatMap,55> UserFmtList{{
739 { AL_FORMAT_MONO8, UserFmtMono, UserFmtUByte },
740 { AL_FORMAT_MONO16, UserFmtMono, UserFmtShort },
741 { AL_FORMAT_MONO_FLOAT32, UserFmtMono, UserFmtFloat },
742 { AL_FORMAT_MONO_DOUBLE_EXT, UserFmtMono, UserFmtDouble },
743 { AL_FORMAT_MONO_IMA4, UserFmtMono, UserFmtIMA4 },
744 { AL_FORMAT_MONO_MSADPCM_SOFT, UserFmtMono, UserFmtMSADPCM },
745 { AL_FORMAT_MONO_MULAW, UserFmtMono, UserFmtMulaw },
746 { AL_FORMAT_MONO_ALAW_EXT, UserFmtMono, UserFmtAlaw },
748 { AL_FORMAT_STEREO8, UserFmtStereo, UserFmtUByte },
749 { AL_FORMAT_STEREO16, UserFmtStereo, UserFmtShort },
750 { AL_FORMAT_STEREO_FLOAT32, UserFmtStereo, UserFmtFloat },
751 { AL_FORMAT_STEREO_DOUBLE_EXT, UserFmtStereo, UserFmtDouble },
752 { AL_FORMAT_STEREO_IMA4, UserFmtStereo, UserFmtIMA4 },
753 { AL_FORMAT_STEREO_MSADPCM_SOFT, UserFmtStereo, UserFmtMSADPCM },
754 { AL_FORMAT_STEREO_MULAW, UserFmtStereo, UserFmtMulaw },
755 { AL_FORMAT_STEREO_ALAW_EXT, UserFmtStereo, UserFmtAlaw },
757 { AL_FORMAT_REAR8, UserFmtRear, UserFmtUByte },
758 { AL_FORMAT_REAR16, UserFmtRear, UserFmtShort },
759 { AL_FORMAT_REAR32, UserFmtRear, UserFmtFloat },
760 { AL_FORMAT_REAR_MULAW, UserFmtRear, UserFmtMulaw },
762 { AL_FORMAT_QUAD8_LOKI, UserFmtQuad, UserFmtUByte },
763 { AL_FORMAT_QUAD16_LOKI, UserFmtQuad, UserFmtShort },
765 { AL_FORMAT_QUAD8, UserFmtQuad, UserFmtUByte },
766 { AL_FORMAT_QUAD16, UserFmtQuad, UserFmtShort },
767 { AL_FORMAT_QUAD32, UserFmtQuad, UserFmtFloat },
768 { AL_FORMAT_QUAD_MULAW, UserFmtQuad, UserFmtMulaw },
770 { AL_FORMAT_51CHN8, UserFmtX51, UserFmtUByte },
771 { AL_FORMAT_51CHN16, UserFmtX51, UserFmtShort },
772 { AL_FORMAT_51CHN32, UserFmtX51, UserFmtFloat },
773 { AL_FORMAT_51CHN_MULAW, UserFmtX51, UserFmtMulaw },
775 { AL_FORMAT_61CHN8, UserFmtX61, UserFmtUByte },
776 { AL_FORMAT_61CHN16, UserFmtX61, UserFmtShort },
777 { AL_FORMAT_61CHN32, UserFmtX61, UserFmtFloat },
778 { AL_FORMAT_61CHN_MULAW, UserFmtX61, UserFmtMulaw },
780 { AL_FORMAT_71CHN8, UserFmtX71, UserFmtUByte },
781 { AL_FORMAT_71CHN16, UserFmtX71, UserFmtShort },
782 { AL_FORMAT_71CHN32, UserFmtX71, UserFmtFloat },
783 { AL_FORMAT_71CHN_MULAW, UserFmtX71, UserFmtMulaw },
785 { AL_FORMAT_BFORMAT2D_8, UserFmtBFormat2D, UserFmtUByte },
786 { AL_FORMAT_BFORMAT2D_16, UserFmtBFormat2D, UserFmtShort },
787 { AL_FORMAT_BFORMAT2D_FLOAT32, UserFmtBFormat2D, UserFmtFloat },
788 { AL_FORMAT_BFORMAT2D_MULAW, UserFmtBFormat2D, UserFmtMulaw },
790 { AL_FORMAT_BFORMAT3D_8, UserFmtBFormat3D, UserFmtUByte },
791 { AL_FORMAT_BFORMAT3D_16, UserFmtBFormat3D, UserFmtShort },
792 { AL_FORMAT_BFORMAT3D_FLOAT32, UserFmtBFormat3D, UserFmtFloat },
793 { AL_FORMAT_BFORMAT3D_MULAW, UserFmtBFormat3D, UserFmtMulaw },
795 { AL_FORMAT_UHJ2CHN8_SOFT, UserFmtUHJ2, UserFmtUByte },
796 { AL_FORMAT_UHJ2CHN16_SOFT, UserFmtUHJ2, UserFmtShort },
797 { AL_FORMAT_UHJ2CHN_FLOAT32_SOFT, UserFmtUHJ2, UserFmtFloat },
799 { AL_FORMAT_UHJ3CHN8_SOFT, UserFmtUHJ3, UserFmtUByte },
800 { AL_FORMAT_UHJ3CHN16_SOFT, UserFmtUHJ3, UserFmtShort },
801 { AL_FORMAT_UHJ3CHN_FLOAT32_SOFT, UserFmtUHJ3, UserFmtFloat },
803 { AL_FORMAT_UHJ4CHN8_SOFT, UserFmtUHJ4, UserFmtUByte },
804 { AL_FORMAT_UHJ4CHN16_SOFT, UserFmtUHJ4, UserFmtShort },
805 { AL_FORMAT_UHJ4CHN_FLOAT32_SOFT, UserFmtUHJ4, UserFmtFloat },
808 for(const auto &fmt : UserFmtList)
810 if(fmt.format == format)
811 return al::make_optional<DecompResult>({fmt.channels, fmt.type});
813 return al::nullopt;
816 } // namespace
819 AL_API void AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
820 START_API_FUNC
822 ContextRef context{GetContextRef()};
823 if UNLIKELY(!context) return;
825 if UNLIKELY(n < 0)
826 context->setError(AL_INVALID_VALUE, "Generating %d buffers", n);
827 if UNLIKELY(n <= 0) return;
829 ALCdevice *device{context->mALDevice.get()};
830 std::lock_guard<std::mutex> _{device->BufferLock};
831 if(!EnsureBuffers(device, static_cast<ALuint>(n)))
833 context->setError(AL_OUT_OF_MEMORY, "Failed to allocate %d buffer%s", n, (n==1)?"":"s");
834 return;
837 if LIKELY(n == 1)
839 /* Special handling for the easy and normal case. */
840 ALbuffer *buffer{AllocBuffer(device)};
841 buffers[0] = buffer->id;
843 else
845 /* Store the allocated buffer IDs in a separate local list, to avoid
846 * modifying the user storage in case of failure.
848 al::vector<ALuint> ids;
849 ids.reserve(static_cast<ALuint>(n));
850 do {
851 ALbuffer *buffer{AllocBuffer(device)};
852 ids.emplace_back(buffer->id);
853 } while(--n);
854 std::copy(ids.begin(), ids.end(), buffers);
857 END_API_FUNC
859 AL_API void AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
860 START_API_FUNC
862 ContextRef context{GetContextRef()};
863 if UNLIKELY(!context) return;
865 if UNLIKELY(n < 0)
866 context->setError(AL_INVALID_VALUE, "Deleting %d buffers", n);
867 if UNLIKELY(n <= 0) return;
869 ALCdevice *device{context->mALDevice.get()};
870 std::lock_guard<std::mutex> _{device->BufferLock};
872 /* First try to find any buffers that are invalid or in-use. */
873 auto validate_buffer = [device, &context](const ALuint bid) -> bool
875 if(!bid) return true;
876 ALbuffer *ALBuf{LookupBuffer(device, bid)};
877 if UNLIKELY(!ALBuf)
879 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", bid);
880 return false;
882 if UNLIKELY(ReadRef(ALBuf->ref) != 0)
884 context->setError(AL_INVALID_OPERATION, "Deleting in-use buffer %u", bid);
885 return false;
887 return true;
889 const ALuint *buffers_end = buffers + n;
890 auto invbuf = std::find_if_not(buffers, buffers_end, validate_buffer);
891 if UNLIKELY(invbuf != buffers_end) return;
893 /* All good. Delete non-0 buffer IDs. */
894 auto delete_buffer = [device](const ALuint bid) -> void
896 ALbuffer *buffer{bid ? LookupBuffer(device, bid) : nullptr};
897 if(buffer) FreeBuffer(device, buffer);
899 std::for_each(buffers, buffers_end, delete_buffer);
901 END_API_FUNC
903 AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
904 START_API_FUNC
906 ContextRef context{GetContextRef()};
907 if LIKELY(context)
909 ALCdevice *device{context->mALDevice.get()};
910 std::lock_guard<std::mutex> _{device->BufferLock};
911 if(!buffer || LookupBuffer(device, buffer))
912 return AL_TRUE;
914 return AL_FALSE;
916 END_API_FUNC
919 AL_API void AL_APIENTRY alBufferData(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq)
920 START_API_FUNC
921 { alBufferStorageSOFT(buffer, format, data, size, freq, 0); }
922 END_API_FUNC
924 AL_API void AL_APIENTRY alBufferStorageSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq, ALbitfieldSOFT flags)
925 START_API_FUNC
927 ContextRef context{GetContextRef()};
928 if UNLIKELY(!context) return;
930 ALCdevice *device{context->mALDevice.get()};
931 std::lock_guard<std::mutex> _{device->BufferLock};
933 ALbuffer *albuf = LookupBuffer(device, buffer);
934 if UNLIKELY(!albuf)
935 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
936 else if UNLIKELY(size < 0)
937 context->setError(AL_INVALID_VALUE, "Negative storage size %d", size);
938 else if UNLIKELY(freq < 1)
939 context->setError(AL_INVALID_VALUE, "Invalid sample rate %d", freq);
940 else if UNLIKELY((flags&INVALID_STORAGE_MASK) != 0)
941 context->setError(AL_INVALID_VALUE, "Invalid storage flags 0x%x",
942 flags&INVALID_STORAGE_MASK);
943 else if UNLIKELY((flags&AL_MAP_PERSISTENT_BIT_SOFT) && !(flags&MAP_READ_WRITE_FLAGS))
944 context->setError(AL_INVALID_VALUE,
945 "Declaring persistently mapped storage without read or write access");
946 else
948 auto usrfmt = DecomposeUserFormat(format);
949 if UNLIKELY(!usrfmt)
950 context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
951 else
953 LoadData(context.get(), albuf, freq, static_cast<ALuint>(size), usrfmt->channels,
954 usrfmt->type, static_cast<const al::byte*>(data), flags);
958 END_API_FUNC
960 AL_API void* AL_APIENTRY alMapBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length, ALbitfieldSOFT access)
961 START_API_FUNC
963 ContextRef context{GetContextRef()};
964 if UNLIKELY(!context) return nullptr;
966 ALCdevice *device{context->mALDevice.get()};
967 std::lock_guard<std::mutex> _{device->BufferLock};
969 ALbuffer *albuf = LookupBuffer(device, buffer);
970 if UNLIKELY(!albuf)
971 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
972 else if UNLIKELY((access&INVALID_MAP_FLAGS) != 0)
973 context->setError(AL_INVALID_VALUE, "Invalid map flags 0x%x", access&INVALID_MAP_FLAGS);
974 else if UNLIKELY(!(access&MAP_READ_WRITE_FLAGS))
975 context->setError(AL_INVALID_VALUE, "Mapping buffer %u without read or write access",
976 buffer);
977 else
979 ALbitfieldSOFT unavailable = (albuf->Access^access) & access;
980 if UNLIKELY(ReadRef(albuf->ref) != 0 && !(access&AL_MAP_PERSISTENT_BIT_SOFT))
981 context->setError(AL_INVALID_OPERATION,
982 "Mapping in-use buffer %u without persistent mapping", buffer);
983 else if UNLIKELY(albuf->MappedAccess != 0)
984 context->setError(AL_INVALID_OPERATION, "Mapping already-mapped buffer %u", buffer);
985 else if UNLIKELY((unavailable&AL_MAP_READ_BIT_SOFT))
986 context->setError(AL_INVALID_VALUE,
987 "Mapping buffer %u for reading without read access", buffer);
988 else if UNLIKELY((unavailable&AL_MAP_WRITE_BIT_SOFT))
989 context->setError(AL_INVALID_VALUE,
990 "Mapping buffer %u for writing without write access", buffer);
991 else if UNLIKELY((unavailable&AL_MAP_PERSISTENT_BIT_SOFT))
992 context->setError(AL_INVALID_VALUE,
993 "Mapping buffer %u persistently without persistent access", buffer);
994 else if UNLIKELY(offset < 0 || length <= 0
995 || static_cast<ALuint>(offset) >= albuf->OriginalSize
996 || static_cast<ALuint>(length) > albuf->OriginalSize - static_cast<ALuint>(offset))
997 context->setError(AL_INVALID_VALUE, "Mapping invalid range %d+%d for buffer %u",
998 offset, length, buffer);
999 else
1001 void *retval{albuf->mData.data() + offset};
1002 albuf->MappedAccess = access;
1003 albuf->MappedOffset = offset;
1004 albuf->MappedSize = length;
1005 return retval;
1009 return nullptr;
1011 END_API_FUNC
1013 AL_API void AL_APIENTRY alUnmapBufferSOFT(ALuint buffer)
1014 START_API_FUNC
1016 ContextRef context{GetContextRef()};
1017 if UNLIKELY(!context) return;
1019 ALCdevice *device{context->mALDevice.get()};
1020 std::lock_guard<std::mutex> _{device->BufferLock};
1022 ALbuffer *albuf = LookupBuffer(device, buffer);
1023 if UNLIKELY(!albuf)
1024 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1025 else if UNLIKELY(albuf->MappedAccess == 0)
1026 context->setError(AL_INVALID_OPERATION, "Unmapping unmapped buffer %u", buffer);
1027 else
1029 albuf->MappedAccess = 0;
1030 albuf->MappedOffset = 0;
1031 albuf->MappedSize = 0;
1034 END_API_FUNC
1036 AL_API void AL_APIENTRY alFlushMappedBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length)
1037 START_API_FUNC
1039 ContextRef context{GetContextRef()};
1040 if UNLIKELY(!context) return;
1042 ALCdevice *device{context->mALDevice.get()};
1043 std::lock_guard<std::mutex> _{device->BufferLock};
1045 ALbuffer *albuf = LookupBuffer(device, buffer);
1046 if UNLIKELY(!albuf)
1047 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1048 else if UNLIKELY(!(albuf->MappedAccess&AL_MAP_WRITE_BIT_SOFT))
1049 context->setError(AL_INVALID_OPERATION, "Flushing buffer %u while not mapped for writing",
1050 buffer);
1051 else if UNLIKELY(offset < albuf->MappedOffset || length <= 0
1052 || offset >= albuf->MappedOffset+albuf->MappedSize
1053 || length > albuf->MappedOffset+albuf->MappedSize-offset)
1054 context->setError(AL_INVALID_VALUE, "Flushing invalid range %d+%d on buffer %u", offset,
1055 length, buffer);
1056 else
1058 /* FIXME: Need to use some method of double-buffering for the mixer and
1059 * app to hold separate memory, which can be safely transfered
1060 * asynchronously. Currently we just say the app shouldn't write where
1061 * OpenAL's reading, and hope for the best...
1063 std::atomic_thread_fence(std::memory_order_seq_cst);
1066 END_API_FUNC
1068 AL_API void AL_APIENTRY alBufferSubDataSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei offset, ALsizei length)
1069 START_API_FUNC
1071 ContextRef context{GetContextRef()};
1072 if UNLIKELY(!context) return;
1074 ALCdevice *device{context->mALDevice.get()};
1075 std::lock_guard<std::mutex> _{device->BufferLock};
1077 ALbuffer *albuf = LookupBuffer(device, buffer);
1078 if UNLIKELY(!albuf)
1080 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1081 return;
1084 auto usrfmt = DecomposeUserFormat(format);
1085 if UNLIKELY(!usrfmt)
1087 context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
1088 return;
1091 ALuint unpack_align{albuf->UnpackAlign};
1092 ALuint align{SanitizeAlignment(usrfmt->type, unpack_align)};
1093 if UNLIKELY(align < 1)
1094 context->setError(AL_INVALID_VALUE, "Invalid unpack alignment %u", unpack_align);
1095 else if UNLIKELY(long{usrfmt->channels} != long{albuf->mChannels}
1096 || usrfmt->type != albuf->OriginalType)
1097 context->setError(AL_INVALID_ENUM, "Unpacking data with mismatched format");
1098 else if UNLIKELY(align != albuf->OriginalAlign)
1099 context->setError(AL_INVALID_VALUE,
1100 "Unpacking data with alignment %u does not match original alignment %u", align,
1101 albuf->OriginalAlign);
1102 else if UNLIKELY(albuf->isBFormat() && albuf->UnpackAmbiOrder != albuf->mAmbiOrder)
1103 context->setError(AL_INVALID_VALUE, "Unpacking data with mismatched ambisonic order");
1104 else if UNLIKELY(albuf->MappedAccess != 0)
1105 context->setError(AL_INVALID_OPERATION, "Unpacking data into mapped buffer %u", buffer);
1106 else
1108 ALuint num_chans{albuf->channelsFromFmt()};
1109 ALuint frame_size{num_chans * albuf->bytesFromFmt()};
1110 ALuint byte_align{
1111 (albuf->OriginalType == UserFmtIMA4) ? ((align-1)/2 + 4) * num_chans :
1112 (albuf->OriginalType == UserFmtMSADPCM) ? ((align-2)/2 + 7) * num_chans :
1113 (align * frame_size)
1116 if UNLIKELY(offset < 0 || length < 0 || static_cast<ALuint>(offset) > albuf->OriginalSize
1117 || static_cast<ALuint>(length) > albuf->OriginalSize-static_cast<ALuint>(offset))
1118 context->setError(AL_INVALID_VALUE, "Invalid data sub-range %d+%d on buffer %u",
1119 offset, length, buffer);
1120 else if UNLIKELY((static_cast<ALuint>(offset)%byte_align) != 0)
1121 context->setError(AL_INVALID_VALUE,
1122 "Sub-range offset %d is not a multiple of frame size %d (%d unpack alignment)",
1123 offset, byte_align, align);
1124 else if UNLIKELY((static_cast<ALuint>(length)%byte_align) != 0)
1125 context->setError(AL_INVALID_VALUE,
1126 "Sub-range length %d is not a multiple of frame size %d (%d unpack alignment)",
1127 length, byte_align, align);
1128 else
1130 /* offset -> byte offset, length -> sample count */
1131 size_t byteoff{static_cast<ALuint>(offset)/byte_align * align * frame_size};
1132 size_t samplen{static_cast<ALuint>(length)/byte_align * align};
1134 void *dst = albuf->mData.data() + byteoff;
1135 if(usrfmt->type == UserFmtIMA4 && albuf->mType == FmtShort)
1136 Convert_int16_ima4(static_cast<int16_t*>(dst), static_cast<const al::byte*>(data),
1137 num_chans, samplen, align);
1138 else if(usrfmt->type == UserFmtMSADPCM && albuf->mType == FmtShort)
1139 Convert_int16_msadpcm(static_cast<int16_t*>(dst),
1140 static_cast<const al::byte*>(data), num_chans, samplen, align);
1141 else
1143 assert(long{usrfmt->type} == long{albuf->mType});
1144 memcpy(dst, data, size_t{samplen} * frame_size);
1149 END_API_FUNC
1152 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint /*buffer*/, ALuint /*samplerate*/,
1153 ALenum /*internalformat*/, ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/,
1154 const ALvoid* /*data*/)
1155 START_API_FUNC
1157 ContextRef context{GetContextRef()};
1158 if UNLIKELY(!context) return;
1160 context->setError(AL_INVALID_OPERATION, "alBufferSamplesSOFT not supported");
1162 END_API_FUNC
1164 AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
1165 ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, const ALvoid* /*data*/)
1166 START_API_FUNC
1168 ContextRef context{GetContextRef()};
1169 if UNLIKELY(!context) return;
1171 context->setError(AL_INVALID_OPERATION, "alBufferSubSamplesSOFT not supported");
1173 END_API_FUNC
1175 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
1176 ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, ALvoid* /*data*/)
1177 START_API_FUNC
1179 ContextRef context{GetContextRef()};
1180 if UNLIKELY(!context) return;
1182 context->setError(AL_INVALID_OPERATION, "alGetBufferSamplesSOFT not supported");
1184 END_API_FUNC
1186 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum /*format*/)
1187 START_API_FUNC
1189 ContextRef context{GetContextRef()};
1190 if UNLIKELY(!context) return AL_FALSE;
1192 context->setError(AL_INVALID_OPERATION, "alIsBufferFormatSupportedSOFT not supported");
1193 return AL_FALSE;
1195 END_API_FUNC
1198 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum param, ALfloat /*value*/)
1199 START_API_FUNC
1201 ContextRef context{GetContextRef()};
1202 if UNLIKELY(!context) return;
1204 ALCdevice *device{context->mALDevice.get()};
1205 std::lock_guard<std::mutex> _{device->BufferLock};
1207 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1208 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1209 else switch(param)
1211 default:
1212 context->setError(AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
1215 END_API_FUNC
1217 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum param,
1218 ALfloat /*value1*/, ALfloat /*value2*/, ALfloat /*value3*/)
1219 START_API_FUNC
1221 ContextRef context{GetContextRef()};
1222 if UNLIKELY(!context) return;
1224 ALCdevice *device{context->mALDevice.get()};
1225 std::lock_guard<std::mutex> _{device->BufferLock};
1227 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1228 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1229 else switch(param)
1231 default:
1232 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
1235 END_API_FUNC
1237 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum param, const ALfloat *values)
1238 START_API_FUNC
1240 ContextRef context{GetContextRef()};
1241 if UNLIKELY(!context) return;
1243 ALCdevice *device{context->mALDevice.get()};
1244 std::lock_guard<std::mutex> _{device->BufferLock};
1246 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1247 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1248 else if UNLIKELY(!values)
1249 context->setError(AL_INVALID_VALUE, "NULL pointer");
1250 else switch(param)
1252 default:
1253 context->setError(AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
1256 END_API_FUNC
1259 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum param, ALint value)
1260 START_API_FUNC
1262 ContextRef context{GetContextRef()};
1263 if UNLIKELY(!context) return;
1265 ALCdevice *device{context->mALDevice.get()};
1266 std::lock_guard<std::mutex> _{device->BufferLock};
1268 ALbuffer *albuf = LookupBuffer(device, buffer);
1269 if UNLIKELY(!albuf)
1270 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1271 else switch(param)
1273 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1274 if UNLIKELY(value < 0)
1275 context->setError(AL_INVALID_VALUE, "Invalid unpack block alignment %d", value);
1276 else
1277 albuf->UnpackAlign = static_cast<ALuint>(value);
1278 break;
1280 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1281 if UNLIKELY(value < 0)
1282 context->setError(AL_INVALID_VALUE, "Invalid pack block alignment %d", value);
1283 else
1284 albuf->PackAlign = static_cast<ALuint>(value);
1285 break;
1287 case AL_AMBISONIC_LAYOUT_SOFT:
1288 if UNLIKELY(ReadRef(albuf->ref) != 0)
1289 context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's ambisonic layout",
1290 buffer);
1291 else if UNLIKELY(value != AL_FUMA_SOFT && value != AL_ACN_SOFT)
1292 context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic layout %04x", value);
1293 else
1294 albuf->mAmbiLayout = AmbiLayoutFromEnum(value).value();
1295 break;
1297 case AL_AMBISONIC_SCALING_SOFT:
1298 if UNLIKELY(ReadRef(albuf->ref) != 0)
1299 context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's ambisonic scaling",
1300 buffer);
1301 else if UNLIKELY(value != AL_FUMA_SOFT && value != AL_SN3D_SOFT && value != AL_N3D_SOFT)
1302 context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic scaling %04x", value);
1303 else
1304 albuf->mAmbiScaling = AmbiScalingFromEnum(value).value();
1305 break;
1307 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1308 if UNLIKELY(value < 1 || value > 14)
1309 context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic order %d", value);
1310 else
1311 albuf->UnpackAmbiOrder = static_cast<ALuint>(value);
1312 break;
1314 default:
1315 context->setError(AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
1318 END_API_FUNC
1320 AL_API void AL_APIENTRY alBuffer3i(ALuint buffer, ALenum param,
1321 ALint /*value1*/, ALint /*value2*/, ALint /*value3*/)
1322 START_API_FUNC
1324 ContextRef context{GetContextRef()};
1325 if UNLIKELY(!context) return;
1327 ALCdevice *device{context->mALDevice.get()};
1328 std::lock_guard<std::mutex> _{device->BufferLock};
1330 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1331 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1332 else switch(param)
1334 default:
1335 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
1338 END_API_FUNC
1340 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum param, const ALint *values)
1341 START_API_FUNC
1343 if(values)
1345 switch(param)
1347 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1348 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1349 case AL_AMBISONIC_LAYOUT_SOFT:
1350 case AL_AMBISONIC_SCALING_SOFT:
1351 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1352 alBufferi(buffer, param, values[0]);
1353 return;
1357 ContextRef context{GetContextRef()};
1358 if UNLIKELY(!context) return;
1360 ALCdevice *device{context->mALDevice.get()};
1361 std::lock_guard<std::mutex> _{device->BufferLock};
1363 ALbuffer *albuf = LookupBuffer(device, buffer);
1364 if UNLIKELY(!albuf)
1365 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1366 else if UNLIKELY(!values)
1367 context->setError(AL_INVALID_VALUE, "NULL pointer");
1368 else switch(param)
1370 case AL_LOOP_POINTS_SOFT:
1371 if UNLIKELY(ReadRef(albuf->ref) != 0)
1372 context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's loop points",
1373 buffer);
1374 else if UNLIKELY(values[0] < 0 || values[0] >= values[1]
1375 || static_cast<ALuint>(values[1]) > albuf->mSampleLen)
1376 context->setError(AL_INVALID_VALUE, "Invalid loop point range %d -> %d on buffer %u",
1377 values[0], values[1], buffer);
1378 else
1380 albuf->mLoopStart = static_cast<ALuint>(values[0]);
1381 albuf->mLoopEnd = static_cast<ALuint>(values[1]);
1383 break;
1385 default:
1386 context->setError(AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x", param);
1389 END_API_FUNC
1392 AL_API void AL_APIENTRY alGetBufferf(ALuint buffer, ALenum param, ALfloat *value)
1393 START_API_FUNC
1395 ContextRef context{GetContextRef()};
1396 if UNLIKELY(!context) return;
1398 ALCdevice *device{context->mALDevice.get()};
1399 std::lock_guard<std::mutex> _{device->BufferLock};
1401 ALbuffer *albuf = LookupBuffer(device, buffer);
1402 if UNLIKELY(!albuf)
1403 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1404 else if UNLIKELY(!value)
1405 context->setError(AL_INVALID_VALUE, "NULL pointer");
1406 else switch(param)
1408 default:
1409 context->setError(AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
1412 END_API_FUNC
1414 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3)
1415 START_API_FUNC
1417 ContextRef context{GetContextRef()};
1418 if UNLIKELY(!context) return;
1420 ALCdevice *device{context->mALDevice.get()};
1421 std::lock_guard<std::mutex> _{device->BufferLock};
1423 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1424 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1425 else if UNLIKELY(!value1 || !value2 || !value3)
1426 context->setError(AL_INVALID_VALUE, "NULL pointer");
1427 else switch(param)
1429 default:
1430 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
1433 END_API_FUNC
1435 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum param, ALfloat *values)
1436 START_API_FUNC
1438 switch(param)
1440 case AL_SEC_LENGTH_SOFT:
1441 alGetBufferf(buffer, param, values);
1442 return;
1445 ContextRef context{GetContextRef()};
1446 if UNLIKELY(!context) return;
1448 ALCdevice *device{context->mALDevice.get()};
1449 std::lock_guard<std::mutex> _{device->BufferLock};
1451 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1452 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1453 else if UNLIKELY(!values)
1454 context->setError(AL_INVALID_VALUE, "NULL pointer");
1455 else switch(param)
1457 default:
1458 context->setError(AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
1461 END_API_FUNC
1464 AL_API void AL_APIENTRY alGetBufferi(ALuint buffer, ALenum param, ALint *value)
1465 START_API_FUNC
1467 ContextRef context{GetContextRef()};
1468 if UNLIKELY(!context) return;
1470 ALCdevice *device{context->mALDevice.get()};
1471 std::lock_guard<std::mutex> _{device->BufferLock};
1472 ALbuffer *albuf = LookupBuffer(device, buffer);
1473 if UNLIKELY(!albuf)
1474 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1475 else if UNLIKELY(!value)
1476 context->setError(AL_INVALID_VALUE, "NULL pointer");
1477 else switch(param)
1479 case AL_FREQUENCY:
1480 *value = static_cast<ALint>(albuf->mSampleRate);
1481 break;
1483 case AL_BITS:
1484 *value = static_cast<ALint>(albuf->bytesFromFmt() * 8);
1485 break;
1487 case AL_CHANNELS:
1488 *value = static_cast<ALint>(albuf->channelsFromFmt());
1489 break;
1491 case AL_SIZE:
1492 *value = static_cast<ALint>(albuf->mSampleLen * albuf->frameSizeFromFmt());
1493 break;
1495 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1496 *value = static_cast<ALint>(albuf->UnpackAlign);
1497 break;
1499 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1500 *value = static_cast<ALint>(albuf->PackAlign);
1501 break;
1503 case AL_AMBISONIC_LAYOUT_SOFT:
1504 *value = EnumFromAmbiLayout(albuf->mAmbiLayout);
1505 break;
1507 case AL_AMBISONIC_SCALING_SOFT:
1508 *value = EnumFromAmbiScaling(albuf->mAmbiScaling);
1509 break;
1511 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1512 *value = static_cast<int>(albuf->UnpackAmbiOrder);
1513 break;
1515 default:
1516 context->setError(AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
1519 END_API_FUNC
1521 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum param, ALint *value1, ALint *value2, ALint *value3)
1522 START_API_FUNC
1524 ContextRef context{GetContextRef()};
1525 if UNLIKELY(!context) return;
1527 ALCdevice *device{context->mALDevice.get()};
1528 std::lock_guard<std::mutex> _{device->BufferLock};
1529 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1530 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1531 else if UNLIKELY(!value1 || !value2 || !value3)
1532 context->setError(AL_INVALID_VALUE, "NULL pointer");
1533 else switch(param)
1535 default:
1536 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
1539 END_API_FUNC
1541 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum param, ALint *values)
1542 START_API_FUNC
1544 switch(param)
1546 case AL_FREQUENCY:
1547 case AL_BITS:
1548 case AL_CHANNELS:
1549 case AL_SIZE:
1550 case AL_INTERNAL_FORMAT_SOFT:
1551 case AL_BYTE_LENGTH_SOFT:
1552 case AL_SAMPLE_LENGTH_SOFT:
1553 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1554 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1555 case AL_AMBISONIC_LAYOUT_SOFT:
1556 case AL_AMBISONIC_SCALING_SOFT:
1557 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1558 alGetBufferi(buffer, param, values);
1559 return;
1562 ContextRef context{GetContextRef()};
1563 if UNLIKELY(!context) return;
1565 ALCdevice *device{context->mALDevice.get()};
1566 std::lock_guard<std::mutex> _{device->BufferLock};
1567 ALbuffer *albuf = LookupBuffer(device, buffer);
1568 if UNLIKELY(!albuf)
1569 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1570 else if UNLIKELY(!values)
1571 context->setError(AL_INVALID_VALUE, "NULL pointer");
1572 else switch(param)
1574 case AL_LOOP_POINTS_SOFT:
1575 values[0] = static_cast<ALint>(albuf->mLoopStart);
1576 values[1] = static_cast<ALint>(albuf->mLoopEnd);
1577 break;
1579 default:
1580 context->setError(AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x", param);
1583 END_API_FUNC
1586 AL_API void AL_APIENTRY alBufferCallbackSOFT(ALuint buffer, ALenum format, ALsizei freq,
1587 ALBUFFERCALLBACKTYPESOFT callback, ALvoid *userptr)
1588 START_API_FUNC
1590 ContextRef context{GetContextRef()};
1591 if UNLIKELY(!context) return;
1593 ALCdevice *device{context->mALDevice.get()};
1594 std::lock_guard<std::mutex> _{device->BufferLock};
1596 ALbuffer *albuf = LookupBuffer(device, buffer);
1597 if UNLIKELY(!albuf)
1598 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1599 else if UNLIKELY(freq < 1)
1600 context->setError(AL_INVALID_VALUE, "Invalid sample rate %d", freq);
1601 else if UNLIKELY(callback == nullptr)
1602 context->setError(AL_INVALID_VALUE, "NULL callback");
1603 else
1605 auto usrfmt = DecomposeUserFormat(format);
1606 if UNLIKELY(!usrfmt)
1607 context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
1608 else
1609 PrepareCallback(context.get(), albuf, freq, usrfmt->channels, usrfmt->type, callback,
1610 userptr);
1613 END_API_FUNC
1615 AL_API void AL_APIENTRY alGetBufferPtrSOFT(ALuint buffer, ALenum param, ALvoid **value)
1616 START_API_FUNC
1618 ContextRef context{GetContextRef()};
1619 if UNLIKELY(!context) return;
1621 ALCdevice *device{context->mALDevice.get()};
1622 std::lock_guard<std::mutex> _{device->BufferLock};
1623 ALbuffer *albuf = LookupBuffer(device, buffer);
1624 if UNLIKELY(!albuf)
1625 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1626 else if UNLIKELY(!value)
1627 context->setError(AL_INVALID_VALUE, "NULL pointer");
1628 else switch(param)
1630 case AL_BUFFER_CALLBACK_FUNCTION_SOFT:
1631 *value = reinterpret_cast<void*>(albuf->mCallback);
1632 break;
1633 case AL_BUFFER_CALLBACK_USER_PARAM_SOFT:
1634 *value = albuf->mUserData;
1635 break;
1637 default:
1638 context->setError(AL_INVALID_ENUM, "Invalid buffer pointer property 0x%04x", param);
1641 END_API_FUNC
1643 AL_API void AL_APIENTRY alGetBuffer3PtrSOFT(ALuint buffer, ALenum param, ALvoid **value1, ALvoid **value2, ALvoid **value3)
1644 START_API_FUNC
1646 ContextRef context{GetContextRef()};
1647 if UNLIKELY(!context) return;
1649 ALCdevice *device{context->mALDevice.get()};
1650 std::lock_guard<std::mutex> _{device->BufferLock};
1651 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1652 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1653 else if UNLIKELY(!value1 || !value2 || !value3)
1654 context->setError(AL_INVALID_VALUE, "NULL pointer");
1655 else switch(param)
1657 default:
1658 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-pointer property 0x%04x", param);
1661 END_API_FUNC
1663 AL_API void AL_APIENTRY alGetBufferPtrvSOFT(ALuint buffer, ALenum param, ALvoid **values)
1664 START_API_FUNC
1666 switch(param)
1668 case AL_BUFFER_CALLBACK_FUNCTION_SOFT:
1669 case AL_BUFFER_CALLBACK_USER_PARAM_SOFT:
1670 alGetBufferPtrSOFT(buffer, param, values);
1671 return;
1674 ContextRef context{GetContextRef()};
1675 if UNLIKELY(!context) return;
1677 ALCdevice *device{context->mALDevice.get()};
1678 std::lock_guard<std::mutex> _{device->BufferLock};
1679 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1680 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1681 else if UNLIKELY(!values)
1682 context->setError(AL_INVALID_VALUE, "NULL pointer");
1683 else switch(param)
1685 default:
1686 context->setError(AL_INVALID_ENUM, "Invalid buffer pointer-vector property 0x%04x", param);
1689 END_API_FUNC
1692 BufferSubList::~BufferSubList()
1694 uint64_t usemask{~FreeMask};
1695 while(usemask)
1697 const int idx{al::countr_zero(usemask)};
1698 al::destroy_at(Buffers+idx);
1699 usemask &= ~(1_u64 << idx);
1701 FreeMask = ~usemask;
1702 al_free(Buffers);
1703 Buffers = nullptr;
1707 #ifdef ALSOFT_EAX
1708 FORCE_ALIGN ALboolean AL_APIENTRY EAXSetBufferMode(ALsizei n, const ALuint* buffers, ALint value)
1709 START_API_FUNC
1711 #define EAX_PREFIX "[EAXSetBufferMode] "
1713 const auto context = ContextRef{GetContextRef()};
1714 if(!context)
1716 ERR(EAX_PREFIX "%s\n", "No current context.");
1717 return ALC_FALSE;
1720 if(!eax_g_is_enabled)
1722 context->setError(AL_INVALID_OPERATION, EAX_PREFIX "%s", "EAX not enabled.");
1723 return ALC_FALSE;
1726 switch(value)
1728 case AL_STORAGE_AUTOMATIC:
1729 case AL_STORAGE_HARDWARE:
1730 case AL_STORAGE_ACCESSIBLE:
1731 break;
1733 default:
1734 context->setError(AL_INVALID_ENUM, EAX_PREFIX "Unsupported X-RAM mode 0x%x", value);
1735 return ALC_FALSE;
1738 if(n == 0)
1739 return ALC_TRUE;
1741 if(n < 0)
1743 context->setError(AL_INVALID_VALUE, EAX_PREFIX "Buffer count %d out of range", n);
1744 return ALC_FALSE;
1747 if(!buffers)
1749 context->setError(AL_INVALID_VALUE, EAX_PREFIX "%s", "Null AL buffers");
1750 return ALC_FALSE;
1753 auto device = context->mALDevice.get();
1754 std::lock_guard<std::mutex> device_lock{device->BufferLock};
1755 size_t total_needed{0};
1757 // Validate the buffers.
1759 for(auto i = 0;i < n;++i)
1761 const auto buffer = buffers[i];
1762 if(buffer == AL_NONE)
1763 continue;
1765 const auto al_buffer = LookupBuffer(device, buffer);
1766 if (!al_buffer)
1768 ERR(EAX_PREFIX "Invalid buffer ID %u.\n", buffer);
1769 return ALC_FALSE;
1772 /* TODO: Is the store location allowed to change for in-use buffers, or
1773 * only when not set/queued on a source?
1776 if(value == AL_STORAGE_HARDWARE && !al_buffer->eax_x_ram_is_hardware)
1778 /* FIXME: This doesn't account for duplicate buffers. When the same
1779 * buffer ID is specified multiple times in the provided list, it
1780 * counts each instance as more memory that needs to fit in X-RAM.
1782 if(unlikely(std::numeric_limits<size_t>::max()-al_buffer->OriginalSize < total_needed))
1784 context->setError(AL_OUT_OF_MEMORY, EAX_PREFIX "Buffer size overflow (%u + %zu)\n",
1785 al_buffer->OriginalSize, total_needed);
1786 return ALC_FALSE;
1788 total_needed += al_buffer->OriginalSize;
1791 if(total_needed > device->eax_x_ram_free_size)
1793 context->setError(AL_INVALID_ENUM, EAX_PREFIX "Out of X-RAM memory (need: %zu, avail: %u)",
1794 total_needed, device->eax_x_ram_free_size);
1795 return ALC_FALSE;
1798 // Update the mode.
1800 for(auto i = 0;i < n;++i)
1802 const auto buffer = buffers[i];
1803 if(buffer == AL_NONE)
1804 continue;
1806 const auto al_buffer = LookupBuffer(device, buffer);
1807 assert(al_buffer);
1809 if(value != AL_STORAGE_ACCESSIBLE)
1810 eax_x_ram_apply(*device, *al_buffer);
1811 else
1812 eax_x_ram_clear(*device, *al_buffer);
1813 al_buffer->eax_x_ram_mode = value;
1816 return AL_TRUE;
1818 #undef EAX_PREFIX
1820 END_API_FUNC
1822 FORCE_ALIGN ALenum AL_APIENTRY EAXGetBufferMode(ALuint buffer, ALint* pReserved)
1823 START_API_FUNC
1825 #define EAX_PREFIX "[EAXGetBufferMode] "
1827 const auto context = ContextRef{GetContextRef()};
1828 if(!context)
1830 ERR(EAX_PREFIX "%s\n", "No current context.");
1831 return AL_NONE;
1834 if(!eax_g_is_enabled)
1836 context->setError(AL_INVALID_OPERATION, EAX_PREFIX "%s", "EAX not enabled.");
1837 return AL_NONE;
1840 if(pReserved)
1842 context->setError(AL_INVALID_VALUE, EAX_PREFIX "%s", "Non-null reserved parameter");
1843 return AL_NONE;
1846 auto device = context->mALDevice.get();
1847 std::lock_guard<std::mutex> device_lock{device->BufferLock};
1849 const auto al_buffer = LookupBuffer(device, buffer);
1850 if(!al_buffer)
1852 context->setError(AL_INVALID_NAME, EAX_PREFIX "Invalid buffer ID %u", buffer);
1853 return AL_NONE;
1856 return al_buffer->eax_x_ram_mode;
1858 #undef EAX_PREFIX
1860 END_API_FUNC
1862 #endif // ALSOFT_EAX