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 void 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
);
46 static const long g_IMAStep_size
[89]={ // IMA ADPCM Stepsize table
47 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
48 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143,
49 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
50 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
51 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,11487,12635,13899,
52 15289,16818,18500,20350,22358,24633,27086,29794,32767
55 static const long g_IMACodeword_4
[16]={ // IMA4 ADPCM Codeword decode table
56 1, 3, 5, 7, 9, 11, 13, 15,
57 -1,-3,-5,-7,-9,-11,-13,-15,
60 static const long g_IMAIndex_adjust_4
[16]={ // IMA4 ADPCM Step index adjust decode table
61 -1,-1,-1,-1, 2, 4, 6, 8,
62 -1,-1,-1,-1, 2, 4, 6, 8
65 static const ALshort muLawDecompressionTable
[256] = {
66 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
67 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
68 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
69 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
70 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
71 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
72 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
73 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
74 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
75 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
76 -876, -844, -812, -780, -748, -716, -684, -652,
77 -620, -588, -556, -524, -492, -460, -428, -396,
78 -372, -356, -340, -324, -308, -292, -276, -260,
79 -244, -228, -212, -196, -180, -164, -148, -132,
80 -120, -112, -104, -96, -88, -80, -72, -64,
81 -56, -48, -40, -32, -24, -16, -8, 0,
82 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
83 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
84 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
85 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
86 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
87 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
88 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
89 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
90 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
91 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
92 876, 844, 812, 780, 748, 716, 684, 652,
93 620, 588, 556, 524, 492, 460, 428, 396,
94 372, 356, 340, 324, 308, 292, 276, 260,
95 244, 228, 212, 196, 180, 164, 148, 132,
96 120, 112, 104, 96, 88, 80, 72, 64,
97 56, 48, 40, 32, 24, 16, 8, 0
101 * alGenBuffers(ALsizei n, ALuint *puiBuffers)
103 * Generates n AL Buffers, and stores the Buffers Names in the array pointed to by puiBuffers
105 ALAPI ALvoid ALAPIENTRY
alGenBuffers(ALsizei n
,ALuint
*puiBuffers
)
110 Context
= GetContextSuspended();
113 // Check that we are actually generation some Buffers
116 ALCdevice
*device
= Context
->Device
;
118 // Check the pointer is valid (and points to enough memory to store Buffer Names)
119 if (!IsBadWritePtr((void*)puiBuffers
, n
* sizeof(ALuint
)))
121 ALbuffer
**list
= &device
->BufferList
;
123 list
= &(*list
)->next
;
125 // Create all the new Buffers
128 *list
= calloc(1, sizeof(ALbuffer
));
131 alDeleteBuffers(i
, puiBuffers
);
132 alSetError(AL_OUT_OF_MEMORY
);
136 puiBuffers
[i
] = (ALuint
)ALTHUNK_ADDENTRY(*list
);
137 (*list
)->buffer
= puiBuffers
[i
];
139 device
->BufferCount
++;
142 list
= &(*list
)->next
;
147 // Pointer does not point to enough memory to write Buffer names
148 alSetError(AL_INVALID_VALUE
);
152 ProcessContext(Context
);
156 * alDeleteBuffers(ALsizei n, ALuint *puiBuffers)
158 * Deletes the n AL Buffers pointed to by puiBuffers
160 ALAPI ALvoid ALAPIENTRY
alDeleteBuffers(ALsizei n
, const ALuint
*puiBuffers
)
165 ALboolean bFailed
= AL_FALSE
;
167 Context
= GetContextSuspended();
170 // Check we are actually Deleting some Buffers
173 ALCdevice
*device
= Context
->Device
;
175 // Check that all the buffers are valid and can actually be deleted
176 for (i
= 0; i
< n
; i
++)
178 // Check for valid Buffer ID (can be NULL buffer)
179 if (alIsBuffer(puiBuffers
[i
]))
181 // If not the NULL buffer, check that the reference count is 0
182 ALBuf
= ((ALbuffer
*)ALTHUNK_LOOKUPENTRY(puiBuffers
[i
]));
185 if (ALBuf
->refcount
!= 0)
187 // Buffer still in use, cannot be deleted
188 alSetError(AL_INVALID_OPERATION
);
196 alSetError(AL_INVALID_NAME
);
201 // If all the Buffers were valid (and have Reference Counts of 0), then we can delete them
204 for (i
= 0; i
< n
; i
++)
206 if (puiBuffers
[i
] && alIsBuffer(puiBuffers
[i
]))
208 ALbuffer
**list
= &device
->BufferList
;
210 ALBuf
=((ALbuffer
*)ALTHUNK_LOOKUPENTRY(puiBuffers
[i
]));
211 while(*list
&& *list
!= ALBuf
)
212 list
= &(*list
)->next
;
215 *list
= (*list
)->next
;
217 // Release the memory used to store audio data
220 // Release buffer structure
221 ALTHUNK_REMOVEENTRY(puiBuffers
[i
]);
222 memset(ALBuf
, 0, sizeof(ALbuffer
));
223 device
->BufferCount
--;
230 alSetError(AL_INVALID_VALUE
);
232 ProcessContext(Context
);
236 * alIsBuffer(ALuint uiBuffer)
238 * Checks if ulBuffer is a valid Buffer Name
240 ALAPI ALboolean ALAPIENTRY
alIsBuffer(ALuint uiBuffer
)
243 ALboolean result
=AL_FALSE
;
247 Context
= GetContextSuspended();
248 if(!Context
) return AL_FALSE
;
252 ALCdevice
*device
= Context
->Device
;
254 TgtALBuf
= (ALbuffer
*)ALTHUNK_LOOKUPENTRY(uiBuffer
);
256 // Check through list of generated buffers for uiBuffer
257 ALBuf
= device
->BufferList
;
260 if (ALBuf
== TgtALBuf
)
275 ProcessContext(Context
);
281 * alBufferData(ALuint buffer,ALenum format,ALvoid *data,ALsizei size,ALsizei freq)
283 * Fill buffer with audio data
285 ALAPI ALvoid ALAPIENTRY
alBufferData(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei size
,ALsizei freq
)
291 Context
= GetContextSuspended();
294 if (alIsBuffer(buffer
) && (buffer
!= 0))
296 ALBuf
=((ALbuffer
*)ALTHUNK_LOOKUPENTRY(buffer
));
298 if(Context
->SampleSource
)
302 if(Context
->SampleSource
->state
== MAPPED
)
304 alSetError(AL_INVALID_OPERATION
);
305 ProcessContext(Context
);
309 offset
= (ALuint
)data
;
310 data
= Context
->SampleSource
->data
+ offset
;
313 if ((ALBuf
->refcount
==0)&&(data
))
317 case AL_FORMAT_MONO8
:
318 case AL_FORMAT_MONO16
:
319 case AL_FORMAT_MONO_FLOAT32
:
320 case AL_FORMAT_MONO_DOUBLE_EXT
:
321 LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_MONO_FLOAT32
);
324 case AL_FORMAT_STEREO8
:
325 case AL_FORMAT_STEREO16
:
326 case AL_FORMAT_STEREO_FLOAT32
:
327 case AL_FORMAT_STEREO_DOUBLE_EXT
:
328 LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_STEREO_FLOAT32
);
331 case AL_FORMAT_REAR8
:
332 case AL_FORMAT_REAR16
:
333 case AL_FORMAT_REAR32
: {
334 ALuint NewFormat
= AL_FORMAT_QUAD32
;
335 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
336 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
337 ALuint OrigBytes
= ((format
==AL_FORMAT_REAR8
) ? 1 :
338 ((format
==AL_FORMAT_REAR16
) ? 2 :
341 assert(aluBytesFromFormat(NewFormat
) == 2);
343 if((size
%(OrigBytes
*2)) != 0)
345 alSetError(AL_INVALID_VALUE
);
352 // Samples are converted here
353 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ size
) * NewBytes
);
357 ConvertDataRear(ALBuf
->data
, data
, OrigBytes
, size
);
359 ALBuf
->format
= NewFormat
;
360 ALBuf
->eOriginalFormat
= format
;
361 ALBuf
->size
= size
*NewBytes
;
362 ALBuf
->frequency
= freq
;
365 alSetError(AL_OUT_OF_MEMORY
);
368 case AL_FORMAT_QUAD8_LOKI
:
369 case AL_FORMAT_QUAD16_LOKI
:
370 case AL_FORMAT_QUAD8
:
371 case AL_FORMAT_QUAD16
:
372 case AL_FORMAT_QUAD32
:
373 LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_QUAD32
);
376 case AL_FORMAT_51CHN8
:
377 case AL_FORMAT_51CHN16
:
378 case AL_FORMAT_51CHN32
:
379 LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_51CHN32
);
382 case AL_FORMAT_61CHN8
:
383 case AL_FORMAT_61CHN16
:
384 case AL_FORMAT_61CHN32
:
385 LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_61CHN32
);
388 case AL_FORMAT_71CHN8
:
389 case AL_FORMAT_71CHN16
:
390 case AL_FORMAT_71CHN32
:
391 LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_71CHN32
);
394 case AL_FORMAT_MONO_IMA4
:
395 case AL_FORMAT_STEREO_IMA4
: {
396 int OrigChans
= ((format
==AL_FORMAT_MONO_IMA4
) ? 1 : 2);
397 ALuint NewFormat
= ((OrigChans
==1) ? AL_FORMAT_MONO_FLOAT32
:
398 AL_FORMAT_STEREO_FLOAT32
);
399 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
401 // Here is where things vary:
402 // nVidia and Apple use 64+1 samples per channel per block => block_size=36*chans bytes
403 // Most PC sound software uses 2040+1 samples per channel per block -> block_size=1024*chans bytes
404 if((size
%(36*OrigChans
)) != 0)
406 alSetError(AL_INVALID_VALUE
);
413 // Allocate extra padding samples
414 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*OrigChans
+ size
)*NewBytes
);
418 ConvertDataIMA4(ALBuf
->data
, data
, OrigChans
, size
/65);
420 ALBuf
->format
= NewFormat
;
421 ALBuf
->eOriginalFormat
= format
;
422 ALBuf
->size
= size
*NewBytes
;
423 ALBuf
->frequency
= freq
;
426 alSetError(AL_OUT_OF_MEMORY
);
429 case AL_FORMAT_MONO_MULAW
:
430 case AL_FORMAT_STEREO_MULAW
:
431 case AL_FORMAT_QUAD_MULAW
:
432 case AL_FORMAT_51CHN_MULAW
:
433 case AL_FORMAT_61CHN_MULAW
:
434 case AL_FORMAT_71CHN_MULAW
: {
435 int Channels
= ((format
==AL_FORMAT_MONO_MULAW
) ? 1 :
436 ((format
==AL_FORMAT_STEREO_MULAW
) ? 2 :
437 ((format
==AL_FORMAT_QUAD_MULAW
) ? 4 :
438 ((format
==AL_FORMAT_51CHN_MULAW
) ? 6 :
439 ((format
==AL_FORMAT_61CHN_MULAW
) ? 7 : 8)))));
440 ALuint NewFormat
= ((Channels
==1) ? AL_FORMAT_MONO_FLOAT32
:
441 ((Channels
==2) ? AL_FORMAT_STEREO_FLOAT32
:
442 ((Channels
==4) ? AL_FORMAT_QUAD32
:
443 ((Channels
==6) ? AL_FORMAT_51CHN32
:
444 ((Channels
==7) ? AL_FORMAT_61CHN32
:
445 AL_FORMAT_71CHN32
)))));
446 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
448 if((size
%(1*Channels
)) != 0)
450 alSetError(AL_INVALID_VALUE
);
454 // Allocate extra padding samples
455 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*Channels
+ size
)*NewBytes
);
459 ConvertDataMULaw(ALBuf
->data
, data
, size
);
461 ALBuf
->format
= NewFormat
;
462 ALBuf
->eOriginalFormat
= format
;
463 ALBuf
->size
= size
*NewBytes
;
464 ALBuf
->frequency
= freq
;
467 alSetError(AL_OUT_OF_MEMORY
);
470 case AL_FORMAT_REAR_MULAW
: {
472 ALuint NewFormat
= AL_FORMAT_QUAD32
;
473 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
474 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
476 if((size
%(1*OrigChans
)) != 0)
478 alSetError(AL_INVALID_VALUE
);
484 // Allocate extra padding samples
485 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ size
)*NewBytes
);
489 ConvertDataMULawRear(ALBuf
->data
, data
, size
);
491 ALBuf
->format
= NewFormat
;
492 ALBuf
->eOriginalFormat
= format
;
493 ALBuf
->size
= size
*NewBytes
;
494 ALBuf
->frequency
= freq
;
497 alSetError(AL_OUT_OF_MEMORY
);
501 alSetError(AL_INVALID_ENUM
);
507 // Buffer is in use, or data is a NULL pointer
508 alSetError(AL_INVALID_VALUE
);
513 // Invalid Buffer Name
514 alSetError(AL_INVALID_NAME
);
517 ProcessContext(Context
);
521 * alBufferSubDataEXT(ALuint buffer,ALenum format,ALvoid *data,ALsizei offset,ALsizei length)
523 * Fill buffer with audio data
525 ALvoid ALAPIENTRY
alBufferSubDataEXT(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei offset
,ALsizei length
)
530 Context
= GetContextSuspended();
533 if(alIsBuffer(buffer
) && buffer
!= 0)
535 ALBuf
= (ALbuffer
*)ALTHUNK_LOOKUPENTRY(buffer
);
537 if(Context
->SampleSource
)
541 if(Context
->SampleSource
->state
== MAPPED
)
543 alSetError(AL_INVALID_OPERATION
);
544 ProcessContext(Context
);
548 offset
= (ALuint
)data
;
549 data
= Context
->SampleSource
->data
+ offset
;
552 if(ALBuf
->data
== NULL
)
554 // buffer does not have any data
555 alSetError(AL_INVALID_NAME
);
557 else if(length
< 0 || offset
< 0 || (length
> 0 && data
== NULL
))
559 // data is NULL or offset/length is negative
560 alSetError(AL_INVALID_VALUE
);
566 case AL_FORMAT_REAR8
:
567 case AL_FORMAT_REAR16
:
568 case AL_FORMAT_REAR32
: {
569 ALuint OrigBytes
= ((format
==AL_FORMAT_REAR8
) ? 1 :
570 ((format
==AL_FORMAT_REAR16
) ? 2 :
572 ALuint NewBytes
= aluBytesFromFormat(ALBuf
->format
);
574 if(ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR8
&&
575 ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR16
&&
576 ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR32
)
578 alSetError(AL_INVALID_ENUM
);
582 if(ALBuf
->size
/4/NewBytes
< (ALuint
)offset
+length
)
584 alSetError(AL_INVALID_VALUE
);
588 ConvertDataRear(&ALBuf
->data
[offset
*4], data
, OrigBytes
, length
*2);
591 case AL_FORMAT_MONO_IMA4
:
592 case AL_FORMAT_STEREO_IMA4
: {
593 int Channels
= aluChannelsFromFormat(ALBuf
->format
);
594 ALuint Bytes
= aluBytesFromFormat(ALBuf
->format
);
596 if(ALBuf
->eOriginalFormat
!= format
)
598 alSetError(AL_INVALID_ENUM
);
602 if((offset
%65) != 0 || (length
%65) != 0 ||
603 ALBuf
->size
/Channels
/Bytes
< (ALuint
)offset
+length
)
605 alSetError(AL_INVALID_VALUE
);
609 ConvertDataIMA4(&ALBuf
->data
[offset
*Channels
], data
, Channels
, length
/65*Channels
);
612 case AL_FORMAT_MONO_MULAW
:
613 case AL_FORMAT_STEREO_MULAW
:
614 case AL_FORMAT_QUAD_MULAW
:
615 case AL_FORMAT_REAR_MULAW
:
616 case AL_FORMAT_51CHN_MULAW
:
617 case AL_FORMAT_61CHN_MULAW
:
618 case AL_FORMAT_71CHN_MULAW
: {
619 int Channels
= aluChannelsFromFormat(ALBuf
->format
);
620 ALuint Bytes
= aluBytesFromFormat(ALBuf
->format
);
622 if(ALBuf
->eOriginalFormat
!= format
)
624 alSetError(AL_INVALID_ENUM
);
628 if(ALBuf
->size
/Channels
/Bytes
< (ALuint
)offset
+length
)
630 alSetError(AL_INVALID_VALUE
);
634 if(ALBuf
->eOriginalFormat
== AL_FORMAT_REAR_MULAW
)
635 ConvertDataMULawRear(&ALBuf
->data
[offset
*Channels
], data
, length
*2);
637 ConvertDataMULaw(&ALBuf
->data
[offset
*Channels
], data
, length
*Channels
);
641 ALuint Channels
= aluChannelsFromFormat(format
);
642 ALuint Bytes
= aluBytesFromFormat(format
);
643 ALuint NewBytes
= aluBytesFromFormat(ALBuf
->format
);
645 if(Channels
!= aluChannelsFromFormat(ALBuf
->format
))
647 alSetError(AL_INVALID_ENUM
);
651 if(ALBuf
->size
/Channels
/NewBytes
< (ALuint
)offset
+length
)
653 alSetError(AL_INVALID_VALUE
);
657 ConvertData(&ALBuf
->data
[offset
*Channels
], data
, Bytes
, length
*Channels
);
664 // Invalid Buffer Name
665 alSetError(AL_INVALID_NAME
);
668 ProcessContext(Context
);
672 ALAPI
void ALAPIENTRY
alBufferf(ALuint buffer
, ALenum eParam
, ALfloat flValue
)
674 ALCcontext
*pContext
;
678 pContext
= GetContextSuspended();
679 if(!pContext
) return;
681 if (alIsBuffer(buffer
) && (buffer
!= 0))
686 alSetError(AL_INVALID_ENUM
);
692 alSetError(AL_INVALID_NAME
);
695 ProcessContext(pContext
);
699 ALAPI
void ALAPIENTRY
alBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat flValue1
, ALfloat flValue2
, ALfloat flValue3
)
701 ALCcontext
*pContext
;
707 pContext
= GetContextSuspended();
708 if(!pContext
) return;
710 if (alIsBuffer(buffer
) && (buffer
!= 0))
715 alSetError(AL_INVALID_ENUM
);
721 alSetError(AL_INVALID_NAME
);
724 ProcessContext(pContext
);
728 ALAPI
void ALAPIENTRY
alBufferfv(ALuint buffer
, ALenum eParam
, const ALfloat
* flValues
)
730 ALCcontext
*pContext
;
734 pContext
= GetContextSuspended();
735 if(!pContext
) return;
737 if (alIsBuffer(buffer
) && (buffer
!= 0))
742 alSetError(AL_INVALID_ENUM
);
748 alSetError(AL_INVALID_NAME
);
751 ProcessContext(pContext
);
755 ALAPI
void ALAPIENTRY
alBufferi(ALuint buffer
, ALenum eParam
, ALint lValue
)
757 ALCcontext
*pContext
;
761 pContext
= GetContextSuspended();
762 if(!pContext
) return;
764 if (alIsBuffer(buffer
) && (buffer
!= 0))
769 alSetError(AL_INVALID_ENUM
);
775 alSetError(AL_INVALID_NAME
);
778 ProcessContext(pContext
);
782 ALAPI
void ALAPIENTRY
alBuffer3i( ALuint buffer
, ALenum eParam
, ALint lValue1
, ALint lValue2
, ALint lValue3
)
784 ALCcontext
*pContext
;
790 pContext
= GetContextSuspended();
791 if(!pContext
) return;
793 if (alIsBuffer(buffer
) && (buffer
!= 0))
798 alSetError(AL_INVALID_ENUM
);
804 alSetError(AL_INVALID_NAME
);
807 ProcessContext(pContext
);
811 ALAPI
void ALAPIENTRY
alBufferiv(ALuint buffer
, ALenum eParam
, const ALint
* plValues
)
813 ALCcontext
*pContext
;
817 pContext
= GetContextSuspended();
818 if(!pContext
) return;
820 if (alIsBuffer(buffer
) && (buffer
!= 0))
825 alSetError(AL_INVALID_ENUM
);
831 alSetError(AL_INVALID_NAME
);
834 ProcessContext(pContext
);
838 ALAPI ALvoid ALAPIENTRY
alGetBufferf(ALuint buffer
, ALenum eParam
, ALfloat
*pflValue
)
840 ALCcontext
*pContext
;
842 pContext
= GetContextSuspended();
843 if(!pContext
) return;
847 if (alIsBuffer(buffer
) && (buffer
!= 0))
852 alSetError(AL_INVALID_ENUM
);
858 alSetError(AL_INVALID_NAME
);
863 alSetError(AL_INVALID_VALUE
);
866 ProcessContext(pContext
);
870 ALAPI
void ALAPIENTRY
alGetBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat
* pflValue1
, ALfloat
* pflValue2
, ALfloat
* pflValue3
)
872 ALCcontext
*pContext
;
874 pContext
= GetContextSuspended();
875 if(!pContext
) return;
877 if ((pflValue1
) && (pflValue2
) && (pflValue3
))
879 if (alIsBuffer(buffer
) && (buffer
!= 0))
884 alSetError(AL_INVALID_ENUM
);
890 alSetError(AL_INVALID_NAME
);
895 alSetError(AL_INVALID_VALUE
);
898 ProcessContext(pContext
);
902 ALAPI
void ALAPIENTRY
alGetBufferfv(ALuint buffer
, ALenum eParam
, ALfloat
* pflValues
)
904 ALCcontext
*pContext
;
906 pContext
= GetContextSuspended();
907 if(!pContext
) return;
911 if (alIsBuffer(buffer
) && (buffer
!= 0))
916 alSetError(AL_INVALID_ENUM
);
922 alSetError(AL_INVALID_NAME
);
927 alSetError(AL_INVALID_VALUE
);
930 ProcessContext(pContext
);
934 ALAPI ALvoid ALAPIENTRY
alGetBufferi(ALuint buffer
, ALenum eParam
, ALint
*plValue
)
936 ALCcontext
*pContext
;
939 pContext
= GetContextSuspended();
940 if(!pContext
) return;
944 if (alIsBuffer(buffer
) && (buffer
!= 0))
946 pBuffer
= ((ALbuffer
*)ALTHUNK_LOOKUPENTRY(buffer
));
951 *plValue
= pBuffer
->frequency
;
955 *plValue
= aluBytesFromFormat(pBuffer
->format
) * 8;
959 *plValue
= aluChannelsFromFormat(pBuffer
->format
);
963 *plValue
= pBuffer
->size
;
967 alSetError(AL_INVALID_ENUM
);
973 alSetError(AL_INVALID_NAME
);
978 alSetError(AL_INVALID_VALUE
);
981 ProcessContext(pContext
);
985 ALAPI
void ALAPIENTRY
alGetBuffer3i(ALuint buffer
, ALenum eParam
, ALint
* plValue1
, ALint
* plValue2
, ALint
* plValue3
)
987 ALCcontext
*pContext
;
989 pContext
= GetContextSuspended();
990 if(!pContext
) return;
992 if ((plValue1
) && (plValue2
) && (plValue3
))
994 if (alIsBuffer(buffer
) && (buffer
!= 0))
999 alSetError(AL_INVALID_ENUM
);
1005 alSetError(AL_INVALID_NAME
);
1010 alSetError(AL_INVALID_VALUE
);
1013 ProcessContext(pContext
);
1017 ALAPI
void ALAPIENTRY
alGetBufferiv(ALuint buffer
, ALenum eParam
, ALint
* plValues
)
1019 ALCcontext
*pContext
;
1021 pContext
= GetContextSuspended();
1022 if(!pContext
) return;
1026 if (alIsBuffer(buffer
) && (buffer
!= 0))
1034 alGetBufferi(buffer
, eParam
, plValues
);
1038 alSetError(AL_INVALID_ENUM
);
1044 alSetError(AL_INVALID_NAME
);
1049 alSetError(AL_INVALID_VALUE
);
1052 ProcessContext(pContext
);
1058 * Loads the specified data into the buffer, using the specified formats.
1059 * Currently, the new format must be 32-bit float, and must have the same
1060 * channel configuration as the original format. This does NOT handle
1061 * compressed formats (eg. IMA4).
1063 static void LoadData(ALbuffer
*ALBuf
, const ALubyte
*data
, ALsizei size
, ALuint freq
, ALenum OrigFormat
, ALenum NewFormat
)
1065 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
1066 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
1067 ALuint OrigBytes
= aluBytesFromFormat(OrigFormat
);
1068 ALuint OrigChannels
= aluChannelsFromFormat(OrigFormat
);
1071 assert(NewBytes
== 4);
1072 assert(NewChannels
== OrigChannels
);
1074 if ((size
%(OrigBytes
*OrigChannels
)) != 0)
1076 alSetError(AL_INVALID_VALUE
);
1080 // Samples are converted here
1082 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ size
) * NewBytes
);
1086 ConvertData(ALBuf
->data
, data
, OrigBytes
, size
);
1088 ALBuf
->format
= NewFormat
;
1089 ALBuf
->eOriginalFormat
= OrigFormat
;
1090 ALBuf
->size
= size
*NewBytes
;
1091 ALBuf
->frequency
= freq
;
1094 alSetError(AL_OUT_OF_MEMORY
);
1097 static void ConvertData(ALfloat
*dst
, const ALvoid
*src
, ALint origBytes
, ALsizei len
)
1104 for(i
= 0;i
< len
;i
++)
1106 smp
= ((ALubyte
*)src
)[i
];
1107 dst
[i
] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1112 for(i
= 0;i
< len
;i
++)
1114 smp
= ((ALshort
*)src
)[i
];
1115 dst
[i
] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1120 for(i
= 0;i
< len
;i
++)
1121 dst
[i
] = ((ALfloat
*)src
)[i
];
1125 for(i
= 0;i
< len
;i
++)
1126 dst
[i
] = ((ALdouble
*)src
)[i
];
1134 static void ConvertDataRear(ALfloat
*dst
, const ALvoid
*src
, ALint origBytes
, ALsizei len
)
1141 for(i
= 0;i
< len
;i
+=4)
1145 smp
= ((ALubyte
*)src
)[i
/2+0];
1146 dst
[i
+2] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1147 smp
= ((ALubyte
*)src
)[i
/2+1];
1148 dst
[i
+3] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1153 for(i
= 0;i
< len
;i
+=4)
1157 smp
= ((ALshort
*)src
)[i
/2+0];
1158 dst
[i
+2] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1159 smp
= ((ALshort
*)src
)[i
/2+1];
1160 dst
[i
+3] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1165 for(i
= 0;i
< len
;i
+=4)
1169 dst
[i
+2] = ((ALfloat
*)src
)[i
/2+0];
1170 dst
[i
+3] = ((ALfloat
*)src
)[i
/2+1];
1179 static void ConvertDataIMA4(ALfloat
*dst
, const ALvoid
*src
, ALint origChans
, ALsizei len
)
1181 const ALuint
*IMAData
;
1182 ALint Sample
[2],Index
[2];
1186 assert(origChans
<= 2);
1189 for(i
= 0;i
< len
/origChans
;i
++)
1191 for(c
= 0;c
< origChans
;c
++)
1193 Sample
[c
] = ((ALshort
*)IMAData
)[0];
1194 Index
[c
] = ((ALshort
*)IMAData
)[1];
1196 Index
[c
] = ((Index
[c
]<0) ? 0 : Index
[c
]);
1197 Index
[c
] = ((Index
[c
]>88) ? 88 : Index
[c
]);
1199 dst
[i
*65*origChans
+ c
] = ((Sample
[c
] < 0) ? (Sample
[c
]/32768.0f
) : (Sample
[c
]/32767.0f
));
1204 for(j
= 1;j
< 65;j
+= 8)
1206 for(c
= 0;c
< origChans
;c
++)
1207 IMACode
[c
] = *(IMAData
++);
1209 for(k
= 0;k
< 8;k
++)
1211 for(c
= 0;c
< origChans
;c
++)
1213 Sample
[c
] += ((g_IMAStep_size
[Index
[c
]]*g_IMACodeword_4
[IMACode
[c
]&15])/8);
1214 Index
[c
] += g_IMAIndex_adjust_4
[IMACode
[c
]&15];
1216 if(Sample
[c
] < -32768) Sample
[c
] = -32768;
1217 else if(Sample
[c
] > 32767) Sample
[c
] = 32767;
1219 if(Index
[c
]<0) Index
[c
] = 0;
1220 else if(Index
[c
]>88) Index
[c
] = 88;
1222 dst
[(i
*65+j
+k
)*origChans
+ c
] = ((Sample
[c
] < 0) ? (Sample
[c
]/32768.0f
) : (Sample
[c
]/32767.0f
));
1230 static void ConvertDataMULaw(ALfloat
*dst
, const ALvoid
*src
, ALsizei len
)
1234 for(i
= 0;i
< len
;i
++)
1236 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
]];
1237 dst
[i
] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1241 static void ConvertDataMULawRear(ALfloat
*dst
, const ALvoid
*src
, ALsizei len
)
1245 for(i
= 0;i
< len
;i
+=4)
1249 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
/2+0]];
1250 dst
[i
+2] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1251 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
/2+1]];
1252 dst
[i
+3] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1257 * ReleaseALBuffers()
1259 * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
1261 ALvoid
ReleaseALBuffers(ALCdevice
*device
)
1264 ALbuffer
*ALBufferTemp
;
1266 ALBuffer
= device
->BufferList
;
1269 // Release sample data
1270 free(ALBuffer
->data
);
1272 // Release Buffer structure
1273 ALBufferTemp
= ALBuffer
;
1274 ALBuffer
= ALBuffer
->next
;
1275 memset(ALBufferTemp
, 0, sizeof(ALbuffer
));
1278 device
->BufferList
= NULL
;
1279 device
->BufferCount
= 0;