Check for a couple missing input formats and remove an unneeded default case
[openal-soft.git] / OpenAL32 / alBuffer.c
blob0c97845edb120b7a9807c5d720ac03be4d623541
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 SrcFmtChannels chans, enum SrcFmtType type, const ALvoid *data);
38 static void ConvertData(ALvoid *dst, enum FmtType dstType, const ALvoid *src, enum SrcFmtType srcType, ALsizei len);
39 static void ConvertDataIMA4(ALvoid *dst, const ALvoid *src, ALint origChans, ALsizei len);
41 #define LookupBuffer(m, k) ((ALbuffer*)LookupUIntMapKey(&(m), (k)))
45 * Global Variables
48 static const long g_IMAStep_size[89]={ // IMA ADPCM Stepsize table
49 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
50 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143,
51 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
52 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
53 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,11487,12635,13899,
54 15289,16818,18500,20350,22358,24633,27086,29794,32767
57 static const long g_IMACodeword_4[16]={ // IMA4 ADPCM Codeword decode table
58 1, 3, 5, 7, 9, 11, 13, 15,
59 -1,-3,-5,-7,-9,-11,-13,-15,
62 static const long g_IMAIndex_adjust_4[16]={ // IMA4 ADPCM Step index adjust decode table
63 -1,-1,-1,-1, 2, 4, 6, 8,
64 -1,-1,-1,-1, 2, 4, 6, 8
67 static const ALshort muLawDecompressionTable[256] = {
68 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
69 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
70 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
71 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
72 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
73 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
74 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
75 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
76 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
77 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
78 -876, -844, -812, -780, -748, -716, -684, -652,
79 -620, -588, -556, -524, -492, -460, -428, -396,
80 -372, -356, -340, -324, -308, -292, -276, -260,
81 -244, -228, -212, -196, -180, -164, -148, -132,
82 -120, -112, -104, -96, -88, -80, -72, -64,
83 -56, -48, -40, -32, -24, -16, -8, 0,
84 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
85 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
86 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
87 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
88 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
89 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
90 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
91 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
92 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
93 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
94 876, 844, 812, 780, 748, 716, 684, 652,
95 620, 588, 556, 524, 492, 460, 428, 396,
96 372, 356, 340, 324, 308, 292, 276, 260,
97 244, 228, 212, 196, 180, 164, 148, 132,
98 120, 112, 104, 96, 88, 80, 72, 64,
99 56, 48, 40, 32, 24, 16, 8, 0
103 * alGenBuffers(ALsizei n, ALuint *buffers)
105 * Generates n AL Buffers, and stores the Buffers Names in the array pointed to by buffers
107 AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
109 ALCcontext *Context;
110 ALsizei i=0;
112 Context = GetContextSuspended();
113 if(!Context) return;
115 // Check that we are actually generating some Buffers
116 if(n < 0 || IsBadWritePtr((void*)buffers, n * sizeof(ALuint)))
117 alSetError(Context, AL_INVALID_VALUE);
118 else
120 ALCdevice *device = Context->Device;
121 ALenum err;
123 // Create all the new Buffers
124 while(i < n)
126 ALbuffer *buffer = calloc(1, sizeof(ALbuffer));
127 if(!buffer)
129 alSetError(Context, AL_OUT_OF_MEMORY);
130 alDeleteBuffers(i, buffers);
131 break;
134 buffer->buffer = (ALuint)ALTHUNK_ADDENTRY(buffer);
135 err = InsertUIntMapEntry(&device->BufferMap, buffer->buffer, buffer);
136 if(err != AL_NO_ERROR)
138 ALTHUNK_REMOVEENTRY(buffer->buffer);
139 memset(buffer, 0, sizeof(ALbuffer));
140 free(buffer);
142 alSetError(Context, err);
143 alDeleteBuffers(i, buffers);
144 break;
146 buffers[i++] = buffer->buffer;
150 ProcessContext(Context);
154 * alDeleteBuffers(ALsizei n, ALuint *buffers)
156 * Deletes the n AL Buffers pointed to by buffers
158 AL_API ALvoid AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
160 ALCcontext *Context;
161 ALCdevice *device;
162 ALboolean Failed;
163 ALbuffer *ALBuf;
164 ALsizei i;
166 Context = GetContextSuspended();
167 if(!Context) return;
169 Failed = AL_TRUE;
170 device = Context->Device;
171 /* Check we are actually Deleting some Buffers */
172 if(n < 0)
173 alSetError(Context, AL_INVALID_VALUE);
174 else
176 Failed = AL_FALSE;
178 /* Check that all the buffers are valid and can actually be deleted */
179 for(i = 0;i < n;i++)
181 if(!buffers[i])
182 continue;
184 /* Check for valid Buffer ID */
185 if((ALBuf=LookupBuffer(device->BufferMap, buffers[i])) == NULL)
187 alSetError(Context, AL_INVALID_NAME);
188 Failed = AL_TRUE;
189 break;
191 else if(ALBuf->refcount != 0)
193 /* Buffer still in use, cannot be deleted */
194 alSetError(Context, AL_INVALID_OPERATION);
195 Failed = AL_TRUE;
196 break;
201 /* If all the Buffers were valid (and have Reference Counts of 0), then we can delete them */
202 if(!Failed)
204 for(i = 0;i < n;i++)
206 if((ALBuf=LookupBuffer(device->BufferMap, buffers[i])) == NULL)
207 continue;
209 /* Release the memory used to store audio data */
210 free(ALBuf->data);
212 /* Release buffer structure */
213 RemoveUIntMapKey(&device->BufferMap, ALBuf->buffer);
214 ALTHUNK_REMOVEENTRY(ALBuf->buffer);
216 memset(ALBuf, 0, sizeof(ALbuffer));
217 free(ALBuf);
221 ProcessContext(Context);
225 * alIsBuffer(ALuint buffer)
227 * Checks if buffer is a valid Buffer Name
229 AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
231 ALCcontext *Context;
232 ALboolean result;
234 Context = GetContextSuspended();
235 if(!Context) return AL_FALSE;
237 result = ((!buffer || LookupBuffer(Context->Device->BufferMap, buffer)) ?
238 AL_TRUE : AL_FALSE);
240 ProcessContext(Context);
242 return result;
246 * alBufferData(ALuint buffer,ALenum format,const ALvoid *data,ALsizei size,ALsizei freq)
248 * Fill buffer with audio data
250 AL_API ALvoid AL_APIENTRY alBufferData(ALuint buffer,ALenum format,const ALvoid *data,ALsizei size,ALsizei freq)
252 enum SrcFmtChannels SrcChannels;
253 enum SrcFmtType SrcType;
254 ALCcontext *Context;
255 ALCdevice *device;
256 ALbuffer *ALBuf;
257 ALvoid *temp;
258 ALenum err;
260 Context = GetContextSuspended();
261 if(!Context) return;
263 if(Context->SampleSource)
265 ALintptrEXT offset;
267 if(Context->SampleSource->state == MAPPED)
269 alSetError(Context, AL_INVALID_OPERATION);
270 ProcessContext(Context);
271 return;
274 offset = (const ALubyte*)data - (ALubyte*)NULL;
275 data = Context->SampleSource->data + offset;
278 device = Context->Device;
279 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
280 alSetError(Context, AL_INVALID_NAME);
281 else if(ALBuf->refcount != 0)
282 alSetError(Context, AL_INVALID_VALUE);
283 else if(size < 0 || freq < 0)
284 alSetError(Context, AL_INVALID_VALUE);
285 else if(DecomposeInputFormat(format, &SrcChannels, &SrcType) == AL_FALSE)
286 alSetError(Context, AL_INVALID_ENUM);
287 else switch(SrcType)
289 case SrcFmtByte:
290 case SrcFmtUByte:
291 case SrcFmtShort:
292 case SrcFmtUShort:
293 case SrcFmtInt:
294 case SrcFmtUInt:
295 case SrcFmtFloat:
296 err = LoadData(ALBuf, freq, format, size, SrcChannels, SrcType, data);
297 if(err != AL_NO_ERROR)
298 alSetError(Context, err);
299 break;
301 case SrcFmtDouble:
302 if(SrcChannels == SrcFmtMono)
303 err = LoadData(ALBuf, freq, AL_FORMAT_MONO_FLOAT32, size, SrcChannels, SrcType, data);
304 else
305 err = LoadData(ALBuf, freq, AL_FORMAT_STEREO_FLOAT32, size, SrcChannels, SrcType, data);
306 if(err != AL_NO_ERROR)
307 alSetError(Context, err);
308 break;
310 case SrcFmtMulaw:
311 if(SrcChannels == SrcFmtRear)
312 err = LoadData(ALBuf, freq, AL_FORMAT_REAR16, size, SrcChannels, SrcType, data);
313 else
315 ALuint Channels = ChannelsFromSrcFmt(SrcChannels);
316 ALenum NewFormat = ((Channels==1) ? AL_FORMAT_MONO16 :
317 ((Channels==2) ? AL_FORMAT_STEREO16 :
318 ((Channels==4) ? AL_FORMAT_QUAD16 :
319 ((Channels==6) ? AL_FORMAT_51CHN16 :
320 ((Channels==7) ? AL_FORMAT_61CHN16 :
321 AL_FORMAT_71CHN16)))));
322 err = LoadData(ALBuf, freq, NewFormat, size, SrcChannels, SrcType, data);
324 if(err != AL_NO_ERROR)
325 alSetError(Context, err);
326 break;
328 case SrcFmtIMA4: {
329 ALuint Channels = ((SrcChannels==SrcFmtMono) ? 1 : 2);
330 ALenum NewFormat = ((Channels==1) ? AL_FORMAT_MONO16 :
331 AL_FORMAT_STEREO16);
332 ALuint NewBytes = aluBytesFromFormat(NewFormat);
333 ALuint64 newsize;
335 /* Here is where things vary:
336 * nVidia and Apple use 64+1 sample frames per block => block_size=36*chans bytes
337 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024*chans bytes
339 if((size%(36*Channels)) != 0)
341 alSetError(Context, AL_INVALID_VALUE);
342 break;
345 newsize = size / 36;
346 newsize *= 65;
347 newsize *= NewBytes;
349 if(newsize > INT_MAX)
351 alSetError(Context, AL_OUT_OF_MEMORY);
352 break;
354 temp = realloc(ALBuf->data, newsize);
355 if(temp)
357 ALBuf->data = temp;
358 ALBuf->size = newsize;
360 ConvertDataIMA4(ALBuf->data, data, Channels, newsize/(65*Channels*NewBytes));
362 ALBuf->Frequency = freq;
363 DecomposeFormat(NewFormat, &ALBuf->FmtChannels, &ALBuf->FmtType);
365 ALBuf->LoopStart = 0;
366 ALBuf->LoopEnd = newsize / Channels / NewBytes;
368 ALBuf->OriginalChannels = SrcChannels;
369 ALBuf->OriginalType = SrcType;
370 ALBuf->OriginalSize = size;
371 ALBuf->OriginalAlign = 36 * Channels;
373 else
374 alSetError(Context, AL_OUT_OF_MEMORY);
375 } break;
378 ProcessContext(Context);
382 * alBufferSubDataSOFT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
384 * Update buffer's audio data
386 AL_API ALvoid AL_APIENTRY alBufferSubDataSOFT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
388 enum SrcFmtChannels SrcChannels;
389 enum SrcFmtType SrcType;
390 ALCcontext *Context;
391 ALCdevice *device;
392 ALbuffer *ALBuf;
394 Context = GetContextSuspended();
395 if(!Context) return;
397 if(Context->SampleSource)
399 ALintptrEXT offset;
401 if(Context->SampleSource->state == MAPPED)
403 alSetError(Context, AL_INVALID_OPERATION);
404 ProcessContext(Context);
405 return;
408 offset = (const ALubyte*)data - (ALubyte*)NULL;
409 data = Context->SampleSource->data + offset;
412 device = Context->Device;
413 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
414 alSetError(Context, AL_INVALID_NAME);
415 else if(length < 0 || offset < 0 || (length > 0 && data == NULL))
416 alSetError(Context, AL_INVALID_VALUE);
417 else if(DecomposeInputFormat(format, &SrcChannels, &SrcType) == AL_FALSE ||
418 SrcChannels != ALBuf->OriginalChannels ||
419 SrcType != ALBuf->OriginalType)
420 alSetError(Context, AL_INVALID_ENUM);
421 else if(offset > ALBuf->OriginalSize ||
422 length > ALBuf->OriginalSize-offset ||
423 (offset%ALBuf->OriginalAlign) != 0 ||
424 (length%ALBuf->OriginalAlign) != 0)
425 alSetError(Context, AL_INVALID_VALUE);
426 else
428 if(SrcType == SrcFmtIMA4)
430 ALuint Channels = ChannelsFromFmt(ALBuf->FmtChannels);
431 ALuint Bytes = BytesFromFmt(ALBuf->FmtType);
433 /* offset -> byte offset, length -> block count */
434 offset /= 36;
435 offset *= 65;
436 offset *= Bytes;
437 length /= ALBuf->OriginalAlign;
439 ConvertDataIMA4(&((ALubyte*)ALBuf->data)[offset], data, Channels, length);
441 else
443 ALuint OldBytes = BytesFromFmt(SrcType);
444 ALuint Bytes = BytesFromFmt(ALBuf->FmtType);
446 offset /= OldBytes;
447 offset *= Bytes;
448 length /= OldBytes;
450 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
451 data, SrcType, length);
455 ProcessContext(Context);
458 AL_API ALvoid AL_APIENTRY alBufferSubDataEXT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
460 alBufferSubDataSOFT(buffer, format, data, offset, length);
464 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
466 ALCcontext *pContext;
467 ALCdevice *device;
469 (void)flValue;
471 pContext = GetContextSuspended();
472 if(!pContext) return;
474 device = pContext->Device;
475 if(LookupBuffer(device->BufferMap, buffer) == NULL)
476 alSetError(pContext, AL_INVALID_NAME);
477 else
479 switch(eParam)
481 default:
482 alSetError(pContext, AL_INVALID_ENUM);
483 break;
487 ProcessContext(pContext);
491 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
493 ALCcontext *pContext;
494 ALCdevice *device;
496 (void)flValue1;
497 (void)flValue2;
498 (void)flValue3;
500 pContext = GetContextSuspended();
501 if(!pContext) return;
503 device = pContext->Device;
504 if(LookupBuffer(device->BufferMap, buffer) == NULL)
505 alSetError(pContext, AL_INVALID_NAME);
506 else
508 switch(eParam)
510 default:
511 alSetError(pContext, AL_INVALID_ENUM);
512 break;
516 ProcessContext(pContext);
520 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
522 ALCcontext *pContext;
523 ALCdevice *device;
525 pContext = GetContextSuspended();
526 if(!pContext) return;
528 device = pContext->Device;
529 if(!flValues)
530 alSetError(pContext, AL_INVALID_VALUE);
531 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
532 alSetError(pContext, AL_INVALID_NAME);
533 else
535 switch(eParam)
537 default:
538 alSetError(pContext, AL_INVALID_ENUM);
539 break;
543 ProcessContext(pContext);
547 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
549 ALCcontext *pContext;
550 ALCdevice *device;
552 (void)lValue;
554 pContext = GetContextSuspended();
555 if(!pContext) return;
557 device = pContext->Device;
558 if(LookupBuffer(device->BufferMap, buffer) == NULL)
559 alSetError(pContext, AL_INVALID_NAME);
560 else
562 switch(eParam)
564 default:
565 alSetError(pContext, AL_INVALID_ENUM);
566 break;
570 ProcessContext(pContext);
574 AL_API void AL_APIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
576 ALCcontext *pContext;
577 ALCdevice *device;
579 (void)lValue1;
580 (void)lValue2;
581 (void)lValue3;
583 pContext = GetContextSuspended();
584 if(!pContext) return;
586 device = pContext->Device;
587 if(LookupBuffer(device->BufferMap, buffer) == NULL)
588 alSetError(pContext, AL_INVALID_NAME);
589 else
591 switch(eParam)
593 default:
594 alSetError(pContext, AL_INVALID_ENUM);
595 break;
599 ProcessContext(pContext);
603 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
605 ALCcontext *pContext;
606 ALCdevice *device;
607 ALbuffer *ALBuf;
609 pContext = GetContextSuspended();
610 if(!pContext) return;
612 device = pContext->Device;
613 if(!plValues)
614 alSetError(pContext, AL_INVALID_VALUE);
615 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
616 alSetError(pContext, AL_INVALID_NAME);
617 else
619 switch(eParam)
621 case AL_LOOP_POINTS:
622 if(ALBuf->refcount > 0)
623 alSetError(pContext, AL_INVALID_OPERATION);
624 else if(plValues[0] < 0 || plValues[1] < 0 ||
625 plValues[0] >= plValues[1] || ALBuf->size == 0)
626 alSetError(pContext, AL_INVALID_VALUE);
627 else
629 ALint maxlen = ALBuf->size /
630 FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
631 if(plValues[0] > maxlen || plValues[1] > maxlen)
632 alSetError(pContext, AL_INVALID_VALUE);
633 else
635 ALBuf->LoopStart = plValues[0];
636 ALBuf->LoopEnd = plValues[1];
639 break;
641 default:
642 alSetError(pContext, AL_INVALID_ENUM);
643 break;
647 ProcessContext(pContext);
651 AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
653 ALCcontext *pContext;
654 ALCdevice *device;
656 pContext = GetContextSuspended();
657 if(!pContext) return;
659 device = pContext->Device;
660 if(!pflValue)
661 alSetError(pContext, AL_INVALID_VALUE);
662 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
663 alSetError(pContext, AL_INVALID_NAME);
664 else
666 switch(eParam)
668 default:
669 alSetError(pContext, AL_INVALID_ENUM);
670 break;
674 ProcessContext(pContext);
678 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
680 ALCcontext *pContext;
681 ALCdevice *device;
683 pContext = GetContextSuspended();
684 if(!pContext) return;
686 device = pContext->Device;
687 if(!pflValue1 || !pflValue2 || !pflValue3)
688 alSetError(pContext, AL_INVALID_VALUE);
689 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
690 alSetError(pContext, AL_INVALID_NAME);
691 else
693 switch(eParam)
695 default:
696 alSetError(pContext, AL_INVALID_ENUM);
697 break;
701 ProcessContext(pContext);
705 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
707 ALCcontext *pContext;
708 ALCdevice *device;
710 pContext = GetContextSuspended();
711 if(!pContext) return;
713 device = pContext->Device;
714 if(!pflValues)
715 alSetError(pContext, AL_INVALID_VALUE);
716 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
717 alSetError(pContext, AL_INVALID_NAME);
718 else
720 switch(eParam)
722 default:
723 alSetError(pContext, AL_INVALID_ENUM);
724 break;
728 ProcessContext(pContext);
732 AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
734 ALCcontext *pContext;
735 ALbuffer *pBuffer;
736 ALCdevice *device;
738 pContext = GetContextSuspended();
739 if(!pContext) return;
741 device = pContext->Device;
742 if(!plValue)
743 alSetError(pContext, AL_INVALID_VALUE);
744 else if((pBuffer=LookupBuffer(device->BufferMap, buffer)) == NULL)
745 alSetError(pContext, AL_INVALID_NAME);
746 else
748 switch(eParam)
750 case AL_FREQUENCY:
751 *plValue = pBuffer->Frequency;
752 break;
754 case AL_BITS:
755 *plValue = BytesFromFmt(pBuffer->FmtType) * 8;
756 break;
758 case AL_CHANNELS:
759 *plValue = ChannelsFromFmt(pBuffer->FmtChannels);
760 break;
762 case AL_SIZE:
763 *plValue = pBuffer->size;
764 break;
766 default:
767 alSetError(pContext, AL_INVALID_ENUM);
768 break;
772 ProcessContext(pContext);
776 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
778 ALCcontext *pContext;
779 ALCdevice *device;
781 pContext = GetContextSuspended();
782 if(!pContext) return;
784 device = pContext->Device;
785 if(!plValue1 || !plValue2 || !plValue3)
786 alSetError(pContext, AL_INVALID_VALUE);
787 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
788 alSetError(pContext, AL_INVALID_NAME);
789 else
791 switch(eParam)
793 default:
794 alSetError(pContext, AL_INVALID_ENUM);
795 break;
799 ProcessContext(pContext);
803 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
805 ALCcontext *pContext;
806 ALCdevice *device;
807 ALbuffer *ALBuf;
809 pContext = GetContextSuspended();
810 if(!pContext) return;
812 device = pContext->Device;
813 if(!plValues)
814 alSetError(pContext, AL_INVALID_VALUE);
815 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
816 alSetError(pContext, AL_INVALID_NAME);
817 else
819 switch(eParam)
821 case AL_FREQUENCY:
822 case AL_BITS:
823 case AL_CHANNELS:
824 case AL_SIZE:
825 alGetBufferi(buffer, eParam, plValues);
826 break;
828 case AL_LOOP_POINTS:
829 plValues[0] = ALBuf->LoopStart;
830 plValues[1] = ALBuf->LoopEnd;
831 break;
833 default:
834 alSetError(pContext, AL_INVALID_ENUM);
835 break;
839 ProcessContext(pContext);
843 static void ConvertDataIMA4(ALvoid *dst, const ALvoid *src, ALint chans, ALsizei len)
845 const ALubyte *IMAData;
846 ALint Sample[2],Index[2];
847 ALuint IMACode[2];
848 ALsizei i,j,k,c;
850 if(src == NULL)
851 return;
853 IMAData = src;
854 for(i = 0;i < len;i++)
856 for(c = 0;c < chans;c++)
858 Sample[c] = *(IMAData++);
859 Sample[c] |= *(IMAData++) << 8;
860 Sample[c] = (Sample[c]^0x8000) - 32768;
861 Index[c] = *(IMAData++);
862 Index[c] |= *(IMAData++) << 8;
863 Index[c] = (Index[c]^0x8000) - 32768;
865 Index[c] = ((Index[c]<0) ? 0 : Index[c]);
866 Index[c] = ((Index[c]>88) ? 88 : Index[c]);
868 ((ALshort*)dst)[i*65*chans + c] = Sample[c];
871 for(j = 1;j < 65;j += 8)
873 for(c = 0;c < chans;c++)
875 IMACode[c] = *(IMAData++);
876 IMACode[c] |= *(IMAData++) << 8;
877 IMACode[c] |= *(IMAData++) << 16;
878 IMACode[c] |= *(IMAData++) << 24;
881 for(k = 0;k < 8;k++)
883 for(c = 0;c < chans;c++)
885 Sample[c] += ((g_IMAStep_size[Index[c]]*g_IMACodeword_4[IMACode[c]&15])/8);
886 Index[c] += g_IMAIndex_adjust_4[IMACode[c]&15];
888 if(Sample[c] < -32768) Sample[c] = -32768;
889 else if(Sample[c] > 32767) Sample[c] = 32767;
891 if(Index[c]<0) Index[c] = 0;
892 else if(Index[c]>88) Index[c] = 88;
894 ((ALshort*)dst)[(i*65+j+k)*chans + c] = Sample[c];
895 IMACode[c] >>= 4;
903 typedef ALubyte ALmulaw;
905 static __inline ALbyte Conv_ALbyte_ALbyte(ALbyte val)
906 { return val; }
907 static __inline ALbyte Conv_ALbyte_ALubyte(ALubyte val)
908 { return val^0x80; }
909 static __inline ALbyte Conv_ALbyte_ALshort(ALshort val)
910 { return val>>8; }
911 static __inline ALbyte Conv_ALbyte_ALushort(ALushort val)
912 { return (val>>8)-128; }
913 static __inline ALbyte Conv_ALbyte_ALint(ALint val)
914 { return val>>24; }
915 static __inline ALbyte Conv_ALbyte_ALuint(ALuint val)
916 { return (val>>24)-128; }
917 static __inline ALbyte Conv_ALbyte_ALfloat(ALfloat val)
919 if(val >= 1.0f) return 127;
920 if(val <= -1.0f) return -128;
921 return (ALint)(val * 127.0f);
923 static __inline ALbyte Conv_ALbyte_ALdouble(ALdouble val)
925 if(val >= 1.0) return 127;
926 if(val <= -1.0) return -128;
927 return (ALint)(val * 127.0);
929 static __inline ALbyte Conv_ALbyte_ALmulaw(ALmulaw val)
930 { return muLawDecompressionTable[val]>>8; }
932 static __inline ALubyte Conv_ALubyte_ALbyte(ALbyte val)
933 { return val^0x80; }
934 static __inline ALubyte Conv_ALubyte_ALubyte(ALubyte val)
935 { return val; }
936 static __inline ALubyte Conv_ALubyte_ALshort(ALshort val)
937 { return (val>>8)+128; }
938 static __inline ALubyte Conv_ALubyte_ALushort(ALushort val)
939 { return val>>8; }
940 static __inline ALubyte Conv_ALubyte_ALint(ALint val)
941 { return (val>>24)+128; }
942 static __inline ALubyte Conv_ALubyte_ALuint(ALuint val)
943 { return val>>24; }
944 static __inline ALubyte Conv_ALubyte_ALfloat(ALfloat val)
946 if(val >= 1.0f) return 255;
947 if(val <= -1.0f) return 0;
948 return (ALint)(val * 127.0f) + 128;
950 static __inline ALubyte Conv_ALubyte_ALdouble(ALdouble val)
952 if(val >= 1.0) return 255;
953 if(val <= -1.0) return 0;
954 return (ALint)(val * 127.0) + 128;
956 static __inline ALubyte Conv_ALubyte_ALmulaw(ALmulaw val)
957 { return (muLawDecompressionTable[val]>>8)+128; }
959 static __inline ALshort Conv_ALshort_ALbyte(ALbyte val)
960 { return val<<8; }
961 static __inline ALshort Conv_ALshort_ALubyte(ALubyte val)
962 { return (val-128)<<8; }
963 static __inline ALshort Conv_ALshort_ALshort(ALshort val)
964 { return val; }
965 static __inline ALshort Conv_ALshort_ALushort(ALushort val)
966 { return val^0x8000; }
967 static __inline ALshort Conv_ALshort_ALint(ALint val)
968 { return val>>16; }
969 static __inline ALshort Conv_ALshort_ALuint(ALuint val)
970 { return (val>>16)-32768; }
971 static __inline ALshort Conv_ALshort_ALfloat(ALfloat val)
973 if(val >= 1.0f) return 32767;
974 if(val <= -1.0f) return -32768;
975 return (ALint)(val * 32767.0f);
977 static __inline ALshort Conv_ALshort_ALdouble(ALdouble val)
979 if(val >= 1.0) return 32767;
980 if(val <= -1.0) return -32768;
981 return (ALint)(val * 32767.0);
983 static __inline ALshort Conv_ALshort_ALmulaw(ALmulaw val)
984 { return muLawDecompressionTable[val]; }
986 static __inline ALushort Conv_ALushort_ALbyte(ALbyte val)
987 { return (val+128)<<8; }
988 static __inline ALushort Conv_ALushort_ALubyte(ALubyte val)
989 { return val<<8; }
990 static __inline ALushort Conv_ALushort_ALshort(ALshort val)
991 { return val^0x8000; }
992 static __inline ALushort Conv_ALushort_ALushort(ALushort val)
993 { return val; }
994 static __inline ALushort Conv_ALushort_ALint(ALint val)
995 { return (val>>16)+32768; }
996 static __inline ALushort Conv_ALushort_ALuint(ALuint val)
997 { return val>>16; }
998 static __inline ALushort Conv_ALushort_ALfloat(ALfloat val)
1000 if(val >= 1.0f) return 65535;
1001 if(val <= -1.0f) return 0;
1002 return (ALint)(val * 32767.0f) + 32768;
1004 static __inline ALushort Conv_ALushort_ALdouble(ALdouble val)
1006 if(val >= 1.0) return 65535;
1007 if(val <= -1.0) return 0;
1008 return (ALint)(val * 32767.0) + 32768;
1010 static __inline ALushort Conv_ALushort_ALmulaw(ALmulaw val)
1011 { return muLawDecompressionTable[val]^0x8000; }
1013 static __inline ALint Conv_ALint_ALbyte(ALbyte val)
1014 { return val<<24; }
1015 static __inline ALint Conv_ALint_ALubyte(ALubyte val)
1016 { return (val-128)<<24; }
1017 static __inline ALint Conv_ALint_ALshort(ALshort val)
1018 { return val<<16; }
1019 static __inline ALint Conv_ALint_ALushort(ALushort val)
1020 { return (val-32768)<<16; }
1021 static __inline ALint Conv_ALint_ALint(ALint val)
1022 { return val; }
1023 static __inline ALint Conv_ALint_ALuint(ALuint val)
1024 { return val-2147483648u; }
1025 static __inline ALint Conv_ALint_ALfloat(ALfloat val)
1027 if(val >= 1.0f) return 2147483647;
1028 if(val <= -1.0f) return -2147483648u;
1029 return (ALint)(val * 2147483647.0);
1031 static __inline ALint Conv_ALint_ALdouble(ALdouble val)
1033 if(val >= 1.0) return 2147483647;
1034 if(val <= -1.0) return -2147483648u;
1035 return (ALint)(val * 2147483647.0);
1037 static __inline ALint Conv_ALint_ALmulaw(ALmulaw val)
1038 { return muLawDecompressionTable[val]<<16; }
1040 static __inline ALuint Conv_ALuint_ALbyte(ALbyte val)
1041 { return (val+128)<<24; }
1042 static __inline ALuint Conv_ALuint_ALubyte(ALubyte val)
1043 { return val<<24; }
1044 static __inline ALuint Conv_ALuint_ALshort(ALshort val)
1045 { return (val+32768)<<16; }
1046 static __inline ALuint Conv_ALuint_ALushort(ALushort val)
1047 { return val<<16; }
1048 static __inline ALuint Conv_ALuint_ALint(ALint val)
1049 { return val+2147483648u; }
1050 static __inline ALuint Conv_ALuint_ALuint(ALuint val)
1051 { return val; }
1052 static __inline ALuint Conv_ALuint_ALfloat(ALfloat val)
1054 if(val >= 1.0f) return 4294967295u;
1055 if(val <= -1.0f) return 0;
1056 return (ALint)(val * 2147483647.0) + 2147483648u;
1058 static __inline ALuint Conv_ALuint_ALdouble(ALdouble val)
1060 if(val >= 1.0) return 4294967295u;
1061 if(val <= -1.0) return 0;
1062 return (ALint)(val * 2147483647.0) + 2147483648u;
1064 static __inline ALuint Conv_ALuint_ALmulaw(ALmulaw val)
1065 { return (muLawDecompressionTable[val]+32768)<<16; }
1067 static __inline ALfloat Conv_ALfloat_ALbyte(ALbyte val)
1068 { return val * (1.0f/127.0f); }
1069 static __inline ALfloat Conv_ALfloat_ALubyte(ALubyte val)
1070 { return (val-128) * (1.0f/127.0f); }
1071 static __inline ALfloat Conv_ALfloat_ALshort(ALshort val)
1072 { return val * (1.0f/32767.0f); }
1073 static __inline ALfloat Conv_ALfloat_ALushort(ALushort val)
1074 { return (val-32768) * (1.0f/32767.0f); }
1075 static __inline ALfloat Conv_ALfloat_ALint(ALint val)
1076 { return val * (1.0/2147483647.0); }
1077 static __inline ALfloat Conv_ALfloat_ALuint(ALuint val)
1078 { return (ALint)(val-2147483648u) * (1.0/2147483647.0); }
1079 static __inline ALfloat Conv_ALfloat_ALfloat(ALfloat val)
1080 { return val; }
1081 static __inline ALfloat Conv_ALfloat_ALdouble(ALdouble val)
1082 { return val; }
1083 static __inline ALfloat Conv_ALfloat_ALmulaw(ALmulaw val)
1084 { return muLawDecompressionTable[val] * (1.0f/32767.0f); }
1086 static __inline ALdouble Conv_ALdouble_ALbyte(ALbyte val)
1087 { return val * (1.0/127.0); }
1088 static __inline ALdouble Conv_ALdouble_ALubyte(ALubyte val)
1089 { return (val-128) * (1.0/127.0); }
1090 static __inline ALdouble Conv_ALdouble_ALshort(ALshort val)
1091 { return val * (1.0/32767.0); }
1092 static __inline ALdouble Conv_ALdouble_ALushort(ALushort val)
1093 { return (val-32768) * (1.0/32767.0); }
1094 static __inline ALdouble Conv_ALdouble_ALint(ALint val)
1095 { return val * (1.0/214748364.0); }
1096 static __inline ALdouble Conv_ALdouble_ALuint(ALuint val)
1097 { return (ALint)(val-2147483648u) * (1.0/2147483647.0); }
1098 static __inline ALdouble Conv_ALdouble_ALfloat(ALfloat val)
1099 { return val; }
1100 static __inline ALdouble Conv_ALdouble_ALdouble(ALdouble val)
1101 { return val; }
1102 static __inline ALdouble Conv_ALdouble_ALmulaw(ALmulaw val)
1103 { return muLawDecompressionTable[val] * (1.0/32767.0); }
1106 #define DECL_TEMPLATE(T1, T2) \
1107 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint len) \
1109 ALuint i; \
1110 for(i = 0;i < len;i++) \
1111 *(dst++) = Conv_##T1##_##T2(*(src++)); \
1114 DECL_TEMPLATE(ALbyte, ALbyte)
1115 DECL_TEMPLATE(ALbyte, ALubyte)
1116 DECL_TEMPLATE(ALbyte, ALshort)
1117 DECL_TEMPLATE(ALbyte, ALushort)
1118 DECL_TEMPLATE(ALbyte, ALint)
1119 DECL_TEMPLATE(ALbyte, ALuint)
1120 DECL_TEMPLATE(ALbyte, ALfloat)
1121 DECL_TEMPLATE(ALbyte, ALdouble)
1122 DECL_TEMPLATE(ALbyte, ALmulaw)
1124 DECL_TEMPLATE(ALubyte, ALbyte)
1125 DECL_TEMPLATE(ALubyte, ALubyte)
1126 DECL_TEMPLATE(ALubyte, ALshort)
1127 DECL_TEMPLATE(ALubyte, ALushort)
1128 DECL_TEMPLATE(ALubyte, ALint)
1129 DECL_TEMPLATE(ALubyte, ALuint)
1130 DECL_TEMPLATE(ALubyte, ALfloat)
1131 DECL_TEMPLATE(ALubyte, ALdouble)
1132 DECL_TEMPLATE(ALubyte, ALmulaw)
1134 DECL_TEMPLATE(ALshort, ALbyte)
1135 DECL_TEMPLATE(ALshort, ALubyte)
1136 DECL_TEMPLATE(ALshort, ALshort)
1137 DECL_TEMPLATE(ALshort, ALushort)
1138 DECL_TEMPLATE(ALshort, ALint)
1139 DECL_TEMPLATE(ALshort, ALuint)
1140 DECL_TEMPLATE(ALshort, ALfloat)
1141 DECL_TEMPLATE(ALshort, ALdouble)
1142 DECL_TEMPLATE(ALshort, ALmulaw)
1144 DECL_TEMPLATE(ALushort, ALbyte)
1145 DECL_TEMPLATE(ALushort, ALubyte)
1146 DECL_TEMPLATE(ALushort, ALshort)
1147 DECL_TEMPLATE(ALushort, ALushort)
1148 DECL_TEMPLATE(ALushort, ALint)
1149 DECL_TEMPLATE(ALushort, ALuint)
1150 DECL_TEMPLATE(ALushort, ALfloat)
1151 DECL_TEMPLATE(ALushort, ALdouble)
1152 DECL_TEMPLATE(ALushort, ALmulaw)
1154 DECL_TEMPLATE(ALint, ALbyte)
1155 DECL_TEMPLATE(ALint, ALubyte)
1156 DECL_TEMPLATE(ALint, ALshort)
1157 DECL_TEMPLATE(ALint, ALushort)
1158 DECL_TEMPLATE(ALint, ALint)
1159 DECL_TEMPLATE(ALint, ALuint)
1160 DECL_TEMPLATE(ALint, ALfloat)
1161 DECL_TEMPLATE(ALint, ALdouble)
1162 DECL_TEMPLATE(ALint, ALmulaw)
1164 DECL_TEMPLATE(ALuint, ALbyte)
1165 DECL_TEMPLATE(ALuint, ALubyte)
1166 DECL_TEMPLATE(ALuint, ALshort)
1167 DECL_TEMPLATE(ALuint, ALushort)
1168 DECL_TEMPLATE(ALuint, ALint)
1169 DECL_TEMPLATE(ALuint, ALuint)
1170 DECL_TEMPLATE(ALuint, ALfloat)
1171 DECL_TEMPLATE(ALuint, ALdouble)
1172 DECL_TEMPLATE(ALuint, ALmulaw)
1174 DECL_TEMPLATE(ALfloat, ALbyte)
1175 DECL_TEMPLATE(ALfloat, ALubyte)
1176 DECL_TEMPLATE(ALfloat, ALshort)
1177 DECL_TEMPLATE(ALfloat, ALushort)
1178 DECL_TEMPLATE(ALfloat, ALint)
1179 DECL_TEMPLATE(ALfloat, ALuint)
1180 DECL_TEMPLATE(ALfloat, ALfloat)
1181 DECL_TEMPLATE(ALfloat, ALdouble)
1182 DECL_TEMPLATE(ALfloat, ALmulaw)
1184 DECL_TEMPLATE(ALdouble, ALbyte)
1185 DECL_TEMPLATE(ALdouble, ALubyte)
1186 DECL_TEMPLATE(ALdouble, ALshort)
1187 DECL_TEMPLATE(ALdouble, ALushort)
1188 DECL_TEMPLATE(ALdouble, ALint)
1189 DECL_TEMPLATE(ALdouble, ALuint)
1190 DECL_TEMPLATE(ALdouble, ALfloat)
1191 DECL_TEMPLATE(ALdouble, ALdouble)
1192 DECL_TEMPLATE(ALdouble, ALmulaw)
1194 #undef DECL_TEMPLATE
1196 #define DECL_TEMPLATE(T) \
1197 static void Convert_##T(T *dst, const ALvoid *src, enum SrcFmtType srcType, \
1198 ALsizei len) \
1200 switch(srcType) \
1202 case SrcFmtByte: \
1203 Convert_##T##_ALbyte(dst, src, len); \
1204 break; \
1205 case SrcFmtUByte: \
1206 Convert_##T##_ALubyte(dst, src, len); \
1207 break; \
1208 case SrcFmtShort: \
1209 Convert_##T##_ALshort(dst, src, len); \
1210 break; \
1211 case SrcFmtUShort: \
1212 Convert_##T##_ALushort(dst, src, len); \
1213 break; \
1214 case SrcFmtInt: \
1215 Convert_##T##_ALint(dst, src, len); \
1216 break; \
1217 case SrcFmtUInt: \
1218 Convert_##T##_ALuint(dst, src, len); \
1219 break; \
1220 case SrcFmtFloat: \
1221 Convert_##T##_ALfloat(dst, src, len); \
1222 break; \
1223 case SrcFmtDouble: \
1224 Convert_##T##_ALdouble(dst, src, len); \
1225 break; \
1226 case SrcFmtMulaw: \
1227 Convert_##T##_ALmulaw(dst, src, len); \
1228 break; \
1229 case SrcFmtIMA4: \
1230 break; /* not handled here */ \
1234 DECL_TEMPLATE(ALbyte)
1235 DECL_TEMPLATE(ALubyte)
1236 DECL_TEMPLATE(ALshort)
1237 DECL_TEMPLATE(ALushort)
1238 DECL_TEMPLATE(ALint)
1239 DECL_TEMPLATE(ALuint)
1240 DECL_TEMPLATE(ALfloat)
1241 DECL_TEMPLATE(ALdouble)
1243 #undef DECL_TEMPLATE
1247 * LoadData
1249 * Loads the specified data into the buffer, using the specified formats.
1250 * Currently, the new format must have the same channel configuration as the
1251 * original format. This does NOT handle compressed formats (eg. IMA4).
1253 static ALenum LoadData(ALbuffer *ALBuf, ALuint freq, ALenum NewFormat, ALsizei size, enum SrcFmtChannels SrcChannels, enum SrcFmtType SrcType, const ALvoid *data)
1255 ALuint NewBytes = aluBytesFromFormat(NewFormat);
1256 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
1257 ALuint OrigBytes = BytesFromSrcFmt(SrcType);
1258 ALuint OrigChannels = ChannelsFromSrcFmt(SrcChannels);
1259 enum FmtChannels DstChannels;
1260 enum FmtType DstType;
1261 ALuint64 newsize;
1262 ALvoid *temp;
1264 assert(NewChannels == OrigChannels);
1266 DecomposeFormat(NewFormat, &DstChannels, &DstType);
1268 if((size%(OrigBytes*OrigChannels)) != 0)
1269 return AL_INVALID_VALUE;
1271 newsize = size / OrigBytes;
1272 newsize *= NewBytes;
1273 if(newsize > INT_MAX)
1274 return AL_OUT_OF_MEMORY;
1276 temp = realloc(ALBuf->data, newsize);
1277 if(!temp) return AL_OUT_OF_MEMORY;
1278 ALBuf->data = temp;
1279 ALBuf->size = newsize;
1281 if(data != NULL)
1283 // Samples are converted here
1284 ConvertData(ALBuf->data, DstType, data, SrcType, newsize/NewBytes);
1287 ALBuf->Frequency = freq;
1288 ALBuf->FmtType = DstType;
1289 ALBuf->FmtChannels = DstChannels;
1291 ALBuf->LoopStart = 0;
1292 ALBuf->LoopEnd = newsize / NewChannels / NewBytes;
1294 ALBuf->OriginalChannels = SrcChannels;
1295 ALBuf->OriginalType = SrcType;
1296 ALBuf->OriginalSize = size;
1297 ALBuf->OriginalAlign = OrigBytes * OrigChannels;
1299 return AL_NO_ERROR;
1302 static void ConvertData(ALvoid *dst, enum FmtType dstType, const ALvoid *src, enum SrcFmtType srcType, ALsizei len)
1304 switch(dstType)
1306 (void)Convert_ALbyte;
1307 case FmtUByte:
1308 Convert_ALubyte(dst, src, srcType, len);
1309 break;
1310 case FmtShort:
1311 Convert_ALshort(dst, src, srcType, len);
1312 break;
1313 (void)Convert_ALushort;
1314 (void)Convert_ALint;
1315 (void)Convert_ALuint;
1316 case FmtFloat:
1317 Convert_ALfloat(dst, src, srcType, len);
1318 break;
1319 (void)Convert_ALdouble;
1324 ALuint BytesFromSrcFmt(enum SrcFmtType type)
1326 switch(type)
1328 case SrcFmtByte: return sizeof(ALbyte);
1329 case SrcFmtUByte: return sizeof(ALubyte);
1330 case SrcFmtShort: return sizeof(ALshort);
1331 case SrcFmtUShort: return sizeof(ALushort);
1332 case SrcFmtInt: return sizeof(ALint);
1333 case SrcFmtUInt: return sizeof(ALuint);
1334 case SrcFmtFloat: return sizeof(ALfloat);
1335 case SrcFmtDouble: return sizeof(ALdouble);
1336 case SrcFmtMulaw: return sizeof(ALubyte);
1337 case SrcFmtIMA4: break; /* not handled here */
1339 return 0;
1341 ALuint ChannelsFromSrcFmt(enum SrcFmtChannels chans)
1343 switch(chans)
1345 case SrcFmtMono: return 1;
1346 case SrcFmtStereo: return 2;
1347 case SrcFmtRear: return 2;
1348 case SrcFmtQuad: return 4;
1349 case SrcFmtX51: return 6;
1350 case SrcFmtX61: return 7;
1351 case SrcFmtX71: return 8;
1353 return 0;
1355 ALboolean DecomposeInputFormat(ALenum format, enum SrcFmtChannels *chans,
1356 enum SrcFmtType *type)
1358 switch(format)
1360 case AL_FORMAT_MONO8:
1361 *chans = SrcFmtMono;
1362 *type = SrcFmtUByte;
1363 return AL_TRUE;
1364 case AL_FORMAT_MONO16:
1365 *chans = SrcFmtMono;
1366 *type = SrcFmtShort;
1367 return AL_TRUE;
1368 case AL_FORMAT_MONO_FLOAT32:
1369 *chans = SrcFmtMono;
1370 *type = SrcFmtFloat;
1371 return AL_TRUE;
1372 case AL_FORMAT_MONO_DOUBLE_EXT:
1373 *chans = SrcFmtMono;
1374 *type = SrcFmtDouble;
1375 return AL_TRUE;
1376 case AL_FORMAT_MONO_IMA4:
1377 *chans = SrcFmtMono;
1378 *type = SrcFmtIMA4;
1379 return AL_TRUE;
1380 case AL_FORMAT_STEREO8:
1381 *chans = SrcFmtStereo;
1382 *type = SrcFmtUByte;
1383 return AL_TRUE;
1384 case AL_FORMAT_STEREO16:
1385 *chans = SrcFmtStereo;
1386 *type = SrcFmtShort;
1387 return AL_TRUE;
1388 case AL_FORMAT_STEREO_FLOAT32:
1389 *chans = SrcFmtStereo;
1390 *type = SrcFmtFloat;
1391 return AL_TRUE;
1392 case AL_FORMAT_STEREO_DOUBLE_EXT:
1393 *chans = SrcFmtStereo;
1394 *type = SrcFmtDouble;
1395 return AL_TRUE;
1396 case AL_FORMAT_STEREO_IMA4:
1397 *chans = SrcFmtStereo;
1398 *type = SrcFmtIMA4;
1399 return AL_TRUE;
1400 case AL_FORMAT_QUAD8_LOKI:
1401 case AL_FORMAT_QUAD8:
1402 *chans = SrcFmtQuad;
1403 *type = SrcFmtUByte;
1404 return AL_TRUE;
1405 case AL_FORMAT_QUAD16_LOKI:
1406 case AL_FORMAT_QUAD16:
1407 *chans = SrcFmtQuad;
1408 *type = SrcFmtShort;
1409 return AL_TRUE;
1410 case AL_FORMAT_QUAD32:
1411 *chans = SrcFmtQuad;
1412 *type = SrcFmtFloat;
1413 return AL_TRUE;
1414 case AL_FORMAT_REAR8:
1415 *chans = SrcFmtRear;
1416 *type = SrcFmtUByte;
1417 return AL_TRUE;
1418 case AL_FORMAT_REAR16:
1419 *chans = SrcFmtRear;
1420 *type = SrcFmtShort;
1421 return AL_TRUE;
1422 case AL_FORMAT_REAR32:
1423 *chans = SrcFmtRear;
1424 *type = SrcFmtFloat;
1425 return AL_TRUE;
1426 case AL_FORMAT_51CHN8:
1427 *chans = SrcFmtX51;
1428 *type = SrcFmtUByte;
1429 return AL_TRUE;
1430 case AL_FORMAT_51CHN16:
1431 *chans = SrcFmtX51;
1432 *type = SrcFmtShort;
1433 return AL_TRUE;
1434 case AL_FORMAT_51CHN32:
1435 *chans = SrcFmtX51;
1436 *type = SrcFmtFloat;
1437 return AL_TRUE;
1438 case AL_FORMAT_61CHN8:
1439 *chans = SrcFmtX61;
1440 *type = SrcFmtUByte;
1441 return AL_TRUE;
1442 case AL_FORMAT_61CHN16:
1443 *chans = SrcFmtX61;
1444 *type = SrcFmtShort;
1445 return AL_TRUE;
1446 case AL_FORMAT_61CHN32:
1447 *chans = SrcFmtX61;
1448 *type = SrcFmtFloat;
1449 return AL_TRUE;
1450 case AL_FORMAT_71CHN8:
1451 *chans = SrcFmtX71;
1452 *type = SrcFmtUByte;
1453 return AL_TRUE;
1454 case AL_FORMAT_71CHN16:
1455 *chans = SrcFmtX71;
1456 *type = SrcFmtShort;
1457 return AL_TRUE;
1458 case AL_FORMAT_71CHN32:
1459 *chans = SrcFmtX71;
1460 *type = SrcFmtFloat;
1461 return AL_TRUE;
1462 case AL_FORMAT_MONO_MULAW:
1463 *chans = SrcFmtMono;
1464 *type = SrcFmtMulaw;
1465 return AL_TRUE;
1466 case AL_FORMAT_STEREO_MULAW:
1467 *chans = SrcFmtStereo;
1468 *type = SrcFmtMulaw;
1469 return AL_TRUE;
1470 case AL_FORMAT_QUAD_MULAW:
1471 *chans = SrcFmtQuad;
1472 *type = SrcFmtMulaw;
1473 return AL_TRUE;
1474 case AL_FORMAT_REAR_MULAW:
1475 *chans = SrcFmtRear;
1476 *type = SrcFmtMulaw;
1477 return AL_TRUE;
1478 case AL_FORMAT_51CHN_MULAW:
1479 *chans = SrcFmtX51;
1480 *type = SrcFmtMulaw;
1481 return AL_TRUE;
1482 case AL_FORMAT_61CHN_MULAW:
1483 *chans = SrcFmtX61;
1484 *type = SrcFmtMulaw;
1485 return AL_TRUE;
1486 case AL_FORMAT_71CHN_MULAW:
1487 *chans = SrcFmtX71;
1488 *type = SrcFmtMulaw;
1489 return AL_TRUE;
1491 return AL_FALSE;
1494 ALuint BytesFromFmt(enum FmtType type)
1496 switch(type)
1498 case FmtUByte: return sizeof(ALubyte);
1499 case FmtShort: return sizeof(ALshort);
1500 case FmtFloat: return sizeof(ALfloat);
1502 return 0;
1504 ALuint ChannelsFromFmt(enum FmtChannels chans)
1506 switch(chans)
1508 case FmtMono: return 1;
1509 case FmtStereo: return 2;
1510 case FmtRear: return 2;
1511 case FmtQuad: return 4;
1512 case FmtX51: return 6;
1513 case FmtX61: return 7;
1514 case FmtX71: return 8;
1516 return 0;
1518 ALboolean DecomposeFormat(ALenum format, enum FmtChannels *chans, enum FmtType *type)
1520 switch(format)
1522 case AL_FORMAT_MONO8:
1523 *chans = FmtMono;
1524 *type = FmtUByte;
1525 return AL_TRUE;
1526 case AL_FORMAT_MONO16:
1527 *chans = FmtMono;
1528 *type = FmtShort;
1529 return AL_TRUE;
1530 case AL_FORMAT_MONO_FLOAT32:
1531 *chans = FmtMono;
1532 *type = FmtFloat;
1533 return AL_TRUE;
1534 case AL_FORMAT_STEREO8:
1535 *chans = FmtStereo;
1536 *type = FmtUByte;
1537 return AL_TRUE;
1538 case AL_FORMAT_STEREO16:
1539 *chans = FmtStereo;
1540 *type = FmtShort;
1541 return AL_TRUE;
1542 case AL_FORMAT_STEREO_FLOAT32:
1543 *chans = FmtStereo;
1544 *type = FmtFloat;
1545 return AL_TRUE;
1546 case AL_FORMAT_QUAD8_LOKI:
1547 case AL_FORMAT_QUAD8:
1548 *chans = FmtQuad;
1549 *type = FmtUByte;
1550 return AL_TRUE;
1551 case AL_FORMAT_QUAD16_LOKI:
1552 case AL_FORMAT_QUAD16:
1553 *chans = FmtQuad;
1554 *type = FmtShort;
1555 return AL_TRUE;
1556 case AL_FORMAT_QUAD32:
1557 *chans = FmtQuad;
1558 *type = FmtFloat;
1559 return AL_TRUE;
1560 case AL_FORMAT_REAR8:
1561 *chans = FmtRear;
1562 *type = FmtUByte;
1563 return AL_TRUE;
1564 case AL_FORMAT_REAR16:
1565 *chans = FmtRear;
1566 *type = FmtShort;
1567 return AL_TRUE;
1568 case AL_FORMAT_REAR32:
1569 *chans = FmtRear;
1570 *type = FmtFloat;
1571 return AL_TRUE;
1572 case AL_FORMAT_51CHN8:
1573 *chans = FmtX51;
1574 *type = FmtUByte;
1575 return AL_TRUE;
1576 case AL_FORMAT_51CHN16:
1577 *chans = FmtX51;
1578 *type = FmtShort;
1579 return AL_TRUE;
1580 case AL_FORMAT_51CHN32:
1581 *chans = FmtX51;
1582 *type = FmtFloat;
1583 return AL_TRUE;
1584 case AL_FORMAT_61CHN8:
1585 *chans = FmtX61;
1586 *type = FmtUByte;
1587 return AL_TRUE;
1588 case AL_FORMAT_61CHN16:
1589 *chans = FmtX61;
1590 *type = FmtShort;
1591 return AL_TRUE;
1592 case AL_FORMAT_61CHN32:
1593 *chans = FmtX61;
1594 *type = FmtFloat;
1595 return AL_TRUE;
1596 case AL_FORMAT_71CHN8:
1597 *chans = FmtX71;
1598 *type = FmtUByte;
1599 return AL_TRUE;
1600 case AL_FORMAT_71CHN16:
1601 *chans = FmtX71;
1602 *type = FmtShort;
1603 return AL_TRUE;
1604 case AL_FORMAT_71CHN32:
1605 *chans = FmtX71;
1606 *type = FmtFloat;
1607 return AL_TRUE;
1609 return AL_FALSE;
1614 * ReleaseALBuffers()
1616 * INTERNAL FN : Called by alcCloseDevice to destroy any buffers that still exist
1618 ALvoid ReleaseALBuffers(ALCdevice *device)
1620 ALsizei i;
1621 for(i = 0;i < device->BufferMap.size;i++)
1623 ALbuffer *temp = device->BufferMap.array[i].value;
1624 device->BufferMap.array[i].value = NULL;
1626 // Release sample data
1627 free(temp->data);
1629 // Release Buffer structure
1630 ALTHUNK_REMOVEENTRY(temp->buffer);
1631 memset(temp, 0, sizeof(ALbuffer));
1632 free(temp);