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 AL_API ALvoid AL_APIENTRY
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
)))
124 ALbuffer
**list
= &device
->BufferList
;
126 list
= &(*list
)->next
;
128 // Create all the new Buffers
132 *list
= calloc(1, sizeof(ALbuffer
));
137 ALbuffer
*temp
= end
->next
;
138 end
->next
= temp
->next
;
140 ALTHUNK_REMOVEENTRY(temp
->buffer
);
141 device
->BufferCount
--;
144 alSetError(Context
, AL_OUT_OF_MEMORY
);
148 puiBuffers
[i
] = (ALuint
)ALTHUNK_ADDENTRY(*list
);
149 (*list
)->buffer
= puiBuffers
[i
];
151 device
->BufferCount
++;
154 list
= &(*list
)->next
;
159 // Pointer does not point to enough memory to write Buffer names
160 alSetError(Context
, AL_INVALID_VALUE
);
164 ProcessContext(Context
);
168 * alDeleteBuffers(ALsizei n, ALuint *puiBuffers)
170 * Deletes the n AL Buffers pointed to by puiBuffers
172 AL_API ALvoid AL_APIENTRY
alDeleteBuffers(ALsizei n
, const ALuint
*puiBuffers
)
177 ALboolean bFailed
= AL_FALSE
;
179 Context
= GetContextSuspended();
182 // Check we are actually Deleting some Buffers
185 ALCdevice
*device
= Context
->Device
;
187 // Check that all the buffers are valid and can actually be deleted
188 for (i
= 0; i
< n
; i
++)
193 // Check for valid Buffer ID (can be NULL buffer)
194 if((ALBuf
=VerifyBuffer(device
->BufferList
, puiBuffers
[i
])) != NULL
)
196 if(ALBuf
->refcount
!= 0)
198 // Buffer still in use, cannot be deleted
199 alSetError(Context
, AL_INVALID_OPERATION
);
207 alSetError(Context
, AL_INVALID_NAME
);
213 // If all the Buffers were valid (and have Reference Counts of 0), then we can delete them
216 for (i
= 0; i
< n
; i
++)
218 if((ALBuf
=VerifyBuffer(device
->BufferList
, puiBuffers
[i
])) != NULL
)
220 ALbuffer
**list
= &device
->BufferList
;
222 while(*list
&& *list
!= ALBuf
)
223 list
= &(*list
)->next
;
226 *list
= (*list
)->next
;
228 // Release the memory used to store audio data
231 // Release buffer structure
232 ALTHUNK_REMOVEENTRY(puiBuffers
[i
]);
233 memset(ALBuf
, 0, sizeof(ALbuffer
));
234 device
->BufferCount
--;
241 alSetError(Context
, AL_INVALID_VALUE
);
243 ProcessContext(Context
);
247 * alIsBuffer(ALuint uiBuffer)
249 * Checks if ulBuffer is a valid Buffer Name
251 AL_API ALboolean AL_APIENTRY
alIsBuffer(ALuint uiBuffer
)
254 ALboolean result
= AL_TRUE
;
256 Context
= GetContextSuspended();
257 if(!Context
) return AL_FALSE
;
260 result
= (VerifyBuffer(Context
->Device
->BufferList
, uiBuffer
) ?
263 ProcessContext(Context
);
269 * alBufferData(ALuint buffer,ALenum format,ALvoid *data,ALsizei size,ALsizei freq)
271 * Fill buffer with audio data
273 AL_API ALvoid AL_APIENTRY
alBufferData(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei size
,ALsizei freq
)
281 Context
= GetContextSuspended();
284 device
= Context
->Device
;
285 if((ALBuf
=VerifyBuffer(device
->BufferList
, buffer
)) != NULL
)
287 if(Context
->SampleSource
)
291 if(Context
->SampleSource
->state
== MAPPED
)
293 alSetError(Context
, AL_INVALID_OPERATION
);
294 ProcessContext(Context
);
298 offset
= (const ALubyte
*)data
- (ALubyte
*)NULL
;
299 data
= Context
->SampleSource
->data
+ offset
;
302 if ((ALBuf
->refcount
==0)&&(data
))
306 case AL_FORMAT_MONO8
:
307 case AL_FORMAT_MONO16
:
308 case AL_FORMAT_MONO_FLOAT32
:
309 case AL_FORMAT_MONO_DOUBLE_EXT
:
310 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_MONO_FLOAT32
);
311 if(err
!= AL_NO_ERROR
)
312 alSetError(Context
, err
);
315 case AL_FORMAT_STEREO8
:
316 case AL_FORMAT_STEREO16
:
317 case AL_FORMAT_STEREO_FLOAT32
:
318 case AL_FORMAT_STEREO_DOUBLE_EXT
:
319 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_STEREO_FLOAT32
);
320 if(err
!= AL_NO_ERROR
)
321 alSetError(Context
, err
);
324 case AL_FORMAT_REAR8
:
325 case AL_FORMAT_REAR16
:
326 case AL_FORMAT_REAR32
: {
327 ALenum NewFormat
= AL_FORMAT_QUAD32
;
328 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
329 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
330 ALuint OrigBytes
= ((format
==AL_FORMAT_REAR8
) ? 1 :
331 ((format
==AL_FORMAT_REAR16
) ? 2 :
334 if((size
%(OrigBytes
*2)) != 0)
336 alSetError(Context
, AL_INVALID_VALUE
);
343 // Samples are converted here
344 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ size
) * NewBytes
);
348 ConvertDataRear(ALBuf
->data
, data
, OrigBytes
, size
);
350 ALBuf
->format
= NewFormat
;
351 ALBuf
->eOriginalFormat
= format
;
352 ALBuf
->size
= size
*NewBytes
;
353 ALBuf
->frequency
= freq
;
356 alSetError(Context
, AL_OUT_OF_MEMORY
);
359 case AL_FORMAT_QUAD8_LOKI
:
360 case AL_FORMAT_QUAD16_LOKI
:
361 case AL_FORMAT_QUAD8
:
362 case AL_FORMAT_QUAD16
:
363 case AL_FORMAT_QUAD32
:
364 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_QUAD32
);
365 if(err
!= AL_NO_ERROR
)
366 alSetError(Context
, err
);
369 case AL_FORMAT_51CHN8
:
370 case AL_FORMAT_51CHN16
:
371 case AL_FORMAT_51CHN32
:
372 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_51CHN32
);
373 if(err
!= AL_NO_ERROR
)
374 alSetError(Context
, err
);
377 case AL_FORMAT_61CHN8
:
378 case AL_FORMAT_61CHN16
:
379 case AL_FORMAT_61CHN32
:
380 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_61CHN32
);
381 if(err
!= AL_NO_ERROR
)
382 alSetError(Context
, err
);
385 case AL_FORMAT_71CHN8
:
386 case AL_FORMAT_71CHN16
:
387 case AL_FORMAT_71CHN32
:
388 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_71CHN32
);
389 if(err
!= AL_NO_ERROR
)
390 alSetError(Context
, err
);
393 case AL_FORMAT_MONO_IMA4
:
394 case AL_FORMAT_STEREO_IMA4
: {
395 int OrigChans
= ((format
==AL_FORMAT_MONO_IMA4
) ? 1 : 2);
396 ALenum NewFormat
= ((OrigChans
==1) ? AL_FORMAT_MONO_FLOAT32
:
397 AL_FORMAT_STEREO_FLOAT32
);
398 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
400 // Here is where things vary:
401 // nVidia and Apple use 64+1 samples per channel per block => block_size=36*chans bytes
402 // Most PC sound software uses 2040+1 samples per channel per block -> block_size=1024*chans bytes
403 if((size
%(36*OrigChans
)) != 0)
405 alSetError(Context
, AL_INVALID_VALUE
);
412 // Allocate extra padding samples
413 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*OrigChans
+ size
)*NewBytes
);
417 ConvertDataIMA4(ALBuf
->data
, data
, OrigChans
, size
/65);
419 ALBuf
->format
= NewFormat
;
420 ALBuf
->eOriginalFormat
= format
;
421 ALBuf
->size
= size
*NewBytes
;
422 ALBuf
->frequency
= freq
;
425 alSetError(Context
, AL_OUT_OF_MEMORY
);
428 case AL_FORMAT_MONO_MULAW
:
429 case AL_FORMAT_STEREO_MULAW
:
430 case AL_FORMAT_QUAD_MULAW
:
431 case AL_FORMAT_51CHN_MULAW
:
432 case AL_FORMAT_61CHN_MULAW
:
433 case AL_FORMAT_71CHN_MULAW
: {
434 int Channels
= ((format
==AL_FORMAT_MONO_MULAW
) ? 1 :
435 ((format
==AL_FORMAT_STEREO_MULAW
) ? 2 :
436 ((format
==AL_FORMAT_QUAD_MULAW
) ? 4 :
437 ((format
==AL_FORMAT_51CHN_MULAW
) ? 6 :
438 ((format
==AL_FORMAT_61CHN_MULAW
) ? 7 : 8)))));
439 ALenum NewFormat
= ((Channels
==1) ? AL_FORMAT_MONO_FLOAT32
:
440 ((Channels
==2) ? AL_FORMAT_STEREO_FLOAT32
:
441 ((Channels
==4) ? AL_FORMAT_QUAD32
:
442 ((Channels
==6) ? AL_FORMAT_51CHN32
:
443 ((Channels
==7) ? AL_FORMAT_61CHN32
:
444 AL_FORMAT_71CHN32
)))));
445 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
447 if((size
%(1*Channels
)) != 0)
449 alSetError(Context
, AL_INVALID_VALUE
);
453 // Allocate extra padding samples
454 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*Channels
+ size
)*NewBytes
);
458 ConvertDataMULaw(ALBuf
->data
, data
, size
);
460 ALBuf
->format
= NewFormat
;
461 ALBuf
->eOriginalFormat
= format
;
462 ALBuf
->size
= size
*NewBytes
;
463 ALBuf
->frequency
= freq
;
466 alSetError(Context
, AL_OUT_OF_MEMORY
);
469 case AL_FORMAT_REAR_MULAW
: {
471 ALenum NewFormat
= AL_FORMAT_QUAD32
;
472 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
473 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
475 if((size
%(1*OrigChans
)) != 0)
477 alSetError(Context
, AL_INVALID_VALUE
);
483 // Allocate extra padding samples
484 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ size
)*NewBytes
);
488 ConvertDataMULawRear(ALBuf
->data
, data
, size
);
490 ALBuf
->format
= NewFormat
;
491 ALBuf
->eOriginalFormat
= format
;
492 ALBuf
->size
= size
*NewBytes
;
493 ALBuf
->frequency
= freq
;
496 alSetError(Context
, AL_OUT_OF_MEMORY
);
500 alSetError(Context
, AL_INVALID_ENUM
);
506 // Buffer is in use, or data is a NULL pointer
507 alSetError(Context
, AL_INVALID_VALUE
);
512 // Invalid Buffer Name
513 alSetError(Context
, AL_INVALID_NAME
);
516 ProcessContext(Context
);
520 * alBufferSubDataEXT(ALuint buffer,ALenum format,ALvoid *data,ALsizei offset,ALsizei length)
522 * Fill buffer with audio data
524 ALvoid AL_APIENTRY
alBufferSubDataEXT(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei offset
,ALsizei length
)
530 Context
= GetContextSuspended();
533 device
= Context
->Device
;
534 if((ALBuf
=VerifyBuffer(device
->BufferList
, buffer
)) != NULL
)
536 if(Context
->SampleSource
)
540 if(Context
->SampleSource
->state
== MAPPED
)
542 alSetError(Context
, AL_INVALID_OPERATION
);
543 ProcessContext(Context
);
547 offset
= (const ALubyte
*)data
- (ALubyte
*)NULL
;
548 data
= Context
->SampleSource
->data
+ offset
;
551 if(ALBuf
->data
== NULL
)
553 // buffer does not have any data
554 alSetError(Context
, AL_INVALID_NAME
);
556 else if(length
< 0 || offset
< 0 || (length
> 0 && data
== NULL
))
558 // data is NULL or offset/length is negative
559 alSetError(Context
, AL_INVALID_VALUE
);
565 case AL_FORMAT_REAR8
:
566 case AL_FORMAT_REAR16
:
567 case AL_FORMAT_REAR32
: {
568 ALuint OrigBytes
= ((format
==AL_FORMAT_REAR8
) ? 1 :
569 ((format
==AL_FORMAT_REAR16
) ? 2 :
571 ALuint NewBytes
= aluBytesFromFormat(ALBuf
->format
);
573 if(ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR8
&&
574 ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR16
&&
575 ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR32
)
577 alSetError(Context
, AL_INVALID_ENUM
);
581 if(ALBuf
->size
/4/NewBytes
< (ALuint
)offset
+length
)
583 alSetError(Context
, AL_INVALID_VALUE
);
587 ConvertDataRear(&ALBuf
->data
[offset
*4], data
, OrigBytes
, length
*2);
590 case AL_FORMAT_MONO_IMA4
:
591 case AL_FORMAT_STEREO_IMA4
: {
592 int Channels
= aluChannelsFromFormat(ALBuf
->format
);
593 ALuint Bytes
= aluBytesFromFormat(ALBuf
->format
);
595 if(ALBuf
->eOriginalFormat
!= format
)
597 alSetError(Context
, AL_INVALID_ENUM
);
601 if((offset
%65) != 0 || (length
%65) != 0 ||
602 ALBuf
->size
/Channels
/Bytes
< (ALuint
)offset
+length
)
604 alSetError(Context
, AL_INVALID_VALUE
);
608 ConvertDataIMA4(&ALBuf
->data
[offset
*Channels
], data
, Channels
, length
/65*Channels
);
611 case AL_FORMAT_MONO_MULAW
:
612 case AL_FORMAT_STEREO_MULAW
:
613 case AL_FORMAT_QUAD_MULAW
:
614 case AL_FORMAT_REAR_MULAW
:
615 case AL_FORMAT_51CHN_MULAW
:
616 case AL_FORMAT_61CHN_MULAW
:
617 case AL_FORMAT_71CHN_MULAW
: {
618 int Channels
= aluChannelsFromFormat(ALBuf
->format
);
619 ALuint Bytes
= aluBytesFromFormat(ALBuf
->format
);
621 if(ALBuf
->eOriginalFormat
!= format
)
623 alSetError(Context
, AL_INVALID_ENUM
);
627 if(ALBuf
->size
/Channels
/Bytes
< (ALuint
)offset
+length
)
629 alSetError(Context
, AL_INVALID_VALUE
);
633 if(ALBuf
->eOriginalFormat
== AL_FORMAT_REAR_MULAW
)
634 ConvertDataMULawRear(&ALBuf
->data
[offset
*Channels
], data
, length
*2);
636 ConvertDataMULaw(&ALBuf
->data
[offset
*Channels
], data
, length
*Channels
);
640 ALuint Channels
= aluChannelsFromFormat(format
);
641 ALuint Bytes
= aluBytesFromFormat(format
);
642 ALuint NewBytes
= aluBytesFromFormat(ALBuf
->format
);
644 if(Channels
!= aluChannelsFromFormat(ALBuf
->format
))
646 alSetError(Context
, AL_INVALID_ENUM
);
650 if(ALBuf
->size
/Channels
/NewBytes
< (ALuint
)offset
+length
)
652 alSetError(Context
, AL_INVALID_VALUE
);
656 ConvertData(&ALBuf
->data
[offset
*Channels
], data
, Bytes
, length
*Channels
);
663 // Invalid Buffer Name
664 alSetError(Context
, AL_INVALID_NAME
);
667 ProcessContext(Context
);
671 AL_API
void AL_APIENTRY
alBufferf(ALuint buffer
, ALenum eParam
, ALfloat flValue
)
673 ALCcontext
*pContext
;
678 pContext
= GetContextSuspended();
679 if(!pContext
) return;
681 device
= pContext
->Device
;
682 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
687 alSetError(pContext
, AL_INVALID_ENUM
);
693 alSetError(pContext
, AL_INVALID_NAME
);
696 ProcessContext(pContext
);
700 AL_API
void AL_APIENTRY
alBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat flValue1
, ALfloat flValue2
, ALfloat flValue3
)
702 ALCcontext
*pContext
;
709 pContext
= GetContextSuspended();
710 if(!pContext
) return;
712 device
= pContext
->Device
;
713 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
718 alSetError(pContext
, AL_INVALID_ENUM
);
724 alSetError(pContext
, AL_INVALID_NAME
);
727 ProcessContext(pContext
);
731 AL_API
void AL_APIENTRY
alBufferfv(ALuint buffer
, ALenum eParam
, const ALfloat
* flValues
)
733 ALCcontext
*pContext
;
738 pContext
= GetContextSuspended();
739 if(!pContext
) return;
741 device
= pContext
->Device
;
742 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
747 alSetError(pContext
, AL_INVALID_ENUM
);
753 alSetError(pContext
, AL_INVALID_NAME
);
756 ProcessContext(pContext
);
760 AL_API
void AL_APIENTRY
alBufferi(ALuint buffer
, ALenum eParam
, ALint lValue
)
762 ALCcontext
*pContext
;
767 pContext
= GetContextSuspended();
768 if(!pContext
) return;
770 device
= pContext
->Device
;
771 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
776 alSetError(pContext
, AL_INVALID_ENUM
);
782 alSetError(pContext
, AL_INVALID_NAME
);
785 ProcessContext(pContext
);
789 AL_API
void AL_APIENTRY
alBuffer3i( ALuint buffer
, ALenum eParam
, ALint lValue1
, ALint lValue2
, ALint lValue3
)
791 ALCcontext
*pContext
;
798 pContext
= GetContextSuspended();
799 if(!pContext
) return;
801 device
= pContext
->Device
;
802 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
807 alSetError(pContext
, AL_INVALID_ENUM
);
813 alSetError(pContext
, AL_INVALID_NAME
);
816 ProcessContext(pContext
);
820 AL_API
void AL_APIENTRY
alBufferiv(ALuint buffer
, ALenum eParam
, const ALint
* plValues
)
822 ALCcontext
*pContext
;
827 pContext
= GetContextSuspended();
828 if(!pContext
) return;
830 device
= pContext
->Device
;
831 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
836 alSetError(pContext
, AL_INVALID_ENUM
);
842 alSetError(pContext
, AL_INVALID_NAME
);
845 ProcessContext(pContext
);
849 AL_API ALvoid AL_APIENTRY
alGetBufferf(ALuint buffer
, ALenum eParam
, ALfloat
*pflValue
)
851 ALCcontext
*pContext
;
854 pContext
= GetContextSuspended();
855 if(!pContext
) return;
859 device
= pContext
->Device
;
860 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
865 alSetError(pContext
, AL_INVALID_ENUM
);
871 alSetError(pContext
, AL_INVALID_NAME
);
876 alSetError(pContext
, AL_INVALID_VALUE
);
879 ProcessContext(pContext
);
883 AL_API
void AL_APIENTRY
alGetBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat
* pflValue1
, ALfloat
* pflValue2
, ALfloat
* pflValue3
)
885 ALCcontext
*pContext
;
888 pContext
= GetContextSuspended();
889 if(!pContext
) return;
891 if ((pflValue1
) && (pflValue2
) && (pflValue3
))
893 device
= pContext
->Device
;
894 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
899 alSetError(pContext
, AL_INVALID_ENUM
);
905 alSetError(pContext
, AL_INVALID_NAME
);
910 alSetError(pContext
, AL_INVALID_VALUE
);
913 ProcessContext(pContext
);
917 AL_API
void AL_APIENTRY
alGetBufferfv(ALuint buffer
, ALenum eParam
, ALfloat
* pflValues
)
919 ALCcontext
*pContext
;
922 pContext
= GetContextSuspended();
923 if(!pContext
) return;
927 device
= pContext
->Device
;
928 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
933 alSetError(pContext
, AL_INVALID_ENUM
);
939 alSetError(pContext
, AL_INVALID_NAME
);
944 alSetError(pContext
, AL_INVALID_VALUE
);
947 ProcessContext(pContext
);
951 AL_API ALvoid AL_APIENTRY
alGetBufferi(ALuint buffer
, ALenum eParam
, ALint
*plValue
)
953 ALCcontext
*pContext
;
957 pContext
= GetContextSuspended();
958 if(!pContext
) return;
962 device
= pContext
->Device
;
963 if((pBuffer
=VerifyBuffer(device
->BufferList
, buffer
)) != NULL
)
968 *plValue
= pBuffer
->frequency
;
972 *plValue
= aluBytesFromFormat(pBuffer
->format
) * 8;
976 *plValue
= aluChannelsFromFormat(pBuffer
->format
);
980 *plValue
= pBuffer
->size
;
984 alSetError(pContext
, AL_INVALID_ENUM
);
990 alSetError(pContext
, AL_INVALID_NAME
);
995 alSetError(pContext
, AL_INVALID_VALUE
);
998 ProcessContext(pContext
);
1002 AL_API
void AL_APIENTRY
alGetBuffer3i(ALuint buffer
, ALenum eParam
, ALint
* plValue1
, ALint
* plValue2
, ALint
* plValue3
)
1004 ALCcontext
*pContext
;
1007 pContext
= GetContextSuspended();
1008 if(!pContext
) return;
1010 if ((plValue1
) && (plValue2
) && (plValue3
))
1012 device
= pContext
->Device
;
1013 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
1018 alSetError(pContext
, AL_INVALID_ENUM
);
1024 alSetError(pContext
, AL_INVALID_NAME
);
1029 alSetError(pContext
, AL_INVALID_VALUE
);
1032 ProcessContext(pContext
);
1036 AL_API
void AL_APIENTRY
alGetBufferiv(ALuint buffer
, ALenum eParam
, ALint
* plValues
)
1038 ALCcontext
*pContext
;
1041 pContext
= GetContextSuspended();
1042 if(!pContext
) return;
1046 device
= pContext
->Device
;
1047 if(VerifyBuffer(device
->BufferList
, buffer
) != NULL
)
1055 alGetBufferi(buffer
, eParam
, plValues
);
1059 alSetError(pContext
, AL_INVALID_ENUM
);
1065 alSetError(pContext
, AL_INVALID_NAME
);
1070 alSetError(pContext
, AL_INVALID_VALUE
);
1073 ProcessContext(pContext
);
1079 * Loads the specified data into the buffer, using the specified formats.
1080 * Currently, the new format must be 32-bit float, and must have the same
1081 * channel configuration as the original format. This does NOT handle
1082 * compressed formats (eg. IMA4).
1084 static ALenum
LoadData(ALbuffer
*ALBuf
, const ALubyte
*data
, ALsizei size
, ALuint freq
, ALenum OrigFormat
, ALenum NewFormat
)
1086 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
1087 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
1088 ALuint OrigBytes
= aluBytesFromFormat(OrigFormat
);
1089 ALuint OrigChannels
= aluChannelsFromFormat(OrigFormat
);
1092 assert(NewBytes
== 4);
1093 assert(NewChannels
== OrigChannels
);
1095 if ((size
%(OrigBytes
*OrigChannels
)) != 0)
1096 return AL_INVALID_VALUE
;
1098 // Samples are converted here
1100 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ size
) * NewBytes
);
1101 if(!temp
) return AL_OUT_OF_MEMORY
;
1103 ConvertData(ALBuf
->data
, data
, OrigBytes
, size
);
1105 ALBuf
->format
= NewFormat
;
1106 ALBuf
->eOriginalFormat
= OrigFormat
;
1107 ALBuf
->size
= size
*NewBytes
;
1108 ALBuf
->frequency
= freq
;
1113 static void ConvertData(ALfloat
*dst
, const ALvoid
*src
, ALint origBytes
, ALsizei len
)
1120 for(i
= 0;i
< len
;i
++)
1122 smp
= ((ALubyte
*)src
)[i
];
1123 dst
[i
] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1128 for(i
= 0;i
< len
;i
++)
1130 smp
= ((ALshort
*)src
)[i
];
1131 dst
[i
] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1136 for(i
= 0;i
< len
;i
++)
1137 dst
[i
] = ((ALfloat
*)src
)[i
];
1141 for(i
= 0;i
< len
;i
++)
1142 dst
[i
] = ((ALdouble
*)src
)[i
];
1150 static void ConvertDataRear(ALfloat
*dst
, const ALvoid
*src
, ALint origBytes
, ALsizei len
)
1157 for(i
= 0;i
< len
;i
+=4)
1161 smp
= ((ALubyte
*)src
)[i
/2+0];
1162 dst
[i
+2] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1163 smp
= ((ALubyte
*)src
)[i
/2+1];
1164 dst
[i
+3] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1169 for(i
= 0;i
< len
;i
+=4)
1173 smp
= ((ALshort
*)src
)[i
/2+0];
1174 dst
[i
+2] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1175 smp
= ((ALshort
*)src
)[i
/2+1];
1176 dst
[i
+3] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1181 for(i
= 0;i
< len
;i
+=4)
1185 dst
[i
+2] = ((ALfloat
*)src
)[i
/2+0];
1186 dst
[i
+3] = ((ALfloat
*)src
)[i
/2+1];
1195 static void ConvertDataIMA4(ALfloat
*dst
, const ALvoid
*src
, ALint origChans
, ALsizei len
)
1197 const ALuint
*IMAData
;
1198 ALint Sample
[2],Index
[2];
1203 for(i
= 0;i
< len
/origChans
;i
++)
1205 for(c
= 0;c
< origChans
;c
++)
1207 Sample
[c
] = ((ALshort
*)IMAData
)[0];
1208 Index
[c
] = ((ALshort
*)IMAData
)[1];
1210 Index
[c
] = ((Index
[c
]<0) ? 0 : Index
[c
]);
1211 Index
[c
] = ((Index
[c
]>88) ? 88 : Index
[c
]);
1213 dst
[i
*65*origChans
+ c
] = ((Sample
[c
] < 0) ? (Sample
[c
]/32768.0f
) : (Sample
[c
]/32767.0f
));
1218 for(j
= 1;j
< 65;j
+= 8)
1220 for(c
= 0;c
< origChans
;c
++)
1221 IMACode
[c
] = *(IMAData
++);
1223 for(k
= 0;k
< 8;k
++)
1225 for(c
= 0;c
< origChans
;c
++)
1227 Sample
[c
] += ((g_IMAStep_size
[Index
[c
]]*g_IMACodeword_4
[IMACode
[c
]&15])/8);
1228 Index
[c
] += g_IMAIndex_adjust_4
[IMACode
[c
]&15];
1230 if(Sample
[c
] < -32768) Sample
[c
] = -32768;
1231 else if(Sample
[c
] > 32767) Sample
[c
] = 32767;
1233 if(Index
[c
]<0) Index
[c
] = 0;
1234 else if(Index
[c
]>88) Index
[c
] = 88;
1236 dst
[(i
*65+j
+k
)*origChans
+ c
] = ((Sample
[c
] < 0) ? (Sample
[c
]/32768.0f
) : (Sample
[c
]/32767.0f
));
1244 static void ConvertDataMULaw(ALfloat
*dst
, const ALvoid
*src
, ALsizei len
)
1248 for(i
= 0;i
< len
;i
++)
1250 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
]];
1251 dst
[i
] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1255 static void ConvertDataMULawRear(ALfloat
*dst
, const ALvoid
*src
, ALsizei len
)
1259 for(i
= 0;i
< len
;i
+=4)
1263 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
/2+0]];
1264 dst
[i
+2] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1265 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
/2+1]];
1266 dst
[i
+3] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1271 * ReleaseALBuffers()
1273 * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
1275 ALvoid
ReleaseALBuffers(ALCdevice
*device
)
1278 ALbuffer
*ALBufferTemp
;
1280 ALBuffer
= device
->BufferList
;
1283 // Release sample data
1284 free(ALBuffer
->data
);
1286 // Release Buffer structure
1287 ALBufferTemp
= ALBuffer
;
1288 ALBuffer
= ALBuffer
->next
;
1289 memset(ALBufferTemp
, 0, sizeof(ALbuffer
));
1292 device
->BufferList
= NULL
;
1293 device
->BufferCount
= 0;