Implement AL_EXT_FLOAT32
[openal-soft.git] / OpenAL32 / alBuffer.c
blobd1a8c359014f8a8d67e2c6f803eaae0a1e53edbc
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 #define _CRT_SECURE_NO_DEPRECATE // get rid of sprintf security warnings on VS2005
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include "alMain.h"
26 #include "AL/al.h"
27 #include "AL/alc.h"
28 #include "alError.h"
29 #include "alBuffer.h"
32 static void LoadData(ALbuffer *ALBuf, const ALubyte *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat);
35 * AL Buffer Functions
37 * AL Buffers are shared amoung Contexts, so we store the list of generated Buffers
38 * as a global variable in this module. (A valid context is not required to make
39 * AL Buffer function calls
44 * Global Variables
47 static ALbuffer *g_pBuffers = NULL; // Linked List of Buffers
48 static ALuint g_uiBufferCount = 0; // Buffer Count
50 static const long g_IMAStep_size[89]={ // IMA ADPCM Stepsize table
51 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
52 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143,
53 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
54 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
55 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,11487,12635,13899,
56 15289,16818,18500,20350,22358,24633,27086,29794,32767
59 static const long g_IMACodeword_4[16]={ // IMA4 ADPCM Codeword decode table
60 1, 3, 5, 7, 9, 11, 13, 15,
61 -1,-3,-5,-7,-9,-11,-13,-15,
64 static const long g_IMAIndex_adjust_4[16]={ // IMA4 ADPCM Step index adjust decode table
65 -1,-1,-1,-1, 2, 4, 6, 8,
66 -1,-1,-1,-1, 2, 4, 6, 8
70 * alGenBuffers(ALsizei n, ALuint *puiBuffers)
72 * Generates n AL Buffers, and stores the Buffers Names in the array pointed to by puiBuffers
74 ALAPI ALvoid ALAPIENTRY alGenBuffers(ALsizei n,ALuint *puiBuffers)
76 ALCcontext *Context;
77 ALsizei i=0;
79 Context = alcGetCurrentContext();
80 SuspendContext(Context);
82 // Check that we are actually generation some Buffers
83 if (n > 0)
85 // Check the pointer is valid (and points to enough memory to store Buffer Names)
86 if (!IsBadWritePtr((void*)puiBuffers, n * sizeof(ALuint)))
88 ALbuffer **list = &g_pBuffers;
89 while(*list)
90 list = &(*list)->next;
92 // Create all the new Buffers
93 while(i < n)
95 *list = calloc(1, sizeof(ALbuffer));
96 if(*list)
98 puiBuffers[i] = (ALuint)ALTHUNK_ADDENTRY(*list);
99 (*list)->state = UNUSED;
100 g_uiBufferCount++;
101 i++;
103 list = &(*list)->next;
107 // If we didn't create all the Buffers, we must have run out of memory
108 if (i != n)
109 alSetError(AL_OUT_OF_MEMORY);
111 else
113 // Pointer does not point to enough memory to write Buffer names
114 alSetError(AL_INVALID_VALUE);
118 ProcessContext(Context);
120 return;
124 * alDeleteBuffers(ALsizei n, ALuint *puiBuffers)
126 * Deletes the n AL Buffers pointed to by puiBuffers
128 ALAPI ALvoid ALAPIENTRY alDeleteBuffers(ALsizei n, const ALuint *puiBuffers)
130 ALCcontext *Context;
131 ALbuffer *ALBuf;
132 ALsizei i;
133 ALboolean bFailed = AL_FALSE;
135 Context = alcGetCurrentContext();
136 SuspendContext(Context);
138 // Check we are actually Deleting some Buffers
139 if (n >= 0)
141 if ((ALuint)n <= g_uiBufferCount)
143 // Check that all the buffers are valid and can actually be deleted
144 for (i = 0; i < n; i++)
146 // Check for valid Buffer ID (can be NULL buffer)
147 if (alIsBuffer(puiBuffers[i]))
149 // If not the NULL buffer, check that the reference count is 0
150 ALBuf = ((ALbuffer *)ALTHUNK_LOOKUPENTRY(puiBuffers[i]));
151 if (ALBuf)
153 if (ALBuf->refcount != 0)
155 // Buffer still in use, cannot be deleted
156 alSetError(AL_INVALID_OPERATION);
157 bFailed = AL_TRUE;
161 else
163 // Invalid Buffer
164 alSetError(AL_INVALID_NAME);
165 bFailed = AL_TRUE;
169 // If all the Buffers were valid (and have Reference Counts of 0), then we can delete them
170 if (!bFailed)
172 for (i = 0; i < n; i++)
174 ALBuf=((ALbuffer *)ALTHUNK_LOOKUPENTRY(puiBuffers[i]));
175 if (ALBuf)
177 ALbuffer **list = &g_pBuffers;
178 while(*list && *list != ALBuf)
179 list = &(*list)->next;
181 if(*list)
182 *list = (*list)->next;
184 // Release the memory used to store audio data
185 free(ALBuf->data);
187 // Release buffer structure
188 ALTHUNK_REMOVEENTRY(puiBuffers[i]);
189 memset(ALBuf, 0, sizeof(ALbuffer));
190 g_uiBufferCount--;
191 free(ALBuf);
196 else
197 alSetError(AL_INVALID_NAME);
199 else
200 alSetError(AL_INVALID_VALUE);
202 ProcessContext(Context);
204 return;
209 * alIsBuffer(ALuint uiBuffer)
211 * Checks if ulBuffer is a valid Buffer Name
213 ALAPI ALboolean ALAPIENTRY alIsBuffer(ALuint uiBuffer)
215 ALCcontext *Context;
216 ALboolean result=AL_FALSE;
217 ALbuffer *ALBuf;
218 ALbuffer *TgtALBuf;
220 Context = alcGetCurrentContext();
221 SuspendContext(Context);
223 if (uiBuffer)
225 TgtALBuf = (ALbuffer *)ALTHUNK_LOOKUPENTRY(uiBuffer);
227 // Check through list of generated buffers for uiBuffer
228 ALBuf = g_pBuffers;
229 while (ALBuf)
231 if (ALBuf == TgtALBuf)
233 result = AL_TRUE;
234 break;
237 ALBuf = ALBuf->next;
240 else
242 result = AL_TRUE;
246 ProcessContext(Context);
248 return result;
252 * alBufferData(ALuint buffer,ALenum format,ALvoid *data,ALsizei size,ALsizei freq)
254 * Fill buffer with audio data
256 ALAPI ALvoid ALAPIENTRY alBufferData(ALuint buffer,ALenum format,const ALvoid *data,ALsizei size,ALsizei freq)
258 ALuint *IMAData,IMACode;
259 ALCcontext *Context;
260 ALint Sample,Index;
261 ALint LeftSample,LeftIndex;
262 ALint RightSample,RightIndex;
263 ALuint LeftIMACode,RightIMACode;
264 ALbuffer *ALBuf;
265 ALsizei i,j,k;
267 Context = alcGetCurrentContext();
268 SuspendContext(Context);
270 if (alIsBuffer(buffer) && (buffer != 0))
272 ALBuf=((ALbuffer *)ALTHUNK_LOOKUPENTRY(buffer));
273 if ((ALBuf->refcount==0)&&(data))
275 switch(format)
277 case AL_FORMAT_MONO8:
278 LoadData(ALBuf, data, size, freq, format, AL_FORMAT_MONO16);
279 break;
281 case AL_FORMAT_MONO16:
282 LoadData(ALBuf, data, size, freq, format, AL_FORMAT_MONO16);
283 break;
285 case AL_FORMAT_MONO_FLOAT32:
286 LoadData(ALBuf, data, size, freq, format, AL_FORMAT_MONO16);
287 break;
289 case AL_FORMAT_STEREO8:
290 LoadData(ALBuf, data, size, freq, format, AL_FORMAT_STEREO16);
291 break;
293 case AL_FORMAT_STEREO16:
294 LoadData(ALBuf, data, size, freq, format, AL_FORMAT_STEREO16);
295 break;
297 case AL_FORMAT_STEREO_FLOAT32:
298 LoadData(ALBuf, data, size, freq, format, AL_FORMAT_STEREO16);
299 break;
301 case AL_FORMAT_MONO_IMA4:
302 // Here is where things vary:
303 // nVidia and Apple use 64+1 samples per block => block_size=36 bytes
304 // Most PC sound software uses 2040+1 samples per block -> block_size=1024 bytes
305 if ((size%36) == 0)
307 // Allocate 8 extra samples (16 bytes)
308 ALBuf->data=realloc(ALBuf->data,16+(size/36)*(65*sizeof(ALshort)));
309 if (ALBuf->data)
311 ALBuf->format = AL_FORMAT_MONO16;
312 ALBuf->eOriginalFormat = AL_FORMAT_MONO_IMA4;
313 IMAData=(ALuint *)data;
314 for (i=0;i<size/36;i++)
316 Sample=((ALshort *)IMAData)[0];
317 Index=((ALshort *)IMAData)[1];
319 Index=Index<0?0:Index;
320 Index=Index>88?88:Index;
322 ALBuf->data[i*65]=(short)Sample;
324 IMAData++;
326 for (j=1;j<65;j+=8)
328 IMACode=*IMAData;
329 for (k=0;k<8;k+=2)
331 Sample+=((g_IMAStep_size[Index]*g_IMACodeword_4[IMACode&15])/8);
332 Index+=g_IMAIndex_adjust_4[IMACode&15];
333 if (Sample<-32768) Sample=-32768;
334 else if (Sample>32767) Sample=32767;
335 if (Index<0) Index=0;
336 else if (Index>88) Index=88;
337 ALBuf->data[i*65+j+k]=(short)Sample;
338 IMACode>>=4;
340 Sample+=((g_IMAStep_size[Index]*g_IMACodeword_4[IMACode&15])/8);
341 Index+=g_IMAIndex_adjust_4[IMACode&15];
342 if (Sample<-32768) Sample=-32768;
343 else if (Sample>32767) Sample=32767;
344 if (Index<0) Index=0;
345 else if (Index>88) Index=88;
346 ALBuf->data[i*65+j+k+1]=(short)Sample;
347 IMACode>>=4;
349 IMAData++;
352 memset(&(ALBuf->data[(size/36*65)]), 0, 16);
353 ALBuf->size=size/36*65*sizeof(ALshort);
354 ALBuf->frequency=freq;
356 else
357 alSetError(AL_OUT_OF_MEMORY);
359 else
360 alSetError(AL_INVALID_VALUE);
361 break;
363 case AL_FORMAT_STEREO_IMA4:
364 // Here is where things vary:
365 // nVidia and Apple use 64+1 samples per channel per block => block_size=72 bytes
366 // Most PC sound software uses 2040+1 samples per channel per block -> block_size=2048 bytes
367 if ((size%72) == 0)
369 // Allocate 8 extra samples (32 bytes)
370 ALBuf->data=realloc(ALBuf->data,32+(size/72)*(2*65*sizeof(ALshort)));
371 if (ALBuf->data)
373 ALBuf->format = AL_FORMAT_STEREO16;
374 ALBuf->eOriginalFormat = AL_FORMAT_STEREO_IMA4;
375 IMAData=(ALuint *)data;
376 for (i=0;i<size/72;i++)
378 LeftSample=((ALshort *)IMAData)[0];
379 LeftIndex=((ALshort *)IMAData)[1];
381 LeftIndex=LeftIndex<0?0:LeftIndex;
382 LeftIndex=LeftIndex>88?88:LeftIndex;
384 ALBuf->data[i*2*65]=(short)LeftSample;
386 IMAData++;
388 RightSample=((ALshort *)IMAData)[0];
389 RightIndex=((ALshort *)IMAData)[1];
391 RightIndex=RightIndex<0?0:RightIndex;
392 RightIndex=RightIndex>88?88:RightIndex;
394 ALBuf->data[i*2*65+1]=(short)RightSample;
396 IMAData++;
398 for (j=2;j<130;j+=16)
400 LeftIMACode=IMAData[0];
401 RightIMACode=IMAData[1];
402 for (k=0;k<16;k+=4)
404 LeftSample+=((g_IMAStep_size[LeftIndex]*g_IMACodeword_4[LeftIMACode&15])/8);
405 LeftIndex+=g_IMAIndex_adjust_4[LeftIMACode&15];
406 if (LeftSample<-32768) LeftSample=-32768;
407 else if (LeftSample>32767) LeftSample=32767;
408 if (LeftIndex<0) LeftIndex=0;
409 else if (LeftIndex>88) LeftIndex=88;
410 ALBuf->data[i*2*65+j+k]=(short)LeftSample;
411 LeftIMACode>>=4;
413 RightSample+=((g_IMAStep_size[RightIndex]*g_IMACodeword_4[RightIMACode&15])/8);
414 RightIndex+=g_IMAIndex_adjust_4[RightIMACode&15];
415 if (RightSample<-32768) RightSample=-32768;
416 else if (RightSample>32767) RightSample=32767;
417 if (RightIndex<0) RightIndex=0;
418 else if (RightIndex>88) RightIndex=88;
419 ALBuf->data[i*2*65+j+k+1]=(short)RightSample;
420 RightIMACode>>=4;
422 LeftSample+=((g_IMAStep_size[LeftIndex]*g_IMACodeword_4[LeftIMACode&15])/8);
423 LeftIndex+=g_IMAIndex_adjust_4[LeftIMACode&15];
424 if (LeftSample<-32768) LeftSample=-32768;
425 else if (LeftSample>32767) LeftSample=32767;
426 if (LeftIndex<0) LeftIndex=0;
427 else if (LeftIndex>88) LeftIndex=88;
428 ALBuf->data[i*2*65+j+k+2]=(short)LeftSample;
429 LeftIMACode>>=4;
431 RightSample+=((g_IMAStep_size[RightIndex]*g_IMACodeword_4[RightIMACode&15])/8);
432 RightIndex+=g_IMAIndex_adjust_4[RightIMACode&15];
433 if (RightSample<-32768) RightSample=-32768;
434 else if (RightSample>32767) RightSample=32767;
435 if (RightIndex<0) RightIndex=0;
436 else if (RightIndex>88) RightIndex=88;
437 ALBuf->data[i*2*65+j+k+3]=(short)RightSample;
438 RightIMACode>>=4;
440 IMAData+=2;
443 memset(&(ALBuf->data[(size/72*2*65)]), 0, 32);
444 ALBuf->size=size/72*2*65*sizeof(ALshort);
445 ALBuf->frequency=freq;
447 else
448 alSetError(AL_OUT_OF_MEMORY);
450 else
451 alSetError(AL_INVALID_VALUE);
452 break;
454 default:
455 alSetError(AL_INVALID_ENUM);
456 break;
459 else
461 // Buffer is in use, or data is a NULL pointer
462 alSetError(AL_INVALID_VALUE);
465 else
467 // Invalid Buffer Name
468 alSetError(AL_INVALID_NAME);
471 ProcessContext(Context);
475 ALAPI void ALAPIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
477 ALCcontext *pContext;
479 (void)flValue;
481 pContext = alcGetCurrentContext();
482 SuspendContext(pContext);
484 if (alIsBuffer(buffer) && (buffer != 0))
486 switch(eParam)
488 default:
489 alSetError(AL_INVALID_ENUM);
490 break;
493 else
495 alSetError(AL_INVALID_NAME);
498 ProcessContext(pContext);
502 ALAPI void ALAPIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
504 ALCcontext *pContext;
506 (void)flValue1;
507 (void)flValue2;
508 (void)flValue3;
510 pContext = alcGetCurrentContext();
511 SuspendContext(pContext);
513 if (alIsBuffer(buffer) && (buffer != 0))
515 switch(eParam)
517 default:
518 alSetError(AL_INVALID_ENUM);
519 break;
522 else
524 alSetError(AL_INVALID_NAME);
527 ProcessContext(pContext);
531 ALAPI void ALAPIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
533 ALCcontext *pContext;
535 (void)flValues;
537 pContext = alcGetCurrentContext();
538 SuspendContext(pContext);
540 if (alIsBuffer(buffer) && (buffer != 0))
542 switch(eParam)
544 default:
545 alSetError(AL_INVALID_ENUM);
546 break;
549 else
551 alSetError(AL_INVALID_NAME);
554 ProcessContext(pContext);
558 ALAPI void ALAPIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
560 ALCcontext *pContext;
562 (void)lValue;
564 pContext = alcGetCurrentContext();
565 SuspendContext(pContext);
567 if (alIsBuffer(buffer) && (buffer != 0))
569 switch(eParam)
571 default:
572 alSetError(AL_INVALID_ENUM);
573 break;
576 else
578 alSetError(AL_INVALID_NAME);
581 ProcessContext(pContext);
585 ALAPI void ALAPIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
587 ALCcontext *pContext;
589 (void)lValue1;
590 (void)lValue2;
591 (void)lValue3;
593 pContext = alcGetCurrentContext();
594 SuspendContext(pContext);
596 if (alIsBuffer(buffer) && (buffer != 0))
598 switch(eParam)
600 default:
601 alSetError(AL_INVALID_ENUM);
602 break;
605 else
607 alSetError(AL_INVALID_NAME);
610 ProcessContext(pContext);
614 ALAPI void ALAPIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
616 ALCcontext *pContext;
618 (void)plValues;
620 pContext = alcGetCurrentContext();
621 SuspendContext(pContext);
623 if (alIsBuffer(buffer) && (buffer != 0))
625 switch(eParam)
627 default:
628 alSetError(AL_INVALID_ENUM);
629 break;
632 else
634 alSetError(AL_INVALID_NAME);
637 ProcessContext(pContext);
641 ALAPI ALvoid ALAPIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
643 ALCcontext *pContext;
645 pContext = alcGetCurrentContext();
646 SuspendContext(pContext);
648 if (pflValue)
650 if (alIsBuffer(buffer) && (buffer != 0))
652 switch(eParam)
654 default:
655 alSetError(AL_INVALID_ENUM);
656 break;
659 else
661 alSetError(AL_INVALID_NAME);
664 else
666 alSetError(AL_INVALID_VALUE);
669 ProcessContext(pContext);
673 ALAPI void ALAPIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
675 ALCcontext *pContext;
677 pContext = alcGetCurrentContext();
678 SuspendContext(pContext);
680 if ((pflValue1) && (pflValue2) && (pflValue3))
682 if (alIsBuffer(buffer) && (buffer != 0))
684 switch(eParam)
686 default:
687 alSetError(AL_INVALID_ENUM);
688 break;
691 else
693 alSetError(AL_INVALID_NAME);
696 else
698 alSetError(AL_INVALID_VALUE);
701 ProcessContext(pContext);
705 ALAPI void ALAPIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
707 ALCcontext *pContext;
709 pContext = alcGetCurrentContext();
710 SuspendContext(pContext);
712 if (pflValues)
714 if (alIsBuffer(buffer) && (buffer != 0))
716 switch(eParam)
718 default:
719 alSetError(AL_INVALID_ENUM);
720 break;
723 else
725 alSetError(AL_INVALID_NAME);
728 else
730 alSetError(AL_INVALID_VALUE);
733 ProcessContext(pContext);
737 ALAPI ALvoid ALAPIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
739 ALCcontext *pContext;
740 ALbuffer *pBuffer;
742 pContext = alcGetCurrentContext();
743 SuspendContext(pContext);
745 if (plValue)
747 if (alIsBuffer(buffer) && (buffer != 0))
749 pBuffer = ((ALbuffer *)ALTHUNK_LOOKUPENTRY(buffer));
751 switch (eParam)
753 case AL_FREQUENCY:
754 *plValue = pBuffer->frequency;
755 break;
757 case AL_BITS:
758 *plValue = aluBytesFromFormat(pBuffer->format) * 8;
759 break;
761 case AL_CHANNELS:
762 *plValue = aluChannelsFromFormat(pBuffer->format);
763 break;
765 case AL_SIZE:
766 *plValue = pBuffer->size;
767 break;
769 default:
770 alSetError(AL_INVALID_ENUM);
771 break;
774 else
776 alSetError(AL_INVALID_NAME);
779 else
781 alSetError(AL_INVALID_VALUE);
784 ProcessContext(pContext);
788 ALAPI void ALAPIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
790 ALCcontext *pContext;
792 pContext = alcGetCurrentContext();
793 SuspendContext(pContext);
795 if ((plValue1) && (plValue2) && (plValue3))
797 if (alIsBuffer(buffer) && (buffer != 0))
799 switch(eParam)
801 default:
802 alSetError(AL_INVALID_ENUM);
803 break;
806 else
808 alSetError(AL_INVALID_NAME);
811 else
813 alSetError(AL_INVALID_VALUE);
816 ProcessContext(pContext);
820 ALAPI void ALAPIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
822 ALCcontext *pContext;
824 pContext = alcGetCurrentContext();
825 SuspendContext(pContext);
827 if (plValues)
829 if (alIsBuffer(buffer) && (buffer != 0))
831 switch (eParam)
833 case AL_FREQUENCY:
834 case AL_BITS:
835 case AL_CHANNELS:
836 case AL_SIZE:
837 alGetBufferi(buffer, eParam, plValues);
838 break;
840 default:
841 alSetError(AL_INVALID_ENUM);
842 break;
845 else
847 alSetError(AL_INVALID_NAME);
850 else
852 alSetError(AL_INVALID_VALUE);
855 ProcessContext(pContext);
859 * LoadData
861 * Loads the specified data into the buffer, using the specified formats.
862 * Currently, the new format must be 16-bit, and must have the same channel
863 * configuration as the original format. This does NOT handle compressed
864 * formats (eg. IMA4).
866 static void LoadData(ALbuffer *ALBuf, const ALubyte *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat)
868 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
869 ALuint OrigBytes = aluBytesFromFormat(OrigFormat);
870 ALuint OrigChannels = aluChannelsFromFormat(OrigFormat);
871 ALsizei i;
873 assert(aluBytesFromFormat(NewFormat) == 2);
874 assert(NewChannels == OrigChannels);
876 if ((size%(OrigBytes*OrigChannels)) != 0)
878 alSetError(AL_INVALID_VALUE);
879 return;
882 switch(OrigBytes)
884 case 1:
885 size /= sizeof(ALubyte);
887 // 8bit Samples are converted to 16 bit here
888 // Allocate 8 extra samples
889 ALBuf->data = realloc(ALBuf->data, (8*NewChannels + size) * (1*sizeof(ALshort)));
890 if (ALBuf->data)
892 for (i = 0;i < size;i++)
893 ALBuf->data[i] = (ALshort)((data[i]-128) << 8);
894 memset(&(ALBuf->data[size]), 0, 16*NewChannels);
896 ALBuf->format = NewFormat;
897 ALBuf->eOriginalFormat = OrigFormat;
898 ALBuf->size = size*1*sizeof(ALshort);
899 ALBuf->frequency = freq;
901 else
902 alSetError(AL_OUT_OF_MEMORY);
903 break;
905 case 2:
906 size /= sizeof(ALshort);
908 // Allocate 8 extra samples
909 ALBuf->data = realloc(ALBuf->data, (8*NewChannels + size) * (1*sizeof(ALshort)));
910 if (ALBuf->data)
912 memcpy(ALBuf->data, data, size*1*sizeof(ALshort));
913 memset(&(ALBuf->data[size]), 0, 16*NewChannels);
915 ALBuf->format = NewFormat;
916 ALBuf->eOriginalFormat = OrigFormat;
917 ALBuf->size = size*1*sizeof(ALshort);
918 ALBuf->frequency = freq;
920 else
921 alSetError(AL_OUT_OF_MEMORY);
922 break;
924 case 4:
925 size /= sizeof(ALfloat);
927 // Allocate 8 extra samples
928 ALBuf->data = realloc(ALBuf->data, (8*NewChannels + size) * (1*sizeof(ALshort)));
929 if (ALBuf->data)
931 for (i = 0;i < size;i++)
932 ALBuf->data[i] = (ALshort)(((ALfloat*)data)[i] * 32767.5f - 0.5);
933 memset(&(ALBuf->data[size]), 0, 16*NewChannels);
935 ALBuf->format = NewFormat;
936 ALBuf->eOriginalFormat = OrigFormat;
937 ALBuf->size = size*1*sizeof(ALshort);
938 ALBuf->frequency = freq;
940 else
941 alSetError(AL_OUT_OF_MEMORY);
942 break;
944 default:
945 assert(0);
951 * ReleaseALBuffers()
953 * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
955 ALvoid ReleaseALBuffers(ALvoid)
957 ALbuffer *ALBuffer;
958 ALbuffer *ALBufferTemp;
960 #ifdef _DEBUG
961 if(g_uiBufferCount > 0)
962 AL_PRINT("exit() %d Buffer(s) NOT deleted\n", g_uiBufferCount);
963 #endif
965 ALBuffer = g_pBuffers;
966 while(ALBuffer)
968 // Release sample data
969 free(ALBuffer->data);
971 // Release Buffer structure
972 ALBufferTemp = ALBuffer;
973 ALBuffer = ALBuffer->next;
974 memset(ALBufferTemp, 0, sizeof(ALbuffer));
975 free(ALBufferTemp);
977 g_pBuffers = NULL;
978 g_uiBufferCount = 0;