Make better use of the type range when converting from float
[openal-soft.git] / OpenAL32 / alBuffer.c
blob30ca9ea8b72a8910f0f395302b5f3fd2b13630be
1 /**
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
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <assert.h>
26 #include <limits.h>
28 #include "alMain.h"
29 #include "AL/al.h"
30 #include "AL/alc.h"
31 #include "alError.h"
32 #include "alBuffer.h"
33 #include "alDatabuffer.h"
34 #include "alThunk.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)))
45 * Global Variables
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,
58 32767
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
137 * to by buffers
139 AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
141 ALCcontext *Context;
142 ALsizei i=0;
144 Context = GetContextSuspended();
145 if(!Context) return;
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);
150 else
152 ALCdevice *device = Context->Device;
153 ALenum err;
155 // Create all the new Buffers
156 while(i < n)
158 ALbuffer *buffer = calloc(1, sizeof(ALbuffer));
159 if(!buffer)
161 alSetError(Context, AL_OUT_OF_MEMORY);
162 alDeleteBuffers(i, buffers);
163 break;
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));
172 free(buffer);
174 alSetError(Context, err);
175 alDeleteBuffers(i, buffers);
176 break;
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)
192 ALCcontext *Context;
193 ALCdevice *device;
194 ALboolean Failed;
195 ALbuffer *ALBuf;
196 ALsizei i;
198 Context = GetContextSuspended();
199 if(!Context) return;
201 Failed = AL_TRUE;
202 device = Context->Device;
203 /* Check we are actually Deleting some Buffers */
204 if(n < 0)
205 alSetError(Context, AL_INVALID_VALUE);
206 else
208 Failed = AL_FALSE;
210 /* Check that all the buffers are valid and can actually be deleted */
211 for(i = 0;i < n;i++)
213 if(!buffers[i])
214 continue;
216 /* Check for valid Buffer ID */
217 if((ALBuf=LookupBuffer(device->BufferMap, buffers[i])) == NULL)
219 alSetError(Context, AL_INVALID_NAME);
220 Failed = AL_TRUE;
221 break;
223 else if(ALBuf->refcount != 0)
225 /* Buffer still in use, cannot be deleted */
226 alSetError(Context, AL_INVALID_OPERATION);
227 Failed = AL_TRUE;
228 break;
233 /* If all the Buffers were valid (and have Reference Counts of 0), then we
234 * can delete them */
235 if(!Failed)
237 for(i = 0;i < n;i++)
239 if((ALBuf=LookupBuffer(device->BufferMap, buffers[i])) == NULL)
240 continue;
242 /* Release the memory used to store audio data */
243 free(ALBuf->data);
245 /* Release buffer structure */
246 RemoveUIntMapKey(&device->BufferMap, ALBuf->buffer);
247 ALTHUNK_REMOVEENTRY(ALBuf->buffer);
249 memset(ALBuf, 0, sizeof(ALbuffer));
250 free(ALBuf);
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)
264 ALCcontext *Context;
265 ALboolean result;
267 Context = GetContextSuspended();
268 if(!Context) return AL_FALSE;
270 result = ((!buffer || LookupBuffer(Context->Device->BufferMap, buffer)) ?
271 AL_TRUE : AL_FALSE);
273 ProcessContext(Context);
275 return result;
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;
288 ALCcontext *Context;
289 ALCdevice *device;
290 ALbuffer *ALBuf;
291 ALenum err;
293 Context = GetContextSuspended();
294 if(!Context) return;
296 if(Context->SampleSource)
298 ALintptrEXT offset;
300 if(Context->SampleSource->state == MAPPED)
302 alSetError(Context, AL_INVALID_OPERATION);
303 ProcessContext(Context);
304 return;
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);
320 else switch(SrcType)
322 case UserFmtByte:
323 case UserFmtUByte:
324 case UserFmtShort:
325 case UserFmtUShort:
326 case UserFmtInt:
327 case UserFmtUInt:
328 case UserFmtFloat:
329 err = LoadData(ALBuf, freq, format, size, SrcChannels, SrcType, data);
330 if(err != AL_NO_ERROR)
331 alSetError(Context, err);
332 break;
334 case UserFmtDouble: {
335 ALenum NewFormat = AL_FORMAT_MONO_FLOAT32;
336 switch(SrcChannels)
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);
349 } break;
351 case UserFmtMulaw:
352 case UserFmtIMA4: {
353 ALenum NewFormat = AL_FORMAT_MONO16;
354 switch(SrcChannels)
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);
367 } break;
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;
383 ALCcontext *Context;
384 ALCdevice *device;
385 ALbuffer *ALBuf;
387 Context = GetContextSuspended();
388 if(!Context) return;
390 if(Context->SampleSource)
392 ALintptrEXT offset;
394 if(Context->SampleSource->state == MAPPED)
396 alSetError(Context, AL_INVALID_OPERATION);
397 ProcessContext(Context);
398 return;
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);
419 else
421 if(SrcType == UserFmtIMA4)
423 ALuint Channels = ChannelsFromFmt(ALBuf->FmtChannels);
424 ALuint Bytes = BytesFromFmt(ALBuf->FmtType);
426 /* offset -> byte offset, length -> block count */
427 offset /= 36;
428 offset *= 65;
429 offset *= Bytes;
430 length /= ALBuf->OriginalAlign;
432 ConvertDataIMA4(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
433 data, Channels, length);
435 else
437 ALuint OldBytes = BytesFromUserFmt(SrcType);
438 ALuint Bytes = BytesFromFmt(ALBuf->FmtType);
440 offset /= OldBytes;
441 offset *= Bytes;
442 length /= OldBytes;
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;
456 ALCdevice *device;
458 (void)flValue;
460 pContext = GetContextSuspended();
461 if(!pContext) return;
463 device = pContext->Device;
464 if(LookupBuffer(device->BufferMap, buffer) == NULL)
465 alSetError(pContext, AL_INVALID_NAME);
466 else
468 switch(eParam)
470 default:
471 alSetError(pContext, AL_INVALID_ENUM);
472 break;
476 ProcessContext(pContext);
480 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
482 ALCcontext *pContext;
483 ALCdevice *device;
485 (void)flValue1;
486 (void)flValue2;
487 (void)flValue3;
489 pContext = GetContextSuspended();
490 if(!pContext) return;
492 device = pContext->Device;
493 if(LookupBuffer(device->BufferMap, buffer) == NULL)
494 alSetError(pContext, AL_INVALID_NAME);
495 else
497 switch(eParam)
499 default:
500 alSetError(pContext, AL_INVALID_ENUM);
501 break;
505 ProcessContext(pContext);
509 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
511 ALCcontext *pContext;
512 ALCdevice *device;
514 pContext = GetContextSuspended();
515 if(!pContext) return;
517 device = pContext->Device;
518 if(!flValues)
519 alSetError(pContext, AL_INVALID_VALUE);
520 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
521 alSetError(pContext, AL_INVALID_NAME);
522 else
524 switch(eParam)
526 default:
527 alSetError(pContext, AL_INVALID_ENUM);
528 break;
532 ProcessContext(pContext);
536 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
538 ALCcontext *pContext;
539 ALCdevice *device;
541 (void)lValue;
543 pContext = GetContextSuspended();
544 if(!pContext) return;
546 device = pContext->Device;
547 if(LookupBuffer(device->BufferMap, buffer) == NULL)
548 alSetError(pContext, AL_INVALID_NAME);
549 else
551 switch(eParam)
553 default:
554 alSetError(pContext, AL_INVALID_ENUM);
555 break;
559 ProcessContext(pContext);
563 AL_API void AL_APIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
565 ALCcontext *pContext;
566 ALCdevice *device;
568 (void)lValue1;
569 (void)lValue2;
570 (void)lValue3;
572 pContext = GetContextSuspended();
573 if(!pContext) return;
575 device = pContext->Device;
576 if(LookupBuffer(device->BufferMap, buffer) == NULL)
577 alSetError(pContext, AL_INVALID_NAME);
578 else
580 switch(eParam)
582 default:
583 alSetError(pContext, AL_INVALID_ENUM);
584 break;
588 ProcessContext(pContext);
592 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
594 ALCcontext *pContext;
595 ALCdevice *device;
596 ALbuffer *ALBuf;
598 pContext = GetContextSuspended();
599 if(!pContext) return;
601 device = pContext->Device;
602 if(!plValues)
603 alSetError(pContext, AL_INVALID_VALUE);
604 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
605 alSetError(pContext, AL_INVALID_NAME);
606 else
608 switch(eParam)
610 case AL_LOOP_POINTS:
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);
616 else
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);
622 else
624 ALBuf->LoopStart = plValues[0];
625 ALBuf->LoopEnd = plValues[1];
628 break;
630 default:
631 alSetError(pContext, AL_INVALID_ENUM);
632 break;
636 ProcessContext(pContext);
640 AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
642 ALCcontext *pContext;
643 ALCdevice *device;
645 pContext = GetContextSuspended();
646 if(!pContext) return;
648 device = pContext->Device;
649 if(!pflValue)
650 alSetError(pContext, AL_INVALID_VALUE);
651 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
652 alSetError(pContext, AL_INVALID_NAME);
653 else
655 switch(eParam)
657 default:
658 alSetError(pContext, AL_INVALID_ENUM);
659 break;
663 ProcessContext(pContext);
667 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
669 ALCcontext *pContext;
670 ALCdevice *device;
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);
680 else
682 switch(eParam)
684 default:
685 alSetError(pContext, AL_INVALID_ENUM);
686 break;
690 ProcessContext(pContext);
694 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
696 ALCcontext *pContext;
697 ALCdevice *device;
699 pContext = GetContextSuspended();
700 if(!pContext) return;
702 device = pContext->Device;
703 if(!pflValues)
704 alSetError(pContext, AL_INVALID_VALUE);
705 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
706 alSetError(pContext, AL_INVALID_NAME);
707 else
709 switch(eParam)
711 default:
712 alSetError(pContext, AL_INVALID_ENUM);
713 break;
717 ProcessContext(pContext);
721 AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
723 ALCcontext *pContext;
724 ALbuffer *pBuffer;
725 ALCdevice *device;
727 pContext = GetContextSuspended();
728 if(!pContext) return;
730 device = pContext->Device;
731 if(!plValue)
732 alSetError(pContext, AL_INVALID_VALUE);
733 else if((pBuffer=LookupBuffer(device->BufferMap, buffer)) == NULL)
734 alSetError(pContext, AL_INVALID_NAME);
735 else
737 switch(eParam)
739 case AL_FREQUENCY:
740 *plValue = pBuffer->Frequency;
741 break;
743 case AL_BITS:
744 *plValue = BytesFromFmt(pBuffer->FmtType) * 8;
745 break;
747 case AL_CHANNELS:
748 *plValue = ChannelsFromFmt(pBuffer->FmtChannels);
749 break;
751 case AL_SIZE:
752 *plValue = pBuffer->size;
753 break;
755 default:
756 alSetError(pContext, AL_INVALID_ENUM);
757 break;
761 ProcessContext(pContext);
765 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
767 ALCcontext *pContext;
768 ALCdevice *device;
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);
778 else
780 switch(eParam)
782 default:
783 alSetError(pContext, AL_INVALID_ENUM);
784 break;
788 ProcessContext(pContext);
792 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
794 ALCcontext *pContext;
795 ALCdevice *device;
796 ALbuffer *ALBuf;
798 pContext = GetContextSuspended();
799 if(!pContext) return;
801 device = pContext->Device;
802 if(!plValues)
803 alSetError(pContext, AL_INVALID_VALUE);
804 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
805 alSetError(pContext, AL_INVALID_NAME);
806 else
808 switch(eParam)
810 case AL_FREQUENCY:
811 case AL_BITS:
812 case AL_CHANNELS:
813 case AL_SIZE:
814 alGetBufferi(buffer, eParam, plValues);
815 break;
817 case AL_LOOP_POINTS:
818 plValues[0] = ALBuf->LoopStart;
819 plValues[1] = ALBuf->LoopEnd;
820 break;
822 default:
823 alSetError(pContext, AL_INVALID_ENUM);
824 break;
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;
842 if(sign)
844 /* -32768 doesn't properly negate on a short; it results in itself.
845 * So clamp to -32767 */
846 val = max(val, -32767);
847 val = -val;
850 val = min(val, muLawClip);
851 val += muLawBias;
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];
863 ALsizei j,k,c;
865 for(c = 0;c < numchans;c++)
867 sample[c] = *(src++);
868 sample[c] |= *(src++) << 8;
869 sample[c] = (sample[c]^0x8000) - 32768;
870 index[c] = *(src++);
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);
877 dst[c] = sample[c];
880 j = 1;
881 while(j < 65)
883 for(c = 0;c < numchans;c++)
885 code[c] = *(src++);
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;
896 code[c] >>= 4;
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)
914 ALsizei j,k,c;
916 for(c = 0;c < numchans;c++)
918 int diff = src[c] - sample[c];
919 int step = IMAStep_size[index[c]];
920 int nibble;
922 nibble = 0;
923 if(diff < 0)
925 nibble = 0x8;
926 diff = -diff;
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;
946 j = 1;
947 while(j < 65)
949 for(c = 0;c < numchans;c++)
951 for(k = 0;k < 8;k++)
953 int diff = src[(j+k)*numchans + c] - sample[c];
954 int step = IMAStep_size[index[c]];
955 int nibble;
957 nibble = 0;
958 if(diff < 0)
960 nibble = 0x8;
961 diff = -diff;
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;
979 j += 8;
984 static __inline ALbyte Conv_ALbyte_ALbyte(ALbyte val)
985 { return val; }
986 static __inline ALbyte Conv_ALbyte_ALubyte(ALubyte val)
987 { return val^0x80; }
988 static __inline ALbyte Conv_ALbyte_ALshort(ALshort val)
989 { return val>>8; }
990 static __inline ALbyte Conv_ALbyte_ALushort(ALushort val)
991 { return (val>>8)-128; }
992 static __inline ALbyte Conv_ALbyte_ALint(ALint val)
993 { return val>>24; }
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)
1014 { return val; }
1015 static __inline ALubyte Conv_ALubyte_ALshort(ALshort val)
1016 { return (val>>8)+128; }
1017 static __inline ALubyte Conv_ALubyte_ALushort(ALushort val)
1018 { return val>>8; }
1019 static __inline ALubyte Conv_ALubyte_ALint(ALint val)
1020 { return (val>>24)+128; }
1021 static __inline ALubyte Conv_ALubyte_ALuint(ALuint val)
1022 { return val>>24; }
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)
1039 { return val<<8; }
1040 static __inline ALshort Conv_ALshort_ALubyte(ALubyte val)
1041 { return (val-128)<<8; }
1042 static __inline ALshort Conv_ALshort_ALshort(ALshort val)
1043 { return val; }
1044 static __inline ALshort Conv_ALshort_ALushort(ALushort val)
1045 { return val^0x8000; }
1046 static __inline ALshort Conv_ALshort_ALint(ALint val)
1047 { return val>>16; }
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)
1068 { return val<<8; }
1069 static __inline ALushort Conv_ALushort_ALshort(ALshort val)
1070 { return val^0x8000; }
1071 static __inline ALushort Conv_ALushort_ALushort(ALushort val)
1072 { return val; }
1073 static __inline ALushort Conv_ALushort_ALint(ALint val)
1074 { return (val>>16)+32768; }
1075 static __inline ALushort Conv_ALushort_ALuint(ALuint val)
1076 { return val>>16; }
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)
1093 { return val<<24; }
1094 static __inline ALint Conv_ALint_ALubyte(ALubyte val)
1095 { return (val-128)<<24; }
1096 static __inline ALint Conv_ALint_ALshort(ALshort val)
1097 { return val<<16; }
1098 static __inline ALint Conv_ALint_ALushort(ALushort val)
1099 { return (val-32768)<<16; }
1100 static __inline ALint Conv_ALint_ALint(ALint val)
1101 { return 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)
1122 { return val<<24; }
1123 static __inline ALuint Conv_ALuint_ALshort(ALshort val)
1124 { return (val+32768)<<16; }
1125 static __inline ALuint Conv_ALuint_ALushort(ALushort val)
1126 { return val<<16; }
1127 static __inline ALuint Conv_ALuint_ALint(ALint val)
1128 { return val^0x80000000; }
1129 static __inline ALuint Conv_ALuint_ALuint(ALuint val)
1130 { return 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)
1159 { return val; }
1160 static __inline ALfloat Conv_ALfloat_ALdouble(ALdouble val)
1161 { return 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)
1178 { return val; }
1179 static __inline ALdouble Conv_ALdouble_ALdouble(ALdouble val)
1180 { return 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) \
1203 ALuint i; \
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, \
1302 ALuint numblocks) \
1304 ALuint i, j; \
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, \
1329 ALuint numblocks) \
1331 ALuint i, j; \
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, \
1358 ALsizei len) \
1360 switch(srcType) \
1362 case UserFmtByte: \
1363 Convert_##T##_ALbyte(dst, src, len); \
1364 break; \
1365 case UserFmtUByte: \
1366 Convert_##T##_ALubyte(dst, src, len); \
1367 break; \
1368 case UserFmtShort: \
1369 Convert_##T##_ALshort(dst, src, len); \
1370 break; \
1371 case UserFmtUShort: \
1372 Convert_##T##_ALushort(dst, src, len); \
1373 break; \
1374 case UserFmtInt: \
1375 Convert_##T##_ALint(dst, src, len); \
1376 break; \
1377 case UserFmtUInt: \
1378 Convert_##T##_ALuint(dst, src, len); \
1379 break; \
1380 case UserFmtFloat: \
1381 Convert_##T##_ALfloat(dst, src, len); \
1382 break; \
1383 case UserFmtDouble: \
1384 Convert_##T##_ALdouble(dst, src, len); \
1385 break; \
1386 case UserFmtMulaw: \
1387 Convert_##T##_ALmulaw(dst, src, len); \
1388 break; \
1389 case UserFmtIMA4: \
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)
1409 switch(srcType)
1411 case UserFmtByte:
1412 Convert_IMA4_ALbyte(dst, src, chans, len);
1413 break;
1414 case UserFmtUByte:
1415 Convert_IMA4_ALubyte(dst, src, chans, len);
1416 break;
1417 case UserFmtShort:
1418 Convert_IMA4_ALshort(dst, src, chans, len);
1419 break;
1420 case UserFmtUShort:
1421 Convert_IMA4_ALushort(dst, src, chans, len);
1422 break;
1423 case UserFmtInt:
1424 Convert_IMA4_ALint(dst, src, chans, len);
1425 break;
1426 case UserFmtUInt:
1427 Convert_IMA4_ALuint(dst, src, chans, len);
1428 break;
1429 case UserFmtFloat:
1430 Convert_IMA4_ALfloat(dst, src, chans, len);
1431 break;
1432 case UserFmtDouble:
1433 Convert_IMA4_ALdouble(dst, src, chans, len);
1434 break;
1435 case UserFmtMulaw:
1436 Convert_IMA4_ALmulaw(dst, src, chans, len);
1437 break;
1438 case UserFmtIMA4:
1439 memcpy(dst, src, len*36*chans);
1440 break;
1445 static void ConvertData(ALvoid *dst, enum FmtType dstType, const ALvoid *src, enum UserFmtType srcType, ALsizei len)
1447 switch(dstType)
1449 (void)Convert_ALbyte;
1450 case FmtUByte:
1451 Convert_ALubyte(dst, src, srcType, len);
1452 break;
1453 case FmtShort:
1454 Convert_ALshort(dst, src, srcType, len);
1455 break;
1456 (void)Convert_ALushort;
1457 (void)Convert_ALint;
1458 (void)Convert_ALuint;
1459 case FmtFloat:
1460 Convert_ALfloat(dst, src, srcType, len);
1461 break;
1462 (void)Convert_ALdouble;
1463 (void)Convert_ALmulaw;
1464 (void)Convert_IMA4;
1468 static void ConvertDataIMA4(ALvoid *dst, enum FmtType dstType, const ALvoid *src, ALint chans, ALsizei len)
1470 switch(dstType)
1472 (void)Convert_ALbyte_IMA4;
1473 case FmtUByte:
1474 Convert_ALubyte_IMA4(dst, src, chans, len);
1475 break;
1476 case FmtShort:
1477 Convert_ALshort_IMA4(dst, src, chans, len);
1478 break;
1479 (void)Convert_ALushort_IMA4;
1480 (void)Convert_ALint_IMA4;
1481 (void)Convert_ALuint_IMA4;
1482 case FmtFloat:
1483 Convert_ALfloat_IMA4(dst, src, chans, len);
1484 break;
1485 (void)Convert_ALdouble_IMA4;
1486 (void)Convert_ALmulaw_IMA4;
1492 * LoadData
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
1496 * original format.
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;
1503 ALuint64 newsize;
1504 ALvoid *temp;
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;
1524 newsize *= 65;
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;
1531 ALBuf->data = temp;
1532 ALBuf->size = newsize;
1534 if(data != NULL)
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;
1543 else
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;
1558 ALBuf->data = temp;
1559 ALBuf->size = newsize;
1561 if(data != NULL)
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;
1577 return AL_NO_ERROR;
1581 ALuint BytesFromUserFmt(enum UserFmtType type)
1583 switch(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 */
1596 return 0;
1598 ALuint ChannelsFromUserFmt(enum UserFmtChannels chans)
1600 switch(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;
1610 return 0;
1612 ALboolean DecomposeUserFormat(ALenum format, enum UserFmtChannels *chans,
1613 enum UserFmtType *type)
1615 switch(format)
1617 case AL_FORMAT_MONO8:
1618 *chans = UserFmtMono;
1619 *type = UserFmtUByte;
1620 return AL_TRUE;
1621 case AL_FORMAT_MONO16:
1622 *chans = UserFmtMono;
1623 *type = UserFmtShort;
1624 return AL_TRUE;
1625 case AL_FORMAT_MONO_FLOAT32:
1626 *chans = UserFmtMono;
1627 *type = UserFmtFloat;
1628 return AL_TRUE;
1629 case AL_FORMAT_MONO_DOUBLE_EXT:
1630 *chans = UserFmtMono;
1631 *type = UserFmtDouble;
1632 return AL_TRUE;
1633 case AL_FORMAT_MONO_IMA4:
1634 *chans = UserFmtMono;
1635 *type = UserFmtIMA4;
1636 return AL_TRUE;
1637 case AL_FORMAT_STEREO8:
1638 *chans = UserFmtStereo;
1639 *type = UserFmtUByte;
1640 return AL_TRUE;
1641 case AL_FORMAT_STEREO16:
1642 *chans = UserFmtStereo;
1643 *type = UserFmtShort;
1644 return AL_TRUE;
1645 case AL_FORMAT_STEREO_FLOAT32:
1646 *chans = UserFmtStereo;
1647 *type = UserFmtFloat;
1648 return AL_TRUE;
1649 case AL_FORMAT_STEREO_DOUBLE_EXT:
1650 *chans = UserFmtStereo;
1651 *type = UserFmtDouble;
1652 return AL_TRUE;
1653 case AL_FORMAT_STEREO_IMA4:
1654 *chans = UserFmtStereo;
1655 *type = UserFmtIMA4;
1656 return AL_TRUE;
1657 case AL_FORMAT_QUAD8_LOKI:
1658 case AL_FORMAT_QUAD8:
1659 *chans = UserFmtQuad;
1660 *type = UserFmtUByte;
1661 return AL_TRUE;
1662 case AL_FORMAT_QUAD16_LOKI:
1663 case AL_FORMAT_QUAD16:
1664 *chans = UserFmtQuad;
1665 *type = UserFmtShort;
1666 return AL_TRUE;
1667 case AL_FORMAT_QUAD32:
1668 *chans = UserFmtQuad;
1669 *type = UserFmtFloat;
1670 return AL_TRUE;
1671 case AL_FORMAT_REAR8:
1672 *chans = UserFmtRear;
1673 *type = UserFmtUByte;
1674 return AL_TRUE;
1675 case AL_FORMAT_REAR16:
1676 *chans = UserFmtRear;
1677 *type = UserFmtShort;
1678 return AL_TRUE;
1679 case AL_FORMAT_REAR32:
1680 *chans = UserFmtRear;
1681 *type = UserFmtFloat;
1682 return AL_TRUE;
1683 case AL_FORMAT_51CHN8:
1684 *chans = UserFmtX51;
1685 *type = UserFmtUByte;
1686 return AL_TRUE;
1687 case AL_FORMAT_51CHN16:
1688 *chans = UserFmtX51;
1689 *type = UserFmtShort;
1690 return AL_TRUE;
1691 case AL_FORMAT_51CHN32:
1692 *chans = UserFmtX51;
1693 *type = UserFmtFloat;
1694 return AL_TRUE;
1695 case AL_FORMAT_61CHN8:
1696 *chans = UserFmtX61;
1697 *type = UserFmtUByte;
1698 return AL_TRUE;
1699 case AL_FORMAT_61CHN16:
1700 *chans = UserFmtX61;
1701 *type = UserFmtShort;
1702 return AL_TRUE;
1703 case AL_FORMAT_61CHN32:
1704 *chans = UserFmtX61;
1705 *type = UserFmtFloat;
1706 return AL_TRUE;
1707 case AL_FORMAT_71CHN8:
1708 *chans = UserFmtX71;
1709 *type = UserFmtUByte;
1710 return AL_TRUE;
1711 case AL_FORMAT_71CHN16:
1712 *chans = UserFmtX71;
1713 *type = UserFmtShort;
1714 return AL_TRUE;
1715 case AL_FORMAT_71CHN32:
1716 *chans = UserFmtX71;
1717 *type = UserFmtFloat;
1718 return AL_TRUE;
1719 case AL_FORMAT_MONO_MULAW:
1720 *chans = UserFmtMono;
1721 *type = UserFmtMulaw;
1722 return AL_TRUE;
1723 case AL_FORMAT_STEREO_MULAW:
1724 *chans = UserFmtStereo;
1725 *type = UserFmtMulaw;
1726 return AL_TRUE;
1727 case AL_FORMAT_QUAD_MULAW:
1728 *chans = UserFmtQuad;
1729 *type = UserFmtMulaw;
1730 return AL_TRUE;
1731 case AL_FORMAT_REAR_MULAW:
1732 *chans = UserFmtRear;
1733 *type = UserFmtMulaw;
1734 return AL_TRUE;
1735 case AL_FORMAT_51CHN_MULAW:
1736 *chans = UserFmtX51;
1737 *type = UserFmtMulaw;
1738 return AL_TRUE;
1739 case AL_FORMAT_61CHN_MULAW:
1740 *chans = UserFmtX61;
1741 *type = UserFmtMulaw;
1742 return AL_TRUE;
1743 case AL_FORMAT_71CHN_MULAW:
1744 *chans = UserFmtX71;
1745 *type = UserFmtMulaw;
1746 return AL_TRUE;
1748 return AL_FALSE;
1751 ALuint BytesFromFmt(enum FmtType type)
1753 switch(type)
1755 case FmtUByte: return sizeof(ALubyte);
1756 case FmtShort: return sizeof(ALshort);
1757 case FmtFloat: return sizeof(ALfloat);
1759 return 0;
1761 ALuint ChannelsFromFmt(enum FmtChannels chans)
1763 switch(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;
1773 return 0;
1775 ALboolean DecomposeFormat(ALenum format, enum FmtChannels *chans, enum FmtType *type)
1777 switch(format)
1779 case AL_FORMAT_MONO8:
1780 *chans = FmtMono;
1781 *type = FmtUByte;
1782 return AL_TRUE;
1783 case AL_FORMAT_MONO16:
1784 *chans = FmtMono;
1785 *type = FmtShort;
1786 return AL_TRUE;
1787 case AL_FORMAT_MONO_FLOAT32:
1788 *chans = FmtMono;
1789 *type = FmtFloat;
1790 return AL_TRUE;
1791 case AL_FORMAT_STEREO8:
1792 *chans = FmtStereo;
1793 *type = FmtUByte;
1794 return AL_TRUE;
1795 case AL_FORMAT_STEREO16:
1796 *chans = FmtStereo;
1797 *type = FmtShort;
1798 return AL_TRUE;
1799 case AL_FORMAT_STEREO_FLOAT32:
1800 *chans = FmtStereo;
1801 *type = FmtFloat;
1802 return AL_TRUE;
1803 case AL_FORMAT_QUAD8_LOKI:
1804 case AL_FORMAT_QUAD8:
1805 *chans = FmtQuad;
1806 *type = FmtUByte;
1807 return AL_TRUE;
1808 case AL_FORMAT_QUAD16_LOKI:
1809 case AL_FORMAT_QUAD16:
1810 *chans = FmtQuad;
1811 *type = FmtShort;
1812 return AL_TRUE;
1813 case AL_FORMAT_QUAD32:
1814 *chans = FmtQuad;
1815 *type = FmtFloat;
1816 return AL_TRUE;
1817 case AL_FORMAT_REAR8:
1818 *chans = FmtRear;
1819 *type = FmtUByte;
1820 return AL_TRUE;
1821 case AL_FORMAT_REAR16:
1822 *chans = FmtRear;
1823 *type = FmtShort;
1824 return AL_TRUE;
1825 case AL_FORMAT_REAR32:
1826 *chans = FmtRear;
1827 *type = FmtFloat;
1828 return AL_TRUE;
1829 case AL_FORMAT_51CHN8:
1830 *chans = FmtX51;
1831 *type = FmtUByte;
1832 return AL_TRUE;
1833 case AL_FORMAT_51CHN16:
1834 *chans = FmtX51;
1835 *type = FmtShort;
1836 return AL_TRUE;
1837 case AL_FORMAT_51CHN32:
1838 *chans = FmtX51;
1839 *type = FmtFloat;
1840 return AL_TRUE;
1841 case AL_FORMAT_61CHN8:
1842 *chans = FmtX61;
1843 *type = FmtUByte;
1844 return AL_TRUE;
1845 case AL_FORMAT_61CHN16:
1846 *chans = FmtX61;
1847 *type = FmtShort;
1848 return AL_TRUE;
1849 case AL_FORMAT_61CHN32:
1850 *chans = FmtX61;
1851 *type = FmtFloat;
1852 return AL_TRUE;
1853 case AL_FORMAT_71CHN8:
1854 *chans = FmtX71;
1855 *type = FmtUByte;
1856 return AL_TRUE;
1857 case AL_FORMAT_71CHN16:
1858 *chans = FmtX71;
1859 *type = FmtShort;
1860 return AL_TRUE;
1861 case AL_FORMAT_71CHN32:
1862 *chans = FmtX71;
1863 *type = FmtFloat;
1864 return AL_TRUE;
1866 return AL_FALSE;
1871 * ReleaseALBuffers()
1873 * INTERNAL: Called to destroy any buffers that still exist on the device
1875 ALvoid ReleaseALBuffers(ALCdevice *device)
1877 ALsizei i;
1878 for(i = 0;i < device->BufferMap.size;i++)
1880 ALbuffer *temp = device->BufferMap.array[i].value;
1881 device->BufferMap.array[i].value = NULL;
1883 free(temp->data);
1885 ALTHUNK_REMOVEENTRY(temp->buffer);
1886 memset(temp, 0, sizeof(ALbuffer));
1887 free(temp);