Remove unused atomic_invflag
[openal-soft.git] / al / buffer.cpp
blob3d9caf5425f27d8246e6ad46f4d941896a4de1b1
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] = al::to_integer<int>(src[0]) | (al::to_integer<int>(src[1])<<8);
122 sample[c] = (sample[c]^0x8000) - 32768;
123 src += 2;
124 index[c] = al::to_integer<int>(src[0]) | (al::to_integer<int>(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] = al::to_integer<ALuint>(src[0]) | (al::to_integer<ALuint>(src[1])<< 8) |
138 (al::to_integer<ALuint>(src[2])<<16) | (al::to_integer<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>(al::to_integer<ALubyte>(src[0]), 6);
168 ++src;
170 for(size_t c{0};c < numchans;c++)
172 delta[c] = al::to_integer<int>(src[0]) | (al::to_integer<int>(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>(al::to_integer<int>(src[0]) |
179 (al::to_integer<int>(src[1])<<8));
180 src += 2;
182 for(size_t c{0};c < numchans;c++)
184 samples[c][1] = static_cast<ALshort>(al::to_integer<int>(src[0]) |
185 (al::to_integer<int>(src[1])<<8));
186 src += 2;
189 /* Second sample is written first. */
190 for(size_t c{0};c < numchans;c++)
191 *(dst++) = samples[c][1];
192 for(size_t c{0};c < numchans;c++)
193 *(dst++) = samples[c][0];
195 int num{0};
196 for(size_t i{2};i < align;i++)
198 for(size_t c{0};c < numchans;c++)
200 /* Read the nibble (first is in the upper bits). */
201 al::byte nibble;
202 if(!(num++ & 1))
203 nibble = *src >> 4;
204 else
205 nibble = *(src++) & 0x0f;
207 int pred{(samples[c][0]*MSADPCMAdaptionCoeff[blockpred[c]][0] +
208 samples[c][1]*MSADPCMAdaptionCoeff[blockpred[c]][1]) / 256};
209 pred += (al::to_integer<int>(nibble^0x08) - 0x08) * delta[c];
210 pred = clampi(pred, -32768, 32767);
212 samples[c][1] = samples[c][0];
213 samples[c][0] = static_cast<int16_t>(pred);
215 delta[c] = (MSADPCMAdaption[al::to_integer<ALubyte>(nibble)] * delta[c]) / 256;
216 delta[c] = maxi(16, delta[c]);
218 *(dst++) = static_cast<int16_t>(pred);
223 void Convert_int16_ima4(int16_t *dst, const al::byte *src, size_t numchans, size_t len,
224 size_t align)
226 assert(numchans <= MaxAdpcmChannels);
227 const size_t byte_align{((align-1)/2 + 4) * numchans};
229 len /= align;
230 while(len--)
232 DecodeIMA4Block(dst, src, numchans, align);
233 src += byte_align;
234 dst += align*numchans;
238 void Convert_int16_msadpcm(int16_t *dst, const al::byte *src, size_t numchans, size_t len,
239 size_t align)
241 assert(numchans <= MaxAdpcmChannels);
242 const size_t byte_align{((align-2)/2 + 7) * numchans};
244 len /= align;
245 while(len--)
247 DecodeMSADPCMBlock(dst, src, numchans, align);
248 src += byte_align;
249 dst += align*numchans;
254 ALuint BytesFromUserFmt(UserFmtType type) noexcept
256 switch(type)
258 case UserFmtUByte: return sizeof(uint8_t);
259 case UserFmtShort: return sizeof(int16_t);
260 case UserFmtFloat: return sizeof(float);
261 case UserFmtDouble: return sizeof(double);
262 case UserFmtMulaw: return sizeof(uint8_t);
263 case UserFmtAlaw: return sizeof(uint8_t);
264 case UserFmtIMA4: break; /* not handled here */
265 case UserFmtMSADPCM: break; /* not handled here */
267 return 0;
269 ALuint ChannelsFromUserFmt(UserFmtChannels chans, ALuint ambiorder) noexcept
271 switch(chans)
273 case UserFmtMono: return 1;
274 case UserFmtStereo: return 2;
275 case UserFmtRear: return 2;
276 case UserFmtQuad: return 4;
277 case UserFmtX51: return 6;
278 case UserFmtX61: return 7;
279 case UserFmtX71: return 8;
280 case UserFmtBFormat2D: return (ambiorder*2) + 1;
281 case UserFmtBFormat3D: return (ambiorder+1) * (ambiorder+1);
282 case UserFmtUHJ2: return 2;
283 case UserFmtUHJ3: return 3;
284 case UserFmtUHJ4: return 4;
286 return 0;
289 al::optional<AmbiLayout> AmbiLayoutFromEnum(ALenum layout)
291 switch(layout)
293 case AL_FUMA_SOFT: return al::make_optional(AmbiLayout::FuMa);
294 case AL_ACN_SOFT: return al::make_optional(AmbiLayout::ACN);
296 return al::nullopt;
298 ALenum EnumFromAmbiLayout(AmbiLayout layout)
300 switch(layout)
302 case AmbiLayout::FuMa: return AL_FUMA_SOFT;
303 case AmbiLayout::ACN: return AL_ACN_SOFT;
305 throw std::runtime_error{"Invalid AmbiLayout: "+std::to_string(int(layout))};
308 al::optional<AmbiScaling> AmbiScalingFromEnum(ALenum scale)
310 switch(scale)
312 case AL_FUMA_SOFT: return al::make_optional(AmbiScaling::FuMa);
313 case AL_SN3D_SOFT: return al::make_optional(AmbiScaling::SN3D);
314 case AL_N3D_SOFT: return al::make_optional(AmbiScaling::N3D);
316 return al::nullopt;
318 ALenum EnumFromAmbiScaling(AmbiScaling scale)
320 switch(scale)
322 case AmbiScaling::FuMa: return AL_FUMA_SOFT;
323 case AmbiScaling::SN3D: return AL_SN3D_SOFT;
324 case AmbiScaling::N3D: return AL_N3D_SOFT;
325 case AmbiScaling::UHJ: break;
327 throw std::runtime_error{"Invalid AmbiScaling: "+std::to_string(int(scale))};
330 al::optional<FmtChannels> FmtFromUserFmt(UserFmtChannels chans)
332 switch(chans)
334 case UserFmtMono: return al::make_optional(FmtMono);
335 case UserFmtStereo: return al::make_optional(FmtStereo);
336 case UserFmtRear: return al::make_optional(FmtRear);
337 case UserFmtQuad: return al::make_optional(FmtQuad);
338 case UserFmtX51: return al::make_optional(FmtX51);
339 case UserFmtX61: return al::make_optional(FmtX61);
340 case UserFmtX71: return al::make_optional(FmtX71);
341 case UserFmtBFormat2D: return al::make_optional(FmtBFormat2D);
342 case UserFmtBFormat3D: return al::make_optional(FmtBFormat3D);
343 case UserFmtUHJ2: return al::make_optional(FmtUHJ2);
344 case UserFmtUHJ3: return al::make_optional(FmtUHJ3);
345 case UserFmtUHJ4: return al::make_optional(FmtUHJ4);
347 return al::nullopt;
349 al::optional<FmtType> FmtFromUserFmt(UserFmtType type)
351 switch(type)
353 case UserFmtUByte: return al::make_optional(FmtUByte);
354 case UserFmtShort: return al::make_optional(FmtShort);
355 case UserFmtFloat: return al::make_optional(FmtFloat);
356 case UserFmtDouble: return al::make_optional(FmtDouble);
357 case UserFmtMulaw: return al::make_optional(FmtMulaw);
358 case UserFmtAlaw: return al::make_optional(FmtAlaw);
359 /* ADPCM not handled here. */
360 case UserFmtIMA4: break;
361 case UserFmtMSADPCM: break;
363 return al::nullopt;
367 constexpr ALbitfieldSOFT INVALID_STORAGE_MASK{~unsigned(AL_MAP_READ_BIT_SOFT |
368 AL_MAP_WRITE_BIT_SOFT | AL_MAP_PERSISTENT_BIT_SOFT | AL_PRESERVE_DATA_BIT_SOFT)};
369 constexpr ALbitfieldSOFT MAP_READ_WRITE_FLAGS{AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT};
370 constexpr ALbitfieldSOFT INVALID_MAP_FLAGS{~unsigned(AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT |
371 AL_MAP_PERSISTENT_BIT_SOFT)};
374 bool EnsureBuffers(ALCdevice *device, size_t needed)
376 size_t count{std::accumulate(device->BufferList.cbegin(), device->BufferList.cend(), size_t{0},
377 [](size_t cur, const BufferSubList &sublist) noexcept -> size_t
378 { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })};
380 while(needed > count)
382 if UNLIKELY(device->BufferList.size() >= 1<<25)
383 return false;
385 device->BufferList.emplace_back();
386 auto sublist = device->BufferList.end() - 1;
387 sublist->FreeMask = ~0_u64;
388 sublist->Buffers = static_cast<ALbuffer*>(al_calloc(alignof(ALbuffer), sizeof(ALbuffer)*64));
389 if UNLIKELY(!sublist->Buffers)
391 device->BufferList.pop_back();
392 return false;
394 count += 64;
396 return true;
399 ALbuffer *AllocBuffer(ALCdevice *device)
401 auto sublist = std::find_if(device->BufferList.begin(), device->BufferList.end(),
402 [](const BufferSubList &entry) noexcept -> bool
403 { return entry.FreeMask != 0; });
404 auto lidx = static_cast<ALuint>(std::distance(device->BufferList.begin(), sublist));
405 auto slidx = static_cast<ALuint>(al::countr_zero(sublist->FreeMask));
406 ASSUME(slidx < 64);
408 ALbuffer *buffer{al::construct_at(sublist->Buffers + slidx)};
410 /* Add 1 to avoid buffer ID 0. */
411 buffer->id = ((lidx<<6) | slidx) + 1;
413 sublist->FreeMask &= ~(1_u64 << slidx);
415 return buffer;
418 void FreeBuffer(ALCdevice *device, ALbuffer *buffer)
420 #ifdef ALSOFT_EAX
421 if (buffer->eax_x_ram_is_hardware)
423 const auto buffer_size = static_cast<ALsizei>(buffer->OriginalSize);
424 assert((device->eax_x_ram_free_size + buffer_size) <= eax_x_ram_max_size);
425 device->eax_x_ram_free_size += buffer_size;
427 #endif // ALSOFT_EAX
429 const ALuint id{buffer->id - 1};
430 const size_t lidx{id >> 6};
431 const ALuint slidx{id & 0x3f};
433 al::destroy_at(buffer);
435 device->BufferList[lidx].FreeMask |= 1_u64 << slidx;
438 inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id)
440 const size_t lidx{(id-1) >> 6};
441 const ALuint slidx{(id-1) & 0x3f};
443 if UNLIKELY(lidx >= device->BufferList.size())
444 return nullptr;
445 BufferSubList &sublist = device->BufferList[lidx];
446 if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
447 return nullptr;
448 return sublist.Buffers + slidx;
452 ALuint SanitizeAlignment(UserFmtType type, ALuint align)
454 if(align == 0)
456 if(type == UserFmtIMA4)
458 /* Here is where things vary:
459 * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
460 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
462 return 65;
464 if(type == UserFmtMSADPCM)
465 return 64;
466 return 1;
469 if(type == UserFmtIMA4)
471 /* IMA4 block alignment must be a multiple of 8, plus 1. */
472 if((align&7) == 1) return static_cast<ALuint>(align);
473 return 0;
475 if(type == UserFmtMSADPCM)
477 /* MSADPCM block alignment must be a multiple of 2. */
478 if((align&1) == 0) return static_cast<ALuint>(align);
479 return 0;
482 return static_cast<ALuint>(align);
486 const ALchar *NameFromUserFmtType(UserFmtType type)
488 switch(type)
490 case UserFmtUByte: return "UInt8";
491 case UserFmtShort: return "Int16";
492 case UserFmtFloat: return "Float32";
493 case UserFmtDouble: return "Float64";
494 case UserFmtMulaw: return "muLaw";
495 case UserFmtAlaw: return "aLaw";
496 case UserFmtIMA4: return "IMA4 ADPCM";
497 case UserFmtMSADPCM: return "MSADPCM";
499 return "<internal type error>";
502 #ifdef ALSOFT_EAX
503 bool eax_x_ram_validate_buffer(
504 ALCdevice& al_device,
505 ALbuffer& al_buffer)
507 switch (al_buffer.eax_x_ram_mode)
509 case AL_STORAGE_HARDWARE:
510 if (al_buffer.OriginalSize > static_cast<ALuint>(al_device.eax_x_ram_free_size))
512 return false;
515 break;
517 case AL_STORAGE_AUTOMATIC:
518 case AL_STORAGE_ACCESSIBLE:
519 break;
521 default:
522 assert(false && "Unsupported X-RAM mode.");
523 return false;
526 return true;
529 void eax_x_ram_update_buffer(
530 ALCdevice& al_device,
531 ALbuffer& al_buffer)
533 const auto buffer_size = static_cast<ALsizei>(al_buffer.OriginalSize);
535 auto is_hardware = al_buffer.eax_x_ram_is_hardware;
536 auto size_delta = ALsizei{};
538 switch (al_buffer.eax_x_ram_mode)
540 case AL_STORAGE_AUTOMATIC:
541 if (!al_buffer.eax_x_ram_is_dirty)
543 // First usage.
545 if (buffer_size <= al_device.eax_x_ram_free_size)
547 // Have enough X-RAM memory.
549 is_hardware = true;
550 size_delta = -buffer_size;
553 else
555 // Used at least once.
556 // From now on, use only system memory.
558 is_hardware = false;
560 if (al_buffer.eax_x_ram_is_hardware)
562 // First allocation was in X-RAM.
563 // Free that block.
565 size_delta = buffer_size;
569 break;
571 case AL_STORAGE_HARDWARE:
572 is_hardware = true;
573 size_delta = buffer_size;
575 break;
577 case AL_STORAGE_ACCESSIBLE:
578 // Always use system memory.
579 is_hardware = false;
580 break;
582 default:
583 break;
586 al_buffer.eax_x_ram_is_hardware = is_hardware;
587 al_buffer.eax_x_ram_is_dirty = true;
589 assert(
590 (al_device.eax_x_ram_free_size + size_delta) >= eax_x_ram_min_size &&
591 (al_device.eax_x_ram_free_size + size_delta) <= eax_x_ram_max_size
594 al_device.eax_x_ram_free_size += size_delta;
596 #endif // ALSOFT_EAX
598 /** Loads the specified data into the buffer, using the specified format. */
599 void LoadData(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq, ALuint size,
600 UserFmtChannels SrcChannels, UserFmtType SrcType, const al::byte *SrcData,
601 ALbitfieldSOFT access)
603 if UNLIKELY(ReadRef(ALBuf->ref) != 0 || ALBuf->MappedAccess != 0)
604 SETERR_RETURN(context, AL_INVALID_OPERATION,, "Modifying storage for in-use buffer %u",
605 ALBuf->id);
607 /* Currently no channel configurations need to be converted. */
608 auto DstChannels = FmtFromUserFmt(SrcChannels);
609 if UNLIKELY(!DstChannels)
610 SETERR_RETURN(context, AL_INVALID_ENUM, , "Invalid format");
612 /* IMA4 and MSADPCM convert to 16-bit short.
614 * TODO: Currently we can only map samples when they're not converted. To
615 * allow it would need some kind of double-buffering to hold onto a copy of
616 * the original data.
618 if((access&MAP_READ_WRITE_FLAGS))
620 if UNLIKELY(SrcType == UserFmtIMA4 || SrcType == UserFmtMSADPCM)
621 SETERR_RETURN(context, AL_INVALID_VALUE,, "%s samples cannot be mapped",
622 NameFromUserFmtType(SrcType));
624 auto DstType = (SrcType == UserFmtIMA4 || SrcType == UserFmtMSADPCM)
625 ? al::make_optional(FmtShort) : FmtFromUserFmt(SrcType);
626 if UNLIKELY(!DstType)
627 SETERR_RETURN(context, AL_INVALID_ENUM, , "Invalid format");
629 const ALuint unpackalign{ALBuf->UnpackAlign};
630 const ALuint align{SanitizeAlignment(SrcType, unpackalign)};
631 if UNLIKELY(align < 1)
632 SETERR_RETURN(context, AL_INVALID_VALUE,, "Invalid unpack alignment %u for %s samples",
633 unpackalign, NameFromUserFmtType(SrcType));
635 const ALuint ambiorder{IsBFormat(*DstChannels) ? ALBuf->UnpackAmbiOrder :
636 (IsUHJ(*DstChannels) ? 1 : 0)};
638 if((access&AL_PRESERVE_DATA_BIT_SOFT))
640 /* Can only preserve data with the same format and alignment. */
641 if UNLIKELY(ALBuf->mChannels != *DstChannels || ALBuf->OriginalType != SrcType)
642 SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched format");
643 if UNLIKELY(ALBuf->OriginalAlign != align)
644 SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched alignment");
645 if(ALBuf->mAmbiOrder != ambiorder)
646 SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched order");
649 /* Convert the input/source size in bytes to sample frames using the unpack
650 * block alignment.
652 const ALuint SrcByteAlign{ChannelsFromUserFmt(SrcChannels, ambiorder) *
653 ((SrcType == UserFmtIMA4) ? (align-1)/2 + 4 :
654 (SrcType == UserFmtMSADPCM) ? (align-2)/2 + 7 :
655 (align * BytesFromUserFmt(SrcType)))};
656 if UNLIKELY((size%SrcByteAlign) != 0)
657 SETERR_RETURN(context, AL_INVALID_VALUE,,
658 "Data size %d is not a multiple of frame size %d (%d unpack alignment)",
659 size, SrcByteAlign, align);
661 if UNLIKELY(size/SrcByteAlign > std::numeric_limits<ALsizei>::max()/align)
662 SETERR_RETURN(context, AL_OUT_OF_MEMORY,,
663 "Buffer size overflow, %d blocks x %d samples per block", size/SrcByteAlign, align);
664 const ALuint frames{size / SrcByteAlign * align};
666 /* Convert the sample frames to the number of bytes needed for internal
667 * storage.
669 ALuint NumChannels{ChannelsFromFmt(*DstChannels, ambiorder)};
670 ALuint FrameSize{NumChannels * BytesFromFmt(*DstType)};
671 if UNLIKELY(frames > std::numeric_limits<size_t>::max()/FrameSize)
672 SETERR_RETURN(context, AL_OUT_OF_MEMORY,,
673 "Buffer size overflow, %d frames x %d bytes per frame", frames, FrameSize);
674 size_t newsize{static_cast<size_t>(frames) * FrameSize};
676 /* Round up to the next 16-byte multiple. This could reallocate only when
677 * increasing or the new size is less than half the current, but then the
678 * buffer's AL_SIZE would not be very reliable for accounting buffer memory
679 * usage, and reporting the real size could cause problems for apps that
680 * use AL_SIZE to try to get the buffer's play length.
682 newsize = RoundUp(newsize, 16);
683 if(newsize != ALBuf->mData.size())
685 auto newdata = al::vector<al::byte,16>(newsize, al::byte{});
686 if((access&AL_PRESERVE_DATA_BIT_SOFT))
688 const size_t tocopy{minz(newdata.size(), ALBuf->mData.size())};
689 std::copy_n(ALBuf->mData.begin(), tocopy, newdata.begin());
691 newdata.swap(ALBuf->mData);
694 if(SrcType == UserFmtIMA4)
696 assert(*DstType == FmtShort);
697 if(SrcData != nullptr && !ALBuf->mData.empty())
698 Convert_int16_ima4(reinterpret_cast<int16_t*>(ALBuf->mData.data()), SrcData,
699 NumChannels, frames, align);
700 ALBuf->OriginalAlign = align;
702 else if(SrcType == UserFmtMSADPCM)
704 assert(*DstType == FmtShort);
705 if(SrcData != nullptr && !ALBuf->mData.empty())
706 Convert_int16_msadpcm(reinterpret_cast<int16_t*>(ALBuf->mData.data()), SrcData,
707 NumChannels, frames, align);
708 ALBuf->OriginalAlign = align;
710 else
712 assert(DstType.has_value());
713 if(SrcData != nullptr && !ALBuf->mData.empty())
714 std::copy_n(SrcData, frames*FrameSize, ALBuf->mData.begin());
715 ALBuf->OriginalAlign = 1;
717 ALBuf->OriginalSize = size;
718 ALBuf->OriginalType = SrcType;
720 ALBuf->Access = access;
722 ALBuf->mSampleRate = static_cast<ALuint>(freq);
723 ALBuf->mChannels = *DstChannels;
724 ALBuf->mType = *DstType;
725 ALBuf->mAmbiOrder = ambiorder;
727 ALBuf->mCallback = nullptr;
728 ALBuf->mUserData = nullptr;
730 ALBuf->mSampleLen = frames;
731 ALBuf->mLoopStart = 0;
732 ALBuf->mLoopEnd = ALBuf->mSampleLen;
734 #ifdef ALSOFT_EAX
735 if (eax_g_is_enabled)
737 eax_x_ram_update_buffer(*context->mALDevice, *ALBuf);
739 #endif // ALSOFT_EAX
742 /** Prepares the buffer to use the specified callback, using the specified format. */
743 void PrepareCallback(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq,
744 UserFmtChannels SrcChannels, UserFmtType SrcType, ALBUFFERCALLBACKTYPESOFT callback,
745 void *userptr)
747 if UNLIKELY(ReadRef(ALBuf->ref) != 0 || ALBuf->MappedAccess != 0)
748 SETERR_RETURN(context, AL_INVALID_OPERATION,, "Modifying callback for in-use buffer %u",
749 ALBuf->id);
751 /* Currently no channel configurations need to be converted. */
752 auto DstChannels = FmtFromUserFmt(SrcChannels);
753 if UNLIKELY(!DstChannels)
754 SETERR_RETURN(context, AL_INVALID_ENUM,, "Invalid format");
756 /* IMA4 and MSADPCM convert to 16-bit short. Not supported with callbacks. */
757 auto DstType = FmtFromUserFmt(SrcType);
758 if UNLIKELY(!DstType)
759 SETERR_RETURN(context, AL_INVALID_ENUM,, "Unsupported callback format");
761 const ALuint ambiorder{IsBFormat(*DstChannels) ? ALBuf->UnpackAmbiOrder :
762 (IsUHJ(*DstChannels) ? 1 : 0)};
764 static constexpr uint line_size{BufferLineSize + MaxPostVoiceLoad};
765 al::vector<al::byte,16>(FrameSizeFromFmt(*DstChannels, *DstType, ambiorder) *
766 size_t{line_size}).swap(ALBuf->mData);
768 ALBuf->mCallback = callback;
769 ALBuf->mUserData = userptr;
771 ALBuf->OriginalType = SrcType;
772 ALBuf->OriginalSize = 0;
773 ALBuf->OriginalAlign = 1;
774 ALBuf->Access = 0;
776 ALBuf->mSampleRate = static_cast<ALuint>(freq);
777 ALBuf->mChannels = *DstChannels;
778 ALBuf->mType = *DstType;
779 ALBuf->mAmbiOrder = ambiorder;
781 ALBuf->mSampleLen = 0;
782 ALBuf->mLoopStart = 0;
783 ALBuf->mLoopEnd = ALBuf->mSampleLen;
787 struct DecompResult { UserFmtChannels channels; UserFmtType type; };
788 al::optional<DecompResult> DecomposeUserFormat(ALenum format)
790 struct FormatMap {
791 ALenum format;
792 UserFmtChannels channels;
793 UserFmtType type;
795 static const std::array<FormatMap,55> UserFmtList{{
796 { AL_FORMAT_MONO8, UserFmtMono, UserFmtUByte },
797 { AL_FORMAT_MONO16, UserFmtMono, UserFmtShort },
798 { AL_FORMAT_MONO_FLOAT32, UserFmtMono, UserFmtFloat },
799 { AL_FORMAT_MONO_DOUBLE_EXT, UserFmtMono, UserFmtDouble },
800 { AL_FORMAT_MONO_IMA4, UserFmtMono, UserFmtIMA4 },
801 { AL_FORMAT_MONO_MSADPCM_SOFT, UserFmtMono, UserFmtMSADPCM },
802 { AL_FORMAT_MONO_MULAW, UserFmtMono, UserFmtMulaw },
803 { AL_FORMAT_MONO_ALAW_EXT, UserFmtMono, UserFmtAlaw },
805 { AL_FORMAT_STEREO8, UserFmtStereo, UserFmtUByte },
806 { AL_FORMAT_STEREO16, UserFmtStereo, UserFmtShort },
807 { AL_FORMAT_STEREO_FLOAT32, UserFmtStereo, UserFmtFloat },
808 { AL_FORMAT_STEREO_DOUBLE_EXT, UserFmtStereo, UserFmtDouble },
809 { AL_FORMAT_STEREO_IMA4, UserFmtStereo, UserFmtIMA4 },
810 { AL_FORMAT_STEREO_MSADPCM_SOFT, UserFmtStereo, UserFmtMSADPCM },
811 { AL_FORMAT_STEREO_MULAW, UserFmtStereo, UserFmtMulaw },
812 { AL_FORMAT_STEREO_ALAW_EXT, UserFmtStereo, UserFmtAlaw },
814 { AL_FORMAT_REAR8, UserFmtRear, UserFmtUByte },
815 { AL_FORMAT_REAR16, UserFmtRear, UserFmtShort },
816 { AL_FORMAT_REAR32, UserFmtRear, UserFmtFloat },
817 { AL_FORMAT_REAR_MULAW, UserFmtRear, UserFmtMulaw },
819 { AL_FORMAT_QUAD8_LOKI, UserFmtQuad, UserFmtUByte },
820 { AL_FORMAT_QUAD16_LOKI, UserFmtQuad, UserFmtShort },
822 { AL_FORMAT_QUAD8, UserFmtQuad, UserFmtUByte },
823 { AL_FORMAT_QUAD16, UserFmtQuad, UserFmtShort },
824 { AL_FORMAT_QUAD32, UserFmtQuad, UserFmtFloat },
825 { AL_FORMAT_QUAD_MULAW, UserFmtQuad, UserFmtMulaw },
827 { AL_FORMAT_51CHN8, UserFmtX51, UserFmtUByte },
828 { AL_FORMAT_51CHN16, UserFmtX51, UserFmtShort },
829 { AL_FORMAT_51CHN32, UserFmtX51, UserFmtFloat },
830 { AL_FORMAT_51CHN_MULAW, UserFmtX51, UserFmtMulaw },
832 { AL_FORMAT_61CHN8, UserFmtX61, UserFmtUByte },
833 { AL_FORMAT_61CHN16, UserFmtX61, UserFmtShort },
834 { AL_FORMAT_61CHN32, UserFmtX61, UserFmtFloat },
835 { AL_FORMAT_61CHN_MULAW, UserFmtX61, UserFmtMulaw },
837 { AL_FORMAT_71CHN8, UserFmtX71, UserFmtUByte },
838 { AL_FORMAT_71CHN16, UserFmtX71, UserFmtShort },
839 { AL_FORMAT_71CHN32, UserFmtX71, UserFmtFloat },
840 { AL_FORMAT_71CHN_MULAW, UserFmtX71, UserFmtMulaw },
842 { AL_FORMAT_BFORMAT2D_8, UserFmtBFormat2D, UserFmtUByte },
843 { AL_FORMAT_BFORMAT2D_16, UserFmtBFormat2D, UserFmtShort },
844 { AL_FORMAT_BFORMAT2D_FLOAT32, UserFmtBFormat2D, UserFmtFloat },
845 { AL_FORMAT_BFORMAT2D_MULAW, UserFmtBFormat2D, UserFmtMulaw },
847 { AL_FORMAT_BFORMAT3D_8, UserFmtBFormat3D, UserFmtUByte },
848 { AL_FORMAT_BFORMAT3D_16, UserFmtBFormat3D, UserFmtShort },
849 { AL_FORMAT_BFORMAT3D_FLOAT32, UserFmtBFormat3D, UserFmtFloat },
850 { AL_FORMAT_BFORMAT3D_MULAW, UserFmtBFormat3D, UserFmtMulaw },
852 { AL_FORMAT_UHJ2CHN8_SOFT, UserFmtUHJ2, UserFmtUByte },
853 { AL_FORMAT_UHJ2CHN16_SOFT, UserFmtUHJ2, UserFmtShort },
854 { AL_FORMAT_UHJ2CHN_FLOAT32_SOFT, UserFmtUHJ2, UserFmtFloat },
856 { AL_FORMAT_UHJ3CHN8_SOFT, UserFmtUHJ3, UserFmtUByte },
857 { AL_FORMAT_UHJ3CHN16_SOFT, UserFmtUHJ3, UserFmtShort },
858 { AL_FORMAT_UHJ3CHN_FLOAT32_SOFT, UserFmtUHJ3, UserFmtFloat },
860 { AL_FORMAT_UHJ4CHN8_SOFT, UserFmtUHJ4, UserFmtUByte },
861 { AL_FORMAT_UHJ4CHN16_SOFT, UserFmtUHJ4, UserFmtShort },
862 { AL_FORMAT_UHJ4CHN_FLOAT32_SOFT, UserFmtUHJ4, UserFmtFloat },
865 for(const auto &fmt : UserFmtList)
867 if(fmt.format == format)
868 return al::make_optional<DecompResult>({fmt.channels, fmt.type});
870 return al::nullopt;
873 } // namespace
876 AL_API void AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
877 START_API_FUNC
879 ContextRef context{GetContextRef()};
880 if UNLIKELY(!context) return;
882 if UNLIKELY(n < 0)
883 context->setError(AL_INVALID_VALUE, "Generating %d buffers", n);
884 if UNLIKELY(n <= 0) return;
886 ALCdevice *device{context->mALDevice.get()};
887 std::lock_guard<std::mutex> _{device->BufferLock};
888 if(!EnsureBuffers(device, static_cast<ALuint>(n)))
890 context->setError(AL_OUT_OF_MEMORY, "Failed to allocate %d buffer%s", n, (n==1)?"":"s");
891 return;
894 if LIKELY(n == 1)
896 /* Special handling for the easy and normal case. */
897 ALbuffer *buffer{AllocBuffer(device)};
898 buffers[0] = buffer->id;
900 else
902 /* Store the allocated buffer IDs in a separate local list, to avoid
903 * modifying the user storage in case of failure.
905 al::vector<ALuint> ids;
906 ids.reserve(static_cast<ALuint>(n));
907 do {
908 ALbuffer *buffer{AllocBuffer(device)};
909 ids.emplace_back(buffer->id);
910 } while(--n);
911 std::copy(ids.begin(), ids.end(), buffers);
914 END_API_FUNC
916 AL_API void AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
917 START_API_FUNC
919 ContextRef context{GetContextRef()};
920 if UNLIKELY(!context) return;
922 if UNLIKELY(n < 0)
923 context->setError(AL_INVALID_VALUE, "Deleting %d buffers", n);
924 if UNLIKELY(n <= 0) return;
926 ALCdevice *device{context->mALDevice.get()};
927 std::lock_guard<std::mutex> _{device->BufferLock};
929 /* First try to find any buffers that are invalid or in-use. */
930 auto validate_buffer = [device, &context](const ALuint bid) -> bool
932 if(!bid) return true;
933 ALbuffer *ALBuf{LookupBuffer(device, bid)};
934 if UNLIKELY(!ALBuf)
936 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", bid);
937 return false;
939 if UNLIKELY(ReadRef(ALBuf->ref) != 0)
941 context->setError(AL_INVALID_OPERATION, "Deleting in-use buffer %u", bid);
942 return false;
944 return true;
946 const ALuint *buffers_end = buffers + n;
947 auto invbuf = std::find_if_not(buffers, buffers_end, validate_buffer);
948 if UNLIKELY(invbuf != buffers_end) return;
950 /* All good. Delete non-0 buffer IDs. */
951 auto delete_buffer = [device](const ALuint bid) -> void
953 ALbuffer *buffer{bid ? LookupBuffer(device, bid) : nullptr};
954 if(buffer) FreeBuffer(device, buffer);
956 std::for_each(buffers, buffers_end, delete_buffer);
958 END_API_FUNC
960 AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
961 START_API_FUNC
963 ContextRef context{GetContextRef()};
964 if LIKELY(context)
966 ALCdevice *device{context->mALDevice.get()};
967 std::lock_guard<std::mutex> _{device->BufferLock};
968 if(!buffer || LookupBuffer(device, buffer))
969 return AL_TRUE;
971 return AL_FALSE;
973 END_API_FUNC
976 AL_API void AL_APIENTRY alBufferData(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq)
977 START_API_FUNC
978 { alBufferStorageSOFT(buffer, format, data, size, freq, 0); }
979 END_API_FUNC
981 AL_API void AL_APIENTRY alBufferStorageSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq, ALbitfieldSOFT flags)
982 START_API_FUNC
984 ContextRef context{GetContextRef()};
985 if UNLIKELY(!context) return;
987 ALCdevice *device{context->mALDevice.get()};
988 std::lock_guard<std::mutex> _{device->BufferLock};
990 ALbuffer *albuf = LookupBuffer(device, buffer);
991 if UNLIKELY(!albuf)
992 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
993 else if UNLIKELY(size < 0)
994 context->setError(AL_INVALID_VALUE, "Negative storage size %d", size);
995 else if UNLIKELY(freq < 1)
996 context->setError(AL_INVALID_VALUE, "Invalid sample rate %d", freq);
997 else if UNLIKELY((flags&INVALID_STORAGE_MASK) != 0)
998 context->setError(AL_INVALID_VALUE, "Invalid storage flags 0x%x",
999 flags&INVALID_STORAGE_MASK);
1000 else if UNLIKELY((flags&AL_MAP_PERSISTENT_BIT_SOFT) && !(flags&MAP_READ_WRITE_FLAGS))
1001 context->setError(AL_INVALID_VALUE,
1002 "Declaring persistently mapped storage without read or write access");
1003 else
1005 auto usrfmt = DecomposeUserFormat(format);
1006 if UNLIKELY(!usrfmt)
1007 context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
1008 else
1010 #ifdef ALSOFT_EAX
1011 if (eax_g_is_enabled)
1013 const auto is_buffer_valid = eax_x_ram_validate_buffer(*device, *albuf);
1015 if (!is_buffer_valid)
1017 context->setError(AL_OUT_OF_MEMORY, "Out of X-RAM memory.");
1018 return;
1021 #endif // ALSOFT_EAX
1022 LoadData(context.get(), albuf, freq, static_cast<ALuint>(size), usrfmt->channels,
1023 usrfmt->type, static_cast<const al::byte*>(data), flags);
1027 END_API_FUNC
1029 AL_API void* AL_APIENTRY alMapBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length, ALbitfieldSOFT access)
1030 START_API_FUNC
1032 ContextRef context{GetContextRef()};
1033 if UNLIKELY(!context) return nullptr;
1035 ALCdevice *device{context->mALDevice.get()};
1036 std::lock_guard<std::mutex> _{device->BufferLock};
1038 ALbuffer *albuf = LookupBuffer(device, buffer);
1039 if UNLIKELY(!albuf)
1040 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1041 else if UNLIKELY((access&INVALID_MAP_FLAGS) != 0)
1042 context->setError(AL_INVALID_VALUE, "Invalid map flags 0x%x", access&INVALID_MAP_FLAGS);
1043 else if UNLIKELY(!(access&MAP_READ_WRITE_FLAGS))
1044 context->setError(AL_INVALID_VALUE, "Mapping buffer %u without read or write access",
1045 buffer);
1046 else
1048 ALbitfieldSOFT unavailable = (albuf->Access^access) & access;
1049 if UNLIKELY(ReadRef(albuf->ref) != 0 && !(access&AL_MAP_PERSISTENT_BIT_SOFT))
1050 context->setError(AL_INVALID_OPERATION,
1051 "Mapping in-use buffer %u without persistent mapping", buffer);
1052 else if UNLIKELY(albuf->MappedAccess != 0)
1053 context->setError(AL_INVALID_OPERATION, "Mapping already-mapped buffer %u", buffer);
1054 else if UNLIKELY((unavailable&AL_MAP_READ_BIT_SOFT))
1055 context->setError(AL_INVALID_VALUE,
1056 "Mapping buffer %u for reading without read access", buffer);
1057 else if UNLIKELY((unavailable&AL_MAP_WRITE_BIT_SOFT))
1058 context->setError(AL_INVALID_VALUE,
1059 "Mapping buffer %u for writing without write access", buffer);
1060 else if UNLIKELY((unavailable&AL_MAP_PERSISTENT_BIT_SOFT))
1061 context->setError(AL_INVALID_VALUE,
1062 "Mapping buffer %u persistently without persistent access", buffer);
1063 else if UNLIKELY(offset < 0 || length <= 0
1064 || static_cast<ALuint>(offset) >= albuf->OriginalSize
1065 || static_cast<ALuint>(length) > albuf->OriginalSize - static_cast<ALuint>(offset))
1066 context->setError(AL_INVALID_VALUE, "Mapping invalid range %d+%d for buffer %u",
1067 offset, length, buffer);
1068 else
1070 void *retval{albuf->mData.data() + offset};
1071 albuf->MappedAccess = access;
1072 albuf->MappedOffset = offset;
1073 albuf->MappedSize = length;
1074 return retval;
1078 return nullptr;
1080 END_API_FUNC
1082 AL_API void AL_APIENTRY alUnmapBufferSOFT(ALuint buffer)
1083 START_API_FUNC
1085 ContextRef context{GetContextRef()};
1086 if UNLIKELY(!context) return;
1088 ALCdevice *device{context->mALDevice.get()};
1089 std::lock_guard<std::mutex> _{device->BufferLock};
1091 ALbuffer *albuf = LookupBuffer(device, buffer);
1092 if UNLIKELY(!albuf)
1093 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1094 else if UNLIKELY(albuf->MappedAccess == 0)
1095 context->setError(AL_INVALID_OPERATION, "Unmapping unmapped buffer %u", buffer);
1096 else
1098 albuf->MappedAccess = 0;
1099 albuf->MappedOffset = 0;
1100 albuf->MappedSize = 0;
1103 END_API_FUNC
1105 AL_API void AL_APIENTRY alFlushMappedBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length)
1106 START_API_FUNC
1108 ContextRef context{GetContextRef()};
1109 if UNLIKELY(!context) return;
1111 ALCdevice *device{context->mALDevice.get()};
1112 std::lock_guard<std::mutex> _{device->BufferLock};
1114 ALbuffer *albuf = LookupBuffer(device, buffer);
1115 if UNLIKELY(!albuf)
1116 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1117 else if UNLIKELY(!(albuf->MappedAccess&AL_MAP_WRITE_BIT_SOFT))
1118 context->setError(AL_INVALID_OPERATION, "Flushing buffer %u while not mapped for writing",
1119 buffer);
1120 else if UNLIKELY(offset < albuf->MappedOffset || length <= 0
1121 || offset >= albuf->MappedOffset+albuf->MappedSize
1122 || length > albuf->MappedOffset+albuf->MappedSize-offset)
1123 context->setError(AL_INVALID_VALUE, "Flushing invalid range %d+%d on buffer %u", offset,
1124 length, buffer);
1125 else
1127 /* FIXME: Need to use some method of double-buffering for the mixer and
1128 * app to hold separate memory, which can be safely transfered
1129 * asynchronously. Currently we just say the app shouldn't write where
1130 * OpenAL's reading, and hope for the best...
1132 std::atomic_thread_fence(std::memory_order_seq_cst);
1135 END_API_FUNC
1137 AL_API void AL_APIENTRY alBufferSubDataSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei offset, ALsizei length)
1138 START_API_FUNC
1140 ContextRef context{GetContextRef()};
1141 if UNLIKELY(!context) return;
1143 ALCdevice *device{context->mALDevice.get()};
1144 std::lock_guard<std::mutex> _{device->BufferLock};
1146 ALbuffer *albuf = LookupBuffer(device, buffer);
1147 if UNLIKELY(!albuf)
1149 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1150 return;
1153 auto usrfmt = DecomposeUserFormat(format);
1154 if UNLIKELY(!usrfmt)
1156 context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
1157 return;
1160 ALuint unpack_align{albuf->UnpackAlign};
1161 ALuint align{SanitizeAlignment(usrfmt->type, unpack_align)};
1162 if UNLIKELY(align < 1)
1163 context->setError(AL_INVALID_VALUE, "Invalid unpack alignment %u", unpack_align);
1164 else if UNLIKELY(long{usrfmt->channels} != long{albuf->mChannels}
1165 || usrfmt->type != albuf->OriginalType)
1166 context->setError(AL_INVALID_ENUM, "Unpacking data with mismatched format");
1167 else if UNLIKELY(align != albuf->OriginalAlign)
1168 context->setError(AL_INVALID_VALUE,
1169 "Unpacking data with alignment %u does not match original alignment %u", align,
1170 albuf->OriginalAlign);
1171 else if UNLIKELY(albuf->isBFormat() && albuf->UnpackAmbiOrder != albuf->mAmbiOrder)
1172 context->setError(AL_INVALID_VALUE, "Unpacking data with mismatched ambisonic order");
1173 else if UNLIKELY(albuf->MappedAccess != 0)
1174 context->setError(AL_INVALID_OPERATION, "Unpacking data into mapped buffer %u", buffer);
1175 else
1177 ALuint num_chans{albuf->channelsFromFmt()};
1178 ALuint frame_size{num_chans * albuf->bytesFromFmt()};
1179 ALuint byte_align{
1180 (albuf->OriginalType == UserFmtIMA4) ? ((align-1)/2 + 4) * num_chans :
1181 (albuf->OriginalType == UserFmtMSADPCM) ? ((align-2)/2 + 7) * num_chans :
1182 (align * frame_size)
1185 if UNLIKELY(offset < 0 || length < 0 || static_cast<ALuint>(offset) > albuf->OriginalSize
1186 || static_cast<ALuint>(length) > albuf->OriginalSize-static_cast<ALuint>(offset))
1187 context->setError(AL_INVALID_VALUE, "Invalid data sub-range %d+%d on buffer %u",
1188 offset, length, buffer);
1189 else if UNLIKELY((static_cast<ALuint>(offset)%byte_align) != 0)
1190 context->setError(AL_INVALID_VALUE,
1191 "Sub-range offset %d is not a multiple of frame size %d (%d unpack alignment)",
1192 offset, byte_align, align);
1193 else if UNLIKELY((static_cast<ALuint>(length)%byte_align) != 0)
1194 context->setError(AL_INVALID_VALUE,
1195 "Sub-range length %d is not a multiple of frame size %d (%d unpack alignment)",
1196 length, byte_align, align);
1197 else
1199 /* offset -> byte offset, length -> sample count */
1200 size_t byteoff{static_cast<ALuint>(offset)/byte_align * align * frame_size};
1201 size_t samplen{static_cast<ALuint>(length)/byte_align * align};
1203 void *dst = albuf->mData.data() + byteoff;
1204 if(usrfmt->type == UserFmtIMA4 && albuf->mType == FmtShort)
1205 Convert_int16_ima4(static_cast<int16_t*>(dst), static_cast<const al::byte*>(data),
1206 num_chans, samplen, align);
1207 else if(usrfmt->type == UserFmtMSADPCM && albuf->mType == FmtShort)
1208 Convert_int16_msadpcm(static_cast<int16_t*>(dst),
1209 static_cast<const al::byte*>(data), num_chans, samplen, align);
1210 else
1212 assert(long{usrfmt->type} == long{albuf->mType});
1213 memcpy(dst, data, size_t{samplen} * frame_size);
1218 END_API_FUNC
1221 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint /*buffer*/, ALuint /*samplerate*/,
1222 ALenum /*internalformat*/, ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/,
1223 const ALvoid* /*data*/)
1224 START_API_FUNC
1226 ContextRef context{GetContextRef()};
1227 if UNLIKELY(!context) return;
1229 context->setError(AL_INVALID_OPERATION, "alBufferSamplesSOFT not supported");
1231 END_API_FUNC
1233 AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
1234 ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, const ALvoid* /*data*/)
1235 START_API_FUNC
1237 ContextRef context{GetContextRef()};
1238 if UNLIKELY(!context) return;
1240 context->setError(AL_INVALID_OPERATION, "alBufferSubSamplesSOFT not supported");
1242 END_API_FUNC
1244 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
1245 ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, ALvoid* /*data*/)
1246 START_API_FUNC
1248 ContextRef context{GetContextRef()};
1249 if UNLIKELY(!context) return;
1251 context->setError(AL_INVALID_OPERATION, "alGetBufferSamplesSOFT not supported");
1253 END_API_FUNC
1255 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum /*format*/)
1256 START_API_FUNC
1258 ContextRef context{GetContextRef()};
1259 if UNLIKELY(!context) return AL_FALSE;
1261 context->setError(AL_INVALID_OPERATION, "alIsBufferFormatSupportedSOFT not supported");
1262 return AL_FALSE;
1264 END_API_FUNC
1267 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum param, ALfloat /*value*/)
1268 START_API_FUNC
1270 ContextRef context{GetContextRef()};
1271 if UNLIKELY(!context) return;
1273 ALCdevice *device{context->mALDevice.get()};
1274 std::lock_guard<std::mutex> _{device->BufferLock};
1276 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1277 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1278 else switch(param)
1280 default:
1281 context->setError(AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
1284 END_API_FUNC
1286 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum param,
1287 ALfloat /*value1*/, ALfloat /*value2*/, ALfloat /*value3*/)
1288 START_API_FUNC
1290 ContextRef context{GetContextRef()};
1291 if UNLIKELY(!context) return;
1293 ALCdevice *device{context->mALDevice.get()};
1294 std::lock_guard<std::mutex> _{device->BufferLock};
1296 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1297 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1298 else switch(param)
1300 default:
1301 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
1304 END_API_FUNC
1306 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum param, const ALfloat *values)
1307 START_API_FUNC
1309 ContextRef context{GetContextRef()};
1310 if UNLIKELY(!context) return;
1312 ALCdevice *device{context->mALDevice.get()};
1313 std::lock_guard<std::mutex> _{device->BufferLock};
1315 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1316 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1317 else if UNLIKELY(!values)
1318 context->setError(AL_INVALID_VALUE, "NULL pointer");
1319 else switch(param)
1321 default:
1322 context->setError(AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
1325 END_API_FUNC
1328 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum param, ALint value)
1329 START_API_FUNC
1331 ContextRef context{GetContextRef()};
1332 if UNLIKELY(!context) return;
1334 ALCdevice *device{context->mALDevice.get()};
1335 std::lock_guard<std::mutex> _{device->BufferLock};
1337 ALbuffer *albuf = LookupBuffer(device, buffer);
1338 if UNLIKELY(!albuf)
1339 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1340 else switch(param)
1342 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1343 if UNLIKELY(value < 0)
1344 context->setError(AL_INVALID_VALUE, "Invalid unpack block alignment %d", value);
1345 else
1346 albuf->UnpackAlign = static_cast<ALuint>(value);
1347 break;
1349 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1350 if UNLIKELY(value < 0)
1351 context->setError(AL_INVALID_VALUE, "Invalid pack block alignment %d", value);
1352 else
1353 albuf->PackAlign = static_cast<ALuint>(value);
1354 break;
1356 case AL_AMBISONIC_LAYOUT_SOFT:
1357 if UNLIKELY(ReadRef(albuf->ref) != 0)
1358 context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's ambisonic layout",
1359 buffer);
1360 else if UNLIKELY(value != AL_FUMA_SOFT && value != AL_ACN_SOFT)
1361 context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic layout %04x", value);
1362 else
1363 albuf->mAmbiLayout = AmbiLayoutFromEnum(value).value();
1364 break;
1366 case AL_AMBISONIC_SCALING_SOFT:
1367 if UNLIKELY(ReadRef(albuf->ref) != 0)
1368 context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's ambisonic scaling",
1369 buffer);
1370 else if UNLIKELY(value != AL_FUMA_SOFT && value != AL_SN3D_SOFT && value != AL_N3D_SOFT)
1371 context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic scaling %04x", value);
1372 else
1373 albuf->mAmbiScaling = AmbiScalingFromEnum(value).value();
1374 break;
1376 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1377 if UNLIKELY(value < 1 || value > 14)
1378 context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic order %d", value);
1379 else
1380 albuf->UnpackAmbiOrder = static_cast<ALuint>(value);
1381 break;
1383 default:
1384 context->setError(AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
1387 END_API_FUNC
1389 AL_API void AL_APIENTRY alBuffer3i(ALuint buffer, ALenum param,
1390 ALint /*value1*/, ALint /*value2*/, ALint /*value3*/)
1391 START_API_FUNC
1393 ContextRef context{GetContextRef()};
1394 if UNLIKELY(!context) return;
1396 ALCdevice *device{context->mALDevice.get()};
1397 std::lock_guard<std::mutex> _{device->BufferLock};
1399 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1400 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1401 else switch(param)
1403 default:
1404 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
1407 END_API_FUNC
1409 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum param, const ALint *values)
1410 START_API_FUNC
1412 if(values)
1414 switch(param)
1416 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1417 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1418 case AL_AMBISONIC_LAYOUT_SOFT:
1419 case AL_AMBISONIC_SCALING_SOFT:
1420 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1421 alBufferi(buffer, param, values[0]);
1422 return;
1426 ContextRef context{GetContextRef()};
1427 if UNLIKELY(!context) return;
1429 ALCdevice *device{context->mALDevice.get()};
1430 std::lock_guard<std::mutex> _{device->BufferLock};
1432 ALbuffer *albuf = LookupBuffer(device, buffer);
1433 if UNLIKELY(!albuf)
1434 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1435 else if UNLIKELY(!values)
1436 context->setError(AL_INVALID_VALUE, "NULL pointer");
1437 else switch(param)
1439 case AL_LOOP_POINTS_SOFT:
1440 if UNLIKELY(ReadRef(albuf->ref) != 0)
1441 context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's loop points",
1442 buffer);
1443 else if UNLIKELY(values[0] < 0 || values[0] >= values[1]
1444 || static_cast<ALuint>(values[1]) > albuf->mSampleLen)
1445 context->setError(AL_INVALID_VALUE, "Invalid loop point range %d -> %d on buffer %u",
1446 values[0], values[1], buffer);
1447 else
1449 albuf->mLoopStart = static_cast<ALuint>(values[0]);
1450 albuf->mLoopEnd = static_cast<ALuint>(values[1]);
1452 break;
1454 default:
1455 context->setError(AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x", param);
1458 END_API_FUNC
1461 AL_API void AL_APIENTRY alGetBufferf(ALuint buffer, ALenum param, ALfloat *value)
1462 START_API_FUNC
1464 ContextRef context{GetContextRef()};
1465 if UNLIKELY(!context) return;
1467 ALCdevice *device{context->mALDevice.get()};
1468 std::lock_guard<std::mutex> _{device->BufferLock};
1470 ALbuffer *albuf = LookupBuffer(device, buffer);
1471 if UNLIKELY(!albuf)
1472 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1473 else if UNLIKELY(!value)
1474 context->setError(AL_INVALID_VALUE, "NULL pointer");
1475 else switch(param)
1477 default:
1478 context->setError(AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
1481 END_API_FUNC
1483 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3)
1484 START_API_FUNC
1486 ContextRef context{GetContextRef()};
1487 if UNLIKELY(!context) return;
1489 ALCdevice *device{context->mALDevice.get()};
1490 std::lock_guard<std::mutex> _{device->BufferLock};
1492 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1493 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1494 else if UNLIKELY(!value1 || !value2 || !value3)
1495 context->setError(AL_INVALID_VALUE, "NULL pointer");
1496 else switch(param)
1498 default:
1499 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
1502 END_API_FUNC
1504 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum param, ALfloat *values)
1505 START_API_FUNC
1507 switch(param)
1509 case AL_SEC_LENGTH_SOFT:
1510 alGetBufferf(buffer, param, values);
1511 return;
1514 ContextRef context{GetContextRef()};
1515 if UNLIKELY(!context) return;
1517 ALCdevice *device{context->mALDevice.get()};
1518 std::lock_guard<std::mutex> _{device->BufferLock};
1520 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1521 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1522 else if UNLIKELY(!values)
1523 context->setError(AL_INVALID_VALUE, "NULL pointer");
1524 else switch(param)
1526 default:
1527 context->setError(AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
1530 END_API_FUNC
1533 AL_API void AL_APIENTRY alGetBufferi(ALuint buffer, ALenum param, ALint *value)
1534 START_API_FUNC
1536 ContextRef context{GetContextRef()};
1537 if UNLIKELY(!context) return;
1539 ALCdevice *device{context->mALDevice.get()};
1540 std::lock_guard<std::mutex> _{device->BufferLock};
1541 ALbuffer *albuf = LookupBuffer(device, buffer);
1542 if UNLIKELY(!albuf)
1543 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1544 else if UNLIKELY(!value)
1545 context->setError(AL_INVALID_VALUE, "NULL pointer");
1546 else switch(param)
1548 case AL_FREQUENCY:
1549 *value = static_cast<ALint>(albuf->mSampleRate);
1550 break;
1552 case AL_BITS:
1553 *value = static_cast<ALint>(albuf->bytesFromFmt() * 8);
1554 break;
1556 case AL_CHANNELS:
1557 *value = static_cast<ALint>(albuf->channelsFromFmt());
1558 break;
1560 case AL_SIZE:
1561 *value = static_cast<ALint>(albuf->mSampleLen * albuf->frameSizeFromFmt());
1562 break;
1564 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1565 *value = static_cast<ALint>(albuf->UnpackAlign);
1566 break;
1568 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1569 *value = static_cast<ALint>(albuf->PackAlign);
1570 break;
1572 case AL_AMBISONIC_LAYOUT_SOFT:
1573 *value = EnumFromAmbiLayout(albuf->mAmbiLayout);
1574 break;
1576 case AL_AMBISONIC_SCALING_SOFT:
1577 *value = EnumFromAmbiScaling(albuf->mAmbiScaling);
1578 break;
1580 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1581 *value = static_cast<int>(albuf->UnpackAmbiOrder);
1582 break;
1584 default:
1585 context->setError(AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
1588 END_API_FUNC
1590 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum param, ALint *value1, ALint *value2, ALint *value3)
1591 START_API_FUNC
1593 ContextRef context{GetContextRef()};
1594 if UNLIKELY(!context) return;
1596 ALCdevice *device{context->mALDevice.get()};
1597 std::lock_guard<std::mutex> _{device->BufferLock};
1598 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1599 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1600 else if UNLIKELY(!value1 || !value2 || !value3)
1601 context->setError(AL_INVALID_VALUE, "NULL pointer");
1602 else switch(param)
1604 default:
1605 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
1608 END_API_FUNC
1610 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum param, ALint *values)
1611 START_API_FUNC
1613 switch(param)
1615 case AL_FREQUENCY:
1616 case AL_BITS:
1617 case AL_CHANNELS:
1618 case AL_SIZE:
1619 case AL_INTERNAL_FORMAT_SOFT:
1620 case AL_BYTE_LENGTH_SOFT:
1621 case AL_SAMPLE_LENGTH_SOFT:
1622 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1623 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1624 case AL_AMBISONIC_LAYOUT_SOFT:
1625 case AL_AMBISONIC_SCALING_SOFT:
1626 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1627 alGetBufferi(buffer, param, values);
1628 return;
1631 ContextRef context{GetContextRef()};
1632 if UNLIKELY(!context) return;
1634 ALCdevice *device{context->mALDevice.get()};
1635 std::lock_guard<std::mutex> _{device->BufferLock};
1636 ALbuffer *albuf = LookupBuffer(device, buffer);
1637 if UNLIKELY(!albuf)
1638 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1639 else if UNLIKELY(!values)
1640 context->setError(AL_INVALID_VALUE, "NULL pointer");
1641 else switch(param)
1643 case AL_LOOP_POINTS_SOFT:
1644 values[0] = static_cast<ALint>(albuf->mLoopStart);
1645 values[1] = static_cast<ALint>(albuf->mLoopEnd);
1646 break;
1648 default:
1649 context->setError(AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x", param);
1652 END_API_FUNC
1655 AL_API void AL_APIENTRY alBufferCallbackSOFT(ALuint buffer, ALenum format, ALsizei freq,
1656 ALBUFFERCALLBACKTYPESOFT callback, ALvoid *userptr, ALbitfieldSOFT flags)
1657 START_API_FUNC
1659 ContextRef context{GetContextRef()};
1660 if UNLIKELY(!context) return;
1662 ALCdevice *device{context->mALDevice.get()};
1663 std::lock_guard<std::mutex> _{device->BufferLock};
1665 ALbuffer *albuf = LookupBuffer(device, buffer);
1666 if UNLIKELY(!albuf)
1667 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1668 else if UNLIKELY(freq < 1)
1669 context->setError(AL_INVALID_VALUE, "Invalid sample rate %d", freq);
1670 else if UNLIKELY(callback == nullptr)
1671 context->setError(AL_INVALID_VALUE, "NULL callback");
1672 else if UNLIKELY(flags != 0)
1673 context->setError(AL_INVALID_VALUE, "Invalid callback flags 0x%x", flags);
1674 else
1676 auto usrfmt = DecomposeUserFormat(format);
1677 if UNLIKELY(!usrfmt)
1678 context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
1679 else
1680 PrepareCallback(context.get(), albuf, freq, usrfmt->channels, usrfmt->type, callback,
1681 userptr);
1684 END_API_FUNC
1686 AL_API void AL_APIENTRY alGetBufferPtrSOFT(ALuint buffer, ALenum param, ALvoid **value)
1687 START_API_FUNC
1689 ContextRef context{GetContextRef()};
1690 if UNLIKELY(!context) return;
1692 ALCdevice *device{context->mALDevice.get()};
1693 std::lock_guard<std::mutex> _{device->BufferLock};
1694 ALbuffer *albuf = LookupBuffer(device, buffer);
1695 if UNLIKELY(!albuf)
1696 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1697 else if UNLIKELY(!value)
1698 context->setError(AL_INVALID_VALUE, "NULL pointer");
1699 else switch(param)
1701 case AL_BUFFER_CALLBACK_FUNCTION_SOFT:
1702 *value = reinterpret_cast<void*>(albuf->mCallback);
1703 break;
1704 case AL_BUFFER_CALLBACK_USER_PARAM_SOFT:
1705 *value = albuf->mUserData;
1706 break;
1708 default:
1709 context->setError(AL_INVALID_ENUM, "Invalid buffer pointer property 0x%04x", param);
1712 END_API_FUNC
1714 AL_API void AL_APIENTRY alGetBuffer3PtrSOFT(ALuint buffer, ALenum param, ALvoid **value1, ALvoid **value2, ALvoid **value3)
1715 START_API_FUNC
1717 ContextRef context{GetContextRef()};
1718 if UNLIKELY(!context) return;
1720 ALCdevice *device{context->mALDevice.get()};
1721 std::lock_guard<std::mutex> _{device->BufferLock};
1722 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1723 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1724 else if UNLIKELY(!value1 || !value2 || !value3)
1725 context->setError(AL_INVALID_VALUE, "NULL pointer");
1726 else switch(param)
1728 default:
1729 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-pointer property 0x%04x", param);
1732 END_API_FUNC
1734 AL_API void AL_APIENTRY alGetBufferPtrvSOFT(ALuint buffer, ALenum param, ALvoid **values)
1735 START_API_FUNC
1737 switch(param)
1739 case AL_BUFFER_CALLBACK_FUNCTION_SOFT:
1740 case AL_BUFFER_CALLBACK_USER_PARAM_SOFT:
1741 alGetBufferPtrSOFT(buffer, param, values);
1742 return;
1745 ContextRef context{GetContextRef()};
1746 if UNLIKELY(!context) return;
1748 ALCdevice *device{context->mALDevice.get()};
1749 std::lock_guard<std::mutex> _{device->BufferLock};
1750 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1751 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1752 else if UNLIKELY(!values)
1753 context->setError(AL_INVALID_VALUE, "NULL pointer");
1754 else switch(param)
1756 default:
1757 context->setError(AL_INVALID_ENUM, "Invalid buffer pointer-vector property 0x%04x", param);
1760 END_API_FUNC
1763 BufferSubList::~BufferSubList()
1765 uint64_t usemask{~FreeMask};
1766 while(usemask)
1768 const int idx{al::countr_zero(usemask)};
1769 al::destroy_at(Buffers+idx);
1770 usemask &= ~(1_u64 << idx);
1772 FreeMask = ~usemask;
1773 al_free(Buffers);
1774 Buffers = nullptr;
1778 #ifdef ALSOFT_EAX
1779 ALboolean AL_APIENTRY EAXSetBufferMode(
1780 ALsizei n,
1781 const ALuint* buffers,
1782 ALint value)
1783 START_API_FUNC
1785 #define EAX_PREFIX "[EAXSetBufferMode] "
1787 if (n == 0)
1789 return ALC_TRUE;
1792 if (n < 0)
1794 ERR(EAX_PREFIX "Buffer count %d out of range.\n", n);
1795 return ALC_FALSE;
1798 if (!buffers)
1800 ERR(EAX_PREFIX "%s\n", "Null AL buffers.");
1801 return ALC_FALSE;
1804 switch (value)
1806 case AL_STORAGE_AUTOMATIC:
1807 case AL_STORAGE_HARDWARE:
1808 case AL_STORAGE_ACCESSIBLE:
1809 break;
1811 default:
1812 ERR(EAX_PREFIX "Unsupported X-RAM mode %d.\n", value);
1813 return ALC_FALSE;
1816 const auto context = ContextRef{GetContextRef()};
1818 if (!context)
1820 ERR(EAX_PREFIX "%s\n", "No current context.");
1821 return ALC_FALSE;
1824 if (!eax_g_is_enabled)
1826 ERR(EAX_PREFIX "%s\n", "EAX not enabled.");
1827 return ALC_FALSE;
1830 auto device = context->mALDevice.get();
1831 std::lock_guard<std::mutex> device_lock{device->BufferLock};
1833 // Validate the buffers.
1835 for (auto i = 0; i < n; ++i)
1837 const auto buffer = buffers[i];
1839 if (buffer == AL_NONE)
1841 continue;
1844 const auto al_buffer = LookupBuffer(device, buffer);
1846 if (!al_buffer)
1848 ERR(EAX_PREFIX "Invalid buffer ID %u.\n", buffer);
1849 return ALC_FALSE;
1852 if (al_buffer->eax_x_ram_is_dirty)
1854 ERR(EAX_PREFIX "Buffer %u has audio data.\n", buffer);
1855 return ALC_FALSE;
1859 // Update the mode.
1861 for (auto i = 0; i < n; ++i)
1863 const auto buffer = buffers[i];
1865 if (buffer == AL_NONE)
1867 continue;
1870 const auto al_buffer = LookupBuffer(device, buffer);
1871 assert(al_buffer);
1872 assert(!al_buffer->eax_x_ram_is_dirty);
1874 al_buffer->eax_x_ram_mode = value;
1877 return AL_TRUE;
1879 #undef EAX_PREFIX
1881 END_API_FUNC
1883 ALenum AL_APIENTRY EAXGetBufferMode(
1884 ALuint buffer,
1885 ALint* pReserved)
1886 START_API_FUNC
1888 #define EAX_PREFIX "[EAXGetBufferMode] "
1890 if (buffer == AL_NONE)
1892 ERR(EAX_PREFIX "%s\n", "Null AL buffer.");
1893 return AL_NONE;
1896 if (pReserved)
1898 ERR(EAX_PREFIX "%s\n", "Non-null reserved parameter.");
1899 return AL_NONE;
1902 const auto context = ContextRef{GetContextRef()};
1904 if (!context)
1906 ERR(EAX_PREFIX "%s\n", "No current context.");
1907 return AL_NONE;
1910 if (!eax_g_is_enabled)
1912 ERR(EAX_PREFIX "%s\n", "EAX not enabled.");
1913 return AL_NONE;
1916 auto device = context->mALDevice.get();
1917 std::lock_guard<std::mutex> device_lock{device->BufferLock};
1919 const auto al_buffer = LookupBuffer(device, buffer);
1921 if (!al_buffer)
1923 ERR(EAX_PREFIX "Invalid buffer ID %u.\n", buffer);
1924 return AL_NONE;
1927 return al_buffer->eax_x_ram_mode;
1929 #undef EAX_PREFIX
1931 END_API_FUNC
1933 #endif // ALSOFT_EAX