Avoid calling alDelete* from alGen*
[openal-soft.git] / OpenAL32 / alBuffer.c
blobeaf508af9bbeb99830750e58b1f59b734981c31d
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 "alError.h"
30 #include "alBuffer.h"
31 #include "alDatabuffer.h"
32 #include "alThunk.h"
35 static ALenum LoadData(ALbuffer *ALBuf, const ALubyte *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat);
36 static void ConvertData(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len);
37 static void ConvertDataRear(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len);
38 static void ConvertDataIMA4(ALfloat *dst, const ALvoid *src, ALint origChans, ALsizei len);
39 static void ConvertDataMULaw(ALfloat *dst, const ALvoid *src, ALsizei len);
40 static void ConvertDataMULawRear(ALfloat *dst, const ALvoid *src, ALsizei len);
42 DECL_VERIFIER(Buffer, ALbuffer, buffer)
45 * Global Variables
48 static const long g_IMAStep_size[89]={ // IMA ADPCM Stepsize table
49 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
50 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143,
51 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
52 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
53 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,11487,12635,13899,
54 15289,16818,18500,20350,22358,24633,27086,29794,32767
57 static const long g_IMACodeword_4[16]={ // IMA4 ADPCM Codeword decode table
58 1, 3, 5, 7, 9, 11, 13, 15,
59 -1,-3,-5,-7,-9,-11,-13,-15,
62 static const long g_IMAIndex_adjust_4[16]={ // IMA4 ADPCM Step index adjust decode table
63 -1,-1,-1,-1, 2, 4, 6, 8,
64 -1,-1,-1,-1, 2, 4, 6, 8
67 static const ALshort muLawDecompressionTable[256] = {
68 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
69 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
70 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
71 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
72 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
73 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
74 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
75 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
76 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
77 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
78 -876, -844, -812, -780, -748, -716, -684, -652,
79 -620, -588, -556, -524, -492, -460, -428, -396,
80 -372, -356, -340, -324, -308, -292, -276, -260,
81 -244, -228, -212, -196, -180, -164, -148, -132,
82 -120, -112, -104, -96, -88, -80, -72, -64,
83 -56, -48, -40, -32, -24, -16, -8, 0,
84 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
85 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
86 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
87 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
88 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
89 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
90 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
91 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
92 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
93 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
94 876, 844, 812, 780, 748, 716, 684, 652,
95 620, 588, 556, 524, 492, 460, 428, 396,
96 372, 356, 340, 324, 308, 292, 276, 260,
97 244, 228, 212, 196, 180, 164, 148, 132,
98 120, 112, 104, 96, 88, 80, 72, 64,
99 56, 48, 40, 32, 24, 16, 8, 0
103 * alGenBuffers(ALsizei n, ALuint *puiBuffers)
105 * Generates n AL Buffers, and stores the Buffers Names in the array pointed to by puiBuffers
107 AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n,ALuint *puiBuffers)
109 ALCcontext *Context;
110 ALsizei i=0;
112 Context = GetContextSuspended();
113 if(!Context) return;
115 // Check that we are actually generation some Buffers
116 if (n > 0)
118 ALCdevice *device = Context->Device;
120 // Check the pointer is valid (and points to enough memory to store Buffer Names)
121 if (!IsBadWritePtr((void*)puiBuffers, n * sizeof(ALuint)))
123 ALbuffer *end;
124 ALbuffer **list = &device->BufferList;
125 while(*list)
126 list = &(*list)->next;
128 // Create all the new Buffers
129 end = *list;
130 while(i < n)
132 *list = calloc(1, sizeof(ALbuffer));
133 if(!(*list))
135 while(end->next)
137 ALbuffer *temp = end->next;
138 end->next = temp->next;
140 ALTHUNK_REMOVEENTRY(temp->buffer);
141 device->BufferCount--;
142 free(temp);
144 alSetError(Context, AL_OUT_OF_MEMORY);
145 break;
148 puiBuffers[i] = (ALuint)ALTHUNK_ADDENTRY(*list);
149 (*list)->buffer = puiBuffers[i];
151 device->BufferCount++;
152 i++;
154 list = &(*list)->next;
157 else
159 // Pointer does not point to enough memory to write Buffer names
160 alSetError(Context, AL_INVALID_VALUE);
164 ProcessContext(Context);
168 * alDeleteBuffers(ALsizei n, ALuint *puiBuffers)
170 * Deletes the n AL Buffers pointed to by puiBuffers
172 AL_API ALvoid AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *puiBuffers)
174 ALCcontext *Context;
175 ALbuffer *ALBuf;
176 ALsizei i;
177 ALboolean bFailed = AL_FALSE;
179 Context = GetContextSuspended();
180 if(!Context) return;
182 // Check we are actually Deleting some Buffers
183 if (n >= 0)
185 ALCdevice *device = Context->Device;
187 // Check that all the buffers are valid and can actually be deleted
188 for (i = 0; i < n; i++)
190 if(!puiBuffers[i])
191 continue;
193 // Check for valid Buffer ID (can be NULL buffer)
194 if((ALBuf=VerifyBuffer(device->BufferList, puiBuffers[i])) != NULL)
196 if(ALBuf->refcount != 0)
198 // Buffer still in use, cannot be deleted
199 alSetError(Context, AL_INVALID_OPERATION);
200 bFailed = AL_TRUE;
201 break;
204 else
206 // Invalid Buffer
207 alSetError(Context, AL_INVALID_NAME);
208 bFailed = AL_TRUE;
209 break;
213 // If all the Buffers were valid (and have Reference Counts of 0), then we can delete them
214 if (!bFailed)
216 for (i = 0; i < n; i++)
218 if((ALBuf=VerifyBuffer(device->BufferList, puiBuffers[i])) != NULL)
220 ALbuffer **list = &device->BufferList;
222 while(*list && *list != ALBuf)
223 list = &(*list)->next;
225 if(*list)
226 *list = (*list)->next;
228 // Release the memory used to store audio data
229 free(ALBuf->data);
231 // Release buffer structure
232 ALTHUNK_REMOVEENTRY(puiBuffers[i]);
233 memset(ALBuf, 0, sizeof(ALbuffer));
234 device->BufferCount--;
235 free(ALBuf);
240 else
241 alSetError(Context, AL_INVALID_VALUE);
243 ProcessContext(Context);
247 * alIsBuffer(ALuint uiBuffer)
249 * Checks if ulBuffer is a valid Buffer Name
251 AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint uiBuffer)
253 ALCcontext *Context;
254 ALboolean result = AL_TRUE;
256 Context = GetContextSuspended();
257 if(!Context) return AL_FALSE;
259 if(uiBuffer)
260 result = (VerifyBuffer(Context->Device->BufferList, uiBuffer) ?
261 AL_TRUE : AL_FALSE);
263 ProcessContext(Context);
265 return result;
269 * alBufferData(ALuint buffer,ALenum format,ALvoid *data,ALsizei size,ALsizei freq)
271 * Fill buffer with audio data
273 AL_API ALvoid AL_APIENTRY alBufferData(ALuint buffer,ALenum format,const ALvoid *data,ALsizei size,ALsizei freq)
275 ALCcontext *Context;
276 ALCdevice *device;
277 ALbuffer *ALBuf;
278 ALvoid *temp;
279 ALenum err;
281 Context = GetContextSuspended();
282 if(!Context) return;
284 device = Context->Device;
285 if((ALBuf=VerifyBuffer(device->BufferList, buffer)) != NULL)
287 if(Context->SampleSource)
289 ALintptrEXT offset;
291 if(Context->SampleSource->state == MAPPED)
293 alSetError(Context, AL_INVALID_OPERATION);
294 ProcessContext(Context);
295 return;
298 offset = (const ALubyte*)data - (ALubyte*)NULL;
299 data = Context->SampleSource->data + offset;
302 if ((ALBuf->refcount==0)&&(data))
304 switch(format)
306 case AL_FORMAT_MONO8:
307 case AL_FORMAT_MONO16:
308 case AL_FORMAT_MONO_FLOAT32:
309 case AL_FORMAT_MONO_DOUBLE_EXT:
310 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_MONO_FLOAT32);
311 if(err != AL_NO_ERROR)
312 alSetError(Context, err);
313 break;
315 case AL_FORMAT_STEREO8:
316 case AL_FORMAT_STEREO16:
317 case AL_FORMAT_STEREO_FLOAT32:
318 case AL_FORMAT_STEREO_DOUBLE_EXT:
319 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_STEREO_FLOAT32);
320 if(err != AL_NO_ERROR)
321 alSetError(Context, err);
322 break;
324 case AL_FORMAT_REAR8:
325 case AL_FORMAT_REAR16:
326 case AL_FORMAT_REAR32: {
327 ALenum NewFormat = AL_FORMAT_QUAD32;
328 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
329 ALuint NewBytes = aluBytesFromFormat(NewFormat);
330 ALuint OrigBytes = ((format==AL_FORMAT_REAR8) ? 1 :
331 ((format==AL_FORMAT_REAR16) ? 2 :
332 4));
334 if((size%(OrigBytes*2)) != 0)
336 alSetError(Context, AL_INVALID_VALUE);
337 break;
340 size /= OrigBytes;
341 size *= 2;
343 // Samples are converted here
344 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + size) * NewBytes);
345 if(temp)
347 ALBuf->data = temp;
348 ConvertDataRear(ALBuf->data, data, OrigBytes, size);
350 ALBuf->format = NewFormat;
351 ALBuf->eOriginalFormat = format;
352 ALBuf->size = size*NewBytes;
353 ALBuf->frequency = freq;
355 else
356 alSetError(Context, AL_OUT_OF_MEMORY);
357 } break;
359 case AL_FORMAT_QUAD8_LOKI:
360 case AL_FORMAT_QUAD16_LOKI:
361 case AL_FORMAT_QUAD8:
362 case AL_FORMAT_QUAD16:
363 case AL_FORMAT_QUAD32:
364 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_QUAD32);
365 if(err != AL_NO_ERROR)
366 alSetError(Context, err);
367 break;
369 case AL_FORMAT_51CHN8:
370 case AL_FORMAT_51CHN16:
371 case AL_FORMAT_51CHN32:
372 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_51CHN32);
373 if(err != AL_NO_ERROR)
374 alSetError(Context, err);
375 break;
377 case AL_FORMAT_61CHN8:
378 case AL_FORMAT_61CHN16:
379 case AL_FORMAT_61CHN32:
380 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_61CHN32);
381 if(err != AL_NO_ERROR)
382 alSetError(Context, err);
383 break;
385 case AL_FORMAT_71CHN8:
386 case AL_FORMAT_71CHN16:
387 case AL_FORMAT_71CHN32:
388 err = LoadData(ALBuf, data, size, freq, format, AL_FORMAT_71CHN32);
389 if(err != AL_NO_ERROR)
390 alSetError(Context, err);
391 break;
393 case AL_FORMAT_MONO_IMA4:
394 case AL_FORMAT_STEREO_IMA4: {
395 int OrigChans = ((format==AL_FORMAT_MONO_IMA4) ? 1 : 2);
396 ALenum NewFormat = ((OrigChans==1) ? AL_FORMAT_MONO_FLOAT32 :
397 AL_FORMAT_STEREO_FLOAT32);
398 ALuint NewBytes = aluBytesFromFormat(NewFormat);
400 // Here is where things vary:
401 // nVidia and Apple use 64+1 samples per channel per block => block_size=36*chans bytes
402 // Most PC sound software uses 2040+1 samples per channel per block -> block_size=1024*chans bytes
403 if((size%(36*OrigChans)) != 0)
405 alSetError(Context, AL_INVALID_VALUE);
406 break;
409 size /= 36;
410 size *= 65;
412 // Allocate extra padding samples
413 temp = realloc(ALBuf->data, (BUFFER_PADDING*OrigChans + size)*NewBytes);
414 if(temp)
416 ALBuf->data = temp;
417 ConvertDataIMA4(ALBuf->data, data, OrigChans, size/65);
419 ALBuf->format = NewFormat;
420 ALBuf->eOriginalFormat = format;
421 ALBuf->size = size*NewBytes;
422 ALBuf->frequency = freq;
424 else
425 alSetError(Context, AL_OUT_OF_MEMORY);
426 } break;
428 case AL_FORMAT_MONO_MULAW:
429 case AL_FORMAT_STEREO_MULAW:
430 case AL_FORMAT_QUAD_MULAW:
431 case AL_FORMAT_51CHN_MULAW:
432 case AL_FORMAT_61CHN_MULAW:
433 case AL_FORMAT_71CHN_MULAW: {
434 int Channels = ((format==AL_FORMAT_MONO_MULAW) ? 1 :
435 ((format==AL_FORMAT_STEREO_MULAW) ? 2 :
436 ((format==AL_FORMAT_QUAD_MULAW) ? 4 :
437 ((format==AL_FORMAT_51CHN_MULAW) ? 6 :
438 ((format==AL_FORMAT_61CHN_MULAW) ? 7 : 8)))));
439 ALenum NewFormat = ((Channels==1) ? AL_FORMAT_MONO_FLOAT32 :
440 ((Channels==2) ? AL_FORMAT_STEREO_FLOAT32 :
441 ((Channels==4) ? AL_FORMAT_QUAD32 :
442 ((Channels==6) ? AL_FORMAT_51CHN32 :
443 ((Channels==7) ? AL_FORMAT_61CHN32 :
444 AL_FORMAT_71CHN32)))));
445 ALuint NewBytes = aluBytesFromFormat(NewFormat);
447 if((size%(1*Channels)) != 0)
449 alSetError(Context, AL_INVALID_VALUE);
450 break;
453 // Allocate extra padding samples
454 temp = realloc(ALBuf->data, (BUFFER_PADDING*Channels + size)*NewBytes);
455 if(temp)
457 ALBuf->data = temp;
458 ConvertDataMULaw(ALBuf->data, data, size);
460 ALBuf->format = NewFormat;
461 ALBuf->eOriginalFormat = format;
462 ALBuf->size = size*NewBytes;
463 ALBuf->frequency = freq;
465 else
466 alSetError(Context, AL_OUT_OF_MEMORY);
467 } break;
469 case AL_FORMAT_REAR_MULAW: {
470 int OrigChans = 2;
471 ALenum NewFormat = AL_FORMAT_QUAD32;
472 ALuint NewBytes = aluBytesFromFormat(NewFormat);
473 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
475 if((size%(1*OrigChans)) != 0)
477 alSetError(Context, AL_INVALID_VALUE);
478 break;
481 size *= 2;
483 // Allocate extra padding samples
484 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + size)*NewBytes);
485 if(temp)
487 ALBuf->data = temp;
488 ConvertDataMULawRear(ALBuf->data, data, size);
490 ALBuf->format = NewFormat;
491 ALBuf->eOriginalFormat = format;
492 ALBuf->size = size*NewBytes;
493 ALBuf->frequency = freq;
495 else
496 alSetError(Context, AL_OUT_OF_MEMORY);
497 } break;
499 default:
500 alSetError(Context, AL_INVALID_ENUM);
501 break;
504 else
506 // Buffer is in use, or data is a NULL pointer
507 alSetError(Context, AL_INVALID_VALUE);
510 else
512 // Invalid Buffer Name
513 alSetError(Context, AL_INVALID_NAME);
516 ProcessContext(Context);
520 * alBufferSubDataEXT(ALuint buffer,ALenum format,ALvoid *data,ALsizei offset,ALsizei length)
522 * Fill buffer with audio data
524 ALvoid AL_APIENTRY alBufferSubDataEXT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
526 ALCcontext *Context;
527 ALCdevice *device;
528 ALbuffer *ALBuf;
530 Context = GetContextSuspended();
531 if(!Context) return;
533 device = Context->Device;
534 if((ALBuf=VerifyBuffer(device->BufferList, buffer)) != NULL)
536 if(Context->SampleSource)
538 ALintptrEXT offset;
540 if(Context->SampleSource->state == MAPPED)
542 alSetError(Context, AL_INVALID_OPERATION);
543 ProcessContext(Context);
544 return;
547 offset = (const ALubyte*)data - (ALubyte*)NULL;
548 data = Context->SampleSource->data + offset;
551 if(ALBuf->data == NULL)
553 // buffer does not have any data
554 alSetError(Context, AL_INVALID_NAME);
556 else if(length < 0 || offset < 0 || (length > 0 && data == NULL))
558 // data is NULL or offset/length is negative
559 alSetError(Context, AL_INVALID_VALUE);
561 else
563 switch(format)
565 case AL_FORMAT_REAR8:
566 case AL_FORMAT_REAR16:
567 case AL_FORMAT_REAR32: {
568 ALuint OrigBytes = ((format==AL_FORMAT_REAR8) ? 1 :
569 ((format==AL_FORMAT_REAR16) ? 2 :
570 4));
571 ALuint NewBytes = aluBytesFromFormat(ALBuf->format);
573 if(ALBuf->eOriginalFormat != AL_FORMAT_REAR8 &&
574 ALBuf->eOriginalFormat != AL_FORMAT_REAR16 &&
575 ALBuf->eOriginalFormat != AL_FORMAT_REAR32)
577 alSetError(Context, AL_INVALID_ENUM);
578 break;
581 if(ALBuf->size/4/NewBytes < (ALuint)offset+length)
583 alSetError(Context, AL_INVALID_VALUE);
584 break;
587 ConvertDataRear(&ALBuf->data[offset*4], data, OrigBytes, length*2);
588 } break;
590 case AL_FORMAT_MONO_IMA4:
591 case AL_FORMAT_STEREO_IMA4: {
592 int Channels = aluChannelsFromFormat(ALBuf->format);
593 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
595 if(ALBuf->eOriginalFormat != format)
597 alSetError(Context, AL_INVALID_ENUM);
598 break;
601 if((offset%65) != 0 || (length%65) != 0 ||
602 ALBuf->size/Channels/Bytes < (ALuint)offset+length)
604 alSetError(Context, AL_INVALID_VALUE);
605 break;
608 ConvertDataIMA4(&ALBuf->data[offset*Channels], data, Channels, length/65*Channels);
609 } break;
611 case AL_FORMAT_MONO_MULAW:
612 case AL_FORMAT_STEREO_MULAW:
613 case AL_FORMAT_QUAD_MULAW:
614 case AL_FORMAT_REAR_MULAW:
615 case AL_FORMAT_51CHN_MULAW:
616 case AL_FORMAT_61CHN_MULAW:
617 case AL_FORMAT_71CHN_MULAW: {
618 int Channels = aluChannelsFromFormat(ALBuf->format);
619 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
621 if(ALBuf->eOriginalFormat != format)
623 alSetError(Context, AL_INVALID_ENUM);
624 break;
627 if(ALBuf->size/Channels/Bytes < (ALuint)offset+length)
629 alSetError(Context, AL_INVALID_VALUE);
630 break;
633 if(ALBuf->eOriginalFormat == AL_FORMAT_REAR_MULAW)
634 ConvertDataMULawRear(&ALBuf->data[offset*Channels], data, length*2);
635 else
636 ConvertDataMULaw(&ALBuf->data[offset*Channels], data, length*Channels);
637 } break;
639 default: {
640 ALuint Channels = aluChannelsFromFormat(format);
641 ALuint Bytes = aluBytesFromFormat(format);
642 ALuint NewBytes = aluBytesFromFormat(ALBuf->format);
644 if(Channels != aluChannelsFromFormat(ALBuf->format))
646 alSetError(Context, AL_INVALID_ENUM);
647 break;
650 if(ALBuf->size/Channels/NewBytes < (ALuint)offset+length)
652 alSetError(Context, AL_INVALID_VALUE);
653 break;
656 ConvertData(&ALBuf->data[offset*Channels], data, Bytes, length*Channels);
657 } break;
661 else
663 // Invalid Buffer Name
664 alSetError(Context, AL_INVALID_NAME);
667 ProcessContext(Context);
671 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
673 ALCcontext *pContext;
674 ALCdevice *device;
676 (void)flValue;
678 pContext = GetContextSuspended();
679 if(!pContext) return;
681 device = pContext->Device;
682 if(VerifyBuffer(device->BufferList, buffer) != NULL)
684 switch(eParam)
686 default:
687 alSetError(pContext, AL_INVALID_ENUM);
688 break;
691 else
693 alSetError(pContext, AL_INVALID_NAME);
696 ProcessContext(pContext);
700 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
702 ALCcontext *pContext;
703 ALCdevice *device;
705 (void)flValue1;
706 (void)flValue2;
707 (void)flValue3;
709 pContext = GetContextSuspended();
710 if(!pContext) return;
712 device = pContext->Device;
713 if(VerifyBuffer(device->BufferList, buffer) != NULL)
715 switch(eParam)
717 default:
718 alSetError(pContext, AL_INVALID_ENUM);
719 break;
722 else
724 alSetError(pContext, AL_INVALID_NAME);
727 ProcessContext(pContext);
731 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
733 ALCcontext *pContext;
734 ALCdevice *device;
736 (void)flValues;
738 pContext = GetContextSuspended();
739 if(!pContext) return;
741 device = pContext->Device;
742 if(VerifyBuffer(device->BufferList, buffer) != NULL)
744 switch(eParam)
746 default:
747 alSetError(pContext, AL_INVALID_ENUM);
748 break;
751 else
753 alSetError(pContext, AL_INVALID_NAME);
756 ProcessContext(pContext);
760 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
762 ALCcontext *pContext;
763 ALCdevice *device;
765 (void)lValue;
767 pContext = GetContextSuspended();
768 if(!pContext) return;
770 device = pContext->Device;
771 if(VerifyBuffer(device->BufferList, buffer) != NULL)
773 switch(eParam)
775 default:
776 alSetError(pContext, AL_INVALID_ENUM);
777 break;
780 else
782 alSetError(pContext, AL_INVALID_NAME);
785 ProcessContext(pContext);
789 AL_API void AL_APIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
791 ALCcontext *pContext;
792 ALCdevice *device;
794 (void)lValue1;
795 (void)lValue2;
796 (void)lValue3;
798 pContext = GetContextSuspended();
799 if(!pContext) return;
801 device = pContext->Device;
802 if(VerifyBuffer(device->BufferList, buffer) != NULL)
804 switch(eParam)
806 default:
807 alSetError(pContext, AL_INVALID_ENUM);
808 break;
811 else
813 alSetError(pContext, AL_INVALID_NAME);
816 ProcessContext(pContext);
820 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
822 ALCcontext *pContext;
823 ALCdevice *device;
825 (void)plValues;
827 pContext = GetContextSuspended();
828 if(!pContext) return;
830 device = pContext->Device;
831 if(VerifyBuffer(device->BufferList, buffer) != NULL)
833 switch(eParam)
835 default:
836 alSetError(pContext, AL_INVALID_ENUM);
837 break;
840 else
842 alSetError(pContext, AL_INVALID_NAME);
845 ProcessContext(pContext);
849 AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
851 ALCcontext *pContext;
852 ALCdevice *device;
854 pContext = GetContextSuspended();
855 if(!pContext) return;
857 if (pflValue)
859 device = pContext->Device;
860 if(VerifyBuffer(device->BufferList, buffer) != NULL)
862 switch(eParam)
864 default:
865 alSetError(pContext, AL_INVALID_ENUM);
866 break;
869 else
871 alSetError(pContext, AL_INVALID_NAME);
874 else
876 alSetError(pContext, AL_INVALID_VALUE);
879 ProcessContext(pContext);
883 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
885 ALCcontext *pContext;
886 ALCdevice *device;
888 pContext = GetContextSuspended();
889 if(!pContext) return;
891 if ((pflValue1) && (pflValue2) && (pflValue3))
893 device = pContext->Device;
894 if(VerifyBuffer(device->BufferList, buffer) != NULL)
896 switch(eParam)
898 default:
899 alSetError(pContext, AL_INVALID_ENUM);
900 break;
903 else
905 alSetError(pContext, AL_INVALID_NAME);
908 else
910 alSetError(pContext, AL_INVALID_VALUE);
913 ProcessContext(pContext);
917 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
919 ALCcontext *pContext;
920 ALCdevice *device;
922 pContext = GetContextSuspended();
923 if(!pContext) return;
925 if (pflValues)
927 device = pContext->Device;
928 if(VerifyBuffer(device->BufferList, buffer) != NULL)
930 switch(eParam)
932 default:
933 alSetError(pContext, AL_INVALID_ENUM);
934 break;
937 else
939 alSetError(pContext, AL_INVALID_NAME);
942 else
944 alSetError(pContext, AL_INVALID_VALUE);
947 ProcessContext(pContext);
951 AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
953 ALCcontext *pContext;
954 ALbuffer *pBuffer;
955 ALCdevice *device;
957 pContext = GetContextSuspended();
958 if(!pContext) return;
960 if (plValue)
962 device = pContext->Device;
963 if((pBuffer=VerifyBuffer(device->BufferList, buffer)) != NULL)
965 switch (eParam)
967 case AL_FREQUENCY:
968 *plValue = pBuffer->frequency;
969 break;
971 case AL_BITS:
972 *plValue = aluBytesFromFormat(pBuffer->format) * 8;
973 break;
975 case AL_CHANNELS:
976 *plValue = aluChannelsFromFormat(pBuffer->format);
977 break;
979 case AL_SIZE:
980 *plValue = pBuffer->size;
981 break;
983 default:
984 alSetError(pContext, AL_INVALID_ENUM);
985 break;
988 else
990 alSetError(pContext, AL_INVALID_NAME);
993 else
995 alSetError(pContext, AL_INVALID_VALUE);
998 ProcessContext(pContext);
1002 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
1004 ALCcontext *pContext;
1005 ALCdevice *device;
1007 pContext = GetContextSuspended();
1008 if(!pContext) return;
1010 if ((plValue1) && (plValue2) && (plValue3))
1012 device = pContext->Device;
1013 if(VerifyBuffer(device->BufferList, buffer) != NULL)
1015 switch(eParam)
1017 default:
1018 alSetError(pContext, AL_INVALID_ENUM);
1019 break;
1022 else
1024 alSetError(pContext, AL_INVALID_NAME);
1027 else
1029 alSetError(pContext, AL_INVALID_VALUE);
1032 ProcessContext(pContext);
1036 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
1038 ALCcontext *pContext;
1039 ALCdevice *device;
1041 pContext = GetContextSuspended();
1042 if(!pContext) return;
1044 if (plValues)
1046 device = pContext->Device;
1047 if(VerifyBuffer(device->BufferList, buffer) != NULL)
1049 switch (eParam)
1051 case AL_FREQUENCY:
1052 case AL_BITS:
1053 case AL_CHANNELS:
1054 case AL_SIZE:
1055 alGetBufferi(buffer, eParam, plValues);
1056 break;
1058 default:
1059 alSetError(pContext, AL_INVALID_ENUM);
1060 break;
1063 else
1065 alSetError(pContext, AL_INVALID_NAME);
1068 else
1070 alSetError(pContext, AL_INVALID_VALUE);
1073 ProcessContext(pContext);
1077 * LoadData
1079 * Loads the specified data into the buffer, using the specified formats.
1080 * Currently, the new format must be 32-bit float, and must have the same
1081 * channel configuration as the original format. This does NOT handle
1082 * compressed formats (eg. IMA4).
1084 static ALenum LoadData(ALbuffer *ALBuf, const ALubyte *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat)
1086 ALuint NewBytes = aluBytesFromFormat(NewFormat);
1087 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
1088 ALuint OrigBytes = aluBytesFromFormat(OrigFormat);
1089 ALuint OrigChannels = aluChannelsFromFormat(OrigFormat);
1090 ALvoid *temp;
1092 assert(NewBytes == 4);
1093 assert(NewChannels == OrigChannels);
1095 if ((size%(OrigBytes*OrigChannels)) != 0)
1096 return AL_INVALID_VALUE;
1098 // Samples are converted here
1099 size /= OrigBytes;
1100 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + size) * NewBytes);
1101 if(!temp) return AL_OUT_OF_MEMORY;
1102 ALBuf->data = temp;
1103 ConvertData(ALBuf->data, data, OrigBytes, size);
1105 ALBuf->format = NewFormat;
1106 ALBuf->eOriginalFormat = OrigFormat;
1107 ALBuf->size = size*NewBytes;
1108 ALBuf->frequency = freq;
1110 return AL_NO_ERROR;
1113 static void ConvertData(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len)
1115 ALsizei i;
1116 ALint smp;
1117 switch(origBytes)
1119 case 1:
1120 for(i = 0;i < len;i++)
1122 smp = ((ALubyte*)src)[i];
1123 dst[i] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1125 break;
1127 case 2:
1128 for(i = 0;i < len;i++)
1130 smp = ((ALshort*)src)[i];
1131 dst[i] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1133 break;
1135 case 4:
1136 for(i = 0;i < len;i++)
1137 dst[i] = ((ALfloat*)src)[i];
1138 break;
1140 case 8:
1141 for(i = 0;i < len;i++)
1142 dst[i] = ((ALdouble*)src)[i];
1143 break;
1145 default:
1146 assert(0);
1150 static void ConvertDataRear(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len)
1152 ALsizei i;
1153 ALint smp;
1154 switch(origBytes)
1156 case 1:
1157 for(i = 0;i < len;i+=4)
1159 dst[i+0] = 0;
1160 dst[i+1] = 0;
1161 smp = ((ALubyte*)src)[i/2+0];
1162 dst[i+2] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1163 smp = ((ALubyte*)src)[i/2+1];
1164 dst[i+3] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1166 break;
1168 case 2:
1169 for(i = 0;i < len;i+=4)
1171 dst[i+0] = 0;
1172 dst[i+1] = 0;
1173 smp = ((ALshort*)src)[i/2+0];
1174 dst[i+2] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1175 smp = ((ALshort*)src)[i/2+1];
1176 dst[i+3] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1178 break;
1180 case 4:
1181 for(i = 0;i < len;i+=4)
1183 dst[i+0] = 0;
1184 dst[i+1] = 0;
1185 dst[i+2] = ((ALfloat*)src)[i/2+0];
1186 dst[i+3] = ((ALfloat*)src)[i/2+1];
1188 break;
1190 default:
1191 assert(0);
1195 static void ConvertDataIMA4(ALfloat *dst, const ALvoid *src, ALint origChans, ALsizei len)
1197 const ALuint *IMAData;
1198 ALint Sample[2],Index[2];
1199 ALuint IMACode[2];
1200 ALsizei i,j,k,c;
1202 IMAData = src;
1203 for(i = 0;i < len/origChans;i++)
1205 for(c = 0;c < origChans;c++)
1207 Sample[c] = ((ALshort*)IMAData)[0];
1208 Index[c] = ((ALshort*)IMAData)[1];
1210 Index[c] = ((Index[c]<0) ? 0 : Index[c]);
1211 Index[c] = ((Index[c]>88) ? 88 : Index[c]);
1213 dst[i*65*origChans + c] = ((Sample[c] < 0) ? (Sample[c]/32768.0f) : (Sample[c]/32767.0f));
1215 IMAData++;
1218 for(j = 1;j < 65;j += 8)
1220 for(c = 0;c < origChans;c++)
1221 IMACode[c] = *(IMAData++);
1223 for(k = 0;k < 8;k++)
1225 for(c = 0;c < origChans;c++)
1227 Sample[c] += ((g_IMAStep_size[Index[c]]*g_IMACodeword_4[IMACode[c]&15])/8);
1228 Index[c] += g_IMAIndex_adjust_4[IMACode[c]&15];
1230 if(Sample[c] < -32768) Sample[c] = -32768;
1231 else if(Sample[c] > 32767) Sample[c] = 32767;
1233 if(Index[c]<0) Index[c] = 0;
1234 else if(Index[c]>88) Index[c] = 88;
1236 dst[(i*65+j+k)*origChans + c] = ((Sample[c] < 0) ? (Sample[c]/32768.0f) : (Sample[c]/32767.0f));
1237 IMACode[c] >>= 4;
1244 static void ConvertDataMULaw(ALfloat *dst, const ALvoid *src, ALsizei len)
1246 ALsizei i;
1247 ALint smp;
1248 for(i = 0;i < len;i++)
1250 smp = muLawDecompressionTable[((ALubyte*)src)[i]];
1251 dst[i] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1255 static void ConvertDataMULawRear(ALfloat *dst, const ALvoid *src, ALsizei len)
1257 ALsizei i;
1258 ALint smp;
1259 for(i = 0;i < len;i+=4)
1261 dst[i+0] = 0;
1262 dst[i+1] = 0;
1263 smp = muLawDecompressionTable[((ALubyte*)src)[i/2+0]];
1264 dst[i+2] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1265 smp = muLawDecompressionTable[((ALubyte*)src)[i/2+1]];
1266 dst[i+3] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1271 * ReleaseALBuffers()
1273 * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
1275 ALvoid ReleaseALBuffers(ALCdevice *device)
1277 ALbuffer *ALBuffer;
1278 ALbuffer *ALBufferTemp;
1280 ALBuffer = device->BufferList;
1281 while(ALBuffer)
1283 // Release sample data
1284 free(ALBuffer->data);
1286 // Release Buffer structure
1287 ALBufferTemp = ALBuffer;
1288 ALBuffer = ALBuffer->next;
1289 memset(ALBufferTemp, 0, sizeof(ALbuffer));
1290 free(ALBufferTemp);
1292 device->BufferList = NULL;
1293 device->BufferCount = 0;