Remove an unnecessary assert
[openal-soft.git] / OpenAL32 / alBuffer.c
bloba847d7d9574629c7528d047ecaa8d0d675faa749
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, const ALvoid *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat);
38 static void ConvertData(ALvoid *dst, enum FmtType dstType, const ALvoid *src, enum SrcFmtType srcType, ALsizei len);
39 static void ConvertDataRear(ALvoid *dst, const ALvoid *src, ALint origBytes, ALsizei len);
40 static void ConvertDataIMA4(ALvoid *dst, const ALvoid *src, ALint origChans, ALsizei len);
41 static void ConvertDataMULaw(ALvoid *dst, const ALvoid *src, ALsizei len);
42 static void ConvertDataMULawRear(ALvoid *dst, const ALvoid *src, ALsizei len);
44 #define LookupBuffer(m, k) ((ALbuffer*)LookupUIntMapKey(&(m), (k)))
48 * Global Variables
51 static const long g_IMAStep_size[89]={ // IMA ADPCM Stepsize table
52 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
53 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143,
54 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
55 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
56 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,11487,12635,13899,
57 15289,16818,18500,20350,22358,24633,27086,29794,32767
60 static const long g_IMACodeword_4[16]={ // IMA4 ADPCM Codeword decode table
61 1, 3, 5, 7, 9, 11, 13, 15,
62 -1,-3,-5,-7,-9,-11,-13,-15,
65 static const long g_IMAIndex_adjust_4[16]={ // IMA4 ADPCM Step index adjust decode table
66 -1,-1,-1,-1, 2, 4, 6, 8,
67 -1,-1,-1,-1, 2, 4, 6, 8
70 static const ALshort muLawDecompressionTable[256] = {
71 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
72 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
73 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
74 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
75 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
76 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
77 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
78 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
79 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
80 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
81 -876, -844, -812, -780, -748, -716, -684, -652,
82 -620, -588, -556, -524, -492, -460, -428, -396,
83 -372, -356, -340, -324, -308, -292, -276, -260,
84 -244, -228, -212, -196, -180, -164, -148, -132,
85 -120, -112, -104, -96, -88, -80, -72, -64,
86 -56, -48, -40, -32, -24, -16, -8, 0,
87 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
88 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
89 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
90 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
91 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
92 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
93 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
94 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
95 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
96 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
97 876, 844, 812, 780, 748, 716, 684, 652,
98 620, 588, 556, 524, 492, 460, 428, 396,
99 372, 356, 340, 324, 308, 292, 276, 260,
100 244, 228, 212, 196, 180, 164, 148, 132,
101 120, 112, 104, 96, 88, 80, 72, 64,
102 56, 48, 40, 32, 24, 16, 8, 0
106 * alGenBuffers(ALsizei n, ALuint *buffers)
108 * Generates n AL Buffers, and stores the Buffers Names in the array pointed to by buffers
110 AL_API ALvoid AL_APIENTRY alGenBuffers(ALsizei n, ALuint *buffers)
112 ALCcontext *Context;
113 ALsizei i=0;
115 Context = GetContextSuspended();
116 if(!Context) return;
118 // Check that we are actually generating some Buffers
119 if(n < 0 || IsBadWritePtr((void*)buffers, n * sizeof(ALuint)))
120 alSetError(Context, AL_INVALID_VALUE);
121 else
123 ALCdevice *device = Context->Device;
124 ALenum err;
126 // Create all the new Buffers
127 while(i < n)
129 ALbuffer *buffer = calloc(1, sizeof(ALbuffer));
130 if(!buffer)
132 alSetError(Context, AL_OUT_OF_MEMORY);
133 alDeleteBuffers(i, buffers);
134 break;
137 buffer->buffer = (ALuint)ALTHUNK_ADDENTRY(buffer);
138 err = InsertUIntMapEntry(&device->BufferMap, buffer->buffer, buffer);
139 if(err != AL_NO_ERROR)
141 ALTHUNK_REMOVEENTRY(buffer->buffer);
142 memset(buffer, 0, sizeof(ALbuffer));
143 free(buffer);
145 alSetError(Context, err);
146 alDeleteBuffers(i, buffers);
147 break;
149 buffers[i++] = buffer->buffer;
153 ProcessContext(Context);
157 * alDeleteBuffers(ALsizei n, ALuint *buffers)
159 * Deletes the n AL Buffers pointed to by buffers
161 AL_API ALvoid AL_APIENTRY alDeleteBuffers(ALsizei n, const ALuint *buffers)
163 ALCcontext *Context;
164 ALCdevice *device;
165 ALboolean Failed;
166 ALbuffer *ALBuf;
167 ALsizei i;
169 Context = GetContextSuspended();
170 if(!Context) return;
172 Failed = AL_TRUE;
173 device = Context->Device;
174 /* Check we are actually Deleting some Buffers */
175 if(n < 0)
176 alSetError(Context, AL_INVALID_VALUE);
177 else
179 Failed = AL_FALSE;
181 /* Check that all the buffers are valid and can actually be deleted */
182 for(i = 0;i < n;i++)
184 if(!buffers[i])
185 continue;
187 /* Check for valid Buffer ID */
188 if((ALBuf=LookupBuffer(device->BufferMap, buffers[i])) == NULL)
190 alSetError(Context, AL_INVALID_NAME);
191 Failed = AL_TRUE;
192 break;
194 else if(ALBuf->refcount != 0)
196 /* Buffer still in use, cannot be deleted */
197 alSetError(Context, AL_INVALID_OPERATION);
198 Failed = AL_TRUE;
199 break;
204 /* If all the Buffers were valid (and have Reference Counts of 0), then we can delete them */
205 if(!Failed)
207 for(i = 0;i < n;i++)
209 if((ALBuf=LookupBuffer(device->BufferMap, buffers[i])) == NULL)
210 continue;
212 /* Release the memory used to store audio data */
213 free(ALBuf->data);
215 /* Release buffer structure */
216 RemoveUIntMapKey(&device->BufferMap, ALBuf->buffer);
217 ALTHUNK_REMOVEENTRY(ALBuf->buffer);
219 memset(ALBuf, 0, sizeof(ALbuffer));
220 free(ALBuf);
224 ProcessContext(Context);
228 * alIsBuffer(ALuint buffer)
230 * Checks if buffer is a valid Buffer Name
232 AL_API ALboolean AL_APIENTRY alIsBuffer(ALuint buffer)
234 ALCcontext *Context;
235 ALboolean result;
237 Context = GetContextSuspended();
238 if(!Context) return AL_FALSE;
240 result = ((!buffer || LookupBuffer(Context->Device->BufferMap, buffer)) ?
241 AL_TRUE : AL_FALSE);
243 ProcessContext(Context);
245 return result;
249 * alBufferData(ALuint buffer,ALenum format,const ALvoid *data,ALsizei size,ALsizei freq)
251 * Fill buffer with audio data
253 AL_API ALvoid AL_APIENTRY alBufferData(ALuint buffer,ALenum format,const ALvoid *data,ALsizei size,ALsizei freq)
255 ALCcontext *Context;
256 ALCdevice *device;
257 ALbuffer *ALBuf;
258 ALvoid *temp;
259 ALenum err;
261 Context = GetContextSuspended();
262 if(!Context) return;
264 if(Context->SampleSource)
266 ALintptrEXT offset;
268 if(Context->SampleSource->state == MAPPED)
270 alSetError(Context, AL_INVALID_OPERATION);
271 ProcessContext(Context);
272 return;
275 offset = (const ALubyte*)data - (ALubyte*)NULL;
276 data = Context->SampleSource->data + offset;
279 device = Context->Device;
280 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
281 alSetError(Context, AL_INVALID_NAME);
282 else if(size < 0 || freq < 0)
283 alSetError(Context, AL_INVALID_VALUE);
284 else if(ALBuf->refcount != 0)
285 alSetError(Context, AL_INVALID_VALUE);
286 else switch(format)
288 case AL_FORMAT_MONO8:
289 case AL_FORMAT_MONO16:
290 case AL_FORMAT_MONO_FLOAT32:
291 case AL_FORMAT_MONO_DOUBLE_EXT:
292 case AL_FORMAT_STEREO8:
293 case AL_FORMAT_STEREO16:
294 case AL_FORMAT_STEREO_FLOAT32:
295 case AL_FORMAT_STEREO_DOUBLE_EXT:
296 case AL_FORMAT_QUAD8_LOKI:
297 case AL_FORMAT_QUAD16_LOKI:
298 case AL_FORMAT_QUAD8:
299 case AL_FORMAT_QUAD16:
300 case AL_FORMAT_QUAD32:
301 case AL_FORMAT_51CHN8:
302 case AL_FORMAT_51CHN16:
303 case AL_FORMAT_51CHN32:
304 case AL_FORMAT_61CHN8:
305 case AL_FORMAT_61CHN16:
306 case AL_FORMAT_61CHN32:
307 case AL_FORMAT_71CHN8:
308 case AL_FORMAT_71CHN16:
309 case AL_FORMAT_71CHN32:
310 err = LoadData(ALBuf, data, size, freq, format, format);
311 if(err != AL_NO_ERROR)
312 alSetError(Context, err);
313 break;
315 case AL_FORMAT_REAR8:
316 case AL_FORMAT_REAR16:
317 case AL_FORMAT_REAR32: {
318 ALuint OrigBytes = ((format==AL_FORMAT_REAR8) ? 1 :
319 ((format==AL_FORMAT_REAR16) ? 2 : 4));
320 ALenum NewFormat = ((OrigBytes==4) ? AL_FORMAT_QUAD32 :
321 ((OrigBytes==2) ? AL_FORMAT_QUAD16 :
322 AL_FORMAT_QUAD8));
323 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
324 ALuint NewBytes = aluBytesFromFormat(NewFormat);
325 ALuint64 newsize;
327 if((size%(OrigBytes*2)) != 0)
329 alSetError(Context, AL_INVALID_VALUE);
330 break;
333 newsize = size / OrigBytes;
334 newsize *= 2;
335 newsize *= NewBytes;
337 if(newsize > INT_MAX)
339 alSetError(Context, AL_OUT_OF_MEMORY);
340 break;
342 temp = realloc(ALBuf->data, newsize);
343 if(temp)
345 ALBuf->data = temp;
346 ConvertDataRear(ALBuf->data, data, OrigBytes, newsize/NewBytes);
348 ALBuf->format = NewFormat;
349 ALBuf->eOriginalFormat = format;
350 ALBuf->size = newsize;
351 ALBuf->frequency = freq;
353 ALBuf->LoopStart = 0;
354 ALBuf->LoopEnd = newsize / NewChannels / NewBytes;
356 DecomposeFormat(NewFormat, &ALBuf->FmtType, &ALBuf->FmtChannels);
358 ALBuf->OriginalSize = size;
359 ALBuf->OriginalAlign = OrigBytes * 2;
361 else
362 alSetError(Context, AL_OUT_OF_MEMORY);
363 } break;
365 case AL_FORMAT_MONO_IMA4:
366 case AL_FORMAT_STEREO_IMA4: {
367 ALuint Channels = ((format==AL_FORMAT_MONO_IMA4) ? 1 : 2);
368 ALenum NewFormat = ((Channels==1) ? AL_FORMAT_MONO16 :
369 AL_FORMAT_STEREO16);
370 ALuint NewBytes = aluBytesFromFormat(NewFormat);
371 ALuint64 newsize;
373 /* Here is where things vary:
374 * nVidia and Apple use 64+1 sample frames per block => block_size=36*chans bytes
375 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024*chans bytes
377 if((size%(36*Channels)) != 0)
379 alSetError(Context, AL_INVALID_VALUE);
380 break;
383 newsize = size / 36;
384 newsize *= 65;
385 newsize *= NewBytes;
387 if(newsize > INT_MAX)
389 alSetError(Context, AL_OUT_OF_MEMORY);
390 break;
392 temp = realloc(ALBuf->data, newsize);
393 if(temp)
395 ALBuf->data = temp;
396 ConvertDataIMA4(ALBuf->data, data, Channels, newsize/(65*Channels*NewBytes));
398 ALBuf->format = NewFormat;
399 ALBuf->eOriginalFormat = format;
400 ALBuf->size = newsize;
401 ALBuf->frequency = freq;
403 ALBuf->LoopStart = 0;
404 ALBuf->LoopEnd = newsize / Channels / NewBytes;
406 DecomposeFormat(NewFormat, &ALBuf->FmtType, &ALBuf->FmtChannels);
408 ALBuf->OriginalSize = size;
409 ALBuf->OriginalAlign = 36 * Channels;
411 else
412 alSetError(Context, AL_OUT_OF_MEMORY);
413 } break;
415 case AL_FORMAT_MONO_MULAW:
416 case AL_FORMAT_STEREO_MULAW:
417 case AL_FORMAT_QUAD_MULAW:
418 case AL_FORMAT_51CHN_MULAW:
419 case AL_FORMAT_61CHN_MULAW:
420 case AL_FORMAT_71CHN_MULAW: {
421 ALuint Channels = ((format==AL_FORMAT_MONO_MULAW) ? 1 :
422 ((format==AL_FORMAT_STEREO_MULAW) ? 2 :
423 ((format==AL_FORMAT_QUAD_MULAW) ? 4 :
424 ((format==AL_FORMAT_51CHN_MULAW) ? 6 :
425 ((format==AL_FORMAT_61CHN_MULAW) ? 7 : 8)))));
426 ALenum NewFormat = ((Channels==1) ? AL_FORMAT_MONO16 :
427 ((Channels==2) ? AL_FORMAT_STEREO16 :
428 ((Channels==4) ? AL_FORMAT_QUAD16 :
429 ((Channels==6) ? AL_FORMAT_51CHN16 :
430 ((Channels==7) ? AL_FORMAT_61CHN16 :
431 AL_FORMAT_71CHN16)))));
432 ALuint NewBytes = aluBytesFromFormat(NewFormat);
433 ALuint64 newsize;
435 if((size%(1*Channels)) != 0)
437 alSetError(Context, AL_INVALID_VALUE);
438 break;
441 newsize = size * NewBytes;
442 if(newsize > INT_MAX)
444 alSetError(Context, AL_OUT_OF_MEMORY);
445 break;
447 temp = realloc(ALBuf->data, newsize);
448 if(temp)
450 ALBuf->data = temp;
451 ConvertDataMULaw(ALBuf->data, data, newsize/NewBytes);
453 ALBuf->format = NewFormat;
454 ALBuf->eOriginalFormat = format;
455 ALBuf->size = newsize;
456 ALBuf->frequency = freq;
458 ALBuf->LoopStart = 0;
459 ALBuf->LoopEnd = newsize / Channels / NewBytes;
461 DecomposeFormat(NewFormat, &ALBuf->FmtType, &ALBuf->FmtChannels);
463 ALBuf->OriginalSize = size;
464 ALBuf->OriginalAlign = 1 * Channels;
466 else
467 alSetError(Context, AL_OUT_OF_MEMORY);
468 } break;
470 case AL_FORMAT_REAR_MULAW: {
471 ALenum NewFormat = AL_FORMAT_QUAD16;
472 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
473 ALuint NewBytes = aluBytesFromFormat(NewFormat);
474 ALuint64 newsize;
476 if((size%(1*2)) != 0)
478 alSetError(Context, AL_INVALID_VALUE);
479 break;
482 newsize = size * 2;
483 newsize *= NewBytes;
485 if(newsize > INT_MAX)
487 alSetError(Context, AL_OUT_OF_MEMORY);
488 break;
490 temp = realloc(ALBuf->data, newsize);
491 if(temp)
493 ALBuf->data = temp;
494 ConvertDataMULawRear(ALBuf->data, data, newsize/NewBytes);
496 ALBuf->format = NewFormat;
497 ALBuf->eOriginalFormat = format;
498 ALBuf->size = newsize;
499 ALBuf->frequency = freq;
501 ALBuf->LoopStart = 0;
502 ALBuf->LoopEnd = newsize / NewChannels / NewBytes;
504 DecomposeFormat(NewFormat, &ALBuf->FmtType, &ALBuf->FmtChannels);
506 ALBuf->OriginalSize = size;
507 ALBuf->OriginalAlign = 1 * 2;
509 else
510 alSetError(Context, AL_OUT_OF_MEMORY);
511 } break;
513 default:
514 alSetError(Context, AL_INVALID_ENUM);
515 break;
518 ProcessContext(Context);
522 * alBufferSubDataSOFT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
524 * Update buffer's audio data
526 AL_API ALvoid AL_APIENTRY alBufferSubDataSOFT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
528 ALCcontext *Context;
529 ALCdevice *device;
530 ALbuffer *ALBuf;
532 Context = GetContextSuspended();
533 if(!Context) return;
535 if(Context->SampleSource)
537 ALintptrEXT offset;
539 if(Context->SampleSource->state == MAPPED)
541 alSetError(Context, AL_INVALID_OPERATION);
542 ProcessContext(Context);
543 return;
546 offset = (const ALubyte*)data - (ALubyte*)NULL;
547 data = Context->SampleSource->data + offset;
550 device = Context->Device;
551 if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
552 alSetError(Context, AL_INVALID_NAME);
553 else if(length < 0 || offset < 0 || (length > 0 && data == NULL))
554 alSetError(Context, AL_INVALID_VALUE);
555 else if(ALBuf->eOriginalFormat != format)
556 alSetError(Context, AL_INVALID_ENUM);
557 else if(offset > ALBuf->OriginalSize ||
558 length > ALBuf->OriginalSize-offset ||
559 (offset%ALBuf->OriginalAlign) != 0 ||
560 (length%ALBuf->OriginalAlign) != 0)
561 alSetError(Context, AL_INVALID_VALUE);
562 else switch(format)
564 case AL_FORMAT_MONO8:
565 case AL_FORMAT_MONO16:
566 case AL_FORMAT_MONO_FLOAT32:
567 case AL_FORMAT_MONO_DOUBLE_EXT:
568 case AL_FORMAT_STEREO8:
569 case AL_FORMAT_STEREO16:
570 case AL_FORMAT_STEREO_FLOAT32:
571 case AL_FORMAT_STEREO_DOUBLE_EXT:
572 case AL_FORMAT_QUAD8_LOKI:
573 case AL_FORMAT_QUAD16_LOKI:
574 case AL_FORMAT_QUAD8:
575 case AL_FORMAT_QUAD16:
576 case AL_FORMAT_QUAD32:
577 case AL_FORMAT_51CHN8:
578 case AL_FORMAT_51CHN16:
579 case AL_FORMAT_51CHN32:
580 case AL_FORMAT_61CHN8:
581 case AL_FORMAT_61CHN16:
582 case AL_FORMAT_61CHN32:
583 case AL_FORMAT_71CHN8:
584 case AL_FORMAT_71CHN16:
585 case AL_FORMAT_71CHN32: {
586 ALuint OldBytes = aluBytesFromFormat(format);
587 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
588 enum SrcFmtChannels SrcChannels;
589 enum SrcFmtType SrcType;
591 offset /= OldBytes;
592 offset *= Bytes;
593 length /= OldBytes;
595 DecomposeInputFormat(format, &SrcType, &SrcChannels);
596 ConvertData(&((ALubyte*)ALBuf->data)[offset], ALBuf->FmtType,
597 data, SrcType, length);
598 } break;
600 case AL_FORMAT_REAR8:
601 case AL_FORMAT_REAR16:
602 case AL_FORMAT_REAR32: {
603 ALuint OldBytes = ((format==AL_FORMAT_REAR8) ? 1 :
604 ((format==AL_FORMAT_REAR16) ? 2 : 4));
605 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
607 offset /= OldBytes;
608 offset *= 2;
609 offset *= Bytes;
610 length /= OldBytes;
611 length *= 2;
613 ConvertDataRear(&((ALubyte*)ALBuf->data)[offset], data, Bytes, length);
614 } break;
616 case AL_FORMAT_MONO_IMA4:
617 case AL_FORMAT_STEREO_IMA4: {
618 ALuint Channels = aluChannelsFromFormat(ALBuf->format);
619 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
621 /* offset -> byte offset, length -> block count */
622 offset /= 36;
623 offset *= 65;
624 offset *= Bytes;
625 length /= ALBuf->OriginalAlign;
627 ConvertDataIMA4(&((ALubyte*)ALBuf->data)[offset], data, Channels, length);
628 } break;
630 case AL_FORMAT_MONO_MULAW:
631 case AL_FORMAT_STEREO_MULAW:
632 case AL_FORMAT_QUAD_MULAW:
633 case AL_FORMAT_51CHN_MULAW:
634 case AL_FORMAT_61CHN_MULAW:
635 case AL_FORMAT_71CHN_MULAW: {
636 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
638 offset *= Bytes;
640 ConvertDataMULaw(&((ALubyte*)ALBuf->data)[offset], data, length);
641 } break;
643 case AL_FORMAT_REAR_MULAW: {
644 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
646 offset *= 2;
647 offset *= Bytes;
648 length *= 2;
650 ConvertDataMULawRear(&((ALubyte*)ALBuf->data)[offset], data, length);
651 } break;
653 default:
654 alSetError(Context, AL_INVALID_ENUM);
655 break;
658 ProcessContext(Context);
661 AL_API ALvoid AL_APIENTRY alBufferSubDataEXT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
663 alBufferSubDataSOFT(buffer, format, data, offset, length);
667 AL_API void AL_APIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
669 ALCcontext *pContext;
670 ALCdevice *device;
672 (void)flValue;
674 pContext = GetContextSuspended();
675 if(!pContext) return;
677 device = pContext->Device;
678 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 alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
696 ALCcontext *pContext;
697 ALCdevice *device;
699 (void)flValue1;
700 (void)flValue2;
701 (void)flValue3;
703 pContext = GetContextSuspended();
704 if(!pContext) return;
706 device = pContext->Device;
707 if(LookupBuffer(device->BufferMap, buffer) == NULL)
708 alSetError(pContext, AL_INVALID_NAME);
709 else
711 switch(eParam)
713 default:
714 alSetError(pContext, AL_INVALID_ENUM);
715 break;
719 ProcessContext(pContext);
723 AL_API void AL_APIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
725 ALCcontext *pContext;
726 ALCdevice *device;
728 pContext = GetContextSuspended();
729 if(!pContext) return;
731 device = pContext->Device;
732 if(!flValues)
733 alSetError(pContext, AL_INVALID_VALUE);
734 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
735 alSetError(pContext, AL_INVALID_NAME);
736 else
738 switch(eParam)
740 default:
741 alSetError(pContext, AL_INVALID_ENUM);
742 break;
746 ProcessContext(pContext);
750 AL_API void AL_APIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
752 ALCcontext *pContext;
753 ALCdevice *device;
755 (void)lValue;
757 pContext = GetContextSuspended();
758 if(!pContext) return;
760 device = pContext->Device;
761 if(LookupBuffer(device->BufferMap, buffer) == NULL)
762 alSetError(pContext, AL_INVALID_NAME);
763 else
765 switch(eParam)
767 default:
768 alSetError(pContext, AL_INVALID_ENUM);
769 break;
773 ProcessContext(pContext);
777 AL_API void AL_APIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
779 ALCcontext *pContext;
780 ALCdevice *device;
782 (void)lValue1;
783 (void)lValue2;
784 (void)lValue3;
786 pContext = GetContextSuspended();
787 if(!pContext) return;
789 device = pContext->Device;
790 if(LookupBuffer(device->BufferMap, buffer) == NULL)
791 alSetError(pContext, AL_INVALID_NAME);
792 else
794 switch(eParam)
796 default:
797 alSetError(pContext, AL_INVALID_ENUM);
798 break;
802 ProcessContext(pContext);
806 AL_API void AL_APIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
808 ALCcontext *pContext;
809 ALCdevice *device;
810 ALbuffer *ALBuf;
812 pContext = GetContextSuspended();
813 if(!pContext) return;
815 device = pContext->Device;
816 if(!plValues)
817 alSetError(pContext, AL_INVALID_VALUE);
818 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
819 alSetError(pContext, AL_INVALID_NAME);
820 else
822 switch(eParam)
824 case AL_LOOP_POINTS:
825 if(ALBuf->refcount > 0)
826 alSetError(pContext, AL_INVALID_OPERATION);
827 else if(plValues[0] < 0 || plValues[1] < 0 ||
828 plValues[0] >= plValues[1] || ALBuf->size == 0)
829 alSetError(pContext, AL_INVALID_VALUE);
830 else
832 ALint maxlen = ALBuf->size / aluFrameSizeFromFormat(ALBuf->format);
833 if(plValues[0] > maxlen || plValues[1] > maxlen)
834 alSetError(pContext, AL_INVALID_VALUE);
835 else
837 ALBuf->LoopStart = plValues[0];
838 ALBuf->LoopEnd = plValues[1];
841 break;
843 default:
844 alSetError(pContext, AL_INVALID_ENUM);
845 break;
849 ProcessContext(pContext);
853 AL_API ALvoid AL_APIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
855 ALCcontext *pContext;
856 ALCdevice *device;
858 pContext = GetContextSuspended();
859 if(!pContext) return;
861 device = pContext->Device;
862 if(!pflValue)
863 alSetError(pContext, AL_INVALID_VALUE);
864 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
865 alSetError(pContext, AL_INVALID_NAME);
866 else
868 switch(eParam)
870 default:
871 alSetError(pContext, AL_INVALID_ENUM);
872 break;
876 ProcessContext(pContext);
880 AL_API void AL_APIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
882 ALCcontext *pContext;
883 ALCdevice *device;
885 pContext = GetContextSuspended();
886 if(!pContext) return;
888 device = pContext->Device;
889 if(!pflValue1 || !pflValue2 || !pflValue3)
890 alSetError(pContext, AL_INVALID_VALUE);
891 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
892 alSetError(pContext, AL_INVALID_NAME);
893 else
895 switch(eParam)
897 default:
898 alSetError(pContext, AL_INVALID_ENUM);
899 break;
903 ProcessContext(pContext);
907 AL_API void AL_APIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
909 ALCcontext *pContext;
910 ALCdevice *device;
912 pContext = GetContextSuspended();
913 if(!pContext) return;
915 device = pContext->Device;
916 if(!pflValues)
917 alSetError(pContext, AL_INVALID_VALUE);
918 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
919 alSetError(pContext, AL_INVALID_NAME);
920 else
922 switch(eParam)
924 default:
925 alSetError(pContext, AL_INVALID_ENUM);
926 break;
930 ProcessContext(pContext);
934 AL_API ALvoid AL_APIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
936 ALCcontext *pContext;
937 ALbuffer *pBuffer;
938 ALCdevice *device;
940 pContext = GetContextSuspended();
941 if(!pContext) return;
943 device = pContext->Device;
944 if(!plValue)
945 alSetError(pContext, AL_INVALID_VALUE);
946 else if((pBuffer=LookupBuffer(device->BufferMap, buffer)) == NULL)
947 alSetError(pContext, AL_INVALID_NAME);
948 else
950 switch(eParam)
952 case AL_FREQUENCY:
953 *plValue = pBuffer->frequency;
954 break;
956 case AL_BITS:
957 *plValue = aluBytesFromFormat(pBuffer->format) * 8;
958 break;
960 case AL_CHANNELS:
961 *plValue = aluChannelsFromFormat(pBuffer->format);
962 break;
964 case AL_SIZE:
965 *plValue = pBuffer->size;
966 break;
968 default:
969 alSetError(pContext, AL_INVALID_ENUM);
970 break;
974 ProcessContext(pContext);
978 AL_API void AL_APIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
980 ALCcontext *pContext;
981 ALCdevice *device;
983 pContext = GetContextSuspended();
984 if(!pContext) return;
986 device = pContext->Device;
987 if(!plValue1 || !plValue2 || !plValue3)
988 alSetError(pContext, AL_INVALID_VALUE);
989 else if(LookupBuffer(device->BufferMap, buffer) == NULL)
990 alSetError(pContext, AL_INVALID_NAME);
991 else
993 switch(eParam)
995 default:
996 alSetError(pContext, AL_INVALID_ENUM);
997 break;
1001 ProcessContext(pContext);
1005 AL_API void AL_APIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
1007 ALCcontext *pContext;
1008 ALCdevice *device;
1009 ALbuffer *ALBuf;
1011 pContext = GetContextSuspended();
1012 if(!pContext) return;
1014 device = pContext->Device;
1015 if(!plValues)
1016 alSetError(pContext, AL_INVALID_VALUE);
1017 else if((ALBuf=LookupBuffer(device->BufferMap, buffer)) == NULL)
1018 alSetError(pContext, AL_INVALID_NAME);
1019 else
1021 switch(eParam)
1023 case AL_FREQUENCY:
1024 case AL_BITS:
1025 case AL_CHANNELS:
1026 case AL_SIZE:
1027 alGetBufferi(buffer, eParam, plValues);
1028 break;
1030 case AL_LOOP_POINTS:
1031 plValues[0] = ALBuf->LoopStart;
1032 plValues[1] = ALBuf->LoopEnd;
1033 break;
1035 default:
1036 alSetError(pContext, AL_INVALID_ENUM);
1037 break;
1041 ProcessContext(pContext);
1045 static void ConvertDataRear(ALvoid *dst, const ALvoid *src, ALint origBytes, ALsizei len)
1047 ALsizei i;
1048 if(src == NULL)
1049 return;
1050 switch(origBytes)
1052 case 1:
1053 for(i = 0;i < len;i+=4)
1055 ((ALubyte*)dst)[i+0] = 0;
1056 ((ALubyte*)dst)[i+1] = 0;
1057 ((ALubyte*)dst)[i+2] = ((ALubyte*)src)[i/2+0];
1058 ((ALubyte*)dst)[i+3] = ((ALubyte*)src)[i/2+1];
1060 break;
1062 case 2:
1063 for(i = 0;i < len;i+=4)
1065 ((ALshort*)dst)[i+0] = 0;
1066 ((ALshort*)dst)[i+1] = 0;
1067 ((ALshort*)dst)[i+2] = ((ALshort*)src)[i/2+0];
1068 ((ALshort*)dst)[i+3] = ((ALshort*)src)[i/2+1];
1070 break;
1072 case 4:
1073 for(i = 0;i < len;i+=4)
1075 ((ALfloat*)dst)[i+0] = 0.f;
1076 ((ALfloat*)dst)[i+1] = 0.f;
1077 ((ALfloat*)dst)[i+2] = ((ALfloat*)src)[i/2+0];
1078 ((ALfloat*)dst)[i+3] = ((ALfloat*)src)[i/2+1];
1080 break;
1082 default:
1083 assert(0);
1087 static void ConvertDataIMA4(ALvoid *dst, const ALvoid *src, ALint chans, ALsizei len)
1089 const ALubyte *IMAData;
1090 ALint Sample[2],Index[2];
1091 ALuint IMACode[2];
1092 ALsizei i,j,k,c;
1094 if(src == NULL)
1095 return;
1097 IMAData = src;
1098 for(i = 0;i < len;i++)
1100 for(c = 0;c < chans;c++)
1102 Sample[c] = *(IMAData++);
1103 Sample[c] |= *(IMAData++) << 8;
1104 Sample[c] = (Sample[c]^0x8000) - 32768;
1105 Index[c] = *(IMAData++);
1106 Index[c] |= *(IMAData++) << 8;
1107 Index[c] = (Index[c]^0x8000) - 32768;
1109 Index[c] = ((Index[c]<0) ? 0 : Index[c]);
1110 Index[c] = ((Index[c]>88) ? 88 : Index[c]);
1112 ((ALshort*)dst)[i*65*chans + c] = Sample[c];
1115 for(j = 1;j < 65;j += 8)
1117 for(c = 0;c < chans;c++)
1119 IMACode[c] = *(IMAData++);
1120 IMACode[c] |= *(IMAData++) << 8;
1121 IMACode[c] |= *(IMAData++) << 16;
1122 IMACode[c] |= *(IMAData++) << 24;
1125 for(k = 0;k < 8;k++)
1127 for(c = 0;c < chans;c++)
1129 Sample[c] += ((g_IMAStep_size[Index[c]]*g_IMACodeword_4[IMACode[c]&15])/8);
1130 Index[c] += g_IMAIndex_adjust_4[IMACode[c]&15];
1132 if(Sample[c] < -32768) Sample[c] = -32768;
1133 else if(Sample[c] > 32767) Sample[c] = 32767;
1135 if(Index[c]<0) Index[c] = 0;
1136 else if(Index[c]>88) Index[c] = 88;
1138 ((ALshort*)dst)[(i*65+j+k)*chans + c] = Sample[c];
1139 IMACode[c] >>= 4;
1146 static void ConvertDataMULaw(ALvoid *dst, const ALvoid *src, ALsizei len)
1148 ALsizei i;
1149 if(src == NULL)
1150 return;
1151 for(i = 0;i < len;i++)
1152 ((ALshort*)dst)[i] = muLawDecompressionTable[((ALubyte*)src)[i]];
1155 static void ConvertDataMULawRear(ALvoid *dst, const ALvoid *src, ALsizei len)
1157 ALsizei i;
1158 if(src == NULL)
1159 return;
1160 for(i = 0;i < len;i+=4)
1162 ((ALshort*)dst)[i+0] = 0;
1163 ((ALshort*)dst)[i+1] = 0;
1164 ((ALshort*)dst)[i+2] = muLawDecompressionTable[((ALubyte*)src)[i/2+0]];
1165 ((ALshort*)dst)[i+3] = muLawDecompressionTable[((ALubyte*)src)[i/2+1]];
1170 static __inline ALbyte Conv_ALbyte_ALbyte(ALbyte val)
1171 { return val; }
1172 static __inline ALbyte Conv_ALbyte_ALubyte(ALubyte val)
1173 { return val^0x80; }
1174 static __inline ALbyte Conv_ALbyte_ALshort(ALshort val)
1175 { return val>>8; }
1176 static __inline ALbyte Conv_ALbyte_ALushort(ALushort val)
1177 { return (val>>8)-128; }
1178 static __inline ALbyte Conv_ALbyte_ALfloat(ALfloat val)
1180 if(val >= 1.0f) return 127;
1181 if(val <= -1.0f) return -128;
1182 return (ALint)(val * 127.0f);
1184 static __inline ALbyte Conv_ALbyte_ALdouble(ALdouble val)
1186 if(val >= 1.0) return 127;
1187 if(val <= -1.0) return -128;
1188 return (ALint)(val * 127.0);
1191 static __inline ALubyte Conv_ALubyte_ALbyte(ALbyte val)
1192 { return val^0x80; }
1193 static __inline ALubyte Conv_ALubyte_ALubyte(ALubyte val)
1194 { return val; }
1195 static __inline ALubyte Conv_ALubyte_ALshort(ALshort val)
1196 { return (val>>8)+128; }
1197 static __inline ALubyte Conv_ALubyte_ALushort(ALushort val)
1198 { return val>>8; }
1199 static __inline ALubyte Conv_ALubyte_ALfloat(ALfloat val)
1201 if(val >= 1.0f) return 255;
1202 if(val <= -1.0f) return 0;
1203 return (ALint)(val * 127.0f) + 128;
1205 static __inline ALubyte Conv_ALubyte_ALdouble(ALdouble val)
1207 if(val >= 1.0) return 255;
1208 if(val <= -1.0) return 0;
1209 return (ALint)(val * 127.0) + 128;
1212 static __inline ALshort Conv_ALshort_ALbyte(ALbyte val)
1213 { return val<<8; }
1214 static __inline ALshort Conv_ALshort_ALubyte(ALubyte val)
1215 { return (val-128)<<8; }
1216 static __inline ALshort Conv_ALshort_ALshort(ALshort val)
1217 { return val; }
1218 static __inline ALshort Conv_ALshort_ALushort(ALushort val)
1219 { return val^0x8000; }
1220 static __inline ALshort Conv_ALshort_ALfloat(ALfloat val)
1222 if(val >= 1.0f) return 32767;
1223 if(val <= -1.0f) return -32768;
1224 return (ALint)(val * 32767.0f);
1226 static __inline ALshort Conv_ALshort_ALdouble(ALdouble val)
1228 if(val >= 1.0) return 32767;
1229 if(val <= -1.0) return -32768;
1230 return (ALint)(val * 32767.0);
1233 static __inline ALushort Conv_ALushort_ALbyte(ALbyte val)
1234 { return (val+128)<<8; }
1235 static __inline ALushort Conv_ALushort_ALubyte(ALubyte val)
1236 { return val<<8; }
1237 static __inline ALushort Conv_ALushort_ALshort(ALshort val)
1238 { return val^0x8000; }
1239 static __inline ALushort Conv_ALushort_ALushort(ALushort val)
1240 { return val; }
1241 static __inline ALushort Conv_ALushort_ALfloat(ALfloat val)
1243 if(val >= 1.0f) return 65535;
1244 if(val <= -1.0f) return 0;
1245 return (ALint)(val * 32767.0f) + 32768;
1247 static __inline ALushort Conv_ALushort_ALdouble(ALdouble val)
1249 if(val >= 1.0) return 65535;
1250 if(val <= -1.0) return 0;
1251 return (ALint)(val * 32767.0) + 32768;
1254 static __inline ALfloat Conv_ALfloat_ALbyte(ALbyte val)
1255 { return val * (1.0f/127.0f); }
1256 static __inline ALfloat Conv_ALfloat_ALubyte(ALubyte val)
1257 { return (val-128) * (1.0f/127.0f); }
1258 static __inline ALfloat Conv_ALfloat_ALshort(ALshort val)
1259 { return val * (1.0f/32767.0f); }
1260 static __inline ALfloat Conv_ALfloat_ALushort(ALushort val)
1261 { return (val-32768) * (1.0f/32767.0f); }
1262 static __inline ALfloat Conv_ALfloat_ALfloat(ALfloat val)
1263 { return val; }
1264 static __inline ALfloat Conv_ALfloat_ALdouble(ALdouble val)
1265 { return val; }
1267 static __inline ALdouble Conv_ALdouble_ALbyte(ALbyte val)
1268 { return val * (1.0/127.0); }
1269 static __inline ALdouble Conv_ALdouble_ALubyte(ALubyte val)
1270 { return (val-128) * (1.0/127.0); }
1271 static __inline ALdouble Conv_ALdouble_ALshort(ALshort val)
1272 { return val * (1.0/32767.0); }
1273 static __inline ALdouble Conv_ALdouble_ALushort(ALushort val)
1274 { return (val-32768) * (1.0/32767.0); }
1275 static __inline ALdouble Conv_ALdouble_ALfloat(ALfloat val)
1276 { return val; }
1277 static __inline ALdouble Conv_ALdouble_ALdouble(ALdouble val)
1278 { return val; }
1281 #define DECL_TEMPLATE(T1, T2) \
1282 static void Convert_##T1##_##T2(T1 *dst, const T2 *src, ALuint len) \
1284 ALuint i; \
1285 for(i = 0;i < len;i++) \
1286 *(dst++) = Conv_##T1##_##T2(*(src++)); \
1289 DECL_TEMPLATE(ALubyte, ALbyte)
1290 DECL_TEMPLATE(ALubyte, ALubyte)
1291 DECL_TEMPLATE(ALubyte, ALshort)
1292 DECL_TEMPLATE(ALubyte, ALushort)
1293 DECL_TEMPLATE(ALubyte, ALfloat)
1294 DECL_TEMPLATE(ALubyte, ALdouble)
1296 DECL_TEMPLATE(ALbyte, ALbyte)
1297 DECL_TEMPLATE(ALbyte, ALubyte)
1298 DECL_TEMPLATE(ALbyte, ALshort)
1299 DECL_TEMPLATE(ALbyte, ALushort)
1300 DECL_TEMPLATE(ALbyte, ALfloat)
1301 DECL_TEMPLATE(ALbyte, ALdouble)
1303 DECL_TEMPLATE(ALshort, ALbyte)
1304 DECL_TEMPLATE(ALshort, ALubyte)
1305 DECL_TEMPLATE(ALshort, ALshort)
1306 DECL_TEMPLATE(ALshort, ALushort)
1307 DECL_TEMPLATE(ALshort, ALfloat)
1308 DECL_TEMPLATE(ALshort, ALdouble)
1310 DECL_TEMPLATE(ALushort, ALbyte)
1311 DECL_TEMPLATE(ALushort, ALubyte)
1312 DECL_TEMPLATE(ALushort, ALshort)
1313 DECL_TEMPLATE(ALushort, ALushort)
1314 DECL_TEMPLATE(ALushort, ALfloat)
1315 DECL_TEMPLATE(ALushort, ALdouble)
1317 DECL_TEMPLATE(ALfloat, ALbyte)
1318 DECL_TEMPLATE(ALfloat, ALubyte)
1319 DECL_TEMPLATE(ALfloat, ALshort)
1320 DECL_TEMPLATE(ALfloat, ALushort)
1321 DECL_TEMPLATE(ALfloat, ALfloat)
1322 DECL_TEMPLATE(ALfloat, ALdouble)
1324 DECL_TEMPLATE(ALdouble, ALbyte)
1325 DECL_TEMPLATE(ALdouble, ALubyte)
1326 DECL_TEMPLATE(ALdouble, ALshort)
1327 DECL_TEMPLATE(ALdouble, ALushort)
1328 DECL_TEMPLATE(ALdouble, ALfloat)
1329 DECL_TEMPLATE(ALdouble, ALdouble)
1331 #undef DECL_TEMPLATE
1333 #define DECL_TEMPLATE(T) \
1334 static void Convert_##T(T *dst, const ALvoid *src, enum SrcFmtType srcType, \
1335 ALsizei len) \
1337 switch(srcType) \
1339 case SrcFmtByte: \
1340 Convert_##T##_ALbyte(dst, src, len); \
1341 break; \
1342 case SrcFmtUByte: \
1343 Convert_##T##_ALubyte(dst, src, len); \
1344 break; \
1345 case SrcFmtShort: \
1346 Convert_##T##_ALshort(dst, src, len); \
1347 break; \
1348 case SrcFmtUShort: \
1349 Convert_##T##_ALushort(dst, src, len); \
1350 break; \
1351 case SrcFmtFloat: \
1352 Convert_##T##_ALfloat(dst, src, len); \
1353 break; \
1354 case SrcFmtDouble: \
1355 Convert_##T##_ALdouble(dst, src, len); \
1356 break; \
1360 DECL_TEMPLATE(ALbyte)
1361 DECL_TEMPLATE(ALubyte)
1362 DECL_TEMPLATE(ALshort)
1363 DECL_TEMPLATE(ALushort)
1364 DECL_TEMPLATE(ALfloat)
1365 DECL_TEMPLATE(ALdouble)
1367 #undef DECL_TEMPLATE
1371 * LoadData
1373 * Loads the specified data into the buffer, using the specified formats.
1374 * Currently, the new format must have the same channel configuration as the
1375 * original format. This does NOT handle compressed formats (eg. IMA4).
1377 static ALenum LoadData(ALbuffer *ALBuf, const ALvoid *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat)
1379 ALuint NewBytes = aluBytesFromFormat(NewFormat);
1380 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
1381 ALuint OrigBytes = aluBytesFromFormat(OrigFormat);
1382 ALuint OrigChannels = aluChannelsFromFormat(OrigFormat);
1383 enum SrcFmtChannels SrcChannels;
1384 enum FmtChannels DstChannels;
1385 enum SrcFmtType SrcType;
1386 enum FmtType DstType;
1387 ALuint64 newsize;
1388 ALvoid *temp;
1390 assert(NewChannels == OrigChannels);
1392 DecomposeInputFormat(OrigFormat, &SrcType, &SrcChannels);
1393 DecomposeFormat(NewFormat, &DstType, &DstChannels);
1395 if((size%(OrigBytes*OrigChannels)) != 0)
1396 return AL_INVALID_VALUE;
1398 newsize = size / OrigBytes;
1399 newsize *= NewBytes;
1400 if(newsize > INT_MAX)
1401 return AL_OUT_OF_MEMORY;
1403 temp = realloc(ALBuf->data, newsize);
1404 if(!temp) return AL_OUT_OF_MEMORY;
1405 ALBuf->data = temp;
1407 if(data != NULL)
1409 // Samples are converted here
1410 ConvertData(ALBuf->data, DstType, data, SrcType, newsize/NewBytes);
1413 ALBuf->format = NewFormat;
1414 ALBuf->eOriginalFormat = OrigFormat;
1415 ALBuf->size = newsize;
1416 ALBuf->frequency = freq;
1418 ALBuf->LoopStart = 0;
1419 ALBuf->LoopEnd = newsize / NewChannels / NewBytes;
1421 ALBuf->FmtType = DstType;
1422 ALBuf->FmtChannels = DstChannels;
1424 ALBuf->OriginalSize = size;
1425 ALBuf->OriginalAlign = OrigBytes * OrigChannels;
1427 return AL_NO_ERROR;
1430 static void ConvertData(ALvoid *dst, enum FmtType dstType, const ALvoid *src, enum SrcFmtType srcType, ALsizei len)
1432 switch(dstType)
1434 (void)Convert_ALbyte;
1435 case FmtUByte:
1436 Convert_ALubyte(dst, src, srcType, len);
1437 break;
1438 case FmtShort:
1439 Convert_ALshort(dst, src, srcType, len);
1440 break;
1441 (void)Convert_ALushort;
1442 case FmtFloat:
1443 Convert_ALfloat(dst, src, srcType, len);
1444 break;
1445 case FmtDouble:
1446 Convert_ALdouble(dst, src, srcType, len);
1447 break;
1452 void DecomposeInputFormat(ALenum format, enum SrcFmtType *type,
1453 enum SrcFmtChannels *order)
1455 switch(format)
1457 case AL_FORMAT_MONO8:
1458 *type = SrcFmtUByte;
1459 *order = SrcFmtMono;
1460 break;
1461 case AL_FORMAT_MONO16:
1462 *type = SrcFmtShort;
1463 *order = SrcFmtMono;
1464 break;
1465 case AL_FORMAT_MONO_FLOAT32:
1466 *type = SrcFmtFloat;
1467 *order = SrcFmtMono;
1468 break;
1469 case AL_FORMAT_MONO_DOUBLE_EXT:
1470 *type = SrcFmtDouble;
1471 *order = SrcFmtMono;
1472 break;
1473 case AL_FORMAT_STEREO8:
1474 *type = SrcFmtUByte;
1475 *order = SrcFmtStereo;
1476 break;
1477 case AL_FORMAT_STEREO16:
1478 *type = SrcFmtShort;
1479 *order = SrcFmtStereo;
1480 break;
1481 case AL_FORMAT_STEREO_FLOAT32:
1482 *type = SrcFmtFloat;
1483 *order = SrcFmtStereo;
1484 break;
1485 case AL_FORMAT_STEREO_DOUBLE_EXT:
1486 *type = SrcFmtDouble;
1487 *order = SrcFmtStereo;
1488 break;
1489 case AL_FORMAT_QUAD8_LOKI:
1490 case AL_FORMAT_QUAD8:
1491 *type = SrcFmtUByte;
1492 *order = SrcFmtQuad;
1493 break;
1494 case AL_FORMAT_QUAD16_LOKI:
1495 case AL_FORMAT_QUAD16:
1496 *type = SrcFmtShort;
1497 *order = SrcFmtQuad;
1498 break;
1499 case AL_FORMAT_QUAD32:
1500 *type = SrcFmtFloat;
1501 *order = SrcFmtQuad;
1502 break;
1503 case AL_FORMAT_51CHN8:
1504 *type = SrcFmtUByte;
1505 *order = SrcFmtX51;
1506 break;
1507 case AL_FORMAT_51CHN16:
1508 *type = SrcFmtShort;
1509 *order = SrcFmtX51;
1510 break;
1511 case AL_FORMAT_51CHN32:
1512 *type = SrcFmtFloat;
1513 *order = SrcFmtX51;
1514 break;
1515 case AL_FORMAT_61CHN8:
1516 *type = SrcFmtUByte;
1517 *order = SrcFmtX61;
1518 break;
1519 case AL_FORMAT_61CHN16:
1520 *type = SrcFmtShort;
1521 *order = SrcFmtX61;
1522 break;
1523 case AL_FORMAT_61CHN32:
1524 *type = SrcFmtFloat;
1525 *order = SrcFmtX61;
1526 break;
1527 case AL_FORMAT_71CHN8:
1528 *type = SrcFmtUByte;
1529 *order = SrcFmtX71;
1530 break;
1531 case AL_FORMAT_71CHN16:
1532 *type = SrcFmtShort;
1533 *order = SrcFmtX71;
1534 break;
1535 case AL_FORMAT_71CHN32:
1536 *type = SrcFmtFloat;
1537 *order = SrcFmtX71;
1538 break;
1540 default:
1541 AL_PRINT("Unhandled format specified: 0x%X\n", format);
1542 abort();
1546 void DecomposeFormat(ALenum format, enum FmtType *type, enum FmtChannels *order)
1548 switch(format)
1550 case AL_FORMAT_MONO8:
1551 *type = FmtUByte;
1552 *order = FmtMono;
1553 break;
1554 case AL_FORMAT_MONO16:
1555 *type = FmtShort;
1556 *order = FmtMono;
1557 break;
1558 case AL_FORMAT_MONO_FLOAT32:
1559 *type = FmtFloat;
1560 *order = FmtMono;
1561 break;
1562 case AL_FORMAT_MONO_DOUBLE_EXT:
1563 *type = FmtDouble;
1564 *order = FmtMono;
1565 break;
1566 case AL_FORMAT_STEREO8:
1567 *type = FmtUByte;
1568 *order = FmtStereo;
1569 break;
1570 case AL_FORMAT_STEREO16:
1571 *type = FmtShort;
1572 *order = FmtStereo;
1573 break;
1574 case AL_FORMAT_STEREO_FLOAT32:
1575 *type = FmtFloat;
1576 *order = FmtStereo;
1577 break;
1578 case AL_FORMAT_STEREO_DOUBLE_EXT:
1579 *type = FmtDouble;
1580 *order = FmtStereo;
1581 break;
1582 case AL_FORMAT_QUAD8_LOKI:
1583 case AL_FORMAT_QUAD8:
1584 *type = FmtUByte;
1585 *order = FmtQuad;
1586 break;
1587 case AL_FORMAT_QUAD16_LOKI:
1588 case AL_FORMAT_QUAD16:
1589 *type = FmtShort;
1590 *order = FmtQuad;
1591 break;
1592 case AL_FORMAT_QUAD32:
1593 *type = FmtFloat;
1594 *order = FmtQuad;
1595 break;
1596 case AL_FORMAT_51CHN8:
1597 *type = FmtUByte;
1598 *order = Fmt51ChanWFX;
1599 break;
1600 case AL_FORMAT_51CHN16:
1601 *type = FmtShort;
1602 *order = Fmt51ChanWFX;
1603 break;
1604 case AL_FORMAT_51CHN32:
1605 *type = FmtFloat;
1606 *order = Fmt51ChanWFX;
1607 break;
1608 case AL_FORMAT_61CHN8:
1609 *type = FmtUByte;
1610 *order = Fmt61ChanWFX;
1611 break;
1612 case AL_FORMAT_61CHN16:
1613 *type = FmtShort;
1614 *order = Fmt61ChanWFX;
1615 break;
1616 case AL_FORMAT_61CHN32:
1617 *type = FmtFloat;
1618 *order = Fmt61ChanWFX;
1619 break;
1620 case AL_FORMAT_71CHN8:
1621 *type = FmtUByte;
1622 *order = Fmt71ChanWFX;
1623 break;
1624 case AL_FORMAT_71CHN16:
1625 *type = FmtShort;
1626 *order = Fmt71ChanWFX;
1627 break;
1628 case AL_FORMAT_71CHN32:
1629 *type = FmtFloat;
1630 *order = Fmt71ChanWFX;
1631 break;
1633 default:
1634 AL_PRINT("Unhandled format specified: 0x%X\n", format);
1635 abort();
1641 * ReleaseALBuffers()
1643 * INTERNAL FN : Called by alcCloseDevice to destroy any buffers that still exist
1645 ALvoid ReleaseALBuffers(ALCdevice *device)
1647 ALsizei i;
1648 for(i = 0;i < device->BufferMap.size;i++)
1650 ALbuffer *temp = device->BufferMap.array[i].value;
1651 device->BufferMap.array[i].value = NULL;
1653 // Release sample data
1654 free(temp->data);
1656 // Release Buffer structure
1657 ALTHUNK_REMOVEENTRY(temp->buffer);
1658 memset(temp, 0, sizeof(ALbuffer));
1659 free(temp);