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
31 #include "alDatabuffer.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
)
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 ALAPI ALvoid ALAPIENTRY
alGenBuffers(ALsizei n
,ALuint
*puiBuffers
)
112 Context
= GetContextSuspended();
115 // Check that we are actually generation some Buffers
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
**list
= &device
->BufferList
;
125 list
= &(*list
)->next
;
127 // Create all the new Buffers
130 *list
= calloc(1, sizeof(ALbuffer
));
133 alDeleteBuffers(i
, puiBuffers
);
134 alSetError(Context
, AL_OUT_OF_MEMORY
);
138 puiBuffers
[i
] = (ALuint
)ALTHUNK_ADDENTRY(*list
);
139 (*list
)->buffer
= puiBuffers
[i
];
141 device
->BufferCount
++;
144 list
= &(*list
)->next
;
149 // Pointer does not point to enough memory to write Buffer names
150 alSetError(Context
, AL_INVALID_VALUE
);
154 ProcessContext(Context
);
158 * alDeleteBuffers(ALsizei n, ALuint *puiBuffers)
160 * Deletes the n AL Buffers pointed to by puiBuffers
162 ALAPI ALvoid ALAPIENTRY
alDeleteBuffers(ALsizei n
, const ALuint
*puiBuffers
)
167 ALboolean bFailed
= AL_FALSE
;
169 Context
= GetContextSuspended();
172 // Check we are actually Deleting some Buffers
175 ALCdevice
*device
= Context
->Device
;
177 // Check that all the buffers are valid and can actually be deleted
178 for (i
= 0; i
< n
; i
++)
183 // Check for valid Buffer ID (can be NULL buffer)
184 if((ALBuf
=VerifyBuffer(device
->BufferList
, puiBuffers
[i
])) != NULL
)
186 if(ALBuf
->refcount
!= 0)
188 // Buffer still in use, cannot be deleted
189 alSetError(Context
, AL_INVALID_OPERATION
);
197 alSetError(Context
, AL_INVALID_NAME
);
203 // If all the Buffers were valid (and have Reference Counts of 0), then we can delete them
206 for (i
= 0; i
< n
; i
++)
208 if((ALBuf
=VerifyBuffer(device
->BufferList
, puiBuffers
[i
])) != NULL
)
210 ALbuffer
**list
= &device
->BufferList
;
212 while(*list
&& *list
!= ALBuf
)
213 list
= &(*list
)->next
;
216 *list
= (*list
)->next
;
218 // Release the memory used to store audio data
221 // Release buffer structure
222 ALTHUNK_REMOVEENTRY(puiBuffers
[i
]);
223 memset(ALBuf
, 0, sizeof(ALbuffer
));
224 device
->BufferCount
--;
231 alSetError(Context
, AL_INVALID_VALUE
);
233 ProcessContext(Context
);
237 * alIsBuffer(ALuint uiBuffer)
239 * Checks if ulBuffer is a valid Buffer Name
241 ALAPI ALboolean ALAPIENTRY
alIsBuffer(ALuint uiBuffer
)
244 ALboolean result
= AL_TRUE
;
246 Context
= GetContextSuspended();
247 if(!Context
) return AL_FALSE
;
250 result
= (VerifyBuffer(Context
->Device
->BufferList
, uiBuffer
) ?
253 ProcessContext(Context
);
259 * alBufferData(ALuint buffer,ALenum format,ALvoid *data,ALsizei size,ALsizei freq)
261 * Fill buffer with audio data
263 ALAPI ALvoid ALAPIENTRY
alBufferData(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei size
,ALsizei freq
)
271 Context
= GetContextSuspended();
274 device
= Context
->Device
;
275 if((ALBuf
=VerifyBuffer(device
->BufferList
, buffer
)) != NULL
)
277 if(Context
->SampleSource
)
281 if(Context
->SampleSource
->state
== MAPPED
)
283 alSetError(Context
, AL_INVALID_OPERATION
);
284 ProcessContext(Context
);
288 offset
= (const ALubyte
*)data
- (ALubyte
*)NULL
;
289 data
= Context
->SampleSource
->data
+ offset
;
292 if ((ALBuf
->refcount
==0)&&(data
))
296 case AL_FORMAT_MONO8
:
297 case AL_FORMAT_MONO16
:
298 case AL_FORMAT_MONO_FLOAT32
:
299 case AL_FORMAT_MONO_DOUBLE_EXT
:
300 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_MONO_FLOAT32
);
301 if(err
!= AL_NO_ERROR
)
302 alSetError(Context
, err
);
305 case AL_FORMAT_STEREO8
:
306 case AL_FORMAT_STEREO16
:
307 case AL_FORMAT_STEREO_FLOAT32
:
308 case AL_FORMAT_STEREO_DOUBLE_EXT
:
309 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_STEREO_FLOAT32
);
310 if(err
!= AL_NO_ERROR
)
311 alSetError(Context
, err
);
314 case AL_FORMAT_REAR8
:
315 case AL_FORMAT_REAR16
:
316 case AL_FORMAT_REAR32
: {
317 ALenum NewFormat
= AL_FORMAT_QUAD32
;
318 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
319 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
320 ALuint OrigBytes
= ((format
==AL_FORMAT_REAR8
) ? 1 :
321 ((format
==AL_FORMAT_REAR16
) ? 2 :
324 if((size
%(OrigBytes
*2)) != 0)
326 alSetError(Context
, AL_INVALID_VALUE
);
333 // Samples are converted here
334 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ size
) * NewBytes
);
338 ConvertDataRear(ALBuf
->data
, data
, OrigBytes
, size
);
340 ALBuf
->format
= NewFormat
;
341 ALBuf
->eOriginalFormat
= format
;
342 ALBuf
->size
= size
*NewBytes
;
343 ALBuf
->frequency
= freq
;
346 alSetError(Context
, AL_OUT_OF_MEMORY
);
349 case AL_FORMAT_QUAD8_LOKI
:
350 case AL_FORMAT_QUAD16_LOKI
:
351 case AL_FORMAT_QUAD8
:
352 case AL_FORMAT_QUAD16
:
353 case AL_FORMAT_QUAD32
:
354 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_QUAD32
);
355 if(err
!= AL_NO_ERROR
)
356 alSetError(Context
, err
);
359 case AL_FORMAT_51CHN8
:
360 case AL_FORMAT_51CHN16
:
361 case AL_FORMAT_51CHN32
:
362 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_51CHN32
);
363 if(err
!= AL_NO_ERROR
)
364 alSetError(Context
, err
);
367 case AL_FORMAT_61CHN8
:
368 case AL_FORMAT_61CHN16
:
369 case AL_FORMAT_61CHN32
:
370 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_61CHN32
);
371 if(err
!= AL_NO_ERROR
)
372 alSetError(Context
, err
);
375 case AL_FORMAT_71CHN8
:
376 case AL_FORMAT_71CHN16
:
377 case AL_FORMAT_71CHN32
:
378 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_71CHN32
);
379 if(err
!= AL_NO_ERROR
)
380 alSetError(Context
, err
);
383 case AL_FORMAT_MONO_IMA4
:
384 case AL_FORMAT_STEREO_IMA4
: {
385 int OrigChans
= ((format
==AL_FORMAT_MONO_IMA4
) ? 1 : 2);
386 ALenum NewFormat
= ((OrigChans
==1) ? AL_FORMAT_MONO_FLOAT32
:
387 AL_FORMAT_STEREO_FLOAT32
);
388 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
390 // Here is where things vary:
391 // nVidia and Apple use 64+1 samples per channel per block => block_size=36*chans bytes
392 // Most PC sound software uses 2040+1 samples per channel per block -> block_size=1024*chans bytes
393 if((size
%(36*OrigChans
)) != 0)
395 alSetError(Context
, AL_INVALID_VALUE
);
402 // Allocate extra padding samples
403 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*OrigChans
+ size
)*NewBytes
);
407 ConvertDataIMA4(ALBuf
->data
, data
, OrigChans
, size
/65);
409 ALBuf
->format
= NewFormat
;
410 ALBuf
->eOriginalFormat
= format
;
411 ALBuf
->size
= size
*NewBytes
;
412 ALBuf
->frequency
= freq
;
415 alSetError(Context
, AL_OUT_OF_MEMORY
);
418 case AL_FORMAT_MONO_MULAW
:
419 case AL_FORMAT_STEREO_MULAW
:
420 case AL_FORMAT_QUAD_MULAW
:
421 case AL_FORMAT_51CHN_MULAW
:
422 case AL_FORMAT_61CHN_MULAW
:
423 case AL_FORMAT_71CHN_MULAW
: {
424 int Channels
= ((format
==AL_FORMAT_MONO_MULAW
) ? 1 :
425 ((format
==AL_FORMAT_STEREO_MULAW
) ? 2 :
426 ((format
==AL_FORMAT_QUAD_MULAW
) ? 4 :
427 ((format
==AL_FORMAT_51CHN_MULAW
) ? 6 :
428 ((format
==AL_FORMAT_61CHN_MULAW
) ? 7 : 8)))));
429 ALenum NewFormat
= ((Channels
==1) ? AL_FORMAT_MONO_FLOAT32
:
430 ((Channels
==2) ? AL_FORMAT_STEREO_FLOAT32
:
431 ((Channels
==4) ? AL_FORMAT_QUAD32
:
432 ((Channels
==6) ? AL_FORMAT_51CHN32
:
433 ((Channels
==7) ? AL_FORMAT_61CHN32
:
434 AL_FORMAT_71CHN32
)))));
435 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
437 if((size
%(1*Channels
)) != 0)
439 alSetError(Context
, AL_INVALID_VALUE
);
443 // Allocate extra padding samples
444 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*Channels
+ size
)*NewBytes
);
448 ConvertDataMULaw(ALBuf
->data
, data
, size
);
450 ALBuf
->format
= NewFormat
;
451 ALBuf
->eOriginalFormat
= format
;
452 ALBuf
->size
= size
*NewBytes
;
453 ALBuf
->frequency
= freq
;
456 alSetError(Context
, AL_OUT_OF_MEMORY
);
459 case AL_FORMAT_REAR_MULAW
: {
461 ALenum NewFormat
= AL_FORMAT_QUAD32
;
462 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
463 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
465 if((size
%(1*OrigChans
)) != 0)
467 alSetError(Context
, AL_INVALID_VALUE
);
473 // Allocate extra padding samples
474 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ size
)*NewBytes
);
478 ConvertDataMULawRear(ALBuf
->data
, data
, size
);
480 ALBuf
->format
= NewFormat
;
481 ALBuf
->eOriginalFormat
= format
;
482 ALBuf
->size
= size
*NewBytes
;
483 ALBuf
->frequency
= freq
;
486 alSetError(Context
, AL_OUT_OF_MEMORY
);
490 alSetError(Context
, AL_INVALID_ENUM
);
496 // Buffer is in use, or data is a NULL pointer
497 alSetError(Context
, AL_INVALID_VALUE
);
502 // Invalid Buffer Name
503 alSetError(Context
, AL_INVALID_NAME
);
506 ProcessContext(Context
);
510 * alBufferSubDataEXT(ALuint buffer,ALenum format,ALvoid *data,ALsizei offset,ALsizei length)
512 * Fill buffer with audio data
514 ALvoid ALAPIENTRY
alBufferSubDataEXT(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei offset
,ALsizei length
)
520 Context
= GetContextSuspended();
523 device
= Context
->Device
;
524 if((ALBuf
=VerifyBuffer(device
->BufferList
, buffer
)) != NULL
)
526 if(Context
->SampleSource
)
530 if(Context
->SampleSource
->state
== MAPPED
)
532 alSetError(Context
, AL_INVALID_OPERATION
);
533 ProcessContext(Context
);
537 offset
= (const ALubyte
*)data
- (ALubyte
*)NULL
;
538 data
= Context
->SampleSource
->data
+ offset
;
541 if(ALBuf
->data
== NULL
)
543 // buffer does not have any data
544 alSetError(Context
, AL_INVALID_NAME
);
546 else if(length
< 0 || offset
< 0 || (length
> 0 && data
== NULL
))
548 // data is NULL or offset/length is negative
549 alSetError(Context
, AL_INVALID_VALUE
);
555 case AL_FORMAT_REAR8
:
556 case AL_FORMAT_REAR16
:
557 case AL_FORMAT_REAR32
: {
558 ALuint OrigBytes
= ((format
==AL_FORMAT_REAR8
) ? 1 :
559 ((format
==AL_FORMAT_REAR16
) ? 2 :
561 ALuint NewBytes
= aluBytesFromFormat(ALBuf
->format
);
563 if(ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR8
&&
564 ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR16
&&
565 ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR32
)
567 alSetError(Context
, AL_INVALID_ENUM
);
571 if(ALBuf
->size
/4/NewBytes
< (ALuint
)offset
+length
)
573 alSetError(Context
, AL_INVALID_VALUE
);
577 ConvertDataRear(&ALBuf
->data
[offset
*4], data
, OrigBytes
, length
*2);
580 case AL_FORMAT_MONO_IMA4
:
581 case AL_FORMAT_STEREO_IMA4
: {
582 int Channels
= aluChannelsFromFormat(ALBuf
->format
);
583 ALuint Bytes
= aluBytesFromFormat(ALBuf
->format
);
585 if(ALBuf
->eOriginalFormat
!= format
)
587 alSetError(Context
, AL_INVALID_ENUM
);
591 if((offset
%65) != 0 || (length
%65) != 0 ||
592 ALBuf
->size
/Channels
/Bytes
< (ALuint
)offset
+length
)
594 alSetError(Context
, AL_INVALID_VALUE
);
598 ConvertDataIMA4(&ALBuf
->data
[offset
*Channels
], data
, Channels
, length
/65*Channels
);
601 case AL_FORMAT_MONO_MULAW
:
602 case AL_FORMAT_STEREO_MULAW
:
603 case AL_FORMAT_QUAD_MULAW
:
604 case AL_FORMAT_REAR_MULAW
:
605 case AL_FORMAT_51CHN_MULAW
:
606 case AL_FORMAT_61CHN_MULAW
:
607 case AL_FORMAT_71CHN_MULAW
: {
608 int Channels
= aluChannelsFromFormat(ALBuf
->format
);
609 ALuint Bytes
= aluBytesFromFormat(ALBuf
->format
);
611 if(ALBuf
->eOriginalFormat
!= format
)
613 alSetError(Context
, AL_INVALID_ENUM
);
617 if(ALBuf
->size
/Channels
/Bytes
< (ALuint
)offset
+length
)
619 alSetError(Context
, AL_INVALID_VALUE
);
623 if(ALBuf
->eOriginalFormat
== AL_FORMAT_REAR_MULAW
)
624 ConvertDataMULawRear(&ALBuf
->data
[offset
*Channels
], data
, length
*2);
626 ConvertDataMULaw(&ALBuf
->data
[offset
*Channels
], data
, length
*Channels
);
630 ALuint Channels
= aluChannelsFromFormat(format
);
631 ALuint Bytes
= aluBytesFromFormat(format
);
632 ALuint NewBytes
= aluBytesFromFormat(ALBuf
->format
);
634 if(Channels
!= aluChannelsFromFormat(ALBuf
->format
))
636 alSetError(Context
, AL_INVALID_ENUM
);
640 if(ALBuf
->size
/Channels
/NewBytes
< (ALuint
)offset
+length
)
642 alSetError(Context
, AL_INVALID_VALUE
);
646 ConvertData(&ALBuf
->data
[offset
*Channels
], data
, Bytes
, length
*Channels
);
653 // Invalid Buffer Name
654 alSetError(Context
, AL_INVALID_NAME
);
657 ProcessContext(Context
);
661 ALAPI
void ALAPIENTRY
alBufferf(ALuint buffer
, ALenum eParam
, ALfloat flValue
)
663 ALCcontext
*pContext
;
668 pContext
= GetContextSuspended();
669 if(!pContext
) return;
671 device
= pContext
->Device
;
672 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
677 alSetError(pContext
, AL_INVALID_ENUM
);
683 alSetError(pContext
, AL_INVALID_NAME
);
686 ProcessContext(pContext
);
690 ALAPI
void ALAPIENTRY
alBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat flValue1
, ALfloat flValue2
, ALfloat flValue3
)
692 ALCcontext
*pContext
;
699 pContext
= GetContextSuspended();
700 if(!pContext
) return;
702 device
= pContext
->Device
;
703 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
708 alSetError(pContext
, AL_INVALID_ENUM
);
714 alSetError(pContext
, AL_INVALID_NAME
);
717 ProcessContext(pContext
);
721 ALAPI
void ALAPIENTRY
alBufferfv(ALuint buffer
, ALenum eParam
, const ALfloat
* flValues
)
723 ALCcontext
*pContext
;
728 pContext
= GetContextSuspended();
729 if(!pContext
) return;
731 device
= pContext
->Device
;
732 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
737 alSetError(pContext
, AL_INVALID_ENUM
);
743 alSetError(pContext
, AL_INVALID_NAME
);
746 ProcessContext(pContext
);
750 ALAPI
void ALAPIENTRY
alBufferi(ALuint buffer
, ALenum eParam
, ALint lValue
)
752 ALCcontext
*pContext
;
757 pContext
= GetContextSuspended();
758 if(!pContext
) return;
760 device
= pContext
->Device
;
761 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
766 alSetError(pContext
, AL_INVALID_ENUM
);
772 alSetError(pContext
, AL_INVALID_NAME
);
775 ProcessContext(pContext
);
779 ALAPI
void ALAPIENTRY
alBuffer3i( ALuint buffer
, ALenum eParam
, ALint lValue1
, ALint lValue2
, ALint lValue3
)
781 ALCcontext
*pContext
;
788 pContext
= GetContextSuspended();
789 if(!pContext
) return;
791 device
= pContext
->Device
;
792 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
797 alSetError(pContext
, AL_INVALID_ENUM
);
803 alSetError(pContext
, AL_INVALID_NAME
);
806 ProcessContext(pContext
);
810 ALAPI
void ALAPIENTRY
alBufferiv(ALuint buffer
, ALenum eParam
, const ALint
* plValues
)
812 ALCcontext
*pContext
;
817 pContext
= GetContextSuspended();
818 if(!pContext
) return;
820 device
= pContext
->Device
;
821 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
826 alSetError(pContext
, AL_INVALID_ENUM
);
832 alSetError(pContext
, AL_INVALID_NAME
);
835 ProcessContext(pContext
);
839 ALAPI ALvoid ALAPIENTRY
alGetBufferf(ALuint buffer
, ALenum eParam
, ALfloat
*pflValue
)
841 ALCcontext
*pContext
;
844 pContext
= GetContextSuspended();
845 if(!pContext
) return;
849 device
= pContext
->Device
;
850 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
855 alSetError(pContext
, AL_INVALID_ENUM
);
861 alSetError(pContext
, AL_INVALID_NAME
);
866 alSetError(pContext
, AL_INVALID_VALUE
);
869 ProcessContext(pContext
);
873 ALAPI
void ALAPIENTRY
alGetBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat
* pflValue1
, ALfloat
* pflValue2
, ALfloat
* pflValue3
)
875 ALCcontext
*pContext
;
878 pContext
= GetContextSuspended();
879 if(!pContext
) return;
881 if ((pflValue1
) && (pflValue2
) && (pflValue3
))
883 device
= pContext
->Device
;
884 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
889 alSetError(pContext
, AL_INVALID_ENUM
);
895 alSetError(pContext
, AL_INVALID_NAME
);
900 alSetError(pContext
, AL_INVALID_VALUE
);
903 ProcessContext(pContext
);
907 ALAPI
void ALAPIENTRY
alGetBufferfv(ALuint buffer
, ALenum eParam
, ALfloat
* pflValues
)
909 ALCcontext
*pContext
;
912 pContext
= GetContextSuspended();
913 if(!pContext
) return;
917 device
= pContext
->Device
;
918 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
923 alSetError(pContext
, AL_INVALID_ENUM
);
929 alSetError(pContext
, AL_INVALID_NAME
);
934 alSetError(pContext
, AL_INVALID_VALUE
);
937 ProcessContext(pContext
);
941 ALAPI ALvoid ALAPIENTRY
alGetBufferi(ALuint buffer
, ALenum eParam
, ALint
*plValue
)
943 ALCcontext
*pContext
;
947 pContext
= GetContextSuspended();
948 if(!pContext
) return;
952 device
= pContext
->Device
;
953 if((pBuffer
=VerifyBuffer(device
->BufferList
, buffer
)) != NULL
)
958 *plValue
= pBuffer
->frequency
;
962 *plValue
= aluBytesFromFormat(pBuffer
->format
) * 8;
966 *plValue
= aluChannelsFromFormat(pBuffer
->format
);
970 *plValue
= pBuffer
->size
;
974 alSetError(pContext
, AL_INVALID_ENUM
);
980 alSetError(pContext
, AL_INVALID_NAME
);
985 alSetError(pContext
, AL_INVALID_VALUE
);
988 ProcessContext(pContext
);
992 ALAPI
void ALAPIENTRY
alGetBuffer3i(ALuint buffer
, ALenum eParam
, ALint
* plValue1
, ALint
* plValue2
, ALint
* plValue3
)
994 ALCcontext
*pContext
;
997 pContext
= GetContextSuspended();
998 if(!pContext
) return;
1000 if ((plValue1
) && (plValue2
) && (plValue3
))
1002 device
= pContext
->Device
;
1003 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
1008 alSetError(pContext
, AL_INVALID_ENUM
);
1014 alSetError(pContext
, AL_INVALID_NAME
);
1019 alSetError(pContext
, AL_INVALID_VALUE
);
1022 ProcessContext(pContext
);
1026 ALAPI
void ALAPIENTRY
alGetBufferiv(ALuint buffer
, ALenum eParam
, ALint
* plValues
)
1028 ALCcontext
*pContext
;
1031 pContext
= GetContextSuspended();
1032 if(!pContext
) return;
1036 device
= pContext
->Device
;
1037 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
1045 alGetBufferi(buffer
, eParam
, plValues
);
1049 alSetError(pContext
, AL_INVALID_ENUM
);
1055 alSetError(pContext
, AL_INVALID_NAME
);
1060 alSetError(pContext
, AL_INVALID_VALUE
);
1063 ProcessContext(pContext
);
1069 * Loads the specified data into the buffer, using the specified formats.
1070 * Currently, the new format must be 32-bit float, and must have the same
1071 * channel configuration as the original format. This does NOT handle
1072 * compressed formats (eg. IMA4).
1074 static ALenum
LoadData(ALbuffer
*ALBuf
, const ALubyte
*data
, ALsizei size
, ALuint freq
, ALenum OrigFormat
, ALenum NewFormat
)
1076 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
1077 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
1078 ALuint OrigBytes
= aluBytesFromFormat(OrigFormat
);
1079 ALuint OrigChannels
= aluChannelsFromFormat(OrigFormat
);
1082 assert(NewBytes
== 4);
1083 assert(NewChannels
== OrigChannels
);
1085 if ((size
%(OrigBytes
*OrigChannels
)) != 0)
1086 return AL_INVALID_VALUE
;
1088 // Samples are converted here
1090 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ size
) * NewBytes
);
1091 if(!temp
) return AL_OUT_OF_MEMORY
;
1093 ConvertData(ALBuf
->data
, data
, OrigBytes
, size
);
1095 ALBuf
->format
= NewFormat
;
1096 ALBuf
->eOriginalFormat
= OrigFormat
;
1097 ALBuf
->size
= size
*NewBytes
;
1098 ALBuf
->frequency
= freq
;
1103 static void ConvertData(ALfloat
*dst
, const ALvoid
*src
, ALint origBytes
, ALsizei len
)
1110 for(i
= 0;i
< len
;i
++)
1112 smp
= ((ALubyte
*)src
)[i
];
1113 dst
[i
] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1118 for(i
= 0;i
< len
;i
++)
1120 smp
= ((ALshort
*)src
)[i
];
1121 dst
[i
] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1126 for(i
= 0;i
< len
;i
++)
1127 dst
[i
] = ((ALfloat
*)src
)[i
];
1131 for(i
= 0;i
< len
;i
++)
1132 dst
[i
] = ((ALdouble
*)src
)[i
];
1140 static void ConvertDataRear(ALfloat
*dst
, const ALvoid
*src
, ALint origBytes
, ALsizei len
)
1147 for(i
= 0;i
< len
;i
+=4)
1151 smp
= ((ALubyte
*)src
)[i
/2+0];
1152 dst
[i
+2] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1153 smp
= ((ALubyte
*)src
)[i
/2+1];
1154 dst
[i
+3] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1159 for(i
= 0;i
< len
;i
+=4)
1163 smp
= ((ALshort
*)src
)[i
/2+0];
1164 dst
[i
+2] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1165 smp
= ((ALshort
*)src
)[i
/2+1];
1166 dst
[i
+3] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1171 for(i
= 0;i
< len
;i
+=4)
1175 dst
[i
+2] = ((ALfloat
*)src
)[i
/2+0];
1176 dst
[i
+3] = ((ALfloat
*)src
)[i
/2+1];
1185 static void ConvertDataIMA4(ALfloat
*dst
, const ALvoid
*src
, ALint origChans
, ALsizei len
)
1187 const ALuint
*IMAData
;
1188 ALint Sample
[2],Index
[2];
1193 for(i
= 0;i
< len
/origChans
;i
++)
1195 for(c
= 0;c
< origChans
;c
++)
1197 Sample
[c
] = ((ALshort
*)IMAData
)[0];
1198 Index
[c
] = ((ALshort
*)IMAData
)[1];
1200 Index
[c
] = ((Index
[c
]<0) ? 0 : Index
[c
]);
1201 Index
[c
] = ((Index
[c
]>88) ? 88 : Index
[c
]);
1203 dst
[i
*65*origChans
+ c
] = ((Sample
[c
] < 0) ? (Sample
[c
]/32768.0f
) : (Sample
[c
]/32767.0f
));
1208 for(j
= 1;j
< 65;j
+= 8)
1210 for(c
= 0;c
< origChans
;c
++)
1211 IMACode
[c
] = *(IMAData
++);
1213 for(k
= 0;k
< 8;k
++)
1215 for(c
= 0;c
< origChans
;c
++)
1217 Sample
[c
] += ((g_IMAStep_size
[Index
[c
]]*g_IMACodeword_4
[IMACode
[c
]&15])/8);
1218 Index
[c
] += g_IMAIndex_adjust_4
[IMACode
[c
]&15];
1220 if(Sample
[c
] < -32768) Sample
[c
] = -32768;
1221 else if(Sample
[c
] > 32767) Sample
[c
] = 32767;
1223 if(Index
[c
]<0) Index
[c
] = 0;
1224 else if(Index
[c
]>88) Index
[c
] = 88;
1226 dst
[(i
*65+j
+k
)*origChans
+ c
] = ((Sample
[c
] < 0) ? (Sample
[c
]/32768.0f
) : (Sample
[c
]/32767.0f
));
1234 static void ConvertDataMULaw(ALfloat
*dst
, const ALvoid
*src
, ALsizei len
)
1238 for(i
= 0;i
< len
;i
++)
1240 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
]];
1241 dst
[i
] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1245 static void ConvertDataMULawRear(ALfloat
*dst
, const ALvoid
*src
, ALsizei len
)
1249 for(i
= 0;i
< len
;i
+=4)
1253 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
/2+0]];
1254 dst
[i
+2] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1255 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
/2+1]];
1256 dst
[i
+3] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1261 * ReleaseALBuffers()
1263 * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
1265 ALvoid
ReleaseALBuffers(ALCdevice
*device
)
1268 ALbuffer
*ALBufferTemp
;
1270 ALBuffer
= device
->BufferList
;
1273 // Release sample data
1274 free(ALBuffer
->data
);
1276 // Release Buffer structure
1277 ALBufferTemp
= ALBuffer
;
1278 ALBuffer
= ALBuffer
->next
;
1279 memset(ALBufferTemp
, 0, sizeof(ALbuffer
));
1282 device
->BufferList
= NULL
;
1283 device
->BufferCount
= 0;