Allow LoadData to handle IMA4 data
[openal-soft.git] / OpenAL32 / alBuffer.c
blobbba92b21d5221c37a0a3f672a6966c56e46f9c20
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, enum FmtType dstType, const ALvoid *src, ALint chans, 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 ALenum err;
259 Context = GetContextSuspended();
260 if(!Context) return;
262 if(Context->SampleSource)
264 ALintptrEXT offset;
266 if(Context->SampleSource->state == MAPPED)
268 alSetError(Context, AL_INVALID_OPERATION);
269 ProcessContext(Context);
270 return;
273 offset = (const ALubyte*)data - (ALubyte*)NULL;
274 data = Context->SampleSource->data + offset;
277 device = Context->Device;
278 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
279 alSetError(Context, AL_INVALID_NAME);
280 else if(ALBuf->refcount != 0)
281 alSetError(Context, AL_INVALID_VALUE);
282 else if(size < 0 || freq < 0)
283 alSetError(Context, AL_INVALID_VALUE);
284 else if(DecomposeInputFormat(format, &SrcChannels, &SrcType) == AL_FALSE)
285 alSetError(Context, AL_INVALID_ENUM);
286 else switch(SrcType)
288 case SrcFmtByte:
289 case SrcFmtUByte:
290 case SrcFmtShort:
291 case SrcFmtUShort:
292 case SrcFmtInt:
293 case SrcFmtUInt:
294 case SrcFmtFloat:
295 err = LoadData(ALBuf, freq, format, size, SrcChannels, SrcType, data);
296 if(err != AL_NO_ERROR)
297 alSetError(Context, err);
298 break;
300 case SrcFmtDouble:
301 if(SrcChannels == SrcFmtMono)
302 err = LoadData(ALBuf, freq, AL_FORMAT_MONO_FLOAT32, size, SrcChannels, SrcType, data);
303 else
304 err = LoadData(ALBuf, freq, AL_FORMAT_STEREO_FLOAT32, size, SrcChannels, SrcType, data);
305 if(err != AL_NO_ERROR)
306 alSetError(Context, err);
307 break;
309 case SrcFmtMulaw:
310 case SrcFmtIMA4: {
311 ALenum NewFormat = AL_FORMAT_MONO16;
312 switch(SrcChannels)
314 case SrcFmtMono: NewFormat = AL_FORMAT_MONO16; break;
315 case SrcFmtStereo: NewFormat = AL_FORMAT_STEREO16; break;
316 case SrcFmtRear: NewFormat = AL_FORMAT_REAR16; break;
317 case SrcFmtQuad: NewFormat = AL_FORMAT_QUAD16; break;
318 case SrcFmtX51: NewFormat = AL_FORMAT_51CHN16; break;
319 case SrcFmtX61: NewFormat = AL_FORMAT_61CHN16; break;
320 case SrcFmtX71: NewFormat = AL_FORMAT_71CHN16; break;
322 err = LoadData(ALBuf, freq, NewFormat, size, SrcChannels, SrcType, data);
323 if(err != AL_NO_ERROR)
324 alSetError(Context, err);
325 } break;
328 ProcessContext(Context);
332 * alBufferSubDataSOFT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
334 * Update buffer's audio data
336 AL_API ALvoid AL_APIENTRY alBufferSubDataSOFT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
338 enum SrcFmtChannels SrcChannels;
339 enum SrcFmtType SrcType;
340 ALCcontext *Context;
341 ALCdevice *device;
342 ALbuffer *ALBuf;
344 Context = GetContextSuspended();
345 if(!Context) return;
347 if(Context->SampleSource)
349 ALintptrEXT offset;
351 if(Context->SampleSource->state == MAPPED)
353 alSetError(Context, AL_INVALID_OPERATION);
354 ProcessContext(Context);
355 return;
358 offset = (const ALubyte*)data - (ALubyte*)NULL;
359 data = Context->SampleSource->data + offset;
362 device = Context->Device;
363 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
364 alSetError(Context, AL_INVALID_NAME);
365 else if(length < 0 || offset < 0 || (length > 0 && data == NULL))
366 alSetError(Context, AL_INVALID_VALUE);
367 else if(DecomposeInputFormat(format, &SrcChannels, &SrcType) == AL_FALSE ||
368 SrcChannels != ALBuf->OriginalChannels ||
369 SrcType != ALBuf->OriginalType)
370 alSetError(Context, AL_INVALID_ENUM);
371 else if(offset > ALBuf->OriginalSize ||
372 length > ALBuf->OriginalSize-offset ||
373 (offset%ALBuf->OriginalAlign) != 0 ||
374 (length%ALBuf->OriginalAlign) != 0)
375 alSetError(Context, AL_INVALID_VALUE);
376 else
378 if(SrcType == SrcFmtIMA4)
380 ALuint Channels = ChannelsFromFmt(ALBuf->FmtChannels);
381 ALuint Bytes = BytesFromFmt(ALBuf->FmtType);
383 /* offset -> byte offset, length -> block count */
384 offset /= 36;
385 offset *= 65;
386 offset *= Bytes;
387 length /= ALBuf->OriginalAlign;
389 ConvertDataIMA4(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
390 data, Channels, length);
392 else
394 ALuint OldBytes = BytesFromFmt(SrcType);
395 ALuint Bytes = BytesFromFmt(ALBuf->FmtType);
397 offset /= OldBytes;
398 offset *= Bytes;
399 length /= OldBytes;
401 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
402 data, SrcType, length);
406 ProcessContext(Context);
409 AL_API ALvoid AL_APIENTRY alBufferSubDataEXT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
411 alBufferSubDataSOFT(buffer, format, data, offset, length);
415 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
417 ALCcontext *pContext;
418 ALCdevice *device;
420 (void)flValue;
422 pContext = GetContextSuspended();
423 if(!pContext) return;
425 device = pContext->Device;
426 if(LookupBuffer(device->BufferMap, buffer) == NULL)
427 alSetError(pContext, AL_INVALID_NAME);
428 else
430 switch(eParam)
432 default:
433 alSetError(pContext, AL_INVALID_ENUM);
434 break;
438 ProcessContext(pContext);
442 AL_API void AL_APIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
444 ALCcontext *pContext;
445 ALCdevice *device;
447 (void)flValue1;
448 (void)flValue2;
449 (void)flValue3;
451 pContext = GetContextSuspended();
452 if(!pContext) return;
454 device = pContext->Device;
455 if(LookupBuffer(device->BufferMap, buffer) == NULL)
456 alSetError(pContext, AL_INVALID_NAME);
457 else
459 switch(eParam)
461 default:
462 alSetError(pContext, AL_INVALID_ENUM);
463 break;
467 ProcessContext(pContext);
471 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
473 ALCcontext *pContext;
474 ALCdevice *device;
476 pContext = GetContextSuspended();
477 if(!pContext) return;
479 device = pContext->Device;
480 if(!flValues)
481 alSetError(pContext, AL_INVALID_VALUE);
482 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
483 alSetError(pContext, AL_INVALID_NAME);
484 else
486 switch(eParam)
488 default:
489 alSetError(pContext, AL_INVALID_ENUM);
490 break;
494 ProcessContext(pContext);
498 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
500 ALCcontext *pContext;
501 ALCdevice *device;
503 (void)lValue;
505 pContext = GetContextSuspended();
506 if(!pContext) return;
508 device = pContext->Device;
509 if(LookupBuffer(device->BufferMap, buffer) == NULL)
510 alSetError(pContext, AL_INVALID_NAME);
511 else
513 switch(eParam)
515 default:
516 alSetError(pContext, AL_INVALID_ENUM);
517 break;
521 ProcessContext(pContext);
525 AL_API void AL_APIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
527 ALCcontext *pContext;
528 ALCdevice *device;
530 (void)lValue1;
531 (void)lValue2;
532 (void)lValue3;
534 pContext = GetContextSuspended();
535 if(!pContext) return;
537 device = pContext->Device;
538 if(LookupBuffer(device->BufferMap, buffer) == NULL)
539 alSetError(pContext, AL_INVALID_NAME);
540 else
542 switch(eParam)
544 default:
545 alSetError(pContext, AL_INVALID_ENUM);
546 break;
550 ProcessContext(pContext);
554 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
556 ALCcontext *pContext;
557 ALCdevice *device;
558 ALbuffer *ALBuf;
560 pContext = GetContextSuspended();
561 if(!pContext) return;
563 device = pContext->Device;
564 if(!plValues)
565 alSetError(pContext, AL_INVALID_VALUE);
566 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
567 alSetError(pContext, AL_INVALID_NAME);
568 else
570 switch(eParam)
572 case AL_LOOP_POINTS:
573 if(ALBuf->refcount > 0)
574 alSetError(pContext, AL_INVALID_OPERATION);
575 else if(plValues[0] < 0 || plValues[1] < 0 ||
576 plValues[0] >= plValues[1] || ALBuf->size == 0)
577 alSetError(pContext, AL_INVALID_VALUE);
578 else
580 ALint maxlen = ALBuf->size /
581 FrameSizeFromFmt(ALBuf->FmtChannels, ALBuf->FmtType);
582 if(plValues[0] > maxlen || plValues[1] > maxlen)
583 alSetError(pContext, AL_INVALID_VALUE);
584 else
586 ALBuf->LoopStart = plValues[0];
587 ALBuf->LoopEnd = plValues[1];
590 break;
592 default:
593 alSetError(pContext, AL_INVALID_ENUM);
594 break;
598 ProcessContext(pContext);
602 AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
604 ALCcontext *pContext;
605 ALCdevice *device;
607 pContext = GetContextSuspended();
608 if(!pContext) return;
610 device = pContext->Device;
611 if(!pflValue)
612 alSetError(pContext, AL_INVALID_VALUE);
613 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
614 alSetError(pContext, AL_INVALID_NAME);
615 else
617 switch(eParam)
619 default:
620 alSetError(pContext, AL_INVALID_ENUM);
621 break;
625 ProcessContext(pContext);
629 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
631 ALCcontext *pContext;
632 ALCdevice *device;
634 pContext = GetContextSuspended();
635 if(!pContext) return;
637 device = pContext->Device;
638 if(!pflValue1 || !pflValue2 || !pflValue3)
639 alSetError(pContext, AL_INVALID_VALUE);
640 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
641 alSetError(pContext, AL_INVALID_NAME);
642 else
644 switch(eParam)
646 default:
647 alSetError(pContext, AL_INVALID_ENUM);
648 break;
652 ProcessContext(pContext);
656 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
658 ALCcontext *pContext;
659 ALCdevice *device;
661 pContext = GetContextSuspended();
662 if(!pContext) return;
664 device = pContext->Device;
665 if(!pflValues)
666 alSetError(pContext, AL_INVALID_VALUE);
667 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
668 alSetError(pContext, AL_INVALID_NAME);
669 else
671 switch(eParam)
673 default:
674 alSetError(pContext, AL_INVALID_ENUM);
675 break;
679 ProcessContext(pContext);
683 AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
685 ALCcontext *pContext;
686 ALbuffer *pBuffer;
687 ALCdevice *device;
689 pContext = GetContextSuspended();
690 if(!pContext) return;
692 device = pContext->Device;
693 if(!plValue)
694 alSetError(pContext, AL_INVALID_VALUE);
695 else if((pBuffer=LookupBuffer(device->BufferMap, buffer)) == NULL)
696 alSetError(pContext, AL_INVALID_NAME);
697 else
699 switch(eParam)
701 case AL_FREQUENCY:
702 *plValue = pBuffer->Frequency;
703 break;
705 case AL_BITS:
706 *plValue = BytesFromFmt(pBuffer->FmtType) * 8;
707 break;
709 case AL_CHANNELS:
710 *plValue = ChannelsFromFmt(pBuffer->FmtChannels);
711 break;
713 case AL_SIZE:
714 *plValue = pBuffer->size;
715 break;
717 default:
718 alSetError(pContext, AL_INVALID_ENUM);
719 break;
723 ProcessContext(pContext);
727 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
729 ALCcontext *pContext;
730 ALCdevice *device;
732 pContext = GetContextSuspended();
733 if(!pContext) return;
735 device = pContext->Device;
736 if(!plValue1 || !plValue2 || !plValue3)
737 alSetError(pContext, AL_INVALID_VALUE);
738 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
739 alSetError(pContext, AL_INVALID_NAME);
740 else
742 switch(eParam)
744 default:
745 alSetError(pContext, AL_INVALID_ENUM);
746 break;
750 ProcessContext(pContext);
754 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
756 ALCcontext *pContext;
757 ALCdevice *device;
758 ALbuffer *ALBuf;
760 pContext = GetContextSuspended();
761 if(!pContext) return;
763 device = pContext->Device;
764 if(!plValues)
765 alSetError(pContext, AL_INVALID_VALUE);
766 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
767 alSetError(pContext, AL_INVALID_NAME);
768 else
770 switch(eParam)
772 case AL_FREQUENCY:
773 case AL_BITS:
774 case AL_CHANNELS:
775 case AL_SIZE:
776 alGetBufferi(buffer, eParam, plValues);
777 break;
779 case AL_LOOP_POINTS:
780 plValues[0] = ALBuf->LoopStart;
781 plValues[1] = ALBuf->LoopEnd;
782 break;
784 default:
785 alSetError(pContext, AL_INVALID_ENUM);
786 break;
790 ProcessContext(pContext);
794 static void ConvertIMA4Block(ALshort *dst, const ALubyte *IMAData, ALint numchans)
796 ALint Sample[2],Index[2];
797 ALuint IMACode[2];
798 ALsizei j,k,c;
800 for(c = 0;c < numchans;c++)
802 Sample[c] = *(IMAData++);
803 Sample[c] |= *(IMAData++) << 8;
804 Sample[c] = (Sample[c]^0x8000) - 32768;
805 Index[c] = *(IMAData++);
806 Index[c] |= *(IMAData++) << 8;
807 Index[c] = (Index[c]^0x8000) - 32768;
809 Index[c] = ((Index[c]<0) ? 0 : Index[c]);
810 Index[c] = ((Index[c]>88) ? 88 : Index[c]);
812 dst[c] = Sample[c];
815 j = 1;
816 while(j < 65)
818 for(c = 0;c < numchans;c++)
820 IMACode[c] = *(IMAData++);
821 IMACode[c] |= *(IMAData++) << 8;
822 IMACode[c] |= *(IMAData++) << 16;
823 IMACode[c] |= *(IMAData++) << 24;
826 for(k = 0;k < 8;k++,j++)
828 for(c = 0;c < numchans;c++)
830 Sample[c] += ((g_IMAStep_size[Index[c]]*g_IMACodeword_4[IMACode[c]&15])/8);
831 Index[c] += g_IMAIndex_adjust_4[IMACode[c]&15];
833 if(Sample[c] < -32768) Sample[c] = -32768;
834 else if(Sample[c] > 32767) Sample[c] = 32767;
836 if(Index[c]<0) Index[c] = 0;
837 else if(Index[c]>88) Index[c] = 88;
839 dst[(j+k)*numchans + c] = Sample[c];
840 IMACode[c] >>= 4;
847 typedef ALubyte ALmulaw;
849 static __inline ALbyte Conv_ALbyte_ALbyte(ALbyte val)
850 { return val; }
851 static __inline ALbyte Conv_ALbyte_ALubyte(ALubyte val)
852 { return val^0x80; }
853 static __inline ALbyte Conv_ALbyte_ALshort(ALshort val)
854 { return val>>8; }
855 static __inline ALbyte Conv_ALbyte_ALushort(ALushort val)
856 { return (val>>8)-128; }
857 static __inline ALbyte Conv_ALbyte_ALint(ALint val)
858 { return val>>24; }
859 static __inline ALbyte Conv_ALbyte_ALuint(ALuint val)
860 { return (val>>24)-128; }
861 static __inline ALbyte Conv_ALbyte_ALfloat(ALfloat val)
863 if(val >= 1.0f) return 127;
864 if(val <= -1.0f) return -128;
865 return (ALint)(val * 127.0f);
867 static __inline ALbyte Conv_ALbyte_ALdouble(ALdouble val)
869 if(val >= 1.0) return 127;
870 if(val <= -1.0) return -128;
871 return (ALint)(val * 127.0);
873 static __inline ALbyte Conv_ALbyte_ALmulaw(ALmulaw val)
874 { return muLawDecompressionTable[val]>>8; }
876 static __inline ALubyte Conv_ALubyte_ALbyte(ALbyte val)
877 { return val^0x80; }
878 static __inline ALubyte Conv_ALubyte_ALubyte(ALubyte val)
879 { return val; }
880 static __inline ALubyte Conv_ALubyte_ALshort(ALshort val)
881 { return (val>>8)+128; }
882 static __inline ALubyte Conv_ALubyte_ALushort(ALushort val)
883 { return val>>8; }
884 static __inline ALubyte Conv_ALubyte_ALint(ALint val)
885 { return (val>>24)+128; }
886 static __inline ALubyte Conv_ALubyte_ALuint(ALuint val)
887 { return val>>24; }
888 static __inline ALubyte Conv_ALubyte_ALfloat(ALfloat val)
890 if(val >= 1.0f) return 255;
891 if(val <= -1.0f) return 0;
892 return (ALint)(val * 127.0f) + 128;
894 static __inline ALubyte Conv_ALubyte_ALdouble(ALdouble val)
896 if(val >= 1.0) return 255;
897 if(val <= -1.0) return 0;
898 return (ALint)(val * 127.0) + 128;
900 static __inline ALubyte Conv_ALubyte_ALmulaw(ALmulaw val)
901 { return (muLawDecompressionTable[val]>>8)+128; }
903 static __inline ALshort Conv_ALshort_ALbyte(ALbyte val)
904 { return val<<8; }
905 static __inline ALshort Conv_ALshort_ALubyte(ALubyte val)
906 { return (val-128)<<8; }
907 static __inline ALshort Conv_ALshort_ALshort(ALshort val)
908 { return val; }
909 static __inline ALshort Conv_ALshort_ALushort(ALushort val)
910 { return val^0x8000; }
911 static __inline ALshort Conv_ALshort_ALint(ALint val)
912 { return val>>16; }
913 static __inline ALshort Conv_ALshort_ALuint(ALuint val)
914 { return (val>>16)-32768; }
915 static __inline ALshort Conv_ALshort_ALfloat(ALfloat val)
917 if(val >= 1.0f) return 32767;
918 if(val <= -1.0f) return -32768;
919 return (ALint)(val * 32767.0f);
921 static __inline ALshort Conv_ALshort_ALdouble(ALdouble val)
923 if(val >= 1.0) return 32767;
924 if(val <= -1.0) return -32768;
925 return (ALint)(val * 32767.0);
927 static __inline ALshort Conv_ALshort_ALmulaw(ALmulaw val)
928 { return muLawDecompressionTable[val]; }
930 static __inline ALushort Conv_ALushort_ALbyte(ALbyte val)
931 { return (val+128)<<8; }
932 static __inline ALushort Conv_ALushort_ALubyte(ALubyte val)
933 { return val<<8; }
934 static __inline ALushort Conv_ALushort_ALshort(ALshort val)
935 { return val^0x8000; }
936 static __inline ALushort Conv_ALushort_ALushort(ALushort val)
937 { return val; }
938 static __inline ALushort Conv_ALushort_ALint(ALint val)
939 { return (val>>16)+32768; }
940 static __inline ALushort Conv_ALushort_ALuint(ALuint val)
941 { return val>>16; }
942 static __inline ALushort Conv_ALushort_ALfloat(ALfloat val)
944 if(val >= 1.0f) return 65535;
945 if(val <= -1.0f) return 0;
946 return (ALint)(val * 32767.0f) + 32768;
948 static __inline ALushort Conv_ALushort_ALdouble(ALdouble val)
950 if(val >= 1.0) return 65535;
951 if(val <= -1.0) return 0;
952 return (ALint)(val * 32767.0) + 32768;
954 static __inline ALushort Conv_ALushort_ALmulaw(ALmulaw val)
955 { return muLawDecompressionTable[val]^0x8000; }
957 static __inline ALint Conv_ALint_ALbyte(ALbyte val)
958 { return val<<24; }
959 static __inline ALint Conv_ALint_ALubyte(ALubyte val)
960 { return (val-128)<<24; }
961 static __inline ALint Conv_ALint_ALshort(ALshort val)
962 { return val<<16; }
963 static __inline ALint Conv_ALint_ALushort(ALushort val)
964 { return (val-32768)<<16; }
965 static __inline ALint Conv_ALint_ALint(ALint val)
966 { return val; }
967 static __inline ALint Conv_ALint_ALuint(ALuint val)
968 { return val-2147483648u; }
969 static __inline ALint Conv_ALint_ALfloat(ALfloat val)
971 if(val >= 1.0f) return 2147483647;
972 if(val <= -1.0f) return -2147483648u;
973 return (ALint)(val * 2147483647.0);
975 static __inline ALint Conv_ALint_ALdouble(ALdouble val)
977 if(val >= 1.0) return 2147483647;
978 if(val <= -1.0) return -2147483648u;
979 return (ALint)(val * 2147483647.0);
981 static __inline ALint Conv_ALint_ALmulaw(ALmulaw val)
982 { return muLawDecompressionTable[val]<<16; }
984 static __inline ALuint Conv_ALuint_ALbyte(ALbyte val)
985 { return (val+128)<<24; }
986 static __inline ALuint Conv_ALuint_ALubyte(ALubyte val)
987 { return val<<24; }
988 static __inline ALuint Conv_ALuint_ALshort(ALshort val)
989 { return (val+32768)<<16; }
990 static __inline ALuint Conv_ALuint_ALushort(ALushort val)
991 { return val<<16; }
992 static __inline ALuint Conv_ALuint_ALint(ALint val)
993 { return val+2147483648u; }
994 static __inline ALuint Conv_ALuint_ALuint(ALuint val)
995 { return val; }
996 static __inline ALuint Conv_ALuint_ALfloat(ALfloat val)
998 if(val >= 1.0f) return 4294967295u;
999 if(val <= -1.0f) return 0;
1000 return (ALint)(val * 2147483647.0) + 2147483648u;
1002 static __inline ALuint Conv_ALuint_ALdouble(ALdouble val)
1004 if(val >= 1.0) return 4294967295u;
1005 if(val <= -1.0) return 0;
1006 return (ALint)(val * 2147483647.0) + 2147483648u;
1008 static __inline ALuint Conv_ALuint_ALmulaw(ALmulaw val)
1009 { return (muLawDecompressionTable[val]+32768)<<16; }
1011 static __inline ALfloat Conv_ALfloat_ALbyte(ALbyte val)
1012 { return val * (1.0f/127.0f); }
1013 static __inline ALfloat Conv_ALfloat_ALubyte(ALubyte val)
1014 { return (val-128) * (1.0f/127.0f); }
1015 static __inline ALfloat Conv_ALfloat_ALshort(ALshort val)
1016 { return val * (1.0f/32767.0f); }
1017 static __inline ALfloat Conv_ALfloat_ALushort(ALushort val)
1018 { return (val-32768) * (1.0f/32767.0f); }
1019 static __inline ALfloat Conv_ALfloat_ALint(ALint val)
1020 { return val * (1.0/2147483647.0); }
1021 static __inline ALfloat Conv_ALfloat_ALuint(ALuint val)
1022 { return (ALint)(val-2147483648u) * (1.0/2147483647.0); }
1023 static __inline ALfloat Conv_ALfloat_ALfloat(ALfloat val)
1024 { return val; }
1025 static __inline ALfloat Conv_ALfloat_ALdouble(ALdouble val)
1026 { return val; }
1027 static __inline ALfloat Conv_ALfloat_ALmulaw(ALmulaw val)
1028 { return muLawDecompressionTable[val] * (1.0f/32767.0f); }
1030 static __inline ALdouble Conv_ALdouble_ALbyte(ALbyte val)
1031 { return val * (1.0/127.0); }
1032 static __inline ALdouble Conv_ALdouble_ALubyte(ALubyte val)
1033 { return (val-128) * (1.0/127.0); }
1034 static __inline ALdouble Conv_ALdouble_ALshort(ALshort val)
1035 { return val * (1.0/32767.0); }
1036 static __inline ALdouble Conv_ALdouble_ALushort(ALushort val)
1037 { return (val-32768) * (1.0/32767.0); }
1038 static __inline ALdouble Conv_ALdouble_ALint(ALint val)
1039 { return val * (1.0/214748364.0); }
1040 static __inline ALdouble Conv_ALdouble_ALuint(ALuint val)
1041 { return (ALint)(val-2147483648u) * (1.0/2147483647.0); }
1042 static __inline ALdouble Conv_ALdouble_ALfloat(ALfloat val)
1043 { return val; }
1044 static __inline ALdouble Conv_ALdouble_ALdouble(ALdouble val)
1045 { return val; }
1046 static __inline ALdouble Conv_ALdouble_ALmulaw(ALmulaw val)
1047 { return muLawDecompressionTable[val] * (1.0/32767.0); }
1050 #define DECL_TEMPLATE(T1, T2) \
1051 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint len) \
1053 ALuint i; \
1054 for(i = 0;i < len;i++) \
1055 *(dst++) = Conv_##T1##_##T2(*(src++)); \
1058 DECL_TEMPLATE(ALbyte, ALbyte)
1059 DECL_TEMPLATE(ALbyte, ALubyte)
1060 DECL_TEMPLATE(ALbyte, ALshort)
1061 DECL_TEMPLATE(ALbyte, ALushort)
1062 DECL_TEMPLATE(ALbyte, ALint)
1063 DECL_TEMPLATE(ALbyte, ALuint)
1064 DECL_TEMPLATE(ALbyte, ALfloat)
1065 DECL_TEMPLATE(ALbyte, ALdouble)
1066 DECL_TEMPLATE(ALbyte, ALmulaw)
1068 DECL_TEMPLATE(ALubyte, ALbyte)
1069 DECL_TEMPLATE(ALubyte, ALubyte)
1070 DECL_TEMPLATE(ALubyte, ALshort)
1071 DECL_TEMPLATE(ALubyte, ALushort)
1072 DECL_TEMPLATE(ALubyte, ALint)
1073 DECL_TEMPLATE(ALubyte, ALuint)
1074 DECL_TEMPLATE(ALubyte, ALfloat)
1075 DECL_TEMPLATE(ALubyte, ALdouble)
1076 DECL_TEMPLATE(ALubyte, ALmulaw)
1078 DECL_TEMPLATE(ALshort, ALbyte)
1079 DECL_TEMPLATE(ALshort, ALubyte)
1080 DECL_TEMPLATE(ALshort, ALshort)
1081 DECL_TEMPLATE(ALshort, ALushort)
1082 DECL_TEMPLATE(ALshort, ALint)
1083 DECL_TEMPLATE(ALshort, ALuint)
1084 DECL_TEMPLATE(ALshort, ALfloat)
1085 DECL_TEMPLATE(ALshort, ALdouble)
1086 DECL_TEMPLATE(ALshort, ALmulaw)
1088 DECL_TEMPLATE(ALushort, ALbyte)
1089 DECL_TEMPLATE(ALushort, ALubyte)
1090 DECL_TEMPLATE(ALushort, ALshort)
1091 DECL_TEMPLATE(ALushort, ALushort)
1092 DECL_TEMPLATE(ALushort, ALint)
1093 DECL_TEMPLATE(ALushort, ALuint)
1094 DECL_TEMPLATE(ALushort, ALfloat)
1095 DECL_TEMPLATE(ALushort, ALdouble)
1096 DECL_TEMPLATE(ALushort, ALmulaw)
1098 DECL_TEMPLATE(ALint, ALbyte)
1099 DECL_TEMPLATE(ALint, ALubyte)
1100 DECL_TEMPLATE(ALint, ALshort)
1101 DECL_TEMPLATE(ALint, ALushort)
1102 DECL_TEMPLATE(ALint, ALint)
1103 DECL_TEMPLATE(ALint, ALuint)
1104 DECL_TEMPLATE(ALint, ALfloat)
1105 DECL_TEMPLATE(ALint, ALdouble)
1106 DECL_TEMPLATE(ALint, ALmulaw)
1108 DECL_TEMPLATE(ALuint, ALbyte)
1109 DECL_TEMPLATE(ALuint, ALubyte)
1110 DECL_TEMPLATE(ALuint, ALshort)
1111 DECL_TEMPLATE(ALuint, ALushort)
1112 DECL_TEMPLATE(ALuint, ALint)
1113 DECL_TEMPLATE(ALuint, ALuint)
1114 DECL_TEMPLATE(ALuint, ALfloat)
1115 DECL_TEMPLATE(ALuint, ALdouble)
1116 DECL_TEMPLATE(ALuint, ALmulaw)
1118 DECL_TEMPLATE(ALfloat, ALbyte)
1119 DECL_TEMPLATE(ALfloat, ALubyte)
1120 DECL_TEMPLATE(ALfloat, ALshort)
1121 DECL_TEMPLATE(ALfloat, ALushort)
1122 DECL_TEMPLATE(ALfloat, ALint)
1123 DECL_TEMPLATE(ALfloat, ALuint)
1124 DECL_TEMPLATE(ALfloat, ALfloat)
1125 DECL_TEMPLATE(ALfloat, ALdouble)
1126 DECL_TEMPLATE(ALfloat, ALmulaw)
1128 DECL_TEMPLATE(ALdouble, ALbyte)
1129 DECL_TEMPLATE(ALdouble, ALubyte)
1130 DECL_TEMPLATE(ALdouble, ALshort)
1131 DECL_TEMPLATE(ALdouble, ALushort)
1132 DECL_TEMPLATE(ALdouble, ALint)
1133 DECL_TEMPLATE(ALdouble, ALuint)
1134 DECL_TEMPLATE(ALdouble, ALfloat)
1135 DECL_TEMPLATE(ALdouble, ALdouble)
1136 DECL_TEMPLATE(ALdouble, ALmulaw)
1138 #undef DECL_TEMPLATE
1140 #define DECL_TEMPLATE(T) \
1141 static void Convert_##T##_IMA4(T *dst, const ALubyte *src, ALuint numchans, \
1142 ALuint numblocks) \
1144 ALshort tmp[65*2]; /* Max samples an IMA4 frame can be */ \
1145 ALuint i, j; \
1146 for(i = 0;i < numblocks;i++) \
1148 ConvertIMA4Block(tmp, src, numchans); \
1149 src += 36*numchans; \
1150 for(j = 0;j < 65*numchans;j++) \
1151 *(dst++) = Conv_##T##_ALshort(tmp[j]); \
1155 DECL_TEMPLATE(ALbyte)
1156 DECL_TEMPLATE(ALubyte)
1157 DECL_TEMPLATE(ALshort)
1158 DECL_TEMPLATE(ALushort)
1159 DECL_TEMPLATE(ALint)
1160 DECL_TEMPLATE(ALuint)
1161 DECL_TEMPLATE(ALfloat)
1162 DECL_TEMPLATE(ALdouble)
1164 #undef DECL_TEMPLATE
1166 #define DECL_TEMPLATE(T) \
1167 static void Convert_##T(T *dst, const ALvoid *src, enum SrcFmtType srcType, \
1168 ALsizei len) \
1170 switch(srcType) \
1172 case SrcFmtByte: \
1173 Convert_##T##_ALbyte(dst, src, len); \
1174 break; \
1175 case SrcFmtUByte: \
1176 Convert_##T##_ALubyte(dst, src, len); \
1177 break; \
1178 case SrcFmtShort: \
1179 Convert_##T##_ALshort(dst, src, len); \
1180 break; \
1181 case SrcFmtUShort: \
1182 Convert_##T##_ALushort(dst, src, len); \
1183 break; \
1184 case SrcFmtInt: \
1185 Convert_##T##_ALint(dst, src, len); \
1186 break; \
1187 case SrcFmtUInt: \
1188 Convert_##T##_ALuint(dst, src, len); \
1189 break; \
1190 case SrcFmtFloat: \
1191 Convert_##T##_ALfloat(dst, src, len); \
1192 break; \
1193 case SrcFmtDouble: \
1194 Convert_##T##_ALdouble(dst, src, len); \
1195 break; \
1196 case SrcFmtMulaw: \
1197 Convert_##T##_ALmulaw(dst, src, len); \
1198 break; \
1199 case SrcFmtIMA4: \
1200 break; /* not handled here */ \
1204 DECL_TEMPLATE(ALbyte)
1205 DECL_TEMPLATE(ALubyte)
1206 DECL_TEMPLATE(ALshort)
1207 DECL_TEMPLATE(ALushort)
1208 DECL_TEMPLATE(ALint)
1209 DECL_TEMPLATE(ALuint)
1210 DECL_TEMPLATE(ALfloat)
1211 DECL_TEMPLATE(ALdouble)
1213 #undef DECL_TEMPLATE
1216 static void ConvertData(ALvoid *dst, enum FmtType dstType, const ALvoid *src, enum SrcFmtType srcType, ALsizei len)
1218 switch(dstType)
1220 (void)Convert_ALbyte;
1221 case FmtUByte:
1222 Convert_ALubyte(dst, src, srcType, len);
1223 break;
1224 case FmtShort:
1225 Convert_ALshort(dst, src, srcType, len);
1226 break;
1227 (void)Convert_ALushort;
1228 (void)Convert_ALint;
1229 (void)Convert_ALuint;
1230 case FmtFloat:
1231 Convert_ALfloat(dst, src, srcType, len);
1232 break;
1233 (void)Convert_ALdouble;
1237 static void ConvertDataIMA4(ALvoid *dst, enum FmtType dstType, const ALvoid *src, ALint chans, ALsizei len)
1239 switch(dstType)
1241 (void)Convert_ALbyte_IMA4;
1242 case FmtUByte:
1243 Convert_ALubyte_IMA4(dst, src, chans, len);
1244 break;
1245 case FmtShort:
1246 Convert_ALshort_IMA4(dst, src, chans, len);
1247 break;
1248 (void)Convert_ALushort_IMA4;
1249 (void)Convert_ALint_IMA4;
1250 (void)Convert_ALuint_IMA4;
1251 case FmtFloat:
1252 Convert_ALfloat_IMA4(dst, src, chans, len);
1253 break;
1254 (void)Convert_ALdouble_IMA4;
1260 * LoadData
1262 * Loads the specified data into the buffer, using the specified formats.
1263 * Currently, the new format must have the same channel configuration as the
1264 * original format.
1266 static ALenum LoadData(ALbuffer *ALBuf, ALuint freq, ALenum NewFormat, ALsizei size, enum SrcFmtChannels SrcChannels, enum SrcFmtType SrcType, const ALvoid *data)
1268 ALuint NewChannels, NewBytes;
1269 enum FmtChannels DstChannels;
1270 enum FmtType DstType;
1271 ALuint64 newsize;
1272 ALvoid *temp;
1274 DecomposeFormat(NewFormat, &DstChannels, &DstType);
1275 NewChannels = ChannelsFromFmt(DstChannels);
1276 NewBytes = BytesFromFmt(DstType);
1278 assert(SrcChannels == DstChannels);
1280 if(SrcType == SrcFmtIMA4)
1282 ALuint OrigChannels = ChannelsFromSrcFmt(SrcChannels);
1284 /* Here is where things vary:
1285 * nVidia and Apple use 64+1 sample frames per block => block_size=36*chans bytes
1286 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024*chans bytes
1288 if((size%(36*OrigChannels)) != 0)
1289 return AL_INVALID_VALUE;
1291 newsize = size / 36;
1292 newsize *= 65;
1293 newsize *= NewBytes;
1294 if(newsize > INT_MAX)
1295 return AL_OUT_OF_MEMORY;
1297 temp = realloc(ALBuf->data, newsize);
1298 if(!temp) return AL_OUT_OF_MEMORY;
1299 ALBuf->data = temp;
1300 ALBuf->size = newsize;
1302 if(data != NULL)
1303 ConvertDataIMA4(ALBuf->data, DstType, data, OrigChannels,
1304 newsize/(65*NewChannels*NewBytes));
1306 ALBuf->OriginalChannels = SrcChannels;
1307 ALBuf->OriginalType = SrcType;
1308 ALBuf->OriginalSize = size;
1309 ALBuf->OriginalAlign = 36 * OrigChannels;
1311 else
1313 ALuint OrigBytes = BytesFromSrcFmt(SrcType);
1314 ALuint OrigChannels = ChannelsFromSrcFmt(SrcChannels);
1316 if((size%(OrigBytes*OrigChannels)) != 0)
1317 return AL_INVALID_VALUE;
1319 newsize = size / OrigBytes;
1320 newsize *= NewBytes;
1321 if(newsize > INT_MAX)
1322 return AL_OUT_OF_MEMORY;
1324 temp = realloc(ALBuf->data, newsize);
1325 if(!temp) return AL_OUT_OF_MEMORY;
1326 ALBuf->data = temp;
1327 ALBuf->size = newsize;
1329 if(data != NULL)
1330 ConvertData(ALBuf->data, DstType, data, SrcType, newsize/NewBytes);
1332 ALBuf->OriginalChannels = SrcChannels;
1333 ALBuf->OriginalType = SrcType;
1334 ALBuf->OriginalSize = size;
1335 ALBuf->OriginalAlign = OrigBytes * OrigChannels;
1338 ALBuf->Frequency = freq;
1339 ALBuf->FmtChannels = DstChannels;
1340 ALBuf->FmtType = DstType;
1342 ALBuf->LoopStart = 0;
1343 ALBuf->LoopEnd = newsize / NewChannels / NewBytes;
1345 return AL_NO_ERROR;
1349 ALuint BytesFromSrcFmt(enum SrcFmtType type)
1351 switch(type)
1353 case SrcFmtByte: return sizeof(ALbyte);
1354 case SrcFmtUByte: return sizeof(ALubyte);
1355 case SrcFmtShort: return sizeof(ALshort);
1356 case SrcFmtUShort: return sizeof(ALushort);
1357 case SrcFmtInt: return sizeof(ALint);
1358 case SrcFmtUInt: return sizeof(ALuint);
1359 case SrcFmtFloat: return sizeof(ALfloat);
1360 case SrcFmtDouble: return sizeof(ALdouble);
1361 case SrcFmtMulaw: return sizeof(ALubyte);
1362 case SrcFmtIMA4: break; /* not handled here */
1364 return 0;
1366 ALuint ChannelsFromSrcFmt(enum SrcFmtChannels chans)
1368 switch(chans)
1370 case SrcFmtMono: return 1;
1371 case SrcFmtStereo: return 2;
1372 case SrcFmtRear: return 2;
1373 case SrcFmtQuad: return 4;
1374 case SrcFmtX51: return 6;
1375 case SrcFmtX61: return 7;
1376 case SrcFmtX71: return 8;
1378 return 0;
1380 ALboolean DecomposeInputFormat(ALenum format, enum SrcFmtChannels *chans,
1381 enum SrcFmtType *type)
1383 switch(format)
1385 case AL_FORMAT_MONO8:
1386 *chans = SrcFmtMono;
1387 *type = SrcFmtUByte;
1388 return AL_TRUE;
1389 case AL_FORMAT_MONO16:
1390 *chans = SrcFmtMono;
1391 *type = SrcFmtShort;
1392 return AL_TRUE;
1393 case AL_FORMAT_MONO_FLOAT32:
1394 *chans = SrcFmtMono;
1395 *type = SrcFmtFloat;
1396 return AL_TRUE;
1397 case AL_FORMAT_MONO_DOUBLE_EXT:
1398 *chans = SrcFmtMono;
1399 *type = SrcFmtDouble;
1400 return AL_TRUE;
1401 case AL_FORMAT_MONO_IMA4:
1402 *chans = SrcFmtMono;
1403 *type = SrcFmtIMA4;
1404 return AL_TRUE;
1405 case AL_FORMAT_STEREO8:
1406 *chans = SrcFmtStereo;
1407 *type = SrcFmtUByte;
1408 return AL_TRUE;
1409 case AL_FORMAT_STEREO16:
1410 *chans = SrcFmtStereo;
1411 *type = SrcFmtShort;
1412 return AL_TRUE;
1413 case AL_FORMAT_STEREO_FLOAT32:
1414 *chans = SrcFmtStereo;
1415 *type = SrcFmtFloat;
1416 return AL_TRUE;
1417 case AL_FORMAT_STEREO_DOUBLE_EXT:
1418 *chans = SrcFmtStereo;
1419 *type = SrcFmtDouble;
1420 return AL_TRUE;
1421 case AL_FORMAT_STEREO_IMA4:
1422 *chans = SrcFmtStereo;
1423 *type = SrcFmtIMA4;
1424 return AL_TRUE;
1425 case AL_FORMAT_QUAD8_LOKI:
1426 case AL_FORMAT_QUAD8:
1427 *chans = SrcFmtQuad;
1428 *type = SrcFmtUByte;
1429 return AL_TRUE;
1430 case AL_FORMAT_QUAD16_LOKI:
1431 case AL_FORMAT_QUAD16:
1432 *chans = SrcFmtQuad;
1433 *type = SrcFmtShort;
1434 return AL_TRUE;
1435 case AL_FORMAT_QUAD32:
1436 *chans = SrcFmtQuad;
1437 *type = SrcFmtFloat;
1438 return AL_TRUE;
1439 case AL_FORMAT_REAR8:
1440 *chans = SrcFmtRear;
1441 *type = SrcFmtUByte;
1442 return AL_TRUE;
1443 case AL_FORMAT_REAR16:
1444 *chans = SrcFmtRear;
1445 *type = SrcFmtShort;
1446 return AL_TRUE;
1447 case AL_FORMAT_REAR32:
1448 *chans = SrcFmtRear;
1449 *type = SrcFmtFloat;
1450 return AL_TRUE;
1451 case AL_FORMAT_51CHN8:
1452 *chans = SrcFmtX51;
1453 *type = SrcFmtUByte;
1454 return AL_TRUE;
1455 case AL_FORMAT_51CHN16:
1456 *chans = SrcFmtX51;
1457 *type = SrcFmtShort;
1458 return AL_TRUE;
1459 case AL_FORMAT_51CHN32:
1460 *chans = SrcFmtX51;
1461 *type = SrcFmtFloat;
1462 return AL_TRUE;
1463 case AL_FORMAT_61CHN8:
1464 *chans = SrcFmtX61;
1465 *type = SrcFmtUByte;
1466 return AL_TRUE;
1467 case AL_FORMAT_61CHN16:
1468 *chans = SrcFmtX61;
1469 *type = SrcFmtShort;
1470 return AL_TRUE;
1471 case AL_FORMAT_61CHN32:
1472 *chans = SrcFmtX61;
1473 *type = SrcFmtFloat;
1474 return AL_TRUE;
1475 case AL_FORMAT_71CHN8:
1476 *chans = SrcFmtX71;
1477 *type = SrcFmtUByte;
1478 return AL_TRUE;
1479 case AL_FORMAT_71CHN16:
1480 *chans = SrcFmtX71;
1481 *type = SrcFmtShort;
1482 return AL_TRUE;
1483 case AL_FORMAT_71CHN32:
1484 *chans = SrcFmtX71;
1485 *type = SrcFmtFloat;
1486 return AL_TRUE;
1487 case AL_FORMAT_MONO_MULAW:
1488 *chans = SrcFmtMono;
1489 *type = SrcFmtMulaw;
1490 return AL_TRUE;
1491 case AL_FORMAT_STEREO_MULAW:
1492 *chans = SrcFmtStereo;
1493 *type = SrcFmtMulaw;
1494 return AL_TRUE;
1495 case AL_FORMAT_QUAD_MULAW:
1496 *chans = SrcFmtQuad;
1497 *type = SrcFmtMulaw;
1498 return AL_TRUE;
1499 case AL_FORMAT_REAR_MULAW:
1500 *chans = SrcFmtRear;
1501 *type = SrcFmtMulaw;
1502 return AL_TRUE;
1503 case AL_FORMAT_51CHN_MULAW:
1504 *chans = SrcFmtX51;
1505 *type = SrcFmtMulaw;
1506 return AL_TRUE;
1507 case AL_FORMAT_61CHN_MULAW:
1508 *chans = SrcFmtX61;
1509 *type = SrcFmtMulaw;
1510 return AL_TRUE;
1511 case AL_FORMAT_71CHN_MULAW:
1512 *chans = SrcFmtX71;
1513 *type = SrcFmtMulaw;
1514 return AL_TRUE;
1516 return AL_FALSE;
1519 ALuint BytesFromFmt(enum FmtType type)
1521 switch(type)
1523 case FmtUByte: return sizeof(ALubyte);
1524 case FmtShort: return sizeof(ALshort);
1525 case FmtFloat: return sizeof(ALfloat);
1527 return 0;
1529 ALuint ChannelsFromFmt(enum FmtChannels chans)
1531 switch(chans)
1533 case FmtMono: return 1;
1534 case FmtStereo: return 2;
1535 case FmtRear: return 2;
1536 case FmtQuad: return 4;
1537 case FmtX51: return 6;
1538 case FmtX61: return 7;
1539 case FmtX71: return 8;
1541 return 0;
1543 ALboolean DecomposeFormat(ALenum format, enum FmtChannels *chans, enum FmtType *type)
1545 switch(format)
1547 case AL_FORMAT_MONO8:
1548 *chans = FmtMono;
1549 *type = FmtUByte;
1550 return AL_TRUE;
1551 case AL_FORMAT_MONO16:
1552 *chans = FmtMono;
1553 *type = FmtShort;
1554 return AL_TRUE;
1555 case AL_FORMAT_MONO_FLOAT32:
1556 *chans = FmtMono;
1557 *type = FmtFloat;
1558 return AL_TRUE;
1559 case AL_FORMAT_STEREO8:
1560 *chans = FmtStereo;
1561 *type = FmtUByte;
1562 return AL_TRUE;
1563 case AL_FORMAT_STEREO16:
1564 *chans = FmtStereo;
1565 *type = FmtShort;
1566 return AL_TRUE;
1567 case AL_FORMAT_STEREO_FLOAT32:
1568 *chans = FmtStereo;
1569 *type = FmtFloat;
1570 return AL_TRUE;
1571 case AL_FORMAT_QUAD8_LOKI:
1572 case AL_FORMAT_QUAD8:
1573 *chans = FmtQuad;
1574 *type = FmtUByte;
1575 return AL_TRUE;
1576 case AL_FORMAT_QUAD16_LOKI:
1577 case AL_FORMAT_QUAD16:
1578 *chans = FmtQuad;
1579 *type = FmtShort;
1580 return AL_TRUE;
1581 case AL_FORMAT_QUAD32:
1582 *chans = FmtQuad;
1583 *type = FmtFloat;
1584 return AL_TRUE;
1585 case AL_FORMAT_REAR8:
1586 *chans = FmtRear;
1587 *type = FmtUByte;
1588 return AL_TRUE;
1589 case AL_FORMAT_REAR16:
1590 *chans = FmtRear;
1591 *type = FmtShort;
1592 return AL_TRUE;
1593 case AL_FORMAT_REAR32:
1594 *chans = FmtRear;
1595 *type = FmtFloat;
1596 return AL_TRUE;
1597 case AL_FORMAT_51CHN8:
1598 *chans = FmtX51;
1599 *type = FmtUByte;
1600 return AL_TRUE;
1601 case AL_FORMAT_51CHN16:
1602 *chans = FmtX51;
1603 *type = FmtShort;
1604 return AL_TRUE;
1605 case AL_FORMAT_51CHN32:
1606 *chans = FmtX51;
1607 *type = FmtFloat;
1608 return AL_TRUE;
1609 case AL_FORMAT_61CHN8:
1610 *chans = FmtX61;
1611 *type = FmtUByte;
1612 return AL_TRUE;
1613 case AL_FORMAT_61CHN16:
1614 *chans = FmtX61;
1615 *type = FmtShort;
1616 return AL_TRUE;
1617 case AL_FORMAT_61CHN32:
1618 *chans = FmtX61;
1619 *type = FmtFloat;
1620 return AL_TRUE;
1621 case AL_FORMAT_71CHN8:
1622 *chans = FmtX71;
1623 *type = FmtUByte;
1624 return AL_TRUE;
1625 case AL_FORMAT_71CHN16:
1626 *chans = FmtX71;
1627 *type = FmtShort;
1628 return AL_TRUE;
1629 case AL_FORMAT_71CHN32:
1630 *chans = FmtX71;
1631 *type = FmtFloat;
1632 return AL_TRUE;
1634 return AL_FALSE;
1639 * ReleaseALBuffers()
1641 * INTERNAL FN : Called by alcCloseDevice to destroy any buffers that still exist
1643 ALvoid ReleaseALBuffers(ALCdevice *device)
1645 ALsizei i;
1646 for(i = 0;i < device->BufferMap.size;i++)
1648 ALbuffer *temp = device->BufferMap.array[i].value;
1649 device->BufferMap.array[i].value = NULL;
1651 // Release sample data
1652 free(temp->data);
1654 // Release Buffer structure
1655 ALTHUNK_REMOVEENTRY(temp->buffer);
1656 memset(temp, 0, sizeof(ALbuffer));
1657 free(temp);