alDatabuffer.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <assert.h>
  24. #include "alMain.h"
  25. #include "AL/al.h"
  26. #include "AL/alc.h"
  27. #include "AL/alext.h"
  28. #include "alError.h"
  29. #include "alDatabuffer.h"
  30. #include "alThunk.h"
  31. #define LookupDatabuffer(m, k) ((ALdatabuffer*)LookupUIntMapKey(&(m), (k)))
  32. /*
  33. * alGenDatabuffersEXT(ALsizei n, ALuint *puiBuffers)
  34. *
  35. * Generates n AL Databuffers, and stores the Databuffers Names in the array pointed to by puiBuffers
  36. */
  37. AL_API ALvoid AL_APIENTRY alGenDatabuffersEXT(ALsizei n,ALuint *puiBuffers)
  38. {
  39. ALCcontext *Context;
  40. ALsizei i=0;
  41. Context = GetContextSuspended();
  42. if(!Context) return;
  43. /* Check that we are actually generation some Databuffers */
  44. if(n < 0 || IsBadWritePtr((void*)puiBuffers, n * sizeof(ALuint)))
  45. alSetError(Context, AL_INVALID_VALUE);
  46. else
  47. {
  48. ALCdevice *device = Context->Device;
  49. ALenum err;
  50. /* Create all the new Databuffers */
  51. while(i < n)
  52. {
  53. ALdatabuffer *buffer = calloc(1, sizeof(ALdatabuffer));
  54. if(!buffer)
  55. {
  56. alSetError(Context, AL_OUT_OF_MEMORY);
  57. alDeleteDatabuffersEXT(i, puiBuffers);
  58. break;
  59. }
  60. buffer->databuffer = ALTHUNK_ADDENTRY(buffer);
  61. err = InsertUIntMapEntry(&device->DatabufferMap,
  62. buffer->databuffer, buffer);
  63. if(err != AL_NO_ERROR)
  64. {
  65. ALTHUNK_REMOVEENTRY(buffer->databuffer);
  66. memset(buffer, 0, sizeof(ALdatabuffer));
  67. free(buffer);
  68. alSetError(Context, err);
  69. alDeleteDatabuffersEXT(i, puiBuffers);
  70. break;
  71. }
  72. puiBuffers[i++] = buffer->databuffer;
  73. buffer->state = UNMAPPED;
  74. }
  75. }
  76. ProcessContext(Context);
  77. }
  78. /*
  79. * alDatabeleteBuffersEXT(ALsizei n, ALuint *puiBuffers)
  80. *
  81. * Deletes the n AL Databuffers pointed to by puiBuffers
  82. */
  83. AL_API ALvoid AL_APIENTRY alDeleteDatabuffersEXT(ALsizei n, const ALuint *buffers)
  84. {
  85. ALCcontext *Context;
  86. ALCdevice *device;
  87. ALdatabuffer *ALBuf;
  88. ALboolean Failed;
  89. ALsizei i;
  90. Context = GetContextSuspended();
  91. if(!Context) return;
  92. /* Check we are actually Deleting some Databuffers */
  93. Failed = AL_TRUE;
  94. device = Context->Device;
  95. if(n < 0)
  96. alSetError(Context, AL_INVALID_VALUE);
  97. else
  98. {
  99. Failed = AL_FALSE;
  100. /* Check that all the databuffers are valid and can actually be
  101. * deleted */
  102. for(i = 0;i < n;i++)
  103. {
  104. if(!buffers[i])
  105. continue;
  106. /* Check for valid Buffer ID */
  107. if((ALBuf=LookupDatabuffer(device->DatabufferMap, buffers[i])) == NULL)
  108. {
  109. /* Invalid Databuffer */
  110. alSetError(Context, AL_INVALID_NAME);
  111. Failed = AL_TRUE;
  112. break;
  113. }
  114. else if(ALBuf->state != UNMAPPED)
  115. {
  116. /* Databuffer still in use, cannot be deleted */
  117. alSetError(Context, AL_INVALID_OPERATION);
  118. Failed = AL_TRUE;
  119. break;
  120. }
  121. }
  122. }
  123. /* If all the Databuffers were valid (and unmapped), then we can delete them */
  124. if(!Failed)
  125. {
  126. for(i = 0;i < n;i++)
  127. {
  128. if((ALBuf=LookupDatabuffer(device->DatabufferMap, buffers[i])) == NULL)
  129. continue;
  130. if(ALBuf == Context->SampleSource)
  131. Context->SampleSource = NULL;
  132. if(ALBuf == Context->SampleSink)
  133. Context->SampleSink = NULL;
  134. // Release the memory used to store audio data
  135. free(ALBuf->data);
  136. // Release buffer structure
  137. RemoveUIntMapKey(&device->DatabufferMap, ALBuf->databuffer);
  138. ALTHUNK_REMOVEENTRY(ALBuf->databuffer);
  139. memset(ALBuf, 0, sizeof(ALdatabuffer));
  140. free(ALBuf);
  141. }
  142. }
  143. ProcessContext(Context);
  144. }
  145. /*
  146. * alIsDatabufferEXT(ALuint uiBuffer)
  147. *
  148. * Checks if ulBuffer is a valid Databuffer Name
  149. */
  150. AL_API ALboolean AL_APIENTRY alIsDatabufferEXT(ALuint buffer)
  151. {
  152. ALCcontext *Context;
  153. ALboolean result;
  154. ALCdevice *device;
  155. Context = GetContextSuspended();
  156. if(!Context) return AL_FALSE;
  157. device = Context->Device;
  158. result = ((!buffer || LookupDatabuffer(device->DatabufferMap, buffer)) ?
  159. AL_TRUE : AL_FALSE);
  160. ProcessContext(Context);
  161. return result;
  162. }
  163. /*
  164. * alDatabufferDataEXT(ALuint buffer,ALvoid *data,ALsizei size,ALenum usage)
  165. *
  166. * Fill databuffer with data
  167. */
  168. AL_API ALvoid AL_APIENTRY alDatabufferDataEXT(ALuint buffer,const ALvoid *data,ALsizeiptrEXT size,ALenum usage)
  169. {
  170. ALCcontext *Context;
  171. ALdatabuffer *ALBuf;
  172. ALCdevice *Device;
  173. ALvoid *temp;
  174. Context = GetContextSuspended();
  175. if(!Context) return;
  176. Device = Context->Device;
  177. if((ALBuf=LookupDatabuffer(Device->DatabufferMap, buffer)) != NULL)
  178. {
  179. if(ALBuf->state == UNMAPPED)
  180. {
  181. if(usage == AL_STREAM_WRITE_EXT || usage == AL_STREAM_READ_EXT ||
  182. usage == AL_STREAM_COPY_EXT || usage == AL_STATIC_WRITE_EXT ||
  183. usage == AL_STATIC_READ_EXT || usage == AL_STATIC_COPY_EXT ||
  184. usage == AL_DYNAMIC_WRITE_EXT || usage == AL_DYNAMIC_READ_EXT ||
  185. usage == AL_DYNAMIC_COPY_EXT)
  186. {
  187. if(size >= 0)
  188. {
  189. /* (Re)allocate data */
  190. temp = realloc(ALBuf->data, size);
  191. if(temp)
  192. {
  193. ALBuf->data = temp;
  194. ALBuf->size = size;
  195. ALBuf->usage = usage;
  196. if(data)
  197. memcpy(ALBuf->data, data, size);
  198. }
  199. else
  200. alSetError(Context, AL_OUT_OF_MEMORY);
  201. }
  202. else
  203. alSetError(Context, AL_INVALID_VALUE);
  204. }
  205. else
  206. alSetError(Context, AL_INVALID_ENUM);
  207. }
  208. else
  209. alSetError(Context, AL_INVALID_OPERATION);
  210. }
  211. else
  212. alSetError(Context, AL_INVALID_NAME);
  213. ProcessContext(Context);
  214. }
  215. AL_API ALvoid AL_APIENTRY alDatabufferSubDataEXT(ALuint uiBuffer, ALintptrEXT start, ALsizeiptrEXT length, const ALvoid *data)
  216. {
  217. ALCcontext *pContext;
  218. ALdatabuffer *pBuffer;
  219. ALCdevice *Device;
  220. pContext = GetContextSuspended();
  221. if(!pContext) return;
  222. Device = pContext->Device;
  223. if((pBuffer=LookupDatabuffer(Device->DatabufferMap, uiBuffer)) != NULL)
  224. {
  225. if(start >= 0 && length >= 0 && start+length <= pBuffer->size)
  226. {
  227. if(pBuffer->state == UNMAPPED)
  228. memcpy(pBuffer->data+start, data, length);
  229. else
  230. alSetError(pContext, AL_INVALID_OPERATION);
  231. }
  232. else
  233. alSetError(pContext, AL_INVALID_VALUE);
  234. }
  235. else
  236. alSetError(pContext, AL_INVALID_NAME);
  237. ProcessContext(pContext);
  238. }
  239. AL_API ALvoid AL_APIENTRY alGetDatabufferSubDataEXT(ALuint uiBuffer, ALintptrEXT start, ALsizeiptrEXT length, ALvoid *data)
  240. {
  241. ALCcontext *pContext;
  242. ALdatabuffer *pBuffer;
  243. ALCdevice *Device;
  244. pContext = GetContextSuspended();
  245. if(!pContext) return;
  246. Device = pContext->Device;
  247. if((pBuffer=LookupDatabuffer(Device->DatabufferMap, uiBuffer)) != NULL)
  248. {
  249. if(start >= 0 && length >= 0 && start+length <= pBuffer->size)
  250. {
  251. if(pBuffer->state == UNMAPPED)
  252. memcpy(data, pBuffer->data+start, length);
  253. else
  254. alSetError(pContext, AL_INVALID_OPERATION);
  255. }
  256. else
  257. alSetError(pContext, AL_INVALID_VALUE);
  258. }
  259. else
  260. alSetError(pContext, AL_INVALID_NAME);
  261. ProcessContext(pContext);
  262. }
  263. AL_API ALvoid AL_APIENTRY alDatabufferfEXT(ALuint buffer, ALenum eParam, ALfloat flValue)
  264. {
  265. ALCcontext *pContext;
  266. ALCdevice *Device;
  267. (void)flValue;
  268. pContext = GetContextSuspended();
  269. if(!pContext) return;
  270. Device = pContext->Device;
  271. if(LookupDatabuffer(Device->DatabufferMap, buffer) != NULL)
  272. {
  273. switch(eParam)
  274. {
  275. default:
  276. alSetError(pContext, AL_INVALID_ENUM);
  277. break;
  278. }
  279. }
  280. else
  281. alSetError(pContext, AL_INVALID_NAME);
  282. ProcessContext(pContext);
  283. }
  284. AL_API ALvoid AL_APIENTRY alDatabufferfvEXT(ALuint buffer, ALenum eParam, const ALfloat* flValues)
  285. {
  286. ALCcontext *pContext;
  287. ALCdevice *Device;
  288. (void)flValues;
  289. pContext = GetContextSuspended();
  290. if(!pContext) return;
  291. Device = pContext->Device;
  292. if(LookupDatabuffer(Device->DatabufferMap, buffer) != NULL)
  293. {
  294. switch(eParam)
  295. {
  296. default:
  297. alSetError(pContext, AL_INVALID_ENUM);
  298. break;
  299. }
  300. }
  301. else
  302. alSetError(pContext, AL_INVALID_NAME);
  303. ProcessContext(pContext);
  304. }
  305. AL_API ALvoid AL_APIENTRY alDatabufferiEXT(ALuint buffer, ALenum eParam, ALint lValue)
  306. {
  307. ALCcontext *pContext;
  308. ALCdevice *Device;
  309. (void)lValue;
  310. pContext = GetContextSuspended();
  311. if(!pContext) return;
  312. Device = pContext->Device;
  313. if(LookupDatabuffer(Device->DatabufferMap, buffer) != NULL)
  314. {
  315. switch(eParam)
  316. {
  317. default:
  318. alSetError(pContext, AL_INVALID_ENUM);
  319. break;
  320. }
  321. }
  322. else
  323. alSetError(pContext, AL_INVALID_NAME);
  324. ProcessContext(pContext);
  325. }
  326. AL_API ALvoid AL_APIENTRY alDatabufferivEXT(ALuint buffer, ALenum eParam, const ALint* plValues)
  327. {
  328. ALCcontext *pContext;
  329. ALCdevice *Device;
  330. (void)plValues;
  331. pContext = GetContextSuspended();
  332. if(!pContext) return;
  333. Device = pContext->Device;
  334. if(LookupDatabuffer(Device->DatabufferMap, buffer) != NULL)
  335. {
  336. switch(eParam)
  337. {
  338. default:
  339. alSetError(pContext, AL_INVALID_ENUM);
  340. break;
  341. }
  342. }
  343. else
  344. alSetError(pContext, AL_INVALID_NAME);
  345. ProcessContext(pContext);
  346. }
  347. AL_API ALvoid AL_APIENTRY alGetDatabufferfEXT(ALuint buffer, ALenum eParam, ALfloat *pflValue)
  348. {
  349. ALCcontext *pContext;
  350. ALCdevice *Device;
  351. pContext = GetContextSuspended();
  352. if(!pContext) return;
  353. if(pflValue)
  354. {
  355. Device = pContext->Device;
  356. if(LookupDatabuffer(Device->DatabufferMap, buffer) != NULL)
  357. {
  358. switch(eParam)
  359. {
  360. default:
  361. alSetError(pContext, AL_INVALID_ENUM);
  362. break;
  363. }
  364. }
  365. else
  366. alSetError(pContext, AL_INVALID_NAME);
  367. }
  368. else
  369. alSetError(pContext, AL_INVALID_VALUE);
  370. ProcessContext(pContext);
  371. }
  372. AL_API ALvoid AL_APIENTRY alGetDatabufferfvEXT(ALuint buffer, ALenum eParam, ALfloat* pflValues)
  373. {
  374. ALCcontext *pContext;
  375. ALCdevice *Device;
  376. pContext = GetContextSuspended();
  377. if(!pContext) return;
  378. if(pflValues)
  379. {
  380. Device = pContext->Device;
  381. if(LookupDatabuffer(Device->DatabufferMap, buffer) != NULL)
  382. {
  383. switch(eParam)
  384. {
  385. default:
  386. alSetError(pContext, AL_INVALID_ENUM);
  387. break;
  388. }
  389. }
  390. else
  391. alSetError(pContext, AL_INVALID_NAME);
  392. }
  393. else
  394. alSetError(pContext, AL_INVALID_VALUE);
  395. ProcessContext(pContext);
  396. }
  397. AL_API ALvoid AL_APIENTRY alGetDatabufferiEXT(ALuint buffer, ALenum eParam, ALint *plValue)
  398. {
  399. ALCcontext *pContext;
  400. ALdatabuffer *pBuffer;
  401. ALCdevice *Device;
  402. pContext = GetContextSuspended();
  403. if(!pContext) return;
  404. if(plValue)
  405. {
  406. Device = pContext->Device;
  407. if((pBuffer=LookupDatabuffer(Device->DatabufferMap, buffer)) != NULL)
  408. {
  409. switch(eParam)
  410. {
  411. case AL_SIZE:
  412. *plValue = (ALint)pBuffer->size;
  413. break;
  414. default:
  415. alSetError(pContext, AL_INVALID_ENUM);
  416. break;
  417. }
  418. }
  419. else
  420. alSetError(pContext, AL_INVALID_NAME);
  421. }
  422. else
  423. alSetError(pContext, AL_INVALID_VALUE);
  424. ProcessContext(pContext);
  425. }
  426. AL_API ALvoid AL_APIENTRY alGetDatabufferivEXT(ALuint buffer, ALenum eParam, ALint* plValues)
  427. {
  428. ALCcontext *pContext;
  429. ALCdevice *Device;
  430. pContext = GetContextSuspended();
  431. if(!pContext) return;
  432. if(plValues)
  433. {
  434. Device = pContext->Device;
  435. if(LookupDatabuffer(Device->DatabufferMap, buffer) != NULL)
  436. {
  437. switch (eParam)
  438. {
  439. case AL_SIZE:
  440. alGetDatabufferiEXT(buffer, eParam, plValues);
  441. break;
  442. default:
  443. alSetError(pContext, AL_INVALID_ENUM);
  444. break;
  445. }
  446. }
  447. else
  448. alSetError(pContext, AL_INVALID_NAME);
  449. }
  450. else
  451. alSetError(pContext, AL_INVALID_VALUE);
  452. ProcessContext(pContext);
  453. }
  454. AL_API ALvoid AL_APIENTRY alSelectDatabufferEXT(ALenum target, ALuint uiBuffer)
  455. {
  456. ALCcontext *pContext;
  457. ALdatabuffer *pBuffer = NULL;
  458. ALCdevice *Device;
  459. pContext = GetContextSuspended();
  460. if(!pContext) return;
  461. Device = pContext->Device;
  462. if(uiBuffer == 0 ||
  463. (pBuffer=LookupDatabuffer(Device->DatabufferMap, uiBuffer)) != NULL)
  464. {
  465. if(target == AL_SAMPLE_SOURCE_EXT)
  466. pContext->SampleSource = pBuffer;
  467. else if(target == AL_SAMPLE_SINK_EXT)
  468. pContext->SampleSink = pBuffer;
  469. else
  470. alSetError(pContext, AL_INVALID_VALUE);
  471. }
  472. else
  473. alSetError(pContext, AL_INVALID_NAME);
  474. ProcessContext(pContext);
  475. }
  476. AL_API ALvoid* AL_APIENTRY alMapDatabufferEXT(ALuint uiBuffer, ALintptrEXT start, ALsizeiptrEXT length, ALenum access)
  477. {
  478. ALCcontext *pContext;
  479. ALdatabuffer *pBuffer;
  480. ALvoid *ret = NULL;
  481. ALCdevice *Device;
  482. pContext = GetContextSuspended();
  483. if(!pContext) return NULL;
  484. Device = pContext->Device;
  485. if((pBuffer=LookupDatabuffer(Device->DatabufferMap, uiBuffer)) != NULL)
  486. {
  487. if(start >= 0 && length >= 0 && start+length <= pBuffer->size)
  488. {
  489. if(access == AL_READ_ONLY_EXT || access == AL_WRITE_ONLY_EXT ||
  490. access == AL_READ_WRITE_EXT)
  491. {
  492. if(pBuffer->state == UNMAPPED)
  493. {
  494. ret = pBuffer->data + start;
  495. pBuffer->state = MAPPED;
  496. }
  497. else
  498. alSetError(pContext, AL_INVALID_OPERATION);
  499. }
  500. else
  501. alSetError(pContext, AL_INVALID_ENUM);
  502. }
  503. else
  504. alSetError(pContext, AL_INVALID_VALUE);
  505. }
  506. else
  507. alSetError(pContext, AL_INVALID_NAME);
  508. ProcessContext(pContext);
  509. return ret;
  510. }
  511. AL_API ALvoid AL_APIENTRY alUnmapDatabufferEXT(ALuint uiBuffer)
  512. {
  513. ALCcontext *pContext;
  514. ALdatabuffer *pBuffer;
  515. ALCdevice *Device;
  516. pContext = GetContextSuspended();
  517. if(!pContext) return;
  518. Device = pContext->Device;
  519. if((pBuffer=LookupDatabuffer(Device->DatabufferMap, uiBuffer)) != NULL)
  520. {
  521. if(pBuffer->state == MAPPED)
  522. pBuffer->state = UNMAPPED;
  523. else
  524. alSetError(pContext, AL_INVALID_OPERATION);
  525. }
  526. else
  527. alSetError(pContext, AL_INVALID_NAME);
  528. ProcessContext(pContext);
  529. }
  530. /*
  531. * ReleaseALDatabuffers()
  532. *
  533. * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
  534. */
  535. ALvoid ReleaseALDatabuffers(ALCdevice *device)
  536. {
  537. ALsizei i;
  538. for(i = 0;i < device->DatabufferMap.size;i++)
  539. {
  540. ALdatabuffer *temp = device->DatabufferMap.array[i].value;
  541. device->DatabufferMap.array[i].value = NULL;
  542. // Release buffer data
  543. free(temp->data);
  544. // Release Buffer structure
  545. ALTHUNK_REMOVEENTRY(temp->databuffer);
  546. memset(temp, 0, sizeof(ALdatabuffer));
  547. free(temp);
  548. }
  549. }