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
47 #include "alc/context.h"
48 #include "alc/device.h"
49 #include "alc/inprogext.h"
51 #include "alnumeric.h"
52 #include "aloptional.h"
54 #include "core/except.h"
55 #include "core/logging.h"
56 #include "core/voice.h"
57 #include "opthelpers.h"
60 #include "eax/globals.h"
61 #include "eax/x_ram.h"
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,
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] = {
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;
124 index
[c
] = src
[0] | (src
[1]<<8);
125 index
[c
] = clampi((index
[c
]^0x8000) - 32768, 0, 88);
128 *(dst
++) = static_cast<int16_t>(sample
[c
]);
131 for(size_t i
{1};i
< align
;i
++)
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);
143 for(size_t c
{0};c
< numchans
;c
++)
145 const ALuint nibble
{code
[c
]&0xf};
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);
170 for(size_t c
{0};c
< numchans
;c
++)
172 delta
[c
] = src
[0] | (src
[1]<<8);
173 delta
[c
] = (delta
[c
]^0x8000) - 32768;
176 for(size_t c
{0};c
< numchans
;c
++)
178 samples
[c
][0] = static_cast<ALshort
>(src
[0] | (src
[1]<<8));
181 for(size_t c
{0};c
< numchans
;c
++)
183 samples
[c
][1] = static_cast<ALshort
>(src
[0] | (src
[1]<<8));
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];
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). */
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
,
224 assert(numchans
<= MaxAdpcmChannels
);
225 const size_t byte_align
{((align
-1)/2 + 4) * numchans
};
230 DecodeIMA4Block(dst
, src
, numchans
, align
);
232 dst
+= align
*numchans
;
236 void Convert_int16_msadpcm(int16_t *dst
, const al::byte
*src
, size_t numchans
, size_t len
,
239 assert(numchans
<= MaxAdpcmChannels
);
240 const size_t byte_align
{((align
-2)/2 + 7) * numchans
};
245 DecodeMSADPCMBlock(dst
, src
, numchans
, align
);
247 dst
+= align
*numchans
;
252 ALuint
BytesFromUserFmt(UserFmtType type
) noexcept
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 */
267 ALuint
ChannelsFromUserFmt(UserFmtChannels chans
, ALuint ambiorder
) noexcept
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;
287 al::optional
<AmbiLayout
> AmbiLayoutFromEnum(ALenum layout
)
291 case AL_FUMA_SOFT
: return AmbiLayout::FuMa
;
292 case AL_ACN_SOFT
: return AmbiLayout::ACN
;
296 ALenum
EnumFromAmbiLayout(AmbiLayout 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
)
310 case AL_FUMA_SOFT
: return AmbiScaling::FuMa
;
311 case AL_SN3D_SOFT
: return AmbiScaling::SN3D
;
312 case AL_N3D_SOFT
: return AmbiScaling::N3D
;
316 ALenum
EnumFromAmbiScaling(AmbiScaling 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
)
332 case UserFmtMono
: return FmtMono
;
333 case UserFmtStereo
: return FmtStereo
;
334 case UserFmtRear
: return FmtRear
;
335 case UserFmtQuad
: return FmtQuad
;
336 case UserFmtX51
: return FmtX51
;
337 case UserFmtX61
: return FmtX61
;
338 case UserFmtX71
: return FmtX71
;
339 case UserFmtBFormat2D
: return FmtBFormat2D
;
340 case UserFmtBFormat3D
: return FmtBFormat3D
;
341 case UserFmtUHJ2
: return FmtUHJ2
;
342 case UserFmtUHJ3
: return FmtUHJ3
;
343 case UserFmtUHJ4
: return FmtUHJ4
;
347 al::optional
<FmtType
> FmtFromUserFmt(UserFmtType type
)
351 case UserFmtUByte
: return FmtUByte
;
352 case UserFmtShort
: return FmtShort
;
353 case UserFmtFloat
: return FmtFloat
;
354 case UserFmtDouble
: return FmtDouble
;
355 case UserFmtMulaw
: return FmtMulaw
;
356 case UserFmtAlaw
: return FmtAlaw
;
357 /* ADPCM not handled here. */
358 case UserFmtIMA4
: break;
359 case UserFmtMSADPCM
: break;
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
)
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;
399 constexpr ALbitfieldSOFT INVALID_STORAGE_MASK
{~unsigned(AL_MAP_READ_BIT_SOFT
|
400 AL_MAP_WRITE_BIT_SOFT
| AL_MAP_PERSISTENT_BIT_SOFT
| AL_PRESERVE_DATA_BIT_SOFT
)};
401 constexpr ALbitfieldSOFT MAP_READ_WRITE_FLAGS
{AL_MAP_READ_BIT_SOFT
| AL_MAP_WRITE_BIT_SOFT
};
402 constexpr ALbitfieldSOFT INVALID_MAP_FLAGS
{~unsigned(AL_MAP_READ_BIT_SOFT
| AL_MAP_WRITE_BIT_SOFT
|
403 AL_MAP_PERSISTENT_BIT_SOFT
)};
406 bool EnsureBuffers(ALCdevice
*device
, size_t needed
)
408 size_t count
{std::accumulate(device
->BufferList
.cbegin(), device
->BufferList
.cend(), size_t{0},
409 [](size_t cur
, const BufferSubList
&sublist
) noexcept
-> size_t
410 { return cur
+ static_cast<ALuint
>(al::popcount(sublist
.FreeMask
)); })};
412 while(needed
> count
)
414 if(device
->BufferList
.size() >= 1<<25) [[unlikely
]]
417 device
->BufferList
.emplace_back();
418 auto sublist
= device
->BufferList
.end() - 1;
419 sublist
->FreeMask
= ~0_u64
;
420 sublist
->Buffers
= static_cast<ALbuffer
*>(al_calloc(alignof(ALbuffer
), sizeof(ALbuffer
)*64));
421 if(!sublist
->Buffers
) [[unlikely
]]
423 device
->BufferList
.pop_back();
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
));
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
);
450 void FreeBuffer(ALCdevice
*device
, ALbuffer
*buffer
)
453 eax_x_ram_clear(*device
, *buffer
);
456 const ALuint id
{buffer
->id
- 1};
457 const size_t lidx
{id
>> 6};
458 const ALuint slidx
{id
& 0x3f};
460 al::destroy_at(buffer
);
462 device
->BufferList
[lidx
].FreeMask
|= 1_u64
<< slidx
;
465 inline ALbuffer
*LookupBuffer(ALCdevice
*device
, ALuint id
)
467 const size_t lidx
{(id
-1) >> 6};
468 const ALuint slidx
{(id
-1) & 0x3f};
470 if(lidx
>= device
->BufferList
.size()) [[unlikely
]]
472 BufferSubList
&sublist
= device
->BufferList
[lidx
];
473 if(sublist
.FreeMask
& (1_u64
<< slidx
)) [[unlikely
]]
475 return sublist
.Buffers
+ slidx
;
479 ALuint
SanitizeAlignment(UserFmtType type
, ALuint align
)
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
491 if(type
== UserFmtMSADPCM
)
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
);
502 if(type
== UserFmtMSADPCM
)
504 /* MSADPCM block alignment must be a multiple of 2. */
505 if((align
&1) == 0) return static_cast<ALuint
>(align
);
509 return static_cast<ALuint
>(align
);
513 const ALchar
*NameFromUserFmtType(UserFmtType type
)
517 case UserFmtUByte
: return "UInt8";
518 case UserFmtShort
: return "Int16";
519 case UserFmtFloat
: return "Float32";
520 case UserFmtDouble
: return "Float64";
521 case UserFmtMulaw
: return "muLaw";
522 case UserFmtAlaw
: return "aLaw";
523 case UserFmtIMA4
: return "IMA4 ADPCM";
524 case UserFmtMSADPCM
: return "MSADPCM";
526 return "<internal type error>";
529 /** Loads the specified data into the buffer, using the specified format. */
530 void LoadData(ALCcontext
*context
, ALbuffer
*ALBuf
, ALsizei freq
, ALuint size
,
531 UserFmtChannels SrcChannels
, UserFmtType SrcType
, const al::byte
*SrcData
,
532 ALbitfieldSOFT access
)
534 if(ReadRef(ALBuf
->ref
) != 0 || ALBuf
->MappedAccess
!= 0) [[unlikely
]]
535 return context
->setError(AL_INVALID_OPERATION
, "Modifying storage for in-use buffer %u",
538 /* Currently no channel configurations need to be converted. */
539 auto DstChannels
= FmtFromUserFmt(SrcChannels
);
540 if(!DstChannels
) [[unlikely
]]
541 return context
->setError(AL_INVALID_ENUM
, "Invalid format");
543 /* IMA4 and MSADPCM convert to 16-bit short.
545 * TODO: Currently we can only map samples when they're not converted. To
546 * allow it would need some kind of double-buffering to hold onto a copy of
549 if((access
&MAP_READ_WRITE_FLAGS
))
551 if(SrcType
== UserFmtIMA4
|| SrcType
== UserFmtMSADPCM
) [[unlikely
]]
552 return context
->setError(AL_INVALID_VALUE
, "%s samples cannot be mapped",
553 NameFromUserFmtType(SrcType
));
555 auto DstType
= (SrcType
== UserFmtIMA4
|| SrcType
== UserFmtMSADPCM
)
556 ? al::make_optional(FmtShort
) : FmtFromUserFmt(SrcType
);
557 if(!DstType
) [[unlikely
]]
558 return context
->setError(AL_INVALID_ENUM
, "Invalid format");
560 const ALuint unpackalign
{ALBuf
->UnpackAlign
};
561 const ALuint align
{SanitizeAlignment(SrcType
, unpackalign
)};
562 if(align
< 1) [[unlikely
]]
563 return context
->setError(AL_INVALID_VALUE
, "Invalid unpack alignment %u for %s samples",
564 unpackalign
, NameFromUserFmtType(SrcType
));
566 const ALuint ambiorder
{IsBFormat(*DstChannels
) ? ALBuf
->UnpackAmbiOrder
:
567 (IsUHJ(*DstChannels
) ? 1 : 0)};
569 if((access
&AL_PRESERVE_DATA_BIT_SOFT
))
571 /* Can only preserve data with the same format and alignment. */
572 if(ALBuf
->mChannels
!= *DstChannels
|| ALBuf
->OriginalType
!= SrcType
) [[unlikely
]]
573 return context
->setError(AL_INVALID_VALUE
, "Preserving data of mismatched format");
574 if(ALBuf
->OriginalAlign
!= align
) [[unlikely
]]
575 return context
->setError(AL_INVALID_VALUE
, "Preserving data of mismatched alignment");
576 if(ALBuf
->mAmbiOrder
!= ambiorder
) [[unlikely
]]
577 return context
->setError(AL_INVALID_VALUE
, "Preserving data of mismatched order");
580 /* Convert the input/source size in bytes to sample frames using the unpack
583 const ALuint SrcByteAlign
{ChannelsFromUserFmt(SrcChannels
, ambiorder
) *
584 ((SrcType
== UserFmtIMA4
) ? (align
-1)/2 + 4 :
585 (SrcType
== UserFmtMSADPCM
) ? (align
-2)/2 + 7 :
586 (align
* BytesFromUserFmt(SrcType
)))};
587 if((size
%SrcByteAlign
) != 0) [[unlikely
]]
588 return context
->setError(AL_INVALID_VALUE
,
589 "Data size %d is not a multiple of frame size %d (%d unpack alignment)",
590 size
, SrcByteAlign
, align
);
592 if(size
/SrcByteAlign
> std::numeric_limits
<ALsizei
>::max()/align
) [[unlikely
]]
593 return context
->setError(AL_OUT_OF_MEMORY
,
594 "Buffer size overflow, %d blocks x %d samples per block", size
/SrcByteAlign
, align
);
595 const ALuint frames
{size
/ SrcByteAlign
* align
};
597 /* Convert the sample frames to the number of bytes needed for internal
600 ALuint NumChannels
{ChannelsFromFmt(*DstChannels
, ambiorder
)};
601 ALuint FrameSize
{NumChannels
* BytesFromFmt(*DstType
)};
602 if(frames
> std::numeric_limits
<size_t>::max()/FrameSize
) [[unlikely
]]
603 return context
->setError(AL_OUT_OF_MEMORY
,
604 "Buffer size overflow, %d frames x %d bytes per frame", frames
, FrameSize
);
605 size_t newsize
{static_cast<size_t>(frames
) * FrameSize
};
608 if(ALBuf
->eax_x_ram_mode
== AL_STORAGE_HARDWARE
)
610 ALCdevice
&device
= *context
->mALDevice
;
611 if(!eax_x_ram_check_availability(device
, *ALBuf
, size
))
612 return context
->setError(AL_OUT_OF_MEMORY
,
613 "Out of X-RAM memory (avail: %u, needed: %u)", device
.eax_x_ram_free_size
, size
);
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 eax_x_ram_clear(*context
->mALDevice
, *ALBuf
);
638 if(SrcType
== UserFmtIMA4
)
640 assert(*DstType
== FmtShort
);
641 if(SrcData
!= nullptr && !ALBuf
->mData
.empty())
642 Convert_int16_ima4(reinterpret_cast<int16_t*>(ALBuf
->mData
.data()), SrcData
,
643 NumChannels
, frames
, align
);
644 ALBuf
->OriginalAlign
= align
;
646 else if(SrcType
== UserFmtMSADPCM
)
648 assert(*DstType
== FmtShort
);
649 if(SrcData
!= nullptr && !ALBuf
->mData
.empty())
650 Convert_int16_msadpcm(reinterpret_cast<int16_t*>(ALBuf
->mData
.data()), SrcData
,
651 NumChannels
, frames
, align
);
652 ALBuf
->OriginalAlign
= align
;
656 assert(DstType
.has_value());
657 if(SrcData
!= nullptr && !ALBuf
->mData
.empty())
658 std::copy_n(SrcData
, frames
*FrameSize
, ALBuf
->mData
.begin());
659 ALBuf
->OriginalAlign
= 1;
661 ALBuf
->OriginalSize
= size
;
662 ALBuf
->OriginalType
= SrcType
;
664 ALBuf
->Access
= access
;
666 ALBuf
->mSampleRate
= static_cast<ALuint
>(freq
);
667 ALBuf
->mChannels
= *DstChannels
;
668 ALBuf
->mType
= *DstType
;
669 ALBuf
->mAmbiOrder
= ambiorder
;
671 ALBuf
->mCallback
= nullptr;
672 ALBuf
->mUserData
= nullptr;
674 ALBuf
->mSampleLen
= frames
;
675 ALBuf
->mLoopStart
= 0;
676 ALBuf
->mLoopEnd
= ALBuf
->mSampleLen
;
679 if(eax_g_is_enabled
&& ALBuf
->eax_x_ram_mode
!= AL_STORAGE_ACCESSIBLE
)
680 eax_x_ram_apply(*context
->mALDevice
, *ALBuf
);
684 /** Prepares the buffer to use the specified callback, using the specified format. */
685 void PrepareCallback(ALCcontext
*context
, ALbuffer
*ALBuf
, ALsizei freq
,
686 UserFmtChannels SrcChannels
, UserFmtType SrcType
, ALBUFFERCALLBACKTYPESOFT callback
,
689 if(ReadRef(ALBuf
->ref
) != 0 || ALBuf
->MappedAccess
!= 0) [[unlikely
]]
690 return context
->setError(AL_INVALID_OPERATION
, "Modifying callback for in-use buffer %u",
693 /* Currently no channel configurations need to be converted. */
694 auto DstChannels
= FmtFromUserFmt(SrcChannels
);
695 if(!DstChannels
) [[unlikely
]]
696 return context
->setError(AL_INVALID_ENUM
, "Invalid format");
698 /* IMA4 and MSADPCM convert to 16-bit short. Not supported with callbacks. */
699 auto DstType
= FmtFromUserFmt(SrcType
);
700 if(!DstType
) [[unlikely
]]
701 return context
->setError(AL_INVALID_ENUM
, "Unsupported callback format");
703 const ALuint ambiorder
{IsBFormat(*DstChannels
) ? ALBuf
->UnpackAmbiOrder
:
704 (IsUHJ(*DstChannels
) ? 1 : 0)};
706 static constexpr uint line_size
{BufferLineSize
+ MaxPostVoiceLoad
};
707 al::vector
<al::byte
,16>(FrameSizeFromFmt(*DstChannels
, *DstType
, ambiorder
) *
708 size_t{line_size
}).swap(ALBuf
->mData
);
711 eax_x_ram_clear(*context
->mALDevice
, *ALBuf
);
714 ALBuf
->mCallback
= callback
;
715 ALBuf
->mUserData
= userptr
;
717 ALBuf
->OriginalType
= SrcType
;
718 ALBuf
->OriginalSize
= 0;
719 ALBuf
->OriginalAlign
= 1;
722 ALBuf
->mSampleRate
= static_cast<ALuint
>(freq
);
723 ALBuf
->mChannels
= *DstChannels
;
724 ALBuf
->mType
= *DstType
;
725 ALBuf
->mAmbiOrder
= ambiorder
;
727 ALBuf
->mSampleLen
= 0;
728 ALBuf
->mLoopStart
= 0;
729 ALBuf
->mLoopEnd
= ALBuf
->mSampleLen
;
733 struct DecompResult
{ UserFmtChannels channels
; UserFmtType type
; };
734 al::optional
<DecompResult
> DecomposeUserFormat(ALenum format
)
738 UserFmtChannels channels
;
741 static const std::array
<FormatMap
,55> UserFmtList
{{
742 { AL_FORMAT_MONO8
, UserFmtMono
, UserFmtUByte
},
743 { AL_FORMAT_MONO16
, UserFmtMono
, UserFmtShort
},
744 { AL_FORMAT_MONO_FLOAT32
, UserFmtMono
, UserFmtFloat
},
745 { AL_FORMAT_MONO_DOUBLE_EXT
, UserFmtMono
, UserFmtDouble
},
746 { AL_FORMAT_MONO_IMA4
, UserFmtMono
, UserFmtIMA4
},
747 { AL_FORMAT_MONO_MSADPCM_SOFT
, UserFmtMono
, UserFmtMSADPCM
},
748 { AL_FORMAT_MONO_MULAW
, UserFmtMono
, UserFmtMulaw
},
749 { AL_FORMAT_MONO_ALAW_EXT
, UserFmtMono
, UserFmtAlaw
},
751 { AL_FORMAT_STEREO8
, UserFmtStereo
, UserFmtUByte
},
752 { AL_FORMAT_STEREO16
, UserFmtStereo
, UserFmtShort
},
753 { AL_FORMAT_STEREO_FLOAT32
, UserFmtStereo
, UserFmtFloat
},
754 { AL_FORMAT_STEREO_DOUBLE_EXT
, UserFmtStereo
, UserFmtDouble
},
755 { AL_FORMAT_STEREO_IMA4
, UserFmtStereo
, UserFmtIMA4
},
756 { AL_FORMAT_STEREO_MSADPCM_SOFT
, UserFmtStereo
, UserFmtMSADPCM
},
757 { AL_FORMAT_STEREO_MULAW
, UserFmtStereo
, UserFmtMulaw
},
758 { AL_FORMAT_STEREO_ALAW_EXT
, UserFmtStereo
, UserFmtAlaw
},
760 { AL_FORMAT_REAR8
, UserFmtRear
, UserFmtUByte
},
761 { AL_FORMAT_REAR16
, UserFmtRear
, UserFmtShort
},
762 { AL_FORMAT_REAR32
, UserFmtRear
, UserFmtFloat
},
763 { AL_FORMAT_REAR_MULAW
, UserFmtRear
, UserFmtMulaw
},
765 { AL_FORMAT_QUAD8_LOKI
, UserFmtQuad
, UserFmtUByte
},
766 { AL_FORMAT_QUAD16_LOKI
, UserFmtQuad
, UserFmtShort
},
768 { AL_FORMAT_QUAD8
, UserFmtQuad
, UserFmtUByte
},
769 { AL_FORMAT_QUAD16
, UserFmtQuad
, UserFmtShort
},
770 { AL_FORMAT_QUAD32
, UserFmtQuad
, UserFmtFloat
},
771 { AL_FORMAT_QUAD_MULAW
, UserFmtQuad
, UserFmtMulaw
},
773 { AL_FORMAT_51CHN8
, UserFmtX51
, UserFmtUByte
},
774 { AL_FORMAT_51CHN16
, UserFmtX51
, UserFmtShort
},
775 { AL_FORMAT_51CHN32
, UserFmtX51
, UserFmtFloat
},
776 { AL_FORMAT_51CHN_MULAW
, UserFmtX51
, UserFmtMulaw
},
778 { AL_FORMAT_61CHN8
, UserFmtX61
, UserFmtUByte
},
779 { AL_FORMAT_61CHN16
, UserFmtX61
, UserFmtShort
},
780 { AL_FORMAT_61CHN32
, UserFmtX61
, UserFmtFloat
},
781 { AL_FORMAT_61CHN_MULAW
, UserFmtX61
, UserFmtMulaw
},
783 { AL_FORMAT_71CHN8
, UserFmtX71
, UserFmtUByte
},
784 { AL_FORMAT_71CHN16
, UserFmtX71
, UserFmtShort
},
785 { AL_FORMAT_71CHN32
, UserFmtX71
, UserFmtFloat
},
786 { AL_FORMAT_71CHN_MULAW
, UserFmtX71
, UserFmtMulaw
},
788 { AL_FORMAT_BFORMAT2D_8
, UserFmtBFormat2D
, UserFmtUByte
},
789 { AL_FORMAT_BFORMAT2D_16
, UserFmtBFormat2D
, UserFmtShort
},
790 { AL_FORMAT_BFORMAT2D_FLOAT32
, UserFmtBFormat2D
, UserFmtFloat
},
791 { AL_FORMAT_BFORMAT2D_MULAW
, UserFmtBFormat2D
, UserFmtMulaw
},
793 { AL_FORMAT_BFORMAT3D_8
, UserFmtBFormat3D
, UserFmtUByte
},
794 { AL_FORMAT_BFORMAT3D_16
, UserFmtBFormat3D
, UserFmtShort
},
795 { AL_FORMAT_BFORMAT3D_FLOAT32
, UserFmtBFormat3D
, UserFmtFloat
},
796 { AL_FORMAT_BFORMAT3D_MULAW
, UserFmtBFormat3D
, UserFmtMulaw
},
798 { AL_FORMAT_UHJ2CHN8_SOFT
, UserFmtUHJ2
, UserFmtUByte
},
799 { AL_FORMAT_UHJ2CHN16_SOFT
, UserFmtUHJ2
, UserFmtShort
},
800 { AL_FORMAT_UHJ2CHN_FLOAT32_SOFT
, UserFmtUHJ2
, UserFmtFloat
},
802 { AL_FORMAT_UHJ3CHN8_SOFT
, UserFmtUHJ3
, UserFmtUByte
},
803 { AL_FORMAT_UHJ3CHN16_SOFT
, UserFmtUHJ3
, UserFmtShort
},
804 { AL_FORMAT_UHJ3CHN_FLOAT32_SOFT
, UserFmtUHJ3
, UserFmtFloat
},
806 { AL_FORMAT_UHJ4CHN8_SOFT
, UserFmtUHJ4
, UserFmtUByte
},
807 { AL_FORMAT_UHJ4CHN16_SOFT
, UserFmtUHJ4
, UserFmtShort
},
808 { AL_FORMAT_UHJ4CHN_FLOAT32_SOFT
, UserFmtUHJ4
, UserFmtFloat
},
811 for(const auto &fmt
: UserFmtList
)
813 if(fmt
.format
== format
)
814 return al::make_optional
<DecompResult
>({fmt
.channels
, fmt
.type
});
822 AL_API
void AL_APIENTRY
alGenBuffers(ALsizei n
, ALuint
*buffers
)
825 ContextRef context
{GetContextRef()};
826 if(!context
) [[unlikely
]] return;
828 if(n
< 0) [[unlikely
]]
829 context
->setError(AL_INVALID_VALUE
, "Generating %d buffers", n
);
830 if(n
<= 0) [[unlikely
]] return;
832 ALCdevice
*device
{context
->mALDevice
.get()};
833 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
834 if(!EnsureBuffers(device
, static_cast<ALuint
>(n
)))
836 context
->setError(AL_OUT_OF_MEMORY
, "Failed to allocate %d buffer%s", n
, (n
==1)?"":"s");
840 if(n
== 1) [[likely
]]
842 /* Special handling for the easy and normal case. */
843 ALbuffer
*buffer
{AllocBuffer(device
)};
844 buffers
[0] = buffer
->id
;
848 /* Store the allocated buffer IDs in a separate local list, to avoid
849 * modifying the user storage in case of failure.
851 al::vector
<ALuint
> ids
;
852 ids
.reserve(static_cast<ALuint
>(n
));
854 ALbuffer
*buffer
{AllocBuffer(device
)};
855 ids
.emplace_back(buffer
->id
);
857 std::copy(ids
.begin(), ids
.end(), buffers
);
862 AL_API
void AL_APIENTRY
alDeleteBuffers(ALsizei n
, const ALuint
*buffers
)
865 ContextRef context
{GetContextRef()};
866 if(!context
) [[unlikely
]] return;
868 if(n
< 0) [[unlikely
]]
869 context
->setError(AL_INVALID_VALUE
, "Deleting %d buffers", n
);
870 if(n
<= 0) [[unlikely
]] return;
872 ALCdevice
*device
{context
->mALDevice
.get()};
873 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
875 /* First try to find any buffers that are invalid or in-use. */
876 auto validate_buffer
= [device
, &context
](const ALuint bid
) -> bool
878 if(!bid
) return true;
879 ALbuffer
*ALBuf
{LookupBuffer(device
, bid
)};
880 if(!ALBuf
) [[unlikely
]]
882 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", bid
);
885 if(ReadRef(ALBuf
->ref
) != 0) [[unlikely
]]
887 context
->setError(AL_INVALID_OPERATION
, "Deleting in-use buffer %u", bid
);
892 const ALuint
*buffers_end
= buffers
+ n
;
893 auto invbuf
= std::find_if_not(buffers
, buffers_end
, validate_buffer
);
894 if(invbuf
!= buffers_end
) [[unlikely
]] return;
896 /* All good. Delete non-0 buffer IDs. */
897 auto delete_buffer
= [device
](const ALuint bid
) -> void
899 ALbuffer
*buffer
{bid
? LookupBuffer(device
, bid
) : nullptr};
900 if(buffer
) FreeBuffer(device
, buffer
);
902 std::for_each(buffers
, buffers_end
, delete_buffer
);
906 AL_API ALboolean AL_APIENTRY
alIsBuffer(ALuint buffer
)
909 ContextRef context
{GetContextRef()};
910 if(context
) [[likely
]]
912 ALCdevice
*device
{context
->mALDevice
.get()};
913 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
914 if(!buffer
|| LookupBuffer(device
, buffer
))
922 AL_API
void AL_APIENTRY
alBufferData(ALuint buffer
, ALenum format
, const ALvoid
*data
, ALsizei size
, ALsizei freq
)
924 { alBufferStorageSOFT(buffer
, format
, data
, size
, freq
, 0); }
927 AL_API
void AL_APIENTRY
alBufferStorageSOFT(ALuint buffer
, ALenum format
, const ALvoid
*data
, ALsizei size
, ALsizei freq
, ALbitfieldSOFT flags
)
930 ContextRef context
{GetContextRef()};
931 if(!context
) [[unlikely
]] return;
933 ALCdevice
*device
{context
->mALDevice
.get()};
934 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
936 ALbuffer
*albuf
= LookupBuffer(device
, buffer
);
937 if(!albuf
) [[unlikely
]]
938 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
939 else if(size
< 0) [[unlikely
]]
940 context
->setError(AL_INVALID_VALUE
, "Negative storage size %d", size
);
941 else if(freq
< 1) [[unlikely
]]
942 context
->setError(AL_INVALID_VALUE
, "Invalid sample rate %d", freq
);
943 else if((flags
&INVALID_STORAGE_MASK
) != 0) [[unlikely
]]
944 context
->setError(AL_INVALID_VALUE
, "Invalid storage flags 0x%x",
945 flags
&INVALID_STORAGE_MASK
);
946 else if((flags
&AL_MAP_PERSISTENT_BIT_SOFT
) && !(flags
&MAP_READ_WRITE_FLAGS
)) [[unlikely
]]
947 context
->setError(AL_INVALID_VALUE
,
948 "Declaring persistently mapped storage without read or write access");
951 auto usrfmt
= DecomposeUserFormat(format
);
952 if(!usrfmt
) [[unlikely
]]
953 context
->setError(AL_INVALID_ENUM
, "Invalid format 0x%04x", format
);
956 LoadData(context
.get(), albuf
, freq
, static_cast<ALuint
>(size
), usrfmt
->channels
,
957 usrfmt
->type
, static_cast<const al::byte
*>(data
), flags
);
963 AL_API
void* AL_APIENTRY
alMapBufferSOFT(ALuint buffer
, ALsizei offset
, ALsizei length
, ALbitfieldSOFT access
)
966 ContextRef context
{GetContextRef()};
967 if(!context
) [[unlikely
]] return nullptr;
969 ALCdevice
*device
{context
->mALDevice
.get()};
970 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
972 ALbuffer
*albuf
= LookupBuffer(device
, buffer
);
973 if(!albuf
) [[unlikely
]]
974 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
975 else if((access
&INVALID_MAP_FLAGS
) != 0) [[unlikely
]]
976 context
->setError(AL_INVALID_VALUE
, "Invalid map flags 0x%x", access
&INVALID_MAP_FLAGS
);
977 else if(!(access
&MAP_READ_WRITE_FLAGS
)) [[unlikely
]]
978 context
->setError(AL_INVALID_VALUE
, "Mapping buffer %u without read or write access",
982 ALbitfieldSOFT unavailable
= (albuf
->Access
^access
) & access
;
983 if(ReadRef(albuf
->ref
) != 0 && !(access
&AL_MAP_PERSISTENT_BIT_SOFT
)) [[unlikely
]]
984 context
->setError(AL_INVALID_OPERATION
,
985 "Mapping in-use buffer %u without persistent mapping", buffer
);
986 else if(albuf
->MappedAccess
!= 0) [[unlikely
]]
987 context
->setError(AL_INVALID_OPERATION
, "Mapping already-mapped buffer %u", buffer
);
988 else if((unavailable
&AL_MAP_READ_BIT_SOFT
)) [[unlikely
]]
989 context
->setError(AL_INVALID_VALUE
,
990 "Mapping buffer %u for reading without read access", buffer
);
991 else if((unavailable
&AL_MAP_WRITE_BIT_SOFT
)) [[unlikely
]]
992 context
->setError(AL_INVALID_VALUE
,
993 "Mapping buffer %u for writing without write access", buffer
);
994 else if((unavailable
&AL_MAP_PERSISTENT_BIT_SOFT
)) [[unlikely
]]
995 context
->setError(AL_INVALID_VALUE
,
996 "Mapping buffer %u persistently without persistent access", buffer
);
997 else if(offset
< 0 || length
<= 0
998 || static_cast<ALuint
>(offset
) >= albuf
->OriginalSize
999 || static_cast<ALuint
>(length
) > albuf
->OriginalSize
- static_cast<ALuint
>(offset
))
1001 context
->setError(AL_INVALID_VALUE
, "Mapping invalid range %d+%d for buffer %u",
1002 offset
, length
, buffer
);
1005 void *retval
{albuf
->mData
.data() + offset
};
1006 albuf
->MappedAccess
= access
;
1007 albuf
->MappedOffset
= offset
;
1008 albuf
->MappedSize
= length
;
1017 AL_API
void AL_APIENTRY
alUnmapBufferSOFT(ALuint buffer
)
1020 ContextRef context
{GetContextRef()};
1021 if(!context
) [[unlikely
]] return;
1023 ALCdevice
*device
{context
->mALDevice
.get()};
1024 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1026 ALbuffer
*albuf
= LookupBuffer(device
, buffer
);
1027 if(!albuf
) [[unlikely
]]
1028 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1029 else if(albuf
->MappedAccess
== 0) [[unlikely
]]
1030 context
->setError(AL_INVALID_OPERATION
, "Unmapping unmapped buffer %u", buffer
);
1033 albuf
->MappedAccess
= 0;
1034 albuf
->MappedOffset
= 0;
1035 albuf
->MappedSize
= 0;
1040 AL_API
void AL_APIENTRY
alFlushMappedBufferSOFT(ALuint buffer
, ALsizei offset
, ALsizei length
)
1043 ContextRef context
{GetContextRef()};
1044 if(!context
) [[unlikely
]] return;
1046 ALCdevice
*device
{context
->mALDevice
.get()};
1047 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1049 ALbuffer
*albuf
= LookupBuffer(device
, buffer
);
1050 if(!albuf
) [[unlikely
]]
1051 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1052 else if(!(albuf
->MappedAccess
&AL_MAP_WRITE_BIT_SOFT
)) [[unlikely
]]
1053 context
->setError(AL_INVALID_OPERATION
, "Flushing buffer %u while not mapped for writing",
1055 else if(offset
< albuf
->MappedOffset
|| length
<= 0
1056 || offset
>= albuf
->MappedOffset
+albuf
->MappedSize
1057 || length
> albuf
->MappedOffset
+albuf
->MappedSize
-offset
) [[unlikely
]]
1058 context
->setError(AL_INVALID_VALUE
, "Flushing invalid range %d+%d on buffer %u", offset
,
1062 /* FIXME: Need to use some method of double-buffering for the mixer and
1063 * app to hold separate memory, which can be safely transfered
1064 * asynchronously. Currently we just say the app shouldn't write where
1065 * OpenAL's reading, and hope for the best...
1067 std::atomic_thread_fence(std::memory_order_seq_cst
);
1072 AL_API
void AL_APIENTRY
alBufferSubDataSOFT(ALuint buffer
, ALenum format
, const ALvoid
*data
, ALsizei offset
, ALsizei length
)
1075 ContextRef context
{GetContextRef()};
1076 if(!context
) [[unlikely
]] return;
1078 ALCdevice
*device
{context
->mALDevice
.get()};
1079 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1081 ALbuffer
*albuf
= LookupBuffer(device
, buffer
);
1082 if(!albuf
) [[unlikely
]]
1084 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1088 auto usrfmt
= DecomposeUserFormat(format
);
1089 if(!usrfmt
) [[unlikely
]]
1091 context
->setError(AL_INVALID_ENUM
, "Invalid format 0x%04x", format
);
1095 ALuint unpack_align
{albuf
->UnpackAlign
};
1096 ALuint align
{SanitizeAlignment(usrfmt
->type
, unpack_align
)};
1097 if(align
< 1) [[unlikely
]]
1098 context
->setError(AL_INVALID_VALUE
, "Invalid unpack alignment %u", unpack_align
);
1099 else if(al::to_underlying(usrfmt
->channels
) != al::to_underlying(albuf
->mChannels
)
1100 || usrfmt
->type
!= albuf
->OriginalType
) [[unlikely
]]
1101 context
->setError(AL_INVALID_ENUM
, "Unpacking data with mismatched format");
1102 else if(align
!= albuf
->OriginalAlign
) [[unlikely
]]
1103 context
->setError(AL_INVALID_VALUE
,
1104 "Unpacking data with alignment %u does not match original alignment %u", align
,
1105 albuf
->OriginalAlign
);
1106 else if(albuf
->isBFormat() && albuf
->UnpackAmbiOrder
!= albuf
->mAmbiOrder
) [[unlikely
]]
1107 context
->setError(AL_INVALID_VALUE
, "Unpacking data with mismatched ambisonic order");
1108 else if(albuf
->MappedAccess
!= 0) [[unlikely
]]
1109 context
->setError(AL_INVALID_OPERATION
, "Unpacking data into mapped buffer %u", buffer
);
1112 ALuint num_chans
{albuf
->channelsFromFmt()};
1113 ALuint frame_size
{num_chans
* albuf
->bytesFromFmt()};
1115 (albuf
->OriginalType
== UserFmtIMA4
) ? ((align
-1)/2 + 4) * num_chans
:
1116 (albuf
->OriginalType
== UserFmtMSADPCM
) ? ((align
-2)/2 + 7) * num_chans
:
1117 (align
* frame_size
)
1120 if(offset
< 0 || length
< 0 || static_cast<ALuint
>(offset
) > albuf
->OriginalSize
1121 || static_cast<ALuint
>(length
) > albuf
->OriginalSize
-static_cast<ALuint
>(offset
))
1123 context
->setError(AL_INVALID_VALUE
, "Invalid data sub-range %d+%d on buffer %u",
1124 offset
, length
, buffer
);
1125 else if((static_cast<ALuint
>(offset
)%byte_align
) != 0) [[unlikely
]]
1126 context
->setError(AL_INVALID_VALUE
,
1127 "Sub-range offset %d is not a multiple of frame size %d (%d unpack alignment)",
1128 offset
, byte_align
, align
);
1129 else if((static_cast<ALuint
>(length
)%byte_align
) != 0) [[unlikely
]]
1130 context
->setError(AL_INVALID_VALUE
,
1131 "Sub-range length %d is not a multiple of frame size %d (%d unpack alignment)",
1132 length
, byte_align
, align
);
1135 /* offset -> byte offset, length -> sample count */
1136 size_t byteoff
{static_cast<ALuint
>(offset
)/byte_align
* align
* frame_size
};
1137 size_t samplen
{static_cast<ALuint
>(length
)/byte_align
* align
};
1139 void *dst
= albuf
->mData
.data() + byteoff
;
1140 if(usrfmt
->type
== UserFmtIMA4
&& albuf
->mType
== FmtShort
)
1141 Convert_int16_ima4(static_cast<int16_t*>(dst
), static_cast<const al::byte
*>(data
),
1142 num_chans
, samplen
, align
);
1143 else if(usrfmt
->type
== UserFmtMSADPCM
&& albuf
->mType
== FmtShort
)
1144 Convert_int16_msadpcm(static_cast<int16_t*>(dst
),
1145 static_cast<const al::byte
*>(data
), num_chans
, samplen
, align
);
1148 assert(al::to_underlying(usrfmt
->type
) == al::to_underlying(albuf
->mType
));
1149 memcpy(dst
, data
, size_t{samplen
} * frame_size
);
1157 AL_API
void AL_APIENTRY
alBufferSamplesSOFT(ALuint
/*buffer*/, ALuint
/*samplerate*/,
1158 ALenum
/*internalformat*/, ALsizei
/*samples*/, ALenum
/*channels*/, ALenum
/*type*/,
1159 const ALvoid
* /*data*/)
1162 ContextRef context
{GetContextRef()};
1163 if(!context
) [[unlikely
]] return;
1165 context
->setError(AL_INVALID_OPERATION
, "alBufferSamplesSOFT not supported");
1169 AL_API
void AL_APIENTRY
alBufferSubSamplesSOFT(ALuint
/*buffer*/, ALsizei
/*offset*/,
1170 ALsizei
/*samples*/, ALenum
/*channels*/, ALenum
/*type*/, const ALvoid
* /*data*/)
1173 ContextRef context
{GetContextRef()};
1174 if(!context
) [[unlikely
]] return;
1176 context
->setError(AL_INVALID_OPERATION
, "alBufferSubSamplesSOFT not supported");
1180 AL_API
void AL_APIENTRY
alGetBufferSamplesSOFT(ALuint
/*buffer*/, ALsizei
/*offset*/,
1181 ALsizei
/*samples*/, ALenum
/*channels*/, ALenum
/*type*/, ALvoid
* /*data*/)
1184 ContextRef context
{GetContextRef()};
1185 if(!context
) [[unlikely
]] return;
1187 context
->setError(AL_INVALID_OPERATION
, "alGetBufferSamplesSOFT not supported");
1191 AL_API ALboolean AL_APIENTRY
alIsBufferFormatSupportedSOFT(ALenum
/*format*/)
1194 ContextRef context
{GetContextRef()};
1195 if(!context
) [[unlikely
]] return AL_FALSE
;
1197 context
->setError(AL_INVALID_OPERATION
, "alIsBufferFormatSupportedSOFT not supported");
1203 AL_API
void AL_APIENTRY
alBufferf(ALuint buffer
, ALenum param
, ALfloat
/*value*/)
1206 ContextRef context
{GetContextRef()};
1207 if(!context
) [[unlikely
]] return;
1209 ALCdevice
*device
{context
->mALDevice
.get()};
1210 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1212 if(LookupBuffer(device
, buffer
) == nullptr) [[unlikely
]]
1213 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1217 context
->setError(AL_INVALID_ENUM
, "Invalid buffer float property 0x%04x", param
);
1222 AL_API
void AL_APIENTRY
alBuffer3f(ALuint buffer
, ALenum param
,
1223 ALfloat
/*value1*/, ALfloat
/*value2*/, ALfloat
/*value3*/)
1226 ContextRef context
{GetContextRef()};
1227 if(!context
) [[unlikely
]] return;
1229 ALCdevice
*device
{context
->mALDevice
.get()};
1230 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1232 if(LookupBuffer(device
, buffer
) == nullptr) [[unlikely
]]
1233 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1237 context
->setError(AL_INVALID_ENUM
, "Invalid buffer 3-float property 0x%04x", param
);
1242 AL_API
void AL_APIENTRY
alBufferfv(ALuint buffer
, ALenum param
, const ALfloat
*values
)
1245 ContextRef context
{GetContextRef()};
1246 if(!context
) [[unlikely
]] return;
1248 ALCdevice
*device
{context
->mALDevice
.get()};
1249 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1251 if(LookupBuffer(device
, buffer
) == nullptr) [[unlikely
]]
1252 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1253 else if(!values
) [[unlikely
]]
1254 context
->setError(AL_INVALID_VALUE
, "NULL pointer");
1258 context
->setError(AL_INVALID_ENUM
, "Invalid buffer float-vector property 0x%04x", param
);
1264 AL_API
void AL_APIENTRY
alBufferi(ALuint buffer
, ALenum param
, ALint value
)
1267 ContextRef context
{GetContextRef()};
1268 if(!context
) [[unlikely
]] return;
1270 ALCdevice
*device
{context
->mALDevice
.get()};
1271 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1273 ALbuffer
*albuf
= LookupBuffer(device
, buffer
);
1274 if(!albuf
) [[unlikely
]]
1275 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1278 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT
:
1279 if(value
< 0) [[unlikely
]]
1280 context
->setError(AL_INVALID_VALUE
, "Invalid unpack block alignment %d", value
);
1282 albuf
->UnpackAlign
= static_cast<ALuint
>(value
);
1285 case AL_PACK_BLOCK_ALIGNMENT_SOFT
:
1286 if(value
< 0) [[unlikely
]]
1287 context
->setError(AL_INVALID_VALUE
, "Invalid pack block alignment %d", value
);
1289 albuf
->PackAlign
= static_cast<ALuint
>(value
);
1292 case AL_AMBISONIC_LAYOUT_SOFT
:
1293 if(ReadRef(albuf
->ref
) != 0) [[unlikely
]]
1294 context
->setError(AL_INVALID_OPERATION
, "Modifying in-use buffer %u's ambisonic layout",
1296 else if(value
!= AL_FUMA_SOFT
&& value
!= AL_ACN_SOFT
) [[unlikely
]]
1297 context
->setError(AL_INVALID_VALUE
, "Invalid unpack ambisonic layout %04x", value
);
1299 albuf
->mAmbiLayout
= AmbiLayoutFromEnum(value
).value();
1302 case AL_AMBISONIC_SCALING_SOFT
:
1303 if(ReadRef(albuf
->ref
) != 0) [[unlikely
]]
1304 context
->setError(AL_INVALID_OPERATION
, "Modifying in-use buffer %u's ambisonic scaling",
1306 else if(value
!= AL_FUMA_SOFT
&& value
!= AL_SN3D_SOFT
&& value
!= AL_N3D_SOFT
)
1308 context
->setError(AL_INVALID_VALUE
, "Invalid unpack ambisonic scaling %04x", value
);
1310 albuf
->mAmbiScaling
= AmbiScalingFromEnum(value
).value();
1313 case AL_UNPACK_AMBISONIC_ORDER_SOFT
:
1314 if(value
< 1 || value
> 14) [[unlikely
]]
1315 context
->setError(AL_INVALID_VALUE
, "Invalid unpack ambisonic order %d", value
);
1317 albuf
->UnpackAmbiOrder
= static_cast<ALuint
>(value
);
1321 context
->setError(AL_INVALID_ENUM
, "Invalid buffer integer property 0x%04x", param
);
1326 AL_API
void AL_APIENTRY
alBuffer3i(ALuint buffer
, ALenum param
,
1327 ALint
/*value1*/, ALint
/*value2*/, ALint
/*value3*/)
1330 ContextRef context
{GetContextRef()};
1331 if(!context
) [[unlikely
]] return;
1333 ALCdevice
*device
{context
->mALDevice
.get()};
1334 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1336 if(LookupBuffer(device
, buffer
) == nullptr) [[unlikely
]]
1337 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1341 context
->setError(AL_INVALID_ENUM
, "Invalid buffer 3-integer property 0x%04x", param
);
1346 AL_API
void AL_APIENTRY
alBufferiv(ALuint buffer
, ALenum param
, const ALint
*values
)
1353 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT
:
1354 case AL_PACK_BLOCK_ALIGNMENT_SOFT
:
1355 case AL_AMBISONIC_LAYOUT_SOFT
:
1356 case AL_AMBISONIC_SCALING_SOFT
:
1357 case AL_UNPACK_AMBISONIC_ORDER_SOFT
:
1358 alBufferi(buffer
, param
, values
[0]);
1363 ContextRef context
{GetContextRef()};
1364 if(!context
) [[unlikely
]] return;
1366 ALCdevice
*device
{context
->mALDevice
.get()};
1367 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1369 ALbuffer
*albuf
= LookupBuffer(device
, buffer
);
1370 if(!albuf
) [[unlikely
]]
1371 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1372 else if(!values
) [[unlikely
]]
1373 context
->setError(AL_INVALID_VALUE
, "NULL pointer");
1376 case AL_LOOP_POINTS_SOFT
:
1377 if(ReadRef(albuf
->ref
) != 0) [[unlikely
]]
1378 context
->setError(AL_INVALID_OPERATION
, "Modifying in-use buffer %u's loop points",
1380 else if(values
[0] < 0 || values
[0] >= values
[1]
1381 || static_cast<ALuint
>(values
[1]) > albuf
->mSampleLen
) [[unlikely
]]
1382 context
->setError(AL_INVALID_VALUE
, "Invalid loop point range %d -> %d on buffer %u",
1383 values
[0], values
[1], buffer
);
1386 albuf
->mLoopStart
= static_cast<ALuint
>(values
[0]);
1387 albuf
->mLoopEnd
= static_cast<ALuint
>(values
[1]);
1392 context
->setError(AL_INVALID_ENUM
, "Invalid buffer integer-vector property 0x%04x", param
);
1398 AL_API
void AL_APIENTRY
alGetBufferf(ALuint buffer
, ALenum param
, ALfloat
*value
)
1401 ContextRef context
{GetContextRef()};
1402 if(!context
) [[unlikely
]] return;
1404 ALCdevice
*device
{context
->mALDevice
.get()};
1405 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1407 ALbuffer
*albuf
= LookupBuffer(device
, buffer
);
1408 if(!albuf
) [[unlikely
]]
1409 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1410 else if(!value
) [[unlikely
]]
1411 context
->setError(AL_INVALID_VALUE
, "NULL pointer");
1415 context
->setError(AL_INVALID_ENUM
, "Invalid buffer float property 0x%04x", param
);
1420 AL_API
void AL_APIENTRY
alGetBuffer3f(ALuint buffer
, ALenum param
, ALfloat
*value1
, ALfloat
*value2
, ALfloat
*value3
)
1423 ContextRef context
{GetContextRef()};
1424 if(!context
) [[unlikely
]] return;
1426 ALCdevice
*device
{context
->mALDevice
.get()};
1427 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1429 if(LookupBuffer(device
, buffer
) == nullptr) [[unlikely
]]
1430 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1431 else if(!value1
|| !value2
|| !value3
) [[unlikely
]]
1432 context
->setError(AL_INVALID_VALUE
, "NULL pointer");
1436 context
->setError(AL_INVALID_ENUM
, "Invalid buffer 3-float property 0x%04x", param
);
1441 AL_API
void AL_APIENTRY
alGetBufferfv(ALuint buffer
, ALenum param
, ALfloat
*values
)
1446 case AL_SEC_LENGTH_SOFT
:
1447 alGetBufferf(buffer
, param
, values
);
1451 ContextRef context
{GetContextRef()};
1452 if(!context
) [[unlikely
]] return;
1454 ALCdevice
*device
{context
->mALDevice
.get()};
1455 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1457 if(LookupBuffer(device
, buffer
) == nullptr) [[unlikely
]]
1458 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1459 else if(!values
) [[unlikely
]]
1460 context
->setError(AL_INVALID_VALUE
, "NULL pointer");
1464 context
->setError(AL_INVALID_ENUM
, "Invalid buffer float-vector property 0x%04x", param
);
1470 AL_API
void AL_APIENTRY
alGetBufferi(ALuint buffer
, ALenum param
, ALint
*value
)
1473 ContextRef context
{GetContextRef()};
1474 if(!context
) [[unlikely
]] return;
1476 ALCdevice
*device
{context
->mALDevice
.get()};
1477 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1478 ALbuffer
*albuf
= LookupBuffer(device
, buffer
);
1479 if(!albuf
) [[unlikely
]]
1480 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1481 else if(!value
) [[unlikely
]]
1482 context
->setError(AL_INVALID_VALUE
, "NULL pointer");
1486 *value
= static_cast<ALint
>(albuf
->mSampleRate
);
1490 *value
= static_cast<ALint
>(albuf
->bytesFromFmt() * 8);
1494 *value
= static_cast<ALint
>(albuf
->channelsFromFmt());
1498 *value
= static_cast<ALint
>(albuf
->mSampleLen
* albuf
->frameSizeFromFmt());
1501 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT
:
1502 *value
= static_cast<ALint
>(albuf
->UnpackAlign
);
1505 case AL_PACK_BLOCK_ALIGNMENT_SOFT
:
1506 *value
= static_cast<ALint
>(albuf
->PackAlign
);
1509 case AL_AMBISONIC_LAYOUT_SOFT
:
1510 *value
= EnumFromAmbiLayout(albuf
->mAmbiLayout
);
1513 case AL_AMBISONIC_SCALING_SOFT
:
1514 *value
= EnumFromAmbiScaling(albuf
->mAmbiScaling
);
1517 case AL_UNPACK_AMBISONIC_ORDER_SOFT
:
1518 *value
= static_cast<int>(albuf
->UnpackAmbiOrder
);
1522 context
->setError(AL_INVALID_ENUM
, "Invalid buffer integer property 0x%04x", param
);
1527 AL_API
void AL_APIENTRY
alGetBuffer3i(ALuint buffer
, ALenum param
, ALint
*value1
, ALint
*value2
, ALint
*value3
)
1530 ContextRef context
{GetContextRef()};
1531 if(!context
) [[unlikely
]] return;
1533 ALCdevice
*device
{context
->mALDevice
.get()};
1534 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1535 if(LookupBuffer(device
, buffer
) == nullptr) [[unlikely
]]
1536 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1537 else if(!value1
|| !value2
|| !value3
) [[unlikely
]]
1538 context
->setError(AL_INVALID_VALUE
, "NULL pointer");
1542 context
->setError(AL_INVALID_ENUM
, "Invalid buffer 3-integer property 0x%04x", param
);
1547 AL_API
void AL_APIENTRY
alGetBufferiv(ALuint buffer
, ALenum param
, ALint
*values
)
1556 case AL_INTERNAL_FORMAT_SOFT
:
1557 case AL_BYTE_LENGTH_SOFT
:
1558 case AL_SAMPLE_LENGTH_SOFT
:
1559 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT
:
1560 case AL_PACK_BLOCK_ALIGNMENT_SOFT
:
1561 case AL_AMBISONIC_LAYOUT_SOFT
:
1562 case AL_AMBISONIC_SCALING_SOFT
:
1563 case AL_UNPACK_AMBISONIC_ORDER_SOFT
:
1564 alGetBufferi(buffer
, param
, values
);
1568 ContextRef context
{GetContextRef()};
1569 if(!context
) [[unlikely
]] return;
1571 ALCdevice
*device
{context
->mALDevice
.get()};
1572 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1573 ALbuffer
*albuf
= LookupBuffer(device
, buffer
);
1574 if(!albuf
) [[unlikely
]]
1575 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1576 else if(!values
) [[unlikely
]]
1577 context
->setError(AL_INVALID_VALUE
, "NULL pointer");
1580 case AL_LOOP_POINTS_SOFT
:
1581 values
[0] = static_cast<ALint
>(albuf
->mLoopStart
);
1582 values
[1] = static_cast<ALint
>(albuf
->mLoopEnd
);
1586 context
->setError(AL_INVALID_ENUM
, "Invalid buffer integer-vector property 0x%04x", param
);
1592 AL_API
void AL_APIENTRY
alBufferCallbackSOFT(ALuint buffer
, ALenum format
, ALsizei freq
,
1593 ALBUFFERCALLBACKTYPESOFT callback
, ALvoid
*userptr
)
1596 ContextRef context
{GetContextRef()};
1597 if(!context
) [[unlikely
]] return;
1599 ALCdevice
*device
{context
->mALDevice
.get()};
1600 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1602 ALbuffer
*albuf
= LookupBuffer(device
, buffer
);
1603 if(!albuf
) [[unlikely
]]
1604 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1605 else if(freq
< 1) [[unlikely
]]
1606 context
->setError(AL_INVALID_VALUE
, "Invalid sample rate %d", freq
);
1607 else if(callback
== nullptr) [[unlikely
]]
1608 context
->setError(AL_INVALID_VALUE
, "NULL callback");
1611 auto usrfmt
= DecomposeUserFormat(format
);
1612 if(!usrfmt
) [[unlikely
]]
1613 context
->setError(AL_INVALID_ENUM
, "Invalid format 0x%04x", format
);
1615 PrepareCallback(context
.get(), albuf
, freq
, usrfmt
->channels
, usrfmt
->type
, callback
,
1621 AL_API
void AL_APIENTRY
alGetBufferPtrSOFT(ALuint buffer
, ALenum param
, ALvoid
**value
)
1624 ContextRef context
{GetContextRef()};
1625 if(!context
) [[unlikely
]] return;
1627 ALCdevice
*device
{context
->mALDevice
.get()};
1628 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1629 ALbuffer
*albuf
= LookupBuffer(device
, buffer
);
1630 if(!albuf
) [[unlikely
]]
1631 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1632 else if(!value
) [[unlikely
]]
1633 context
->setError(AL_INVALID_VALUE
, "NULL pointer");
1636 case AL_BUFFER_CALLBACK_FUNCTION_SOFT
:
1637 *value
= reinterpret_cast<void*>(albuf
->mCallback
);
1639 case AL_BUFFER_CALLBACK_USER_PARAM_SOFT
:
1640 *value
= albuf
->mUserData
;
1644 context
->setError(AL_INVALID_ENUM
, "Invalid buffer pointer property 0x%04x", param
);
1649 AL_API
void AL_APIENTRY
alGetBuffer3PtrSOFT(ALuint buffer
, ALenum param
, ALvoid
**value1
, ALvoid
**value2
, ALvoid
**value3
)
1652 ContextRef context
{GetContextRef()};
1653 if(!context
) [[unlikely
]] return;
1655 ALCdevice
*device
{context
->mALDevice
.get()};
1656 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1657 if(LookupBuffer(device
, buffer
) == nullptr) [[unlikely
]]
1658 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1659 else if(!value1
|| !value2
|| !value3
) [[unlikely
]]
1660 context
->setError(AL_INVALID_VALUE
, "NULL pointer");
1664 context
->setError(AL_INVALID_ENUM
, "Invalid buffer 3-pointer property 0x%04x", param
);
1669 AL_API
void AL_APIENTRY
alGetBufferPtrvSOFT(ALuint buffer
, ALenum param
, ALvoid
**values
)
1674 case AL_BUFFER_CALLBACK_FUNCTION_SOFT
:
1675 case AL_BUFFER_CALLBACK_USER_PARAM_SOFT
:
1676 alGetBufferPtrSOFT(buffer
, param
, values
);
1680 ContextRef context
{GetContextRef()};
1681 if(!context
) [[unlikely
]] return;
1683 ALCdevice
*device
{context
->mALDevice
.get()};
1684 std::lock_guard
<std::mutex
> _
{device
->BufferLock
};
1685 if(LookupBuffer(device
, buffer
) == nullptr) [[unlikely
]]
1686 context
->setError(AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
);
1687 else if(!values
) [[unlikely
]]
1688 context
->setError(AL_INVALID_VALUE
, "NULL pointer");
1692 context
->setError(AL_INVALID_ENUM
, "Invalid buffer pointer-vector property 0x%04x", param
);
1698 BufferSubList::~BufferSubList()
1700 uint64_t usemask
{~FreeMask
};
1703 const int idx
{al::countr_zero(usemask
)};
1704 al::destroy_at(Buffers
+idx
);
1705 usemask
&= ~(1_u64
<< idx
);
1707 FreeMask
= ~usemask
;
1714 FORCE_ALIGN ALboolean AL_APIENTRY
EAXSetBufferMode(ALsizei n
, const ALuint
* buffers
, ALint value
)
1717 #define EAX_PREFIX "[EAXSetBufferMode] "
1719 const auto context
= ContextRef
{GetContextRef()};
1722 ERR(EAX_PREFIX
"%s\n", "No current context.");
1726 if(!eax_g_is_enabled
)
1728 context
->setError(AL_INVALID_OPERATION
, EAX_PREFIX
"%s", "EAX not enabled.");
1734 case AL_STORAGE_AUTOMATIC
:
1735 case AL_STORAGE_HARDWARE
:
1736 case AL_STORAGE_ACCESSIBLE
:
1740 context
->setError(AL_INVALID_ENUM
, EAX_PREFIX
"Unsupported X-RAM mode 0x%x", value
);
1749 context
->setError(AL_INVALID_VALUE
, EAX_PREFIX
"Buffer count %d out of range", n
);
1755 context
->setError(AL_INVALID_VALUE
, EAX_PREFIX
"%s", "Null AL buffers");
1759 auto device
= context
->mALDevice
.get();
1760 std::lock_guard
<std::mutex
> device_lock
{device
->BufferLock
};
1761 size_t total_needed
{0};
1763 // Validate the buffers.
1765 for(auto i
= 0;i
< n
;++i
)
1767 const auto buffer
= buffers
[i
];
1768 if(buffer
== AL_NONE
)
1771 const auto al_buffer
= LookupBuffer(device
, buffer
);
1772 if(!al_buffer
) [[unlikely
]]
1774 ERR(EAX_PREFIX
"Invalid buffer ID %u.\n", buffer
);
1778 /* TODO: Is the store location allowed to change for in-use buffers, or
1779 * only when not set/queued on a source?
1782 if(value
== AL_STORAGE_HARDWARE
&& !al_buffer
->eax_x_ram_is_hardware
)
1784 /* FIXME: This doesn't account for duplicate buffers. When the same
1785 * buffer ID is specified multiple times in the provided list, it
1786 * counts each instance as more memory that needs to fit in X-RAM.
1788 if(std::numeric_limits
<size_t>::max()-al_buffer
->OriginalSize
< total_needed
) [[unlikely
]]
1790 context
->setError(AL_OUT_OF_MEMORY
, EAX_PREFIX
"Buffer size overflow (%u + %zu)\n",
1791 al_buffer
->OriginalSize
, total_needed
);
1794 total_needed
+= al_buffer
->OriginalSize
;
1797 if(total_needed
> device
->eax_x_ram_free_size
)
1799 context
->setError(AL_INVALID_ENUM
, EAX_PREFIX
"Out of X-RAM memory (need: %zu, avail: %u)",
1800 total_needed
, device
->eax_x_ram_free_size
);
1806 for(auto i
= 0;i
< n
;++i
)
1808 const auto buffer
= buffers
[i
];
1809 if(buffer
== AL_NONE
)
1812 const auto al_buffer
= LookupBuffer(device
, buffer
);
1815 if(value
!= AL_STORAGE_ACCESSIBLE
)
1816 eax_x_ram_apply(*device
, *al_buffer
);
1818 eax_x_ram_clear(*device
, *al_buffer
);
1819 al_buffer
->eax_x_ram_mode
= value
;
1828 FORCE_ALIGN ALenum AL_APIENTRY
EAXGetBufferMode(ALuint buffer
, ALint
* pReserved
)
1831 #define EAX_PREFIX "[EAXGetBufferMode] "
1833 const auto context
= ContextRef
{GetContextRef()};
1836 ERR(EAX_PREFIX
"%s\n", "No current context.");
1840 if(!eax_g_is_enabled
)
1842 context
->setError(AL_INVALID_OPERATION
, EAX_PREFIX
"%s", "EAX not enabled.");
1848 context
->setError(AL_INVALID_VALUE
, EAX_PREFIX
"%s", "Non-null reserved parameter");
1852 auto device
= context
->mALDevice
.get();
1853 std::lock_guard
<std::mutex
> device_lock
{device
->BufferLock
};
1855 const auto al_buffer
= LookupBuffer(device
, buffer
);
1858 context
->setError(AL_INVALID_NAME
, EAX_PREFIX
"Invalid buffer ID %u", buffer
);
1862 return al_buffer
->eax_x_ram_mode
;
1868 #endif // ALSOFT_EAX