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 #define _CRT_SECURE_NO_DEPRECATE // get rid of sprintf security warnings on VS2005
36 static void LoadData(ALbuffer
*ALBuf
, const ALubyte
*data
, ALsizei size
, ALuint freq
, ALenum OrigFormat
, ALenum NewFormat
);
41 * AL Buffers are shared amoung Contexts, so we store the list of generated Buffers
42 * as a global variable in this module. (A valid context is not required to make
43 * AL Buffer function calls
51 static ALbuffer
*g_pBuffers
= NULL
; // Linked List of Buffers
52 static ALuint g_uiBufferCount
= 0; // Buffer Count
54 static const long g_IMAStep_size
[89]={ // IMA ADPCM Stepsize table
55 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
56 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143,
57 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
58 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
59 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,11487,12635,13899,
60 15289,16818,18500,20350,22358,24633,27086,29794,32767
63 static const long g_IMACodeword_4
[16]={ // IMA4 ADPCM Codeword decode table
64 1, 3, 5, 7, 9, 11, 13, 15,
65 -1,-3,-5,-7,-9,-11,-13,-15,
68 static const long g_IMAIndex_adjust_4
[16]={ // IMA4 ADPCM Step index adjust decode table
69 -1,-1,-1,-1, 2, 4, 6, 8,
70 -1,-1,-1,-1, 2, 4, 6, 8
74 * alGenBuffers(ALsizei n, ALuint *puiBuffers)
76 * Generates n AL Buffers, and stores the Buffers Names in the array pointed to by puiBuffers
78 ALAPI ALvoid ALAPIENTRY
alGenBuffers(ALsizei n
,ALuint
*puiBuffers
)
83 Context
= alcGetCurrentContext();
84 SuspendContext(Context
);
86 // Check that we are actually generation some Buffers
89 // Check the pointer is valid (and points to enough memory to store Buffer Names)
90 if (!IsBadWritePtr((void*)puiBuffers
, n
* sizeof(ALuint
)))
92 ALbuffer
**list
= &g_pBuffers
;
94 list
= &(*list
)->next
;
96 // Create all the new Buffers
99 *list
= calloc(1, sizeof(ALbuffer
));
102 alDeleteBuffers(i
, puiBuffers
);
103 alSetError(AL_OUT_OF_MEMORY
);
107 puiBuffers
[i
] = (ALuint
)ALTHUNK_ADDENTRY(*list
);
108 (*list
)->state
= UNUSED
;
112 list
= &(*list
)->next
;
117 // Pointer does not point to enough memory to write Buffer names
118 alSetError(AL_INVALID_VALUE
);
122 ProcessContext(Context
);
128 * alDeleteBuffers(ALsizei n, ALuint *puiBuffers)
130 * Deletes the n AL Buffers pointed to by puiBuffers
132 ALAPI ALvoid ALAPIENTRY
alDeleteBuffers(ALsizei n
, const ALuint
*puiBuffers
)
137 ALboolean bFailed
= AL_FALSE
;
139 Context
= alcGetCurrentContext();
140 SuspendContext(Context
);
142 // Check we are actually Deleting some Buffers
145 if ((ALuint
)n
<= g_uiBufferCount
)
147 // Check that all the buffers are valid and can actually be deleted
148 for (i
= 0; i
< n
; i
++)
150 // Check for valid Buffer ID (can be NULL buffer)
151 if (alIsBuffer(puiBuffers
[i
]))
153 // If not the NULL buffer, check that the reference count is 0
154 ALBuf
= ((ALbuffer
*)ALTHUNK_LOOKUPENTRY(puiBuffers
[i
]));
157 if (ALBuf
->refcount
!= 0)
159 // Buffer still in use, cannot be deleted
160 alSetError(AL_INVALID_OPERATION
);
168 alSetError(AL_INVALID_NAME
);
173 // If all the Buffers were valid (and have Reference Counts of 0), then we can delete them
176 for (i
= 0; i
< n
; i
++)
178 ALBuf
=((ALbuffer
*)ALTHUNK_LOOKUPENTRY(puiBuffers
[i
]));
181 ALbuffer
**list
= &g_pBuffers
;
182 while(*list
&& *list
!= ALBuf
)
183 list
= &(*list
)->next
;
186 *list
= (*list
)->next
;
188 // Release the memory used to store audio data
191 // Release buffer structure
192 ALTHUNK_REMOVEENTRY(puiBuffers
[i
]);
193 memset(ALBuf
, 0, sizeof(ALbuffer
));
201 alSetError(AL_INVALID_NAME
);
204 alSetError(AL_INVALID_VALUE
);
206 ProcessContext(Context
);
213 * alIsBuffer(ALuint uiBuffer)
215 * Checks if ulBuffer is a valid Buffer Name
217 ALAPI ALboolean ALAPIENTRY
alIsBuffer(ALuint uiBuffer
)
220 ALboolean result
=AL_FALSE
;
224 Context
= alcGetCurrentContext();
225 SuspendContext(Context
);
229 TgtALBuf
= (ALbuffer
*)ALTHUNK_LOOKUPENTRY(uiBuffer
);
231 // Check through list of generated buffers for uiBuffer
235 if (ALBuf
== TgtALBuf
)
250 ProcessContext(Context
);
256 * alBufferData(ALuint buffer,ALenum format,ALvoid *data,ALsizei size,ALsizei freq)
258 * Fill buffer with audio data
260 ALAPI ALvoid ALAPIENTRY
alBufferData(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei size
,ALsizei freq
)
262 ALuint
*IMAData
,IMACode
;
265 ALint LeftSample
,LeftIndex
;
266 ALint RightSample
,RightIndex
;
267 ALuint LeftIMACode
,RightIMACode
;
271 Context
= alcGetCurrentContext();
272 SuspendContext(Context
);
274 if (alIsBuffer(buffer
) && (buffer
!= 0))
276 ALBuf
=((ALbuffer
*)ALTHUNK_LOOKUPENTRY(buffer
));
277 if ((ALBuf
->refcount
==0)&&(data
))
281 case AL_FORMAT_MONO8
:
282 case AL_FORMAT_MONO16
:
283 case AL_FORMAT_MONO_FLOAT32
:
284 LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_MONO16
);
287 case AL_FORMAT_STEREO8
:
288 case AL_FORMAT_STEREO16
:
289 case AL_FORMAT_STEREO_FLOAT32
:
290 LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_STEREO16
);
293 case AL_FORMAT_REAR8
:
294 case AL_FORMAT_REAR16
:
295 case AL_FORMAT_REAR32
: {
296 ALuint NewFormat
= AL_FORMAT_QUAD16
;
297 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
298 ALuint OrigBytes
= ((format
==AL_FORMAT_REAR8
) ? 1 :
299 ((format
==AL_FORMAT_REAR16
) ? 2 :
303 assert(aluBytesFromFormat(NewFormat
) == 2);
305 if ((size
%(OrigBytes
*2)) != 0)
307 alSetError(AL_INVALID_VALUE
);
314 size
/= sizeof(ALubyte
);
317 // 8bit Samples are converted to 16 bit here
318 // Allocate 8 extra samples
319 ALBuf
->data
= realloc(ALBuf
->data
, (8*NewChannels
+ size
) * (1*sizeof(ALshort
)));
322 for (i
= 0;i
< size
;i
+=4)
324 ALBuf
->data
[i
+0] = 0;
325 ALBuf
->data
[i
+1] = 0;
326 ALBuf
->data
[i
+2] = (ALshort
)((((ALubyte
*)data
)[i
/2+0]-128) << 8);
327 ALBuf
->data
[i
+3] = (ALshort
)((((ALubyte
*)data
)[i
/2+1]-128) << 8);
329 memset(&(ALBuf
->data
[size
]), 0, 16*NewChannels
);
331 ALBuf
->format
= NewFormat
;
332 ALBuf
->eOriginalFormat
= format
;
333 ALBuf
->size
= size
*1*sizeof(ALshort
);
334 ALBuf
->frequency
= freq
;
337 alSetError(AL_OUT_OF_MEMORY
);
341 size
/= sizeof(ALshort
);
344 // Allocate 8 extra samples
345 ALBuf
->data
= realloc(ALBuf
->data
, (8*NewChannels
+ size
) * (1*sizeof(ALshort
)));
348 for (i
= 0;i
< size
;i
+=4)
350 ALBuf
->data
[i
+0] = 0;
351 ALBuf
->data
[i
+1] = 0;
352 ALBuf
->data
[i
+2] = ((ALshort
*)data
)[i
/2+0];
353 ALBuf
->data
[i
+3] = ((ALshort
*)data
)[i
/2+1];
355 memset(&(ALBuf
->data
[size
]), 0, 16*NewChannels
);
357 ALBuf
->format
= NewFormat
;
358 ALBuf
->eOriginalFormat
= format
;
359 ALBuf
->size
= size
*1*sizeof(ALshort
);
360 ALBuf
->frequency
= freq
;
363 alSetError(AL_OUT_OF_MEMORY
);
367 size
/= sizeof(ALfloat
);
370 // Allocate 8 extra samples
371 ALBuf
->data
= realloc(ALBuf
->data
, (8*NewChannels
+ size
) * (1*sizeof(ALshort
)));
374 for (i
= 0;i
< size
;i
+=4)
376 ALBuf
->data
[i
+0] = 0;
377 ALBuf
->data
[i
+1] = 0;
378 ALBuf
->data
[i
+2] = (ALshort
)(((ALfloat
*)data
)[i
/2+0] * 32767.5f
- 0.5);
379 ALBuf
->data
[i
+3] = (ALshort
)(((ALfloat
*)data
)[i
/2+1] * 32767.5f
- 0.5);
381 memset(&(ALBuf
->data
[size
]), 0, 16*NewChannels
);
383 ALBuf
->format
= NewFormat
;
384 ALBuf
->eOriginalFormat
= format
;
385 ALBuf
->size
= size
*1*sizeof(ALshort
);
386 ALBuf
->frequency
= freq
;
389 alSetError(AL_OUT_OF_MEMORY
);
397 case AL_FORMAT_QUAD8_LOKI
:
398 case AL_FORMAT_QUAD16_LOKI
:
399 case AL_FORMAT_QUAD8
:
400 case AL_FORMAT_QUAD16
:
401 case AL_FORMAT_QUAD32
:
402 LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_QUAD16
);
405 case AL_FORMAT_51CHN8
:
406 case AL_FORMAT_51CHN16
:
407 case AL_FORMAT_51CHN32
:
408 LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_51CHN16
);
411 case AL_FORMAT_61CHN8
:
412 case AL_FORMAT_61CHN16
:
413 case AL_FORMAT_61CHN32
:
414 LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_61CHN16
);
417 case AL_FORMAT_71CHN8
:
418 case AL_FORMAT_71CHN16
:
419 case AL_FORMAT_71CHN32
:
420 LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_71CHN16
);
423 case AL_FORMAT_MONO_IMA4
:
424 // Here is where things vary:
425 // nVidia and Apple use 64+1 samples per block => block_size=36 bytes
426 // Most PC sound software uses 2040+1 samples per block -> block_size=1024 bytes
429 // Allocate 8 extra samples (16 bytes)
430 ALBuf
->data
=realloc(ALBuf
->data
,16+(size
/36)*(65*sizeof(ALshort
)));
433 ALBuf
->format
= AL_FORMAT_MONO16
;
434 ALBuf
->eOriginalFormat
= AL_FORMAT_MONO_IMA4
;
435 IMAData
=(ALuint
*)data
;
436 for (i
=0;i
<size
/36;i
++)
438 Sample
=((ALshort
*)IMAData
)[0];
439 Index
=((ALshort
*)IMAData
)[1];
441 Index
=Index
<0?0:Index
;
442 Index
=Index
>88?88:Index
;
444 ALBuf
->data
[i
*65]=(short)Sample
;
453 Sample
+=((g_IMAStep_size
[Index
]*g_IMACodeword_4
[IMACode
&15])/8);
454 Index
+=g_IMAIndex_adjust_4
[IMACode
&15];
455 if (Sample
<-32768) Sample
=-32768;
456 else if (Sample
>32767) Sample
=32767;
457 if (Index
<0) Index
=0;
458 else if (Index
>88) Index
=88;
459 ALBuf
->data
[i
*65+j
+k
]=(short)Sample
;
462 Sample
+=((g_IMAStep_size
[Index
]*g_IMACodeword_4
[IMACode
&15])/8);
463 Index
+=g_IMAIndex_adjust_4
[IMACode
&15];
464 if (Sample
<-32768) Sample
=-32768;
465 else if (Sample
>32767) Sample
=32767;
466 if (Index
<0) Index
=0;
467 else if (Index
>88) Index
=88;
468 ALBuf
->data
[i
*65+j
+k
+1]=(short)Sample
;
474 memset(&(ALBuf
->data
[(size
/36*65)]), 0, 16);
475 ALBuf
->size
=size
/36*65*sizeof(ALshort
);
476 ALBuf
->frequency
=freq
;
479 alSetError(AL_OUT_OF_MEMORY
);
482 alSetError(AL_INVALID_VALUE
);
485 case AL_FORMAT_STEREO_IMA4
:
486 // Here is where things vary:
487 // nVidia and Apple use 64+1 samples per channel per block => block_size=72 bytes
488 // Most PC sound software uses 2040+1 samples per channel per block -> block_size=2048 bytes
491 // Allocate 8 extra samples (32 bytes)
492 ALBuf
->data
=realloc(ALBuf
->data
,32+(size
/72)*(2*65*sizeof(ALshort
)));
495 ALBuf
->format
= AL_FORMAT_STEREO16
;
496 ALBuf
->eOriginalFormat
= AL_FORMAT_STEREO_IMA4
;
497 IMAData
=(ALuint
*)data
;
498 for (i
=0;i
<size
/72;i
++)
500 LeftSample
=((ALshort
*)IMAData
)[0];
501 LeftIndex
=((ALshort
*)IMAData
)[1];
503 LeftIndex
=LeftIndex
<0?0:LeftIndex
;
504 LeftIndex
=LeftIndex
>88?88:LeftIndex
;
506 ALBuf
->data
[i
*2*65]=(short)LeftSample
;
510 RightSample
=((ALshort
*)IMAData
)[0];
511 RightIndex
=((ALshort
*)IMAData
)[1];
513 RightIndex
=RightIndex
<0?0:RightIndex
;
514 RightIndex
=RightIndex
>88?88:RightIndex
;
516 ALBuf
->data
[i
*2*65+1]=(short)RightSample
;
520 for (j
=2;j
<130;j
+=16)
522 LeftIMACode
=IMAData
[0];
523 RightIMACode
=IMAData
[1];
526 LeftSample
+=((g_IMAStep_size
[LeftIndex
]*g_IMACodeword_4
[LeftIMACode
&15])/8);
527 LeftIndex
+=g_IMAIndex_adjust_4
[LeftIMACode
&15];
528 if (LeftSample
<-32768) LeftSample
=-32768;
529 else if (LeftSample
>32767) LeftSample
=32767;
530 if (LeftIndex
<0) LeftIndex
=0;
531 else if (LeftIndex
>88) LeftIndex
=88;
532 ALBuf
->data
[i
*2*65+j
+k
]=(short)LeftSample
;
535 RightSample
+=((g_IMAStep_size
[RightIndex
]*g_IMACodeword_4
[RightIMACode
&15])/8);
536 RightIndex
+=g_IMAIndex_adjust_4
[RightIMACode
&15];
537 if (RightSample
<-32768) RightSample
=-32768;
538 else if (RightSample
>32767) RightSample
=32767;
539 if (RightIndex
<0) RightIndex
=0;
540 else if (RightIndex
>88) RightIndex
=88;
541 ALBuf
->data
[i
*2*65+j
+k
+1]=(short)RightSample
;
544 LeftSample
+=((g_IMAStep_size
[LeftIndex
]*g_IMACodeword_4
[LeftIMACode
&15])/8);
545 LeftIndex
+=g_IMAIndex_adjust_4
[LeftIMACode
&15];
546 if (LeftSample
<-32768) LeftSample
=-32768;
547 else if (LeftSample
>32767) LeftSample
=32767;
548 if (LeftIndex
<0) LeftIndex
=0;
549 else if (LeftIndex
>88) LeftIndex
=88;
550 ALBuf
->data
[i
*2*65+j
+k
+2]=(short)LeftSample
;
553 RightSample
+=((g_IMAStep_size
[RightIndex
]*g_IMACodeword_4
[RightIMACode
&15])/8);
554 RightIndex
+=g_IMAIndex_adjust_4
[RightIMACode
&15];
555 if (RightSample
<-32768) RightSample
=-32768;
556 else if (RightSample
>32767) RightSample
=32767;
557 if (RightIndex
<0) RightIndex
=0;
558 else if (RightIndex
>88) RightIndex
=88;
559 ALBuf
->data
[i
*2*65+j
+k
+3]=(short)RightSample
;
565 memset(&(ALBuf
->data
[(size
/72*2*65)]), 0, 32);
566 ALBuf
->size
=size
/72*2*65*sizeof(ALshort
);
567 ALBuf
->frequency
=freq
;
570 alSetError(AL_OUT_OF_MEMORY
);
573 alSetError(AL_INVALID_VALUE
);
577 alSetError(AL_INVALID_ENUM
);
583 // Buffer is in use, or data is a NULL pointer
584 alSetError(AL_INVALID_VALUE
);
589 // Invalid Buffer Name
590 alSetError(AL_INVALID_NAME
);
593 ProcessContext(Context
);
597 ALAPI
void ALAPIENTRY
alBufferf(ALuint buffer
, ALenum eParam
, ALfloat flValue
)
599 ALCcontext
*pContext
;
603 pContext
= alcGetCurrentContext();
604 SuspendContext(pContext
);
606 if (alIsBuffer(buffer
) && (buffer
!= 0))
611 alSetError(AL_INVALID_ENUM
);
617 alSetError(AL_INVALID_NAME
);
620 ProcessContext(pContext
);
624 ALAPI
void ALAPIENTRY
alBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat flValue1
, ALfloat flValue2
, ALfloat flValue3
)
626 ALCcontext
*pContext
;
632 pContext
= alcGetCurrentContext();
633 SuspendContext(pContext
);
635 if (alIsBuffer(buffer
) && (buffer
!= 0))
640 alSetError(AL_INVALID_ENUM
);
646 alSetError(AL_INVALID_NAME
);
649 ProcessContext(pContext
);
653 ALAPI
void ALAPIENTRY
alBufferfv(ALuint buffer
, ALenum eParam
, const ALfloat
* flValues
)
655 ALCcontext
*pContext
;
659 pContext
= alcGetCurrentContext();
660 SuspendContext(pContext
);
662 if (alIsBuffer(buffer
) && (buffer
!= 0))
667 alSetError(AL_INVALID_ENUM
);
673 alSetError(AL_INVALID_NAME
);
676 ProcessContext(pContext
);
680 ALAPI
void ALAPIENTRY
alBufferi(ALuint buffer
, ALenum eParam
, ALint lValue
)
682 ALCcontext
*pContext
;
686 pContext
= alcGetCurrentContext();
687 SuspendContext(pContext
);
689 if (alIsBuffer(buffer
) && (buffer
!= 0))
694 alSetError(AL_INVALID_ENUM
);
700 alSetError(AL_INVALID_NAME
);
703 ProcessContext(pContext
);
707 ALAPI
void ALAPIENTRY
alBuffer3i( ALuint buffer
, ALenum eParam
, ALint lValue1
, ALint lValue2
, ALint lValue3
)
709 ALCcontext
*pContext
;
715 pContext
= alcGetCurrentContext();
716 SuspendContext(pContext
);
718 if (alIsBuffer(buffer
) && (buffer
!= 0))
723 alSetError(AL_INVALID_ENUM
);
729 alSetError(AL_INVALID_NAME
);
732 ProcessContext(pContext
);
736 ALAPI
void ALAPIENTRY
alBufferiv(ALuint buffer
, ALenum eParam
, const ALint
* plValues
)
738 ALCcontext
*pContext
;
742 pContext
= alcGetCurrentContext();
743 SuspendContext(pContext
);
745 if (alIsBuffer(buffer
) && (buffer
!= 0))
750 alSetError(AL_INVALID_ENUM
);
756 alSetError(AL_INVALID_NAME
);
759 ProcessContext(pContext
);
763 ALAPI ALvoid ALAPIENTRY
alGetBufferf(ALuint buffer
, ALenum eParam
, ALfloat
*pflValue
)
765 ALCcontext
*pContext
;
767 pContext
= alcGetCurrentContext();
768 SuspendContext(pContext
);
772 if (alIsBuffer(buffer
) && (buffer
!= 0))
777 alSetError(AL_INVALID_ENUM
);
783 alSetError(AL_INVALID_NAME
);
788 alSetError(AL_INVALID_VALUE
);
791 ProcessContext(pContext
);
795 ALAPI
void ALAPIENTRY
alGetBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat
* pflValue1
, ALfloat
* pflValue2
, ALfloat
* pflValue3
)
797 ALCcontext
*pContext
;
799 pContext
= alcGetCurrentContext();
800 SuspendContext(pContext
);
802 if ((pflValue1
) && (pflValue2
) && (pflValue3
))
804 if (alIsBuffer(buffer
) && (buffer
!= 0))
809 alSetError(AL_INVALID_ENUM
);
815 alSetError(AL_INVALID_NAME
);
820 alSetError(AL_INVALID_VALUE
);
823 ProcessContext(pContext
);
827 ALAPI
void ALAPIENTRY
alGetBufferfv(ALuint buffer
, ALenum eParam
, ALfloat
* pflValues
)
829 ALCcontext
*pContext
;
831 pContext
= alcGetCurrentContext();
832 SuspendContext(pContext
);
836 if (alIsBuffer(buffer
) && (buffer
!= 0))
841 alSetError(AL_INVALID_ENUM
);
847 alSetError(AL_INVALID_NAME
);
852 alSetError(AL_INVALID_VALUE
);
855 ProcessContext(pContext
);
859 ALAPI ALvoid ALAPIENTRY
alGetBufferi(ALuint buffer
, ALenum eParam
, ALint
*plValue
)
861 ALCcontext
*pContext
;
864 pContext
= alcGetCurrentContext();
865 SuspendContext(pContext
);
869 if (alIsBuffer(buffer
) && (buffer
!= 0))
871 pBuffer
= ((ALbuffer
*)ALTHUNK_LOOKUPENTRY(buffer
));
876 *plValue
= pBuffer
->frequency
;
880 *plValue
= aluBytesFromFormat(pBuffer
->format
) * 8;
884 *plValue
= aluChannelsFromFormat(pBuffer
->format
);
888 *plValue
= pBuffer
->size
;
892 alSetError(AL_INVALID_ENUM
);
898 alSetError(AL_INVALID_NAME
);
903 alSetError(AL_INVALID_VALUE
);
906 ProcessContext(pContext
);
910 ALAPI
void ALAPIENTRY
alGetBuffer3i(ALuint buffer
, ALenum eParam
, ALint
* plValue1
, ALint
* plValue2
, ALint
* plValue3
)
912 ALCcontext
*pContext
;
914 pContext
= alcGetCurrentContext();
915 SuspendContext(pContext
);
917 if ((plValue1
) && (plValue2
) && (plValue3
))
919 if (alIsBuffer(buffer
) && (buffer
!= 0))
924 alSetError(AL_INVALID_ENUM
);
930 alSetError(AL_INVALID_NAME
);
935 alSetError(AL_INVALID_VALUE
);
938 ProcessContext(pContext
);
942 ALAPI
void ALAPIENTRY
alGetBufferiv(ALuint buffer
, ALenum eParam
, ALint
* plValues
)
944 ALCcontext
*pContext
;
946 pContext
= alcGetCurrentContext();
947 SuspendContext(pContext
);
951 if (alIsBuffer(buffer
) && (buffer
!= 0))
959 alGetBufferi(buffer
, eParam
, plValues
);
963 alSetError(AL_INVALID_ENUM
);
969 alSetError(AL_INVALID_NAME
);
974 alSetError(AL_INVALID_VALUE
);
977 ProcessContext(pContext
);
983 * Loads the specified data into the buffer, using the specified formats.
984 * Currently, the new format must be 16-bit, and must have the same channel
985 * configuration as the original format. This does NOT handle compressed
986 * formats (eg. IMA4).
988 static void LoadData(ALbuffer
*ALBuf
, const ALubyte
*data
, ALsizei size
, ALuint freq
, ALenum OrigFormat
, ALenum NewFormat
)
990 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
991 ALuint OrigBytes
= aluBytesFromFormat(OrigFormat
);
992 ALuint OrigChannels
= aluChannelsFromFormat(OrigFormat
);
995 assert(aluBytesFromFormat(NewFormat
) == 2);
996 assert(NewChannels
== OrigChannels
);
998 if ((size
%(OrigBytes
*OrigChannels
)) != 0)
1000 alSetError(AL_INVALID_VALUE
);
1007 size
/= sizeof(ALubyte
);
1009 // 8bit Samples are converted to 16 bit here
1010 // Allocate 8 extra samples
1011 ALBuf
->data
= realloc(ALBuf
->data
, (8*NewChannels
+ size
) * (1*sizeof(ALshort
)));
1014 for (i
= 0;i
< size
;i
++)
1015 ALBuf
->data
[i
] = (ALshort
)((data
[i
]-128) << 8);
1016 memset(&(ALBuf
->data
[size
]), 0, 16*NewChannels
);
1018 ALBuf
->format
= NewFormat
;
1019 ALBuf
->eOriginalFormat
= OrigFormat
;
1020 ALBuf
->size
= size
*1*sizeof(ALshort
);
1021 ALBuf
->frequency
= freq
;
1024 alSetError(AL_OUT_OF_MEMORY
);
1028 size
/= sizeof(ALshort
);
1030 // Allocate 8 extra samples
1031 ALBuf
->data
= realloc(ALBuf
->data
, (8*NewChannels
+ size
) * (1*sizeof(ALshort
)));
1034 memcpy(ALBuf
->data
, data
, size
*1*sizeof(ALshort
));
1035 memset(&(ALBuf
->data
[size
]), 0, 16*NewChannels
);
1037 ALBuf
->format
= NewFormat
;
1038 ALBuf
->eOriginalFormat
= OrigFormat
;
1039 ALBuf
->size
= size
*1*sizeof(ALshort
);
1040 ALBuf
->frequency
= freq
;
1043 alSetError(AL_OUT_OF_MEMORY
);
1047 size
/= sizeof(ALfloat
);
1049 // Allocate 8 extra samples
1050 ALBuf
->data
= realloc(ALBuf
->data
, (8*NewChannels
+ size
) * (1*sizeof(ALshort
)));
1053 for (i
= 0;i
< size
;i
++)
1054 ALBuf
->data
[i
] = (ALshort
)(((ALfloat
*)data
)[i
] * 32767.5f
- 0.5);
1055 memset(&(ALBuf
->data
[size
]), 0, 16*NewChannels
);
1057 ALBuf
->format
= NewFormat
;
1058 ALBuf
->eOriginalFormat
= OrigFormat
;
1059 ALBuf
->size
= size
*1*sizeof(ALshort
);
1060 ALBuf
->frequency
= freq
;
1063 alSetError(AL_OUT_OF_MEMORY
);
1073 * ReleaseALBuffers()
1075 * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
1077 ALvoid
ReleaseALBuffers(ALvoid
)
1080 ALbuffer
*ALBufferTemp
;
1083 if(g_uiBufferCount
> 0)
1084 AL_PRINT("exit() %d Buffer(s) NOT deleted\n", g_uiBufferCount
);
1087 ALBuffer
= g_pBuffers
;
1090 // Release sample data
1091 free(ALBuffer
->data
);
1093 // Release Buffer structure
1094 ALBufferTemp
= ALBuffer
;
1095 ALBuffer
= ALBuffer
->next
;
1096 memset(ALBufferTemp
, 0, sizeof(ALbuffer
));
1100 g_uiBufferCount
= 0;