Improve gain/hf/lf packing when processing voice updates
[openal-soft.git] / al / buffer.cpp
blobabad62366fb0154467f00fad8d23aeba4949b573
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include "buffer.h"
25 #include <algorithm>
26 #include <array>
27 #include <atomic>
28 #include <cassert>
29 #include <cstdint>
30 #include <cstdlib>
31 #include <cstring>
32 #include <iterator>
33 #include <limits>
34 #include <memory>
35 #include <mutex>
36 #include <new>
37 #include <numeric>
38 #include <utility>
40 #include "AL/al.h"
41 #include "AL/alc.h"
42 #include "AL/alext.h"
44 #include "albyte.h"
45 #include "alcmain.h"
46 #include "alcontext.h"
47 #include "alexcpt.h"
48 #include "almalloc.h"
49 #include "alnumeric.h"
50 #include "aloptional.h"
51 #include "atomic.h"
52 #include "inprogext.h"
53 #include "opthelpers.h"
56 namespace {
58 /* IMA ADPCM Stepsize table */
59 constexpr int IMAStep_size[89] = {
60 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19,
61 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
62 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157,
63 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
64 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
65 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660,
66 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,
67 11487,12635,13899,15289,16818,18500,20350,22358,24633,27086,29794,
68 32767
71 /* IMA4 ADPCM Codeword decode table */
72 constexpr int IMA4Codeword[16] = {
73 1, 3, 5, 7, 9, 11, 13, 15,
74 -1,-3,-5,-7,-9,-11,-13,-15,
77 /* IMA4 ADPCM Step index adjust decode table */
78 constexpr int IMA4Index_adjust[16] = {
79 -1,-1,-1,-1, 2, 4, 6, 8,
80 -1,-1,-1,-1, 2, 4, 6, 8
84 /* MSADPCM Adaption table */
85 constexpr int MSADPCMAdaption[16] = {
86 230, 230, 230, 230, 307, 409, 512, 614,
87 768, 614, 512, 409, 307, 230, 230, 230
90 /* MSADPCM Adaption Coefficient tables */
91 constexpr int MSADPCMAdaptionCoeff[7][2] = {
92 { 256, 0 },
93 { 512, -256 },
94 { 0, 0 },
95 { 192, 64 },
96 { 240, 0 },
97 { 460, -208 },
98 { 392, -232 }
102 void DecodeIMA4Block(ALshort *dst, const al::byte *src, size_t numchans, size_t align)
104 ALint sample[MAX_INPUT_CHANNELS]{};
105 ALint index[MAX_INPUT_CHANNELS]{};
106 ALuint code[MAX_INPUT_CHANNELS]{};
108 for(size_t c{0};c < numchans;c++)
110 sample[c] = al::to_integer<int>(src[0]) | (al::to_integer<int>(src[1])<<8);
111 sample[c] = (sample[c]^0x8000) - 32768;
112 src += 2;
113 index[c] = al::to_integer<int>(src[0]) | (al::to_integer<int>(src[1])<<8);
114 index[c] = clampi((index[c]^0x8000) - 32768, 0, 88);
115 src += 2;
117 *(dst++) = static_cast<ALshort>(sample[c]);
120 for(size_t i{1};i < align;i++)
122 if((i&7) == 1)
124 for(size_t c{0};c < numchans;c++)
126 code[c] = al::to_integer<ALuint>(src[0]) | (al::to_integer<ALuint>(src[1])<< 8) |
127 (al::to_integer<ALuint>(src[2])<<16) | (al::to_integer<ALuint>(src[3])<<24);
128 src += 4;
132 for(size_t c{0};c < numchans;c++)
134 const ALuint nibble{code[c]&0xf};
135 code[c] >>= 4;
137 sample[c] += IMA4Codeword[nibble] * IMAStep_size[index[c]] / 8;
138 sample[c] = clampi(sample[c], -32768, 32767);
140 index[c] += IMA4Index_adjust[nibble];
141 index[c] = clampi(index[c], 0, 88);
143 *(dst++) = static_cast<ALshort>(sample[c]);
148 void DecodeMSADPCMBlock(ALshort *dst, const al::byte *src, size_t numchans, size_t align)
150 ALubyte blockpred[MAX_INPUT_CHANNELS]{};
151 ALint delta[MAX_INPUT_CHANNELS]{};
152 ALshort samples[MAX_INPUT_CHANNELS][2]{};
154 for(size_t c{0};c < numchans;c++)
156 blockpred[c] = std::min<ALubyte>(al::to_integer<ALubyte>(src[0]), 6);
157 ++src;
159 for(size_t c{0};c < numchans;c++)
161 delta[c] = al::to_integer<int>(src[0]) | (al::to_integer<int>(src[1])<<8);
162 delta[c] = (delta[c]^0x8000) - 32768;
163 src += 2;
165 for(size_t c{0};c < numchans;c++)
167 samples[c][0] = static_cast<ALshort>(al::to_integer<int>(src[0]) |
168 (al::to_integer<int>(src[1])<<8));
169 src += 2;
171 for(size_t c{0};c < numchans;c++)
173 samples[c][1] = static_cast<ALshort>(al::to_integer<int>(src[0]) |
174 (al::to_integer<int>(src[1])<<8));
175 src += 2;
178 /* Second sample is written first. */
179 for(size_t c{0};c < numchans;c++)
180 *(dst++) = samples[c][1];
181 for(size_t c{0};c < numchans;c++)
182 *(dst++) = samples[c][0];
184 int num{0};
185 for(size_t i{2};i < align;i++)
187 for(size_t c{0};c < numchans;c++)
189 /* Read the nibble (first is in the upper bits). */
190 al::byte nibble;
191 if(!(num++ & 1))
192 nibble = *src >> 4;
193 else
194 nibble = *(src++) & 0x0f;
196 ALint pred{(samples[c][0]*MSADPCMAdaptionCoeff[blockpred[c]][0] +
197 samples[c][1]*MSADPCMAdaptionCoeff[blockpred[c]][1]) / 256};
198 pred += (al::to_integer<int>(nibble^0x08) - 0x08) * delta[c];
199 pred = clampi(pred, -32768, 32767);
201 samples[c][1] = samples[c][0];
202 samples[c][0] = static_cast<ALshort>(pred);
204 delta[c] = (MSADPCMAdaption[al::to_integer<ALubyte>(nibble)] * delta[c]) / 256;
205 delta[c] = maxi(16, delta[c]);
207 *(dst++) = static_cast<ALshort>(pred);
212 void Convert_ALshort_ALima4(ALshort *dst, const al::byte *src, size_t numchans, size_t len,
213 size_t align)
215 const size_t byte_align{((align-1)/2 + 4) * numchans};
217 len /= align;
218 while(len--)
220 DecodeIMA4Block(dst, src, numchans, align);
221 src += byte_align;
222 dst += align*numchans;
226 void Convert_ALshort_ALmsadpcm(ALshort *dst, const al::byte *src, size_t numchans, size_t len,
227 size_t align)
229 const size_t byte_align{((align-2)/2 + 7) * numchans};
231 len /= align;
232 while(len--)
234 DecodeMSADPCMBlock(dst, src, numchans, align);
235 src += byte_align;
236 dst += align*numchans;
241 ALuint BytesFromUserFmt(UserFmtType type)
243 switch(type)
245 case UserFmtUByte: return sizeof(ALubyte);
246 case UserFmtShort: return sizeof(ALshort);
247 case UserFmtFloat: return sizeof(ALfloat);
248 case UserFmtDouble: return sizeof(ALdouble);
249 case UserFmtMulaw: return sizeof(ALubyte);
250 case UserFmtAlaw: return sizeof(ALubyte);
251 case UserFmtIMA4: break; /* not handled here */
252 case UserFmtMSADPCM: break; /* not handled here */
254 return 0;
256 ALuint ChannelsFromUserFmt(UserFmtChannels chans)
258 switch(chans)
260 case UserFmtMono: return 1;
261 case UserFmtStereo: return 2;
262 case UserFmtRear: return 2;
263 case UserFmtQuad: return 4;
264 case UserFmtX51: return 6;
265 case UserFmtX61: return 7;
266 case UserFmtX71: return 8;
267 case UserFmtBFormat2D: return 3;
268 case UserFmtBFormat3D: return 4;
270 return 0;
272 inline ALuint FrameSizeFromUserFmt(UserFmtChannels chans, UserFmtType type)
273 { return ChannelsFromUserFmt(chans) * BytesFromUserFmt(type); }
276 constexpr ALbitfieldSOFT INVALID_STORAGE_MASK{~unsigned(AL_MAP_READ_BIT_SOFT |
277 AL_MAP_WRITE_BIT_SOFT | AL_MAP_PERSISTENT_BIT_SOFT | AL_PRESERVE_DATA_BIT_SOFT)};
278 constexpr ALbitfieldSOFT MAP_READ_WRITE_FLAGS{AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT};
279 constexpr ALbitfieldSOFT INVALID_MAP_FLAGS{~unsigned(AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT |
280 AL_MAP_PERSISTENT_BIT_SOFT)};
283 bool EnsureBuffers(ALCdevice *device, size_t needed)
285 size_t count{std::accumulate(device->BufferList.cbegin(), device->BufferList.cend(), size_t{0},
286 [](size_t cur, const BufferSubList &sublist) noexcept -> size_t
287 { return cur + static_cast<ALuint>(POPCNT64(sublist.FreeMask)); }
290 while(needed > count)
292 if UNLIKELY(device->BufferList.size() >= 1<<25)
293 return false;
295 device->BufferList.emplace_back();
296 auto sublist = device->BufferList.end() - 1;
297 sublist->FreeMask = ~0_u64;
298 sublist->Buffers = static_cast<ALbuffer*>(al_calloc(alignof(ALbuffer), sizeof(ALbuffer)*64));
299 if UNLIKELY(!sublist->Buffers)
301 device->BufferList.pop_back();
302 return false;
304 count += 64;
306 return true;
309 ALbuffer *AllocBuffer(ALCdevice *device)
311 auto sublist = std::find_if(device->BufferList.begin(), device->BufferList.end(),
312 [](const BufferSubList &entry) noexcept -> bool
313 { return entry.FreeMask != 0; }
316 auto lidx = static_cast<ALuint>(std::distance(device->BufferList.begin(), sublist));
317 auto slidx = static_cast<ALuint>(CTZ64(sublist->FreeMask));
319 ALbuffer *buffer{::new (sublist->Buffers + slidx) ALbuffer{}};
321 /* Add 1 to avoid buffer ID 0. */
322 buffer->id = ((lidx<<6) | slidx) + 1;
324 sublist->FreeMask &= ~(1_u64 << slidx);
326 return buffer;
329 void FreeBuffer(ALCdevice *device, ALbuffer *buffer)
331 const ALuint id{buffer->id - 1};
332 const size_t lidx{id >> 6};
333 const ALuint slidx{id & 0x3f};
335 al::destroy_at(buffer);
337 device->BufferList[lidx].FreeMask |= 1_u64 << slidx;
340 inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id)
342 const size_t lidx{(id-1) >> 6};
343 const ALuint slidx{(id-1) & 0x3f};
345 if UNLIKELY(lidx >= device->BufferList.size())
346 return nullptr;
347 BufferSubList &sublist = device->BufferList[lidx];
348 if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
349 return nullptr;
350 return sublist.Buffers + slidx;
354 ALuint SanitizeAlignment(UserFmtType type, ALuint align)
356 if(align == 0)
358 if(type == UserFmtIMA4)
360 /* Here is where things vary:
361 * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
362 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
364 return 65;
366 if(type == UserFmtMSADPCM)
367 return 64;
368 return 1;
371 if(type == UserFmtIMA4)
373 /* IMA4 block alignment must be a multiple of 8, plus 1. */
374 if((align&7) == 1) return static_cast<ALuint>(align);
375 return 0;
377 if(type == UserFmtMSADPCM)
379 /* MSADPCM block alignment must be a multiple of 2. */
380 if((align&1) == 0) return static_cast<ALuint>(align);
381 return 0;
384 return static_cast<ALuint>(align);
388 const ALchar *NameFromUserFmtType(UserFmtType type)
390 switch(type)
392 case UserFmtUByte: return "Unsigned Byte";
393 case UserFmtShort: return "Signed Short";
394 case UserFmtFloat: return "Float32";
395 case UserFmtDouble: return "Float64";
396 case UserFmtMulaw: return "muLaw";
397 case UserFmtAlaw: return "aLaw";
398 case UserFmtIMA4: return "IMA4 ADPCM";
399 case UserFmtMSADPCM: return "MSADPCM";
401 return "<internal type error>";
404 /** Loads the specified data into the buffer, using the specified format. */
405 void LoadData(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq, ALuint size,
406 UserFmtChannels SrcChannels, UserFmtType SrcType, const al::byte *SrcData,
407 ALbitfieldSOFT access)
409 if UNLIKELY(ReadRef(ALBuf->ref) != 0 || ALBuf->MappedAccess != 0)
410 SETERR_RETURN(context, AL_INVALID_OPERATION,, "Modifying storage for in-use buffer %u",
411 ALBuf->id);
413 /* Currently no channel configurations need to be converted. */
414 FmtChannels DstChannels{FmtMono};
415 switch(SrcChannels)
417 case UserFmtMono: DstChannels = FmtMono; break;
418 case UserFmtStereo: DstChannels = FmtStereo; break;
419 case UserFmtRear: DstChannels = FmtRear; break;
420 case UserFmtQuad: DstChannels = FmtQuad; break;
421 case UserFmtX51: DstChannels = FmtX51; break;
422 case UserFmtX61: DstChannels = FmtX61; break;
423 case UserFmtX71: DstChannels = FmtX71; break;
424 case UserFmtBFormat2D: DstChannels = FmtBFormat2D; break;
425 case UserFmtBFormat3D: DstChannels = FmtBFormat3D; break;
427 if UNLIKELY(static_cast<long>(SrcChannels) != static_cast<long>(DstChannels))
428 SETERR_RETURN(context, AL_INVALID_ENUM, , "Invalid format");
430 /* IMA4 and MSADPCM convert to 16-bit short. */
431 FmtType DstType{FmtUByte};
432 switch(SrcType)
434 case UserFmtUByte: DstType = FmtUByte; break;
435 case UserFmtShort: DstType = FmtShort; break;
436 case UserFmtFloat: DstType = FmtFloat; break;
437 case UserFmtDouble: DstType = FmtDouble; break;
438 case UserFmtAlaw: DstType = FmtAlaw; break;
439 case UserFmtMulaw: DstType = FmtMulaw; break;
440 case UserFmtIMA4: DstType = FmtShort; break;
441 case UserFmtMSADPCM: DstType = FmtShort; break;
444 /* TODO: Currently we can only map samples when they're not converted. To
445 * allow it would need some kind of double-buffering to hold onto a copy of
446 * the original data.
448 if((access&MAP_READ_WRITE_FLAGS))
450 if UNLIKELY(static_cast<long>(SrcType) != static_cast<long>(DstType))
451 SETERR_RETURN(context, AL_INVALID_VALUE,, "%s samples cannot be mapped",
452 NameFromUserFmtType(SrcType));
455 const ALuint unpackalign{ALBuf->UnpackAlign};
456 const ALuint align{SanitizeAlignment(SrcType, unpackalign)};
457 if UNLIKELY(align < 1)
458 SETERR_RETURN(context, AL_INVALID_VALUE,, "Invalid unpack alignment %u for %s samples",
459 unpackalign, NameFromUserFmtType(SrcType));
461 if((access&AL_PRESERVE_DATA_BIT_SOFT))
463 /* Can only preserve data with the same format and alignment. */
464 if UNLIKELY(ALBuf->mFmtChannels != DstChannels || ALBuf->OriginalType != SrcType)
465 SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched format");
466 if UNLIKELY(ALBuf->OriginalAlign != align)
467 SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched alignment");
470 /* Convert the input/source size in bytes to sample frames using the unpack
471 * block alignment.
473 const ALuint SrcByteAlign{
474 (SrcType == UserFmtIMA4) ? ((align-1)/2 + 4) * ChannelsFromUserFmt(SrcChannels) :
475 (SrcType == UserFmtMSADPCM) ? ((align-2)/2 + 7) * ChannelsFromUserFmt(SrcChannels) :
476 (align * FrameSizeFromUserFmt(SrcChannels, SrcType))
478 if UNLIKELY((size%SrcByteAlign) != 0)
479 SETERR_RETURN(context, AL_INVALID_VALUE,,
480 "Data size %d is not a multiple of frame size %d (%d unpack alignment)",
481 size, SrcByteAlign, align);
483 if UNLIKELY(size/SrcByteAlign > std::numeric_limits<ALsizei>::max()/align)
484 SETERR_RETURN(context, AL_OUT_OF_MEMORY,,
485 "Buffer size overflow, %d blocks x %d samples per block", size/SrcByteAlign, align);
486 const ALuint frames{size / SrcByteAlign * align};
488 /* Convert the sample frames to the number of bytes needed for internal
489 * storage.
491 ALuint NumChannels{ChannelsFromFmt(DstChannels)};
492 ALuint FrameSize{NumChannels * BytesFromFmt(DstType)};
493 if UNLIKELY(frames > std::numeric_limits<size_t>::max()/FrameSize)
494 SETERR_RETURN(context, AL_OUT_OF_MEMORY,,
495 "Buffer size overflow, %d frames x %d bytes per frame", frames, FrameSize);
496 size_t newsize{static_cast<size_t>(frames) * FrameSize};
498 /* Round up to the next 16-byte multiple. This could reallocate only when
499 * increasing or the new size is less than half the current, but then the
500 * buffer's AL_SIZE would not be very reliable for accounting buffer memory
501 * usage, and reporting the real size could cause problems for apps that
502 * use AL_SIZE to try to get the buffer's play length.
504 newsize = RoundUp(newsize, 16);
505 if(newsize != ALBuf->mData.size())
507 auto newdata = al::vector<al::byte,16>(newsize, al::byte{});
508 if((access&AL_PRESERVE_DATA_BIT_SOFT))
510 const size_t tocopy{minz(newdata.size(), ALBuf->mData.size())};
511 std::copy_n(ALBuf->mData.begin(), tocopy, newdata.begin());
513 ALBuf->mData = std::move(newdata);
516 if(SrcType == UserFmtIMA4)
518 assert(DstType == FmtShort);
519 if(SrcData != nullptr && !ALBuf->mData.empty())
520 Convert_ALshort_ALima4(reinterpret_cast<ALshort*>(ALBuf->mData.data()),
521 SrcData, NumChannels, frames, align);
522 ALBuf->OriginalAlign = align;
524 else if(SrcType == UserFmtMSADPCM)
526 assert(DstType == FmtShort);
527 if(SrcData != nullptr && !ALBuf->mData.empty())
528 Convert_ALshort_ALmsadpcm(reinterpret_cast<ALshort*>(ALBuf->mData.data()),
529 SrcData, NumChannels, frames, align);
530 ALBuf->OriginalAlign = align;
532 else
534 assert(static_cast<long>(SrcType) == static_cast<long>(DstType));
535 if(SrcData != nullptr && !ALBuf->mData.empty())
536 std::copy_n(SrcData, frames*FrameSize, ALBuf->mData.begin());
537 ALBuf->OriginalAlign = 1;
539 ALBuf->OriginalSize = size;
540 ALBuf->OriginalType = SrcType;
542 ALBuf->Frequency = static_cast<ALuint>(freq);
543 ALBuf->mFmtChannels = DstChannels;
544 ALBuf->mFmtType = DstType;
545 ALBuf->Access = access;
547 ALBuf->SampleLen = frames;
548 ALBuf->LoopStart = 0;
549 ALBuf->LoopEnd = ALBuf->SampleLen;
552 struct DecompResult { UserFmtChannels channels; UserFmtType type; };
553 al::optional<DecompResult> DecomposeUserFormat(ALenum format)
555 struct FormatMap {
556 ALenum format;
557 UserFmtChannels channels;
558 UserFmtType type;
560 static const std::array<FormatMap,46> UserFmtList{{
561 { AL_FORMAT_MONO8, UserFmtMono, UserFmtUByte },
562 { AL_FORMAT_MONO16, UserFmtMono, UserFmtShort },
563 { AL_FORMAT_MONO_FLOAT32, UserFmtMono, UserFmtFloat },
564 { AL_FORMAT_MONO_DOUBLE_EXT, UserFmtMono, UserFmtDouble },
565 { AL_FORMAT_MONO_IMA4, UserFmtMono, UserFmtIMA4 },
566 { AL_FORMAT_MONO_MSADPCM_SOFT, UserFmtMono, UserFmtMSADPCM },
567 { AL_FORMAT_MONO_MULAW, UserFmtMono, UserFmtMulaw },
568 { AL_FORMAT_MONO_ALAW_EXT, UserFmtMono, UserFmtAlaw },
570 { AL_FORMAT_STEREO8, UserFmtStereo, UserFmtUByte },
571 { AL_FORMAT_STEREO16, UserFmtStereo, UserFmtShort },
572 { AL_FORMAT_STEREO_FLOAT32, UserFmtStereo, UserFmtFloat },
573 { AL_FORMAT_STEREO_DOUBLE_EXT, UserFmtStereo, UserFmtDouble },
574 { AL_FORMAT_STEREO_IMA4, UserFmtStereo, UserFmtIMA4 },
575 { AL_FORMAT_STEREO_MSADPCM_SOFT, UserFmtStereo, UserFmtMSADPCM },
576 { AL_FORMAT_STEREO_MULAW, UserFmtStereo, UserFmtMulaw },
577 { AL_FORMAT_STEREO_ALAW_EXT, UserFmtStereo, UserFmtAlaw },
579 { AL_FORMAT_REAR8, UserFmtRear, UserFmtUByte },
580 { AL_FORMAT_REAR16, UserFmtRear, UserFmtShort },
581 { AL_FORMAT_REAR32, UserFmtRear, UserFmtFloat },
582 { AL_FORMAT_REAR_MULAW, UserFmtRear, UserFmtMulaw },
584 { AL_FORMAT_QUAD8_LOKI, UserFmtQuad, UserFmtUByte },
585 { AL_FORMAT_QUAD16_LOKI, UserFmtQuad, UserFmtShort },
587 { AL_FORMAT_QUAD8, UserFmtQuad, UserFmtUByte },
588 { AL_FORMAT_QUAD16, UserFmtQuad, UserFmtShort },
589 { AL_FORMAT_QUAD32, UserFmtQuad, UserFmtFloat },
590 { AL_FORMAT_QUAD_MULAW, UserFmtQuad, UserFmtMulaw },
592 { AL_FORMAT_51CHN8, UserFmtX51, UserFmtUByte },
593 { AL_FORMAT_51CHN16, UserFmtX51, UserFmtShort },
594 { AL_FORMAT_51CHN32, UserFmtX51, UserFmtFloat },
595 { AL_FORMAT_51CHN_MULAW, UserFmtX51, UserFmtMulaw },
597 { AL_FORMAT_61CHN8, UserFmtX61, UserFmtUByte },
598 { AL_FORMAT_61CHN16, UserFmtX61, UserFmtShort },
599 { AL_FORMAT_61CHN32, UserFmtX61, UserFmtFloat },
600 { AL_FORMAT_61CHN_MULAW, UserFmtX61, UserFmtMulaw },
602 { AL_FORMAT_71CHN8, UserFmtX71, UserFmtUByte },
603 { AL_FORMAT_71CHN16, UserFmtX71, UserFmtShort },
604 { AL_FORMAT_71CHN32, UserFmtX71, UserFmtFloat },
605 { AL_FORMAT_71CHN_MULAW, UserFmtX71, UserFmtMulaw },
607 { AL_FORMAT_BFORMAT2D_8, UserFmtBFormat2D, UserFmtUByte },
608 { AL_FORMAT_BFORMAT2D_16, UserFmtBFormat2D, UserFmtShort },
609 { AL_FORMAT_BFORMAT2D_FLOAT32, UserFmtBFormat2D, UserFmtFloat },
610 { AL_FORMAT_BFORMAT2D_MULAW, UserFmtBFormat2D, UserFmtMulaw },
612 { AL_FORMAT_BFORMAT3D_8, UserFmtBFormat3D, UserFmtUByte },
613 { AL_FORMAT_BFORMAT3D_16, UserFmtBFormat3D, UserFmtShort },
614 { AL_FORMAT_BFORMAT3D_FLOAT32, UserFmtBFormat3D, UserFmtFloat },
615 { AL_FORMAT_BFORMAT3D_MULAW, UserFmtBFormat3D, UserFmtMulaw },
618 for(const auto &fmt : UserFmtList)
620 if(fmt.format == format)
621 return al::make_optional<DecompResult>({fmt.channels, fmt.type});
623 return al::nullopt;
626 } // namespace
629 AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
630 START_API_FUNC
632 ContextRef context{GetContextRef()};
633 if UNLIKELY(!context) return;
635 if UNLIKELY(n < 0)
636 context->setError(AL_INVALID_VALUE, "Generating %d buffers", n);
637 if UNLIKELY(n <= 0) return;
639 ALCdevice *device{context->mDevice.get()};
640 std::lock_guard<std::mutex> _{device->BufferLock};
641 if(!EnsureBuffers(device, static_cast<ALuint>(n)))
643 context->setError(AL_OUT_OF_MEMORY, "Failed to allocate %d buffer%s", n, (n==1)?"":"s");
644 return;
647 if LIKELY(n == 1)
649 /* Special handling for the easy and normal case. */
650 ALbuffer *buffer{AllocBuffer(device)};
651 buffers[0] = buffer->id;
653 else
655 /* Store the allocated buffer IDs in a separate local list, to avoid
656 * modifying the user storage in case of failure.
658 al::vector<ALuint> ids;
659 ids.reserve(static_cast<ALuint>(n));
660 do {
661 ALbuffer *buffer{AllocBuffer(device)};
662 ids.emplace_back(buffer->id);
663 } while(--n);
664 std::copy(ids.begin(), ids.end(), buffers);
667 END_API_FUNC
669 AL_API ALvoid AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
670 START_API_FUNC
672 ContextRef context{GetContextRef()};
673 if UNLIKELY(!context) return;
675 if UNLIKELY(n < 0)
676 context->setError(AL_INVALID_VALUE, "Deleting %d buffers", n);
677 if UNLIKELY(n <= 0) return;
679 ALCdevice *device{context->mDevice.get()};
680 std::lock_guard<std::mutex> _{device->BufferLock};
682 /* First try to find any buffers that are invalid or in-use. */
683 auto validate_buffer = [device, &context](const ALuint bid) -> bool
685 if(!bid) return true;
686 ALbuffer *ALBuf{LookupBuffer(device, bid)};
687 if UNLIKELY(!ALBuf)
689 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", bid);
690 return false;
692 if UNLIKELY(ReadRef(ALBuf->ref) != 0)
694 context->setError(AL_INVALID_OPERATION, "Deleting in-use buffer %u", bid);
695 return false;
697 return true;
699 const ALuint *buffers_end = buffers + n;
700 auto invbuf = std::find_if_not(buffers, buffers_end, validate_buffer);
701 if UNLIKELY(invbuf != buffers_end) return;
703 /* All good. Delete non-0 buffer IDs. */
704 auto delete_buffer = [device](const ALuint bid) -> void
706 ALbuffer *buffer{bid ? LookupBuffer(device, bid) : nullptr};
707 if(buffer) FreeBuffer(device, buffer);
709 std::for_each(buffers, buffers_end, delete_buffer);
711 END_API_FUNC
713 AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
714 START_API_FUNC
716 ContextRef context{GetContextRef()};
717 if LIKELY(context)
719 ALCdevice *device{context->mDevice.get()};
720 std::lock_guard<std::mutex> _{device->BufferLock};
721 if(!buffer || LookupBuffer(device, buffer))
722 return AL_TRUE;
724 return AL_FALSE;
726 END_API_FUNC
729 AL_API ALvoid AL_APIENTRY alBufferData(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq)
730 START_API_FUNC
731 { alBufferStorageSOFT(buffer, format, data, size, freq, 0); }
732 END_API_FUNC
734 AL_API void AL_APIENTRY alBufferStorageSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq, ALbitfieldSOFT flags)
735 START_API_FUNC
737 ContextRef context{GetContextRef()};
738 if UNLIKELY(!context) return;
740 ALCdevice *device{context->mDevice.get()};
741 std::lock_guard<std::mutex> _{device->BufferLock};
743 ALbuffer *albuf = LookupBuffer(device, buffer);
744 if UNLIKELY(!albuf)
745 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
746 else if UNLIKELY(size < 0)
747 context->setError(AL_INVALID_VALUE, "Negative storage size %d", size);
748 else if UNLIKELY(freq < 1)
749 context->setError(AL_INVALID_VALUE, "Invalid sample rate %d", freq);
750 else if UNLIKELY((flags&INVALID_STORAGE_MASK) != 0)
751 context->setError(AL_INVALID_VALUE, "Invalid storage flags 0x%x",
752 flags&INVALID_STORAGE_MASK);
753 else if UNLIKELY((flags&AL_MAP_PERSISTENT_BIT_SOFT) && !(flags&MAP_READ_WRITE_FLAGS))
754 context->setError(AL_INVALID_VALUE,
755 "Declaring persistently mapped storage without read or write access");
756 else
758 auto usrfmt = DecomposeUserFormat(format);
759 if UNLIKELY(!usrfmt)
760 context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
761 else
762 LoadData(context.get(), albuf, freq, static_cast<ALuint>(size), usrfmt->channels,
763 usrfmt->type, static_cast<const al::byte*>(data), flags);
766 END_API_FUNC
768 AL_API void* AL_APIENTRY alMapBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length, ALbitfieldSOFT access)
769 START_API_FUNC
771 ContextRef context{GetContextRef()};
772 if UNLIKELY(!context) return nullptr;
774 ALCdevice *device{context->mDevice.get()};
775 std::lock_guard<std::mutex> _{device->BufferLock};
777 ALbuffer *albuf = LookupBuffer(device, buffer);
778 if UNLIKELY(!albuf)
779 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
780 else if UNLIKELY((access&INVALID_MAP_FLAGS) != 0)
781 context->setError(AL_INVALID_VALUE, "Invalid map flags 0x%x", access&INVALID_MAP_FLAGS);
782 else if UNLIKELY(!(access&MAP_READ_WRITE_FLAGS))
783 context->setError(AL_INVALID_VALUE, "Mapping buffer %u without read or write access",
784 buffer);
785 else
787 ALbitfieldSOFT unavailable = (albuf->Access^access) & access;
788 if UNLIKELY(ReadRef(albuf->ref) != 0 && !(access&AL_MAP_PERSISTENT_BIT_SOFT))
789 context->setError(AL_INVALID_OPERATION,
790 "Mapping in-use buffer %u without persistent mapping", buffer);
791 else if UNLIKELY(albuf->MappedAccess != 0)
792 context->setError(AL_INVALID_OPERATION, "Mapping already-mapped buffer %u", buffer);
793 else if UNLIKELY((unavailable&AL_MAP_READ_BIT_SOFT))
794 context->setError(AL_INVALID_VALUE,
795 "Mapping buffer %u for reading without read access", buffer);
796 else if UNLIKELY((unavailable&AL_MAP_WRITE_BIT_SOFT))
797 context->setError(AL_INVALID_VALUE,
798 "Mapping buffer %u for writing without write access", buffer);
799 else if UNLIKELY((unavailable&AL_MAP_PERSISTENT_BIT_SOFT))
800 context->setError(AL_INVALID_VALUE,
801 "Mapping buffer %u persistently without persistent access", buffer);
802 else if UNLIKELY(offset < 0 || length <= 0
803 || static_cast<ALuint>(offset) >= albuf->OriginalSize
804 || static_cast<ALuint>(length) > albuf->OriginalSize - static_cast<ALuint>(offset))
805 context->setError(AL_INVALID_VALUE, "Mapping invalid range %d+%d for buffer %u",
806 offset, length, buffer);
807 else
809 void *retval = albuf->mData.data() + offset;
810 albuf->MappedAccess = access;
811 albuf->MappedOffset = offset;
812 albuf->MappedSize = length;
813 return retval;
817 return nullptr;
819 END_API_FUNC
821 AL_API void AL_APIENTRY alUnmapBufferSOFT(ALuint buffer)
822 START_API_FUNC
824 ContextRef context{GetContextRef()};
825 if UNLIKELY(!context) return;
827 ALCdevice *device{context->mDevice.get()};
828 std::lock_guard<std::mutex> _{device->BufferLock};
830 ALbuffer *albuf = LookupBuffer(device, buffer);
831 if UNLIKELY(!albuf)
832 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
833 else if UNLIKELY(albuf->MappedAccess == 0)
834 context->setError(AL_INVALID_OPERATION, "Unmapping unmapped buffer %u", buffer);
835 else
837 albuf->MappedAccess = 0;
838 albuf->MappedOffset = 0;
839 albuf->MappedSize = 0;
842 END_API_FUNC
844 AL_API void AL_APIENTRY alFlushMappedBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length)
845 START_API_FUNC
847 ContextRef context{GetContextRef()};
848 if UNLIKELY(!context) return;
850 ALCdevice *device{context->mDevice.get()};
851 std::lock_guard<std::mutex> _{device->BufferLock};
853 ALbuffer *albuf = LookupBuffer(device, buffer);
854 if UNLIKELY(!albuf)
855 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
856 else if UNLIKELY(!(albuf->MappedAccess&AL_MAP_WRITE_BIT_SOFT))
857 context->setError(AL_INVALID_OPERATION, "Flushing buffer %u while not mapped for writing",
858 buffer);
859 else if UNLIKELY(offset < albuf->MappedOffset || length <= 0
860 || offset >= albuf->MappedOffset+albuf->MappedSize
861 || length > albuf->MappedOffset+albuf->MappedSize-offset)
862 context->setError(AL_INVALID_VALUE, "Flushing invalid range %d+%d on buffer %u", offset,
863 length, buffer);
864 else
866 /* FIXME: Need to use some method of double-buffering for the mixer and
867 * app to hold separate memory, which can be safely transfered
868 * asynchronously. Currently we just say the app shouldn't write where
869 * OpenAL's reading, and hope for the best...
871 std::atomic_thread_fence(std::memory_order_seq_cst);
874 END_API_FUNC
876 AL_API ALvoid AL_APIENTRY alBufferSubDataSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei offset, ALsizei length)
877 START_API_FUNC
879 ContextRef context{GetContextRef()};
880 if UNLIKELY(!context) return;
882 ALCdevice *device{context->mDevice.get()};
883 std::lock_guard<std::mutex> _{device->BufferLock};
885 ALbuffer *albuf = LookupBuffer(device, buffer);
886 if UNLIKELY(!albuf)
888 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
889 return;
892 auto usrfmt = DecomposeUserFormat(format);
893 if UNLIKELY(!usrfmt)
895 context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
896 return;
899 ALuint unpack_align{albuf->UnpackAlign};
900 ALuint align{SanitizeAlignment(usrfmt->type, unpack_align)};
901 if UNLIKELY(align < 1)
902 context->setError(AL_INVALID_VALUE, "Invalid unpack alignment %u", unpack_align);
903 else if UNLIKELY(long{usrfmt->channels} != long{albuf->mFmtChannels}
904 || usrfmt->type != albuf->OriginalType)
905 context->setError(AL_INVALID_ENUM, "Unpacking data with mismatched format");
906 else if UNLIKELY(align != albuf->OriginalAlign)
907 context->setError(AL_INVALID_VALUE,
908 "Unpacking data with alignment %u does not match original alignment %u", align,
909 albuf->OriginalAlign);
910 else if UNLIKELY(albuf->MappedAccess != 0)
911 context->setError(AL_INVALID_OPERATION, "Unpacking data into mapped buffer %u", buffer);
912 else
914 ALuint num_chans{ChannelsFromFmt(albuf->mFmtChannels)};
915 ALuint frame_size{num_chans * BytesFromFmt(albuf->mFmtType)};
916 ALuint byte_align{
917 (albuf->OriginalType == UserFmtIMA4) ? ((align-1)/2 + 4) * num_chans :
918 (albuf->OriginalType == UserFmtMSADPCM) ? ((align-2)/2 + 7) * num_chans :
919 (align * frame_size)
922 if UNLIKELY(offset < 0 || length < 0 || static_cast<ALuint>(offset) > albuf->OriginalSize
923 || static_cast<ALuint>(length) > albuf->OriginalSize-static_cast<ALuint>(offset))
924 context->setError(AL_INVALID_VALUE, "Invalid data sub-range %d+%d on buffer %u",
925 offset, length, buffer);
926 else if UNLIKELY((static_cast<ALuint>(offset)%byte_align) != 0)
927 context->setError(AL_INVALID_VALUE,
928 "Sub-range offset %d is not a multiple of frame size %d (%d unpack alignment)",
929 offset, byte_align, align);
930 else if UNLIKELY((static_cast<ALuint>(length)%byte_align) != 0)
931 context->setError(AL_INVALID_VALUE,
932 "Sub-range length %d is not a multiple of frame size %d (%d unpack alignment)",
933 length, byte_align, align);
934 else
936 /* offset -> byte offset, length -> sample count */
937 size_t byteoff{static_cast<ALuint>(offset)/byte_align * align * frame_size};
938 size_t samplen{static_cast<ALuint>(length)/byte_align * align};
940 void *dst = albuf->mData.data() + byteoff;
941 if(usrfmt->type == UserFmtIMA4 && albuf->mFmtType == FmtShort)
942 Convert_ALshort_ALima4(static_cast<ALshort*>(dst),
943 static_cast<const al::byte*>(data), num_chans, samplen, align);
944 else if(usrfmt->type == UserFmtMSADPCM && albuf->mFmtType == FmtShort)
945 Convert_ALshort_ALmsadpcm(static_cast<ALshort*>(dst),
946 static_cast<const al::byte*>(data), num_chans, samplen, align);
947 else
949 assert(long{usrfmt->type} == long{albuf->mFmtType});
950 memcpy(dst, data, size_t{samplen} * frame_size);
955 END_API_FUNC
958 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint /*buffer*/, ALuint /*samplerate*/,
959 ALenum /*internalformat*/, ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/,
960 const ALvoid* /*data*/)
961 START_API_FUNC
963 ContextRef context{GetContextRef()};
964 if UNLIKELY(!context) return;
966 context->setError(AL_INVALID_OPERATION, "alBufferSamplesSOFT not supported");
968 END_API_FUNC
970 AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
971 ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, const ALvoid* /*data*/)
972 START_API_FUNC
974 ContextRef context{GetContextRef()};
975 if UNLIKELY(!context) return;
977 context->setError(AL_INVALID_OPERATION, "alBufferSubSamplesSOFT not supported");
979 END_API_FUNC
981 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
982 ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, ALvoid* /*data*/)
983 START_API_FUNC
985 ContextRef context{GetContextRef()};
986 if UNLIKELY(!context) return;
988 context->setError(AL_INVALID_OPERATION, "alGetBufferSamplesSOFT not supported");
990 END_API_FUNC
992 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum /*format*/)
993 START_API_FUNC
995 ContextRef context{GetContextRef()};
996 if UNLIKELY(!context) return AL_FALSE;
998 context->setError(AL_INVALID_OPERATION, "alIsBufferFormatSupportedSOFT not supported");
999 return AL_FALSE;
1001 END_API_FUNC
1004 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum param, ALfloat /*value*/)
1005 START_API_FUNC
1007 ContextRef context{GetContextRef()};
1008 if UNLIKELY(!context) return;
1010 ALCdevice *device{context->mDevice.get()};
1011 std::lock_guard<std::mutex> _{device->BufferLock};
1013 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1014 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1015 else switch(param)
1017 default:
1018 context->setError(AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
1021 END_API_FUNC
1023 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum param,
1024 ALfloat /*value1*/, ALfloat /*value2*/, ALfloat /*value3*/)
1025 START_API_FUNC
1027 ContextRef context{GetContextRef()};
1028 if UNLIKELY(!context) return;
1030 ALCdevice *device{context->mDevice.get()};
1031 std::lock_guard<std::mutex> _{device->BufferLock};
1033 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1034 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1035 else switch(param)
1037 default:
1038 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
1041 END_API_FUNC
1043 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum param, const ALfloat *values)
1044 START_API_FUNC
1046 ContextRef context{GetContextRef()};
1047 if UNLIKELY(!context) return;
1049 ALCdevice *device{context->mDevice.get()};
1050 std::lock_guard<std::mutex> _{device->BufferLock};
1052 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1053 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1054 else if UNLIKELY(!values)
1055 context->setError(AL_INVALID_VALUE, "NULL pointer");
1056 else switch(param)
1058 default:
1059 context->setError(AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
1062 END_API_FUNC
1065 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum param, ALint value)
1066 START_API_FUNC
1068 ContextRef context{GetContextRef()};
1069 if UNLIKELY(!context) return;
1071 ALCdevice *device{context->mDevice.get()};
1072 std::lock_guard<std::mutex> _{device->BufferLock};
1074 ALbuffer *albuf = LookupBuffer(device, buffer);
1075 if UNLIKELY(!albuf)
1076 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1077 else switch(param)
1079 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1080 if UNLIKELY(value < 0)
1081 context->setError(AL_INVALID_VALUE, "Invalid unpack block alignment %d", value);
1082 else
1083 albuf->UnpackAlign = static_cast<ALuint>(value);
1084 break;
1086 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1087 if UNLIKELY(value < 0)
1088 context->setError(AL_INVALID_VALUE, "Invalid pack block alignment %d", value);
1089 else
1090 albuf->PackAlign = static_cast<ALuint>(value);
1091 break;
1093 case AL_AMBISONIC_LAYOUT_SOFT:
1094 if UNLIKELY(ReadRef(albuf->ref) != 0)
1095 context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's ambisonic layout",
1096 buffer);
1097 else if UNLIKELY(value != AL_FUMA_SOFT && value != AL_ACN_SOFT)
1098 context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic layout %04x", value);
1099 else
1100 albuf->AmbiLayout = value;
1101 break;
1103 case AL_AMBISONIC_SCALING_SOFT:
1104 if UNLIKELY(ReadRef(albuf->ref) != 0)
1105 context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's ambisonic scaling",
1106 buffer);
1107 else if UNLIKELY(value != AL_FUMA_SOFT && value != AL_SN3D_SOFT && value != AL_N3D_SOFT)
1108 context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic scaling %04x", value);
1109 else
1110 albuf->AmbiScaling = value;
1111 break;
1113 default:
1114 context->setError(AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
1117 END_API_FUNC
1119 AL_API void AL_APIENTRY alBuffer3i(ALuint buffer, ALenum param,
1120 ALint /*value1*/, ALint /*value2*/, ALint /*value3*/)
1121 START_API_FUNC
1123 ContextRef context{GetContextRef()};
1124 if UNLIKELY(!context) return;
1126 ALCdevice *device{context->mDevice.get()};
1127 std::lock_guard<std::mutex> _{device->BufferLock};
1129 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1130 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1131 else switch(param)
1133 default:
1134 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
1137 END_API_FUNC
1139 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum param, const ALint *values)
1140 START_API_FUNC
1142 if(values)
1144 switch(param)
1146 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1147 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1148 case AL_AMBISONIC_LAYOUT_SOFT:
1149 case AL_AMBISONIC_SCALING_SOFT:
1150 alBufferi(buffer, param, values[0]);
1151 return;
1155 ContextRef context{GetContextRef()};
1156 if UNLIKELY(!context) return;
1158 ALCdevice *device{context->mDevice.get()};
1159 std::lock_guard<std::mutex> _{device->BufferLock};
1161 ALbuffer *albuf = LookupBuffer(device, buffer);
1162 if UNLIKELY(!albuf)
1163 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1164 else if UNLIKELY(!values)
1165 context->setError(AL_INVALID_VALUE, "NULL pointer");
1166 else switch(param)
1168 case AL_LOOP_POINTS_SOFT:
1169 if UNLIKELY(ReadRef(albuf->ref) != 0)
1170 context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's loop points",
1171 buffer);
1172 else if UNLIKELY(values[0] < 0 || values[0] >= values[1]
1173 || static_cast<ALuint>(values[1]) > albuf->SampleLen)
1174 context->setError(AL_INVALID_VALUE, "Invalid loop point range %d -> %d on buffer %u",
1175 values[0], values[1], buffer);
1176 else
1178 albuf->LoopStart = static_cast<ALuint>(values[0]);
1179 albuf->LoopEnd = static_cast<ALuint>(values[1]);
1181 break;
1183 default:
1184 context->setError(AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x", param);
1187 END_API_FUNC
1190 AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum param, ALfloat *value)
1191 START_API_FUNC
1193 ContextRef context{GetContextRef()};
1194 if UNLIKELY(!context) return;
1196 ALCdevice *device{context->mDevice.get()};
1197 std::lock_guard<std::mutex> _{device->BufferLock};
1199 ALbuffer *albuf = LookupBuffer(device, buffer);
1200 if UNLIKELY(!albuf)
1201 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1202 else if UNLIKELY(!value)
1203 context->setError(AL_INVALID_VALUE, "NULL pointer");
1204 else switch(param)
1206 default:
1207 context->setError(AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
1210 END_API_FUNC
1212 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3)
1213 START_API_FUNC
1215 ContextRef context{GetContextRef()};
1216 if UNLIKELY(!context) return;
1218 ALCdevice *device{context->mDevice.get()};
1219 std::lock_guard<std::mutex> _{device->BufferLock};
1221 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1222 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1223 else if UNLIKELY(!value1 || !value2 || !value3)
1224 context->setError(AL_INVALID_VALUE, "NULL pointer");
1225 else switch(param)
1227 default:
1228 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
1231 END_API_FUNC
1233 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum param, ALfloat *values)
1234 START_API_FUNC
1236 switch(param)
1238 case AL_SEC_LENGTH_SOFT:
1239 alGetBufferf(buffer, param, values);
1240 return;
1243 ContextRef context{GetContextRef()};
1244 if UNLIKELY(!context) return;
1246 ALCdevice *device{context->mDevice.get()};
1247 std::lock_guard<std::mutex> _{device->BufferLock};
1249 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1250 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1251 else if UNLIKELY(!values)
1252 context->setError(AL_INVALID_VALUE, "NULL pointer");
1253 else switch(param)
1255 default:
1256 context->setError(AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
1259 END_API_FUNC
1262 AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum param, ALint *value)
1263 START_API_FUNC
1265 ContextRef context{GetContextRef()};
1266 if UNLIKELY(!context) return;
1268 ALCdevice *device{context->mDevice.get()};
1269 std::lock_guard<std::mutex> _{device->BufferLock};
1270 ALbuffer *albuf = LookupBuffer(device, buffer);
1271 if UNLIKELY(!albuf)
1272 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1273 else if UNLIKELY(!value)
1274 context->setError(AL_INVALID_VALUE, "NULL pointer");
1275 else switch(param)
1277 case AL_FREQUENCY:
1278 *value = static_cast<ALint>(albuf->Frequency);
1279 break;
1281 case AL_BITS:
1282 *value = static_cast<ALint>(BytesFromFmt(albuf->mFmtType) * 8);
1283 break;
1285 case AL_CHANNELS:
1286 *value = static_cast<ALint>(ChannelsFromFmt(albuf->mFmtChannels));
1287 break;
1289 case AL_SIZE:
1290 *value = static_cast<ALint>(albuf->SampleLen *
1291 FrameSizeFromFmt(albuf->mFmtChannels, albuf->mFmtType));
1292 break;
1294 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1295 *value = static_cast<ALint>(albuf->UnpackAlign);
1296 break;
1298 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1299 *value = static_cast<ALint>(albuf->PackAlign);
1300 break;
1302 case AL_AMBISONIC_LAYOUT_SOFT:
1303 *value = albuf->AmbiLayout;
1304 break;
1306 case AL_AMBISONIC_SCALING_SOFT:
1307 *value = albuf->AmbiScaling;
1308 break;
1310 default:
1311 context->setError(AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
1314 END_API_FUNC
1316 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum param, ALint *value1, ALint *value2, ALint *value3)
1317 START_API_FUNC
1319 ContextRef context{GetContextRef()};
1320 if UNLIKELY(!context) return;
1322 ALCdevice *device{context->mDevice.get()};
1323 std::lock_guard<std::mutex> _{device->BufferLock};
1324 if UNLIKELY(LookupBuffer(device, buffer) == nullptr)
1325 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1326 else if UNLIKELY(!value1 || !value2 || !value3)
1327 context->setError(AL_INVALID_VALUE, "NULL pointer");
1328 else switch(param)
1330 default:
1331 context->setError(AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
1334 END_API_FUNC
1336 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum param, ALint *values)
1337 START_API_FUNC
1339 switch(param)
1341 case AL_FREQUENCY:
1342 case AL_BITS:
1343 case AL_CHANNELS:
1344 case AL_SIZE:
1345 case AL_INTERNAL_FORMAT_SOFT:
1346 case AL_BYTE_LENGTH_SOFT:
1347 case AL_SAMPLE_LENGTH_SOFT:
1348 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1349 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1350 case AL_AMBISONIC_LAYOUT_SOFT:
1351 case AL_AMBISONIC_SCALING_SOFT:
1352 alGetBufferi(buffer, param, values);
1353 return;
1356 ContextRef context{GetContextRef()};
1357 if UNLIKELY(!context) return;
1359 ALCdevice *device{context->mDevice.get()};
1360 std::lock_guard<std::mutex> _{device->BufferLock};
1361 ALbuffer *albuf = LookupBuffer(device, buffer);
1362 if UNLIKELY(!albuf)
1363 context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
1364 else if UNLIKELY(!values)
1365 context->setError(AL_INVALID_VALUE, "NULL pointer");
1366 else switch(param)
1368 case AL_LOOP_POINTS_SOFT:
1369 values[0] = static_cast<ALint>(albuf->LoopStart);
1370 values[1] = static_cast<ALint>(albuf->LoopEnd);
1371 break;
1373 default:
1374 context->setError(AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x", param);
1377 END_API_FUNC
1380 ALuint BytesFromFmt(FmtType type)
1382 switch(type)
1384 case FmtUByte: return sizeof(ALubyte);
1385 case FmtShort: return sizeof(ALshort);
1386 case FmtFloat: return sizeof(ALfloat);
1387 case FmtDouble: return sizeof(ALdouble);
1388 case FmtMulaw: return sizeof(ALubyte);
1389 case FmtAlaw: return sizeof(ALubyte);
1391 return 0;
1393 ALuint ChannelsFromFmt(FmtChannels chans)
1395 switch(chans)
1397 case FmtMono: return 1;
1398 case FmtStereo: return 2;
1399 case FmtRear: return 2;
1400 case FmtQuad: return 4;
1401 case FmtX51: return 6;
1402 case FmtX61: return 7;
1403 case FmtX71: return 8;
1404 case FmtBFormat2D: return 3;
1405 case FmtBFormat3D: return 4;
1407 return 0;
1411 BufferSubList::~BufferSubList()
1413 uint64_t usemask{~FreeMask};
1414 while(usemask)
1416 ALsizei idx{CTZ64(usemask)};
1417 al::destroy_at(Buffers+idx);
1418 usemask &= ~(1_u64 << idx);
1420 FreeMask = ~usemask;
1421 al_free(Buffers);
1422 Buffers = nullptr;