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 #define LookupBuffer(m, k) ((ALbuffer*)LookupUIntMapKey(&(m), (k)))
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
*buffers
)
112 Context
= GetContextSuspended();
115 // Check that we are actually generation some Buffers
118 ALCdevice
*device
= Context
->Device
;
121 // Check the pointer is valid (and points to enough memory to store Buffer Names)
122 if(!IsBadWritePtr((void*)buffers
, n
* sizeof(ALuint
)))
124 // Create all the new Buffers
127 ALbuffer
*buffer
= calloc(1, sizeof(ALbuffer
));
130 alSetError(Context
, AL_OUT_OF_MEMORY
);
131 alDeleteBuffers(i
, buffers
);
135 buffer
->buffer
= (ALuint
)ALTHUNK_ADDENTRY(buffer
);
136 err
= InsertUIntMapEntry(&device
->BufferMap
, buffer
->buffer
,
138 if(err
!= AL_NO_ERROR
)
140 ALTHUNK_REMOVEENTRY(buffer
->buffer
);
141 memset(buffer
, 0, sizeof(ALbuffer
));
144 alSetError(Context
, err
);
145 alDeleteBuffers(i
, buffers
);
148 buffers
[i
++] = buffer
->buffer
;
153 // Pointer does not point to enough memory to write Buffer names
154 alSetError(Context
, AL_INVALID_VALUE
);
158 ProcessContext(Context
);
162 * alDeleteBuffers(ALsizei n, ALuint *puiBuffers)
164 * Deletes the n AL Buffers pointed to by puiBuffers
166 AL_API ALvoid AL_APIENTRY
alDeleteBuffers(ALsizei n
, const ALuint
*puiBuffers
)
171 ALboolean bFailed
= AL_FALSE
;
173 Context
= GetContextSuspended();
176 // Check we are actually Deleting some Buffers
179 ALCdevice
*device
= Context
->Device
;
181 // Check that all the buffers are valid and can actually be deleted
182 for (i
= 0; i
< n
; i
++)
187 // Check for valid Buffer ID (can be NULL buffer)
188 if((ALBuf
=LookupBuffer(device
->BufferMap
, puiBuffers
[i
])) != NULL
)
190 if(ALBuf
->refcount
!= 0)
192 // Buffer still in use, cannot be deleted
193 alSetError(Context
, AL_INVALID_OPERATION
);
201 alSetError(Context
, AL_INVALID_NAME
);
207 // If all the Buffers were valid (and have Reference Counts of 0), then we can delete them
210 for (i
= 0; i
< n
; i
++)
212 if((ALBuf
=LookupBuffer(device
->BufferMap
, puiBuffers
[i
])) != NULL
)
214 // Release the memory used to store audio data
217 // Release buffer structure
218 RemoveUIntMapKey(&device
->BufferMap
, ALBuf
->buffer
);
219 ALTHUNK_REMOVEENTRY(ALBuf
->buffer
);
221 memset(ALBuf
, 0, sizeof(ALbuffer
));
228 alSetError(Context
, AL_INVALID_VALUE
);
230 ProcessContext(Context
);
234 * alIsBuffer(ALuint uiBuffer)
236 * Checks if ulBuffer is a valid Buffer Name
238 AL_API ALboolean AL_APIENTRY
alIsBuffer(ALuint uiBuffer
)
241 ALboolean result
= AL_TRUE
;
243 Context
= GetContextSuspended();
244 if(!Context
) return AL_FALSE
;
247 result
= (LookupBuffer(Context
->Device
->BufferMap
, uiBuffer
) ?
250 ProcessContext(Context
);
256 * alBufferData(ALuint buffer,ALenum format,ALvoid *data,ALsizei size,ALsizei freq)
258 * Fill buffer with audio data
260 AL_API ALvoid AL_APIENTRY
alBufferData(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei size
,ALsizei freq
)
268 Context
= GetContextSuspended();
271 device
= Context
->Device
;
272 if((ALBuf
=LookupBuffer(device
->BufferMap
, buffer
)) != NULL
)
274 if(Context
->SampleSource
)
278 if(Context
->SampleSource
->state
== MAPPED
)
280 alSetError(Context
, AL_INVALID_OPERATION
);
281 ProcessContext(Context
);
285 offset
= (const ALubyte
*)data
- (ALubyte
*)NULL
;
286 data
= Context
->SampleSource
->data
+ offset
;
289 if ((ALBuf
->refcount
==0)&&(data
))
293 case AL_FORMAT_MONO8
:
294 case AL_FORMAT_MONO16
:
295 case AL_FORMAT_MONO_FLOAT32
:
296 case AL_FORMAT_MONO_DOUBLE_EXT
:
297 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_MONO_FLOAT32
);
298 if(err
!= AL_NO_ERROR
)
299 alSetError(Context
, err
);
302 case AL_FORMAT_STEREO8
:
303 case AL_FORMAT_STEREO16
:
304 case AL_FORMAT_STEREO_FLOAT32
:
305 case AL_FORMAT_STEREO_DOUBLE_EXT
:
306 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_STEREO_FLOAT32
);
307 if(err
!= AL_NO_ERROR
)
308 alSetError(Context
, err
);
311 case AL_FORMAT_REAR8
:
312 case AL_FORMAT_REAR16
:
313 case AL_FORMAT_REAR32
: {
314 ALenum NewFormat
= AL_FORMAT_QUAD32
;
315 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
316 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
317 ALuint OrigBytes
= ((format
==AL_FORMAT_REAR8
) ? 1 :
318 ((format
==AL_FORMAT_REAR16
) ? 2 :
322 if((size
%(OrigBytes
*2)) != 0)
324 alSetError(Context
, AL_INVALID_VALUE
);
328 newsize
= size
/ OrigBytes
;
331 // Samples are converted here
332 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ newsize
) * NewBytes
);
336 ConvertDataRear(ALBuf
->data
, data
, OrigBytes
, newsize
);
338 ALBuf
->format
= NewFormat
;
339 ALBuf
->eOriginalFormat
= format
;
340 ALBuf
->size
= newsize
*NewBytes
;
341 ALBuf
->frequency
= freq
;
343 ALBuf
->OriginalSize
= size
;
344 ALBuf
->OriginalAlign
= OrigBytes
* 2;
347 alSetError(Context
, AL_OUT_OF_MEMORY
);
350 case AL_FORMAT_QUAD8_LOKI
:
351 case AL_FORMAT_QUAD16_LOKI
:
352 case AL_FORMAT_QUAD8
:
353 case AL_FORMAT_QUAD16
:
354 case AL_FORMAT_QUAD32
:
355 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_QUAD32
);
356 if(err
!= AL_NO_ERROR
)
357 alSetError(Context
, err
);
360 case AL_FORMAT_51CHN8
:
361 case AL_FORMAT_51CHN16
:
362 case AL_FORMAT_51CHN32
:
363 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_51CHN32
);
364 if(err
!= AL_NO_ERROR
)
365 alSetError(Context
, err
);
368 case AL_FORMAT_61CHN8
:
369 case AL_FORMAT_61CHN16
:
370 case AL_FORMAT_61CHN32
:
371 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_61CHN32
);
372 if(err
!= AL_NO_ERROR
)
373 alSetError(Context
, err
);
376 case AL_FORMAT_71CHN8
:
377 case AL_FORMAT_71CHN16
:
378 case AL_FORMAT_71CHN32
:
379 err
= LoadData(ALBuf
, data
, size
, freq
, format
, AL_FORMAT_71CHN32
);
380 if(err
!= AL_NO_ERROR
)
381 alSetError(Context
, err
);
384 case AL_FORMAT_MONO_IMA4
:
385 case AL_FORMAT_STEREO_IMA4
: {
386 int OrigChans
= ((format
==AL_FORMAT_MONO_IMA4
) ? 1 : 2);
387 ALenum NewFormat
= ((OrigChans
==1) ? AL_FORMAT_MONO_FLOAT32
:
388 AL_FORMAT_STEREO_FLOAT32
);
389 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
+ newsize
)*NewBytes
);
409 ConvertDataIMA4(ALBuf
->data
, data
, OrigChans
, newsize
/65);
411 ALBuf
->format
= NewFormat
;
412 ALBuf
->eOriginalFormat
= format
;
413 ALBuf
->size
= newsize
*NewBytes
;
414 ALBuf
->frequency
= freq
;
416 ALBuf
->OriginalSize
= size
;
417 ALBuf
->OriginalAlign
= 36 * OrigChans
;
420 alSetError(Context
, AL_OUT_OF_MEMORY
);
423 case AL_FORMAT_MONO_MULAW
:
424 case AL_FORMAT_STEREO_MULAW
:
425 case AL_FORMAT_QUAD_MULAW
:
426 case AL_FORMAT_51CHN_MULAW
:
427 case AL_FORMAT_61CHN_MULAW
:
428 case AL_FORMAT_71CHN_MULAW
: {
429 int Channels
= ((format
==AL_FORMAT_MONO_MULAW
) ? 1 :
430 ((format
==AL_FORMAT_STEREO_MULAW
) ? 2 :
431 ((format
==AL_FORMAT_QUAD_MULAW
) ? 4 :
432 ((format
==AL_FORMAT_51CHN_MULAW
) ? 6 :
433 ((format
==AL_FORMAT_61CHN_MULAW
) ? 7 : 8)))));
434 ALenum NewFormat
= ((Channels
==1) ? AL_FORMAT_MONO_FLOAT32
:
435 ((Channels
==2) ? AL_FORMAT_STEREO_FLOAT32
:
436 ((Channels
==4) ? AL_FORMAT_QUAD32
:
437 ((Channels
==6) ? AL_FORMAT_51CHN32
:
438 ((Channels
==7) ? AL_FORMAT_61CHN32
:
439 AL_FORMAT_71CHN32
)))));
440 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
442 if((size
%(1*Channels
)) != 0)
444 alSetError(Context
, AL_INVALID_VALUE
);
448 // Allocate extra padding samples
449 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*Channels
+ size
)*NewBytes
);
453 ConvertDataMULaw(ALBuf
->data
, data
, size
);
455 ALBuf
->format
= NewFormat
;
456 ALBuf
->eOriginalFormat
= format
;
457 ALBuf
->size
= size
*NewBytes
;
458 ALBuf
->frequency
= freq
;
460 ALBuf
->OriginalSize
= size
;
461 ALBuf
->OriginalAlign
= 1 * Channels
;
464 alSetError(Context
, AL_OUT_OF_MEMORY
);
467 case AL_FORMAT_REAR_MULAW
: {
469 ALenum NewFormat
= AL_FORMAT_QUAD32
;
470 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
471 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
474 if((size
%(1*OrigChans
)) != 0)
476 alSetError(Context
, AL_INVALID_VALUE
);
482 // Allocate extra padding samples
483 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ newsize
)*NewBytes
);
487 ConvertDataMULawRear(ALBuf
->data
, data
, newsize
);
489 ALBuf
->format
= NewFormat
;
490 ALBuf
->eOriginalFormat
= format
;
491 ALBuf
->size
= newsize
*NewBytes
;
492 ALBuf
->frequency
= freq
;
494 ALBuf
->OriginalSize
= size
;
495 ALBuf
->OriginalAlign
= 1 * OrigChans
;
498 alSetError(Context
, AL_OUT_OF_MEMORY
);
502 alSetError(Context
, AL_INVALID_ENUM
);
508 // Buffer is in use, or data is a NULL pointer
509 alSetError(Context
, AL_INVALID_VALUE
);
514 // Invalid Buffer Name
515 alSetError(Context
, AL_INVALID_NAME
);
518 ProcessContext(Context
);
522 * alBufferSubDataEXT(ALuint buffer,ALenum format,ALvoid *data,ALsizei offset,ALsizei length)
524 * Fill buffer with audio data
526 AL_API ALvoid AL_APIENTRY
alBufferSubDataEXT(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei offset
,ALsizei length
)
532 Context
= GetContextSuspended();
535 device
= Context
->Device
;
536 if((ALBuf
=LookupBuffer(device
->BufferMap
, buffer
)) != NULL
)
538 if(Context
->SampleSource
)
542 if(Context
->SampleSource
->state
== MAPPED
)
544 alSetError(Context
, AL_INVALID_OPERATION
);
545 ProcessContext(Context
);
549 offset
= (const ALubyte
*)data
- (ALubyte
*)NULL
;
550 data
= Context
->SampleSource
->data
+ offset
;
553 if(ALBuf
->data
== NULL
)
555 // buffer does not have any data
556 alSetError(Context
, AL_INVALID_NAME
);
558 else if(length
< 0 || offset
< 0 || (length
> 0 && data
== NULL
))
560 // data is NULL or offset/length is negative
561 alSetError(Context
, AL_INVALID_VALUE
);
567 case AL_FORMAT_REAR8
:
568 case AL_FORMAT_REAR16
:
569 case AL_FORMAT_REAR32
: {
570 ALuint OrigBytes
= ((format
==AL_FORMAT_REAR8
) ? 1 :
571 ((format
==AL_FORMAT_REAR16
) ? 2 :
573 ALuint NewBytes
= aluBytesFromFormat(ALBuf
->format
);
575 if(ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR8
&&
576 ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR16
&&
577 ALBuf
->eOriginalFormat
!= AL_FORMAT_REAR32
)
579 alSetError(Context
, AL_INVALID_ENUM
);
583 if(ALBuf
->size
/4/NewBytes
< (ALuint
)offset
+length
)
585 alSetError(Context
, AL_INVALID_VALUE
);
589 ConvertDataRear(&ALBuf
->data
[offset
*4], data
, OrigBytes
, length
*2);
592 case AL_FORMAT_MONO_IMA4
:
593 case AL_FORMAT_STEREO_IMA4
: {
594 int Channels
= aluChannelsFromFormat(ALBuf
->format
);
595 ALuint Bytes
= aluBytesFromFormat(ALBuf
->format
);
597 if(ALBuf
->eOriginalFormat
!= format
)
599 alSetError(Context
, AL_INVALID_ENUM
);
603 if((offset
%65) != 0 || (length
%65) != 0 ||
604 ALBuf
->size
/Channels
/Bytes
< (ALuint
)offset
+length
)
606 alSetError(Context
, AL_INVALID_VALUE
);
610 ConvertDataIMA4(&ALBuf
->data
[offset
*Channels
], data
, Channels
, length
/65*Channels
);
613 case AL_FORMAT_MONO_MULAW
:
614 case AL_FORMAT_STEREO_MULAW
:
615 case AL_FORMAT_QUAD_MULAW
:
616 case AL_FORMAT_REAR_MULAW
:
617 case AL_FORMAT_51CHN_MULAW
:
618 case AL_FORMAT_61CHN_MULAW
:
619 case AL_FORMAT_71CHN_MULAW
: {
620 int Channels
= aluChannelsFromFormat(ALBuf
->format
);
621 ALuint Bytes
= aluBytesFromFormat(ALBuf
->format
);
623 if(ALBuf
->eOriginalFormat
!= format
)
625 alSetError(Context
, AL_INVALID_ENUM
);
629 if(ALBuf
->size
/Channels
/Bytes
< (ALuint
)offset
+length
)
631 alSetError(Context
, AL_INVALID_VALUE
);
635 if(ALBuf
->eOriginalFormat
== AL_FORMAT_REAR_MULAW
)
636 ConvertDataMULawRear(&ALBuf
->data
[offset
*Channels
], data
, length
*2);
638 ConvertDataMULaw(&ALBuf
->data
[offset
*Channels
], data
, length
*Channels
);
642 ALuint Channels
= aluChannelsFromFormat(format
);
643 ALuint Bytes
= aluBytesFromFormat(format
);
644 ALuint NewBytes
= aluBytesFromFormat(ALBuf
->format
);
646 if(Channels
!= aluChannelsFromFormat(ALBuf
->format
))
648 alSetError(Context
, AL_INVALID_ENUM
);
652 if(ALBuf
->size
/Channels
/NewBytes
< (ALuint
)offset
+length
)
654 alSetError(Context
, AL_INVALID_VALUE
);
658 ConvertData(&ALBuf
->data
[offset
*Channels
], data
, Bytes
, length
*Channels
);
665 // Invalid Buffer Name
666 alSetError(Context
, AL_INVALID_NAME
);
669 ProcessContext(Context
);
673 AL_API
void AL_APIENTRY
alBufferf(ALuint buffer
, ALenum eParam
, ALfloat flValue
)
675 ALCcontext
*pContext
;
680 pContext
= GetContextSuspended();
681 if(!pContext
) return;
683 device
= pContext
->Device
;
684 if(LookupBuffer(device
->BufferMap
, buffer
) != NULL
)
689 alSetError(pContext
, AL_INVALID_ENUM
);
695 alSetError(pContext
, AL_INVALID_NAME
);
698 ProcessContext(pContext
);
702 AL_API
void AL_APIENTRY
alBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat flValue1
, ALfloat flValue2
, ALfloat flValue3
)
704 ALCcontext
*pContext
;
711 pContext
= GetContextSuspended();
712 if(!pContext
) return;
714 device
= pContext
->Device
;
715 if(LookupBuffer(device
->BufferMap
, buffer
) != NULL
)
720 alSetError(pContext
, AL_INVALID_ENUM
);
726 alSetError(pContext
, AL_INVALID_NAME
);
729 ProcessContext(pContext
);
733 AL_API
void AL_APIENTRY
alBufferfv(ALuint buffer
, ALenum eParam
, const ALfloat
* flValues
)
735 ALCcontext
*pContext
;
740 pContext
= GetContextSuspended();
741 if(!pContext
) return;
743 device
= pContext
->Device
;
744 if(LookupBuffer(device
->BufferMap
, buffer
) != NULL
)
749 alSetError(pContext
, AL_INVALID_ENUM
);
755 alSetError(pContext
, AL_INVALID_NAME
);
758 ProcessContext(pContext
);
762 AL_API
void AL_APIENTRY
alBufferi(ALuint buffer
, ALenum eParam
, ALint lValue
)
764 ALCcontext
*pContext
;
769 pContext
= GetContextSuspended();
770 if(!pContext
) return;
772 device
= pContext
->Device
;
773 if(LookupBuffer(device
->BufferMap
, buffer
) != NULL
)
778 alSetError(pContext
, AL_INVALID_ENUM
);
784 alSetError(pContext
, AL_INVALID_NAME
);
787 ProcessContext(pContext
);
791 AL_API
void AL_APIENTRY
alBuffer3i( ALuint buffer
, ALenum eParam
, ALint lValue1
, ALint lValue2
, ALint lValue3
)
793 ALCcontext
*pContext
;
800 pContext
= GetContextSuspended();
801 if(!pContext
) return;
803 device
= pContext
->Device
;
804 if(LookupBuffer(device
->BufferMap
, buffer
) != NULL
)
809 alSetError(pContext
, AL_INVALID_ENUM
);
815 alSetError(pContext
, AL_INVALID_NAME
);
818 ProcessContext(pContext
);
822 AL_API
void AL_APIENTRY
alBufferiv(ALuint buffer
, ALenum eParam
, const ALint
* plValues
)
824 ALCcontext
*pContext
;
829 pContext
= GetContextSuspended();
830 if(!pContext
) return;
832 device
= pContext
->Device
;
833 if(LookupBuffer(device
->BufferMap
, buffer
) != NULL
)
838 alSetError(pContext
, AL_INVALID_ENUM
);
844 alSetError(pContext
, AL_INVALID_NAME
);
847 ProcessContext(pContext
);
851 AL_API ALvoid AL_APIENTRY
alGetBufferf(ALuint buffer
, ALenum eParam
, ALfloat
*pflValue
)
853 ALCcontext
*pContext
;
856 pContext
= GetContextSuspended();
857 if(!pContext
) return;
861 device
= pContext
->Device
;
862 if(LookupBuffer(device
->BufferMap
, buffer
) != NULL
)
867 alSetError(pContext
, AL_INVALID_ENUM
);
873 alSetError(pContext
, AL_INVALID_NAME
);
878 alSetError(pContext
, AL_INVALID_VALUE
);
881 ProcessContext(pContext
);
885 AL_API
void AL_APIENTRY
alGetBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat
* pflValue1
, ALfloat
* pflValue2
, ALfloat
* pflValue3
)
887 ALCcontext
*pContext
;
890 pContext
= GetContextSuspended();
891 if(!pContext
) return;
893 if ((pflValue1
) && (pflValue2
) && (pflValue3
))
895 device
= pContext
->Device
;
896 if(LookupBuffer(device
->BufferMap
, buffer
) != NULL
)
901 alSetError(pContext
, AL_INVALID_ENUM
);
907 alSetError(pContext
, AL_INVALID_NAME
);
912 alSetError(pContext
, AL_INVALID_VALUE
);
915 ProcessContext(pContext
);
919 AL_API
void AL_APIENTRY
alGetBufferfv(ALuint buffer
, ALenum eParam
, ALfloat
* pflValues
)
921 ALCcontext
*pContext
;
924 pContext
= GetContextSuspended();
925 if(!pContext
) return;
929 device
= pContext
->Device
;
930 if(LookupBuffer(device
->BufferMap
, buffer
) != NULL
)
935 alSetError(pContext
, AL_INVALID_ENUM
);
941 alSetError(pContext
, AL_INVALID_NAME
);
946 alSetError(pContext
, AL_INVALID_VALUE
);
949 ProcessContext(pContext
);
953 AL_API ALvoid AL_APIENTRY
alGetBufferi(ALuint buffer
, ALenum eParam
, ALint
*plValue
)
955 ALCcontext
*pContext
;
959 pContext
= GetContextSuspended();
960 if(!pContext
) return;
964 device
= pContext
->Device
;
965 if((pBuffer
=LookupBuffer(device
->BufferMap
, buffer
)) != NULL
)
970 *plValue
= pBuffer
->frequency
;
974 *plValue
= aluBytesFromFormat(pBuffer
->format
) * 8;
978 *plValue
= aluChannelsFromFormat(pBuffer
->format
);
982 *plValue
= pBuffer
->size
;
986 alSetError(pContext
, AL_INVALID_ENUM
);
992 alSetError(pContext
, AL_INVALID_NAME
);
997 alSetError(pContext
, AL_INVALID_VALUE
);
1000 ProcessContext(pContext
);
1004 AL_API
void AL_APIENTRY
alGetBuffer3i(ALuint buffer
, ALenum eParam
, ALint
* plValue1
, ALint
* plValue2
, ALint
* plValue3
)
1006 ALCcontext
*pContext
;
1009 pContext
= GetContextSuspended();
1010 if(!pContext
) return;
1012 if ((plValue1
) && (plValue2
) && (plValue3
))
1014 device
= pContext
->Device
;
1015 if(LookupBuffer(device
->BufferMap
, buffer
) != NULL
)
1020 alSetError(pContext
, AL_INVALID_ENUM
);
1026 alSetError(pContext
, AL_INVALID_NAME
);
1031 alSetError(pContext
, AL_INVALID_VALUE
);
1034 ProcessContext(pContext
);
1038 AL_API
void AL_APIENTRY
alGetBufferiv(ALuint buffer
, ALenum eParam
, ALint
* plValues
)
1040 ALCcontext
*pContext
;
1043 pContext
= GetContextSuspended();
1044 if(!pContext
) return;
1048 device
= pContext
->Device
;
1049 if(LookupBuffer(device
->BufferMap
, buffer
) != NULL
)
1057 alGetBufferi(buffer
, eParam
, plValues
);
1061 alSetError(pContext
, AL_INVALID_ENUM
);
1067 alSetError(pContext
, AL_INVALID_NAME
);
1072 alSetError(pContext
, AL_INVALID_VALUE
);
1075 ProcessContext(pContext
);
1081 * Loads the specified data into the buffer, using the specified formats.
1082 * Currently, the new format must be 32-bit float, and must have the same
1083 * channel configuration as the original format. This does NOT handle
1084 * compressed formats (eg. IMA4).
1086 static ALenum
LoadData(ALbuffer
*ALBuf
, const ALubyte
*data
, ALsizei size
, ALuint freq
, ALenum OrigFormat
, ALenum NewFormat
)
1088 ALuint NewBytes
= aluBytesFromFormat(NewFormat
);
1089 ALuint NewChannels
= aluChannelsFromFormat(NewFormat
);
1090 ALuint OrigBytes
= aluBytesFromFormat(OrigFormat
);
1091 ALuint OrigChannels
= aluChannelsFromFormat(OrigFormat
);
1095 assert(NewBytes
== 4);
1096 assert(NewChannels
== OrigChannels
);
1098 if ((size
%(OrigBytes
*OrigChannels
)) != 0)
1099 return AL_INVALID_VALUE
;
1101 // Samples are converted here
1102 newsize
= size
/ OrigBytes
;
1103 temp
= realloc(ALBuf
->data
, (BUFFER_PADDING
*NewChannels
+ newsize
) * NewBytes
);
1104 if(!temp
) return AL_OUT_OF_MEMORY
;
1107 ConvertData(ALBuf
->data
, data
, OrigBytes
, newsize
);
1109 ALBuf
->format
= NewFormat
;
1110 ALBuf
->eOriginalFormat
= OrigFormat
;
1111 ALBuf
->size
= newsize
*NewBytes
;
1112 ALBuf
->frequency
= freq
;
1114 ALBuf
->OriginalSize
= size
;
1115 ALBuf
->OriginalAlign
= OrigBytes
* OrigChannels
;
1120 static void ConvertData(ALfloat
*dst
, const ALvoid
*src
, ALint origBytes
, ALsizei len
)
1127 for(i
= 0;i
< len
;i
++)
1129 smp
= ((ALubyte
*)src
)[i
];
1130 dst
[i
] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1135 for(i
= 0;i
< len
;i
++)
1137 smp
= ((ALshort
*)src
)[i
];
1138 dst
[i
] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1143 for(i
= 0;i
< len
;i
++)
1144 dst
[i
] = ((ALfloat
*)src
)[i
];
1148 for(i
= 0;i
< len
;i
++)
1149 dst
[i
] = ((ALdouble
*)src
)[i
];
1157 static void ConvertDataRear(ALfloat
*dst
, const ALvoid
*src
, ALint origBytes
, ALsizei len
)
1164 for(i
= 0;i
< len
;i
+=4)
1168 smp
= ((ALubyte
*)src
)[i
/2+0];
1169 dst
[i
+2] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1170 smp
= ((ALubyte
*)src
)[i
/2+1];
1171 dst
[i
+3] = ((smp
< 0x80) ? ((smp
-128)/128.0f
) : ((smp
-128)/127.0f
));
1176 for(i
= 0;i
< len
;i
+=4)
1180 smp
= ((ALshort
*)src
)[i
/2+0];
1181 dst
[i
+2] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1182 smp
= ((ALshort
*)src
)[i
/2+1];
1183 dst
[i
+3] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1188 for(i
= 0;i
< len
;i
+=4)
1192 dst
[i
+2] = ((ALfloat
*)src
)[i
/2+0];
1193 dst
[i
+3] = ((ALfloat
*)src
)[i
/2+1];
1202 static void ConvertDataIMA4(ALfloat
*dst
, const ALvoid
*src
, ALint origChans
, ALsizei len
)
1204 const ALuint
*IMAData
;
1205 ALint Sample
[2],Index
[2];
1210 for(i
= 0;i
< len
/origChans
;i
++)
1212 for(c
= 0;c
< origChans
;c
++)
1214 Sample
[c
] = ((ALshort
*)IMAData
)[0];
1215 Index
[c
] = ((ALshort
*)IMAData
)[1];
1217 Index
[c
] = ((Index
[c
]<0) ? 0 : Index
[c
]);
1218 Index
[c
] = ((Index
[c
]>88) ? 88 : Index
[c
]);
1220 dst
[i
*65*origChans
+ c
] = ((Sample
[c
] < 0) ? (Sample
[c
]/32768.0f
) : (Sample
[c
]/32767.0f
));
1225 for(j
= 1;j
< 65;j
+= 8)
1227 for(c
= 0;c
< origChans
;c
++)
1228 IMACode
[c
] = *(IMAData
++);
1230 for(k
= 0;k
< 8;k
++)
1232 for(c
= 0;c
< origChans
;c
++)
1234 Sample
[c
] += ((g_IMAStep_size
[Index
[c
]]*g_IMACodeword_4
[IMACode
[c
]&15])/8);
1235 Index
[c
] += g_IMAIndex_adjust_4
[IMACode
[c
]&15];
1237 if(Sample
[c
] < -32768) Sample
[c
] = -32768;
1238 else if(Sample
[c
] > 32767) Sample
[c
] = 32767;
1240 if(Index
[c
]<0) Index
[c
] = 0;
1241 else if(Index
[c
]>88) Index
[c
] = 88;
1243 dst
[(i
*65+j
+k
)*origChans
+ c
] = ((Sample
[c
] < 0) ? (Sample
[c
]/32768.0f
) : (Sample
[c
]/32767.0f
));
1251 static void ConvertDataMULaw(ALfloat
*dst
, const ALvoid
*src
, ALsizei len
)
1255 for(i
= 0;i
< len
;i
++)
1257 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
]];
1258 dst
[i
] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1262 static void ConvertDataMULawRear(ALfloat
*dst
, const ALvoid
*src
, ALsizei len
)
1266 for(i
= 0;i
< len
;i
+=4)
1270 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
/2+0]];
1271 dst
[i
+2] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1272 smp
= muLawDecompressionTable
[((ALubyte
*)src
)[i
/2+1]];
1273 dst
[i
+3] = ((smp
< 0) ? (smp
/32768.0f
) : (smp
/32767.0f
));
1278 * ReleaseALBuffers()
1280 * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
1282 ALvoid
ReleaseALBuffers(ALCdevice
*device
)
1285 for(i
= 0;i
< device
->BufferMap
.size
;i
++)
1287 ALbuffer
*temp
= device
->BufferMap
.array
[i
].value
;
1288 device
->BufferMap
.array
[i
].value
= NULL
;
1290 // Release sample data
1293 // Release Buffer structure
1294 ALTHUNK_REMOVEENTRY(temp
->buffer
);
1295 memset(temp
, 0, sizeof(ALbuffer
));