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 UserFmtChannels chans
, enum UserFmtType type
, const ALvoid
*data
);
38 static void ConvertData(ALvoid
*dst
, enum FmtType dstType
, const ALvoid
*src
, enum UserFmtType 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 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 IMA4Codeword
[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 IMA4Index_adjust
[16] = {
69 -1,-1,-1,-1, 2, 4, 6, 8,
70 -1,-1,-1,-1, 2, 4, 6, 8
73 /* A quick'n'dirty lookup table to decode a muLaw-encoded byte sample into a
74 * signed 16-bit sample */
75 static const ALshort muLawDecompressionTable
[256] = {
76 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
77 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
78 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
79 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
80 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
81 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
82 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
83 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
84 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
85 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
86 -876, -844, -812, -780, -748, -716, -684, -652,
87 -620, -588, -556, -524, -492, -460, -428, -396,
88 -372, -356, -340, -324, -308, -292, -276, -260,
89 -244, -228, -212, -196, -180, -164, -148, -132,
90 -120, -112, -104, -96, -88, -80, -72, -64,
91 -56, -48, -40, -32, -24, -16, -8, 0,
92 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
93 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
94 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
95 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
96 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
97 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
98 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
99 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
100 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
101 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
102 876, 844, 812, 780, 748, 716, 684, 652,
103 620, 588, 556, 524, 492, 460, 428, 396,
104 372, 356, 340, 324, 308, 292, 276, 260,
105 244, 228, 212, 196, 180, 164, 148, 132,
106 120, 112, 104, 96, 88, 80, 72, 64,
107 56, 48, 40, 32, 24, 16, 8, 0
110 /* Values used when encoding a muLaw sample */
111 static const int muLawBias
= 0x84;
112 static const int muLawClip
= 32635;
113 static const char muLawCompressTable
[256] =
115 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
116 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
117 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
118 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
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 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
122 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
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,
129 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
130 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
134 * alGenBuffers(ALsizei n, ALuint *buffers)
136 * Generates n AL Buffers, and stores the Buffers Names in the array pointed
139 AL_API ALvoid AL_APIENTRY
alGenBuffers(ALsizei n
, ALuint
*buffers
)
144 Context
= GetContextSuspended();
147 /* Check that we are actually generating some Buffers */
148 if(n
< 0 || IsBadWritePtr((void*)buffers
, n
* sizeof(ALuint
)))
149 alSetError(Context
, AL_INVALID_VALUE
);
152 ALCdevice
*device
= Context
->Device
;
155 // Create all the new Buffers
158 ALbuffer
*buffer
= calloc(1, sizeof(ALbuffer
));
161 alSetError(Context
, AL_OUT_OF_MEMORY
);
162 alDeleteBuffers(i
, buffers
);
166 buffer
->buffer
= (ALuint
)ALTHUNK_ADDENTRY(buffer
);
167 err
= InsertUIntMapEntry(&device
->BufferMap
, buffer
->buffer
, buffer
);
168 if(err
!= AL_NO_ERROR
)
170 ALTHUNK_REMOVEENTRY(buffer
->buffer
);
171 memset(buffer
, 0, sizeof(ALbuffer
));
174 alSetError(Context
, err
);
175 alDeleteBuffers(i
, buffers
);
178 buffers
[i
++] = buffer
->buffer
;
182 ProcessContext(Context
);
186 * alDeleteBuffers(ALsizei n, ALuint *buffers)
188 * Deletes the n AL Buffers pointed to by buffers
190 AL_API ALvoid AL_APIENTRY
alDeleteBuffers(ALsizei n
, const ALuint
*buffers
)
198 Context
= GetContextSuspended();
202 device
= Context
->Device
;
203 /* Check we are actually Deleting some Buffers */
205 alSetError(Context
, AL_INVALID_VALUE
);
210 /* Check that all the buffers are valid and can actually be deleted */
216 /* Check for valid Buffer ID */
217 if((ALBuf
=LookupBuffer(device
->BufferMap
, buffers
[i
])) == NULL
)
219 alSetError(Context
, AL_INVALID_NAME
);
223 else if(ALBuf
->refcount
!= 0)
225 /* Buffer still in use, cannot be deleted */
226 alSetError(Context
, AL_INVALID_OPERATION
);
233 /* If all the Buffers were valid (and have Reference Counts of 0), then we
239 if((ALBuf
=LookupBuffer(device
->BufferMap
, buffers
[i
])) == NULL
)
242 /* Release the memory used to store audio data */
245 /* Release buffer structure */
246 RemoveUIntMapKey(&device
->BufferMap
, ALBuf
->buffer
);
247 ALTHUNK_REMOVEENTRY(ALBuf
->buffer
);
249 memset(ALBuf
, 0, sizeof(ALbuffer
));
254 ProcessContext(Context
);
258 * alIsBuffer(ALuint buffer)
260 * Checks if buffer is a valid Buffer Name
262 AL_API ALboolean AL_APIENTRY
alIsBuffer(ALuint buffer
)
267 Context
= GetContextSuspended();
268 if(!Context
) return AL_FALSE
;
270 result
= ((!buffer
|| LookupBuffer(Context
->Device
->BufferMap
, buffer
)) ?
273 ProcessContext(Context
);
279 * alBufferData(ALuint buffer, ALenum format, const ALvoid *data,
280 * ALsizei size, ALsizei freq)
282 * Fill buffer with audio data
284 AL_API ALvoid AL_APIENTRY
alBufferData(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei size
,ALsizei freq
)
286 enum UserFmtChannels SrcChannels
;
287 enum UserFmtType SrcType
;
293 Context
= GetContextSuspended();
296 if(Context
->SampleSource
)
300 if(Context
->SampleSource
->state
== MAPPED
)
302 alSetError(Context
, AL_INVALID_OPERATION
);
303 ProcessContext(Context
);
307 offset
= (const ALubyte
*)data
- (ALubyte
*)NULL
;
308 data
= Context
->SampleSource
->data
+ offset
;
311 device
= Context
->Device
;
312 if((ALBuf
=LookupBuffer(device
->BufferMap
, buffer
)) == NULL
)
313 alSetError(Context
, AL_INVALID_NAME
);
314 else if(ALBuf
->refcount
!= 0)
315 alSetError(Context
, AL_INVALID_VALUE
);
316 else if(size
< 0 || freq
< 0)
317 alSetError(Context
, AL_INVALID_VALUE
);
318 else if(DecomposeUserFormat(format
, &SrcChannels
, &SrcType
) == AL_FALSE
)
319 alSetError(Context
, AL_INVALID_ENUM
);
329 err
= LoadData(ALBuf
, freq
, format
, size
, SrcChannels
, SrcType
, data
);
330 if(err
!= AL_NO_ERROR
)
331 alSetError(Context
, err
);
334 case UserFmtDouble
: {
335 ALenum NewFormat
= AL_FORMAT_MONO_FLOAT32
;
338 case UserFmtMono
: NewFormat
= AL_FORMAT_MONO_FLOAT32
; break;
339 case UserFmtStereo
: NewFormat
= AL_FORMAT_STEREO_FLOAT32
; break;
340 case UserFmtRear
: NewFormat
= AL_FORMAT_REAR32
; break;
341 case UserFmtQuad
: NewFormat
= AL_FORMAT_QUAD32
; break;
342 case UserFmtX51
: NewFormat
= AL_FORMAT_51CHN32
; break;
343 case UserFmtX61
: NewFormat
= AL_FORMAT_61CHN32
; break;
344 case UserFmtX71
: NewFormat
= AL_FORMAT_71CHN32
; break;
346 err
= LoadData(ALBuf
, freq
, NewFormat
, size
, SrcChannels
, SrcType
, data
);
347 if(err
!= AL_NO_ERROR
)
348 alSetError(Context
, err
);
353 ALenum NewFormat
= AL_FORMAT_MONO16
;
356 case UserFmtMono
: NewFormat
= AL_FORMAT_MONO16
; break;
357 case UserFmtStereo
: NewFormat
= AL_FORMAT_STEREO16
; break;
358 case UserFmtRear
: NewFormat
= AL_FORMAT_REAR16
; break;
359 case UserFmtQuad
: NewFormat
= AL_FORMAT_QUAD16
; break;
360 case UserFmtX51
: NewFormat
= AL_FORMAT_51CHN16
; break;
361 case UserFmtX61
: NewFormat
= AL_FORMAT_61CHN16
; break;
362 case UserFmtX71
: NewFormat
= AL_FORMAT_71CHN16
; break;
364 err
= LoadData(ALBuf
, freq
, NewFormat
, size
, SrcChannels
, SrcType
, data
);
365 if(err
!= AL_NO_ERROR
)
366 alSetError(Context
, err
);
370 ProcessContext(Context
);
374 * alBufferSubDataSOFT(ALuint buffer, ALenum format, const ALvoid *data,
375 * ALsizei offset, ALsizei length)
377 * Update buffer's audio data
379 AL_API ALvoid AL_APIENTRY
alBufferSubDataSOFT(ALuint buffer
,ALenum format
,const ALvoid
*data
,ALsizei offset
,ALsizei length
)
381 enum UserFmtChannels SrcChannels
;
382 enum UserFmtType SrcType
;
387 Context
= GetContextSuspended();
390 if(Context
->SampleSource
)
394 if(Context
->SampleSource
->state
== MAPPED
)
396 alSetError(Context
, AL_INVALID_OPERATION
);
397 ProcessContext(Context
);
401 offset
= (const ALubyte
*)data
- (ALubyte
*)NULL
;
402 data
= Context
->SampleSource
->data
+ offset
;
405 device
= Context
->Device
;
406 if((ALBuf
=LookupBuffer(device
->BufferMap
, buffer
)) == NULL
)
407 alSetError(Context
, AL_INVALID_NAME
);
408 else if(length
< 0 || offset
< 0 || (length
> 0 && data
== NULL
))
409 alSetError(Context
, AL_INVALID_VALUE
);
410 else if(DecomposeUserFormat(format
, &SrcChannels
, &SrcType
) == AL_FALSE
||
411 SrcChannels
!= ALBuf
->OriginalChannels
||
412 SrcType
!= ALBuf
->OriginalType
)
413 alSetError(Context
, AL_INVALID_ENUM
);
414 else if(offset
> ALBuf
->OriginalSize
||
415 length
> ALBuf
->OriginalSize
-offset
||
416 (offset
%ALBuf
->OriginalAlign
) != 0 ||
417 (length
%ALBuf
->OriginalAlign
) != 0)
418 alSetError(Context
, AL_INVALID_VALUE
);
421 if(SrcType
== UserFmtIMA4
)
423 ALuint Channels
= ChannelsFromFmt(ALBuf
->FmtChannels
);
424 ALuint Bytes
= BytesFromFmt(ALBuf
->FmtType
);
426 /* offset -> byte offset, length -> block count */
430 length
/= ALBuf
->OriginalAlign
;
432 ConvertDataIMA4(&((ALubyte
*)ALBuf
->data
)[offset
], ALBuf
->FmtType
,
433 data
, Channels
, length
);
437 ALuint OldBytes
= BytesFromUserFmt(SrcType
);
438 ALuint Bytes
= BytesFromFmt(ALBuf
->FmtType
);
444 ConvertData(&((ALubyte
*)ALBuf
->data
)[offset
], ALBuf
->FmtType
,
445 data
, SrcType
, length
);
449 ProcessContext(Context
);
453 AL_API
void AL_APIENTRY
alBufferf(ALuint buffer
, ALenum eParam
, ALfloat flValue
)
455 ALCcontext
*pContext
;
460 pContext
= GetContextSuspended();
461 if(!pContext
) return;
463 device
= pContext
->Device
;
464 if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
465 alSetError(pContext
, AL_INVALID_NAME
);
471 alSetError(pContext
, AL_INVALID_ENUM
);
476 ProcessContext(pContext
);
480 AL_API
void AL_APIENTRY
alBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat flValue1
, ALfloat flValue2
, ALfloat flValue3
)
482 ALCcontext
*pContext
;
489 pContext
= GetContextSuspended();
490 if(!pContext
) return;
492 device
= pContext
->Device
;
493 if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
494 alSetError(pContext
, AL_INVALID_NAME
);
500 alSetError(pContext
, AL_INVALID_ENUM
);
505 ProcessContext(pContext
);
509 AL_API
void AL_APIENTRY
alBufferfv(ALuint buffer
, ALenum eParam
, const ALfloat
* flValues
)
511 ALCcontext
*pContext
;
514 pContext
= GetContextSuspended();
515 if(!pContext
) return;
517 device
= pContext
->Device
;
519 alSetError(pContext
, AL_INVALID_VALUE
);
520 else if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
521 alSetError(pContext
, AL_INVALID_NAME
);
527 alSetError(pContext
, AL_INVALID_ENUM
);
532 ProcessContext(pContext
);
536 AL_API
void AL_APIENTRY
alBufferi(ALuint buffer
, ALenum eParam
, ALint lValue
)
538 ALCcontext
*pContext
;
543 pContext
= GetContextSuspended();
544 if(!pContext
) return;
546 device
= pContext
->Device
;
547 if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
548 alSetError(pContext
, AL_INVALID_NAME
);
554 alSetError(pContext
, AL_INVALID_ENUM
);
559 ProcessContext(pContext
);
563 AL_API
void AL_APIENTRY
alBuffer3i( ALuint buffer
, ALenum eParam
, ALint lValue1
, ALint lValue2
, ALint lValue3
)
565 ALCcontext
*pContext
;
572 pContext
= GetContextSuspended();
573 if(!pContext
) return;
575 device
= pContext
->Device
;
576 if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
577 alSetError(pContext
, AL_INVALID_NAME
);
583 alSetError(pContext
, AL_INVALID_ENUM
);
588 ProcessContext(pContext
);
592 AL_API
void AL_APIENTRY
alBufferiv(ALuint buffer
, ALenum eParam
, const ALint
* plValues
)
594 ALCcontext
*pContext
;
598 pContext
= GetContextSuspended();
599 if(!pContext
) return;
601 device
= pContext
->Device
;
603 alSetError(pContext
, AL_INVALID_VALUE
);
604 else if((ALBuf
=LookupBuffer(device
->BufferMap
, buffer
)) == NULL
)
605 alSetError(pContext
, AL_INVALID_NAME
);
611 if(ALBuf
->refcount
> 0)
612 alSetError(pContext
, AL_INVALID_OPERATION
);
613 else if(plValues
[0] < 0 || plValues
[1] < 0 ||
614 plValues
[0] >= plValues
[1] || ALBuf
->size
== 0)
615 alSetError(pContext
, AL_INVALID_VALUE
);
618 ALint maxlen
= ALBuf
->size
/
619 FrameSizeFromFmt(ALBuf
->FmtChannels
, ALBuf
->FmtType
);
620 if(plValues
[0] > maxlen
|| plValues
[1] > maxlen
)
621 alSetError(pContext
, AL_INVALID_VALUE
);
624 ALBuf
->LoopStart
= plValues
[0];
625 ALBuf
->LoopEnd
= plValues
[1];
631 alSetError(pContext
, AL_INVALID_ENUM
);
636 ProcessContext(pContext
);
640 AL_API ALvoid AL_APIENTRY
alGetBufferf(ALuint buffer
, ALenum eParam
, ALfloat
*pflValue
)
642 ALCcontext
*pContext
;
645 pContext
= GetContextSuspended();
646 if(!pContext
) return;
648 device
= pContext
->Device
;
650 alSetError(pContext
, AL_INVALID_VALUE
);
651 else if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
652 alSetError(pContext
, AL_INVALID_NAME
);
658 alSetError(pContext
, AL_INVALID_ENUM
);
663 ProcessContext(pContext
);
667 AL_API
void AL_APIENTRY
alGetBuffer3f(ALuint buffer
, ALenum eParam
, ALfloat
* pflValue1
, ALfloat
* pflValue2
, ALfloat
* pflValue3
)
669 ALCcontext
*pContext
;
672 pContext
= GetContextSuspended();
673 if(!pContext
) return;
675 device
= pContext
->Device
;
676 if(!pflValue1
|| !pflValue2
|| !pflValue3
)
677 alSetError(pContext
, AL_INVALID_VALUE
);
678 else if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
679 alSetError(pContext
, AL_INVALID_NAME
);
685 alSetError(pContext
, AL_INVALID_ENUM
);
690 ProcessContext(pContext
);
694 AL_API
void AL_APIENTRY
alGetBufferfv(ALuint buffer
, ALenum eParam
, ALfloat
* pflValues
)
696 ALCcontext
*pContext
;
699 pContext
= GetContextSuspended();
700 if(!pContext
) return;
702 device
= pContext
->Device
;
704 alSetError(pContext
, AL_INVALID_VALUE
);
705 else if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
706 alSetError(pContext
, AL_INVALID_NAME
);
712 alSetError(pContext
, AL_INVALID_ENUM
);
717 ProcessContext(pContext
);
721 AL_API ALvoid AL_APIENTRY
alGetBufferi(ALuint buffer
, ALenum eParam
, ALint
*plValue
)
723 ALCcontext
*pContext
;
727 pContext
= GetContextSuspended();
728 if(!pContext
) return;
730 device
= pContext
->Device
;
732 alSetError(pContext
, AL_INVALID_VALUE
);
733 else if((pBuffer
=LookupBuffer(device
->BufferMap
, buffer
)) == NULL
)
734 alSetError(pContext
, AL_INVALID_NAME
);
740 *plValue
= pBuffer
->Frequency
;
744 *plValue
= BytesFromFmt(pBuffer
->FmtType
) * 8;
748 *plValue
= ChannelsFromFmt(pBuffer
->FmtChannels
);
752 *plValue
= pBuffer
->size
;
756 alSetError(pContext
, AL_INVALID_ENUM
);
761 ProcessContext(pContext
);
765 AL_API
void AL_APIENTRY
alGetBuffer3i(ALuint buffer
, ALenum eParam
, ALint
* plValue1
, ALint
* plValue2
, ALint
* plValue3
)
767 ALCcontext
*pContext
;
770 pContext
= GetContextSuspended();
771 if(!pContext
) return;
773 device
= pContext
->Device
;
774 if(!plValue1
|| !plValue2
|| !plValue3
)
775 alSetError(pContext
, AL_INVALID_VALUE
);
776 else if(LookupBuffer(device
->BufferMap
, buffer
) == NULL
)
777 alSetError(pContext
, AL_INVALID_NAME
);
783 alSetError(pContext
, AL_INVALID_ENUM
);
788 ProcessContext(pContext
);
792 AL_API
void AL_APIENTRY
alGetBufferiv(ALuint buffer
, ALenum eParam
, ALint
* plValues
)
794 ALCcontext
*pContext
;
798 pContext
= GetContextSuspended();
799 if(!pContext
) return;
801 device
= pContext
->Device
;
803 alSetError(pContext
, AL_INVALID_VALUE
);
804 else if((ALBuf
=LookupBuffer(device
->BufferMap
, buffer
)) == NULL
)
805 alSetError(pContext
, AL_INVALID_NAME
);
814 alGetBufferi(buffer
, eParam
, plValues
);
818 plValues
[0] = ALBuf
->LoopStart
;
819 plValues
[1] = ALBuf
->LoopEnd
;
823 alSetError(pContext
, AL_INVALID_ENUM
);
828 ProcessContext(pContext
);
832 typedef ALubyte ALmulaw
;
834 static __inline ALshort
DecodeMuLaw(ALmulaw val
)
835 { return muLawDecompressionTable
[val
]; }
837 static ALmulaw
EncodeMuLaw(ALshort val
)
839 ALint mant
, exp
, sign
;
841 sign
= (val
>>8) & 0x80;
844 /* -32768 doesn't properly negate on a short; it results in itself.
845 * So clamp to -32767 */
846 val
= max(val
, -32767);
850 val
= min(val
, muLawClip
);
853 exp
= muLawCompressTable
[(val
>>7) & 0xff];
854 mant
= (val
>> (exp
+3)) & 0x0f;
856 return ~(sign
| (exp
<<4) | mant
);
859 static void DecodeIMA4Block(ALshort
*dst
, const ALubyte
*src
, ALint numchans
)
861 ALint sample
[MAXCHANNELS
], index
[MAXCHANNELS
];
862 ALuint code
[MAXCHANNELS
];
865 for(c
= 0;c
< numchans
;c
++)
867 sample
[c
] = *(src
++);
868 sample
[c
] |= *(src
++) << 8;
869 sample
[c
] = (sample
[c
]^0x8000) - 32768;
871 index
[c
] |= *(src
++) << 8;
872 index
[c
] = (index
[c
]^0x8000) - 32768;
874 index
[c
] = max(0, index
[c
]);
875 index
[c
] = min(index
[c
], 88);
883 for(c
= 0;c
< numchans
;c
++)
886 code
[c
] |= *(src
++) << 8;
887 code
[c
] |= *(src
++) << 16;
888 code
[c
] |= *(src
++) << 24;
891 for(k
= 0;k
< 8;k
++,j
++)
893 for(c
= 0;c
< numchans
;c
++)
895 int nibble
= code
[c
]&0xf;
898 sample
[c
] += IMA4Codeword
[nibble
] * IMAStep_size
[index
[c
]] / 8;
899 sample
[c
] = max(-32768, sample
[c
]);
900 sample
[c
] = min(sample
[c
], 32767);
902 index
[c
] += IMA4Index_adjust
[nibble
];
903 index
[c
] = max(0, index
[c
]);
904 index
[c
] = min(index
[c
], 88);
906 dst
[j
*numchans
+ c
] = sample
[c
];
912 static void EncodeIMA4Block(ALubyte
*dst
, const ALshort
*src
, ALint
*sample
, ALint
*index
, ALint numchans
)
916 for(c
= 0;c
< numchans
;c
++)
918 int diff
= src
[c
] - sample
[c
];
919 int step
= IMAStep_size
[index
[c
]];
929 diff
= min(step
*2, diff
);
930 nibble
|= (diff
*8/step
- 1) / 2;
932 sample
[c
] += IMA4Codeword
[nibble
] * step
/ 8;
933 sample
[c
] = max(-32768, sample
[c
]);
934 sample
[c
] = min(sample
[c
], 32767);
936 index
[c
] += IMA4Index_adjust
[nibble
];
937 index
[c
] = max(0, index
[c
]);
938 index
[c
] = min(index
[c
], 88);
940 *(dst
++) = sample
[c
] & 0xff;
941 *(dst
++) = (sample
[c
]>>8) & 0xff;
942 *(dst
++) = index
[c
] & 0xff;
943 *(dst
++) = (index
[c
]>>8) & 0xff;
949 for(c
= 0;c
< numchans
;c
++)
953 int diff
= src
[(j
+k
)*numchans
+ c
] - sample
[c
];
954 int step
= IMAStep_size
[index
[c
]];
964 diff
= min(step
*2, diff
);
965 nibble
|= (diff
*8/step
- 1) / 2;
967 sample
[c
] += IMA4Codeword
[nibble
] * step
/ 8;
968 sample
[c
] = max(-32768, sample
[c
]);
969 sample
[c
] = min(sample
[c
], 32767);
971 index
[c
] += IMA4Index_adjust
[nibble
];
972 index
[c
] = max(0, index
[c
]);
973 index
[c
] = min(index
[c
], 88);
975 if(!(k
&1)) *dst
= nibble
;
976 else *(dst
++) |= nibble
<<4;
984 static __inline ALbyte
Conv_ALbyte_ALbyte(ALbyte val
)
986 static __inline ALbyte
Conv_ALbyte_ALubyte(ALubyte val
)
988 static __inline ALbyte
Conv_ALbyte_ALshort(ALshort val
)
990 static __inline ALbyte
Conv_ALbyte_ALushort(ALushort val
)
991 { return (val
>>8)-128; }
992 static __inline ALbyte
Conv_ALbyte_ALint(ALint val
)
994 static __inline ALbyte
Conv_ALbyte_ALuint(ALuint val
)
995 { return (val
>>24)-128; }
996 static __inline ALbyte
Conv_ALbyte_ALfloat(ALfloat val
)
998 if(val
> 1.0f
) return 127;
999 if(val
< -1.0f
) return -128;
1000 return (ALint
)(val
* 127.0f
);
1002 static __inline ALbyte
Conv_ALbyte_ALdouble(ALdouble val
)
1004 if(val
> 1.0) return 127;
1005 if(val
< -1.0) return -128;
1006 return (ALint
)(val
* 127.0);
1008 static __inline ALbyte
Conv_ALbyte_ALmulaw(ALmulaw val
)
1009 { return Conv_ALbyte_ALshort(DecodeMuLaw(val
)); }
1011 static __inline ALubyte
Conv_ALubyte_ALbyte(ALbyte val
)
1012 { return val
^0x80; }
1013 static __inline ALubyte
Conv_ALubyte_ALubyte(ALubyte val
)
1015 static __inline ALubyte
Conv_ALubyte_ALshort(ALshort val
)
1016 { return (val
>>8)+128; }
1017 static __inline ALubyte
Conv_ALubyte_ALushort(ALushort val
)
1019 static __inline ALubyte
Conv_ALubyte_ALint(ALint val
)
1020 { return (val
>>24)+128; }
1021 static __inline ALubyte
Conv_ALubyte_ALuint(ALuint val
)
1023 static __inline ALubyte
Conv_ALubyte_ALfloat(ALfloat val
)
1025 if(val
> 1.0f
) return 255;
1026 if(val
< -1.0f
) return 0;
1027 return (ALint
)(val
* 127.0f
) + 128;
1029 static __inline ALubyte
Conv_ALubyte_ALdouble(ALdouble val
)
1031 if(val
> 1.0) return 255;
1032 if(val
< -1.0) return 0;
1033 return (ALint
)(val
* 127.0) + 128;
1035 static __inline ALubyte
Conv_ALubyte_ALmulaw(ALmulaw val
)
1036 { return Conv_ALubyte_ALshort(DecodeMuLaw(val
)); }
1038 static __inline ALshort
Conv_ALshort_ALbyte(ALbyte val
)
1040 static __inline ALshort
Conv_ALshort_ALubyte(ALubyte val
)
1041 { return (val
-128)<<8; }
1042 static __inline ALshort
Conv_ALshort_ALshort(ALshort val
)
1044 static __inline ALshort
Conv_ALshort_ALushort(ALushort val
)
1045 { return val
^0x8000; }
1046 static __inline ALshort
Conv_ALshort_ALint(ALint val
)
1048 static __inline ALshort
Conv_ALshort_ALuint(ALuint val
)
1049 { return (val
>>16)-32768; }
1050 static __inline ALshort
Conv_ALshort_ALfloat(ALfloat val
)
1052 if(val
> 1.0f
) return 32767;
1053 if(val
< -1.0f
) return -32768;
1054 return (ALint
)(val
* 32767.0f
);
1056 static __inline ALshort
Conv_ALshort_ALdouble(ALdouble val
)
1058 if(val
> 1.0) return 32767;
1059 if(val
< -1.0) return -32768;
1060 return (ALint
)(val
* 32767.0);
1062 static __inline ALshort
Conv_ALshort_ALmulaw(ALmulaw val
)
1063 { return Conv_ALshort_ALshort(DecodeMuLaw(val
)); }
1065 static __inline ALushort
Conv_ALushort_ALbyte(ALbyte val
)
1066 { return (val
+128)<<8; }
1067 static __inline ALushort
Conv_ALushort_ALubyte(ALubyte val
)
1069 static __inline ALushort
Conv_ALushort_ALshort(ALshort val
)
1070 { return val
^0x8000; }
1071 static __inline ALushort
Conv_ALushort_ALushort(ALushort val
)
1073 static __inline ALushort
Conv_ALushort_ALint(ALint val
)
1074 { return (val
>>16)+32768; }
1075 static __inline ALushort
Conv_ALushort_ALuint(ALuint val
)
1077 static __inline ALushort
Conv_ALushort_ALfloat(ALfloat val
)
1079 if(val
> 1.0f
) return 65535;
1080 if(val
< -1.0f
) return 0;
1081 return (ALint
)(val
* 32767.0f
) + 32768;
1083 static __inline ALushort
Conv_ALushort_ALdouble(ALdouble val
)
1085 if(val
> 1.0) return 65535;
1086 if(val
< -1.0) return 0;
1087 return (ALint
)(val
* 32767.0) + 32768;
1089 static __inline ALushort
Conv_ALushort_ALmulaw(ALmulaw val
)
1090 { return Conv_ALushort_ALshort(DecodeMuLaw(val
)); }
1092 static __inline ALint
Conv_ALint_ALbyte(ALbyte val
)
1094 static __inline ALint
Conv_ALint_ALubyte(ALubyte val
)
1095 { return (val
-128)<<24; }
1096 static __inline ALint
Conv_ALint_ALshort(ALshort val
)
1098 static __inline ALint
Conv_ALint_ALushort(ALushort val
)
1099 { return (val
-32768)<<16; }
1100 static __inline ALint
Conv_ALint_ALint(ALint val
)
1102 static __inline ALint
Conv_ALint_ALuint(ALuint val
)
1103 { return val
^0x80000000; }
1104 static __inline ALint
Conv_ALint_ALfloat(ALfloat val
)
1106 if(val
> 1.0f
) return 2147483647;
1107 if(val
< -1.0f
) return -2147483648u;
1108 return (ALint
)(val
* 2147483647.0);
1110 static __inline ALint
Conv_ALint_ALdouble(ALdouble val
)
1112 if(val
> 1.0) return 2147483647;
1113 if(val
< -1.0) return -2147483648u;
1114 return (ALint
)(val
* 2147483647.0);
1116 static __inline ALint
Conv_ALint_ALmulaw(ALmulaw val
)
1117 { return Conv_ALint_ALshort(DecodeMuLaw(val
)); }
1119 static __inline ALuint
Conv_ALuint_ALbyte(ALbyte val
)
1120 { return (val
+128)<<24; }
1121 static __inline ALuint
Conv_ALuint_ALubyte(ALubyte val
)
1123 static __inline ALuint
Conv_ALuint_ALshort(ALshort val
)
1124 { return (val
+32768)<<16; }
1125 static __inline ALuint
Conv_ALuint_ALushort(ALushort val
)
1127 static __inline ALuint
Conv_ALuint_ALint(ALint val
)
1128 { return val
^0x80000000; }
1129 static __inline ALuint
Conv_ALuint_ALuint(ALuint val
)
1131 static __inline ALuint
Conv_ALuint_ALfloat(ALfloat val
)
1133 if(val
> 1.0f
) return 4294967295u;
1134 if(val
< -1.0f
) return 0;
1135 return (ALint
)(val
* 2147483647.0) + 2147483648u;
1137 static __inline ALuint
Conv_ALuint_ALdouble(ALdouble val
)
1139 if(val
> 1.0) return 4294967295u;
1140 if(val
< -1.0) return 0;
1141 return (ALint
)(val
* 2147483647.0) + 2147483648u;
1143 static __inline ALuint
Conv_ALuint_ALmulaw(ALmulaw val
)
1144 { return Conv_ALuint_ALshort(DecodeMuLaw(val
)); }
1146 static __inline ALfloat
Conv_ALfloat_ALbyte(ALbyte val
)
1147 { return val
* (1.0f
/127.0f
); }
1148 static __inline ALfloat
Conv_ALfloat_ALubyte(ALubyte val
)
1149 { return (val
-128) * (1.0f
/127.0f
); }
1150 static __inline ALfloat
Conv_ALfloat_ALshort(ALshort val
)
1151 { return val
* (1.0f
/32767.0f
); }
1152 static __inline ALfloat
Conv_ALfloat_ALushort(ALushort val
)
1153 { return (val
-32768) * (1.0f
/32767.0f
); }
1154 static __inline ALfloat
Conv_ALfloat_ALint(ALint val
)
1155 { return val
* (1.0/2147483647.0); }
1156 static __inline ALfloat
Conv_ALfloat_ALuint(ALuint val
)
1157 { return (ALint
)(val
-2147483648u) * (1.0/2147483647.0); }
1158 static __inline ALfloat
Conv_ALfloat_ALfloat(ALfloat val
)
1160 static __inline ALfloat
Conv_ALfloat_ALdouble(ALdouble val
)
1162 static __inline ALfloat
Conv_ALfloat_ALmulaw(ALmulaw val
)
1163 { return Conv_ALfloat_ALshort(DecodeMuLaw(val
)); }
1165 static __inline ALdouble
Conv_ALdouble_ALbyte(ALbyte val
)
1166 { return val
* (1.0/127.0); }
1167 static __inline ALdouble
Conv_ALdouble_ALubyte(ALubyte val
)
1168 { return (val
-128) * (1.0/127.0); }
1169 static __inline ALdouble
Conv_ALdouble_ALshort(ALshort val
)
1170 { return val
* (1.0/32767.0); }
1171 static __inline ALdouble
Conv_ALdouble_ALushort(ALushort val
)
1172 { return (val
-32768) * (1.0/32767.0); }
1173 static __inline ALdouble
Conv_ALdouble_ALint(ALint val
)
1174 { return val
* (1.0/2147483647.0); }
1175 static __inline ALdouble
Conv_ALdouble_ALuint(ALuint val
)
1176 { return (ALint
)(val
-2147483648u) * (1.0/2147483647.0); }
1177 static __inline ALdouble
Conv_ALdouble_ALfloat(ALfloat val
)
1179 static __inline ALdouble
Conv_ALdouble_ALdouble(ALdouble val
)
1181 static __inline ALdouble
Conv_ALdouble_ALmulaw(ALmulaw val
)
1182 { return Conv_ALdouble_ALshort(DecodeMuLaw(val
)); }
1184 #define DECL_TEMPLATE(T) \
1185 static __inline ALmulaw Conv_ALmulaw_##T(T val) \
1186 { return EncodeMuLaw(Conv_ALshort_##T(val)); }
1188 DECL_TEMPLATE(ALbyte
)
1189 DECL_TEMPLATE(ALubyte
)
1190 DECL_TEMPLATE(ALshort
)
1191 DECL_TEMPLATE(ALushort
)
1192 DECL_TEMPLATE(ALint
)
1193 DECL_TEMPLATE(ALuint
)
1194 DECL_TEMPLATE(ALfloat
)
1195 DECL_TEMPLATE(ALdouble
)
1196 DECL_TEMPLATE(ALmulaw
)
1198 #undef DECL_TEMPLATE
1200 #define DECL_TEMPLATE(T1, T2) \
1201 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint len) \
1204 for(i = 0;i < len;i++) \
1205 *(dst++) = Conv_##T1##_##T2(*(src++)); \
1208 DECL_TEMPLATE(ALbyte
, ALbyte
)
1209 DECL_TEMPLATE(ALbyte
, ALubyte
)
1210 DECL_TEMPLATE(ALbyte
, ALshort
)
1211 DECL_TEMPLATE(ALbyte
, ALushort
)
1212 DECL_TEMPLATE(ALbyte
, ALint
)
1213 DECL_TEMPLATE(ALbyte
, ALuint
)
1214 DECL_TEMPLATE(ALbyte
, ALfloat
)
1215 DECL_TEMPLATE(ALbyte
, ALdouble
)
1216 DECL_TEMPLATE(ALbyte
, ALmulaw
)
1218 DECL_TEMPLATE(ALubyte
, ALbyte
)
1219 DECL_TEMPLATE(ALubyte
, ALubyte
)
1220 DECL_TEMPLATE(ALubyte
, ALshort
)
1221 DECL_TEMPLATE(ALubyte
, ALushort
)
1222 DECL_TEMPLATE(ALubyte
, ALint
)
1223 DECL_TEMPLATE(ALubyte
, ALuint
)
1224 DECL_TEMPLATE(ALubyte
, ALfloat
)
1225 DECL_TEMPLATE(ALubyte
, ALdouble
)
1226 DECL_TEMPLATE(ALubyte
, ALmulaw
)
1228 DECL_TEMPLATE(ALshort
, ALbyte
)
1229 DECL_TEMPLATE(ALshort
, ALubyte
)
1230 DECL_TEMPLATE(ALshort
, ALshort
)
1231 DECL_TEMPLATE(ALshort
, ALushort
)
1232 DECL_TEMPLATE(ALshort
, ALint
)
1233 DECL_TEMPLATE(ALshort
, ALuint
)
1234 DECL_TEMPLATE(ALshort
, ALfloat
)
1235 DECL_TEMPLATE(ALshort
, ALdouble
)
1236 DECL_TEMPLATE(ALshort
, ALmulaw
)
1238 DECL_TEMPLATE(ALushort
, ALbyte
)
1239 DECL_TEMPLATE(ALushort
, ALubyte
)
1240 DECL_TEMPLATE(ALushort
, ALshort
)
1241 DECL_TEMPLATE(ALushort
, ALushort
)
1242 DECL_TEMPLATE(ALushort
, ALint
)
1243 DECL_TEMPLATE(ALushort
, ALuint
)
1244 DECL_TEMPLATE(ALushort
, ALfloat
)
1245 DECL_TEMPLATE(ALushort
, ALdouble
)
1246 DECL_TEMPLATE(ALushort
, ALmulaw
)
1248 DECL_TEMPLATE(ALint
, ALbyte
)
1249 DECL_TEMPLATE(ALint
, ALubyte
)
1250 DECL_TEMPLATE(ALint
, ALshort
)
1251 DECL_TEMPLATE(ALint
, ALushort
)
1252 DECL_TEMPLATE(ALint
, ALint
)
1253 DECL_TEMPLATE(ALint
, ALuint
)
1254 DECL_TEMPLATE(ALint
, ALfloat
)
1255 DECL_TEMPLATE(ALint
, ALdouble
)
1256 DECL_TEMPLATE(ALint
, ALmulaw
)
1258 DECL_TEMPLATE(ALuint
, ALbyte
)
1259 DECL_TEMPLATE(ALuint
, ALubyte
)
1260 DECL_TEMPLATE(ALuint
, ALshort
)
1261 DECL_TEMPLATE(ALuint
, ALushort
)
1262 DECL_TEMPLATE(ALuint
, ALint
)
1263 DECL_TEMPLATE(ALuint
, ALuint
)
1264 DECL_TEMPLATE(ALuint
, ALfloat
)
1265 DECL_TEMPLATE(ALuint
, ALdouble
)
1266 DECL_TEMPLATE(ALuint
, ALmulaw
)
1268 DECL_TEMPLATE(ALfloat
, ALbyte
)
1269 DECL_TEMPLATE(ALfloat
, ALubyte
)
1270 DECL_TEMPLATE(ALfloat
, ALshort
)
1271 DECL_TEMPLATE(ALfloat
, ALushort
)
1272 DECL_TEMPLATE(ALfloat
, ALint
)
1273 DECL_TEMPLATE(ALfloat
, ALuint
)
1274 DECL_TEMPLATE(ALfloat
, ALfloat
)
1275 DECL_TEMPLATE(ALfloat
, ALdouble
)
1276 DECL_TEMPLATE(ALfloat
, ALmulaw
)
1278 DECL_TEMPLATE(ALdouble
, ALbyte
)
1279 DECL_TEMPLATE(ALdouble
, ALubyte
)
1280 DECL_TEMPLATE(ALdouble
, ALshort
)
1281 DECL_TEMPLATE(ALdouble
, ALushort
)
1282 DECL_TEMPLATE(ALdouble
, ALint
)
1283 DECL_TEMPLATE(ALdouble
, ALuint
)
1284 DECL_TEMPLATE(ALdouble
, ALfloat
)
1285 DECL_TEMPLATE(ALdouble
, ALdouble
)
1286 DECL_TEMPLATE(ALdouble
, ALmulaw
)
1288 DECL_TEMPLATE(ALmulaw
, ALbyte
)
1289 DECL_TEMPLATE(ALmulaw
, ALubyte
)
1290 DECL_TEMPLATE(ALmulaw
, ALshort
)
1291 DECL_TEMPLATE(ALmulaw
, ALushort
)
1292 DECL_TEMPLATE(ALmulaw
, ALint
)
1293 DECL_TEMPLATE(ALmulaw
, ALuint
)
1294 DECL_TEMPLATE(ALmulaw
, ALfloat
)
1295 DECL_TEMPLATE(ALmulaw
, ALdouble
)
1296 DECL_TEMPLATE(ALmulaw
, ALmulaw
)
1298 #undef DECL_TEMPLATE
1300 #define DECL_TEMPLATE(T) \
1301 static void Convert_##T##_IMA4(T *dst, const ALubyte *src, ALuint numchans, \
1305 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
1306 for(i = 0;i < numblocks;i++) \
1308 DecodeIMA4Block(tmp, src, numchans); \
1309 src += 36*numchans; \
1310 for(j = 0;j < 65*numchans;j++) \
1311 *(dst++) = Conv_##T##_ALshort(tmp[j]); \
1315 DECL_TEMPLATE(ALbyte
)
1316 DECL_TEMPLATE(ALubyte
)
1317 DECL_TEMPLATE(ALshort
)
1318 DECL_TEMPLATE(ALushort
)
1319 DECL_TEMPLATE(ALint
)
1320 DECL_TEMPLATE(ALuint
)
1321 DECL_TEMPLATE(ALfloat
)
1322 DECL_TEMPLATE(ALdouble
)
1323 DECL_TEMPLATE(ALmulaw
)
1325 #undef DECL_TEMPLATE
1327 #define DECL_TEMPLATE(T) \
1328 static void Convert_IMA4_##T(ALubyte *dst, const T *src, ALuint numchans, \
1332 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
1333 ALint sample[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
1334 ALint index[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
1335 for(i = 0;i < numblocks;i++) \
1337 for(j = 0;j < 65*numchans;j++) \
1338 tmp[j] = Conv_ALshort_##T(*(src++)); \
1339 EncodeIMA4Block(dst, tmp, sample, index, numchans); \
1340 dst += 36*numchans; \
1344 DECL_TEMPLATE(ALbyte
)
1345 DECL_TEMPLATE(ALubyte
)
1346 DECL_TEMPLATE(ALshort
)
1347 DECL_TEMPLATE(ALushort
)
1348 DECL_TEMPLATE(ALint
)
1349 DECL_TEMPLATE(ALuint
)
1350 DECL_TEMPLATE(ALfloat
)
1351 DECL_TEMPLATE(ALdouble
)
1352 DECL_TEMPLATE(ALmulaw
)
1354 #undef DECL_TEMPLATE
1356 #define DECL_TEMPLATE(T) \
1357 static void Convert_##T(T *dst, const ALvoid *src, enum UserFmtType srcType, \
1363 Convert_##T##_ALbyte(dst, src, len); \
1365 case UserFmtUByte: \
1366 Convert_##T##_ALubyte(dst, src, len); \
1368 case UserFmtShort: \
1369 Convert_##T##_ALshort(dst, src, len); \
1371 case UserFmtUShort: \
1372 Convert_##T##_ALushort(dst, src, len); \
1375 Convert_##T##_ALint(dst, src, len); \
1378 Convert_##T##_ALuint(dst, src, len); \
1380 case UserFmtFloat: \
1381 Convert_##T##_ALfloat(dst, src, len); \
1383 case UserFmtDouble: \
1384 Convert_##T##_ALdouble(dst, src, len); \
1386 case UserFmtMulaw: \
1387 Convert_##T##_ALmulaw(dst, src, len); \
1390 break; /* not handled here */ \
1394 DECL_TEMPLATE(ALbyte
)
1395 DECL_TEMPLATE(ALubyte
)
1396 DECL_TEMPLATE(ALshort
)
1397 DECL_TEMPLATE(ALushort
)
1398 DECL_TEMPLATE(ALint
)
1399 DECL_TEMPLATE(ALuint
)
1400 DECL_TEMPLATE(ALfloat
)
1401 DECL_TEMPLATE(ALdouble
)
1402 DECL_TEMPLATE(ALmulaw
)
1404 #undef DECL_TEMPLATE
1406 static void Convert_IMA4(ALubyte
*dst
, const ALvoid
*src
, enum UserFmtType srcType
,
1407 ALint chans
, ALsizei len
)
1412 Convert_IMA4_ALbyte(dst
, src
, chans
, len
);
1415 Convert_IMA4_ALubyte(dst
, src
, chans
, len
);
1418 Convert_IMA4_ALshort(dst
, src
, chans
, len
);
1421 Convert_IMA4_ALushort(dst
, src
, chans
, len
);
1424 Convert_IMA4_ALint(dst
, src
, chans
, len
);
1427 Convert_IMA4_ALuint(dst
, src
, chans
, len
);
1430 Convert_IMA4_ALfloat(dst
, src
, chans
, len
);
1433 Convert_IMA4_ALdouble(dst
, src
, chans
, len
);
1436 Convert_IMA4_ALmulaw(dst
, src
, chans
, len
);
1439 memcpy(dst
, src
, len
*36*chans
);
1445 static void ConvertData(ALvoid
*dst
, enum FmtType dstType
, const ALvoid
*src
, enum UserFmtType srcType
, ALsizei len
)
1449 (void)Convert_ALbyte
;
1451 Convert_ALubyte(dst
, src
, srcType
, len
);
1454 Convert_ALshort(dst
, src
, srcType
, len
);
1456 (void)Convert_ALushort
;
1457 (void)Convert_ALint
;
1458 (void)Convert_ALuint
;
1460 Convert_ALfloat(dst
, src
, srcType
, len
);
1462 (void)Convert_ALdouble
;
1463 (void)Convert_ALmulaw
;
1468 static void ConvertDataIMA4(ALvoid
*dst
, enum FmtType dstType
, const ALvoid
*src
, ALint chans
, ALsizei len
)
1472 (void)Convert_ALbyte_IMA4
;
1474 Convert_ALubyte_IMA4(dst
, src
, chans
, len
);
1477 Convert_ALshort_IMA4(dst
, src
, chans
, len
);
1479 (void)Convert_ALushort_IMA4
;
1480 (void)Convert_ALint_IMA4
;
1481 (void)Convert_ALuint_IMA4
;
1483 Convert_ALfloat_IMA4(dst
, src
, chans
, len
);
1485 (void)Convert_ALdouble_IMA4
;
1486 (void)Convert_ALmulaw_IMA4
;
1494 * Loads the specified data into the buffer, using the specified formats.
1495 * Currently, the new format must have the same channel configuration as the
1498 static ALenum
LoadData(ALbuffer
*ALBuf
, ALuint freq
, ALenum NewFormat
, ALsizei size
, enum UserFmtChannels SrcChannels
, enum UserFmtType SrcType
, const ALvoid
*data
)
1500 ALuint NewChannels
, NewBytes
;
1501 enum FmtChannels DstChannels
;
1502 enum FmtType DstType
;
1506 DecomposeFormat(NewFormat
, &DstChannels
, &DstType
);
1507 NewChannels
= ChannelsFromFmt(DstChannels
);
1508 NewBytes
= BytesFromFmt(DstType
);
1510 assert(SrcChannels
== DstChannels
);
1512 if(SrcType
== UserFmtIMA4
)
1514 ALuint OrigChannels
= ChannelsFromUserFmt(SrcChannels
);
1516 /* Here is where things vary:
1517 * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
1518 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
1520 if((size
%(36*OrigChannels
)) != 0)
1521 return AL_INVALID_VALUE
;
1523 newsize
= size
/ 36;
1525 newsize
*= NewBytes
;
1526 if(newsize
> INT_MAX
)
1527 return AL_OUT_OF_MEMORY
;
1529 temp
= realloc(ALBuf
->data
, newsize
);
1530 if(!temp
&& newsize
) return AL_OUT_OF_MEMORY
;
1532 ALBuf
->size
= newsize
;
1535 ConvertDataIMA4(ALBuf
->data
, DstType
, data
, OrigChannels
,
1536 newsize
/(65*NewChannels
*NewBytes
));
1538 ALBuf
->OriginalChannels
= SrcChannels
;
1539 ALBuf
->OriginalType
= SrcType
;
1540 ALBuf
->OriginalSize
= size
;
1541 ALBuf
->OriginalAlign
= 36 * OrigChannels
;
1545 ALuint OrigBytes
= BytesFromUserFmt(SrcType
);
1546 ALuint OrigChannels
= ChannelsFromUserFmt(SrcChannels
);
1548 if((size
%(OrigBytes
*OrigChannels
)) != 0)
1549 return AL_INVALID_VALUE
;
1551 newsize
= size
/ OrigBytes
;
1552 newsize
*= NewBytes
;
1553 if(newsize
> INT_MAX
)
1554 return AL_OUT_OF_MEMORY
;
1556 temp
= realloc(ALBuf
->data
, newsize
);
1557 if(!temp
&& newsize
) return AL_OUT_OF_MEMORY
;
1559 ALBuf
->size
= newsize
;
1562 ConvertData(ALBuf
->data
, DstType
, data
, SrcType
, newsize
/NewBytes
);
1564 ALBuf
->OriginalChannels
= SrcChannels
;
1565 ALBuf
->OriginalType
= SrcType
;
1566 ALBuf
->OriginalSize
= size
;
1567 ALBuf
->OriginalAlign
= OrigBytes
* OrigChannels
;
1570 ALBuf
->Frequency
= freq
;
1571 ALBuf
->FmtChannels
= DstChannels
;
1572 ALBuf
->FmtType
= DstType
;
1574 ALBuf
->LoopStart
= 0;
1575 ALBuf
->LoopEnd
= newsize
/ NewChannels
/ NewBytes
;
1581 ALuint
BytesFromUserFmt(enum UserFmtType type
)
1585 case UserFmtByte
: return sizeof(ALbyte
);
1586 case UserFmtUByte
: return sizeof(ALubyte
);
1587 case UserFmtShort
: return sizeof(ALshort
);
1588 case UserFmtUShort
: return sizeof(ALushort
);
1589 case UserFmtInt
: return sizeof(ALint
);
1590 case UserFmtUInt
: return sizeof(ALuint
);
1591 case UserFmtFloat
: return sizeof(ALfloat
);
1592 case UserFmtDouble
: return sizeof(ALdouble
);
1593 case UserFmtMulaw
: return sizeof(ALubyte
);
1594 case UserFmtIMA4
: break; /* not handled here */
1598 ALuint
ChannelsFromUserFmt(enum UserFmtChannels chans
)
1602 case UserFmtMono
: return 1;
1603 case UserFmtStereo
: return 2;
1604 case UserFmtRear
: return 2;
1605 case UserFmtQuad
: return 4;
1606 case UserFmtX51
: return 6;
1607 case UserFmtX61
: return 7;
1608 case UserFmtX71
: return 8;
1612 ALboolean
DecomposeUserFormat(ALenum format
, enum UserFmtChannels
*chans
,
1613 enum UserFmtType
*type
)
1617 case AL_FORMAT_MONO8
:
1618 *chans
= UserFmtMono
;
1619 *type
= UserFmtUByte
;
1621 case AL_FORMAT_MONO16
:
1622 *chans
= UserFmtMono
;
1623 *type
= UserFmtShort
;
1625 case AL_FORMAT_MONO_FLOAT32
:
1626 *chans
= UserFmtMono
;
1627 *type
= UserFmtFloat
;
1629 case AL_FORMAT_MONO_DOUBLE_EXT
:
1630 *chans
= UserFmtMono
;
1631 *type
= UserFmtDouble
;
1633 case AL_FORMAT_MONO_IMA4
:
1634 *chans
= UserFmtMono
;
1635 *type
= UserFmtIMA4
;
1637 case AL_FORMAT_STEREO8
:
1638 *chans
= UserFmtStereo
;
1639 *type
= UserFmtUByte
;
1641 case AL_FORMAT_STEREO16
:
1642 *chans
= UserFmtStereo
;
1643 *type
= UserFmtShort
;
1645 case AL_FORMAT_STEREO_FLOAT32
:
1646 *chans
= UserFmtStereo
;
1647 *type
= UserFmtFloat
;
1649 case AL_FORMAT_STEREO_DOUBLE_EXT
:
1650 *chans
= UserFmtStereo
;
1651 *type
= UserFmtDouble
;
1653 case AL_FORMAT_STEREO_IMA4
:
1654 *chans
= UserFmtStereo
;
1655 *type
= UserFmtIMA4
;
1657 case AL_FORMAT_QUAD8_LOKI
:
1658 case AL_FORMAT_QUAD8
:
1659 *chans
= UserFmtQuad
;
1660 *type
= UserFmtUByte
;
1662 case AL_FORMAT_QUAD16_LOKI
:
1663 case AL_FORMAT_QUAD16
:
1664 *chans
= UserFmtQuad
;
1665 *type
= UserFmtShort
;
1667 case AL_FORMAT_QUAD32
:
1668 *chans
= UserFmtQuad
;
1669 *type
= UserFmtFloat
;
1671 case AL_FORMAT_REAR8
:
1672 *chans
= UserFmtRear
;
1673 *type
= UserFmtUByte
;
1675 case AL_FORMAT_REAR16
:
1676 *chans
= UserFmtRear
;
1677 *type
= UserFmtShort
;
1679 case AL_FORMAT_REAR32
:
1680 *chans
= UserFmtRear
;
1681 *type
= UserFmtFloat
;
1683 case AL_FORMAT_51CHN8
:
1684 *chans
= UserFmtX51
;
1685 *type
= UserFmtUByte
;
1687 case AL_FORMAT_51CHN16
:
1688 *chans
= UserFmtX51
;
1689 *type
= UserFmtShort
;
1691 case AL_FORMAT_51CHN32
:
1692 *chans
= UserFmtX51
;
1693 *type
= UserFmtFloat
;
1695 case AL_FORMAT_61CHN8
:
1696 *chans
= UserFmtX61
;
1697 *type
= UserFmtUByte
;
1699 case AL_FORMAT_61CHN16
:
1700 *chans
= UserFmtX61
;
1701 *type
= UserFmtShort
;
1703 case AL_FORMAT_61CHN32
:
1704 *chans
= UserFmtX61
;
1705 *type
= UserFmtFloat
;
1707 case AL_FORMAT_71CHN8
:
1708 *chans
= UserFmtX71
;
1709 *type
= UserFmtUByte
;
1711 case AL_FORMAT_71CHN16
:
1712 *chans
= UserFmtX71
;
1713 *type
= UserFmtShort
;
1715 case AL_FORMAT_71CHN32
:
1716 *chans
= UserFmtX71
;
1717 *type
= UserFmtFloat
;
1719 case AL_FORMAT_MONO_MULAW
:
1720 *chans
= UserFmtMono
;
1721 *type
= UserFmtMulaw
;
1723 case AL_FORMAT_STEREO_MULAW
:
1724 *chans
= UserFmtStereo
;
1725 *type
= UserFmtMulaw
;
1727 case AL_FORMAT_QUAD_MULAW
:
1728 *chans
= UserFmtQuad
;
1729 *type
= UserFmtMulaw
;
1731 case AL_FORMAT_REAR_MULAW
:
1732 *chans
= UserFmtRear
;
1733 *type
= UserFmtMulaw
;
1735 case AL_FORMAT_51CHN_MULAW
:
1736 *chans
= UserFmtX51
;
1737 *type
= UserFmtMulaw
;
1739 case AL_FORMAT_61CHN_MULAW
:
1740 *chans
= UserFmtX61
;
1741 *type
= UserFmtMulaw
;
1743 case AL_FORMAT_71CHN_MULAW
:
1744 *chans
= UserFmtX71
;
1745 *type
= UserFmtMulaw
;
1751 ALuint
BytesFromFmt(enum FmtType type
)
1755 case FmtUByte
: return sizeof(ALubyte
);
1756 case FmtShort
: return sizeof(ALshort
);
1757 case FmtFloat
: return sizeof(ALfloat
);
1761 ALuint
ChannelsFromFmt(enum FmtChannels chans
)
1765 case FmtMono
: return 1;
1766 case FmtStereo
: return 2;
1767 case FmtRear
: return 2;
1768 case FmtQuad
: return 4;
1769 case FmtX51
: return 6;
1770 case FmtX61
: return 7;
1771 case FmtX71
: return 8;
1775 ALboolean
DecomposeFormat(ALenum format
, enum FmtChannels
*chans
, enum FmtType
*type
)
1779 case AL_FORMAT_MONO8
:
1783 case AL_FORMAT_MONO16
:
1787 case AL_FORMAT_MONO_FLOAT32
:
1791 case AL_FORMAT_STEREO8
:
1795 case AL_FORMAT_STEREO16
:
1799 case AL_FORMAT_STEREO_FLOAT32
:
1803 case AL_FORMAT_QUAD8_LOKI
:
1804 case AL_FORMAT_QUAD8
:
1808 case AL_FORMAT_QUAD16_LOKI
:
1809 case AL_FORMAT_QUAD16
:
1813 case AL_FORMAT_QUAD32
:
1817 case AL_FORMAT_REAR8
:
1821 case AL_FORMAT_REAR16
:
1825 case AL_FORMAT_REAR32
:
1829 case AL_FORMAT_51CHN8
:
1833 case AL_FORMAT_51CHN16
:
1837 case AL_FORMAT_51CHN32
:
1841 case AL_FORMAT_61CHN8
:
1845 case AL_FORMAT_61CHN16
:
1849 case AL_FORMAT_61CHN32
:
1853 case AL_FORMAT_71CHN8
:
1857 case AL_FORMAT_71CHN16
:
1861 case AL_FORMAT_71CHN32
:
1871 * ReleaseALBuffers()
1873 * INTERNAL: Called to destroy any buffers that still exist on the device
1875 ALvoid
ReleaseALBuffers(ALCdevice
*device
)
1878 for(i
= 0;i
< device
->BufferMap
.size
;i
++)
1880 ALbuffer
*temp
= device
->BufferMap
.array
[i
].value
;
1881 device
->BufferMap
.array
[i
].value
= NULL
;
1885 ALTHUNK_REMOVEENTRY(temp
->buffer
);
1886 memset(temp
, 0, sizeof(ALbuffer
));