🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1230 lines
42 KiB

  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.
  8. *
  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.
  13. *
  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
  19. */
  20. #include "config.h"
  21. #include <cstdlib>
  22. #include <cstdio>
  23. #include <cassert>
  24. #ifdef HAVE_MALLOC_H
  25. #include <malloc.h>
  26. #endif
  27. #include <tuple>
  28. #include <array>
  29. #include <vector>
  30. #include <limits>
  31. #include <algorithm>
  32. #include "alMain.h"
  33. #include "alcontext.h"
  34. #include "alu.h"
  35. #include "alError.h"
  36. #include "alBuffer.h"
  37. #include "sample_cvt.h"
  38. #include "alexcpt.h"
  39. namespace {
  40. constexpr ALbitfieldSOFT INVALID_STORAGE_MASK{~unsigned(AL_MAP_READ_BIT_SOFT |
  41. AL_MAP_WRITE_BIT_SOFT | AL_MAP_PERSISTENT_BIT_SOFT | AL_PRESERVE_DATA_BIT_SOFT)};
  42. constexpr ALbitfieldSOFT MAP_READ_WRITE_FLAGS{AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT};
  43. constexpr ALbitfieldSOFT INVALID_MAP_FLAGS{~unsigned(AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT |
  44. AL_MAP_PERSISTENT_BIT_SOFT)};
  45. ALbuffer *AllocBuffer(ALCcontext *context)
  46. {
  47. ALCdevice *device{context->Device};
  48. std::lock_guard<std::mutex> _{device->BufferLock};
  49. auto sublist = std::find_if(device->BufferList.begin(), device->BufferList.end(),
  50. [](const BufferSubList &entry) noexcept -> bool
  51. { return entry.FreeMask != 0; }
  52. );
  53. auto lidx = static_cast<ALsizei>(std::distance(device->BufferList.begin(), sublist));
  54. ALbuffer *buffer{nullptr};
  55. ALsizei slidx{0};
  56. if(LIKELY(sublist != device->BufferList.end()))
  57. {
  58. slidx = CTZ64(sublist->FreeMask);
  59. buffer = sublist->Buffers + slidx;
  60. }
  61. else
  62. {
  63. /* Don't allocate so many list entries that the 32-bit ID could
  64. * overflow...
  65. */
  66. if(UNLIKELY(device->BufferList.size() >= 1<<25))
  67. {
  68. alSetError(context, AL_OUT_OF_MEMORY, "Too many buffers allocated");
  69. return nullptr;
  70. }
  71. device->BufferList.emplace_back();
  72. sublist = device->BufferList.end() - 1;
  73. sublist->FreeMask = ~0_u64;
  74. sublist->Buffers = reinterpret_cast<ALbuffer*>(al_calloc(16, sizeof(ALbuffer)*64));
  75. if(UNLIKELY(!sublist->Buffers))
  76. {
  77. device->BufferList.pop_back();
  78. alSetError(context, AL_OUT_OF_MEMORY, "Failed to allocate buffer batch");
  79. return nullptr;
  80. }
  81. slidx = 0;
  82. buffer = sublist->Buffers + slidx;
  83. }
  84. buffer = new (buffer) ALbuffer{};
  85. /* Add 1 to avoid buffer ID 0. */
  86. buffer->id = ((lidx<<6) | slidx) + 1;
  87. sublist->FreeMask &= ~(1_u64 << slidx);
  88. return buffer;
  89. }
  90. void FreeBuffer(ALCdevice *device, ALbuffer *buffer)
  91. {
  92. ALuint id{buffer->id - 1};
  93. ALsizei lidx = id >> 6;
  94. ALsizei slidx = id & 0x3f;
  95. buffer->~ALbuffer();
  96. device->BufferList[lidx].FreeMask |= 1_u64 << slidx;
  97. }
  98. inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id)
  99. {
  100. ALuint lidx = (id-1) >> 6;
  101. ALsizei slidx = (id-1) & 0x3f;
  102. if(UNLIKELY(lidx >= device->BufferList.size()))
  103. return nullptr;
  104. BufferSubList &sublist = device->BufferList[lidx];
  105. if(UNLIKELY(sublist.FreeMask & (1_u64 << slidx)))
  106. return nullptr;
  107. return sublist.Buffers + slidx;
  108. }
  109. ALsizei SanitizeAlignment(UserFmtType type, ALsizei align)
  110. {
  111. if(align < 0)
  112. return 0;
  113. if(align == 0)
  114. {
  115. if(type == UserFmtIMA4)
  116. {
  117. /* Here is where things vary:
  118. * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
  119. * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
  120. */
  121. return 65;
  122. }
  123. if(type == UserFmtMSADPCM)
  124. return 64;
  125. return 1;
  126. }
  127. if(type == UserFmtIMA4)
  128. {
  129. /* IMA4 block alignment must be a multiple of 8, plus 1. */
  130. if((align&7) == 1) return align;
  131. return 0;
  132. }
  133. if(type == UserFmtMSADPCM)
  134. {
  135. /* MSADPCM block alignment must be a multiple of 2. */
  136. if((align&1) == 0) return align;
  137. return 0;
  138. }
  139. return align;
  140. }
  141. const ALchar *NameFromUserFmtType(UserFmtType type)
  142. {
  143. switch(type)
  144. {
  145. case UserFmtUByte: return "Unsigned Byte";
  146. case UserFmtShort: return "Signed Short";
  147. case UserFmtFloat: return "Float32";
  148. case UserFmtDouble: return "Float64";
  149. case UserFmtMulaw: return "muLaw";
  150. case UserFmtAlaw: return "aLaw";
  151. case UserFmtIMA4: return "IMA4 ADPCM";
  152. case UserFmtMSADPCM: return "MSADPCM";
  153. }
  154. return "<internal type error>";
  155. }
  156. /*
  157. * LoadData
  158. *
  159. * Loads the specified data into the buffer, using the specified format.
  160. */
  161. void LoadData(ALCcontext *context, ALbuffer *ALBuf, ALuint freq, ALsizei size, UserFmtChannels SrcChannels, UserFmtType SrcType, const ALvoid *data, ALbitfieldSOFT access)
  162. {
  163. if(UNLIKELY(ReadRef(&ALBuf->ref) != 0 || ALBuf->MappedAccess != 0))
  164. SETERR_RETURN(context, AL_INVALID_OPERATION,, "Modifying storage for in-use buffer %u",
  165. ALBuf->id);
  166. /* Currently no channel configurations need to be converted. */
  167. FmtChannels DstChannels{FmtMono};
  168. switch(SrcChannels)
  169. {
  170. case UserFmtMono: DstChannels = FmtMono; break;
  171. case UserFmtStereo: DstChannels = FmtStereo; break;
  172. case UserFmtRear: DstChannels = FmtRear; break;
  173. case UserFmtQuad: DstChannels = FmtQuad; break;
  174. case UserFmtX51: DstChannels = FmtX51; break;
  175. case UserFmtX61: DstChannels = FmtX61; break;
  176. case UserFmtX71: DstChannels = FmtX71; break;
  177. case UserFmtBFormat2D: DstChannels = FmtBFormat2D; break;
  178. case UserFmtBFormat3D: DstChannels = FmtBFormat3D; break;
  179. }
  180. if (UNLIKELY(static_cast<long>(SrcChannels) !=
  181. static_cast<long>(DstChannels)))
  182. SETERR_RETURN(context, AL_INVALID_ENUM, , "Invalid format");
  183. /* IMA4 and MSADPCM convert to 16-bit short. */
  184. FmtType DstType{FmtUByte};
  185. switch(SrcType)
  186. {
  187. case UserFmtUByte: DstType = FmtUByte; break;
  188. case UserFmtShort: DstType = FmtShort; break;
  189. case UserFmtFloat: DstType = FmtFloat; break;
  190. case UserFmtDouble: DstType = FmtDouble; break;
  191. case UserFmtAlaw: DstType = FmtAlaw; break;
  192. case UserFmtMulaw: DstType = FmtMulaw; break;
  193. case UserFmtIMA4: DstType = FmtShort; break;
  194. case UserFmtMSADPCM: DstType = FmtShort; break;
  195. }
  196. /* TODO: Currently we can only map samples when they're not converted. To
  197. * allow it would need some kind of double-buffering to hold onto a copy of
  198. * the original data.
  199. */
  200. if((access&MAP_READ_WRITE_FLAGS))
  201. {
  202. if (UNLIKELY(static_cast<long>(SrcType) != static_cast<long>(DstType)))
  203. SETERR_RETURN(context, AL_INVALID_VALUE, ,
  204. "%s samples cannot be mapped",
  205. NameFromUserFmtType(SrcType));
  206. }
  207. ALsizei unpackalign{ALBuf->UnpackAlign.load()};
  208. ALsizei align{SanitizeAlignment(SrcType, unpackalign)};
  209. if(UNLIKELY(align < 1))
  210. SETERR_RETURN(context, AL_INVALID_VALUE,, "Invalid unpack alignment %d for %s samples",
  211. unpackalign, NameFromUserFmtType(SrcType));
  212. if((access&AL_PRESERVE_DATA_BIT_SOFT))
  213. {
  214. /* Can only preserve data with the same format and alignment. */
  215. if(UNLIKELY(ALBuf->mFmtChannels != DstChannels || ALBuf->OriginalType != SrcType))
  216. SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched format");
  217. if(UNLIKELY(ALBuf->OriginalAlign != align))
  218. SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched alignment");
  219. }
  220. /* Convert the input/source size in bytes to sample frames using the unpack
  221. * block alignment.
  222. */
  223. ALsizei SrcByteAlign{
  224. (SrcType == UserFmtIMA4) ? ((align-1)/2 + 4) * ChannelsFromUserFmt(SrcChannels) :
  225. (SrcType == UserFmtMSADPCM) ? ((align-2)/2 + 7) * ChannelsFromUserFmt(SrcChannels) :
  226. (align * FrameSizeFromUserFmt(SrcChannels, SrcType))
  227. };
  228. if(UNLIKELY((size%SrcByteAlign) != 0))
  229. SETERR_RETURN(context, AL_INVALID_VALUE,,
  230. "Data size %d is not a multiple of frame size %d (%d unpack alignment)",
  231. size, SrcByteAlign, align);
  232. if(UNLIKELY(size/SrcByteAlign > std::numeric_limits<ALsizei>::max()/align))
  233. SETERR_RETURN(context, AL_OUT_OF_MEMORY,,
  234. "Buffer size overflow, %d blocks x %d samples per block", size/SrcByteAlign, align);
  235. ALsizei frames{size / SrcByteAlign * align};
  236. /* Convert the sample frames to the number of bytes needed for internal
  237. * storage.
  238. */
  239. ALsizei NumChannels{ChannelsFromFmt(DstChannels)};
  240. ALsizei FrameSize{NumChannels * BytesFromFmt(DstType)};
  241. if(UNLIKELY(frames > std::numeric_limits<ALsizei>::max()/FrameSize))
  242. SETERR_RETURN(context, AL_OUT_OF_MEMORY,,
  243. "Buffer size overflow, %d frames x %d bytes per frame", frames, FrameSize);
  244. ALsizei newsize{frames*FrameSize};
  245. /* Round up to the next 16-byte multiple. This could reallocate only when
  246. * increasing or the new size is less than half the current, but then the
  247. * buffer's AL_SIZE would not be very reliable for accounting buffer memory
  248. * usage, and reporting the real size could cause problems for apps that
  249. * use AL_SIZE to try to get the buffer's play length.
  250. */
  251. if(LIKELY(newsize <= std::numeric_limits<ALsizei>::max()-15))
  252. newsize = (newsize+15) & ~0xf;
  253. if(newsize != ALBuf->BytesAlloc)
  254. {
  255. al::vector<ALbyte,16> newdata(newsize);
  256. if((access&AL_PRESERVE_DATA_BIT_SOFT))
  257. {
  258. ALsizei tocopy{std::min(newsize, ALBuf->BytesAlloc)};
  259. std::copy_n(ALBuf->mData.begin(), tocopy, newdata.begin());
  260. }
  261. ALBuf->mData = std::move(newdata);
  262. ALBuf->BytesAlloc = newsize;
  263. }
  264. if(SrcType == UserFmtIMA4)
  265. {
  266. assert(DstType == FmtShort);
  267. if(data != nullptr && !ALBuf->mData.empty())
  268. Convert_ALshort_ALima4(reinterpret_cast<ALshort*>(ALBuf->mData.data()),
  269. static_cast<const ALubyte*>(data), NumChannels, frames, align);
  270. ALBuf->OriginalAlign = align;
  271. }
  272. else if(SrcType == UserFmtMSADPCM)
  273. {
  274. assert(DstType == FmtShort);
  275. if(data != nullptr && !ALBuf->mData.empty())
  276. Convert_ALshort_ALmsadpcm(reinterpret_cast<ALshort*>(ALBuf->mData.data()),
  277. static_cast<const ALubyte*>(data), NumChannels, frames, align);
  278. ALBuf->OriginalAlign = align;
  279. }
  280. else
  281. {
  282. assert(static_cast<long>(SrcType) == static_cast<long>(DstType));
  283. if(data != nullptr && !ALBuf->mData.empty())
  284. std::copy_n(static_cast<const ALbyte*>(data), frames*FrameSize, ALBuf->mData.begin());
  285. ALBuf->OriginalAlign = 1;
  286. }
  287. ALBuf->OriginalSize = size;
  288. ALBuf->OriginalType = SrcType;
  289. ALBuf->Frequency = freq;
  290. ALBuf->mFmtChannels = DstChannels;
  291. ALBuf->mFmtType = DstType;
  292. ALBuf->Access = access;
  293. ALBuf->SampleLen = frames;
  294. ALBuf->LoopStart = 0;
  295. ALBuf->LoopEnd = ALBuf->SampleLen;
  296. }
  297. using DecompResult = std::tuple<bool, UserFmtChannels, UserFmtType>;
  298. DecompResult DecomposeUserFormat(ALenum format)
  299. {
  300. struct FormatMap {
  301. ALenum format;
  302. UserFmtChannels channels;
  303. UserFmtType type;
  304. };
  305. static constexpr std::array<FormatMap,46> UserFmtList{{
  306. { AL_FORMAT_MONO8, UserFmtMono, UserFmtUByte },
  307. { AL_FORMAT_MONO16, UserFmtMono, UserFmtShort },
  308. { AL_FORMAT_MONO_FLOAT32, UserFmtMono, UserFmtFloat },
  309. { AL_FORMAT_MONO_DOUBLE_EXT, UserFmtMono, UserFmtDouble },
  310. { AL_FORMAT_MONO_IMA4, UserFmtMono, UserFmtIMA4 },
  311. { AL_FORMAT_MONO_MSADPCM_SOFT, UserFmtMono, UserFmtMSADPCM },
  312. { AL_FORMAT_MONO_MULAW, UserFmtMono, UserFmtMulaw },
  313. { AL_FORMAT_MONO_ALAW_EXT, UserFmtMono, UserFmtAlaw },
  314. { AL_FORMAT_STEREO8, UserFmtStereo, UserFmtUByte },
  315. { AL_FORMAT_STEREO16, UserFmtStereo, UserFmtShort },
  316. { AL_FORMAT_STEREO_FLOAT32, UserFmtStereo, UserFmtFloat },
  317. { AL_FORMAT_STEREO_DOUBLE_EXT, UserFmtStereo, UserFmtDouble },
  318. { AL_FORMAT_STEREO_IMA4, UserFmtStereo, UserFmtIMA4 },
  319. { AL_FORMAT_STEREO_MSADPCM_SOFT, UserFmtStereo, UserFmtMSADPCM },
  320. { AL_FORMAT_STEREO_MULAW, UserFmtStereo, UserFmtMulaw },
  321. { AL_FORMAT_STEREO_ALAW_EXT, UserFmtStereo, UserFmtAlaw },
  322. { AL_FORMAT_REAR8, UserFmtRear, UserFmtUByte },
  323. { AL_FORMAT_REAR16, UserFmtRear, UserFmtShort },
  324. { AL_FORMAT_REAR32, UserFmtRear, UserFmtFloat },
  325. { AL_FORMAT_REAR_MULAW, UserFmtRear, UserFmtMulaw },
  326. { AL_FORMAT_QUAD8_LOKI, UserFmtQuad, UserFmtUByte },
  327. { AL_FORMAT_QUAD16_LOKI, UserFmtQuad, UserFmtShort },
  328. { AL_FORMAT_QUAD8, UserFmtQuad, UserFmtUByte },
  329. { AL_FORMAT_QUAD16, UserFmtQuad, UserFmtShort },
  330. { AL_FORMAT_QUAD32, UserFmtQuad, UserFmtFloat },
  331. { AL_FORMAT_QUAD_MULAW, UserFmtQuad, UserFmtMulaw },
  332. { AL_FORMAT_51CHN8, UserFmtX51, UserFmtUByte },
  333. { AL_FORMAT_51CHN16, UserFmtX51, UserFmtShort },
  334. { AL_FORMAT_51CHN32, UserFmtX51, UserFmtFloat },
  335. { AL_FORMAT_51CHN_MULAW, UserFmtX51, UserFmtMulaw },
  336. { AL_FORMAT_61CHN8, UserFmtX61, UserFmtUByte },
  337. { AL_FORMAT_61CHN16, UserFmtX61, UserFmtShort },
  338. { AL_FORMAT_61CHN32, UserFmtX61, UserFmtFloat },
  339. { AL_FORMAT_61CHN_MULAW, UserFmtX61, UserFmtMulaw },
  340. { AL_FORMAT_71CHN8, UserFmtX71, UserFmtUByte },
  341. { AL_FORMAT_71CHN16, UserFmtX71, UserFmtShort },
  342. { AL_FORMAT_71CHN32, UserFmtX71, UserFmtFloat },
  343. { AL_FORMAT_71CHN_MULAW, UserFmtX71, UserFmtMulaw },
  344. { AL_FORMAT_BFORMAT2D_8, UserFmtBFormat2D, UserFmtUByte },
  345. { AL_FORMAT_BFORMAT2D_16, UserFmtBFormat2D, UserFmtShort },
  346. { AL_FORMAT_BFORMAT2D_FLOAT32, UserFmtBFormat2D, UserFmtFloat },
  347. { AL_FORMAT_BFORMAT2D_MULAW, UserFmtBFormat2D, UserFmtMulaw },
  348. { AL_FORMAT_BFORMAT3D_8, UserFmtBFormat3D, UserFmtUByte },
  349. { AL_FORMAT_BFORMAT3D_16, UserFmtBFormat3D, UserFmtShort },
  350. { AL_FORMAT_BFORMAT3D_FLOAT32, UserFmtBFormat3D, UserFmtFloat },
  351. { AL_FORMAT_BFORMAT3D_MULAW, UserFmtBFormat3D, UserFmtMulaw },
  352. }};
  353. DecompResult ret{};
  354. for(const auto &fmt : UserFmtList)
  355. {
  356. if(fmt.format == format)
  357. {
  358. std::get<0>(ret) = true;
  359. std::get<1>(ret) = fmt.channels;
  360. std::get<2>(ret) = fmt.type;
  361. break;
  362. }
  363. }
  364. return ret;
  365. }
  366. } // namespace
  367. AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
  368. START_API_FUNC
  369. {
  370. ContextRef context{GetContextRef()};
  371. if(UNLIKELY(!context)) return;
  372. if(UNLIKELY(n < 0))
  373. {
  374. alSetError(context.get(), AL_INVALID_VALUE, "Generating %d buffers", n);
  375. return;
  376. }
  377. if(LIKELY(n == 1))
  378. {
  379. /* Special handling for the easy and normal case. */
  380. ALbuffer *buffer = AllocBuffer(context.get());
  381. if(buffer) buffers[0] = buffer->id;
  382. }
  383. else if(n > 1)
  384. {
  385. /* Store the allocated buffer IDs in a separate local list, to avoid
  386. * modifying the user storage in case of failure.
  387. */
  388. al::vector<ALuint> ids;
  389. ids.reserve(n);
  390. do {
  391. ALbuffer *buffer = AllocBuffer(context.get());
  392. if(!buffer)
  393. {
  394. alDeleteBuffers(static_cast<ALsizei>(ids.size()), ids.data());
  395. return;
  396. }
  397. ids.emplace_back(buffer->id);
  398. } while(--n);
  399. std::copy(ids.begin(), ids.end(), buffers);
  400. }
  401. }
  402. END_API_FUNC
  403. AL_API ALvoid AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
  404. START_API_FUNC
  405. {
  406. ContextRef context{GetContextRef()};
  407. if(UNLIKELY(!context)) return;
  408. if(UNLIKELY(n < 0))
  409. {
  410. alSetError(context.get(), AL_INVALID_VALUE, "Deleting %d buffers", n);
  411. return;
  412. }
  413. if(UNLIKELY(n == 0))
  414. return;
  415. ALCdevice *device = context->Device;
  416. std::lock_guard<std::mutex> _{device->BufferLock};
  417. /* First try to find any buffers that are invalid or in-use. */
  418. const ALuint *buffers_end = buffers + n;
  419. auto invbuf = std::find_if(buffers, buffers_end,
  420. [device, &context](ALuint bid) -> bool
  421. {
  422. if(!bid) return false;
  423. ALbuffer *ALBuf = LookupBuffer(device, bid);
  424. if(UNLIKELY(!ALBuf))
  425. {
  426. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", bid);
  427. return true;
  428. }
  429. if(UNLIKELY(ReadRef(&ALBuf->ref) != 0))
  430. {
  431. alSetError(context.get(), AL_INVALID_OPERATION, "Deleting in-use buffer %u", bid);
  432. return true;
  433. }
  434. return false;
  435. }
  436. );
  437. if(LIKELY(invbuf == buffers_end))
  438. {
  439. /* All good. Delete non-0 buffer IDs. */
  440. std::for_each(buffers, buffers_end,
  441. [device](ALuint bid) -> void
  442. {
  443. ALbuffer *buffer{bid ? LookupBuffer(device, bid) : nullptr};
  444. if(buffer) FreeBuffer(device, buffer);
  445. }
  446. );
  447. }
  448. }
  449. END_API_FUNC
  450. AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
  451. START_API_FUNC
  452. {
  453. ContextRef context{GetContextRef()};
  454. if(LIKELY(context))
  455. {
  456. ALCdevice *device = context->Device;
  457. std::lock_guard<std::mutex> _{device->BufferLock};
  458. if(!buffer || LookupBuffer(device, buffer))
  459. return AL_TRUE;
  460. }
  461. return AL_FALSE;
  462. }
  463. END_API_FUNC
  464. AL_API ALvoid AL_APIENTRY alBufferData(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq)
  465. START_API_FUNC
  466. { alBufferStorageSOFT(buffer, format, data, size, freq, 0); }
  467. END_API_FUNC
  468. AL_API void AL_APIENTRY alBufferStorageSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq, ALbitfieldSOFT flags)
  469. START_API_FUNC
  470. {
  471. ContextRef context{GetContextRef()};
  472. if(UNLIKELY(!context)) return;
  473. ALCdevice *device = context->Device;
  474. std::lock_guard<std::mutex> _{device->BufferLock};
  475. ALbuffer *albuf = LookupBuffer(device, buffer);
  476. if(UNLIKELY(!albuf))
  477. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  478. else if(UNLIKELY(size < 0))
  479. alSetError(context.get(), AL_INVALID_VALUE, "Negative storage size %d", size);
  480. else if(UNLIKELY(freq < 1))
  481. alSetError(context.get(), AL_INVALID_VALUE, "Invalid sample rate %d", freq);
  482. else if(UNLIKELY((flags&INVALID_STORAGE_MASK) != 0))
  483. alSetError(context.get(), AL_INVALID_VALUE, "Invalid storage flags 0x%x",
  484. flags&INVALID_STORAGE_MASK);
  485. else if(UNLIKELY((flags&AL_MAP_PERSISTENT_BIT_SOFT) && !(flags&MAP_READ_WRITE_FLAGS)))
  486. alSetError(context.get(), AL_INVALID_VALUE,
  487. "Declaring persistently mapped storage without read or write access");
  488. else
  489. {
  490. UserFmtType srctype{UserFmtUByte};
  491. UserFmtChannels srcchannels{UserFmtMono};
  492. bool success;
  493. std::tie(success, srcchannels, srctype) = DecomposeUserFormat(format);
  494. if(UNLIKELY(!success))
  495. alSetError(context.get(), AL_INVALID_ENUM, "Invalid format 0x%04x", format);
  496. else
  497. LoadData(context.get(), albuf, freq, size, srcchannels, srctype, data, flags);
  498. }
  499. }
  500. END_API_FUNC
  501. AL_API void* AL_APIENTRY alMapBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length, ALbitfieldSOFT access)
  502. START_API_FUNC
  503. {
  504. ContextRef context{GetContextRef()};
  505. if(UNLIKELY(!context)) return nullptr;
  506. ALCdevice *device = context->Device;
  507. std::lock_guard<std::mutex> _{device->BufferLock};
  508. ALbuffer *albuf = LookupBuffer(device, buffer);
  509. if(UNLIKELY(!albuf))
  510. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  511. else if(UNLIKELY((access&INVALID_MAP_FLAGS) != 0))
  512. alSetError(context.get(), AL_INVALID_VALUE, "Invalid map flags 0x%x", access&INVALID_MAP_FLAGS);
  513. else if(UNLIKELY(!(access&MAP_READ_WRITE_FLAGS)))
  514. alSetError(context.get(), AL_INVALID_VALUE, "Mapping buffer %u without read or write access",
  515. buffer);
  516. else
  517. {
  518. ALbitfieldSOFT unavailable = (albuf->Access^access) & access;
  519. if(UNLIKELY(ReadRef(&albuf->ref) != 0 && !(access&AL_MAP_PERSISTENT_BIT_SOFT)))
  520. alSetError(context.get(), AL_INVALID_OPERATION,
  521. "Mapping in-use buffer %u without persistent mapping", buffer);
  522. else if(UNLIKELY(albuf->MappedAccess != 0))
  523. alSetError(context.get(), AL_INVALID_OPERATION, "Mapping already-mapped buffer %u", buffer);
  524. else if(UNLIKELY((unavailable&AL_MAP_READ_BIT_SOFT)))
  525. alSetError(context.get(), AL_INVALID_VALUE,
  526. "Mapping buffer %u for reading without read access", buffer);
  527. else if(UNLIKELY((unavailable&AL_MAP_WRITE_BIT_SOFT)))
  528. alSetError(context.get(), AL_INVALID_VALUE,
  529. "Mapping buffer %u for writing without write access", buffer);
  530. else if(UNLIKELY((unavailable&AL_MAP_PERSISTENT_BIT_SOFT)))
  531. alSetError(context.get(), AL_INVALID_VALUE,
  532. "Mapping buffer %u persistently without persistent access", buffer);
  533. else if(UNLIKELY(offset < 0 || offset >= albuf->OriginalSize ||
  534. length <= 0 || length > albuf->OriginalSize - offset))
  535. alSetError(context.get(), AL_INVALID_VALUE, "Mapping invalid range %d+%d for buffer %u",
  536. offset, length, buffer);
  537. else
  538. {
  539. void *retval = albuf->mData.data() + offset;
  540. albuf->MappedAccess = access;
  541. albuf->MappedOffset = offset;
  542. albuf->MappedSize = length;
  543. return retval;
  544. }
  545. }
  546. return nullptr;
  547. }
  548. END_API_FUNC
  549. AL_API void AL_APIENTRY alUnmapBufferSOFT(ALuint buffer)
  550. START_API_FUNC
  551. {
  552. ContextRef context{GetContextRef()};
  553. if(UNLIKELY(!context)) return;
  554. ALCdevice *device = context->Device;
  555. std::lock_guard<std::mutex> _{device->BufferLock};
  556. ALbuffer *albuf = LookupBuffer(device, buffer);
  557. if(UNLIKELY(!albuf))
  558. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  559. else if(albuf->MappedAccess == 0)
  560. alSetError(context.get(), AL_INVALID_OPERATION, "Unmapping unmapped buffer %u", buffer);
  561. else
  562. {
  563. albuf->MappedAccess = 0;
  564. albuf->MappedOffset = 0;
  565. albuf->MappedSize = 0;
  566. }
  567. }
  568. END_API_FUNC
  569. AL_API void AL_APIENTRY alFlushMappedBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length)
  570. START_API_FUNC
  571. {
  572. ContextRef context{GetContextRef()};
  573. if(UNLIKELY(!context)) return;
  574. ALCdevice *device = context->Device;
  575. std::lock_guard<std::mutex> _{device->BufferLock};
  576. ALbuffer *albuf = LookupBuffer(device, buffer);
  577. if(UNLIKELY(!albuf))
  578. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  579. else if(UNLIKELY(!(albuf->MappedAccess&AL_MAP_WRITE_BIT_SOFT)))
  580. alSetError(context.get(), AL_INVALID_OPERATION,
  581. "Flushing buffer %u while not mapped for writing", buffer);
  582. else if(UNLIKELY(offset < albuf->MappedOffset ||
  583. offset >= albuf->MappedOffset+albuf->MappedSize ||
  584. length <= 0 || length > albuf->MappedOffset+albuf->MappedSize-offset))
  585. alSetError(context.get(), AL_INVALID_VALUE, "Flushing invalid range %d+%d on buffer %u",
  586. offset, length, buffer);
  587. else
  588. {
  589. /* FIXME: Need to use some method of double-buffering for the mixer and
  590. * app to hold separate memory, which can be safely transfered
  591. * asynchronously. Currently we just say the app shouldn't write where
  592. * OpenAL's reading, and hope for the best...
  593. */
  594. std::atomic_thread_fence(std::memory_order_seq_cst);
  595. }
  596. }
  597. END_API_FUNC
  598. AL_API ALvoid AL_APIENTRY alBufferSubDataSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei offset, ALsizei length)
  599. START_API_FUNC
  600. {
  601. ContextRef context{GetContextRef()};
  602. if(UNLIKELY(!context)) return;
  603. ALCdevice *device = context->Device;
  604. std::lock_guard<std::mutex> _{device->BufferLock};
  605. ALbuffer *albuf = LookupBuffer(device, buffer);
  606. if(UNLIKELY(!albuf))
  607. {
  608. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  609. return;
  610. }
  611. UserFmtType srctype{UserFmtUByte};
  612. UserFmtChannels srcchannels{UserFmtMono};
  613. bool success;
  614. std::tie(success, srcchannels, srctype) = DecomposeUserFormat(format);
  615. if(UNLIKELY(!success))
  616. {
  617. alSetError(context.get(), AL_INVALID_ENUM, "Invalid format 0x%04x", format);
  618. return;
  619. }
  620. ALsizei unpack_align{albuf->UnpackAlign.load()};
  621. ALsizei align{SanitizeAlignment(srctype, unpack_align)};
  622. if(UNLIKELY(align < 1))
  623. alSetError(context.get(), AL_INVALID_VALUE, "Invalid unpack alignment %d", unpack_align);
  624. else if (UNLIKELY(static_cast<long>(srcchannels) !=
  625. static_cast<long>(albuf->mFmtChannels) ||
  626. srctype != albuf->OriginalType))
  627. alSetError(context.get(), AL_INVALID_ENUM,
  628. "Unpacking data with mismatched format");
  629. else if(UNLIKELY(align != albuf->OriginalAlign))
  630. alSetError(context.get(), AL_INVALID_VALUE,
  631. "Unpacking data with alignment %u does not match original alignment %u",
  632. align, albuf->OriginalAlign);
  633. else if(UNLIKELY(albuf->MappedAccess != 0))
  634. alSetError(context.get(), AL_INVALID_OPERATION, "Unpacking data into mapped buffer %u",
  635. buffer);
  636. else
  637. {
  638. ALsizei num_chans{ChannelsFromFmt(albuf->mFmtChannels)};
  639. ALsizei frame_size{num_chans * BytesFromFmt(albuf->mFmtType)};
  640. ALsizei byte_align{
  641. (albuf->OriginalType == UserFmtIMA4) ? ((align-1)/2 + 4) * num_chans :
  642. (albuf->OriginalType == UserFmtMSADPCM) ? ((align-2)/2 + 7) * num_chans :
  643. (align * frame_size)
  644. };
  645. if(UNLIKELY(offset < 0 || length < 0 || offset > albuf->OriginalSize ||
  646. length > albuf->OriginalSize-offset))
  647. alSetError(context.get(), AL_INVALID_VALUE, "Invalid data sub-range %d+%d on buffer %u",
  648. offset, length, buffer);
  649. else if(UNLIKELY((offset%byte_align) != 0))
  650. alSetError(context.get(), AL_INVALID_VALUE,
  651. "Sub-range offset %d is not a multiple of frame size %d (%d unpack alignment)",
  652. offset, byte_align, align);
  653. else if(UNLIKELY((length%byte_align) != 0))
  654. alSetError(context.get(), AL_INVALID_VALUE,
  655. "Sub-range length %d is not a multiple of frame size %d (%d unpack alignment)",
  656. length, byte_align, align);
  657. else
  658. {
  659. /* offset -> byte offset, length -> sample count */
  660. offset = offset/byte_align * align * frame_size;
  661. length = length/byte_align * align;
  662. void *dst = albuf->mData.data() + offset;
  663. if(srctype == UserFmtIMA4 && albuf->mFmtType == FmtShort)
  664. Convert_ALshort_ALima4(static_cast<ALshort*>(dst),
  665. static_cast<const ALubyte*>(data), num_chans, length, align);
  666. else if(srctype == UserFmtMSADPCM && albuf->mFmtType == FmtShort)
  667. Convert_ALshort_ALmsadpcm(static_cast<ALshort*>(dst),
  668. static_cast<const ALubyte*>(data), num_chans, length, align);
  669. else
  670. {
  671. assert(static_cast<long>(srctype) ==
  672. static_cast<long>(albuf->mFmtType));
  673. memcpy(dst, data, length * frame_size);
  674. }
  675. }
  676. }
  677. }
  678. END_API_FUNC
  679. AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint UNUSED(buffer),
  680. ALuint UNUSED(samplerate), ALenum UNUSED(internalformat), ALsizei UNUSED(samples),
  681. ALenum UNUSED(channels), ALenum UNUSED(type), const ALvoid *UNUSED(data))
  682. START_API_FUNC
  683. {
  684. ContextRef context{GetContextRef()};
  685. if(UNLIKELY(!context)) return;
  686. alSetError(context.get(), AL_INVALID_OPERATION, "alBufferSamplesSOFT not supported");
  687. }
  688. END_API_FUNC
  689. AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint UNUSED(buffer),
  690. ALsizei UNUSED(offset), ALsizei UNUSED(samples),
  691. ALenum UNUSED(channels), ALenum UNUSED(type), const ALvoid *UNUSED(data))
  692. START_API_FUNC
  693. {
  694. ContextRef context{GetContextRef()};
  695. if(UNLIKELY(!context)) return;
  696. alSetError(context.get(), AL_INVALID_OPERATION, "alBufferSubSamplesSOFT not supported");
  697. }
  698. END_API_FUNC
  699. AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint UNUSED(buffer),
  700. ALsizei UNUSED(offset), ALsizei UNUSED(samples),
  701. ALenum UNUSED(channels), ALenum UNUSED(type), ALvoid *UNUSED(data))
  702. START_API_FUNC
  703. {
  704. ContextRef context{GetContextRef()};
  705. if(UNLIKELY(!context)) return;
  706. alSetError(context.get(), AL_INVALID_OPERATION, "alGetBufferSamplesSOFT not supported");
  707. }
  708. END_API_FUNC
  709. AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum UNUSED(format))
  710. START_API_FUNC
  711. {
  712. ContextRef context{GetContextRef()};
  713. if(!context) return AL_FALSE;
  714. alSetError(context.get(), AL_INVALID_OPERATION, "alIsBufferFormatSupportedSOFT not supported");
  715. return AL_FALSE;
  716. }
  717. END_API_FUNC
  718. AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum param, ALfloat UNUSED(value))
  719. START_API_FUNC
  720. {
  721. ContextRef context{GetContextRef()};
  722. if(UNLIKELY(!context)) return;
  723. ALCdevice *device = context->Device;
  724. std::lock_guard<std::mutex> _{device->BufferLock};
  725. if(UNLIKELY(LookupBuffer(device, buffer) == nullptr))
  726. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  727. else switch(param)
  728. {
  729. default:
  730. alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
  731. }
  732. }
  733. END_API_FUNC
  734. AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum param, ALfloat UNUSED(value1), ALfloat UNUSED(value2), ALfloat UNUSED(value3))
  735. START_API_FUNC
  736. {
  737. ContextRef context{GetContextRef()};
  738. if(UNLIKELY(!context)) return;
  739. ALCdevice *device = context->Device;
  740. std::lock_guard<std::mutex> _{device->BufferLock};
  741. if(UNLIKELY(LookupBuffer(device, buffer) == nullptr))
  742. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  743. else switch(param)
  744. {
  745. default:
  746. alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
  747. }
  748. }
  749. END_API_FUNC
  750. AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum param, const ALfloat *values)
  751. START_API_FUNC
  752. {
  753. ContextRef context{GetContextRef()};
  754. if(UNLIKELY(!context)) return;
  755. ALCdevice *device = context->Device;
  756. std::lock_guard<std::mutex> _{device->BufferLock};
  757. if(UNLIKELY(LookupBuffer(device, buffer) == nullptr))
  758. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  759. else if(UNLIKELY(!values))
  760. alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
  761. else switch(param)
  762. {
  763. default:
  764. alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
  765. }
  766. }
  767. END_API_FUNC
  768. AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum param, ALint value)
  769. START_API_FUNC
  770. {
  771. ContextRef context{GetContextRef()};
  772. if(UNLIKELY(!context)) return;
  773. ALCdevice *device = context->Device;
  774. std::lock_guard<std::mutex> _{device->BufferLock};
  775. ALbuffer *albuf = LookupBuffer(device, buffer);
  776. if(UNLIKELY(!albuf))
  777. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  778. else switch(param)
  779. {
  780. case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
  781. if(UNLIKELY(value < 0))
  782. alSetError(context.get(), AL_INVALID_VALUE, "Invalid unpack block alignment %d", value);
  783. else
  784. albuf->UnpackAlign.store(value);
  785. break;
  786. case AL_PACK_BLOCK_ALIGNMENT_SOFT:
  787. if(UNLIKELY(value < 0))
  788. alSetError(context.get(), AL_INVALID_VALUE, "Invalid pack block alignment %d", value);
  789. else
  790. albuf->PackAlign.store(value);
  791. break;
  792. default:
  793. alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
  794. }
  795. }
  796. END_API_FUNC
  797. AL_API void AL_APIENTRY alBuffer3i(ALuint buffer, ALenum param, ALint UNUSED(value1), ALint UNUSED(value2), ALint UNUSED(value3))
  798. START_API_FUNC
  799. {
  800. ContextRef context{GetContextRef()};
  801. if(UNLIKELY(!context)) return;
  802. ALCdevice *device = context->Device;
  803. std::lock_guard<std::mutex> _{device->BufferLock};
  804. if(UNLIKELY(LookupBuffer(device, buffer) == nullptr))
  805. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  806. else switch(param)
  807. {
  808. default:
  809. alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
  810. }
  811. }
  812. END_API_FUNC
  813. AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum param, const ALint *values)
  814. START_API_FUNC
  815. {
  816. if(values)
  817. {
  818. switch(param)
  819. {
  820. case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
  821. case AL_PACK_BLOCK_ALIGNMENT_SOFT:
  822. alBufferi(buffer, param, values[0]);
  823. return;
  824. }
  825. }
  826. ContextRef context{GetContextRef()};
  827. if(UNLIKELY(!context)) return;
  828. ALCdevice *device = context->Device;
  829. std::lock_guard<std::mutex> _{device->BufferLock};
  830. ALbuffer *albuf = LookupBuffer(device, buffer);
  831. if(UNLIKELY(!albuf))
  832. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  833. else if(UNLIKELY(!values))
  834. alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
  835. else switch(param)
  836. {
  837. case AL_LOOP_POINTS_SOFT:
  838. if(UNLIKELY(ReadRef(&albuf->ref) != 0))
  839. alSetError(context.get(), AL_INVALID_OPERATION, "Modifying in-use buffer %u's loop points",
  840. buffer);
  841. else if(UNLIKELY(values[0] >= values[1] || values[0] < 0 || values[1] > albuf->SampleLen))
  842. alSetError(context.get(), AL_INVALID_VALUE, "Invalid loop point range %d -> %d o buffer %u",
  843. values[0], values[1], buffer);
  844. else
  845. {
  846. albuf->LoopStart = values[0];
  847. albuf->LoopEnd = values[1];
  848. }
  849. break;
  850. default:
  851. alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x",
  852. param);
  853. }
  854. }
  855. END_API_FUNC
  856. AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum param, ALfloat *value)
  857. START_API_FUNC
  858. {
  859. ContextRef context{GetContextRef()};
  860. if(UNLIKELY(!context)) return;
  861. ALCdevice *device = context->Device;
  862. std::lock_guard<std::mutex> _{device->BufferLock};
  863. ALbuffer *albuf = LookupBuffer(device, buffer);
  864. if(UNLIKELY(!albuf))
  865. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  866. else if(UNLIKELY(!value))
  867. alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
  868. else switch(param)
  869. {
  870. default:
  871. alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
  872. }
  873. }
  874. END_API_FUNC
  875. AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3)
  876. START_API_FUNC
  877. {
  878. ContextRef context{GetContextRef()};
  879. if(UNLIKELY(!context)) return;
  880. ALCdevice *device = context->Device;
  881. std::lock_guard<std::mutex> _{device->BufferLock};
  882. if(UNLIKELY(LookupBuffer(device, buffer) == nullptr))
  883. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  884. else if(UNLIKELY(!value1 || !value2 || !value3))
  885. alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
  886. else switch(param)
  887. {
  888. default:
  889. alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
  890. }
  891. }
  892. END_API_FUNC
  893. AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum param, ALfloat *values)
  894. START_API_FUNC
  895. {
  896. switch(param)
  897. {
  898. case AL_SEC_LENGTH_SOFT:
  899. alGetBufferf(buffer, param, values);
  900. return;
  901. }
  902. ContextRef context{GetContextRef()};
  903. if(UNLIKELY(!context)) return;
  904. ALCdevice *device = context->Device;
  905. std::lock_guard<std::mutex> _{device->BufferLock};
  906. if(UNLIKELY(LookupBuffer(device, buffer) == nullptr))
  907. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  908. else if(UNLIKELY(!values))
  909. alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
  910. else switch(param)
  911. {
  912. default:
  913. alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
  914. }
  915. }
  916. END_API_FUNC
  917. AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum param, ALint *value)
  918. START_API_FUNC
  919. {
  920. ContextRef context{GetContextRef()};
  921. if(UNLIKELY(!context)) return;
  922. ALCdevice *device = context->Device;
  923. std::lock_guard<std::mutex> _{device->BufferLock};
  924. ALbuffer *albuf = LookupBuffer(device, buffer);
  925. if(UNLIKELY(!albuf))
  926. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  927. else if(UNLIKELY(!value))
  928. alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
  929. else switch(param)
  930. {
  931. case AL_FREQUENCY:
  932. *value = albuf->Frequency;
  933. break;
  934. case AL_BITS:
  935. *value = BytesFromFmt(albuf->mFmtType) * 8;
  936. break;
  937. case AL_CHANNELS:
  938. *value = ChannelsFromFmt(albuf->mFmtChannels);
  939. break;
  940. case AL_SIZE:
  941. *value = albuf->SampleLen * FrameSizeFromFmt(albuf->mFmtChannels, albuf->mFmtType);
  942. break;
  943. case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
  944. *value = albuf->UnpackAlign.load();
  945. break;
  946. case AL_PACK_BLOCK_ALIGNMENT_SOFT:
  947. *value = albuf->PackAlign.load();
  948. break;
  949. default:
  950. alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
  951. }
  952. }
  953. END_API_FUNC
  954. AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum param, ALint *value1, ALint *value2, ALint *value3)
  955. START_API_FUNC
  956. {
  957. ContextRef context{GetContextRef()};
  958. if(UNLIKELY(!context)) return;
  959. ALCdevice *device = context->Device;
  960. std::lock_guard<std::mutex> _{device->BufferLock};
  961. if(UNLIKELY(LookupBuffer(device, buffer) == nullptr))
  962. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  963. else if(UNLIKELY(!value1 || !value2 || !value3))
  964. alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
  965. else switch(param)
  966. {
  967. default:
  968. alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
  969. }
  970. }
  971. END_API_FUNC
  972. AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum param, ALint *values)
  973. START_API_FUNC
  974. {
  975. switch(param)
  976. {
  977. case AL_FREQUENCY:
  978. case AL_BITS:
  979. case AL_CHANNELS:
  980. case AL_SIZE:
  981. case AL_INTERNAL_FORMAT_SOFT:
  982. case AL_BYTE_LENGTH_SOFT:
  983. case AL_SAMPLE_LENGTH_SOFT:
  984. case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
  985. case AL_PACK_BLOCK_ALIGNMENT_SOFT:
  986. alGetBufferi(buffer, param, values);
  987. return;
  988. }
  989. ContextRef context{GetContextRef()};
  990. if(UNLIKELY(!context)) return;
  991. ALCdevice *device = context->Device;
  992. std::lock_guard<std::mutex> _{device->BufferLock};
  993. ALbuffer *albuf = LookupBuffer(device, buffer);
  994. if(UNLIKELY(!albuf))
  995. alSetError(context.get(), AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
  996. else if(UNLIKELY(!values))
  997. alSetError(context.get(), AL_INVALID_VALUE, "NULL pointer");
  998. else switch(param)
  999. {
  1000. case AL_LOOP_POINTS_SOFT:
  1001. values[0] = albuf->LoopStart;
  1002. values[1] = albuf->LoopEnd;
  1003. break;
  1004. default:
  1005. alSetError(context.get(), AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x",
  1006. param);
  1007. }
  1008. }
  1009. END_API_FUNC
  1010. ALsizei BytesFromUserFmt(UserFmtType type)
  1011. {
  1012. switch(type)
  1013. {
  1014. case UserFmtUByte: return sizeof(ALubyte);
  1015. case UserFmtShort: return sizeof(ALshort);
  1016. case UserFmtFloat: return sizeof(ALfloat);
  1017. case UserFmtDouble: return sizeof(ALdouble);
  1018. case UserFmtMulaw: return sizeof(ALubyte);
  1019. case UserFmtAlaw: return sizeof(ALubyte);
  1020. case UserFmtIMA4: break; /* not handled here */
  1021. case UserFmtMSADPCM: break; /* not handled here */
  1022. }
  1023. return 0;
  1024. }
  1025. ALsizei ChannelsFromUserFmt(UserFmtChannels chans)
  1026. {
  1027. switch(chans)
  1028. {
  1029. case UserFmtMono: return 1;
  1030. case UserFmtStereo: return 2;
  1031. case UserFmtRear: return 2;
  1032. case UserFmtQuad: return 4;
  1033. case UserFmtX51: return 6;
  1034. case UserFmtX61: return 7;
  1035. case UserFmtX71: return 8;
  1036. case UserFmtBFormat2D: return 3;
  1037. case UserFmtBFormat3D: return 4;
  1038. }
  1039. return 0;
  1040. }
  1041. ALsizei BytesFromFmt(FmtType type)
  1042. {
  1043. switch(type)
  1044. {
  1045. case FmtUByte: return sizeof(ALubyte);
  1046. case FmtShort: return sizeof(ALshort);
  1047. case FmtFloat: return sizeof(ALfloat);
  1048. case FmtDouble: return sizeof(ALdouble);
  1049. case FmtMulaw: return sizeof(ALubyte);
  1050. case FmtAlaw: return sizeof(ALubyte);
  1051. }
  1052. return 0;
  1053. }
  1054. ALsizei ChannelsFromFmt(FmtChannels chans)
  1055. {
  1056. switch(chans)
  1057. {
  1058. case FmtMono: return 1;
  1059. case FmtStereo: return 2;
  1060. case FmtRear: return 2;
  1061. case FmtQuad: return 4;
  1062. case FmtX51: return 6;
  1063. case FmtX61: return 7;
  1064. case FmtX71: return 8;
  1065. case FmtBFormat2D: return 3;
  1066. case FmtBFormat3D: return 4;
  1067. }
  1068. return 0;
  1069. }
  1070. BufferSubList::~BufferSubList()
  1071. {
  1072. uint64_t usemask{~FreeMask};
  1073. while(usemask)
  1074. {
  1075. ALsizei idx{CTZ64(usemask)};
  1076. Buffers[idx].~ALbuffer();
  1077. usemask &= ~(1_u64 << idx);
  1078. }
  1079. FreeMask = ~usemask;
  1080. al_free(Buffers);
  1081. Buffers = nullptr;
  1082. }