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
)
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
-32768; }
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
+32768; }
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
-2147483648u; }
1104 static __inline ALint
Conv_ALint_ALfloat(ALfloat val
)
1106 if(val
> 1.0f
) return 2147483647;
1107 if(val
< -1.0f
) return 0u-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 0u-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
+2147483648u; }
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 static __inline ALmulaw
Conv_ALmulaw_ALmulaw(ALmulaw val
)
1199 #undef DECL_TEMPLATE
1201 #define DECL_TEMPLATE(T1, T2) \
1202 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint len) \
1205 for(i = 0;i < len;i++) \
1206 *(dst++) = Conv_##T1##_##T2(*(src++)); \
1209 DECL_TEMPLATE(ALbyte
, ALbyte
)
1210 DECL_TEMPLATE(ALbyte
, ALubyte
)
1211 DECL_TEMPLATE(ALbyte
, ALshort
)
1212 DECL_TEMPLATE(ALbyte
, ALushort
)
1213 DECL_TEMPLATE(ALbyte
, ALint
)
1214 DECL_TEMPLATE(ALbyte
, ALuint
)
1215 DECL_TEMPLATE(ALbyte
, ALfloat
)
1216 DECL_TEMPLATE(ALbyte
, ALdouble
)
1217 DECL_TEMPLATE(ALbyte
, ALmulaw
)
1219 DECL_TEMPLATE(ALubyte
, ALbyte
)
1220 DECL_TEMPLATE(ALubyte
, ALubyte
)
1221 DECL_TEMPLATE(ALubyte
, ALshort
)
1222 DECL_TEMPLATE(ALubyte
, ALushort
)
1223 DECL_TEMPLATE(ALubyte
, ALint
)
1224 DECL_TEMPLATE(ALubyte
, ALuint
)
1225 DECL_TEMPLATE(ALubyte
, ALfloat
)
1226 DECL_TEMPLATE(ALubyte
, ALdouble
)
1227 DECL_TEMPLATE(ALubyte
, ALmulaw
)
1229 DECL_TEMPLATE(ALshort
, ALbyte
)
1230 DECL_TEMPLATE(ALshort
, ALubyte
)
1231 DECL_TEMPLATE(ALshort
, ALshort
)
1232 DECL_TEMPLATE(ALshort
, ALushort
)
1233 DECL_TEMPLATE(ALshort
, ALint
)
1234 DECL_TEMPLATE(ALshort
, ALuint
)
1235 DECL_TEMPLATE(ALshort
, ALfloat
)
1236 DECL_TEMPLATE(ALshort
, ALdouble
)
1237 DECL_TEMPLATE(ALshort
, ALmulaw
)
1239 DECL_TEMPLATE(ALushort
, ALbyte
)
1240 DECL_TEMPLATE(ALushort
, ALubyte
)
1241 DECL_TEMPLATE(ALushort
, ALshort
)
1242 DECL_TEMPLATE(ALushort
, ALushort
)
1243 DECL_TEMPLATE(ALushort
, ALint
)
1244 DECL_TEMPLATE(ALushort
, ALuint
)
1245 DECL_TEMPLATE(ALushort
, ALfloat
)
1246 DECL_TEMPLATE(ALushort
, ALdouble
)
1247 DECL_TEMPLATE(ALushort
, ALmulaw
)
1249 DECL_TEMPLATE(ALint
, ALbyte
)
1250 DECL_TEMPLATE(ALint
, ALubyte
)
1251 DECL_TEMPLATE(ALint
, ALshort
)
1252 DECL_TEMPLATE(ALint
, ALushort
)
1253 DECL_TEMPLATE(ALint
, ALint
)
1254 DECL_TEMPLATE(ALint
, ALuint
)
1255 DECL_TEMPLATE(ALint
, ALfloat
)
1256 DECL_TEMPLATE(ALint
, ALdouble
)
1257 DECL_TEMPLATE(ALint
, ALmulaw
)
1259 DECL_TEMPLATE(ALuint
, ALbyte
)
1260 DECL_TEMPLATE(ALuint
, ALubyte
)
1261 DECL_TEMPLATE(ALuint
, ALshort
)
1262 DECL_TEMPLATE(ALuint
, ALushort
)
1263 DECL_TEMPLATE(ALuint
, ALint
)
1264 DECL_TEMPLATE(ALuint
, ALuint
)
1265 DECL_TEMPLATE(ALuint
, ALfloat
)
1266 DECL_TEMPLATE(ALuint
, ALdouble
)
1267 DECL_TEMPLATE(ALuint
, ALmulaw
)
1269 DECL_TEMPLATE(ALfloat
, ALbyte
)
1270 DECL_TEMPLATE(ALfloat
, ALubyte
)
1271 DECL_TEMPLATE(ALfloat
, ALshort
)
1272 DECL_TEMPLATE(ALfloat
, ALushort
)
1273 DECL_TEMPLATE(ALfloat
, ALint
)
1274 DECL_TEMPLATE(ALfloat
, ALuint
)
1275 DECL_TEMPLATE(ALfloat
, ALfloat
)
1276 DECL_TEMPLATE(ALfloat
, ALdouble
)
1277 DECL_TEMPLATE(ALfloat
, ALmulaw
)
1279 DECL_TEMPLATE(ALdouble
, ALbyte
)
1280 DECL_TEMPLATE(ALdouble
, ALubyte
)
1281 DECL_TEMPLATE(ALdouble
, ALshort
)
1282 DECL_TEMPLATE(ALdouble
, ALushort
)
1283 DECL_TEMPLATE(ALdouble
, ALint
)
1284 DECL_TEMPLATE(ALdouble
, ALuint
)
1285 DECL_TEMPLATE(ALdouble
, ALfloat
)
1286 DECL_TEMPLATE(ALdouble
, ALdouble
)
1287 DECL_TEMPLATE(ALdouble
, ALmulaw
)
1289 DECL_TEMPLATE(ALmulaw
, ALbyte
)
1290 DECL_TEMPLATE(ALmulaw
, ALubyte
)
1291 DECL_TEMPLATE(ALmulaw
, ALshort
)
1292 DECL_TEMPLATE(ALmulaw
, ALushort
)
1293 DECL_TEMPLATE(ALmulaw
, ALint
)
1294 DECL_TEMPLATE(ALmulaw
, ALuint
)
1295 DECL_TEMPLATE(ALmulaw
, ALfloat
)
1296 DECL_TEMPLATE(ALmulaw
, ALdouble
)
1297 DECL_TEMPLATE(ALmulaw
, ALmulaw
)
1299 #undef DECL_TEMPLATE
1301 #define DECL_TEMPLATE(T) \
1302 static void Convert_##T##_IMA4(T *dst, const ALubyte *src, ALuint numchans, \
1306 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
1307 for(i = 0;i < numblocks;i++) \
1309 DecodeIMA4Block(tmp, src, numchans); \
1310 src += 36*numchans; \
1311 for(j = 0;j < 65*numchans;j++) \
1312 *(dst++) = Conv_##T##_ALshort(tmp[j]); \
1316 DECL_TEMPLATE(ALbyte
)
1317 DECL_TEMPLATE(ALubyte
)
1318 DECL_TEMPLATE(ALshort
)
1319 DECL_TEMPLATE(ALushort
)
1320 DECL_TEMPLATE(ALint
)
1321 DECL_TEMPLATE(ALuint
)
1322 DECL_TEMPLATE(ALfloat
)
1323 DECL_TEMPLATE(ALdouble
)
1324 DECL_TEMPLATE(ALmulaw
)
1326 #undef DECL_TEMPLATE
1328 #define DECL_TEMPLATE(T) \
1329 static void Convert_IMA4_##T(ALubyte *dst, const T *src, ALuint numchans, \
1333 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
1334 ALint sample[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
1335 ALint index[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
1336 for(i = 0;i < numblocks;i++) \
1338 for(j = 0;j < 65*numchans;j++) \
1339 tmp[j] = Conv_ALshort_##T(*(src++)); \
1340 EncodeIMA4Block(dst, tmp, sample, index, numchans); \
1341 dst += 36*numchans; \
1345 DECL_TEMPLATE(ALbyte
)
1346 DECL_TEMPLATE(ALubyte
)
1347 DECL_TEMPLATE(ALshort
)
1348 DECL_TEMPLATE(ALushort
)
1349 DECL_TEMPLATE(ALint
)
1350 DECL_TEMPLATE(ALuint
)
1351 DECL_TEMPLATE(ALfloat
)
1352 DECL_TEMPLATE(ALdouble
)
1353 DECL_TEMPLATE(ALmulaw
)
1355 #undef DECL_TEMPLATE
1357 static void Convert_IMA4_IMA4(ALubyte
*dst
, const ALubyte
*src
, ALuint numchans
,
1360 memcpy(dst
, src
, numblocks
*36*numchans
);
1363 #define DECL_TEMPLATE(T) \
1364 static void Convert_##T(T *dst, const ALvoid *src, enum UserFmtType srcType, \
1370 Convert_##T##_ALbyte(dst, src, len); \
1372 case UserFmtUByte: \
1373 Convert_##T##_ALubyte(dst, src, len); \
1375 case UserFmtShort: \
1376 Convert_##T##_ALshort(dst, src, len); \
1378 case UserFmtUShort: \
1379 Convert_##T##_ALushort(dst, src, len); \
1382 Convert_##T##_ALint(dst, src, len); \
1385 Convert_##T##_ALuint(dst, src, len); \
1387 case UserFmtFloat: \
1388 Convert_##T##_ALfloat(dst, src, len); \
1390 case UserFmtDouble: \
1391 Convert_##T##_ALdouble(dst, src, len); \
1393 case UserFmtMulaw: \
1394 Convert_##T##_ALmulaw(dst, src, len); \
1397 break; /* not handled here */ \
1401 DECL_TEMPLATE(ALbyte
)
1402 DECL_TEMPLATE(ALubyte
)
1403 DECL_TEMPLATE(ALshort
)
1404 DECL_TEMPLATE(ALushort
)
1405 DECL_TEMPLATE(ALint
)
1406 DECL_TEMPLATE(ALuint
)
1407 DECL_TEMPLATE(ALfloat
)
1408 DECL_TEMPLATE(ALdouble
)
1409 DECL_TEMPLATE(ALmulaw
)
1411 #undef DECL_TEMPLATE
1413 static void Convert_IMA4(ALubyte
*dst
, const ALvoid
*src
, enum UserFmtType srcType
,
1414 ALint chans
, ALsizei len
)
1419 Convert_IMA4_ALbyte(dst
, src
, chans
, len
);
1422 Convert_IMA4_ALubyte(dst
, src
, chans
, len
);
1425 Convert_IMA4_ALshort(dst
, src
, chans
, len
);
1428 Convert_IMA4_ALushort(dst
, src
, chans
, len
);
1431 Convert_IMA4_ALint(dst
, src
, chans
, len
);
1434 Convert_IMA4_ALuint(dst
, src
, chans
, len
);
1437 Convert_IMA4_ALfloat(dst
, src
, chans
, len
);
1440 Convert_IMA4_ALdouble(dst
, src
, chans
, len
);
1443 Convert_IMA4_ALmulaw(dst
, src
, chans
, len
);
1446 Convert_IMA4_IMA4(dst
, src
, chans
, len
);
1452 static void ConvertData(ALvoid
*dst
, enum FmtType dstType
, const ALvoid
*src
, enum UserFmtType srcType
, ALsizei len
)
1456 (void)Convert_ALbyte
;
1458 Convert_ALubyte(dst
, src
, srcType
, len
);
1461 Convert_ALshort(dst
, src
, srcType
, len
);
1463 (void)Convert_ALushort
;
1464 (void)Convert_ALint
;
1465 (void)Convert_ALuint
;
1467 Convert_ALfloat(dst
, src
, srcType
, len
);
1469 (void)Convert_ALdouble
;
1470 (void)Convert_ALmulaw
;
1475 static void ConvertDataIMA4(ALvoid
*dst
, enum FmtType dstType
, const ALvoid
*src
, ALint chans
, ALsizei len
)
1479 (void)Convert_ALbyte_IMA4
;
1481 Convert_ALubyte_IMA4(dst
, src
, chans
, len
);
1484 Convert_ALshort_IMA4(dst
, src
, chans
, len
);
1486 (void)Convert_ALushort_IMA4
;
1487 (void)Convert_ALint_IMA4
;
1488 (void)Convert_ALuint_IMA4
;
1490 Convert_ALfloat_IMA4(dst
, src
, chans
, len
);
1492 (void)Convert_ALdouble_IMA4
;
1493 (void)Convert_ALmulaw_IMA4
;
1501 * Loads the specified data into the buffer, using the specified formats.
1502 * Currently, the new format must have the same channel configuration as the
1505 static ALenum
LoadData(ALbuffer
*ALBuf
, ALuint freq
, ALenum NewFormat
, ALsizei size
, enum UserFmtChannels SrcChannels
, enum UserFmtType SrcType
, const ALvoid
*data
)
1507 ALuint NewChannels
, NewBytes
;
1508 enum FmtChannels DstChannels
;
1509 enum FmtType DstType
;
1513 DecomposeFormat(NewFormat
, &DstChannels
, &DstType
);
1514 NewChannels
= ChannelsFromFmt(DstChannels
);
1515 NewBytes
= BytesFromFmt(DstType
);
1517 assert(SrcChannels
== DstChannels
);
1519 if(SrcType
== UserFmtIMA4
)
1521 ALuint OrigChannels
= ChannelsFromUserFmt(SrcChannels
);
1523 /* Here is where things vary:
1524 * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
1525 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
1527 if((size
%(36*OrigChannels
)) != 0)
1528 return AL_INVALID_VALUE
;
1530 newsize
= size
/ 36;
1532 newsize
*= NewBytes
;
1533 if(newsize
> INT_MAX
)
1534 return AL_OUT_OF_MEMORY
;
1536 temp
= realloc(ALBuf
->data
, newsize
);
1537 if(!temp
&& newsize
) return AL_OUT_OF_MEMORY
;
1539 ALBuf
->size
= newsize
;
1542 ConvertDataIMA4(ALBuf
->data
, DstType
, data
, OrigChannels
,
1543 newsize
/(65*NewChannels
*NewBytes
));
1545 ALBuf
->OriginalChannels
= SrcChannels
;
1546 ALBuf
->OriginalType
= SrcType
;
1547 ALBuf
->OriginalSize
= size
;
1548 ALBuf
->OriginalAlign
= 36 * OrigChannels
;
1552 ALuint OrigBytes
= BytesFromUserFmt(SrcType
);
1553 ALuint OrigChannels
= ChannelsFromUserFmt(SrcChannels
);
1555 if((size
%(OrigBytes
*OrigChannels
)) != 0)
1556 return AL_INVALID_VALUE
;
1558 newsize
= size
/ OrigBytes
;
1559 newsize
*= NewBytes
;
1560 if(newsize
> INT_MAX
)
1561 return AL_OUT_OF_MEMORY
;
1563 temp
= realloc(ALBuf
->data
, newsize
);
1564 if(!temp
&& newsize
) return AL_OUT_OF_MEMORY
;
1566 ALBuf
->size
= newsize
;
1569 ConvertData(ALBuf
->data
, DstType
, data
, SrcType
, newsize
/NewBytes
);
1571 ALBuf
->OriginalChannels
= SrcChannels
;
1572 ALBuf
->OriginalType
= SrcType
;
1573 ALBuf
->OriginalSize
= size
;
1574 ALBuf
->OriginalAlign
= OrigBytes
* OrigChannels
;
1577 ALBuf
->Frequency
= freq
;
1578 ALBuf
->FmtChannels
= DstChannels
;
1579 ALBuf
->FmtType
= DstType
;
1581 ALBuf
->LoopStart
= 0;
1582 ALBuf
->LoopEnd
= newsize
/ NewChannels
/ NewBytes
;
1588 ALuint
BytesFromUserFmt(enum UserFmtType type
)
1592 case UserFmtByte
: return sizeof(ALbyte
);
1593 case UserFmtUByte
: return sizeof(ALubyte
);
1594 case UserFmtShort
: return sizeof(ALshort
);
1595 case UserFmtUShort
: return sizeof(ALushort
);
1596 case UserFmtInt
: return sizeof(ALint
);
1597 case UserFmtUInt
: return sizeof(ALuint
);
1598 case UserFmtFloat
: return sizeof(ALfloat
);
1599 case UserFmtDouble
: return sizeof(ALdouble
);
1600 case UserFmtMulaw
: return sizeof(ALubyte
);
1601 case UserFmtIMA4
: break; /* not handled here */
1605 ALuint
ChannelsFromUserFmt(enum UserFmtChannels chans
)
1609 case UserFmtMono
: return 1;
1610 case UserFmtStereo
: return 2;
1611 case UserFmtRear
: return 2;
1612 case UserFmtQuad
: return 4;
1613 case UserFmtX51
: return 6;
1614 case UserFmtX61
: return 7;
1615 case UserFmtX71
: return 8;
1619 ALboolean
DecomposeUserFormat(ALenum format
, enum UserFmtChannels
*chans
,
1620 enum UserFmtType
*type
)
1624 case AL_FORMAT_MONO8
:
1625 *chans
= UserFmtMono
;
1626 *type
= UserFmtUByte
;
1628 case AL_FORMAT_MONO16
:
1629 *chans
= UserFmtMono
;
1630 *type
= UserFmtShort
;
1632 case AL_FORMAT_MONO_FLOAT32
:
1633 *chans
= UserFmtMono
;
1634 *type
= UserFmtFloat
;
1636 case AL_FORMAT_MONO_DOUBLE_EXT
:
1637 *chans
= UserFmtMono
;
1638 *type
= UserFmtDouble
;
1640 case AL_FORMAT_MONO_IMA4
:
1641 *chans
= UserFmtMono
;
1642 *type
= UserFmtIMA4
;
1644 case AL_FORMAT_STEREO8
:
1645 *chans
= UserFmtStereo
;
1646 *type
= UserFmtUByte
;
1648 case AL_FORMAT_STEREO16
:
1649 *chans
= UserFmtStereo
;
1650 *type
= UserFmtShort
;
1652 case AL_FORMAT_STEREO_FLOAT32
:
1653 *chans
= UserFmtStereo
;
1654 *type
= UserFmtFloat
;
1656 case AL_FORMAT_STEREO_DOUBLE_EXT
:
1657 *chans
= UserFmtStereo
;
1658 *type
= UserFmtDouble
;
1660 case AL_FORMAT_STEREO_IMA4
:
1661 *chans
= UserFmtStereo
;
1662 *type
= UserFmtIMA4
;
1664 case AL_FORMAT_QUAD8_LOKI
:
1665 case AL_FORMAT_QUAD8
:
1666 *chans
= UserFmtQuad
;
1667 *type
= UserFmtUByte
;
1669 case AL_FORMAT_QUAD16_LOKI
:
1670 case AL_FORMAT_QUAD16
:
1671 *chans
= UserFmtQuad
;
1672 *type
= UserFmtShort
;
1674 case AL_FORMAT_QUAD32
:
1675 *chans
= UserFmtQuad
;
1676 *type
= UserFmtFloat
;
1678 case AL_FORMAT_REAR8
:
1679 *chans
= UserFmtRear
;
1680 *type
= UserFmtUByte
;
1682 case AL_FORMAT_REAR16
:
1683 *chans
= UserFmtRear
;
1684 *type
= UserFmtShort
;
1686 case AL_FORMAT_REAR32
:
1687 *chans
= UserFmtRear
;
1688 *type
= UserFmtFloat
;
1690 case AL_FORMAT_51CHN8
:
1691 *chans
= UserFmtX51
;
1692 *type
= UserFmtUByte
;
1694 case AL_FORMAT_51CHN16
:
1695 *chans
= UserFmtX51
;
1696 *type
= UserFmtShort
;
1698 case AL_FORMAT_51CHN32
:
1699 *chans
= UserFmtX51
;
1700 *type
= UserFmtFloat
;
1702 case AL_FORMAT_61CHN8
:
1703 *chans
= UserFmtX61
;
1704 *type
= UserFmtUByte
;
1706 case AL_FORMAT_61CHN16
:
1707 *chans
= UserFmtX61
;
1708 *type
= UserFmtShort
;
1710 case AL_FORMAT_61CHN32
:
1711 *chans
= UserFmtX61
;
1712 *type
= UserFmtFloat
;
1714 case AL_FORMAT_71CHN8
:
1715 *chans
= UserFmtX71
;
1716 *type
= UserFmtUByte
;
1718 case AL_FORMAT_71CHN16
:
1719 *chans
= UserFmtX71
;
1720 *type
= UserFmtShort
;
1722 case AL_FORMAT_71CHN32
:
1723 *chans
= UserFmtX71
;
1724 *type
= UserFmtFloat
;
1726 case AL_FORMAT_MONO_MULAW
:
1727 *chans
= UserFmtMono
;
1728 *type
= UserFmtMulaw
;
1730 case AL_FORMAT_STEREO_MULAW
:
1731 *chans
= UserFmtStereo
;
1732 *type
= UserFmtMulaw
;
1734 case AL_FORMAT_QUAD_MULAW
:
1735 *chans
= UserFmtQuad
;
1736 *type
= UserFmtMulaw
;
1738 case AL_FORMAT_REAR_MULAW
:
1739 *chans
= UserFmtRear
;
1740 *type
= UserFmtMulaw
;
1742 case AL_FORMAT_51CHN_MULAW
:
1743 *chans
= UserFmtX51
;
1744 *type
= UserFmtMulaw
;
1746 case AL_FORMAT_61CHN_MULAW
:
1747 *chans
= UserFmtX61
;
1748 *type
= UserFmtMulaw
;
1750 case AL_FORMAT_71CHN_MULAW
:
1751 *chans
= UserFmtX71
;
1752 *type
= UserFmtMulaw
;
1758 ALuint
BytesFromFmt(enum FmtType type
)
1762 case FmtUByte
: return sizeof(ALubyte
);
1763 case FmtShort
: return sizeof(ALshort
);
1764 case FmtFloat
: return sizeof(ALfloat
);
1768 ALuint
ChannelsFromFmt(enum FmtChannels chans
)
1772 case FmtMono
: return 1;
1773 case FmtStereo
: return 2;
1774 case FmtRear
: return 2;
1775 case FmtQuad
: return 4;
1776 case FmtX51
: return 6;
1777 case FmtX61
: return 7;
1778 case FmtX71
: return 8;
1782 ALboolean
DecomposeFormat(ALenum format
, enum FmtChannels
*chans
, enum FmtType
*type
)
1786 case AL_FORMAT_MONO8
:
1790 case AL_FORMAT_MONO16
:
1794 case AL_FORMAT_MONO_FLOAT32
:
1798 case AL_FORMAT_STEREO8
:
1802 case AL_FORMAT_STEREO16
:
1806 case AL_FORMAT_STEREO_FLOAT32
:
1810 case AL_FORMAT_QUAD8_LOKI
:
1811 case AL_FORMAT_QUAD8
:
1815 case AL_FORMAT_QUAD16_LOKI
:
1816 case AL_FORMAT_QUAD16
:
1820 case AL_FORMAT_QUAD32
:
1824 case AL_FORMAT_REAR8
:
1828 case AL_FORMAT_REAR16
:
1832 case AL_FORMAT_REAR32
:
1836 case AL_FORMAT_51CHN8
:
1840 case AL_FORMAT_51CHN16
:
1844 case AL_FORMAT_51CHN32
:
1848 case AL_FORMAT_61CHN8
:
1852 case AL_FORMAT_61CHN16
:
1856 case AL_FORMAT_61CHN32
:
1860 case AL_FORMAT_71CHN8
:
1864 case AL_FORMAT_71CHN16
:
1868 case AL_FORMAT_71CHN32
:
1878 * ReleaseALBuffers()
1880 * INTERNAL: Called to destroy any buffers that still exist on the device
1882 ALvoid
ReleaseALBuffers(ALCdevice
*device
)
1885 for(i
= 0;i
< device
->BufferMap
.size
;i
++)
1887 ALbuffer
*temp
= device
->BufferMap
.array
[i
].value
;
1888 device
->BufferMap
.array
[i
].value
= NULL
;
1892 ALTHUNK_REMOVEENTRY(temp
->buffer
);
1893 memset(temp
, 0, sizeof(ALbuffer
));