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
= (ALuint
)data
;
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 ALuint 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 assert(aluBytesFromFormat(NewFormat
) == 2);
326 if((size
%(OrigBytes
*2)) != 0)
328 alSetError(Context
, AL_INVALID_VALUE
);
335 // Samples are converted here
336 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ size
) * NewBytes
);
340 ConvertDataRear(ALBuf
->data
, data
, OrigBytes
, size
);
342 ALBuf
->format
= NewFormat
;
343 ALBuf
->eOriginalFormat
= format
;
344 ALBuf
->size
= size
*NewBytes
;
345 ALBuf
->frequency
= freq
;
348 alSetError(Context
, AL_OUT_OF_MEMORY
);
351 case AL_FORMAT_QUAD8_LOKI
:
352 case AL_FORMAT_QUAD16_LOKI
:
353 case AL_FORMAT_QUAD8
:
354 case AL_FORMAT_QUAD16
:
355 case AL_FORMAT_QUAD32
:
356 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_QUAD32
);
357 if(err
!= AL_NO_ERROR
)
358 alSetError(Context
, err
);
361 case AL_FORMAT_51CHN8
:
362 case AL_FORMAT_51CHN16
:
363 case AL_FORMAT_51CHN32
:
364 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_51CHN32
);
365 if(err
!= AL_NO_ERROR
)
366 alSetError(Context
, err
);
369 case AL_FORMAT_61CHN8
:
370 case AL_FORMAT_61CHN16
:
371 case AL_FORMAT_61CHN32
:
372 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_61CHN32
);
373 if(err
!= AL_NO_ERROR
)
374 alSetError(Context
, err
);
377 case AL_FORMAT_71CHN8
:
378 case AL_FORMAT_71CHN16
:
379 case AL_FORMAT_71CHN32
:
380 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_71CHN32
);
381 if(err
!= AL_NO_ERROR
)
382 alSetError(Context
, err
);
385 case AL_FORMAT_MONO_IMA4
:
386 case AL_FORMAT_STEREO_IMA4
: {
387 int OrigChans
= ((format
==AL_FORMAT_MONO_IMA4
) ? 1 : 2);
388 ALuint NewFormat
= ((OrigChans
==1) ? AL_FORMAT_MONO_FLOAT32
:
389 AL_FORMAT_STEREO_FLOAT32
);
390 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
392 // Here is where things vary:
393 // nVidia and Apple use 64+1 samples per channel per block => block_size=36*chans bytes
394 // Most PC sound software uses 2040+1 samples per channel per block -> block_size=1024*chans bytes
395 if((size
%(36*OrigChans
)) != 0)
397 alSetError(Context
, AL_INVALID_VALUE
);
404 // Allocate extra padding samples
405 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*OrigChans
+ size
)*NewBytes
);
409 ConvertDataIMA4(ALBuf
->data
, data
, OrigChans
, size
/65);
411 ALBuf
->format
= NewFormat
;
412 ALBuf
->eOriginalFormat
= format
;
413 ALBuf
->size
= size
*NewBytes
;
414 ALBuf
->frequency
= freq
;
417 alSetError(Context
, AL_OUT_OF_MEMORY
);
420 case AL_FORMAT_MONO_MULAW
:
421 case AL_FORMAT_STEREO_MULAW
:
422 case AL_FORMAT_QUAD_MULAW
:
423 case AL_FORMAT_51CHN_MULAW
:
424 case AL_FORMAT_61CHN_MULAW
:
425 case AL_FORMAT_71CHN_MULAW
: {
426 int Channels
= ((format
==AL_FORMAT_MONO_MULAW
) ? 1 :
427 ((format
==AL_FORMAT_STEREO_MULAW
) ? 2 :
428 ((format
==AL_FORMAT_QUAD_MULAW
) ? 4 :
429 ((format
==AL_FORMAT_51CHN_MULAW
) ? 6 :
430 ((format
==AL_FORMAT_61CHN_MULAW
) ? 7 : 8)))));
431 ALuint NewFormat
= ((Channels
==1) ? AL_FORMAT_MONO_FLOAT32
:
432 ((Channels
==2) ? AL_FORMAT_STEREO_FLOAT32
:
433 ((Channels
==4) ? AL_FORMAT_QUAD32
:
434 ((Channels
==6) ? AL_FORMAT_51CHN32
:
435 ((Channels
==7) ? AL_FORMAT_61CHN32
:
436 AL_FORMAT_71CHN32
)))));
437 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
439 if((size
%(1*Channels
)) != 0)
441 alSetError(Context
, AL_INVALID_VALUE
);
445 // Allocate extra padding samples
446 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*Channels
+ size
)*NewBytes
);
450 ConvertDataMULaw(ALBuf
->data
, data
, size
);
452 ALBuf
->format
= NewFormat
;
453 ALBuf
->eOriginalFormat
= format
;
454 ALBuf
->size
= size
*NewBytes
;
455 ALBuf
->frequency
= freq
;
458 alSetError(Context
, AL_OUT_OF_MEMORY
);
461 case AL_FORMAT_REAR_MULAW
: {
463 ALuint NewFormat
= AL_FORMAT_QUAD32
;
464 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
465 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
467 if((size
%(1*OrigChans
)) != 0)
469 alSetError(Context
, AL_INVALID_VALUE
);
475 // Allocate extra padding samples
476 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ size
)*NewBytes
);
480 ConvertDataMULawRear(ALBuf
->data
, data
, size
);
482 ALBuf
->format
= NewFormat
;
483 ALBuf
->eOriginalFormat
= format
;
484 ALBuf
->size
= size
*NewBytes
;
485 ALBuf
->frequency
= freq
;
488 alSetError(Context
, AL_OUT_OF_MEMORY
);
492 alSetError(Context
, AL_INVALID_ENUM
);
498 // Buffer is in use, or data is a NULL pointer
499 alSetError(Context
, AL_INVALID_VALUE
);
504 // Invalid Buffer Name
505 alSetError(Context
, AL_INVALID_NAME
);
508 ProcessContext(Context
);
512 * alBufferSubDataEXT(ALuint buffer,ALenum format,ALvoid *data,ALsizei offset,ALsizei length)
514 * Fill buffer with audio data
516 ALvoid ALAPIENTRY
alBufferSubDataEXT(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei offset
,ALsizei length
)
522 Context
= GetContextSuspended();
525 device
= Context
->Device
;
526 if((ALBuf
=VerifyBuffer(device
->BufferList
, buffer
)) != NULL
)
528 if(Context
->SampleSource
)
532 if(Context
->SampleSource
->state
== MAPPED
)
534 alSetError(Context
, AL_INVALID_OPERATION
);
535 ProcessContext(Context
);
539 offset
= (ALuint
)data
;
540 data
= Context
->SampleSource
->data
+ offset
;
543 if(ALBuf
->data
== NULL
)
545 // buffer does not have any data
546 alSetError(Context
, AL_INVALID_NAME
);
548 else if(length
< 0 || offset
< 0 || (length
> 0 && data
== NULL
))
550 // data is NULL or offset/length is negative
551 alSetError(Context
, AL_INVALID_VALUE
);
557 case AL_FORMAT_REAR8
:
558 case AL_FORMAT_REAR16
:
559 case AL_FORMAT_REAR32
: {
560 ALuint OrigBytes
= ((format
==AL_FORMAT_REAR8
) ? 1 :
561 ((format
==AL_FORMAT_REAR16
) ? 2 :
563 ALuint NewBytes
= aluBytesFromFormat(ALBuf
->format
);
565 if(ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR8
&&
566 ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR16
&&
567 ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR32
)
569 alSetError(Context
, AL_INVALID_ENUM
);
573 if(ALBuf
->size
/4/NewBytes
< (ALuint
)offset
+length
)
575 alSetError(Context
, AL_INVALID_VALUE
);
579 ConvertDataRear(&ALBuf
->data
[offset
*4], data
, OrigBytes
, length
*2);
582 case AL_FORMAT_MONO_IMA4
:
583 case AL_FORMAT_STEREO_IMA4
: {
584 int Channels
= aluChannelsFromFormat(ALBuf
->format
);
585 ALuint Bytes
= aluBytesFromFormat(ALBuf
->format
);
587 if(ALBuf
->eOriginalFormat
!= format
)
589 alSetError(Context
, AL_INVALID_ENUM
);
593 if((offset
%65) != 0 || (length
%65) != 0 ||
594 ALBuf
->size
/Channels
/Bytes
< (ALuint
)offset
+length
)
596 alSetError(Context
, AL_INVALID_VALUE
);
600 ConvertDataIMA4(&ALBuf
->data
[offset
*Channels
], data
, Channels
, length
/65*Channels
);
603 case AL_FORMAT_MONO_MULAW
:
604 case AL_FORMAT_STEREO_MULAW
:
605 case AL_FORMAT_QUAD_MULAW
:
606 case AL_FORMAT_REAR_MULAW
:
607 case AL_FORMAT_51CHN_MULAW
:
608 case AL_FORMAT_61CHN_MULAW
:
609 case AL_FORMAT_71CHN_MULAW
: {
610 int Channels
= aluChannelsFromFormat(ALBuf
->format
);
611 ALuint Bytes
= aluBytesFromFormat(ALBuf
->format
);
613 if(ALBuf
->eOriginalFormat
!= format
)
615 alSetError(Context
, AL_INVALID_ENUM
);
619 if(ALBuf
->size
/Channels
/Bytes
< (ALuint
)offset
+length
)
621 alSetError(Context
, AL_INVALID_VALUE
);
625 if(ALBuf
->eOriginalFormat
== AL_FORMAT_REAR_MULAW
)
626 ConvertDataMULawRear(&ALBuf
->data
[offset
*Channels
], data
, length
*2);
628 ConvertDataMULaw(&ALBuf
->data
[offset
*Channels
], data
, length
*Channels
);
632 ALuint Channels
= aluChannelsFromFormat(format
);
633 ALuint Bytes
= aluBytesFromFormat(format
);
634 ALuint NewBytes
= aluBytesFromFormat(ALBuf
->format
);
636 if(Channels
!= aluChannelsFromFormat(ALBuf
->format
))
638 alSetError(Context
, AL_INVALID_ENUM
);
642 if(ALBuf
->size
/Channels
/NewBytes
< (ALuint
)offset
+length
)
644 alSetError(Context
, AL_INVALID_VALUE
);
648 ConvertData(&ALBuf
->data
[offset
*Channels
], data
, Bytes
, length
*Channels
);
655 // Invalid Buffer Name
656 alSetError(Context
, AL_INVALID_NAME
);
659 ProcessContext(Context
);
663 ALAPI
void ALAPIENTRY
alBufferf(ALuint buffer
, ALenum eParam
, ALfloat flValue
)
665 ALCcontext
*pContext
;
670 pContext
= GetContextSuspended();
671 if(!pContext
) return;
673 device
= pContext
->Device
;
674 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
679 alSetError(pContext
, AL_INVALID_ENUM
);
685 alSetError(pContext
, AL_INVALID_NAME
);
688 ProcessContext(pContext
);
692 ALAPI
void ALAPIENTRY
alBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat flValue1
, ALfloat flValue2
, ALfloat flValue3
)
694 ALCcontext
*pContext
;
701 pContext
= GetContextSuspended();
702 if(!pContext
) return;
704 device
= pContext
->Device
;
705 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
710 alSetError(pContext
, AL_INVALID_ENUM
);
716 alSetError(pContext
, AL_INVALID_NAME
);
719 ProcessContext(pContext
);
723 ALAPI
void ALAPIENTRY
alBufferfv(ALuint buffer
, ALenum eParam
, const ALfloat
* flValues
)
725 ALCcontext
*pContext
;
730 pContext
= GetContextSuspended();
731 if(!pContext
) return;
733 device
= pContext
->Device
;
734 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
739 alSetError(pContext
, AL_INVALID_ENUM
);
745 alSetError(pContext
, AL_INVALID_NAME
);
748 ProcessContext(pContext
);
752 ALAPI
void ALAPIENTRY
alBufferi(ALuint buffer
, ALenum eParam
, ALint lValue
)
754 ALCcontext
*pContext
;
759 pContext
= GetContextSuspended();
760 if(!pContext
) return;
762 device
= pContext
->Device
;
763 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
768 alSetError(pContext
, AL_INVALID_ENUM
);
774 alSetError(pContext
, AL_INVALID_NAME
);
777 ProcessContext(pContext
);
781 ALAPI
void ALAPIENTRY
alBuffer3i( ALuint buffer
, ALenum eParam
, ALint lValue1
, ALint lValue2
, ALint lValue3
)
783 ALCcontext
*pContext
;
790 pContext
= GetContextSuspended();
791 if(!pContext
) return;
793 device
= pContext
->Device
;
794 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
799 alSetError(pContext
, AL_INVALID_ENUM
);
805 alSetError(pContext
, AL_INVALID_NAME
);
808 ProcessContext(pContext
);
812 ALAPI
void ALAPIENTRY
alBufferiv(ALuint buffer
, ALenum eParam
, const ALint
* plValues
)
814 ALCcontext
*pContext
;
819 pContext
= GetContextSuspended();
820 if(!pContext
) return;
822 device
= pContext
->Device
;
823 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
828 alSetError(pContext
, AL_INVALID_ENUM
);
834 alSetError(pContext
, AL_INVALID_NAME
);
837 ProcessContext(pContext
);
841 ALAPI ALvoid ALAPIENTRY
alGetBufferf(ALuint buffer
, ALenum eParam
, ALfloat
*pflValue
)
843 ALCcontext
*pContext
;
846 pContext
= GetContextSuspended();
847 if(!pContext
) return;
851 device
= pContext
->Device
;
852 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
857 alSetError(pContext
, AL_INVALID_ENUM
);
863 alSetError(pContext
, AL_INVALID_NAME
);
868 alSetError(pContext
, AL_INVALID_VALUE
);
871 ProcessContext(pContext
);
875 ALAPI
void ALAPIENTRY
alGetBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat
* pflValue1
, ALfloat
* pflValue2
, ALfloat
* pflValue3
)
877 ALCcontext
*pContext
;
880 pContext
= GetContextSuspended();
881 if(!pContext
) return;
883 if ((pflValue1
) && (pflValue2
) && (pflValue3
))
885 device
= pContext
->Device
;
886 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
891 alSetError(pContext
, AL_INVALID_ENUM
);
897 alSetError(pContext
, AL_INVALID_NAME
);
902 alSetError(pContext
, AL_INVALID_VALUE
);
905 ProcessContext(pContext
);
909 ALAPI
void ALAPIENTRY
alGetBufferfv(ALuint buffer
, ALenum eParam
, ALfloat
* pflValues
)
911 ALCcontext
*pContext
;
914 pContext
= GetContextSuspended();
915 if(!pContext
) return;
919 device
= pContext
->Device
;
920 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
925 alSetError(pContext
, AL_INVALID_ENUM
);
931 alSetError(pContext
, AL_INVALID_NAME
);
936 alSetError(pContext
, AL_INVALID_VALUE
);
939 ProcessContext(pContext
);
943 ALAPI ALvoid ALAPIENTRY
alGetBufferi(ALuint buffer
, ALenum eParam
, ALint
*plValue
)
945 ALCcontext
*pContext
;
949 pContext
= GetContextSuspended();
950 if(!pContext
) return;
954 device
= pContext
->Device
;
955 if((pBuffer
=VerifyBuffer(device
->BufferList
, buffer
)) != NULL
)
960 *plValue
= pBuffer
->frequency
;
964 *plValue
= aluBytesFromFormat(pBuffer
->format
) * 8;
968 *plValue
= aluChannelsFromFormat(pBuffer
->format
);
972 *plValue
= pBuffer
->size
;
976 alSetError(pContext
, AL_INVALID_ENUM
);
982 alSetError(pContext
, AL_INVALID_NAME
);
987 alSetError(pContext
, AL_INVALID_VALUE
);
990 ProcessContext(pContext
);
994 ALAPI
void ALAPIENTRY
alGetBuffer3i(ALuint buffer
, ALenum eParam
, ALint
* plValue1
, ALint
* plValue2
, ALint
* plValue3
)
996 ALCcontext
*pContext
;
999 pContext
= GetContextSuspended();
1000 if(!pContext
) return;
1002 if ((plValue1
) && (plValue2
) && (plValue3
))
1004 device
= pContext
->Device
;
1005 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
1010 alSetError(pContext
, AL_INVALID_ENUM
);
1016 alSetError(pContext
, AL_INVALID_NAME
);
1021 alSetError(pContext
, AL_INVALID_VALUE
);
1024 ProcessContext(pContext
);
1028 ALAPI
void ALAPIENTRY
alGetBufferiv(ALuint buffer
, ALenum eParam
, ALint
* plValues
)
1030 ALCcontext
*pContext
;
1033 pContext
= GetContextSuspended();
1034 if(!pContext
) return;
1038 device
= pContext
->Device
;
1039 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
1047 alGetBufferi(buffer
, eParam
, plValues
);
1051 alSetError(pContext
, AL_INVALID_ENUM
);
1057 alSetError(pContext
, AL_INVALID_NAME
);
1062 alSetError(pContext
, AL_INVALID_VALUE
);
1065 ProcessContext(pContext
);
1071 * Loads the specified data into the buffer, using the specified formats.
1072 * Currently, the new format must be 32-bit float, and must have the same
1073 * channel configuration as the original format. This does NOT handle
1074 * compressed formats (eg. IMA4).
1076 static ALenum
LoadData(ALbuffer
*ALBuf
, const ALubyte
*data
, ALsizei size
, ALuint freq
, ALenum OrigFormat
, ALenum NewFormat
)
1078 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
1079 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
1080 ALuint OrigBytes
= aluBytesFromFormat(OrigFormat
);
1081 ALuint OrigChannels
= aluChannelsFromFormat(OrigFormat
);
1084 assert(NewBytes
== 4);
1085 assert(NewChannels
== OrigChannels
);
1087 if ((size
%(OrigBytes
*OrigChannels
)) != 0)
1088 return AL_INVALID_VALUE
;
1090 // Samples are converted here
1092 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ size
) * NewBytes
);
1093 if(!temp
) return AL_OUT_OF_MEMORY
;
1095 ConvertData(ALBuf
->data
, data
, OrigBytes
, size
);
1097 ALBuf
->format
= NewFormat
;
1098 ALBuf
->eOriginalFormat
= OrigFormat
;
1099 ALBuf
->size
= size
*NewBytes
;
1100 ALBuf
->frequency
= freq
;
1105 static void ConvertData(ALfloat
*dst
, const ALvoid
*src
, ALint origBytes
, ALsizei len
)
1112 for(i
= 0;i
< len
;i
++)
1114 smp
= ((ALubyte
*)src
)[i
];
1115 dst
[i
] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1120 for(i
= 0;i
< len
;i
++)
1122 smp
= ((ALshort
*)src
)[i
];
1123 dst
[i
] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1128 for(i
= 0;i
< len
;i
++)
1129 dst
[i
] = ((ALfloat
*)src
)[i
];
1133 for(i
= 0;i
< len
;i
++)
1134 dst
[i
] = ((ALdouble
*)src
)[i
];
1142 static void ConvertDataRear(ALfloat
*dst
, const ALvoid
*src
, ALint origBytes
, ALsizei len
)
1149 for(i
= 0;i
< len
;i
+=4)
1153 smp
= ((ALubyte
*)src
)[i
/2+0];
1154 dst
[i
+2] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1155 smp
= ((ALubyte
*)src
)[i
/2+1];
1156 dst
[i
+3] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1161 for(i
= 0;i
< len
;i
+=4)
1165 smp
= ((ALshort
*)src
)[i
/2+0];
1166 dst
[i
+2] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1167 smp
= ((ALshort
*)src
)[i
/2+1];
1168 dst
[i
+3] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1173 for(i
= 0;i
< len
;i
+=4)
1177 dst
[i
+2] = ((ALfloat
*)src
)[i
/2+0];
1178 dst
[i
+3] = ((ALfloat
*)src
)[i
/2+1];
1187 static void ConvertDataIMA4(ALfloat
*dst
, const ALvoid
*src
, ALint origChans
, ALsizei len
)
1189 const ALuint
*IMAData
;
1190 ALint Sample
[2],Index
[2];
1194 assert(origChans
<= 2);
1197 for(i
= 0;i
< len
/origChans
;i
++)
1199 for(c
= 0;c
< origChans
;c
++)
1201 Sample
[c
] = ((ALshort
*)IMAData
)[0];
1202 Index
[c
] = ((ALshort
*)IMAData
)[1];
1204 Index
[c
] = ((Index
[c
]<0) ? 0 : Index
[c
]);
1205 Index
[c
] = ((Index
[c
]>88) ? 88 : Index
[c
]);
1207 dst
[i
*65*origChans
+ c
] = ((Sample
[c
] < 0) ? (Sample
[c
]/32768.0f
) : (Sample
[c
]/32767.0f
));
1212 for(j
= 1;j
< 65;j
+= 8)
1214 for(c
= 0;c
< origChans
;c
++)
1215 IMACode
[c
] = *(IMAData
++);
1217 for(k
= 0;k
< 8;k
++)
1219 for(c
= 0;c
< origChans
;c
++)
1221 Sample
[c
] += ((g_IMAStep_size
[Index
[c
]]*g_IMACodeword_4
[IMACode
[c
]&15])/8);
1222 Index
[c
] += g_IMAIndex_adjust_4
[IMACode
[c
]&15];
1224 if(Sample
[c
] < -32768) Sample
[c
] = -32768;
1225 else if(Sample
[c
] > 32767) Sample
[c
] = 32767;
1227 if(Index
[c
]<0) Index
[c
] = 0;
1228 else if(Index
[c
]>88) Index
[c
] = 88;
1230 dst
[(i
*65+j
+k
)*origChans
+ c
] = ((Sample
[c
] < 0) ? (Sample
[c
]/32768.0f
) : (Sample
[c
]/32767.0f
));
1238 static void ConvertDataMULaw(ALfloat
*dst
, const ALvoid
*src
, ALsizei len
)
1242 for(i
= 0;i
< len
;i
++)
1244 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
]];
1245 dst
[i
] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1249 static void ConvertDataMULawRear(ALfloat
*dst
, const ALvoid
*src
, ALsizei len
)
1253 for(i
= 0;i
< len
;i
+=4)
1257 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
/2+0]];
1258 dst
[i
+2] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1259 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
/2+1]];
1260 dst
[i
+3] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1265 * ReleaseALBuffers()
1267 * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
1269 ALvoid
ReleaseALBuffers(ALCdevice
*device
)
1272 ALbuffer
*ALBufferTemp
;
1274 ALBuffer
= device
->BufferList
;
1277 // Release sample data
1278 free(ALBuffer
->data
);
1280 // Release Buffer structure
1281 ALBufferTemp
= ALBuffer
;
1282 ALBuffer
= ALBuffer
->next
;
1283 memset(ALBufferTemp
, 0, sizeof(ALbuffer
));
1286 device
->BufferList
= NULL
;
1287 device
->BufferCount
= 0;