Fix up some conversions
[openal-soft.git] / OpenAL32 / alBuffer.c
blobf279ac2802e83b502622f86508afdf3f308949a4
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-128; }
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+128; }
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-32768; }
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+32768; }
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-2147483648u; }
1104 static __inline ALint Conv_ALint_ALfloat(ALfloat val)
1106 if(val > 1.0f) return 2147483647;
1107 if(val < -1.0f) return 0u-2147483648u;
1108 return (ALint)(val * 2147483647.0);
1110 static __inline ALint Conv_ALint_ALdouble(ALdouble val)
1112 if(val > 1.0) return 2147483647;
1113 if(val < -1.0) return 0u-2147483648u;
1114 return (ALint)(val * 2147483647.0);
1116 static __inline ALint Conv_ALint_ALmulaw(ALmulaw val)
1117 { return Conv_ALint_ALshort(DecodeMuLaw(val)); }
1119 static __inline ALuint Conv_ALuint_ALbyte(ALbyte val)
1120 { return (val+128)<<24; }
1121 static __inline ALuint Conv_ALuint_ALubyte(ALubyte val)
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+2147483648u; }
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 static __inline ALmulaw Conv_ALmulaw_ALmulaw(ALmulaw val)
1197 { return val; }
1199 #undef DECL_TEMPLATE
1201 #define DECL_TEMPLATE(T1, T2) \
1202 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint len) \
1204 ALuint i; \
1205 for(i = 0;i < len;i++) \
1206 *(dst++) = Conv_##T1##_##T2(*(src++)); \
1209 DECL_TEMPLATE(ALbyte, ALbyte)
1210 DECL_TEMPLATE(ALbyte, ALubyte)
1211 DECL_TEMPLATE(ALbyte, ALshort)
1212 DECL_TEMPLATE(ALbyte, ALushort)
1213 DECL_TEMPLATE(ALbyte, ALint)
1214 DECL_TEMPLATE(ALbyte, ALuint)
1215 DECL_TEMPLATE(ALbyte, ALfloat)
1216 DECL_TEMPLATE(ALbyte, ALdouble)
1217 DECL_TEMPLATE(ALbyte, ALmulaw)
1219 DECL_TEMPLATE(ALubyte, ALbyte)
1220 DECL_TEMPLATE(ALubyte, ALubyte)
1221 DECL_TEMPLATE(ALubyte, ALshort)
1222 DECL_TEMPLATE(ALubyte, ALushort)
1223 DECL_TEMPLATE(ALubyte, ALint)
1224 DECL_TEMPLATE(ALubyte, ALuint)
1225 DECL_TEMPLATE(ALubyte, ALfloat)
1226 DECL_TEMPLATE(ALubyte, ALdouble)
1227 DECL_TEMPLATE(ALubyte, ALmulaw)
1229 DECL_TEMPLATE(ALshort, ALbyte)
1230 DECL_TEMPLATE(ALshort, ALubyte)
1231 DECL_TEMPLATE(ALshort, ALshort)
1232 DECL_TEMPLATE(ALshort, ALushort)
1233 DECL_TEMPLATE(ALshort, ALint)
1234 DECL_TEMPLATE(ALshort, ALuint)
1235 DECL_TEMPLATE(ALshort, ALfloat)
1236 DECL_TEMPLATE(ALshort, ALdouble)
1237 DECL_TEMPLATE(ALshort, ALmulaw)
1239 DECL_TEMPLATE(ALushort, ALbyte)
1240 DECL_TEMPLATE(ALushort, ALubyte)
1241 DECL_TEMPLATE(ALushort, ALshort)
1242 DECL_TEMPLATE(ALushort, ALushort)
1243 DECL_TEMPLATE(ALushort, ALint)
1244 DECL_TEMPLATE(ALushort, ALuint)
1245 DECL_TEMPLATE(ALushort, ALfloat)
1246 DECL_TEMPLATE(ALushort, ALdouble)
1247 DECL_TEMPLATE(ALushort, ALmulaw)
1249 DECL_TEMPLATE(ALint, ALbyte)
1250 DECL_TEMPLATE(ALint, ALubyte)
1251 DECL_TEMPLATE(ALint, ALshort)
1252 DECL_TEMPLATE(ALint, ALushort)
1253 DECL_TEMPLATE(ALint, ALint)
1254 DECL_TEMPLATE(ALint, ALuint)
1255 DECL_TEMPLATE(ALint, ALfloat)
1256 DECL_TEMPLATE(ALint, ALdouble)
1257 DECL_TEMPLATE(ALint, ALmulaw)
1259 DECL_TEMPLATE(ALuint, ALbyte)
1260 DECL_TEMPLATE(ALuint, ALubyte)
1261 DECL_TEMPLATE(ALuint, ALshort)
1262 DECL_TEMPLATE(ALuint, ALushort)
1263 DECL_TEMPLATE(ALuint, ALint)
1264 DECL_TEMPLATE(ALuint, ALuint)
1265 DECL_TEMPLATE(ALuint, ALfloat)
1266 DECL_TEMPLATE(ALuint, ALdouble)
1267 DECL_TEMPLATE(ALuint, ALmulaw)
1269 DECL_TEMPLATE(ALfloat, ALbyte)
1270 DECL_TEMPLATE(ALfloat, ALubyte)
1271 DECL_TEMPLATE(ALfloat, ALshort)
1272 DECL_TEMPLATE(ALfloat, ALushort)
1273 DECL_TEMPLATE(ALfloat, ALint)
1274 DECL_TEMPLATE(ALfloat, ALuint)
1275 DECL_TEMPLATE(ALfloat, ALfloat)
1276 DECL_TEMPLATE(ALfloat, ALdouble)
1277 DECL_TEMPLATE(ALfloat, ALmulaw)
1279 DECL_TEMPLATE(ALdouble, ALbyte)
1280 DECL_TEMPLATE(ALdouble, ALubyte)
1281 DECL_TEMPLATE(ALdouble, ALshort)
1282 DECL_TEMPLATE(ALdouble, ALushort)
1283 DECL_TEMPLATE(ALdouble, ALint)
1284 DECL_TEMPLATE(ALdouble, ALuint)
1285 DECL_TEMPLATE(ALdouble, ALfloat)
1286 DECL_TEMPLATE(ALdouble, ALdouble)
1287 DECL_TEMPLATE(ALdouble, ALmulaw)
1289 DECL_TEMPLATE(ALmulaw, ALbyte)
1290 DECL_TEMPLATE(ALmulaw, ALubyte)
1291 DECL_TEMPLATE(ALmulaw, ALshort)
1292 DECL_TEMPLATE(ALmulaw, ALushort)
1293 DECL_TEMPLATE(ALmulaw, ALint)
1294 DECL_TEMPLATE(ALmulaw, ALuint)
1295 DECL_TEMPLATE(ALmulaw, ALfloat)
1296 DECL_TEMPLATE(ALmulaw, ALdouble)
1297 DECL_TEMPLATE(ALmulaw, ALmulaw)
1299 #undef DECL_TEMPLATE
1301 #define DECL_TEMPLATE(T) \
1302 static void Convert_##T##_IMA4(T *dst, const ALubyte *src, ALuint numchans, \
1303 ALuint numblocks) \
1305 ALuint i, j; \
1306 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
1307 for(i = 0;i < numblocks;i++) \
1309 DecodeIMA4Block(tmp, src, numchans); \
1310 src += 36*numchans; \
1311 for(j = 0;j < 65*numchans;j++) \
1312 *(dst++) = Conv_##T##_ALshort(tmp[j]); \
1316 DECL_TEMPLATE(ALbyte)
1317 DECL_TEMPLATE(ALubyte)
1318 DECL_TEMPLATE(ALshort)
1319 DECL_TEMPLATE(ALushort)
1320 DECL_TEMPLATE(ALint)
1321 DECL_TEMPLATE(ALuint)
1322 DECL_TEMPLATE(ALfloat)
1323 DECL_TEMPLATE(ALdouble)
1324 DECL_TEMPLATE(ALmulaw)
1326 #undef DECL_TEMPLATE
1328 #define DECL_TEMPLATE(T) \
1329 static void Convert_IMA4_##T(ALubyte *dst, const T *src, ALuint numchans, \
1330 ALuint numblocks) \
1332 ALuint i, j; \
1333 ALshort tmp[65*MAXCHANNELS]; /* Max samples an IMA4 frame can be */ \
1334 ALint sample[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
1335 ALint index[MAXCHANNELS] = {0,0,0,0,0,0,0,0}; \
1336 for(i = 0;i < numblocks;i++) \
1338 for(j = 0;j < 65*numchans;j++) \
1339 tmp[j] = Conv_ALshort_##T(*(src++)); \
1340 EncodeIMA4Block(dst, tmp, sample, index, numchans); \
1341 dst += 36*numchans; \
1345 DECL_TEMPLATE(ALbyte)
1346 DECL_TEMPLATE(ALubyte)
1347 DECL_TEMPLATE(ALshort)
1348 DECL_TEMPLATE(ALushort)
1349 DECL_TEMPLATE(ALint)
1350 DECL_TEMPLATE(ALuint)
1351 DECL_TEMPLATE(ALfloat)
1352 DECL_TEMPLATE(ALdouble)
1353 DECL_TEMPLATE(ALmulaw)
1355 #undef DECL_TEMPLATE
1357 static void Convert_IMA4_IMA4(ALubyte *dst, const ALubyte *src, ALuint numchans,
1358 ALuint numblocks)
1360 memcpy(dst, src, numblocks*36*numchans);
1363 #define DECL_TEMPLATE(T) \
1364 static void Convert_##T(T *dst, const ALvoid *src, enum UserFmtType srcType, \
1365 ALsizei len) \
1367 switch(srcType) \
1369 case UserFmtByte: \
1370 Convert_##T##_ALbyte(dst, src, len); \
1371 break; \
1372 case UserFmtUByte: \
1373 Convert_##T##_ALubyte(dst, src, len); \
1374 break; \
1375 case UserFmtShort: \
1376 Convert_##T##_ALshort(dst, src, len); \
1377 break; \
1378 case UserFmtUShort: \
1379 Convert_##T##_ALushort(dst, src, len); \
1380 break; \
1381 case UserFmtInt: \
1382 Convert_##T##_ALint(dst, src, len); \
1383 break; \
1384 case UserFmtUInt: \
1385 Convert_##T##_ALuint(dst, src, len); \
1386 break; \
1387 case UserFmtFloat: \
1388 Convert_##T##_ALfloat(dst, src, len); \
1389 break; \
1390 case UserFmtDouble: \
1391 Convert_##T##_ALdouble(dst, src, len); \
1392 break; \
1393 case UserFmtMulaw: \
1394 Convert_##T##_ALmulaw(dst, src, len); \
1395 break; \
1396 case UserFmtIMA4: \
1397 break; /* not handled here */ \
1401 DECL_TEMPLATE(ALbyte)
1402 DECL_TEMPLATE(ALubyte)
1403 DECL_TEMPLATE(ALshort)
1404 DECL_TEMPLATE(ALushort)
1405 DECL_TEMPLATE(ALint)
1406 DECL_TEMPLATE(ALuint)
1407 DECL_TEMPLATE(ALfloat)
1408 DECL_TEMPLATE(ALdouble)
1409 DECL_TEMPLATE(ALmulaw)
1411 #undef DECL_TEMPLATE
1413 static void Convert_IMA4(ALubyte *dst, const ALvoid *src, enum UserFmtType srcType,
1414 ALint chans, ALsizei len)
1416 switch(srcType)
1418 case UserFmtByte:
1419 Convert_IMA4_ALbyte(dst, src, chans, len);
1420 break;
1421 case UserFmtUByte:
1422 Convert_IMA4_ALubyte(dst, src, chans, len);
1423 break;
1424 case UserFmtShort:
1425 Convert_IMA4_ALshort(dst, src, chans, len);
1426 break;
1427 case UserFmtUShort:
1428 Convert_IMA4_ALushort(dst, src, chans, len);
1429 break;
1430 case UserFmtInt:
1431 Convert_IMA4_ALint(dst, src, chans, len);
1432 break;
1433 case UserFmtUInt:
1434 Convert_IMA4_ALuint(dst, src, chans, len);
1435 break;
1436 case UserFmtFloat:
1437 Convert_IMA4_ALfloat(dst, src, chans, len);
1438 break;
1439 case UserFmtDouble:
1440 Convert_IMA4_ALdouble(dst, src, chans, len);
1441 break;
1442 case UserFmtMulaw:
1443 Convert_IMA4_ALmulaw(dst, src, chans, len);
1444 break;
1445 case UserFmtIMA4:
1446 Convert_IMA4_IMA4(dst, src, chans, len);
1447 break;
1452 static void ConvertData(ALvoid *dst, enum FmtType dstType, const ALvoid *src, enum UserFmtType srcType, ALsizei len)
1454 switch(dstType)
1456 (void)Convert_ALbyte;
1457 case FmtUByte:
1458 Convert_ALubyte(dst, src, srcType, len);
1459 break;
1460 case FmtShort:
1461 Convert_ALshort(dst, src, srcType, len);
1462 break;
1463 (void)Convert_ALushort;
1464 (void)Convert_ALint;
1465 (void)Convert_ALuint;
1466 case FmtFloat:
1467 Convert_ALfloat(dst, src, srcType, len);
1468 break;
1469 (void)Convert_ALdouble;
1470 (void)Convert_ALmulaw;
1471 (void)Convert_IMA4;
1475 static void ConvertDataIMA4(ALvoid *dst, enum FmtType dstType, const ALvoid *src, ALint chans, ALsizei len)
1477 switch(dstType)
1479 (void)Convert_ALbyte_IMA4;
1480 case FmtUByte:
1481 Convert_ALubyte_IMA4(dst, src, chans, len);
1482 break;
1483 case FmtShort:
1484 Convert_ALshort_IMA4(dst, src, chans, len);
1485 break;
1486 (void)Convert_ALushort_IMA4;
1487 (void)Convert_ALint_IMA4;
1488 (void)Convert_ALuint_IMA4;
1489 case FmtFloat:
1490 Convert_ALfloat_IMA4(dst, src, chans, len);
1491 break;
1492 (void)Convert_ALdouble_IMA4;
1493 (void)Convert_ALmulaw_IMA4;
1499 * LoadData
1501 * Loads the specified data into the buffer, using the specified formats.
1502 * Currently, the new format must have the same channel configuration as the
1503 * original format.
1505 static ALenum LoadData(ALbuffer *ALBuf, ALuint freq, ALenum NewFormat, ALsizei size, enum UserFmtChannels SrcChannels, enum UserFmtType SrcType, const ALvoid *data)
1507 ALuint NewChannels, NewBytes;
1508 enum FmtChannels DstChannels;
1509 enum FmtType DstType;
1510 ALuint64 newsize;
1511 ALvoid *temp;
1513 DecomposeFormat(NewFormat, &DstChannels, &DstType);
1514 NewChannels = ChannelsFromFmt(DstChannels);
1515 NewBytes = BytesFromFmt(DstType);
1517 assert(SrcChannels == DstChannels);
1519 if(SrcType == UserFmtIMA4)
1521 ALuint OrigChannels = ChannelsFromUserFmt(SrcChannels);
1523 /* Here is where things vary:
1524 * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
1525 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
1527 if((size%(36*OrigChannels)) != 0)
1528 return AL_INVALID_VALUE;
1530 newsize = size / 36;
1531 newsize *= 65;
1532 newsize *= NewBytes;
1533 if(newsize > INT_MAX)
1534 return AL_OUT_OF_MEMORY;
1536 temp = realloc(ALBuf->data, newsize);
1537 if(!temp && newsize) return AL_OUT_OF_MEMORY;
1538 ALBuf->data = temp;
1539 ALBuf->size = newsize;
1541 if(data != NULL)
1542 ConvertDataIMA4(ALBuf->data, DstType, data, OrigChannels,
1543 newsize/(65*NewChannels*NewBytes));
1545 ALBuf->OriginalChannels = SrcChannels;
1546 ALBuf->OriginalType = SrcType;
1547 ALBuf->OriginalSize = size;
1548 ALBuf->OriginalAlign = 36 * OrigChannels;
1550 else
1552 ALuint OrigBytes = BytesFromUserFmt(SrcType);
1553 ALuint OrigChannels = ChannelsFromUserFmt(SrcChannels);
1555 if((size%(OrigBytes*OrigChannels)) != 0)
1556 return AL_INVALID_VALUE;
1558 newsize = size / OrigBytes;
1559 newsize *= NewBytes;
1560 if(newsize > INT_MAX)
1561 return AL_OUT_OF_MEMORY;
1563 temp = realloc(ALBuf->data, newsize);
1564 if(!temp && newsize) return AL_OUT_OF_MEMORY;
1565 ALBuf->data = temp;
1566 ALBuf->size = newsize;
1568 if(data != NULL)
1569 ConvertData(ALBuf->data, DstType, data, SrcType, newsize/NewBytes);
1571 ALBuf->OriginalChannels = SrcChannels;
1572 ALBuf->OriginalType = SrcType;
1573 ALBuf->OriginalSize = size;
1574 ALBuf->OriginalAlign = OrigBytes * OrigChannels;
1577 ALBuf->Frequency = freq;
1578 ALBuf->FmtChannels = DstChannels;
1579 ALBuf->FmtType = DstType;
1581 ALBuf->LoopStart = 0;
1582 ALBuf->LoopEnd = newsize / NewChannels / NewBytes;
1584 return AL_NO_ERROR;
1588 ALuint BytesFromUserFmt(enum UserFmtType type)
1590 switch(type)
1592 case UserFmtByte: return sizeof(ALbyte);
1593 case UserFmtUByte: return sizeof(ALubyte);
1594 case UserFmtShort: return sizeof(ALshort);
1595 case UserFmtUShort: return sizeof(ALushort);
1596 case UserFmtInt: return sizeof(ALint);
1597 case UserFmtUInt: return sizeof(ALuint);
1598 case UserFmtFloat: return sizeof(ALfloat);
1599 case UserFmtDouble: return sizeof(ALdouble);
1600 case UserFmtMulaw: return sizeof(ALubyte);
1601 case UserFmtIMA4: break; /* not handled here */
1603 return 0;
1605 ALuint ChannelsFromUserFmt(enum UserFmtChannels chans)
1607 switch(chans)
1609 case UserFmtMono: return 1;
1610 case UserFmtStereo: return 2;
1611 case UserFmtRear: return 2;
1612 case UserFmtQuad: return 4;
1613 case UserFmtX51: return 6;
1614 case UserFmtX61: return 7;
1615 case UserFmtX71: return 8;
1617 return 0;
1619 ALboolean DecomposeUserFormat(ALenum format, enum UserFmtChannels *chans,
1620 enum UserFmtType *type)
1622 switch(format)
1624 case AL_FORMAT_MONO8:
1625 *chans = UserFmtMono;
1626 *type = UserFmtUByte;
1627 return AL_TRUE;
1628 case AL_FORMAT_MONO16:
1629 *chans = UserFmtMono;
1630 *type = UserFmtShort;
1631 return AL_TRUE;
1632 case AL_FORMAT_MONO_FLOAT32:
1633 *chans = UserFmtMono;
1634 *type = UserFmtFloat;
1635 return AL_TRUE;
1636 case AL_FORMAT_MONO_DOUBLE_EXT:
1637 *chans = UserFmtMono;
1638 *type = UserFmtDouble;
1639 return AL_TRUE;
1640 case AL_FORMAT_MONO_IMA4:
1641 *chans = UserFmtMono;
1642 *type = UserFmtIMA4;
1643 return AL_TRUE;
1644 case AL_FORMAT_STEREO8:
1645 *chans = UserFmtStereo;
1646 *type = UserFmtUByte;
1647 return AL_TRUE;
1648 case AL_FORMAT_STEREO16:
1649 *chans = UserFmtStereo;
1650 *type = UserFmtShort;
1651 return AL_TRUE;
1652 case AL_FORMAT_STEREO_FLOAT32:
1653 *chans = UserFmtStereo;
1654 *type = UserFmtFloat;
1655 return AL_TRUE;
1656 case AL_FORMAT_STEREO_DOUBLE_EXT:
1657 *chans = UserFmtStereo;
1658 *type = UserFmtDouble;
1659 return AL_TRUE;
1660 case AL_FORMAT_STEREO_IMA4:
1661 *chans = UserFmtStereo;
1662 *type = UserFmtIMA4;
1663 return AL_TRUE;
1664 case AL_FORMAT_QUAD8_LOKI:
1665 case AL_FORMAT_QUAD8:
1666 *chans = UserFmtQuad;
1667 *type = UserFmtUByte;
1668 return AL_TRUE;
1669 case AL_FORMAT_QUAD16_LOKI:
1670 case AL_FORMAT_QUAD16:
1671 *chans = UserFmtQuad;
1672 *type = UserFmtShort;
1673 return AL_TRUE;
1674 case AL_FORMAT_QUAD32:
1675 *chans = UserFmtQuad;
1676 *type = UserFmtFloat;
1677 return AL_TRUE;
1678 case AL_FORMAT_REAR8:
1679 *chans = UserFmtRear;
1680 *type = UserFmtUByte;
1681 return AL_TRUE;
1682 case AL_FORMAT_REAR16:
1683 *chans = UserFmtRear;
1684 *type = UserFmtShort;
1685 return AL_TRUE;
1686 case AL_FORMAT_REAR32:
1687 *chans = UserFmtRear;
1688 *type = UserFmtFloat;
1689 return AL_TRUE;
1690 case AL_FORMAT_51CHN8:
1691 *chans = UserFmtX51;
1692 *type = UserFmtUByte;
1693 return AL_TRUE;
1694 case AL_FORMAT_51CHN16:
1695 *chans = UserFmtX51;
1696 *type = UserFmtShort;
1697 return AL_TRUE;
1698 case AL_FORMAT_51CHN32:
1699 *chans = UserFmtX51;
1700 *type = UserFmtFloat;
1701 return AL_TRUE;
1702 case AL_FORMAT_61CHN8:
1703 *chans = UserFmtX61;
1704 *type = UserFmtUByte;
1705 return AL_TRUE;
1706 case AL_FORMAT_61CHN16:
1707 *chans = UserFmtX61;
1708 *type = UserFmtShort;
1709 return AL_TRUE;
1710 case AL_FORMAT_61CHN32:
1711 *chans = UserFmtX61;
1712 *type = UserFmtFloat;
1713 return AL_TRUE;
1714 case AL_FORMAT_71CHN8:
1715 *chans = UserFmtX71;
1716 *type = UserFmtUByte;
1717 return AL_TRUE;
1718 case AL_FORMAT_71CHN16:
1719 *chans = UserFmtX71;
1720 *type = UserFmtShort;
1721 return AL_TRUE;
1722 case AL_FORMAT_71CHN32:
1723 *chans = UserFmtX71;
1724 *type = UserFmtFloat;
1725 return AL_TRUE;
1726 case AL_FORMAT_MONO_MULAW:
1727 *chans = UserFmtMono;
1728 *type = UserFmtMulaw;
1729 return AL_TRUE;
1730 case AL_FORMAT_STEREO_MULAW:
1731 *chans = UserFmtStereo;
1732 *type = UserFmtMulaw;
1733 return AL_TRUE;
1734 case AL_FORMAT_QUAD_MULAW:
1735 *chans = UserFmtQuad;
1736 *type = UserFmtMulaw;
1737 return AL_TRUE;
1738 case AL_FORMAT_REAR_MULAW:
1739 *chans = UserFmtRear;
1740 *type = UserFmtMulaw;
1741 return AL_TRUE;
1742 case AL_FORMAT_51CHN_MULAW:
1743 *chans = UserFmtX51;
1744 *type = UserFmtMulaw;
1745 return AL_TRUE;
1746 case AL_FORMAT_61CHN_MULAW:
1747 *chans = UserFmtX61;
1748 *type = UserFmtMulaw;
1749 return AL_TRUE;
1750 case AL_FORMAT_71CHN_MULAW:
1751 *chans = UserFmtX71;
1752 *type = UserFmtMulaw;
1753 return AL_TRUE;
1755 return AL_FALSE;
1758 ALuint BytesFromFmt(enum FmtType type)
1760 switch(type)
1762 case FmtUByte: return sizeof(ALubyte);
1763 case FmtShort: return sizeof(ALshort);
1764 case FmtFloat: return sizeof(ALfloat);
1766 return 0;
1768 ALuint ChannelsFromFmt(enum FmtChannels chans)
1770 switch(chans)
1772 case FmtMono: return 1;
1773 case FmtStereo: return 2;
1774 case FmtRear: return 2;
1775 case FmtQuad: return 4;
1776 case FmtX51: return 6;
1777 case FmtX61: return 7;
1778 case FmtX71: return 8;
1780 return 0;
1782 ALboolean DecomposeFormat(ALenum format, enum FmtChannels *chans, enum FmtType *type)
1784 switch(format)
1786 case AL_FORMAT_MONO8:
1787 *chans = FmtMono;
1788 *type = FmtUByte;
1789 return AL_TRUE;
1790 case AL_FORMAT_MONO16:
1791 *chans = FmtMono;
1792 *type = FmtShort;
1793 return AL_TRUE;
1794 case AL_FORMAT_MONO_FLOAT32:
1795 *chans = FmtMono;
1796 *type = FmtFloat;
1797 return AL_TRUE;
1798 case AL_FORMAT_STEREO8:
1799 *chans = FmtStereo;
1800 *type = FmtUByte;
1801 return AL_TRUE;
1802 case AL_FORMAT_STEREO16:
1803 *chans = FmtStereo;
1804 *type = FmtShort;
1805 return AL_TRUE;
1806 case AL_FORMAT_STEREO_FLOAT32:
1807 *chans = FmtStereo;
1808 *type = FmtFloat;
1809 return AL_TRUE;
1810 case AL_FORMAT_QUAD8_LOKI:
1811 case AL_FORMAT_QUAD8:
1812 *chans = FmtQuad;
1813 *type = FmtUByte;
1814 return AL_TRUE;
1815 case AL_FORMAT_QUAD16_LOKI:
1816 case AL_FORMAT_QUAD16:
1817 *chans = FmtQuad;
1818 *type = FmtShort;
1819 return AL_TRUE;
1820 case AL_FORMAT_QUAD32:
1821 *chans = FmtQuad;
1822 *type = FmtFloat;
1823 return AL_TRUE;
1824 case AL_FORMAT_REAR8:
1825 *chans = FmtRear;
1826 *type = FmtUByte;
1827 return AL_TRUE;
1828 case AL_FORMAT_REAR16:
1829 *chans = FmtRear;
1830 *type = FmtShort;
1831 return AL_TRUE;
1832 case AL_FORMAT_REAR32:
1833 *chans = FmtRear;
1834 *type = FmtFloat;
1835 return AL_TRUE;
1836 case AL_FORMAT_51CHN8:
1837 *chans = FmtX51;
1838 *type = FmtUByte;
1839 return AL_TRUE;
1840 case AL_FORMAT_51CHN16:
1841 *chans = FmtX51;
1842 *type = FmtShort;
1843 return AL_TRUE;
1844 case AL_FORMAT_51CHN32:
1845 *chans = FmtX51;
1846 *type = FmtFloat;
1847 return AL_TRUE;
1848 case AL_FORMAT_61CHN8:
1849 *chans = FmtX61;
1850 *type = FmtUByte;
1851 return AL_TRUE;
1852 case AL_FORMAT_61CHN16:
1853 *chans = FmtX61;
1854 *type = FmtShort;
1855 return AL_TRUE;
1856 case AL_FORMAT_61CHN32:
1857 *chans = FmtX61;
1858 *type = FmtFloat;
1859 return AL_TRUE;
1860 case AL_FORMAT_71CHN8:
1861 *chans = FmtX71;
1862 *type = FmtUByte;
1863 return AL_TRUE;
1864 case AL_FORMAT_71CHN16:
1865 *chans = FmtX71;
1866 *type = FmtShort;
1867 return AL_TRUE;
1868 case AL_FORMAT_71CHN32:
1869 *chans = FmtX71;
1870 *type = FmtFloat;
1871 return AL_TRUE;
1873 return AL_FALSE;
1878 * ReleaseALBuffers()
1880 * INTERNAL: Called to destroy any buffers that still exist on the device
1882 ALvoid ReleaseALBuffers(ALCdevice *device)
1884 ALsizei i;
1885 for(i = 0;i < device->BufferMap.size;i++)
1887 ALbuffer *temp = device->BufferMap.array[i].value;
1888 device->BufferMap.array[i].value = NULL;
1890 free(temp->data);
1892 ALTHUNK_REMOVEENTRY(temp->buffer);
1893 memset(temp, 0, sizeof(ALbuffer));
1894 free(temp);