Removed project files that are generated by "android update project" step.
[openal-soft/android.git] / OpenAL32 / alDatabuffer.c
blobb799f1337c35fadb97e4f65d2ddfa3376065517a
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <assert.h>
26 #include "alMain.h"
27 #include "AL/al.h"
28 #include "AL/alc.h"
29 #include "AL/alext.h"
30 #include "alError.h"
31 #include "alDatabuffer.h"
32 #include "alThunk.h"
35 #define LookupDatabuffer(m, k) ((ALdatabuffer*)LookupUIntMapKey(&(m), (k)))
38 * alGenDatabuffersEXT(ALsizei n, ALuint *puiBuffers)
40 * Generates n AL Databuffers, and stores the Databuffers Names in the array pointed to by puiBuffers
42 AL_API ALvoid AL_APIENTRY alGenDatabuffersEXT(ALsizei n,ALuint *puiBuffers)
44 ALCcontext *Context;
45 ALsizei i=0;
47 Context = GetContextSuspended();
48 if(!Context) return;
50 /* Check that we are actually generation some Databuffers */
51 if(n > 0)
53 ALCdevice *device = Context->Device;
55 /* Check the pointer is valid (and points to enough memory to store
56 * Databuffer Names) */
57 if(!IsBadWritePtr((void*)puiBuffers, n * sizeof(ALuint)))
59 ALenum err;
61 /* Create all the new Databuffers */
62 while(i < n)
64 ALdatabuffer *buffer = calloc(1, sizeof(ALdatabuffer));
65 if(!buffer)
67 alSetError(Context, AL_OUT_OF_MEMORY);
68 alDeleteDatabuffersEXT(i, puiBuffers);
69 break;
72 buffer->databuffer = ALTHUNK_ADDENTRY(buffer);
73 err = InsertUIntMapEntry(&device->DatabufferMap,
74 buffer->databuffer, buffer);
75 if(err != AL_NO_ERROR)
77 ALTHUNK_REMOVEENTRY(buffer->databuffer);
78 memset(buffer, 0, sizeof(ALdatabuffer));
79 free(buffer);
81 alSetError(Context, err);
82 alDeleteDatabuffersEXT(i, puiBuffers);
83 break;
85 puiBuffers[i++] = buffer->databuffer;
87 buffer->state = UNMAPPED;
90 else
91 alSetError(Context, AL_INVALID_VALUE);
94 ProcessContext(Context);
98 * alDatabeleteBuffersEXT(ALsizei n, ALuint *puiBuffers)
100 * Deletes the n AL Databuffers pointed to by puiBuffers
102 AL_API ALvoid AL_APIENTRY alDeleteDatabuffersEXT(ALsizei n, const ALuint *puiBuffers)
104 ALCcontext *Context;
105 ALdatabuffer *ALBuf;
106 ALsizei i;
107 ALboolean bFailed = AL_FALSE;
109 Context = GetContextSuspended();
110 if(!Context) return;
112 /* Check we are actually Deleting some Databuffers */
113 if(n >= 0)
115 ALCdevice *device = Context->Device;
117 /* Check that all the databuffers are valid and can actually be
118 * deleted */
119 for(i = 0;i < n;i++)
121 if(!puiBuffers[i])
122 continue;
124 /* Check for valid Buffer ID */
125 if((ALBuf=LookupDatabuffer(device->DatabufferMap, puiBuffers[i])) != NULL)
127 if(ALBuf->state != UNMAPPED)
129 /* Databuffer still in use, cannot be deleted */
130 alSetError(Context, AL_INVALID_OPERATION);
131 bFailed = AL_TRUE;
132 break;
135 else
137 /* Invalid Databuffer */
138 alSetError(Context, AL_INVALID_NAME);
139 bFailed = AL_TRUE;
140 break;
144 /* If all the Databuffers were valid (and unmapped), then we can
145 * delete them */
146 if(!bFailed)
148 for(i = 0;i < n;i++)
150 if((ALBuf=LookupDatabuffer(device->DatabufferMap, puiBuffers[i])) != NULL)
152 if(ALBuf == Context->SampleSource)
153 Context->SampleSource = NULL;
154 if(ALBuf == Context->SampleSink)
155 Context->SampleSink = NULL;
157 // Release the memory used to store audio data
158 free(ALBuf->data);
160 // Release buffer structure
161 RemoveUIntMapKey(&device->DatabufferMap, ALBuf->databuffer);
162 ALTHUNK_REMOVEENTRY(puiBuffers[i]);
164 memset(ALBuf, 0, sizeof(ALdatabuffer));
165 free(ALBuf);
170 else
171 alSetError(Context, AL_INVALID_VALUE);
173 ProcessContext(Context);
177 * alIsDatabufferEXT(ALuint uiBuffer)
179 * Checks if ulBuffer is a valid Databuffer Name
181 AL_API ALboolean AL_APIENTRY alIsDatabufferEXT(ALuint buffer)
183 ALCcontext *Context;
184 ALboolean result;
185 ALCdevice *device;
187 Context = GetContextSuspended();
188 if(!Context) return AL_FALSE;
190 device = Context->Device;
191 result = ((!buffer || LookupDatabuffer(device->DatabufferMap, buffer)) ?
192 AL_TRUE : AL_FALSE);
194 ProcessContext(Context);
196 return result;
200 * alDatabufferDataEXT(ALuint buffer,ALvoid *data,ALsizei size,ALenum usage)
202 * Fill databuffer with data
204 AL_API ALvoid AL_APIENTRY alDatabufferDataEXT(ALuint buffer,const ALvoid *data,ALsizeiptrEXT size,ALenum usage)
206 ALCcontext *Context;
207 ALdatabuffer *ALBuf;
208 ALCdevice *Device;
209 ALvoid *temp;
211 Context = GetContextSuspended();
212 if(!Context) return;
214 Device = Context->Device;
215 if((ALBuf=LookupDatabuffer(Device->DatabufferMap, buffer)) != NULL)
217 if(ALBuf->state == UNMAPPED)
219 if(usage == AL_STREAM_WRITE_EXT || usage == AL_STREAM_READ_EXT ||
220 usage == AL_STREAM_COPY_EXT || usage == AL_STATIC_WRITE_EXT ||
221 usage == AL_STATIC_READ_EXT || usage == AL_STATIC_COPY_EXT ||
222 usage == AL_DYNAMIC_WRITE_EXT || usage == AL_DYNAMIC_READ_EXT ||
223 usage == AL_DYNAMIC_COPY_EXT)
225 if(size >= 0)
227 /* (Re)allocate data */
228 temp = realloc(ALBuf->data, size);
229 if(temp)
231 ALBuf->data = temp;
232 ALBuf->size = size;
233 ALBuf->usage = usage;
234 if(data)
235 memcpy(ALBuf->data, data, size);
237 else
238 alSetError(Context, AL_OUT_OF_MEMORY);
240 else
241 alSetError(Context, AL_INVALID_VALUE);
243 else
244 alSetError(Context, AL_INVALID_ENUM);
246 else
247 alSetError(Context, AL_INVALID_OPERATION);
249 else
250 alSetError(Context, AL_INVALID_NAME);
252 ProcessContext(Context);
255 AL_API ALvoid AL_APIENTRY alDatabufferSubDataEXT(ALuint uiBuffer, ALintptrEXT start, ALsizeiptrEXT length, const ALvoid *data)
257 ALCcontext *pContext;
258 ALdatabuffer *pBuffer;
259 ALCdevice *Device;
261 pContext = GetContextSuspended();
262 if(!pContext) return;
264 Device = pContext->Device;
265 if((pBuffer=LookupDatabuffer(Device->DatabufferMap, uiBuffer)) != NULL)
267 if(start >= 0 && length >= 0 && start+length <= pBuffer->size)
269 if(pBuffer->state == UNMAPPED)
270 memcpy(pBuffer->data+start, data, length);
271 else
272 alSetError(pContext, AL_INVALID_OPERATION);
274 else
275 alSetError(pContext, AL_INVALID_VALUE);
277 else
278 alSetError(pContext, AL_INVALID_NAME);
280 ProcessContext(pContext);
283 AL_API ALvoid AL_APIENTRY alGetDatabufferSubDataEXT(ALuint uiBuffer, ALintptrEXT start, ALsizeiptrEXT length, ALvoid *data)
285 ALCcontext *pContext;
286 ALdatabuffer *pBuffer;
287 ALCdevice *Device;
289 pContext = GetContextSuspended();
290 if(!pContext) return;
292 Device = pContext->Device;
293 if((pBuffer=LookupDatabuffer(Device->DatabufferMap, uiBuffer)) != NULL)
295 if(start >= 0 && length >= 0 && start+length <= pBuffer->size)
297 if(pBuffer->state == UNMAPPED)
298 memcpy(data, pBuffer->data+start, length);
299 else
300 alSetError(pContext, AL_INVALID_OPERATION);
302 else
303 alSetError(pContext, AL_INVALID_VALUE);
305 else
306 alSetError(pContext, AL_INVALID_NAME);
308 ProcessContext(pContext);
312 AL_API ALvoid AL_APIENTRY alDatabufferfEXT(ALuint buffer, ALenum eParam, ALfloat flValue)
314 ALCcontext *pContext;
315 ALCdevice *Device;
317 (void)flValue;
319 pContext = GetContextSuspended();
320 if(!pContext) return;
322 Device = pContext->Device;
323 if(LookupDatabuffer(Device->DatabufferMap, buffer) != NULL)
325 switch(eParam)
327 default:
328 alSetError(pContext, AL_INVALID_ENUM);
329 break;
332 else
333 alSetError(pContext, AL_INVALID_NAME);
335 ProcessContext(pContext);
338 AL_API ALvoid AL_APIENTRY alDatabufferfvEXT(ALuint buffer, ALenum eParam, const ALfloat* flValues)
340 ALCcontext *pContext;
341 ALCdevice *Device;
343 (void)flValues;
345 pContext = GetContextSuspended();
346 if(!pContext) return;
348 Device = pContext->Device;
349 if(LookupDatabuffer(Device->DatabufferMap, buffer) != NULL)
351 switch(eParam)
353 default:
354 alSetError(pContext, AL_INVALID_ENUM);
355 break;
358 else
359 alSetError(pContext, AL_INVALID_NAME);
361 ProcessContext(pContext);
365 AL_API ALvoid AL_APIENTRY alDatabufferiEXT(ALuint buffer, ALenum eParam, ALint lValue)
367 ALCcontext *pContext;
368 ALCdevice *Device;
370 (void)lValue;
372 pContext = GetContextSuspended();
373 if(!pContext) return;
375 Device = pContext->Device;
376 if(LookupDatabuffer(Device->DatabufferMap, buffer) != NULL)
378 switch(eParam)
380 default:
381 alSetError(pContext, AL_INVALID_ENUM);
382 break;
385 else
386 alSetError(pContext, AL_INVALID_NAME);
388 ProcessContext(pContext);
391 AL_API ALvoid AL_APIENTRY alDatabufferivEXT(ALuint buffer, ALenum eParam, const ALint* plValues)
393 ALCcontext *pContext;
394 ALCdevice *Device;
396 (void)plValues;
398 pContext = GetContextSuspended();
399 if(!pContext) return;
401 Device = pContext->Device;
402 if(LookupDatabuffer(Device->DatabufferMap, buffer) != NULL)
404 switch(eParam)
406 default:
407 alSetError(pContext, AL_INVALID_ENUM);
408 break;
411 else
412 alSetError(pContext, AL_INVALID_NAME);
414 ProcessContext(pContext);
418 AL_API ALvoid AL_APIENTRY alGetDatabufferfEXT(ALuint buffer, ALenum eParam, ALfloat *pflValue)
420 ALCcontext *pContext;
421 ALCdevice *Device;
423 pContext = GetContextSuspended();
424 if(!pContext) return;
426 if(pflValue)
428 Device = pContext->Device;
429 if(LookupDatabuffer(Device->DatabufferMap, buffer) != NULL)
431 switch(eParam)
433 default:
434 alSetError(pContext, AL_INVALID_ENUM);
435 break;
438 else
439 alSetError(pContext, AL_INVALID_NAME);
441 else
442 alSetError(pContext, AL_INVALID_VALUE);
444 ProcessContext(pContext);
447 AL_API ALvoid AL_APIENTRY alGetDatabufferfvEXT(ALuint buffer, ALenum eParam, ALfloat* pflValues)
449 ALCcontext *pContext;
450 ALCdevice *Device;
452 pContext = GetContextSuspended();
453 if(!pContext) return;
455 if(pflValues)
457 Device = pContext->Device;
458 if(LookupDatabuffer(Device->DatabufferMap, buffer) != NULL)
460 switch(eParam)
462 default:
463 alSetError(pContext, AL_INVALID_ENUM);
464 break;
467 else
468 alSetError(pContext, AL_INVALID_NAME);
470 else
471 alSetError(pContext, AL_INVALID_VALUE);
473 ProcessContext(pContext);
476 AL_API ALvoid AL_APIENTRY alGetDatabufferiEXT(ALuint buffer, ALenum eParam, ALint *plValue)
478 ALCcontext *pContext;
479 ALdatabuffer *pBuffer;
480 ALCdevice *Device;
482 pContext = GetContextSuspended();
483 if(!pContext) return;
485 if(plValue)
487 Device = pContext->Device;
488 if((pBuffer=LookupDatabuffer(Device->DatabufferMap, buffer)) != NULL)
490 switch(eParam)
492 case AL_SIZE:
493 *plValue = (ALint)pBuffer->size;
494 break;
496 default:
497 alSetError(pContext, AL_INVALID_ENUM);
498 break;
501 else
502 alSetError(pContext, AL_INVALID_NAME);
504 else
505 alSetError(pContext, AL_INVALID_VALUE);
507 ProcessContext(pContext);
510 AL_API ALvoid AL_APIENTRY alGetDatabufferivEXT(ALuint buffer, ALenum eParam, ALint* plValues)
512 ALCcontext *pContext;
513 ALCdevice *Device;
515 pContext = GetContextSuspended();
516 if(!pContext) return;
518 if(plValues)
520 Device = pContext->Device;
521 if(LookupDatabuffer(Device->DatabufferMap, buffer) != NULL)
523 switch (eParam)
525 case AL_SIZE:
526 alGetDatabufferiEXT(buffer, eParam, plValues);
527 break;
529 default:
530 alSetError(pContext, AL_INVALID_ENUM);
531 break;
534 else
535 alSetError(pContext, AL_INVALID_NAME);
537 else
538 alSetError(pContext, AL_INVALID_VALUE);
540 ProcessContext(pContext);
544 AL_API ALvoid AL_APIENTRY alSelectDatabufferEXT(ALenum target, ALuint uiBuffer)
546 ALCcontext *pContext;
547 ALdatabuffer *pBuffer = NULL;
548 ALCdevice *Device;
550 pContext = GetContextSuspended();
551 if(!pContext) return;
553 Device = pContext->Device;
554 if(uiBuffer == 0 ||
555 (pBuffer=LookupDatabuffer(Device->DatabufferMap, uiBuffer)) != NULL)
557 if(target == AL_SAMPLE_SOURCE_EXT)
558 pContext->SampleSource = pBuffer;
559 else if(target == AL_SAMPLE_SINK_EXT)
560 pContext->SampleSink = pBuffer;
561 else
562 alSetError(pContext, AL_INVALID_VALUE);
564 else
565 alSetError(pContext, AL_INVALID_NAME);
567 ProcessContext(pContext);
571 AL_API ALvoid* AL_APIENTRY alMapDatabufferEXT(ALuint uiBuffer, ALintptrEXT start, ALsizeiptrEXT length, ALenum access)
573 ALCcontext *pContext;
574 ALdatabuffer *pBuffer;
575 ALvoid *ret = NULL;
576 ALCdevice *Device;
578 pContext = GetContextSuspended();
579 if(!pContext) return NULL;
581 Device = pContext->Device;
582 if((pBuffer=LookupDatabuffer(Device->DatabufferMap, uiBuffer)) != NULL)
584 if(start >= 0 && length >= 0 && start+length <= pBuffer->size)
586 if(access == AL_READ_ONLY_EXT || access == AL_WRITE_ONLY_EXT ||
587 access == AL_READ_WRITE_EXT)
589 if(pBuffer->state == UNMAPPED)
591 ret = pBuffer->data + start;
592 pBuffer->state = MAPPED;
594 else
595 alSetError(pContext, AL_INVALID_OPERATION);
597 else
598 alSetError(pContext, AL_INVALID_ENUM);
600 else
601 alSetError(pContext, AL_INVALID_VALUE);
603 else
604 alSetError(pContext, AL_INVALID_NAME);
606 ProcessContext(pContext);
608 return ret;
611 AL_API ALvoid AL_APIENTRY alUnmapDatabufferEXT(ALuint uiBuffer)
613 ALCcontext *pContext;
614 ALdatabuffer *pBuffer;
615 ALCdevice *Device;
617 pContext = GetContextSuspended();
618 if(!pContext) return;
620 Device = pContext->Device;
621 if((pBuffer=LookupDatabuffer(Device->DatabufferMap, uiBuffer)) != NULL)
623 if(pBuffer->state == MAPPED)
624 pBuffer->state = UNMAPPED;
625 else
626 alSetError(pContext, AL_INVALID_OPERATION);
628 else
629 alSetError(pContext, AL_INVALID_NAME);
631 ProcessContext(pContext);
636 * ReleaseALDatabuffers()
638 * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
640 ALvoid ReleaseALDatabuffers(ALCdevice *device)
642 ALsizei i;
643 for(i = 0;i < device->DatabufferMap.size;i++)
645 ALdatabuffer *temp = device->DatabufferMap.array[i].value;
646 device->DatabufferMap.array[i].value = NULL;
648 // Release buffer data
649 free(temp->data);
651 // Release Buffer structure
652 ALTHUNK_REMOVEENTRY(temp->databuffer);
653 memset(temp, 0, sizeof(ALdatabuffer));
654 free(temp);