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
33 #include "alDatabuffer.h"
37 static ALenum
LoadData(ALbuffer
*ALBuf
, ALuint freq
, ALenum NewFormat
, ALsizei size
, enum SrcFmtChannels chans
, enum SrcFmtType type
, const ALvoid
*data
);
38 static void ConvertData(ALvoid
*dst
, enum FmtType dstType
, const ALvoid
*src
, enum SrcFmtType srcType
, ALsizei len
);
39 static void ConvertDataIMA4(ALvoid
*dst
, enum FmtType dstType
, const ALvoid
*src
, ALint chans
, ALsizei len
);
41 #define LookupBuffer(m, k) ((ALbuffer*)LookupUIntMapKey(&(m), (k)))
48 /* IMA ADPCM Stepsize table */
49 static const long g_IMAStep_size
[89] = {
50 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19,
51 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
52 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157,
53 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
54 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
55 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660,
56 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,
57 11487,12635,13899,15289,16818,18500,20350,22358,24633,27086,29794,
61 /* IMA4 ADPCM Codeword decode table */
62 static const long g_IMACodeword_4
[16] = {
63 1, 3, 5, 7, 9, 11, 13, 15,
64 -1,-3,-5,-7,-9,-11,-13,-15,
67 /* IMA4 ADPCM Step index adjust decode table */
68 static const long g_IMAIndex_adjust_4
[16] = {
69 -1,-1,-1,-1, 2, 4, 6, 8,
70 -1,-1,-1,-1, 2, 4, 6, 8
73 static const ALshort muLawDecompressionTable
[256] = {
74 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
75 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
76 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
77 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
78 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
79 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
80 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
81 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
82 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
83 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
84 -876, -844, -812, -780, -748, -716, -684, -652,
85 -620, -588, -556, -524, -492, -460, -428, -396,
86 -372, -356, -340, -324, -308, -292, -276, -260,
87 -244, -228, -212, -196, -180, -164, -148, -132,
88 -120, -112, -104, -96, -88, -80, -72, -64,
89 -56, -48, -40, -32, -24, -16, -8, 0,
90 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
91 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
92 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
93 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
94 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
95 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
96 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
97 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
98 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
99 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
100 876, 844, 812, 780, 748, 716, 684, 652,
101 620, 588, 556, 524, 492, 460, 428, 396,
102 372, 356, 340, 324, 308, 292, 276, 260,
103 244, 228, 212, 196, 180, 164, 148, 132,
104 120, 112, 104, 96, 88, 80, 72, 64,
105 56, 48, 40, 32, 24, 16, 8, 0
108 static const int muLawBias
= 0x84;
109 static const int muLawClip
= 32635;
111 static const char muLawCompressTable
[256] =
113 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
114 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
115 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
116 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
117 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
118 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
119 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
120 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
121 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
122 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
123 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
124 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
125 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
126 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
127 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
128 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
132 * alGenBuffers(ALsizei n, ALuint *buffers)
134 * Generates n AL Buffers, and stores the Buffers Names in the array pointed to by buffers
136 AL_API ALvoid AL_APIENTRY
alGenBuffers(ALsizei n
, ALuint
*buffers
)
141 Context
= GetContextSuspended();
144 // Check that we are actually generating some Buffers
145 if(n
< 0 || IsBadWritePtr((void*)buffers
, n
* sizeof(ALuint
)))
146 alSetError(Context
, AL_INVALID_VALUE
);
149 ALCdevice
*device
= Context
->Device
;
152 // Create all the new Buffers
155 ALbuffer
*buffer
= calloc(1, sizeof(ALbuffer
));
158 alSetError(Context
, AL_OUT_OF_MEMORY
);
159 alDeleteBuffers(i
, buffers
);
163 buffer
->buffer
= (ALuint
)ALTHUNK_ADDENTRY(buffer
);
164 err
= InsertUIntMapEntry(&device
->BufferMap
, buffer
->buffer
, buffer
);
165 if(err
!= AL_NO_ERROR
)
167 ALTHUNK_REMOVEENTRY(buffer
->buffer
);
168 memset(buffer
, 0, sizeof(ALbuffer
));
171 alSetError(Context
, err
);
172 alDeleteBuffers(i
, buffers
);
175 buffers
[i
++] = buffer
->buffer
;
179 ProcessContext(Context
);
183 * alDeleteBuffers(ALsizei n, ALuint *buffers)
185 * Deletes the n AL Buffers pointed to by buffers
187 AL_API ALvoid AL_APIENTRY
alDeleteBuffers(ALsizei n
, const ALuint
*buffers
)
195 Context
= GetContextSuspended();
199 device
= Context
->Device
;
200 /* Check we are actually Deleting some Buffers */
202 alSetError(Context
, AL_INVALID_VALUE
);
207 /* Check that all the buffers are valid and can actually be deleted */
213 /* Check for valid Buffer ID */
214 if((ALBuf
=LookupBuffer(device
->BufferMap
, buffers
[i
])) == NULL
)
216 alSetError(Context
, AL_INVALID_NAME
);
220 else if(ALBuf
->refcount
!= 0)
222 /* Buffer still in use, cannot be deleted */
223 alSetError(Context
, AL_INVALID_OPERATION
);
230 /* If all the Buffers were valid (and have Reference Counts of 0), then we can delete them */
235 if((ALBuf
=LookupBuffer(device
->BufferMap
, buffers
[i
])) == NULL
)
238 /* Release the memory used to store audio data */
241 /* Release buffer structure */
242 RemoveUIntMapKey(&device
->BufferMap
, ALBuf
->buffer
);
243 ALTHUNK_REMOVEENTRY(ALBuf
->buffer
);
245 memset(ALBuf
, 0, sizeof(ALbuffer
));
250 ProcessContext(Context
);
254 * alIsBuffer(ALuint buffer)
256 * Checks if buffer is a valid Buffer Name
258 AL_API ALboolean AL_APIENTRY
alIsBuffer(ALuint buffer
)
263 Context
= GetContextSuspended();
264 if(!Context
) return AL_FALSE
;
266 result
= ((!buffer
|| LookupBuffer(Context
->Device
->BufferMap
, buffer
)) ?
269 ProcessContext(Context
);
275 * alBufferData(ALuint buffer,ALenum format,const ALvoid *data,ALsizei size,ALsizei freq)
277 * Fill buffer with audio data
279 AL_API ALvoid AL_APIENTRY
alBufferData(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei size
,ALsizei freq
)
281 enum SrcFmtChannels SrcChannels
;
282 enum SrcFmtType SrcType
;
288 Context
= GetContextSuspended();
291 if(Context
->SampleSource
)
295 if(Context
->SampleSource
->state
== MAPPED
)
297 alSetError(Context
, AL_INVALID_OPERATION
);
298 ProcessContext(Context
);
302 offset
= (const ALubyte
*)data
- (ALubyte
*)NULL
;
303 data
= Context
->SampleSource
->data
+ offset
;
306 device
= Context
->Device
;
307 if((ALBuf
=LookupBuffer(device
->BufferMap
, buffer
)) == NULL
)
308 alSetError(Context
, AL_INVALID_NAME
);
309 else if(ALBuf
->refcount
!= 0)
310 alSetError(Context
, AL_INVALID_VALUE
);
311 else if(size
< 0 || freq
< 0)
312 alSetError(Context
, AL_INVALID_VALUE
);
313 else if(DecomposeInputFormat(format
, &SrcChannels
, &SrcType
) == AL_FALSE
)
314 alSetError(Context
, AL_INVALID_ENUM
);
324 err
= LoadData(ALBuf
, freq
, format
, size
, SrcChannels
, SrcType
, data
);
325 if(err
!= AL_NO_ERROR
)
326 alSetError(Context
, err
);
330 ALenum NewFormat
= AL_FORMAT_MONO_FLOAT32
;
333 case SrcFmtMono
: NewFormat
= AL_FORMAT_MONO_FLOAT32
; break;
334 case SrcFmtStereo
: NewFormat
= AL_FORMAT_STEREO_FLOAT32
; break;
335 case SrcFmtRear
: NewFormat
= AL_FORMAT_REAR32
; break;
336 case SrcFmtQuad
: NewFormat
= AL_FORMAT_QUAD32
; break;
337 case SrcFmtX51
: NewFormat
= AL_FORMAT_51CHN32
; break;
338 case SrcFmtX61
: NewFormat
= AL_FORMAT_61CHN32
; break;
339 case SrcFmtX71
: NewFormat
= AL_FORMAT_71CHN32
; break;
341 err
= LoadData(ALBuf
, freq
, NewFormat
, size
, SrcChannels
, SrcType
, data
);
342 if(err
!= AL_NO_ERROR
)
343 alSetError(Context
, err
);
348 ALenum NewFormat
= AL_FORMAT_MONO16
;
351 case SrcFmtMono
: NewFormat
= AL_FORMAT_MONO16
; break;
352 case SrcFmtStereo
: NewFormat
= AL_FORMAT_STEREO16
; break;
353 case SrcFmtRear
: NewFormat
= AL_FORMAT_REAR16
; break;
354 case SrcFmtQuad
: NewFormat
= AL_FORMAT_QUAD16
; break;
355 case SrcFmtX51
: NewFormat
= AL_FORMAT_51CHN16
; break;
356 case SrcFmtX61
: NewFormat
= AL_FORMAT_61CHN16
; break;
357 case SrcFmtX71
: NewFormat
= AL_FORMAT_71CHN16
; break;
359 err
= LoadData(ALBuf
, freq
, NewFormat
, size
, SrcChannels
, SrcType
, data
);
360 if(err
!= AL_NO_ERROR
)
361 alSetError(Context
, err
);
365 ProcessContext(Context
);
369 * alBufferSubDataSOFT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
371 * Update buffer's audio data
373 AL_API ALvoid AL_APIENTRY
alBufferSubDataSOFT(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei offset
,ALsizei length
)
375 enum SrcFmtChannels SrcChannels
;
376 enum SrcFmtType SrcType
;
381 Context
= GetContextSuspended();
384 if(Context
->SampleSource
)
388 if(Context
->SampleSource
->state
== MAPPED
)
390 alSetError(Context
, AL_INVALID_OPERATION
);
391 ProcessContext(Context
);
395 offset
= (const ALubyte
*)data
- (ALubyte
*)NULL
;
396 data
= Context
->SampleSource
->data
+ offset
;
399 device
= Context
->Device
;
400 if((ALBuf
=LookupBuffer(device
->BufferMap
, buffer
)) == NULL
)
401 alSetError(Context
, AL_INVALID_NAME
);
402 else if(length
< 0 || offset
< 0 || (length
> 0 && data
== NULL
))
403 alSetError(Context
, AL_INVALID_VALUE
);
404 else if(DecomposeInputFormat(format
, &SrcChannels
, &SrcType
) == AL_FALSE
||
405 SrcChannels
!= ALBuf
->OriginalChannels
||
406 SrcType
!= ALBuf
->OriginalType
)
407 alSetError(Context
, AL_INVALID_ENUM
);
408 else if(offset
> ALBuf
->OriginalSize
||
409 length
> ALBuf
->OriginalSize
-offset
||
410 (offset
%ALBuf
->OriginalAlign
) != 0 ||
411 (length
%ALBuf
->OriginalAlign
) != 0)
412 alSetError(Context
, AL_INVALID_VALUE
);
415 if(SrcType
== SrcFmtIMA4
)
417 ALuint Channels
= ChannelsFromFmt(ALBuf
->FmtChannels
);
418 ALuint Bytes
= BytesFromFmt(ALBuf
->FmtType
);
420 /* offset -> byte offset, length -> block count */
424 length
/= ALBuf
->OriginalAlign
;
426 ConvertDataIMA4(&((ALubyte
*)ALBuf
->data
)[offset
], ALBuf
->FmtType
,
427 data
, Channels
, length
);
431 ALuint OldBytes
= BytesFromFmt(SrcType
);
432 ALuint Bytes
= BytesFromFmt(ALBuf
->FmtType
);
438 ConvertData(&((ALubyte
*)ALBuf
->data
)[offset
], ALBuf
->FmtType
,
439 data
, SrcType
, length
);
443 ProcessContext(Context
);
446 AL_API ALvoid AL_APIENTRY
alBufferSubDataEXT(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei offset
,ALsizei length
)
448 alBufferSubDataSOFT(buffer
, format
, data
, offset
, length
);
452 AL_API
void AL_APIENTRY
alBufferf(ALuint buffer
, ALenum eParam
, ALfloat flValue
)
454 ALCcontext
*pContext
;
459 pContext
= GetContextSuspended();
460 if(!pContext
) return;
462 device
= pContext
->Device
;
463 if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
464 alSetError(pContext
, AL_INVALID_NAME
);
470 alSetError(pContext
, AL_INVALID_ENUM
);
475 ProcessContext(pContext
);
479 AL_API
void AL_APIENTRY
alBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat flValue1
, ALfloat flValue2
, ALfloat flValue3
)
481 ALCcontext
*pContext
;
488 pContext
= GetContextSuspended();
489 if(!pContext
) return;
491 device
= pContext
->Device
;
492 if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
493 alSetError(pContext
, AL_INVALID_NAME
);
499 alSetError(pContext
, AL_INVALID_ENUM
);
504 ProcessContext(pContext
);
508 AL_API
void AL_APIENTRY
alBufferfv(ALuint buffer
, ALenum eParam
, const ALfloat
* flValues
)
510 ALCcontext
*pContext
;
513 pContext
= GetContextSuspended();
514 if(!pContext
) return;
516 device
= pContext
->Device
;
518 alSetError(pContext
, AL_INVALID_VALUE
);
519 else if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
520 alSetError(pContext
, AL_INVALID_NAME
);
526 alSetError(pContext
, AL_INVALID_ENUM
);
531 ProcessContext(pContext
);
535 AL_API
void AL_APIENTRY
alBufferi(ALuint buffer
, ALenum eParam
, ALint lValue
)
537 ALCcontext
*pContext
;
542 pContext
= GetContextSuspended();
543 if(!pContext
) return;
545 device
= pContext
->Device
;
546 if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
547 alSetError(pContext
, AL_INVALID_NAME
);
553 alSetError(pContext
, AL_INVALID_ENUM
);
558 ProcessContext(pContext
);
562 AL_API
void AL_APIENTRY
alBuffer3i( ALuint buffer
, ALenum eParam
, ALint lValue1
, ALint lValue2
, ALint lValue3
)
564 ALCcontext
*pContext
;
571 pContext
= GetContextSuspended();
572 if(!pContext
) return;
574 device
= pContext
->Device
;
575 if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
576 alSetError(pContext
, AL_INVALID_NAME
);
582 alSetError(pContext
, AL_INVALID_ENUM
);
587 ProcessContext(pContext
);
591 AL_API
void AL_APIENTRY
alBufferiv(ALuint buffer
, ALenum eParam
, const ALint
* plValues
)
593 ALCcontext
*pContext
;
597 pContext
= GetContextSuspended();
598 if(!pContext
) return;
600 device
= pContext
->Device
;
602 alSetError(pContext
, AL_INVALID_VALUE
);
603 else if((ALBuf
=LookupBuffer(device
->BufferMap
, buffer
)) == NULL
)
604 alSetError(pContext
, AL_INVALID_NAME
);
610 if(ALBuf
->refcount
> 0)
611 alSetError(pContext
, AL_INVALID_OPERATION
);
612 else if(plValues
[0] < 0 || plValues
[1] < 0 ||
613 plValues
[0] >= plValues
[1] || ALBuf
->size
== 0)
614 alSetError(pContext
, AL_INVALID_VALUE
);
617 ALint maxlen
= ALBuf
->size
/
618 FrameSizeFromFmt(ALBuf
->FmtChannels
, ALBuf
->FmtType
);
619 if(plValues
[0] > maxlen
|| plValues
[1] > maxlen
)
620 alSetError(pContext
, AL_INVALID_VALUE
);
623 ALBuf
->LoopStart
= plValues
[0];
624 ALBuf
->LoopEnd
= plValues
[1];
630 alSetError(pContext
, AL_INVALID_ENUM
);
635 ProcessContext(pContext
);
639 AL_API ALvoid AL_APIENTRY
alGetBufferf(ALuint buffer
, ALenum eParam
, ALfloat
*pflValue
)
641 ALCcontext
*pContext
;
644 pContext
= GetContextSuspended();
645 if(!pContext
) return;
647 device
= pContext
->Device
;
649 alSetError(pContext
, AL_INVALID_VALUE
);
650 else if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
651 alSetError(pContext
, AL_INVALID_NAME
);
657 alSetError(pContext
, AL_INVALID_ENUM
);
662 ProcessContext(pContext
);
666 AL_API
void AL_APIENTRY
alGetBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat
* pflValue1
, ALfloat
* pflValue2
, ALfloat
* pflValue3
)
668 ALCcontext
*pContext
;
671 pContext
= GetContextSuspended();
672 if(!pContext
) return;
674 device
= pContext
->Device
;
675 if(!pflValue1
|| !pflValue2
|| !pflValue3
)
676 alSetError(pContext
, AL_INVALID_VALUE
);
677 else if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
678 alSetError(pContext
, AL_INVALID_NAME
);
684 alSetError(pContext
, AL_INVALID_ENUM
);
689 ProcessContext(pContext
);
693 AL_API
void AL_APIENTRY
alGetBufferfv(ALuint buffer
, ALenum eParam
, ALfloat
* pflValues
)
695 ALCcontext
*pContext
;
698 pContext
= GetContextSuspended();
699 if(!pContext
) return;
701 device
= pContext
->Device
;
703 alSetError(pContext
, AL_INVALID_VALUE
);
704 else if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
705 alSetError(pContext
, AL_INVALID_NAME
);
711 alSetError(pContext
, AL_INVALID_ENUM
);
716 ProcessContext(pContext
);
720 AL_API ALvoid AL_APIENTRY
alGetBufferi(ALuint buffer
, ALenum eParam
, ALint
*plValue
)
722 ALCcontext
*pContext
;
726 pContext
= GetContextSuspended();
727 if(!pContext
) return;
729 device
= pContext
->Device
;
731 alSetError(pContext
, AL_INVALID_VALUE
);
732 else if((pBuffer
=LookupBuffer(device
->BufferMap
, buffer
)) == NULL
)
733 alSetError(pContext
, AL_INVALID_NAME
);
739 *plValue
= pBuffer
->Frequency
;
743 *plValue
= BytesFromFmt(pBuffer
->FmtType
) * 8;
747 *plValue
= ChannelsFromFmt(pBuffer
->FmtChannels
);
751 *plValue
= pBuffer
->size
;
755 alSetError(pContext
, AL_INVALID_ENUM
);
760 ProcessContext(pContext
);
764 AL_API
void AL_APIENTRY
alGetBuffer3i(ALuint buffer
, ALenum eParam
, ALint
* plValue1
, ALint
* plValue2
, ALint
* plValue3
)
766 ALCcontext
*pContext
;
769 pContext
= GetContextSuspended();
770 if(!pContext
) return;
772 device
= pContext
->Device
;
773 if(!plValue1
|| !plValue2
|| !plValue3
)
774 alSetError(pContext
, AL_INVALID_VALUE
);
775 else if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
776 alSetError(pContext
, AL_INVALID_NAME
);
782 alSetError(pContext
, AL_INVALID_ENUM
);
787 ProcessContext(pContext
);
791 AL_API
void AL_APIENTRY
alGetBufferiv(ALuint buffer
, ALenum eParam
, ALint
* plValues
)
793 ALCcontext
*pContext
;
797 pContext
= GetContextSuspended();
798 if(!pContext
) return;
800 device
= pContext
->Device
;
802 alSetError(pContext
, AL_INVALID_VALUE
);
803 else if((ALBuf
=LookupBuffer(device
->BufferMap
, buffer
)) == NULL
)
804 alSetError(pContext
, AL_INVALID_NAME
);
813 alGetBufferi(buffer
, eParam
, plValues
);
817 plValues
[0] = ALBuf
->LoopStart
;
818 plValues
[1] = ALBuf
->LoopEnd
;
822 alSetError(pContext
, AL_INVALID_ENUM
);
827 ProcessContext(pContext
);
831 typedef ALubyte ALmulaw
;
833 static __inline ALshort
DecodeMuLaw(ALmulaw val
)
834 { return muLawDecompressionTable
[val
]; }
836 static ALmulaw
EncodeMuLaw(ALshort val
)
838 ALint mant
, exp
, sign
;
840 sign
= (val
>>8) & 0x80;
841 if(sign
) val
= (ALshort
)-val
;
843 val
= min(val
, muLawClip
);
846 exp
= muLawCompressTable
[(val
>>7) & 0xff];
847 mant
= (val
>> (exp
+3)) & 0x0f;
849 return ~(sign
| (exp
<<4) | mant
);
852 static void DecodeIMA4Block(ALshort
*dst
, const ALubyte
*IMAData
, ALint numchans
)
854 ALint Sample
[2],Index
[2];
858 for(c
= 0;c
< numchans
;c
++)
860 Sample
[c
] = *(IMAData
++);
861 Sample
[c
] |= *(IMAData
++) << 8;
862 Sample
[c
] = (Sample
[c
]^0x8000) - 32768;
863 Index
[c
] = *(IMAData
++);
864 Index
[c
] |= *(IMAData
++) << 8;
865 Index
[c
] = (Index
[c
]^0x8000) - 32768;
867 Index
[c
] = ((Index
[c
]<0) ? 0 : Index
[c
]);
868 Index
[c
] = ((Index
[c
]>88) ? 88 : Index
[c
]);
876 for(c
= 0;c
< numchans
;c
++)
878 IMACode
[c
] = *(IMAData
++);
879 IMACode
[c
] |= *(IMAData
++) << 8;
880 IMACode
[c
] |= *(IMAData
++) << 16;
881 IMACode
[c
] |= *(IMAData
++) << 24;
884 for(k
= 0;k
< 8;k
++,j
++)
886 for(c
= 0;c
< numchans
;c
++)
888 Sample
[c
] += g_IMAStep_size
[Index
[c
]] *
889 g_IMACodeword_4
[IMACode
[c
]&15] / 8;
890 Index
[c
] += g_IMAIndex_adjust_4
[IMACode
[c
]&15];
892 if(Sample
[c
] < -32768) Sample
[c
] = -32768;
893 else if(Sample
[c
] > 32767) Sample
[c
] = 32767;
895 if(Index
[c
] < 0) Index
[c
] = 0;
896 else if(Index
[c
] > 88) Index
[c
] = 88;
898 dst
[j
*numchans
+ c
] = Sample
[c
];
906 static __inline ALbyte
Conv_ALbyte_ALbyte(ALbyte val
)
908 static __inline ALbyte
Conv_ALbyte_ALubyte(ALubyte val
)
910 static __inline ALbyte
Conv_ALbyte_ALshort(ALshort val
)
912 static __inline ALbyte
Conv_ALbyte_ALushort(ALushort val
)
913 { return (val
>>8)-128; }
914 static __inline ALbyte
Conv_ALbyte_ALint(ALint val
)
916 static __inline ALbyte
Conv_ALbyte_ALuint(ALuint val
)
917 { return (val
>>24)-128; }
918 static __inline ALbyte
Conv_ALbyte_ALfloat(ALfloat val
)
920 if(val
>= 1.0f
) return 127;
921 if(val
<= -1.0f
) return -128;
922 return (ALint
)(val
* 127.0f
);
924 static __inline ALbyte
Conv_ALbyte_ALdouble(ALdouble val
)
926 if(val
>= 1.0) return 127;
927 if(val
<= -1.0) return -128;
928 return (ALint
)(val
* 127.0);
930 static __inline ALbyte
Conv_ALbyte_ALmulaw(ALmulaw val
)
931 { return Conv_ALbyte_ALshort(DecodeMuLaw(val
)); }
933 static __inline ALubyte
Conv_ALubyte_ALbyte(ALbyte val
)
935 static __inline ALubyte
Conv_ALubyte_ALubyte(ALubyte val
)
937 static __inline ALubyte
Conv_ALubyte_ALshort(ALshort val
)
938 { return (val
>>8)+128; }
939 static __inline ALubyte
Conv_ALubyte_ALushort(ALushort val
)
941 static __inline ALubyte
Conv_ALubyte_ALint(ALint val
)
942 { return (val
>>24)+128; }
943 static __inline ALubyte
Conv_ALubyte_ALuint(ALuint val
)
945 static __inline ALubyte
Conv_ALubyte_ALfloat(ALfloat val
)
947 if(val
>= 1.0f
) return 255;
948 if(val
<= -1.0f
) return 0;
949 return (ALint
)(val
* 127.0f
) + 128;
951 static __inline ALubyte
Conv_ALubyte_ALdouble(ALdouble val
)
953 if(val
>= 1.0) return 255;
954 if(val
<= -1.0) return 0;
955 return (ALint
)(val
* 127.0) + 128;
957 static __inline ALubyte
Conv_ALubyte_ALmulaw(ALmulaw val
)
958 { return Conv_ALubyte_ALshort(DecodeMuLaw(val
)); }
960 static __inline ALshort
Conv_ALshort_ALbyte(ALbyte val
)
962 static __inline ALshort
Conv_ALshort_ALubyte(ALubyte val
)
963 { return (val
-128)<<8; }
964 static __inline ALshort
Conv_ALshort_ALshort(ALshort val
)
966 static __inline ALshort
Conv_ALshort_ALushort(ALushort val
)
967 { return val
^0x8000; }
968 static __inline ALshort
Conv_ALshort_ALint(ALint val
)
970 static __inline ALshort
Conv_ALshort_ALuint(ALuint val
)
971 { return (val
>>16)-32768; }
972 static __inline ALshort
Conv_ALshort_ALfloat(ALfloat val
)
974 if(val
>= 1.0f
) return 32767;
975 if(val
<= -1.0f
) return -32768;
976 return (ALint
)(val
* 32767.0f
);
978 static __inline ALshort
Conv_ALshort_ALdouble(ALdouble val
)
980 if(val
>= 1.0) return 32767;
981 if(val
<= -1.0) return -32768;
982 return (ALint
)(val
* 32767.0);
984 static __inline ALshort
Conv_ALshort_ALmulaw(ALmulaw val
)
985 { return Conv_ALshort_ALshort(DecodeMuLaw(val
)); }
987 static __inline ALushort
Conv_ALushort_ALbyte(ALbyte val
)
988 { return (val
+128)<<8; }
989 static __inline ALushort
Conv_ALushort_ALubyte(ALubyte val
)
991 static __inline ALushort
Conv_ALushort_ALshort(ALshort val
)
992 { return val
^0x8000; }
993 static __inline ALushort
Conv_ALushort_ALushort(ALushort val
)
995 static __inline ALushort
Conv_ALushort_ALint(ALint val
)
996 { return (val
>>16)+32768; }
997 static __inline ALushort
Conv_ALushort_ALuint(ALuint val
)
999 static __inline ALushort
Conv_ALushort_ALfloat(ALfloat val
)
1001 if(val
>= 1.0f
) return 65535;
1002 if(val
<= -1.0f
) return 0;
1003 return (ALint
)(val
* 32767.0f
) + 32768;
1005 static __inline ALushort
Conv_ALushort_ALdouble(ALdouble val
)
1007 if(val
>= 1.0) return 65535;
1008 if(val
<= -1.0) return 0;
1009 return (ALint
)(val
* 32767.0) + 32768;
1011 static __inline ALushort
Conv_ALushort_ALmulaw(ALmulaw val
)
1012 { return Conv_ALushort_ALshort(DecodeMuLaw(val
)); }
1014 static __inline ALint
Conv_ALint_ALbyte(ALbyte val
)
1016 static __inline ALint
Conv_ALint_ALubyte(ALubyte val
)
1017 { return (val
-128)<<24; }
1018 static __inline ALint
Conv_ALint_ALshort(ALshort val
)
1020 static __inline ALint
Conv_ALint_ALushort(ALushort val
)
1021 { return (val
-32768)<<16; }
1022 static __inline ALint
Conv_ALint_ALint(ALint val
)
1024 static __inline ALint
Conv_ALint_ALuint(ALuint val
)
1025 { return val
-2147483648u; }
1026 static __inline ALint
Conv_ALint_ALfloat(ALfloat val
)
1028 if(val
>= 1.0f
) return 2147483647;
1029 if(val
<= -1.0f
) return -2147483648u;
1030 return (ALint
)(val
* 2147483647.0);
1032 static __inline ALint
Conv_ALint_ALdouble(ALdouble val
)
1034 if(val
>= 1.0) return 2147483647;
1035 if(val
<= -1.0) return -2147483648u;
1036 return (ALint
)(val
* 2147483647.0);
1038 static __inline ALint
Conv_ALint_ALmulaw(ALmulaw val
)
1039 { return Conv_ALint_ALshort(DecodeMuLaw(val
)); }
1041 static __inline ALuint
Conv_ALuint_ALbyte(ALbyte val
)
1042 { return (val
+128)<<24; }
1043 static __inline ALuint
Conv_ALuint_ALubyte(ALubyte val
)
1045 static __inline ALuint
Conv_ALuint_ALshort(ALshort val
)
1046 { return (val
+32768)<<16; }
1047 static __inline ALuint
Conv_ALuint_ALushort(ALushort val
)
1049 static __inline ALuint
Conv_ALuint_ALint(ALint val
)
1050 { return val
+2147483648u; }
1051 static __inline ALuint
Conv_ALuint_ALuint(ALuint val
)
1053 static __inline ALuint
Conv_ALuint_ALfloat(ALfloat val
)
1055 if(val
>= 1.0f
) return 4294967295u;
1056 if(val
<= -1.0f
) return 0;
1057 return (ALint
)(val
* 2147483647.0) + 2147483648u;
1059 static __inline ALuint
Conv_ALuint_ALdouble(ALdouble val
)
1061 if(val
>= 1.0) return 4294967295u;
1062 if(val
<= -1.0) return 0;
1063 return (ALint
)(val
* 2147483647.0) + 2147483648u;
1065 static __inline ALuint
Conv_ALuint_ALmulaw(ALmulaw val
)
1066 { return Conv_ALuint_ALshort(DecodeMuLaw(val
)); }
1068 static __inline ALfloat
Conv_ALfloat_ALbyte(ALbyte val
)
1069 { return val
* (1.0f
/127.0f
); }
1070 static __inline ALfloat
Conv_ALfloat_ALubyte(ALubyte val
)
1071 { return (val
-128) * (1.0f
/127.0f
); }
1072 static __inline ALfloat
Conv_ALfloat_ALshort(ALshort val
)
1073 { return val
* (1.0f
/32767.0f
); }
1074 static __inline ALfloat
Conv_ALfloat_ALushort(ALushort val
)
1075 { return (val
-32768) * (1.0f
/32767.0f
); }
1076 static __inline ALfloat
Conv_ALfloat_ALint(ALint val
)
1077 { return val
* (1.0/2147483647.0); }
1078 static __inline ALfloat
Conv_ALfloat_ALuint(ALuint val
)
1079 { return (ALint
)(val
-2147483648u) * (1.0/2147483647.0); }
1080 static __inline ALfloat
Conv_ALfloat_ALfloat(ALfloat val
)
1082 static __inline ALfloat
Conv_ALfloat_ALdouble(ALdouble val
)
1084 static __inline ALfloat
Conv_ALfloat_ALmulaw(ALmulaw val
)
1085 { return Conv_ALfloat_ALshort(DecodeMuLaw(val
)); }
1087 static __inline ALdouble
Conv_ALdouble_ALbyte(ALbyte val
)
1088 { return val
* (1.0/127.0); }
1089 static __inline ALdouble
Conv_ALdouble_ALubyte(ALubyte val
)
1090 { return (val
-128) * (1.0/127.0); }
1091 static __inline ALdouble
Conv_ALdouble_ALshort(ALshort val
)
1092 { return val
* (1.0/32767.0); }
1093 static __inline ALdouble
Conv_ALdouble_ALushort(ALushort val
)
1094 { return (val
-32768) * (1.0/32767.0); }
1095 static __inline ALdouble
Conv_ALdouble_ALint(ALint val
)
1096 { return val
* (1.0/2147483647.0); }
1097 static __inline ALdouble
Conv_ALdouble_ALuint(ALuint val
)
1098 { return (ALint
)(val
-2147483648u) * (1.0/2147483647.0); }
1099 static __inline ALdouble
Conv_ALdouble_ALfloat(ALfloat val
)
1101 static __inline ALdouble
Conv_ALdouble_ALdouble(ALdouble val
)
1103 static __inline ALdouble
Conv_ALdouble_ALmulaw(ALmulaw val
)
1104 { return Conv_ALdouble_ALshort(DecodeMuLaw(val
)); }
1106 #define DECL_TEMPLATE(T) \
1107 static __inline ALmulaw Conv_ALmulaw_##T(T val) \
1108 { return EncodeMuLaw(Conv_ALshort_##T(val)); }
1110 DECL_TEMPLATE(ALbyte
)
1111 DECL_TEMPLATE(ALubyte
)
1112 DECL_TEMPLATE(ALshort
)
1113 DECL_TEMPLATE(ALushort
)
1114 DECL_TEMPLATE(ALint
)
1115 DECL_TEMPLATE(ALuint
)
1116 DECL_TEMPLATE(ALfloat
)
1117 DECL_TEMPLATE(ALdouble
)
1118 DECL_TEMPLATE(ALmulaw
)
1120 #undef DECL_TEMPLATE
1122 #define DECL_TEMPLATE(T1, T2) \
1123 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint len) \
1126 for(i = 0;i < len;i++) \
1127 *(dst++) = Conv_##T1##_##T2(*(src++)); \
1130 DECL_TEMPLATE(ALbyte
, ALbyte
)
1131 DECL_TEMPLATE(ALbyte
, ALubyte
)
1132 DECL_TEMPLATE(ALbyte
, ALshort
)
1133 DECL_TEMPLATE(ALbyte
, ALushort
)
1134 DECL_TEMPLATE(ALbyte
, ALint
)
1135 DECL_TEMPLATE(ALbyte
, ALuint
)
1136 DECL_TEMPLATE(ALbyte
, ALfloat
)
1137 DECL_TEMPLATE(ALbyte
, ALdouble
)
1138 DECL_TEMPLATE(ALbyte
, ALmulaw
)
1140 DECL_TEMPLATE(ALubyte
, ALbyte
)
1141 DECL_TEMPLATE(ALubyte
, ALubyte
)
1142 DECL_TEMPLATE(ALubyte
, ALshort
)
1143 DECL_TEMPLATE(ALubyte
, ALushort
)
1144 DECL_TEMPLATE(ALubyte
, ALint
)
1145 DECL_TEMPLATE(ALubyte
, ALuint
)
1146 DECL_TEMPLATE(ALubyte
, ALfloat
)
1147 DECL_TEMPLATE(ALubyte
, ALdouble
)
1148 DECL_TEMPLATE(ALubyte
, ALmulaw
)
1150 DECL_TEMPLATE(ALshort
, ALbyte
)
1151 DECL_TEMPLATE(ALshort
, ALubyte
)
1152 DECL_TEMPLATE(ALshort
, ALshort
)
1153 DECL_TEMPLATE(ALshort
, ALushort
)
1154 DECL_TEMPLATE(ALshort
, ALint
)
1155 DECL_TEMPLATE(ALshort
, ALuint
)
1156 DECL_TEMPLATE(ALshort
, ALfloat
)
1157 DECL_TEMPLATE(ALshort
, ALdouble
)
1158 DECL_TEMPLATE(ALshort
, ALmulaw
)
1160 DECL_TEMPLATE(ALushort
, ALbyte
)
1161 DECL_TEMPLATE(ALushort
, ALubyte
)
1162 DECL_TEMPLATE(ALushort
, ALshort
)
1163 DECL_TEMPLATE(ALushort
, ALushort
)
1164 DECL_TEMPLATE(ALushort
, ALint
)
1165 DECL_TEMPLATE(ALushort
, ALuint
)
1166 DECL_TEMPLATE(ALushort
, ALfloat
)
1167 DECL_TEMPLATE(ALushort
, ALdouble
)
1168 DECL_TEMPLATE(ALushort
, ALmulaw
)
1170 DECL_TEMPLATE(ALint
, ALbyte
)
1171 DECL_TEMPLATE(ALint
, ALubyte
)
1172 DECL_TEMPLATE(ALint
, ALshort
)
1173 DECL_TEMPLATE(ALint
, ALushort
)
1174 DECL_TEMPLATE(ALint
, ALint
)
1175 DECL_TEMPLATE(ALint
, ALuint
)
1176 DECL_TEMPLATE(ALint
, ALfloat
)
1177 DECL_TEMPLATE(ALint
, ALdouble
)
1178 DECL_TEMPLATE(ALint
, ALmulaw
)
1180 DECL_TEMPLATE(ALuint
, ALbyte
)
1181 DECL_TEMPLATE(ALuint
, ALubyte
)
1182 DECL_TEMPLATE(ALuint
, ALshort
)
1183 DECL_TEMPLATE(ALuint
, ALushort
)
1184 DECL_TEMPLATE(ALuint
, ALint
)
1185 DECL_TEMPLATE(ALuint
, ALuint
)
1186 DECL_TEMPLATE(ALuint
, ALfloat
)
1187 DECL_TEMPLATE(ALuint
, ALdouble
)
1188 DECL_TEMPLATE(ALuint
, ALmulaw
)
1190 DECL_TEMPLATE(ALfloat
, ALbyte
)
1191 DECL_TEMPLATE(ALfloat
, ALubyte
)
1192 DECL_TEMPLATE(ALfloat
, ALshort
)
1193 DECL_TEMPLATE(ALfloat
, ALushort
)
1194 DECL_TEMPLATE(ALfloat
, ALint
)
1195 DECL_TEMPLATE(ALfloat
, ALuint
)
1196 DECL_TEMPLATE(ALfloat
, ALfloat
)
1197 DECL_TEMPLATE(ALfloat
, ALdouble
)
1198 DECL_TEMPLATE(ALfloat
, ALmulaw
)
1200 DECL_TEMPLATE(ALdouble
, ALbyte
)
1201 DECL_TEMPLATE(ALdouble
, ALubyte
)
1202 DECL_TEMPLATE(ALdouble
, ALshort
)
1203 DECL_TEMPLATE(ALdouble
, ALushort
)
1204 DECL_TEMPLATE(ALdouble
, ALint
)
1205 DECL_TEMPLATE(ALdouble
, ALuint
)
1206 DECL_TEMPLATE(ALdouble
, ALfloat
)
1207 DECL_TEMPLATE(ALdouble
, ALdouble
)
1208 DECL_TEMPLATE(ALdouble
, ALmulaw
)
1210 DECL_TEMPLATE(ALmulaw
, ALbyte
)
1211 DECL_TEMPLATE(ALmulaw
, ALubyte
)
1212 DECL_TEMPLATE(ALmulaw
, ALshort
)
1213 DECL_TEMPLATE(ALmulaw
, ALushort
)
1214 DECL_TEMPLATE(ALmulaw
, ALint
)
1215 DECL_TEMPLATE(ALmulaw
, ALuint
)
1216 DECL_TEMPLATE(ALmulaw
, ALfloat
)
1217 DECL_TEMPLATE(ALmulaw
, ALdouble
)
1218 DECL_TEMPLATE(ALmulaw
, ALmulaw
)
1220 #undef DECL_TEMPLATE
1222 #define DECL_TEMPLATE(T) \
1223 static void Convert_##T##_IMA4(T *dst, const ALubyte *src, ALuint numchans, \
1226 ALshort tmp[65*2]; /* Max samples an IMA4 frame can be */ \
1228 for(i = 0;i < numblocks;i++) \
1230 DecodeIMA4Block(tmp, src, numchans); \
1231 src += 36*numchans; \
1232 for(j = 0;j < 65*numchans;j++) \
1233 *(dst++) = Conv_##T##_ALshort(tmp[j]); \
1237 DECL_TEMPLATE(ALbyte
)
1238 DECL_TEMPLATE(ALubyte
)
1239 DECL_TEMPLATE(ALshort
)
1240 DECL_TEMPLATE(ALushort
)
1241 DECL_TEMPLATE(ALint
)
1242 DECL_TEMPLATE(ALuint
)
1243 DECL_TEMPLATE(ALfloat
)
1244 DECL_TEMPLATE(ALdouble
)
1245 DECL_TEMPLATE(ALmulaw
)
1247 #undef DECL_TEMPLATE
1249 #define DECL_TEMPLATE(T) \
1250 static void Convert_##T(T *dst, const ALvoid *src, enum SrcFmtType srcType, \
1256 Convert_##T##_ALbyte(dst, src, len); \
1259 Convert_##T##_ALubyte(dst, src, len); \
1262 Convert_##T##_ALshort(dst, src, len); \
1264 case SrcFmtUShort: \
1265 Convert_##T##_ALushort(dst, src, len); \
1268 Convert_##T##_ALint(dst, src, len); \
1271 Convert_##T##_ALuint(dst, src, len); \
1274 Convert_##T##_ALfloat(dst, src, len); \
1276 case SrcFmtDouble: \
1277 Convert_##T##_ALdouble(dst, src, len); \
1280 Convert_##T##_ALmulaw(dst, src, len); \
1283 break; /* not handled here */ \
1287 DECL_TEMPLATE(ALbyte
)
1288 DECL_TEMPLATE(ALubyte
)
1289 DECL_TEMPLATE(ALshort
)
1290 DECL_TEMPLATE(ALushort
)
1291 DECL_TEMPLATE(ALint
)
1292 DECL_TEMPLATE(ALuint
)
1293 DECL_TEMPLATE(ALfloat
)
1294 DECL_TEMPLATE(ALdouble
)
1295 DECL_TEMPLATE(ALmulaw
)
1297 #undef DECL_TEMPLATE
1300 static void ConvertData(ALvoid
*dst
, enum FmtType dstType
, const ALvoid
*src
, enum SrcFmtType srcType
, ALsizei len
)
1304 (void)Convert_ALbyte
;
1306 Convert_ALubyte(dst
, src
, srcType
, len
);
1309 Convert_ALshort(dst
, src
, srcType
, len
);
1311 (void)Convert_ALushort
;
1312 (void)Convert_ALint
;
1313 (void)Convert_ALuint
;
1315 Convert_ALfloat(dst
, src
, srcType
, len
);
1317 (void)Convert_ALdouble
;
1318 (void)Convert_ALmulaw
;
1322 static void ConvertDataIMA4(ALvoid
*dst
, enum FmtType dstType
, const ALvoid
*src
, ALint chans
, ALsizei len
)
1326 (void)Convert_ALbyte_IMA4
;
1328 Convert_ALubyte_IMA4(dst
, src
, chans
, len
);
1331 Convert_ALshort_IMA4(dst
, src
, chans
, len
);
1333 (void)Convert_ALushort_IMA4
;
1334 (void)Convert_ALint_IMA4
;
1335 (void)Convert_ALuint_IMA4
;
1337 Convert_ALfloat_IMA4(dst
, src
, chans
, len
);
1339 (void)Convert_ALdouble_IMA4
;
1340 (void)Convert_ALmulaw_IMA4
;
1348 * Loads the specified data into the buffer, using the specified formats.
1349 * Currently, the new format must have the same channel configuration as the
1352 static ALenum
LoadData(ALbuffer
*ALBuf
, ALuint freq
, ALenum NewFormat
, ALsizei size
, enum SrcFmtChannels SrcChannels
, enum SrcFmtType SrcType
, const ALvoid
*data
)
1354 ALuint NewChannels
, NewBytes
;
1355 enum FmtChannels DstChannels
;
1356 enum FmtType DstType
;
1360 DecomposeFormat(NewFormat
, &DstChannels
, &DstType
);
1361 NewChannels
= ChannelsFromFmt(DstChannels
);
1362 NewBytes
= BytesFromFmt(DstType
);
1364 assert(SrcChannels
== DstChannels
);
1366 if(SrcType
== SrcFmtIMA4
)
1368 ALuint OrigChannels
= ChannelsFromSrcFmt(SrcChannels
);
1370 /* Here is where things vary:
1371 * nVidia and Apple use 64+1 sample frames per block => block_size=36*chans bytes
1372 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024*chans bytes
1374 if((size
%(36*OrigChannels
)) != 0)
1375 return AL_INVALID_VALUE
;
1377 newsize
= size
/ 36;
1379 newsize
*= NewBytes
;
1380 if(newsize
> INT_MAX
)
1381 return AL_OUT_OF_MEMORY
;
1383 temp
= realloc(ALBuf
->data
, newsize
);
1384 if(!temp
&& newsize
) return AL_OUT_OF_MEMORY
;
1386 ALBuf
->size
= newsize
;
1389 ConvertDataIMA4(ALBuf
->data
, DstType
, data
, OrigChannels
,
1390 newsize
/(65*NewChannels
*NewBytes
));
1392 ALBuf
->OriginalChannels
= SrcChannels
;
1393 ALBuf
->OriginalType
= SrcType
;
1394 ALBuf
->OriginalSize
= size
;
1395 ALBuf
->OriginalAlign
= 36 * OrigChannels
;
1399 ALuint OrigBytes
= BytesFromSrcFmt(SrcType
);
1400 ALuint OrigChannels
= ChannelsFromSrcFmt(SrcChannels
);
1402 if((size
%(OrigBytes
*OrigChannels
)) != 0)
1403 return AL_INVALID_VALUE
;
1405 newsize
= size
/ OrigBytes
;
1406 newsize
*= NewBytes
;
1407 if(newsize
> INT_MAX
)
1408 return AL_OUT_OF_MEMORY
;
1410 temp
= realloc(ALBuf
->data
, newsize
);
1411 if(!temp
&& newsize
) return AL_OUT_OF_MEMORY
;
1413 ALBuf
->size
= newsize
;
1416 ConvertData(ALBuf
->data
, DstType
, data
, SrcType
, newsize
/NewBytes
);
1418 ALBuf
->OriginalChannels
= SrcChannels
;
1419 ALBuf
->OriginalType
= SrcType
;
1420 ALBuf
->OriginalSize
= size
;
1421 ALBuf
->OriginalAlign
= OrigBytes
* OrigChannels
;
1424 ALBuf
->Frequency
= freq
;
1425 ALBuf
->FmtChannels
= DstChannels
;
1426 ALBuf
->FmtType
= DstType
;
1428 ALBuf
->LoopStart
= 0;
1429 ALBuf
->LoopEnd
= newsize
/ NewChannels
/ NewBytes
;
1435 ALuint
BytesFromSrcFmt(enum SrcFmtType type
)
1439 case SrcFmtByte
: return sizeof(ALbyte
);
1440 case SrcFmtUByte
: return sizeof(ALubyte
);
1441 case SrcFmtShort
: return sizeof(ALshort
);
1442 case SrcFmtUShort
: return sizeof(ALushort
);
1443 case SrcFmtInt
: return sizeof(ALint
);
1444 case SrcFmtUInt
: return sizeof(ALuint
);
1445 case SrcFmtFloat
: return sizeof(ALfloat
);
1446 case SrcFmtDouble
: return sizeof(ALdouble
);
1447 case SrcFmtMulaw
: return sizeof(ALubyte
);
1448 case SrcFmtIMA4
: break; /* not handled here */
1452 ALuint
ChannelsFromSrcFmt(enum SrcFmtChannels chans
)
1456 case SrcFmtMono
: return 1;
1457 case SrcFmtStereo
: return 2;
1458 case SrcFmtRear
: return 2;
1459 case SrcFmtQuad
: return 4;
1460 case SrcFmtX51
: return 6;
1461 case SrcFmtX61
: return 7;
1462 case SrcFmtX71
: return 8;
1466 ALboolean
DecomposeInputFormat(ALenum format
, enum SrcFmtChannels
*chans
,
1467 enum SrcFmtType
*type
)
1471 case AL_FORMAT_MONO8
:
1472 *chans
= SrcFmtMono
;
1473 *type
= SrcFmtUByte
;
1475 case AL_FORMAT_MONO16
:
1476 *chans
= SrcFmtMono
;
1477 *type
= SrcFmtShort
;
1479 case AL_FORMAT_MONO_FLOAT32
:
1480 *chans
= SrcFmtMono
;
1481 *type
= SrcFmtFloat
;
1483 case AL_FORMAT_MONO_DOUBLE_EXT
:
1484 *chans
= SrcFmtMono
;
1485 *type
= SrcFmtDouble
;
1487 case AL_FORMAT_MONO_IMA4
:
1488 *chans
= SrcFmtMono
;
1491 case AL_FORMAT_STEREO8
:
1492 *chans
= SrcFmtStereo
;
1493 *type
= SrcFmtUByte
;
1495 case AL_FORMAT_STEREO16
:
1496 *chans
= SrcFmtStereo
;
1497 *type
= SrcFmtShort
;
1499 case AL_FORMAT_STEREO_FLOAT32
:
1500 *chans
= SrcFmtStereo
;
1501 *type
= SrcFmtFloat
;
1503 case AL_FORMAT_STEREO_DOUBLE_EXT
:
1504 *chans
= SrcFmtStereo
;
1505 *type
= SrcFmtDouble
;
1507 case AL_FORMAT_STEREO_IMA4
:
1508 *chans
= SrcFmtStereo
;
1511 case AL_FORMAT_QUAD8_LOKI
:
1512 case AL_FORMAT_QUAD8
:
1513 *chans
= SrcFmtQuad
;
1514 *type
= SrcFmtUByte
;
1516 case AL_FORMAT_QUAD16_LOKI
:
1517 case AL_FORMAT_QUAD16
:
1518 *chans
= SrcFmtQuad
;
1519 *type
= SrcFmtShort
;
1521 case AL_FORMAT_QUAD32
:
1522 *chans
= SrcFmtQuad
;
1523 *type
= SrcFmtFloat
;
1525 case AL_FORMAT_REAR8
:
1526 *chans
= SrcFmtRear
;
1527 *type
= SrcFmtUByte
;
1529 case AL_FORMAT_REAR16
:
1530 *chans
= SrcFmtRear
;
1531 *type
= SrcFmtShort
;
1533 case AL_FORMAT_REAR32
:
1534 *chans
= SrcFmtRear
;
1535 *type
= SrcFmtFloat
;
1537 case AL_FORMAT_51CHN8
:
1539 *type
= SrcFmtUByte
;
1541 case AL_FORMAT_51CHN16
:
1543 *type
= SrcFmtShort
;
1545 case AL_FORMAT_51CHN32
:
1547 *type
= SrcFmtFloat
;
1549 case AL_FORMAT_61CHN8
:
1551 *type
= SrcFmtUByte
;
1553 case AL_FORMAT_61CHN16
:
1555 *type
= SrcFmtShort
;
1557 case AL_FORMAT_61CHN32
:
1559 *type
= SrcFmtFloat
;
1561 case AL_FORMAT_71CHN8
:
1563 *type
= SrcFmtUByte
;
1565 case AL_FORMAT_71CHN16
:
1567 *type
= SrcFmtShort
;
1569 case AL_FORMAT_71CHN32
:
1571 *type
= SrcFmtFloat
;
1573 case AL_FORMAT_MONO_MULAW
:
1574 *chans
= SrcFmtMono
;
1575 *type
= SrcFmtMulaw
;
1577 case AL_FORMAT_STEREO_MULAW
:
1578 *chans
= SrcFmtStereo
;
1579 *type
= SrcFmtMulaw
;
1581 case AL_FORMAT_QUAD_MULAW
:
1582 *chans
= SrcFmtQuad
;
1583 *type
= SrcFmtMulaw
;
1585 case AL_FORMAT_REAR_MULAW
:
1586 *chans
= SrcFmtRear
;
1587 *type
= SrcFmtMulaw
;
1589 case AL_FORMAT_51CHN_MULAW
:
1591 *type
= SrcFmtMulaw
;
1593 case AL_FORMAT_61CHN_MULAW
:
1595 *type
= SrcFmtMulaw
;
1597 case AL_FORMAT_71CHN_MULAW
:
1599 *type
= SrcFmtMulaw
;
1605 ALuint
BytesFromFmt(enum FmtType type
)
1609 case FmtUByte
: return sizeof(ALubyte
);
1610 case FmtShort
: return sizeof(ALshort
);
1611 case FmtFloat
: return sizeof(ALfloat
);
1615 ALuint
ChannelsFromFmt(enum FmtChannels chans
)
1619 case FmtMono
: return 1;
1620 case FmtStereo
: return 2;
1621 case FmtRear
: return 2;
1622 case FmtQuad
: return 4;
1623 case FmtX51
: return 6;
1624 case FmtX61
: return 7;
1625 case FmtX71
: return 8;
1629 ALboolean
DecomposeFormat(ALenum format
, enum FmtChannels
*chans
, enum FmtType
*type
)
1633 case AL_FORMAT_MONO8
:
1637 case AL_FORMAT_MONO16
:
1641 case AL_FORMAT_MONO_FLOAT32
:
1645 case AL_FORMAT_STEREO8
:
1649 case AL_FORMAT_STEREO16
:
1653 case AL_FORMAT_STEREO_FLOAT32
:
1657 case AL_FORMAT_QUAD8_LOKI
:
1658 case AL_FORMAT_QUAD8
:
1662 case AL_FORMAT_QUAD16_LOKI
:
1663 case AL_FORMAT_QUAD16
:
1667 case AL_FORMAT_QUAD32
:
1671 case AL_FORMAT_REAR8
:
1675 case AL_FORMAT_REAR16
:
1679 case AL_FORMAT_REAR32
:
1683 case AL_FORMAT_51CHN8
:
1687 case AL_FORMAT_51CHN16
:
1691 case AL_FORMAT_51CHN32
:
1695 case AL_FORMAT_61CHN8
:
1699 case AL_FORMAT_61CHN16
:
1703 case AL_FORMAT_61CHN32
:
1707 case AL_FORMAT_71CHN8
:
1711 case AL_FORMAT_71CHN16
:
1715 case AL_FORMAT_71CHN32
:
1725 * ReleaseALBuffers()
1727 * INTERNAL FN : Called by alcCloseDevice to destroy any buffers that still exist
1729 ALvoid
ReleaseALBuffers(ALCdevice
*device
)
1732 for(i
= 0;i
< device
->BufferMap
.size
;i
++)
1734 ALbuffer
*temp
= device
->BufferMap
.array
[i
].value
;
1735 device
->BufferMap
.array
[i
].value
= NULL
;
1737 // Release sample data
1740 // Release Buffer structure
1741 ALTHUNK_REMOVEENTRY(temp
->buffer
);
1742 memset(temp
, 0, sizeof(ALbuffer
));