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
40 #include <unordered_map>
49 #include "alc/context.h"
50 #include "alc/device.h"
51 #include "alc/inprogext.h"
53 #include "alnumeric.h"
55 #include "core/device.h"
56 #include "core/resampler_limits.h"
57 #include "core/voice.h"
58 #include "direct_defs.h"
60 #include "intrusive_ptr.h"
61 #include "opthelpers.h"
64 #include <unordered_set>
66 #include "eax/globals.h"
67 #include "eax/x_ram.h"
73 using SubListAllocator
= al::allocator
<std::array
<ALbuffer
,64>>;
75 constexpr auto AmbiLayoutFromEnum(ALenum layout
) noexcept
-> std::optional
<AmbiLayout
>
79 case AL_FUMA_SOFT
: return AmbiLayout::FuMa
;
80 case AL_ACN_SOFT
: return AmbiLayout::ACN
;
84 constexpr auto EnumFromAmbiLayout(AmbiLayout layout
) -> ALenum
88 case AmbiLayout::FuMa
: return AL_FUMA_SOFT
;
89 case AmbiLayout::ACN
: return AL_ACN_SOFT
;
91 throw std::runtime_error
{"Invalid AmbiLayout: "+std::to_string(int(layout
))};
94 constexpr auto AmbiScalingFromEnum(ALenum scale
) noexcept
-> std::optional
<AmbiScaling
>
98 case AL_FUMA_SOFT
: return AmbiScaling::FuMa
;
99 case AL_SN3D_SOFT
: return AmbiScaling::SN3D
;
100 case AL_N3D_SOFT
: return AmbiScaling::N3D
;
104 constexpr auto EnumFromAmbiScaling(AmbiScaling scale
) -> ALenum
108 case AmbiScaling::FuMa
: return AL_FUMA_SOFT
;
109 case AmbiScaling::SN3D
: return AL_SN3D_SOFT
;
110 case AmbiScaling::N3D
: return AL_N3D_SOFT
;
111 case AmbiScaling::UHJ
: break;
113 throw std::runtime_error
{"Invalid AmbiScaling: "+std::to_string(int(scale
))};
117 constexpr auto EaxStorageFromEnum(ALenum scale
) noexcept
-> std::optional
<EaxStorage
>
121 case AL_STORAGE_AUTOMATIC
: return EaxStorage::Automatic
;
122 case AL_STORAGE_ACCESSIBLE
: return EaxStorage::Accessible
;
123 case AL_STORAGE_HARDWARE
: return EaxStorage::Hardware
;
127 constexpr auto EnumFromEaxStorage(EaxStorage storage
) -> ALenum
131 case EaxStorage::Automatic
: return AL_STORAGE_AUTOMATIC
;
132 case EaxStorage::Accessible
: return AL_STORAGE_ACCESSIBLE
;
133 case EaxStorage::Hardware
: return AL_STORAGE_HARDWARE
;
135 throw std::runtime_error
{"Invalid EaxStorage: "+std::to_string(int(storage
))};
139 bool eax_x_ram_check_availability(const ALCdevice
&device
, const ALbuffer
&buffer
,
140 const ALuint newsize
) noexcept
142 ALuint freemem
{device
.eax_x_ram_free_size
};
143 /* If the buffer is currently in "hardware", add its memory to the free
144 * pool since it'll be "replaced".
146 if(buffer
.eax_x_ram_is_hardware
)
147 freemem
+= buffer
.OriginalSize
;
148 return freemem
>= newsize
;
151 void eax_x_ram_apply(ALCdevice
&device
, ALbuffer
&buffer
) noexcept
153 if(buffer
.eax_x_ram_is_hardware
)
156 if(device
.eax_x_ram_free_size
>= buffer
.OriginalSize
)
158 device
.eax_x_ram_free_size
-= buffer
.OriginalSize
;
159 buffer
.eax_x_ram_is_hardware
= true;
163 void eax_x_ram_clear(ALCdevice
& al_device
, ALbuffer
& al_buffer
) noexcept
165 if(al_buffer
.eax_x_ram_is_hardware
)
166 al_device
.eax_x_ram_free_size
+= al_buffer
.OriginalSize
;
167 al_buffer
.eax_x_ram_is_hardware
= false;
172 constexpr ALbitfieldSOFT INVALID_STORAGE_MASK
{~unsigned(AL_MAP_READ_BIT_SOFT
|
173 AL_MAP_WRITE_BIT_SOFT
| AL_MAP_PERSISTENT_BIT_SOFT
| AL_PRESERVE_DATA_BIT_SOFT
)};
174 constexpr ALbitfieldSOFT MAP_READ_WRITE_FLAGS
{AL_MAP_READ_BIT_SOFT
| AL_MAP_WRITE_BIT_SOFT
};
175 constexpr ALbitfieldSOFT INVALID_MAP_FLAGS
{~unsigned(AL_MAP_READ_BIT_SOFT
| AL_MAP_WRITE_BIT_SOFT
|
176 AL_MAP_PERSISTENT_BIT_SOFT
)};
179 auto EnsureBuffers(ALCdevice
*device
, size_t needed
) noexcept
-> bool
181 size_t count
{std::accumulate(device
->BufferList
.cbegin(), device
->BufferList
.cend(), 0_uz
,
182 [](size_t cur
, const BufferSubList
&sublist
) noexcept
-> size_t
183 { return cur
+ static_cast<ALuint
>(al::popcount(sublist
.FreeMask
)); })};
185 while(needed
> count
)
187 if(device
->BufferList
.size() >= 1<<25) UNLIKELY
190 BufferSubList sublist
{};
191 sublist
.FreeMask
= ~0_u64
;
192 sublist
.Buffers
= SubListAllocator
{}.allocate(1);
193 device
->BufferList
.emplace_back(std::move(sublist
));
194 count
+= std::tuple_size_v
<SubListAllocator::value_type
>;
202 ALbuffer
*AllocBuffer(ALCdevice
*device
) noexcept
204 auto sublist
= std::find_if(device
->BufferList
.begin(), device
->BufferList
.end(),
205 [](const BufferSubList
&entry
) noexcept
-> bool
206 { return entry
.FreeMask
!= 0; });
207 auto lidx
= static_cast<ALuint
>(std::distance(device
->BufferList
.begin(), sublist
));
208 auto slidx
= static_cast<ALuint
>(al::countr_zero(sublist
->FreeMask
));
211 ALbuffer
*buffer
{al::construct_at(al::to_address(sublist
->Buffers
->begin() + slidx
))};
213 /* Add 1 to avoid buffer ID 0. */
214 buffer
->id
= ((lidx
<<6) | slidx
) + 1;
216 sublist
->FreeMask
&= ~(1_u64
<< slidx
);
221 void FreeBuffer(ALCdevice
*device
, ALbuffer
*buffer
)
224 eax_x_ram_clear(*device
, *buffer
);
227 device
->mBufferNames
.erase(buffer
->id
);
229 const ALuint id
{buffer
->id
- 1};
230 const size_t lidx
{id
>> 6};
231 const ALuint slidx
{id
& 0x3f};
233 std::destroy_at(buffer
);
235 device
->BufferList
[lidx
].FreeMask
|= 1_u64
<< slidx
;
238 auto LookupBuffer(ALCdevice
*device
, ALuint id
) noexcept
-> ALbuffer
*
240 const size_t lidx
{(id
-1) >> 6};
241 const ALuint slidx
{(id
-1) & 0x3f};
243 if(lidx
>= device
->BufferList
.size()) UNLIKELY
245 BufferSubList
&sublist
= device
->BufferList
[lidx
];
246 if(sublist
.FreeMask
& (1_u64
<< slidx
)) UNLIKELY
248 return al::to_address(sublist
.Buffers
->begin() + slidx
);
252 constexpr auto SanitizeAlignment(FmtType type
, ALuint align
) noexcept
-> ALuint
258 /* Here is where things vary:
259 * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
260 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
264 if(type
== FmtMSADPCM
)
271 /* IMA4 block alignment must be a multiple of 8, plus 1. */
272 if((align
&7) == 1) return align
;
275 if(type
== FmtMSADPCM
)
277 /* MSADPCM block alignment must be a multiple of 2. */
278 if((align
&1) == 0) return align
;
286 /** Loads the specified data into the buffer, using the specified format. */
287 void LoadData(ALCcontext
*context
[[maybe_unused
]], ALbuffer
*ALBuf
, ALsizei freq
, ALuint size
,
288 const FmtChannels DstChannels
, const FmtType DstType
, const std::byte
*SrcData
,
289 ALbitfieldSOFT access
)
291 if(ALBuf
->ref
.load(std::memory_order_relaxed
) != 0 || ALBuf
->MappedAccess
!= 0)
292 throw al::context_error
{AL_INVALID_OPERATION
, "Modifying storage for in-use buffer %u",
295 const ALuint unpackalign
{ALBuf
->UnpackAlign
};
296 const ALuint align
{SanitizeAlignment(DstType
, unpackalign
)};
298 throw al::context_error
{AL_INVALID_VALUE
, "Invalid unpack alignment %u for %s samples",
299 unpackalign
, NameFromFormat(DstType
)};
301 const ALuint ambiorder
{IsBFormat(DstChannels
) ? ALBuf
->UnpackAmbiOrder
:
302 (IsUHJ(DstChannels
) ? 1 : 0)};
304 if((access
&AL_PRESERVE_DATA_BIT_SOFT
))
306 /* Can only preserve data with the same format and alignment. */
307 if(ALBuf
->mChannels
!= DstChannels
|| ALBuf
->mType
!= DstType
)
308 throw al::context_error
{AL_INVALID_VALUE
, "Preserving data of mismatched format"};
309 if(ALBuf
->mBlockAlign
!= align
)
310 throw al::context_error
{AL_INVALID_VALUE
, "Preserving data of mismatched alignment"};
311 if(ALBuf
->mAmbiOrder
!= ambiorder
)
312 throw al::context_error
{AL_INVALID_VALUE
, "Preserving data of mismatched order"};
315 /* Convert the size in bytes to blocks using the unpack block alignment. */
316 const ALuint NumChannels
{ChannelsFromFmt(DstChannels
, ambiorder
)};
317 const ALuint BlockSize
{NumChannels
*
318 ((DstType
== FmtIMA4
) ? (align
-1)/2 + 4 :
319 (DstType
== FmtMSADPCM
) ? (align
-2)/2 + 7 :
320 (align
* BytesFromFmt(DstType
)))};
321 if((size
%BlockSize
) != 0)
322 throw al::context_error
{AL_INVALID_VALUE
,
323 "Data size %d is not a multiple of frame size %d (%d unpack alignment)",
324 size
, BlockSize
, align
};
325 const ALuint blocks
{size
/ BlockSize
};
327 if(blocks
> std::numeric_limits
<ALsizei
>::max()/align
)
328 throw al::context_error
{AL_OUT_OF_MEMORY
,
329 "Buffer size overflow, %d blocks x %d samples per block", blocks
, align
};
330 if(blocks
> std::numeric_limits
<size_t>::max()/BlockSize
)
331 throw al::context_error
{AL_OUT_OF_MEMORY
,
332 "Buffer size overflow, %d frames x %d bytes per frame", blocks
, BlockSize
};
334 const size_t newsize
{static_cast<size_t>(blocks
) * BlockSize
};
337 if(ALBuf
->eax_x_ram_mode
== EaxStorage::Hardware
)
339 ALCdevice
&device
= *context
->mALDevice
;
340 if(!eax_x_ram_check_availability(device
, *ALBuf
, size
))
341 throw al::context_error
{AL_OUT_OF_MEMORY
,
342 "Out of X-RAM memory (avail: %u, needed: %u)", device
.eax_x_ram_free_size
, size
};
346 /* This could reallocate only when increasing the size or the new size is
347 * less than half the current, but then the buffer's AL_SIZE would not be
348 * very reliable for accounting buffer memory usage, and reporting the real
349 * size could cause problems for apps that use AL_SIZE to try to get the
350 * buffer's play length.
352 if(newsize
!= ALBuf
->mDataStorage
.size())
354 auto newdata
= decltype(ALBuf
->mDataStorage
)(newsize
, std::byte
{});
355 if((access
&AL_PRESERVE_DATA_BIT_SOFT
))
357 const size_t tocopy
{std::min(newdata
.size(), ALBuf
->mDataStorage
.size())};
358 std::copy_n(ALBuf
->mDataStorage
.begin(), tocopy
, newdata
.begin());
360 newdata
.swap(ALBuf
->mDataStorage
);
362 ALBuf
->mData
= ALBuf
->mDataStorage
;
364 eax_x_ram_clear(*context
->mALDevice
, *ALBuf
);
367 if(SrcData
!= nullptr && !ALBuf
->mData
.empty())
368 std::copy_n(SrcData
, blocks
*BlockSize
, ALBuf
->mData
.begin());
369 ALBuf
->mBlockAlign
= (DstType
== FmtIMA4
|| DstType
== FmtMSADPCM
) ? align
: 1;
371 ALBuf
->OriginalSize
= size
;
373 ALBuf
->Access
= access
;
375 ALBuf
->mSampleRate
= static_cast<ALuint
>(freq
);
376 ALBuf
->mChannels
= DstChannels
;
377 ALBuf
->mType
= DstType
;
378 ALBuf
->mAmbiOrder
= ambiorder
;
380 ALBuf
->mCallback
= nullptr;
381 ALBuf
->mUserData
= nullptr;
383 ALBuf
->mSampleLen
= blocks
* align
;
384 ALBuf
->mLoopStart
= 0;
385 ALBuf
->mLoopEnd
= ALBuf
->mSampleLen
;
388 if(eax_g_is_enabled
&& ALBuf
->eax_x_ram_mode
== EaxStorage::Hardware
)
389 eax_x_ram_apply(*context
->mALDevice
, *ALBuf
);
393 /** Prepares the buffer to use the specified callback, using the specified format. */
394 void PrepareCallback(ALCcontext
*context
[[maybe_unused
]], ALbuffer
*ALBuf
, ALsizei freq
,
395 const FmtChannels DstChannels
, const FmtType DstType
, ALBUFFERCALLBACKTYPESOFT callback
,
398 if(ALBuf
->ref
.load(std::memory_order_relaxed
) != 0 || ALBuf
->MappedAccess
!= 0)
399 throw al::context_error
{AL_INVALID_OPERATION
, "Modifying callback for in-use buffer %u",
402 const ALuint ambiorder
{IsBFormat(DstChannels
) ? ALBuf
->UnpackAmbiOrder
:
403 (IsUHJ(DstChannels
) ? 1 : 0)};
405 const ALuint unpackalign
{ALBuf
->UnpackAlign
};
406 const ALuint align
{SanitizeAlignment(DstType
, unpackalign
)};
408 throw al::context_error
{AL_INVALID_VALUE
, "Invalid unpack alignment %u for %s samples",
409 unpackalign
, NameFromFormat(DstType
)};
411 const ALuint BlockSize
{ChannelsFromFmt(DstChannels
, ambiorder
) *
412 ((DstType
== FmtIMA4
) ? (align
-1)/2 + 4 :
413 (DstType
== FmtMSADPCM
) ? (align
-2)/2 + 7 :
414 (align
* BytesFromFmt(DstType
)))};
416 /* The maximum number of samples a callback buffer may need to store is a
417 * full mixing line * max pitch * channel count, since it may need to hold
418 * a full line's worth of sample frames before downsampling. An additional
419 * MaxResamplerEdge is needed for "future" samples during resampling (the
420 * voice will hold a history for the past samples).
422 static constexpr size_t line_size
{DeviceBase::MixerLineSize
*MaxPitch
+ MaxResamplerEdge
};
423 const size_t line_blocks
{(line_size
+ align
-1) / align
};
425 using BufferVectorType
= decltype(ALBuf
->mDataStorage
);
426 BufferVectorType(line_blocks
*BlockSize
).swap(ALBuf
->mDataStorage
);
427 ALBuf
->mData
= ALBuf
->mDataStorage
;
430 eax_x_ram_clear(*context
->mALDevice
, *ALBuf
);
433 ALBuf
->mCallback
= callback
;
434 ALBuf
->mUserData
= userptr
;
436 ALBuf
->OriginalSize
= 0;
439 ALBuf
->mBlockAlign
= (DstType
== FmtIMA4
|| DstType
== FmtMSADPCM
) ? align
: 1;
440 ALBuf
->mSampleRate
= static_cast<ALuint
>(freq
);
441 ALBuf
->mChannels
= DstChannels
;
442 ALBuf
->mType
= DstType
;
443 ALBuf
->mAmbiOrder
= ambiorder
;
445 ALBuf
->mSampleLen
= 0;
446 ALBuf
->mLoopStart
= 0;
447 ALBuf
->mLoopEnd
= ALBuf
->mSampleLen
;
450 /** Prepares the buffer to use caller-specified storage. */
451 void PrepareUserPtr(ALCcontext
*context
[[maybe_unused
]], ALbuffer
*ALBuf
, ALsizei freq
,
452 const FmtChannels DstChannels
, const FmtType DstType
, std::byte
*sdata
, const ALuint sdatalen
)
454 if(ALBuf
->ref
.load(std::memory_order_relaxed
) != 0 || ALBuf
->MappedAccess
!= 0)
455 throw al::context_error
{AL_INVALID_OPERATION
, "Modifying storage for in-use buffer %u",
458 const ALuint unpackalign
{ALBuf
->UnpackAlign
};
459 const ALuint align
{SanitizeAlignment(DstType
, unpackalign
)};
461 throw al::context_error
{AL_INVALID_VALUE
, "Invalid unpack alignment %u for %s samples",
462 unpackalign
, NameFromFormat(DstType
)};
464 auto get_type_alignment
= [](const FmtType type
) noexcept
-> ALuint
466 /* NOTE: This only needs to be the required alignment for the CPU to
467 * read/write the given sample type in the mixer.
471 case FmtUByte
: return alignof(ALubyte
);
472 case FmtShort
: return alignof(ALshort
);
473 case FmtInt
: return alignof(ALint
);
474 case FmtFloat
: return alignof(ALfloat
);
475 case FmtDouble
: return alignof(ALdouble
);
476 case FmtMulaw
: return alignof(ALubyte
);
477 case FmtAlaw
: return alignof(ALubyte
);
479 case FmtMSADPCM
: break;
483 const auto typealign
= get_type_alignment(DstType
);
484 if((reinterpret_cast<uintptr_t>(sdata
) & (typealign
-1)) != 0)
485 throw al::context_error
{AL_INVALID_VALUE
, "Pointer %p is misaligned for %s samples (%u)",
486 static_cast<void*>(sdata
), NameFromFormat(DstType
), typealign
};
488 const ALuint ambiorder
{IsBFormat(DstChannels
) ? ALBuf
->UnpackAmbiOrder
:
489 (IsUHJ(DstChannels
) ? 1 : 0)};
491 /* Convert the size in bytes to blocks using the unpack block alignment. */
492 const ALuint NumChannels
{ChannelsFromFmt(DstChannels
, ambiorder
)};
493 const ALuint BlockSize
{NumChannels
*
494 ((DstType
== FmtIMA4
) ? (align
-1)/2 + 4 :
495 (DstType
== FmtMSADPCM
) ? (align
-2)/2 + 7 :
496 (align
* BytesFromFmt(DstType
)))};
497 if((sdatalen
%BlockSize
) != 0)
498 throw al::context_error
{AL_INVALID_VALUE
,
499 "Data size %u is not a multiple of frame size %u (%u unpack alignment)",
500 sdatalen
, BlockSize
, align
};
501 const ALuint blocks
{sdatalen
/ BlockSize
};
503 if(blocks
> std::numeric_limits
<ALsizei
>::max()/align
)
504 throw al::context_error
{AL_OUT_OF_MEMORY
,
505 "Buffer size overflow, %d blocks x %d samples per block", blocks
, align
};
506 if(blocks
> std::numeric_limits
<size_t>::max()/BlockSize
)
507 throw al::context_error
{AL_OUT_OF_MEMORY
,
508 "Buffer size overflow, %d frames x %d bytes per frame", blocks
, BlockSize
};
511 if(ALBuf
->eax_x_ram_mode
== EaxStorage::Hardware
)
513 ALCdevice
&device
= *context
->mALDevice
;
514 if(!eax_x_ram_check_availability(device
, *ALBuf
, sdatalen
))
515 throw al::context_error
{AL_OUT_OF_MEMORY
,
516 "Out of X-RAM memory (avail: %u, needed: %u)", device
.eax_x_ram_free_size
,
521 decltype(ALBuf
->mDataStorage
){}.swap(ALBuf
->mDataStorage
);
522 ALBuf
->mData
= {static_cast<std::byte
*>(sdata
), sdatalen
};
525 eax_x_ram_clear(*context
->mALDevice
, *ALBuf
);
528 ALBuf
->mCallback
= nullptr;
529 ALBuf
->mUserData
= nullptr;
531 ALBuf
->OriginalSize
= sdatalen
;
534 ALBuf
->mBlockAlign
= (DstType
== FmtIMA4
|| DstType
== FmtMSADPCM
) ? align
: 1;
535 ALBuf
->mSampleRate
= static_cast<ALuint
>(freq
);
536 ALBuf
->mChannels
= DstChannels
;
537 ALBuf
->mType
= DstType
;
538 ALBuf
->mAmbiOrder
= ambiorder
;
540 ALBuf
->mSampleLen
= blocks
* align
;
541 ALBuf
->mLoopStart
= 0;
542 ALBuf
->mLoopEnd
= ALBuf
->mSampleLen
;
545 if(ALBuf
->eax_x_ram_mode
== EaxStorage::Hardware
)
546 eax_x_ram_apply(*context
->mALDevice
, *ALBuf
);
551 struct DecompResult
{ FmtChannels channels
; FmtType type
; };
552 auto DecomposeUserFormat(ALenum format
) noexcept
-> std::optional
<DecompResult
>
558 static constexpr std::array UserFmtList
{
559 FormatMap
{AL_FORMAT_MONO8
, {FmtMono
, FmtUByte
} },
560 FormatMap
{AL_FORMAT_MONO16
, {FmtMono
, FmtShort
} },
561 FormatMap
{AL_FORMAT_MONO_I32
, {FmtMono
, FmtInt
} },
562 FormatMap
{AL_FORMAT_MONO_FLOAT32
, {FmtMono
, FmtFloat
} },
563 FormatMap
{AL_FORMAT_MONO_DOUBLE_EXT
, {FmtMono
, FmtDouble
} },
564 FormatMap
{AL_FORMAT_MONO_IMA4
, {FmtMono
, FmtIMA4
} },
565 FormatMap
{AL_FORMAT_MONO_MSADPCM_SOFT
, {FmtMono
, FmtMSADPCM
}},
566 FormatMap
{AL_FORMAT_MONO_MULAW
, {FmtMono
, FmtMulaw
} },
567 FormatMap
{AL_FORMAT_MONO_ALAW_EXT
, {FmtMono
, FmtAlaw
} },
569 FormatMap
{AL_FORMAT_STEREO8
, {FmtStereo
, FmtUByte
} },
570 FormatMap
{AL_FORMAT_STEREO16
, {FmtStereo
, FmtShort
} },
571 FormatMap
{AL_FORMAT_STEREO_I32
, {FmtStereo
, FmtInt
} },
572 FormatMap
{AL_FORMAT_STEREO_FLOAT32
, {FmtStereo
, FmtFloat
} },
573 FormatMap
{AL_FORMAT_STEREO_DOUBLE_EXT
, {FmtStereo
, FmtDouble
} },
574 FormatMap
{AL_FORMAT_STEREO_IMA4
, {FmtStereo
, FmtIMA4
} },
575 FormatMap
{AL_FORMAT_STEREO_MSADPCM_SOFT
, {FmtStereo
, FmtMSADPCM
}},
576 FormatMap
{AL_FORMAT_STEREO_MULAW
, {FmtStereo
, FmtMulaw
} },
577 FormatMap
{AL_FORMAT_STEREO_ALAW_EXT
, {FmtStereo
, FmtAlaw
} },
579 FormatMap
{AL_FORMAT_REAR8
, {FmtRear
, FmtUByte
}},
580 FormatMap
{AL_FORMAT_REAR16
, {FmtRear
, FmtShort
}},
581 FormatMap
{AL_FORMAT_REAR32
, {FmtRear
, FmtFloat
}},
582 FormatMap
{AL_FORMAT_REAR_I32
, {FmtRear
, FmtInt
} },
583 FormatMap
{AL_FORMAT_REAR_FLOAT32
, {FmtRear
, FmtFloat
}},
584 FormatMap
{AL_FORMAT_REAR_MULAW
, {FmtRear
, FmtMulaw
}},
586 FormatMap
{AL_FORMAT_QUAD8_LOKI
, {FmtQuad
, FmtUByte
}},
587 FormatMap
{AL_FORMAT_QUAD16_LOKI
, {FmtQuad
, FmtShort
}},
589 FormatMap
{AL_FORMAT_QUAD8
, {FmtQuad
, FmtUByte
}},
590 FormatMap
{AL_FORMAT_QUAD16
, {FmtQuad
, FmtShort
}},
591 FormatMap
{AL_FORMAT_QUAD32
, {FmtQuad
, FmtFloat
}},
592 FormatMap
{AL_FORMAT_QUAD_I32
, {FmtQuad
, FmtInt
} },
593 FormatMap
{AL_FORMAT_QUAD_FLOAT32
, {FmtQuad
, FmtFloat
}},
594 FormatMap
{AL_FORMAT_QUAD_MULAW
, {FmtQuad
, FmtMulaw
}},
596 FormatMap
{AL_FORMAT_51CHN8
, {FmtX51
, FmtUByte
}},
597 FormatMap
{AL_FORMAT_51CHN16
, {FmtX51
, FmtShort
}},
598 FormatMap
{AL_FORMAT_51CHN32
, {FmtX51
, FmtFloat
}},
599 FormatMap
{AL_FORMAT_51CHN_I32
, {FmtX51
, FmtInt
} },
600 FormatMap
{AL_FORMAT_51CHN_FLOAT32
, {FmtX51
, FmtFloat
}},
601 FormatMap
{AL_FORMAT_51CHN_MULAW
, {FmtX51
, FmtMulaw
}},
603 FormatMap
{AL_FORMAT_61CHN8
, {FmtX61
, FmtUByte
}},
604 FormatMap
{AL_FORMAT_61CHN16
, {FmtX61
, FmtShort
}},
605 FormatMap
{AL_FORMAT_61CHN32
, {FmtX61
, FmtFloat
}},
606 FormatMap
{AL_FORMAT_61CHN_I32
, {FmtX61
, FmtInt
} },
607 FormatMap
{AL_FORMAT_61CHN_FLOAT32
, {FmtX61
, FmtFloat
}},
608 FormatMap
{AL_FORMAT_61CHN_MULAW
, {FmtX61
, FmtMulaw
}},
610 FormatMap
{AL_FORMAT_71CHN8
, {FmtX71
, FmtUByte
}},
611 FormatMap
{AL_FORMAT_71CHN16
, {FmtX71
, FmtShort
}},
612 FormatMap
{AL_FORMAT_71CHN32
, {FmtX71
, FmtFloat
}},
613 FormatMap
{AL_FORMAT_71CHN_I32
, {FmtX71
, FmtInt
} },
614 FormatMap
{AL_FORMAT_71CHN_FLOAT32
, {FmtX71
, FmtFloat
}},
615 FormatMap
{AL_FORMAT_71CHN_MULAW
, {FmtX71
, FmtMulaw
}},
617 FormatMap
{AL_FORMAT_BFORMAT2D_8
, {FmtBFormat2D
, FmtUByte
}},
618 FormatMap
{AL_FORMAT_BFORMAT2D_16
, {FmtBFormat2D
, FmtShort
}},
619 FormatMap
{AL_FORMAT_BFORMAT2D_I32
, {FmtBFormat2D
, FmtInt
} },
620 FormatMap
{AL_FORMAT_BFORMAT2D_FLOAT32
, {FmtBFormat2D
, FmtFloat
}},
621 FormatMap
{AL_FORMAT_BFORMAT2D_MULAW
, {FmtBFormat2D
, FmtMulaw
}},
623 FormatMap
{AL_FORMAT_BFORMAT3D_8
, {FmtBFormat3D
, FmtUByte
}},
624 FormatMap
{AL_FORMAT_BFORMAT3D_16
, {FmtBFormat3D
, FmtShort
}},
625 FormatMap
{AL_FORMAT_BFORMAT2D_I32
, {FmtBFormat3D
, FmtInt
} },
626 FormatMap
{AL_FORMAT_BFORMAT3D_FLOAT32
, {FmtBFormat3D
, FmtFloat
}},
627 FormatMap
{AL_FORMAT_BFORMAT3D_MULAW
, {FmtBFormat3D
, FmtMulaw
}},
629 FormatMap
{AL_FORMAT_UHJ2CHN8_SOFT
, {FmtUHJ2
, FmtUByte
} },
630 FormatMap
{AL_FORMAT_UHJ2CHN16_SOFT
, {FmtUHJ2
, FmtShort
} },
631 FormatMap
{AL_FORMAT_UHJ2CHN_I32_SOFT
, {FmtUHJ2
, FmtInt
} },
632 FormatMap
{AL_FORMAT_UHJ2CHN_FLOAT32_SOFT
, {FmtUHJ2
, FmtFloat
} },
633 FormatMap
{AL_FORMAT_UHJ2CHN_MULAW_SOFT
, {FmtUHJ2
, FmtMulaw
} },
634 FormatMap
{AL_FORMAT_UHJ2CHN_ALAW_SOFT
, {FmtUHJ2
, FmtAlaw
} },
635 FormatMap
{AL_FORMAT_UHJ2CHN_IMA4_SOFT
, {FmtUHJ2
, FmtIMA4
} },
636 FormatMap
{AL_FORMAT_UHJ2CHN_MSADPCM_SOFT
, {FmtUHJ2
, FmtMSADPCM
}},
638 FormatMap
{AL_FORMAT_UHJ3CHN8_SOFT
, {FmtUHJ3
, FmtUByte
}},
639 FormatMap
{AL_FORMAT_UHJ3CHN16_SOFT
, {FmtUHJ3
, FmtShort
}},
640 FormatMap
{AL_FORMAT_UHJ3CHN_I32_SOFT
, {FmtUHJ3
, FmtInt
} },
641 FormatMap
{AL_FORMAT_UHJ3CHN_FLOAT32_SOFT
, {FmtUHJ3
, FmtFloat
}},
642 FormatMap
{AL_FORMAT_UHJ3CHN_MULAW_SOFT
, {FmtUHJ3
, FmtMulaw
}},
643 FormatMap
{AL_FORMAT_UHJ3CHN_ALAW_SOFT
, {FmtUHJ3
, FmtAlaw
} },
645 FormatMap
{AL_FORMAT_UHJ4CHN8_SOFT
, {FmtUHJ4
, FmtUByte
}},
646 FormatMap
{AL_FORMAT_UHJ4CHN16_SOFT
, {FmtUHJ4
, FmtShort
}},
647 FormatMap
{AL_FORMAT_UHJ4CHN_I32_SOFT
, {FmtUHJ4
, FmtInt
} },
648 FormatMap
{AL_FORMAT_UHJ4CHN_FLOAT32_SOFT
, {FmtUHJ4
, FmtFloat
}},
649 FormatMap
{AL_FORMAT_UHJ4CHN_MULAW_SOFT
, {FmtUHJ4
, FmtMulaw
}},
650 FormatMap
{AL_FORMAT_UHJ4CHN_ALAW_SOFT
, {FmtUHJ4
, FmtAlaw
} },
653 auto iter
= std::find_if(UserFmtList
.cbegin(), UserFmtList
.cend(),
654 [format
](const FormatMap
&fmt
) noexcept
{ return fmt
.format
== format
; });
655 if(iter
!= UserFmtList
.cend())
663 AL_API
DECL_FUNC2(void, alGenBuffers
, ALsizei
,n
, ALuint
*,buffers
)
664 FORCE_ALIGN
void AL_APIENTRY
alGenBuffersDirect(ALCcontext
*context
, ALsizei n
, ALuint
*buffers
) noexcept
667 throw al::context_error
{AL_INVALID_VALUE
, "Generating %d buffers", n
};
668 if(n
<= 0) UNLIKELY
return;
670 ALCdevice
*device
{context
->mALDevice
.get()};
671 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
673 const al::span bids
{buffers
, static_cast<ALuint
>(n
)};
674 if(!EnsureBuffers(device
, bids
.size()))
675 throw al::context_error
{AL_OUT_OF_MEMORY
, "Failed to allocate %d buffer%s", n
,
676 (n
== 1) ? "" : "s"};
678 std::generate(bids
.begin(), bids
.end(), [device
]{ return AllocBuffer(device
)->id
; });
680 catch(al::context_error
& e
) {
681 context
->setError(e
.errorCode(), "%s", e
.what());
684 AL_API
DECL_FUNC2(void, alDeleteBuffers
, ALsizei
,n
, const ALuint
*,buffers
)
685 FORCE_ALIGN
void AL_APIENTRY
alDeleteBuffersDirect(ALCcontext
*context
, ALsizei n
,
686 const ALuint
*buffers
) noexcept
689 throw al::context_error
{AL_INVALID_VALUE
, "Deleting %d buffers", n
};
690 if(n
<= 0) UNLIKELY
return;
692 ALCdevice
*device
{context
->mALDevice
.get()};
693 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
695 /* First try to find any buffers that are invalid or in-use. */
696 auto validate_buffer
= [device
](const ALuint bid
)
699 ALbuffer
*ALBuf
{LookupBuffer(device
, bid
)};
701 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", bid
};
702 if(ALBuf
->ref
.load(std::memory_order_relaxed
) != 0)
703 throw al::context_error
{AL_INVALID_OPERATION
, "Deleting in-use buffer %u", bid
};
706 const al::span bids
{buffers
, static_cast<ALuint
>(n
)};
707 std::for_each(bids
.begin(), bids
.end(), validate_buffer
);
709 /* All good. Delete non-0 buffer IDs. */
710 auto delete_buffer
= [device
](const ALuint bid
) -> void
712 if(ALbuffer
*buffer
{bid
? LookupBuffer(device
, bid
) : nullptr})
713 FreeBuffer(device
, buffer
);
715 std::for_each(bids
.begin(), bids
.end(), delete_buffer
);
717 catch(al::context_error
& e
) {
718 context
->setError(e
.errorCode(), "%s", e
.what());
721 AL_API
DECL_FUNC1(ALboolean
, alIsBuffer
, ALuint
,buffer
)
722 FORCE_ALIGN ALboolean AL_APIENTRY
alIsBufferDirect(ALCcontext
*context
, ALuint buffer
) noexcept
724 ALCdevice
*device
{context
->mALDevice
.get()};
725 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
726 if(!buffer
|| LookupBuffer(device
, buffer
))
732 AL_API
void AL_APIENTRY
alBufferData(ALuint buffer
, ALenum format
, const ALvoid
*data
, ALsizei size
, ALsizei freq
) noexcept
734 auto context
= GetContextRef();
735 if(!context
) UNLIKELY
return;
736 alBufferStorageDirectSOFT(context
.get(), buffer
, format
, data
, size
, freq
, 0);
739 FORCE_ALIGN
void AL_APIENTRY
alBufferDataDirect(ALCcontext
*context
, ALuint buffer
, ALenum format
, const ALvoid
*data
, ALsizei size
, ALsizei freq
) noexcept
740 { alBufferStorageDirectSOFT(context
, buffer
, format
, data
, size
, freq
, 0); }
742 AL_API
DECL_FUNCEXT6(void, alBufferStorage
,SOFT
, ALuint
,buffer
, ALenum
,format
, const ALvoid
*,data
, ALsizei
,size
, ALsizei
,freq
, ALbitfieldSOFT
,flags
)
743 FORCE_ALIGN
void AL_APIENTRY
alBufferStorageDirectSOFT(ALCcontext
*context
, ALuint buffer
,
744 ALenum format
, const ALvoid
*data
, ALsizei size
, ALsizei freq
, ALbitfieldSOFT flags
) noexcept
746 ALCdevice
*device
{context
->mALDevice
.get()};
747 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
749 ALbuffer
*albuf
{LookupBuffer(device
, buffer
)};
751 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
753 throw al::context_error
{AL_INVALID_VALUE
, "Negative storage size %d", size
};
755 throw al::context_error
{AL_INVALID_VALUE
, "Invalid sample rate %d", freq
};
756 if((flags
&INVALID_STORAGE_MASK
) != 0)
757 throw al::context_error
{AL_INVALID_VALUE
, "Invalid storage flags 0x%x",
758 flags
&INVALID_STORAGE_MASK
};
759 if((flags
&AL_MAP_PERSISTENT_BIT_SOFT
) && !(flags
&MAP_READ_WRITE_FLAGS
))
760 throw al::context_error
{AL_INVALID_VALUE
,
761 "Declaring persistently mapped storage without read or write access"};
763 auto usrfmt
= DecomposeUserFormat(format
);
765 throw al::context_error
{AL_INVALID_ENUM
, "Invalid format 0x%04x", format
};
767 LoadData(context
, albuf
, freq
, static_cast<ALuint
>(size
), usrfmt
->channels
, usrfmt
->type
,
768 static_cast<const std::byte
*>(data
), flags
);
770 catch(al::context_error
& e
) {
771 context
->setError(e
.errorCode(), "%s", e
.what());
774 FORCE_ALIGN
DECL_FUNC5(void, alBufferDataStatic
, ALuint
,buffer
, ALenum
,format
, ALvoid
*,data
, ALsizei
,size
, ALsizei
,freq
)
775 FORCE_ALIGN
void AL_APIENTRY
alBufferDataStaticDirect(ALCcontext
*context
, const ALuint buffer
,
776 ALenum format
, ALvoid
*data
, ALsizei size
, ALsizei freq
) noexcept
778 ALCdevice
*device
{context
->mALDevice
.get()};
779 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
781 ALbuffer
*albuf
{LookupBuffer(device
, buffer
)};
783 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
785 throw al::context_error
{AL_INVALID_VALUE
, "Negative storage size %d", size
};
787 throw al::context_error
{AL_INVALID_VALUE
, "Invalid sample rate %d", freq
};
789 auto usrfmt
= DecomposeUserFormat(format
);
791 throw al::context_error
{AL_INVALID_ENUM
, "Invalid format 0x%04x", format
};
793 PrepareUserPtr(context
, albuf
, freq
, usrfmt
->channels
, usrfmt
->type
,
794 static_cast<std::byte
*>(data
), static_cast<ALuint
>(size
));
796 catch(al::context_error
& e
) {
797 context
->setError(e
.errorCode(), "%s", e
.what());
800 AL_API
DECL_FUNCEXT4(void*, alMapBuffer
,SOFT
, ALuint
,buffer
, ALsizei
,offset
, ALsizei
,length
, ALbitfieldSOFT
,access
)
801 FORCE_ALIGN
void* AL_APIENTRY
alMapBufferDirectSOFT(ALCcontext
*context
, ALuint buffer
,
802 ALsizei offset
, ALsizei length
, ALbitfieldSOFT access
) noexcept
804 ALCdevice
*device
{context
->mALDevice
.get()};
805 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
807 ALbuffer
*albuf
{LookupBuffer(device
, buffer
)};
809 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
810 if((access
&INVALID_MAP_FLAGS
) != 0)
811 throw al::context_error
{AL_INVALID_VALUE
, "Invalid map flags 0x%x",
812 access
&INVALID_MAP_FLAGS
};
813 if(!(access
&MAP_READ_WRITE_FLAGS
))
814 throw al::context_error
{AL_INVALID_VALUE
, "Mapping buffer %u without read or write access",
817 const ALbitfieldSOFT unavailable
{(albuf
->Access
^access
) & access
};
818 if(albuf
->ref
.load(std::memory_order_relaxed
) != 0 && !(access
&AL_MAP_PERSISTENT_BIT_SOFT
))
819 throw al::context_error
{AL_INVALID_OPERATION
,
820 "Mapping in-use buffer %u without persistent mapping", buffer
};
821 if(albuf
->MappedAccess
!= 0)
822 throw al::context_error
{AL_INVALID_OPERATION
, "Mapping already-mapped buffer %u", buffer
};
823 if((unavailable
&AL_MAP_READ_BIT_SOFT
))
824 throw al::context_error
{AL_INVALID_VALUE
,
825 "Mapping buffer %u for reading without read access", buffer
};
826 if((unavailable
&AL_MAP_WRITE_BIT_SOFT
))
827 throw al::context_error
{AL_INVALID_VALUE
,
828 "Mapping buffer %u for writing without write access", buffer
};
829 if((unavailable
&AL_MAP_PERSISTENT_BIT_SOFT
))
830 throw al::context_error
{AL_INVALID_VALUE
,
831 "Mapping buffer %u persistently without persistent access", buffer
};
832 if(offset
< 0 || length
<= 0 || static_cast<ALuint
>(offset
) >= albuf
->OriginalSize
833 || static_cast<ALuint
>(length
) > albuf
->OriginalSize
- static_cast<ALuint
>(offset
))
834 throw al::context_error
{AL_INVALID_VALUE
, "Mapping invalid range %d+%d for buffer %u",
835 offset
, length
, buffer
};
837 void *retval
{albuf
->mData
.data() + offset
};
838 albuf
->MappedAccess
= access
;
839 albuf
->MappedOffset
= offset
;
840 albuf
->MappedSize
= length
;
843 catch(al::context_error
& e
) {
844 context
->setError(e
.errorCode(), "%s", e
.what());
848 AL_API
DECL_FUNCEXT1(void, alUnmapBuffer
,SOFT
, ALuint
,buffer
)
849 FORCE_ALIGN
void AL_APIENTRY
alUnmapBufferDirectSOFT(ALCcontext
*context
, ALuint buffer
) noexcept
851 ALCdevice
*device
{context
->mALDevice
.get()};
852 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
854 ALbuffer
*albuf
{LookupBuffer(device
, buffer
)};
856 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
857 if(albuf
->MappedAccess
== 0)
858 throw al::context_error
{AL_INVALID_OPERATION
, "Unmapping unmapped buffer %u", buffer
};
860 albuf
->MappedAccess
= 0;
861 albuf
->MappedOffset
= 0;
862 albuf
->MappedSize
= 0;
864 catch(al::context_error
& e
) {
865 context
->setError(e
.errorCode(), "%s", e
.what());
868 AL_API
DECL_FUNCEXT3(void, alFlushMappedBuffer
,SOFT
, ALuint
,buffer
, ALsizei
,offset
, ALsizei
,length
)
869 FORCE_ALIGN
void AL_APIENTRY
alFlushMappedBufferDirectSOFT(ALCcontext
*context
, ALuint buffer
,
870 ALsizei offset
, ALsizei length
) noexcept
872 ALCdevice
*device
{context
->mALDevice
.get()};
873 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
875 ALbuffer
*albuf
{LookupBuffer(device
, buffer
)};
877 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
878 if(!(albuf
->MappedAccess
&AL_MAP_WRITE_BIT_SOFT
))
879 throw al::context_error
{AL_INVALID_OPERATION
,
880 "Flushing buffer %u while not mapped for writing", buffer
};
881 if(offset
< albuf
->MappedOffset
|| length
<= 0
882 || offset
>= albuf
->MappedOffset
+albuf
->MappedSize
883 || length
> albuf
->MappedOffset
+albuf
->MappedSize
-offset
)
884 throw al::context_error
{AL_INVALID_VALUE
, "Flushing invalid range %d+%d on buffer %u",
885 offset
, length
, buffer
};
887 /* FIXME: Need to use some method of double-buffering for the mixer and app
888 * to hold separate memory, which can be safely transferred asynchronously.
889 * Currently we just say the app shouldn't write where OpenAL's reading,
890 * and hope for the best...
892 std::atomic_thread_fence(std::memory_order_seq_cst
);
894 catch(al::context_error
& e
) {
895 context
->setError(e
.errorCode(), "%s", e
.what());
898 AL_API
DECL_FUNCEXT5(void, alBufferSubData
,SOFT
, ALuint
,buffer
, ALenum
,format
, const ALvoid
*,data
, ALsizei
,offset
, ALsizei
,length
)
899 FORCE_ALIGN
void AL_APIENTRY
alBufferSubDataDirectSOFT(ALCcontext
*context
, ALuint buffer
,
900 ALenum format
, const ALvoid
*data
, ALsizei offset
, ALsizei length
) noexcept
902 ALCdevice
*device
{context
->mALDevice
.get()};
903 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
905 ALbuffer
*albuf
{LookupBuffer(device
, buffer
)};
907 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
909 auto usrfmt
= DecomposeUserFormat(format
);
911 throw al::context_error
{AL_INVALID_ENUM
, "Invalid format 0x%04x", format
};
913 const ALuint unpack_align
{albuf
->UnpackAlign
};
914 const ALuint align
{SanitizeAlignment(usrfmt
->type
, unpack_align
)};
916 throw al::context_error
{AL_INVALID_VALUE
, "Invalid unpack alignment %u", unpack_align
};
917 if(usrfmt
->channels
!= albuf
->mChannels
|| usrfmt
->type
!= albuf
->mType
)
918 throw al::context_error
{AL_INVALID_ENUM
, "Unpacking data with mismatched format"};
919 if(align
!= albuf
->mBlockAlign
)
920 throw al::context_error
{AL_INVALID_VALUE
,
921 "Unpacking data with alignment %u does not match original alignment %u", align
,
923 if(albuf
->isBFormat() && albuf
->UnpackAmbiOrder
!= albuf
->mAmbiOrder
)
924 throw al::context_error
{AL_INVALID_VALUE
,
925 "Unpacking data with mismatched ambisonic order"};
926 if(albuf
->MappedAccess
!= 0)
927 throw al::context_error
{AL_INVALID_OPERATION
, "Unpacking data into mapped buffer %u",
930 const ALuint num_chans
{albuf
->channelsFromFmt()};
931 const ALuint byte_align
{
932 (albuf
->mType
== FmtIMA4
) ? ((align
-1)/2 + 4) * num_chans
:
933 (albuf
->mType
== FmtMSADPCM
) ? ((align
-2)/2 + 7) * num_chans
:
934 (align
* albuf
->bytesFromFmt() * num_chans
)};
936 if(offset
< 0 || length
< 0 || static_cast<ALuint
>(offset
) > albuf
->OriginalSize
937 || static_cast<ALuint
>(length
) > albuf
->OriginalSize
-static_cast<ALuint
>(offset
))
938 throw al::context_error
{AL_INVALID_VALUE
, "Invalid data sub-range %d+%d on buffer %u",
939 offset
, length
, buffer
};
940 if((static_cast<ALuint
>(offset
)%byte_align
) != 0)
941 throw al::context_error
{AL_INVALID_VALUE
,
942 "Sub-range offset %d is not a multiple of frame size %d (%d unpack alignment)",
943 offset
, byte_align
, align
};
944 if((static_cast<ALuint
>(length
)%byte_align
) != 0)
945 throw al::context_error
{AL_INVALID_VALUE
,
946 "Sub-range length %d is not a multiple of frame size %d (%d unpack alignment)",
947 length
, byte_align
, align
};
949 std::memcpy(albuf
->mData
.data()+offset
, data
, static_cast<ALuint
>(length
));
951 catch(al::context_error
& e
) {
952 context
->setError(e
.errorCode(), "%s", e
.what());
956 AL_API
DECL_FUNC3(void, alBufferf
, ALuint
,buffer
, ALenum
,param
, ALfloat
,value
)
957 FORCE_ALIGN
void AL_APIENTRY
alBufferfDirect(ALCcontext
*context
, ALuint buffer
, ALenum param
,
958 ALfloat value
[[maybe_unused
]]) noexcept
960 ALCdevice
*device
{context
->mALDevice
.get()};
961 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
963 if(LookupBuffer(device
, buffer
) == nullptr)
964 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
969 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer float property 0x%04x", param
};
971 catch(al::context_error
& e
) {
972 context
->setError(e
.errorCode(), "%s", e
.what());
975 AL_API
DECL_FUNC5(void, alBuffer3f
, ALuint
,buffer
, ALenum
,param
, ALfloat
,value1
, ALfloat
,value2
, ALfloat
,value3
)
976 FORCE_ALIGN
void AL_APIENTRY
alBuffer3fDirect(ALCcontext
*context
, ALuint buffer
, ALenum param
,
977 ALfloat value1
[[maybe_unused
]], ALfloat value2
[[maybe_unused
]],
978 ALfloat value3
[[maybe_unused
]]) noexcept
980 ALCdevice
*device
{context
->mALDevice
.get()};
981 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
983 if(LookupBuffer(device
, buffer
) == nullptr)
984 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
989 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer 3-float property 0x%04x", param
};
991 catch(al::context_error
& e
) {
992 context
->setError(e
.errorCode(), "%s", e
.what());
995 AL_API
DECL_FUNC3(void, alBufferfv
, ALuint
,buffer
, ALenum
,param
, const ALfloat
*,values
)
996 FORCE_ALIGN
void AL_APIENTRY
alBufferfvDirect(ALCcontext
*context
, ALuint buffer
, ALenum param
,
997 const ALfloat
*values
) noexcept
999 ALCdevice
*device
{context
->mALDevice
.get()};
1000 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1002 if(LookupBuffer(device
, buffer
) == nullptr)
1003 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1005 throw al::context_error
{AL_INVALID_VALUE
, "NULL pointer"};
1010 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer float-vector property 0x%04x", param
};
1012 catch(al::context_error
& e
) {
1013 context
->setError(e
.errorCode(), "%s", e
.what());
1017 AL_API
DECL_FUNC3(void, alBufferi
, ALuint
,buffer
, ALenum
,param
, ALint
,value
)
1018 FORCE_ALIGN
void AL_APIENTRY
alBufferiDirect(ALCcontext
*context
, ALuint buffer
, ALenum param
,
1019 ALint value
) noexcept
1021 ALCdevice
*device
{context
->mALDevice
.get()};
1022 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1024 ALbuffer
*albuf
{LookupBuffer(device
, buffer
)};
1026 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1030 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT
:
1032 throw al::context_error
{AL_INVALID_VALUE
, "Invalid unpack block alignment %d", value
};
1033 albuf
->UnpackAlign
= static_cast<ALuint
>(value
);
1036 case AL_PACK_BLOCK_ALIGNMENT_SOFT
:
1038 throw al::context_error
{AL_INVALID_VALUE
, "Invalid pack block alignment %d", value
};
1039 albuf
->PackAlign
= static_cast<ALuint
>(value
);
1042 case AL_AMBISONIC_LAYOUT_SOFT
:
1043 if(albuf
->ref
.load(std::memory_order_relaxed
) != 0)
1044 throw al::context_error
{AL_INVALID_OPERATION
,
1045 "Modifying in-use buffer %u's ambisonic layout", buffer
};
1046 if(const auto layout
= AmbiLayoutFromEnum(value
))
1048 albuf
->mAmbiLayout
= layout
.value();
1051 throw al::context_error
{AL_INVALID_VALUE
, "Invalid unpack ambisonic layout %04x", value
};
1053 case AL_AMBISONIC_SCALING_SOFT
:
1054 if(albuf
->ref
.load(std::memory_order_relaxed
) != 0)
1055 throw al::context_error
{AL_INVALID_OPERATION
,
1056 "Modifying in-use buffer %u's ambisonic scaling", buffer
};
1057 if(const auto scaling
= AmbiScalingFromEnum(value
))
1059 albuf
->mAmbiScaling
= scaling
.value();
1062 throw al::context_error
{AL_INVALID_VALUE
, "Invalid unpack ambisonic scaling %04x", value
};
1064 case AL_UNPACK_AMBISONIC_ORDER_SOFT
:
1065 if(value
< 1 || value
> 14)
1066 throw al::context_error
{AL_INVALID_VALUE
, "Invalid unpack ambisonic order %d", value
};
1067 albuf
->UnpackAmbiOrder
= static_cast<ALuint
>(value
);
1071 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer integer property 0x%04x", param
};
1073 catch(al::context_error
& e
) {
1074 context
->setError(e
.errorCode(), "%s", e
.what());
1077 AL_API
DECL_FUNC5(void, alBuffer3i
, ALuint
,buffer
, ALenum
,param
, ALint
,value1
, ALint
,value2
, ALint
,value3
)
1078 FORCE_ALIGN
void AL_APIENTRY
alBuffer3iDirect(ALCcontext
*context
, ALuint buffer
, ALenum param
,
1079 ALint value1
[[maybe_unused
]], ALint value2
[[maybe_unused
]], ALint value3
[[maybe_unused
]]) noexcept
1081 ALCdevice
*device
{context
->mALDevice
.get()};
1082 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1084 if(LookupBuffer(device
, buffer
) == nullptr)
1085 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1090 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer 3-integer property 0x%04x", param
};
1092 catch(al::context_error
& e
) {
1093 context
->setError(e
.errorCode(), "%s", e
.what());
1096 AL_API
DECL_FUNC3(void, alBufferiv
, ALuint
,buffer
, ALenum
,param
, const ALint
*,values
)
1097 FORCE_ALIGN
void AL_APIENTRY
alBufferivDirect(ALCcontext
*context
, ALuint buffer
, ALenum param
,
1098 const ALint
*values
) noexcept
1101 throw al::context_error
{AL_INVALID_VALUE
, "NULL pointer"};
1105 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT
:
1106 case AL_PACK_BLOCK_ALIGNMENT_SOFT
:
1107 case AL_AMBISONIC_LAYOUT_SOFT
:
1108 case AL_AMBISONIC_SCALING_SOFT
:
1109 case AL_UNPACK_AMBISONIC_ORDER_SOFT
:
1110 alBufferiDirect(context
, buffer
, param
, *values
);
1114 ALCdevice
*device
{context
->mALDevice
.get()};
1115 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1117 ALbuffer
*albuf
{LookupBuffer(device
, buffer
)};
1119 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1123 case AL_LOOP_POINTS_SOFT
:
1124 auto vals
= al::span
{values
, 2_uz
};
1125 if(albuf
->ref
.load(std::memory_order_relaxed
) != 0)
1126 throw al::context_error
{AL_INVALID_OPERATION
,
1127 "Modifying in-use buffer %u's loop points", buffer
};
1128 if(vals
[0] < 0 || vals
[0] >= vals
[1] || static_cast<ALuint
>(vals
[1]) > albuf
->mSampleLen
)
1129 throw al::context_error
{AL_INVALID_VALUE
,
1130 "Invalid loop point range %d -> %d on buffer %u", vals
[0], vals
[1], buffer
};
1132 albuf
->mLoopStart
= static_cast<ALuint
>(vals
[0]);
1133 albuf
->mLoopEnd
= static_cast<ALuint
>(vals
[1]);
1137 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer integer-vector property 0x%04x",
1140 catch(al::context_error
& e
) {
1141 context
->setError(e
.errorCode(), "%s", e
.what());
1145 AL_API
DECL_FUNC3(void, alGetBufferf
, ALuint
,buffer
, ALenum
,param
, ALfloat
*,value
)
1146 FORCE_ALIGN
void AL_APIENTRY
alGetBufferfDirect(ALCcontext
*context
, ALuint buffer
, ALenum param
,
1147 ALfloat
*value
) noexcept
1149 ALCdevice
*device
{context
->mALDevice
.get()};
1150 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1152 ALbuffer
*albuf
{LookupBuffer(device
, buffer
)};
1154 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1156 throw al::context_error
{AL_INVALID_VALUE
, "NULL pointer"};
1160 case AL_SEC_LENGTH_SOFT
:
1161 *value
= (albuf
->mSampleRate
< 1) ? 0.0f
:
1162 (static_cast<float>(albuf
->mSampleLen
) / static_cast<float>(albuf
->mSampleRate
));
1166 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer float property 0x%04x", param
};
1168 catch(al::context_error
& e
) {
1169 context
->setError(e
.errorCode(), "%s", e
.what());
1172 AL_API
DECL_FUNC5(void, alGetBuffer3f
, ALuint
,buffer
, ALenum
,param
, ALfloat
*,value1
, ALfloat
*,value2
, ALfloat
*,value3
)
1173 FORCE_ALIGN
void AL_APIENTRY
alGetBuffer3fDirect(ALCcontext
*context
, ALuint buffer
, ALenum param
,
1174 ALfloat
*value1
, ALfloat
*value2
, ALfloat
*value3
) noexcept
1176 ALCdevice
*device
{context
->mALDevice
.get()};
1177 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1179 if(LookupBuffer(device
, buffer
) == nullptr)
1180 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1181 if(!value1
|| !value2
|| !value3
)
1182 throw al::context_error
{AL_INVALID_VALUE
, "NULL pointer"};
1187 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer 3-float property 0x%04x", param
};
1189 catch(al::context_error
& e
) {
1190 context
->setError(e
.errorCode(), "%s", e
.what());
1193 AL_API
DECL_FUNC3(void, alGetBufferfv
, ALuint
,buffer
, ALenum
,param
, ALfloat
*,values
)
1194 FORCE_ALIGN
void AL_APIENTRY
alGetBufferfvDirect(ALCcontext
*context
, ALuint buffer
, ALenum param
,
1195 ALfloat
*values
) noexcept
1199 case AL_SEC_LENGTH_SOFT
:
1200 alGetBufferfDirect(context
, buffer
, param
, values
);
1204 ALCdevice
*device
{context
->mALDevice
.get()};
1205 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1207 if(LookupBuffer(device
, buffer
) == nullptr)
1208 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1210 throw al::context_error
{AL_INVALID_VALUE
, "NULL pointer"};
1215 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer float-vector property 0x%04x", param
};
1217 catch(al::context_error
& e
) {
1218 context
->setError(e
.errorCode(), "%s", e
.what());
1222 AL_API
DECL_FUNC3(void, alGetBufferi
, ALuint
,buffer
, ALenum
,param
, ALint
*,value
)
1223 FORCE_ALIGN
void AL_APIENTRY
alGetBufferiDirect(ALCcontext
*context
, ALuint buffer
, ALenum param
,
1224 ALint
*value
) noexcept
1226 ALCdevice
*device
{context
->mALDevice
.get()};
1227 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1229 ALbuffer
*albuf
{LookupBuffer(device
, buffer
)};
1231 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1233 throw al::context_error
{AL_INVALID_VALUE
, "NULL pointer"};
1238 *value
= static_cast<ALint
>(albuf
->mSampleRate
);
1242 *value
= (albuf
->mType
== FmtIMA4
|| albuf
->mType
== FmtMSADPCM
) ? 4
1243 : static_cast<ALint
>(albuf
->bytesFromFmt() * 8);
1247 *value
= static_cast<ALint
>(albuf
->channelsFromFmt());
1251 *value
= albuf
->mCallback
? 0 : static_cast<ALint
>(albuf
->mData
.size());
1254 case AL_BYTE_LENGTH_SOFT
:
1255 *value
= static_cast<ALint
>(albuf
->mSampleLen
/ albuf
->mBlockAlign
1256 * albuf
->blockSizeFromFmt());
1259 case AL_SAMPLE_LENGTH_SOFT
:
1260 *value
= static_cast<ALint
>(albuf
->mSampleLen
);
1263 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT
:
1264 *value
= static_cast<ALint
>(albuf
->UnpackAlign
);
1267 case AL_PACK_BLOCK_ALIGNMENT_SOFT
:
1268 *value
= static_cast<ALint
>(albuf
->PackAlign
);
1271 case AL_AMBISONIC_LAYOUT_SOFT
:
1272 *value
= EnumFromAmbiLayout(albuf
->mAmbiLayout
);
1275 case AL_AMBISONIC_SCALING_SOFT
:
1276 *value
= EnumFromAmbiScaling(albuf
->mAmbiScaling
);
1279 case AL_UNPACK_AMBISONIC_ORDER_SOFT
:
1280 *value
= static_cast<int>(albuf
->UnpackAmbiOrder
);
1284 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer integer property 0x%04x", param
};
1286 catch(al::context_error
& e
) {
1287 context
->setError(e
.errorCode(), "%s", e
.what());
1290 AL_API
DECL_FUNC5(void, alGetBuffer3i
, ALuint
,buffer
, ALenum
,param
, ALint
*,value1
, ALint
*,value2
, ALint
*,value3
)
1291 FORCE_ALIGN
void AL_APIENTRY
alGetBuffer3iDirect(ALCcontext
*context
, ALuint buffer
, ALenum param
,
1292 ALint
*value1
, ALint
*value2
, ALint
*value3
) noexcept
1294 ALCdevice
*device
{context
->mALDevice
.get()};
1295 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1297 if(LookupBuffer(device
, buffer
) == nullptr)
1298 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1299 if(!value1
|| !value2
|| !value3
)
1300 throw al::context_error
{AL_INVALID_VALUE
, "NULL pointer"};
1305 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer 3-integer property 0x%04x", param
};
1307 catch(al::context_error
& e
) {
1308 context
->setError(e
.errorCode(), "%s", e
.what());
1311 AL_API
DECL_FUNC3(void, alGetBufferiv
, ALuint
,buffer
, ALenum
,param
, ALint
*,values
)
1312 FORCE_ALIGN
void AL_APIENTRY
alGetBufferivDirect(ALCcontext
*context
, ALuint buffer
, ALenum param
,
1313 ALint
*values
) noexcept
1321 case AL_INTERNAL_FORMAT_SOFT
:
1322 case AL_BYTE_LENGTH_SOFT
:
1323 case AL_SAMPLE_LENGTH_SOFT
:
1324 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT
:
1325 case AL_PACK_BLOCK_ALIGNMENT_SOFT
:
1326 case AL_AMBISONIC_LAYOUT_SOFT
:
1327 case AL_AMBISONIC_SCALING_SOFT
:
1328 case AL_UNPACK_AMBISONIC_ORDER_SOFT
:
1329 alGetBufferiDirect(context
, buffer
, param
, values
);
1333 ALCdevice
*device
{context
->mALDevice
.get()};
1334 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1336 ALbuffer
*albuf
{LookupBuffer(device
, buffer
)};
1338 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1340 throw al::context_error
{AL_INVALID_VALUE
, "NULL pointer"};
1344 case AL_LOOP_POINTS_SOFT
:
1345 auto vals
= al::span
{values
, 2_uz
};
1346 vals
[0] = static_cast<ALint
>(albuf
->mLoopStart
);
1347 vals
[1] = static_cast<ALint
>(albuf
->mLoopEnd
);
1351 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer integer-vector property 0x%04x",
1354 catch(al::context_error
& e
) {
1355 context
->setError(e
.errorCode(), "%s", e
.what());
1359 AL_API
DECL_FUNCEXT5(void, alBufferCallback
,SOFT
, ALuint
,buffer
, ALenum
,format
, ALsizei
,freq
, ALBUFFERCALLBACKTYPESOFT
,callback
, ALvoid
*,userptr
)
1360 FORCE_ALIGN
void AL_APIENTRY
alBufferCallbackDirectSOFT(ALCcontext
*context
, ALuint buffer
,
1361 ALenum format
, ALsizei freq
, ALBUFFERCALLBACKTYPESOFT callback
, ALvoid
*userptr
) noexcept
1363 ALCdevice
*device
{context
->mALDevice
.get()};
1364 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1366 ALbuffer
*albuf
{LookupBuffer(device
, buffer
)};
1368 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1370 throw al::context_error
{AL_INVALID_VALUE
, "Invalid sample rate %d", freq
};
1371 if(callback
== nullptr)
1372 throw al::context_error
{AL_INVALID_VALUE
, "NULL callback"};
1374 auto usrfmt
= DecomposeUserFormat(format
);
1376 throw al::context_error
{AL_INVALID_ENUM
, "Invalid format 0x%04x", format
};
1378 PrepareCallback(context
, albuf
, freq
, usrfmt
->channels
, usrfmt
->type
, callback
, userptr
);
1380 catch(al::context_error
& e
) {
1381 context
->setError(e
.errorCode(), "%s", e
.what());
1384 AL_API
DECL_FUNCEXT3(void, alGetBufferPtr
,SOFT
, ALuint
,buffer
, ALenum
,param
, ALvoid
**,value
)
1385 FORCE_ALIGN
void AL_APIENTRY
alGetBufferPtrDirectSOFT(ALCcontext
*context
, ALuint buffer
,
1386 ALenum param
, ALvoid
**value
) noexcept
1388 ALCdevice
*device
{context
->mALDevice
.get()};
1389 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1391 ALbuffer
*albuf
{LookupBuffer(device
, buffer
)};
1393 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1395 throw al::context_error
{AL_INVALID_VALUE
, "NULL pointer"};
1399 case AL_BUFFER_CALLBACK_FUNCTION_SOFT
:
1400 *value
= reinterpret_cast<void*>(albuf
->mCallback
);
1402 case AL_BUFFER_CALLBACK_USER_PARAM_SOFT
:
1403 *value
= albuf
->mUserData
;
1407 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer pointer property 0x%04x", param
};
1409 catch(al::context_error
& e
) {
1410 context
->setError(e
.errorCode(), "%s", e
.what());
1413 AL_API
DECL_FUNCEXT5(void, alGetBuffer3Ptr
,SOFT
, ALuint
,buffer
, ALenum
,param
, ALvoid
**,value1
, ALvoid
**,value2
, ALvoid
**,value3
)
1414 FORCE_ALIGN
void AL_APIENTRY
alGetBuffer3PtrDirectSOFT(ALCcontext
*context
, ALuint buffer
,
1415 ALenum param
, ALvoid
**value1
, ALvoid
**value2
, ALvoid
**value3
) noexcept
1417 ALCdevice
*device
{context
->mALDevice
.get()};
1418 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1420 if(LookupBuffer(device
, buffer
) == nullptr)
1421 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1422 if(!value1
|| !value2
|| !value3
)
1423 throw al::context_error
{AL_INVALID_VALUE
, "NULL pointer"};
1428 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer 3-pointer property 0x%04x", param
};
1430 catch(al::context_error
& e
) {
1431 context
->setError(e
.errorCode(), "%s", e
.what());
1434 AL_API
DECL_FUNCEXT3(void, alGetBufferPtrv
,SOFT
, ALuint
,buffer
, ALenum
,param
, ALvoid
**,values
)
1435 FORCE_ALIGN
void AL_APIENTRY
alGetBufferPtrvDirectSOFT(ALCcontext
*context
, ALuint buffer
,
1436 ALenum param
, ALvoid
**values
) noexcept
1440 case AL_BUFFER_CALLBACK_FUNCTION_SOFT
:
1441 case AL_BUFFER_CALLBACK_USER_PARAM_SOFT
:
1442 alGetBufferPtrDirectSOFT(context
, buffer
, param
, values
);
1446 ALCdevice
*device
{context
->mALDevice
.get()};
1447 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1449 if(LookupBuffer(device
, buffer
) == nullptr)
1450 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1452 throw al::context_error
{AL_INVALID_VALUE
, "NULL pointer"};
1457 throw al::context_error
{AL_INVALID_ENUM
, "Invalid buffer pointer-vector property 0x%04x",
1460 catch(al::context_error
& e
) {
1461 context
->setError(e
.errorCode(), "%s", e
.what());
1465 AL_API
void AL_APIENTRY
alBufferSamplesSOFT(ALuint
/*buffer*/, ALuint
/*samplerate*/,
1466 ALenum
/*internalformat*/, ALsizei
/*samples*/, ALenum
/*channels*/, ALenum
/*type*/,
1467 const ALvoid
* /*data*/) noexcept
1469 ContextRef context
{GetContextRef()};
1470 if(!context
) UNLIKELY
return;
1472 context
->setError(AL_INVALID_OPERATION
, "alBufferSamplesSOFT not supported");
1475 AL_API
void AL_APIENTRY
alBufferSubSamplesSOFT(ALuint
/*buffer*/, ALsizei
/*offset*/,
1476 ALsizei
/*samples*/, ALenum
/*channels*/, ALenum
/*type*/, const ALvoid
* /*data*/) noexcept
1478 ContextRef context
{GetContextRef()};
1479 if(!context
) UNLIKELY
return;
1481 context
->setError(AL_INVALID_OPERATION
, "alBufferSubSamplesSOFT not supported");
1484 AL_API
void AL_APIENTRY
alGetBufferSamplesSOFT(ALuint
/*buffer*/, ALsizei
/*offset*/,
1485 ALsizei
/*samples*/, ALenum
/*channels*/, ALenum
/*type*/, ALvoid
* /*data*/) noexcept
1487 ContextRef context
{GetContextRef()};
1488 if(!context
) UNLIKELY
return;
1490 context
->setError(AL_INVALID_OPERATION
, "alGetBufferSamplesSOFT not supported");
1493 AL_API ALboolean AL_APIENTRY
alIsBufferFormatSupportedSOFT(ALenum
/*format*/) noexcept
1495 ContextRef context
{GetContextRef()};
1496 if(!context
) UNLIKELY
return AL_FALSE
;
1498 context
->setError(AL_INVALID_OPERATION
, "alIsBufferFormatSupportedSOFT not supported");
1503 void ALbuffer::SetName(ALCcontext
*context
, ALuint id
, std::string_view name
)
1505 ALCdevice
*device
{context
->mALDevice
.get()};
1506 std::lock_guard
<std::mutex
> buflock
{device
->BufferLock
};
1508 auto buffer
= LookupBuffer(device
, id
);
1510 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", id
};
1512 device
->mBufferNames
.insert_or_assign(id
, name
);
1516 BufferSubList::~BufferSubList()
1521 uint64_t usemask
{~FreeMask
};
1524 const int idx
{al::countr_zero(usemask
)};
1525 std::destroy_at(al::to_address(Buffers
->begin() + idx
));
1526 usemask
&= ~(1_u64
<< idx
);
1528 FreeMask
= ~usemask
;
1529 SubListAllocator
{}.deallocate(Buffers
, 1);
1535 FORCE_ALIGN
DECL_FUNC3(ALboolean
, EAXSetBufferMode
, ALsizei
,n
, const ALuint
*,buffers
, ALint
,value
)
1536 FORCE_ALIGN ALboolean AL_APIENTRY
EAXSetBufferModeDirect(ALCcontext
*context
, ALsizei n
,
1537 const ALuint
*buffers
, ALint value
) noexcept
1539 if(!eax_g_is_enabled
)
1540 throw al::context_error
{AL_INVALID_OPERATION
, "EAX not enabled"};
1542 const auto storage
= EaxStorageFromEnum(value
);
1544 throw al::context_error
{AL_INVALID_ENUM
, "Unsupported X-RAM mode 0x%x", value
};
1550 throw al::context_error
{AL_INVALID_VALUE
, "Buffer count %d out of range", n
};
1552 throw al::context_error
{AL_INVALID_VALUE
, "Null AL buffers"};
1554 auto device
= context
->mALDevice
.get();
1555 std::lock_guard
<std::mutex
> devlock
{device
->BufferLock
};
1557 /* Special-case setting a single buffer, to avoid extraneous allocations. */
1560 const auto bufid
= *buffers
;
1561 if(bufid
== AL_NONE
)
1564 const auto buffer
= LookupBuffer(device
, bufid
);
1566 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", bufid
};
1568 /* TODO: Is the store location allowed to change for in-use buffers, or
1569 * only when not set/queued on a source?
1572 if(*storage
== EaxStorage::Hardware
)
1574 if(!buffer
->eax_x_ram_is_hardware
1575 && buffer
->OriginalSize
> device
->eax_x_ram_free_size
)
1576 throw al::context_error
{AL_OUT_OF_MEMORY
,
1577 "Out of X-RAM memory (need: %u, avail: %u)", buffer
->OriginalSize
,
1578 device
->eax_x_ram_free_size
};
1580 eax_x_ram_apply(*device
, *buffer
);
1583 eax_x_ram_clear(*device
, *buffer
);
1584 buffer
->eax_x_ram_mode
= *storage
;
1588 /* Validate the buffers. */
1589 std::unordered_set
<ALbuffer
*> buflist
;
1590 for(const ALuint bufid
: al::span
{buffers
, static_cast<ALuint
>(n
)})
1592 if(bufid
== AL_NONE
)
1595 const auto buffer
= LookupBuffer(device
, bufid
);
1597 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", bufid
};
1599 /* TODO: Is the store location allowed to change for in-use buffers, or
1600 * only when not set/queued on a source?
1603 buflist
.emplace(buffer
);
1606 if(*storage
== EaxStorage::Hardware
)
1608 size_t total_needed
{0};
1609 for(ALbuffer
*buffer
: buflist
)
1611 if(!buffer
->eax_x_ram_is_hardware
)
1613 if(std::numeric_limits
<size_t>::max() - buffer
->OriginalSize
< total_needed
)
1614 throw al::context_error
{AL_OUT_OF_MEMORY
, "Size overflow (%u + %zu)",
1615 buffer
->OriginalSize
, total_needed
};
1617 total_needed
+= buffer
->OriginalSize
;
1620 if(total_needed
> device
->eax_x_ram_free_size
)
1621 throw al::context_error
{AL_OUT_OF_MEMORY
, "Out of X-RAM memory (need: %zu, avail: %u)",
1622 total_needed
, device
->eax_x_ram_free_size
};
1625 /* Update the mode. */
1626 for(ALbuffer
*buffer
: buflist
)
1628 if(*storage
== EaxStorage::Hardware
)
1629 eax_x_ram_apply(*device
, *buffer
);
1631 eax_x_ram_clear(*device
, *buffer
);
1632 buffer
->eax_x_ram_mode
= *storage
;
1637 catch(al::context_error
& e
) {
1638 context
->setError(e
.errorCode(), "[EAXSetBufferMode] %s", e
.what());
1642 FORCE_ALIGN
DECL_FUNC2(ALenum
, EAXGetBufferMode
, ALuint
,buffer
, ALint
*,pReserved
)
1643 FORCE_ALIGN ALenum AL_APIENTRY
EAXGetBufferModeDirect(ALCcontext
*context
, ALuint buffer
,
1644 ALint
*pReserved
) noexcept
1647 if(!eax_g_is_enabled
)
1648 throw al::context_error
{AL_INVALID_OPERATION
, "EAX not enabled."};
1651 throw al::context_error
{AL_INVALID_VALUE
, "Non-null reserved parameter"};
1653 auto device
= context
->mALDevice
.get();
1654 std::lock_guard
<std::mutex
> devlock
{device
->BufferLock
};
1656 const auto al_buffer
= LookupBuffer(device
, buffer
);
1658 throw al::context_error
{AL_INVALID_NAME
, "Invalid buffer ID %u", buffer
};
1660 return EnumFromEaxStorage(al_buffer
->eax_x_ram_mode
);
1662 catch(al::context_error
& e
) {
1663 context
->setError(e
.errorCode(), "[EAXGetBufferMode] %s", e
.what());
1667 #endif // ALSOFT_EAX