Remove some duplication
[openal-soft/openal-hmr.git] / OpenAL32 / alBuffer.c
blob33db7cfb57888d21790f3a6cb826316457ce94c9
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 case AL_FORMAT_MONO16:
279 case AL_FORMAT_MONO_FLOAT32:
280 LoadData(ALBuf, data, size, freq, format, AL_FORMAT_MONO16);
281 break;
283 case AL_FORMAT_STEREO8:
284 case AL_FORMAT_STEREO16:
285 case AL_FORMAT_STEREO_FLOAT32:
286 LoadData(ALBuf, data, size, freq, format, AL_FORMAT_STEREO16);
287 break;
289 case AL_FORMAT_MONO_IMA4:
290 // Here is where things vary:
291 // nVidia and Apple use 64+1 samples per block => block_size=36 bytes
292 // Most PC sound software uses 2040+1 samples per block -> block_size=1024 bytes
293 if ((size%36) == 0)
295 // Allocate 8 extra samples (16 bytes)
296 ALBuf->data=realloc(ALBuf->data,16+(size/36)*(65*sizeof(ALshort)));
297 if (ALBuf->data)
299 ALBuf->format = AL_FORMAT_MONO16;
300 ALBuf->eOriginalFormat = AL_FORMAT_MONO_IMA4;
301 IMAData=(ALuint *)data;
302 for (i=0;i<size/36;i++)
304 Sample=((ALshort *)IMAData)[0];
305 Index=((ALshort *)IMAData)[1];
307 Index=Index<0?0:Index;
308 Index=Index>88?88:Index;
310 ALBuf->data[i*65]=(short)Sample;
312 IMAData++;
314 for (j=1;j<65;j+=8)
316 IMACode=*IMAData;
317 for (k=0;k<8;k+=2)
319 Sample+=((g_IMAStep_size[Index]*g_IMACodeword_4[IMACode&15])/8);
320 Index+=g_IMAIndex_adjust_4[IMACode&15];
321 if (Sample<-32768) Sample=-32768;
322 else if (Sample>32767) Sample=32767;
323 if (Index<0) Index=0;
324 else if (Index>88) Index=88;
325 ALBuf->data[i*65+j+k]=(short)Sample;
326 IMACode>>=4;
328 Sample+=((g_IMAStep_size[Index]*g_IMACodeword_4[IMACode&15])/8);
329 Index+=g_IMAIndex_adjust_4[IMACode&15];
330 if (Sample<-32768) Sample=-32768;
331 else if (Sample>32767) Sample=32767;
332 if (Index<0) Index=0;
333 else if (Index>88) Index=88;
334 ALBuf->data[i*65+j+k+1]=(short)Sample;
335 IMACode>>=4;
337 IMAData++;
340 memset(&(ALBuf->data[(size/36*65)]), 0, 16);
341 ALBuf->size=size/36*65*sizeof(ALshort);
342 ALBuf->frequency=freq;
344 else
345 alSetError(AL_OUT_OF_MEMORY);
347 else
348 alSetError(AL_INVALID_VALUE);
349 break;
351 case AL_FORMAT_STEREO_IMA4:
352 // Here is where things vary:
353 // nVidia and Apple use 64+1 samples per channel per block => block_size=72 bytes
354 // Most PC sound software uses 2040+1 samples per channel per block -> block_size=2048 bytes
355 if ((size%72) == 0)
357 // Allocate 8 extra samples (32 bytes)
358 ALBuf->data=realloc(ALBuf->data,32+(size/72)*(2*65*sizeof(ALshort)));
359 if (ALBuf->data)
361 ALBuf->format = AL_FORMAT_STEREO16;
362 ALBuf->eOriginalFormat = AL_FORMAT_STEREO_IMA4;
363 IMAData=(ALuint *)data;
364 for (i=0;i<size/72;i++)
366 LeftSample=((ALshort *)IMAData)[0];
367 LeftIndex=((ALshort *)IMAData)[1];
369 LeftIndex=LeftIndex<0?0:LeftIndex;
370 LeftIndex=LeftIndex>88?88:LeftIndex;
372 ALBuf->data[i*2*65]=(short)LeftSample;
374 IMAData++;
376 RightSample=((ALshort *)IMAData)[0];
377 RightIndex=((ALshort *)IMAData)[1];
379 RightIndex=RightIndex<0?0:RightIndex;
380 RightIndex=RightIndex>88?88:RightIndex;
382 ALBuf->data[i*2*65+1]=(short)RightSample;
384 IMAData++;
386 for (j=2;j<130;j+=16)
388 LeftIMACode=IMAData[0];
389 RightIMACode=IMAData[1];
390 for (k=0;k<16;k+=4)
392 LeftSample+=((g_IMAStep_size[LeftIndex]*g_IMACodeword_4[LeftIMACode&15])/8);
393 LeftIndex+=g_IMAIndex_adjust_4[LeftIMACode&15];
394 if (LeftSample<-32768) LeftSample=-32768;
395 else if (LeftSample>32767) LeftSample=32767;
396 if (LeftIndex<0) LeftIndex=0;
397 else if (LeftIndex>88) LeftIndex=88;
398 ALBuf->data[i*2*65+j+k]=(short)LeftSample;
399 LeftIMACode>>=4;
401 RightSample+=((g_IMAStep_size[RightIndex]*g_IMACodeword_4[RightIMACode&15])/8);
402 RightIndex+=g_IMAIndex_adjust_4[RightIMACode&15];
403 if (RightSample<-32768) RightSample=-32768;
404 else if (RightSample>32767) RightSample=32767;
405 if (RightIndex<0) RightIndex=0;
406 else if (RightIndex>88) RightIndex=88;
407 ALBuf->data[i*2*65+j+k+1]=(short)RightSample;
408 RightIMACode>>=4;
410 LeftSample+=((g_IMAStep_size[LeftIndex]*g_IMACodeword_4[LeftIMACode&15])/8);
411 LeftIndex+=g_IMAIndex_adjust_4[LeftIMACode&15];
412 if (LeftSample<-32768) LeftSample=-32768;
413 else if (LeftSample>32767) LeftSample=32767;
414 if (LeftIndex<0) LeftIndex=0;
415 else if (LeftIndex>88) LeftIndex=88;
416 ALBuf->data[i*2*65+j+k+2]=(short)LeftSample;
417 LeftIMACode>>=4;
419 RightSample+=((g_IMAStep_size[RightIndex]*g_IMACodeword_4[RightIMACode&15])/8);
420 RightIndex+=g_IMAIndex_adjust_4[RightIMACode&15];
421 if (RightSample<-32768) RightSample=-32768;
422 else if (RightSample>32767) RightSample=32767;
423 if (RightIndex<0) RightIndex=0;
424 else if (RightIndex>88) RightIndex=88;
425 ALBuf->data[i*2*65+j+k+3]=(short)RightSample;
426 RightIMACode>>=4;
428 IMAData+=2;
431 memset(&(ALBuf->data[(size/72*2*65)]), 0, 32);
432 ALBuf->size=size/72*2*65*sizeof(ALshort);
433 ALBuf->frequency=freq;
435 else
436 alSetError(AL_OUT_OF_MEMORY);
438 else
439 alSetError(AL_INVALID_VALUE);
440 break;
442 default:
443 alSetError(AL_INVALID_ENUM);
444 break;
447 else
449 // Buffer is in use, or data is a NULL pointer
450 alSetError(AL_INVALID_VALUE);
453 else
455 // Invalid Buffer Name
456 alSetError(AL_INVALID_NAME);
459 ProcessContext(Context);
463 ALAPI void ALAPIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
465 ALCcontext *pContext;
467 (void)flValue;
469 pContext = alcGetCurrentContext();
470 SuspendContext(pContext);
472 if (alIsBuffer(buffer) && (buffer != 0))
474 switch(eParam)
476 default:
477 alSetError(AL_INVALID_ENUM);
478 break;
481 else
483 alSetError(AL_INVALID_NAME);
486 ProcessContext(pContext);
490 ALAPI void ALAPIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
492 ALCcontext *pContext;
494 (void)flValue1;
495 (void)flValue2;
496 (void)flValue3;
498 pContext = alcGetCurrentContext();
499 SuspendContext(pContext);
501 if (alIsBuffer(buffer) && (buffer != 0))
503 switch(eParam)
505 default:
506 alSetError(AL_INVALID_ENUM);
507 break;
510 else
512 alSetError(AL_INVALID_NAME);
515 ProcessContext(pContext);
519 ALAPI void ALAPIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
521 ALCcontext *pContext;
523 (void)flValues;
525 pContext = alcGetCurrentContext();
526 SuspendContext(pContext);
528 if (alIsBuffer(buffer) && (buffer != 0))
530 switch(eParam)
532 default:
533 alSetError(AL_INVALID_ENUM);
534 break;
537 else
539 alSetError(AL_INVALID_NAME);
542 ProcessContext(pContext);
546 ALAPI void ALAPIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
548 ALCcontext *pContext;
550 (void)lValue;
552 pContext = alcGetCurrentContext();
553 SuspendContext(pContext);
555 if (alIsBuffer(buffer) && (buffer != 0))
557 switch(eParam)
559 default:
560 alSetError(AL_INVALID_ENUM);
561 break;
564 else
566 alSetError(AL_INVALID_NAME);
569 ProcessContext(pContext);
573 ALAPI void ALAPIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
575 ALCcontext *pContext;
577 (void)lValue1;
578 (void)lValue2;
579 (void)lValue3;
581 pContext = alcGetCurrentContext();
582 SuspendContext(pContext);
584 if (alIsBuffer(buffer) && (buffer != 0))
586 switch(eParam)
588 default:
589 alSetError(AL_INVALID_ENUM);
590 break;
593 else
595 alSetError(AL_INVALID_NAME);
598 ProcessContext(pContext);
602 ALAPI void ALAPIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
604 ALCcontext *pContext;
606 (void)plValues;
608 pContext = alcGetCurrentContext();
609 SuspendContext(pContext);
611 if (alIsBuffer(buffer) && (buffer != 0))
613 switch(eParam)
615 default:
616 alSetError(AL_INVALID_ENUM);
617 break;
620 else
622 alSetError(AL_INVALID_NAME);
625 ProcessContext(pContext);
629 ALAPI ALvoid ALAPIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
631 ALCcontext *pContext;
633 pContext = alcGetCurrentContext();
634 SuspendContext(pContext);
636 if (pflValue)
638 if (alIsBuffer(buffer) && (buffer != 0))
640 switch(eParam)
642 default:
643 alSetError(AL_INVALID_ENUM);
644 break;
647 else
649 alSetError(AL_INVALID_NAME);
652 else
654 alSetError(AL_INVALID_VALUE);
657 ProcessContext(pContext);
661 ALAPI void ALAPIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
663 ALCcontext *pContext;
665 pContext = alcGetCurrentContext();
666 SuspendContext(pContext);
668 if ((pflValue1) && (pflValue2) && (pflValue3))
670 if (alIsBuffer(buffer) && (buffer != 0))
672 switch(eParam)
674 default:
675 alSetError(AL_INVALID_ENUM);
676 break;
679 else
681 alSetError(AL_INVALID_NAME);
684 else
686 alSetError(AL_INVALID_VALUE);
689 ProcessContext(pContext);
693 ALAPI void ALAPIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
695 ALCcontext *pContext;
697 pContext = alcGetCurrentContext();
698 SuspendContext(pContext);
700 if (pflValues)
702 if (alIsBuffer(buffer) && (buffer != 0))
704 switch(eParam)
706 default:
707 alSetError(AL_INVALID_ENUM);
708 break;
711 else
713 alSetError(AL_INVALID_NAME);
716 else
718 alSetError(AL_INVALID_VALUE);
721 ProcessContext(pContext);
725 ALAPI ALvoid ALAPIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
727 ALCcontext *pContext;
728 ALbuffer *pBuffer;
730 pContext = alcGetCurrentContext();
731 SuspendContext(pContext);
733 if (plValue)
735 if (alIsBuffer(buffer) && (buffer != 0))
737 pBuffer = ((ALbuffer *)ALTHUNK_LOOKUPENTRY(buffer));
739 switch (eParam)
741 case AL_FREQUENCY:
742 *plValue = pBuffer->frequency;
743 break;
745 case AL_BITS:
746 *plValue = aluBytesFromFormat(pBuffer->format) * 8;
747 break;
749 case AL_CHANNELS:
750 *plValue = aluChannelsFromFormat(pBuffer->format);
751 break;
753 case AL_SIZE:
754 *plValue = pBuffer->size;
755 break;
757 default:
758 alSetError(AL_INVALID_ENUM);
759 break;
762 else
764 alSetError(AL_INVALID_NAME);
767 else
769 alSetError(AL_INVALID_VALUE);
772 ProcessContext(pContext);
776 ALAPI void ALAPIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
778 ALCcontext *pContext;
780 pContext = alcGetCurrentContext();
781 SuspendContext(pContext);
783 if ((plValue1) && (plValue2) && (plValue3))
785 if (alIsBuffer(buffer) && (buffer != 0))
787 switch(eParam)
789 default:
790 alSetError(AL_INVALID_ENUM);
791 break;
794 else
796 alSetError(AL_INVALID_NAME);
799 else
801 alSetError(AL_INVALID_VALUE);
804 ProcessContext(pContext);
808 ALAPI void ALAPIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
810 ALCcontext *pContext;
812 pContext = alcGetCurrentContext();
813 SuspendContext(pContext);
815 if (plValues)
817 if (alIsBuffer(buffer) && (buffer != 0))
819 switch (eParam)
821 case AL_FREQUENCY:
822 case AL_BITS:
823 case AL_CHANNELS:
824 case AL_SIZE:
825 alGetBufferi(buffer, eParam, plValues);
826 break;
828 default:
829 alSetError(AL_INVALID_ENUM);
830 break;
833 else
835 alSetError(AL_INVALID_NAME);
838 else
840 alSetError(AL_INVALID_VALUE);
843 ProcessContext(pContext);
847 * LoadData
849 * Loads the specified data into the buffer, using the specified formats.
850 * Currently, the new format must be 16-bit, and must have the same channel
851 * configuration as the original format. This does NOT handle compressed
852 * formats (eg. IMA4).
854 static void LoadData(ALbuffer *ALBuf, const ALubyte *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat)
856 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
857 ALuint OrigBytes = aluBytesFromFormat(OrigFormat);
858 ALuint OrigChannels = aluChannelsFromFormat(OrigFormat);
859 ALsizei i;
861 assert(aluBytesFromFormat(NewFormat) == 2);
862 assert(NewChannels == OrigChannels);
864 if ((size%(OrigBytes*OrigChannels)) != 0)
866 alSetError(AL_INVALID_VALUE);
867 return;
870 switch(OrigBytes)
872 case 1:
873 size /= sizeof(ALubyte);
875 // 8bit Samples are converted to 16 bit here
876 // Allocate 8 extra samples
877 ALBuf->data = realloc(ALBuf->data, (8*NewChannels + size) * (1*sizeof(ALshort)));
878 if (ALBuf->data)
880 for (i = 0;i < size;i++)
881 ALBuf->data[i] = (ALshort)((data[i]-128) << 8);
882 memset(&(ALBuf->data[size]), 0, 16*NewChannels);
884 ALBuf->format = NewFormat;
885 ALBuf->eOriginalFormat = OrigFormat;
886 ALBuf->size = size*1*sizeof(ALshort);
887 ALBuf->frequency = freq;
889 else
890 alSetError(AL_OUT_OF_MEMORY);
891 break;
893 case 2:
894 size /= sizeof(ALshort);
896 // Allocate 8 extra samples
897 ALBuf->data = realloc(ALBuf->data, (8*NewChannels + size) * (1*sizeof(ALshort)));
898 if (ALBuf->data)
900 memcpy(ALBuf->data, data, size*1*sizeof(ALshort));
901 memset(&(ALBuf->data[size]), 0, 16*NewChannels);
903 ALBuf->format = NewFormat;
904 ALBuf->eOriginalFormat = OrigFormat;
905 ALBuf->size = size*1*sizeof(ALshort);
906 ALBuf->frequency = freq;
908 else
909 alSetError(AL_OUT_OF_MEMORY);
910 break;
912 case 4:
913 size /= sizeof(ALfloat);
915 // Allocate 8 extra samples
916 ALBuf->data = realloc(ALBuf->data, (8*NewChannels + size) * (1*sizeof(ALshort)));
917 if (ALBuf->data)
919 for (i = 0;i < size;i++)
920 ALBuf->data[i] = (ALshort)(((ALfloat*)data)[i] * 32767.5f - 0.5);
921 memset(&(ALBuf->data[size]), 0, 16*NewChannels);
923 ALBuf->format = NewFormat;
924 ALBuf->eOriginalFormat = OrigFormat;
925 ALBuf->size = size*1*sizeof(ALshort);
926 ALBuf->frequency = freq;
928 else
929 alSetError(AL_OUT_OF_MEMORY);
930 break;
932 default:
933 assert(0);
939 * ReleaseALBuffers()
941 * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
943 ALvoid ReleaseALBuffers(ALvoid)
945 ALbuffer *ALBuffer;
946 ALbuffer *ALBufferTemp;
948 #ifdef _DEBUG
949 if(g_uiBufferCount > 0)
950 AL_PRINT("exit() %d Buffer(s) NOT deleted\n", g_uiBufferCount);
951 #endif
953 ALBuffer = g_pBuffers;
954 while(ALBuffer)
956 // Release sample data
957 free(ALBuffer->data);
959 // Release Buffer structure
960 ALBufferTemp = ALBuffer;
961 ALBuffer = ALBuffer->next;
962 memset(ALBufferTemp, 0, sizeof(ALbuffer));
963 free(ALBufferTemp);
965 g_pBuffers = NULL;
966 g_uiBufferCount = 0;