Rename some struct members for consistency
[openal-soft.git] / OpenAL32 / alBuffer.c
blob2c909653688f874174739c7308c1f0f804e17a28
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 "alMain.h"
27 #include "AL/al.h"
28 #include "AL/alc.h"
29 #include "alError.h"
30 #include "alBuffer.h"
31 #include "alDatabuffer.h"
32 #include "alThunk.h"
35 static void LoadData(ALbuffer *ALBuf, const ALubyte *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat);
36 static void ConvertData(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len);
37 static void ConvertDataRear(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len);
38 static void ConvertDataIMA4(ALfloat *dst, const ALvoid *src, ALint origChans, ALsizei len);
39 static void ConvertDataMULaw(ALfloat *dst, const ALvoid *src, ALsizei len);
40 static void ConvertDataMULawRear(ALfloat *dst, const ALvoid *src, ALsizei len);
43 * Global Variables
46 static const long g_IMAStep_size[89]={ // IMA ADPCM Stepsize table
47 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
48 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143,
49 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
50 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
51 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442,11487,12635,13899,
52 15289,16818,18500,20350,22358,24633,27086,29794,32767
55 static const long g_IMACodeword_4[16]={ // IMA4 ADPCM Codeword decode table
56 1, 3, 5, 7, 9, 11, 13, 15,
57 -1,-3,-5,-7,-9,-11,-13,-15,
60 static const long g_IMAIndex_adjust_4[16]={ // IMA4 ADPCM Step index adjust decode table
61 -1,-1,-1,-1, 2, 4, 6, 8,
62 -1,-1,-1,-1, 2, 4, 6, 8
65 static const ALshort muLawDecompressionTable[256] = {
66 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
67 -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
68 -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
69 -11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
70 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
71 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
72 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
73 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
74 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
75 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
76 -876, -844, -812, -780, -748, -716, -684, -652,
77 -620, -588, -556, -524, -492, -460, -428, -396,
78 -372, -356, -340, -324, -308, -292, -276, -260,
79 -244, -228, -212, -196, -180, -164, -148, -132,
80 -120, -112, -104, -96, -88, -80, -72, -64,
81 -56, -48, -40, -32, -24, -16, -8, 0,
82 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
83 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
84 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
85 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
86 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
87 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
88 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
89 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
90 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
91 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
92 876, 844, 812, 780, 748, 716, 684, 652,
93 620, 588, 556, 524, 492, 460, 428, 396,
94 372, 356, 340, 324, 308, 292, 276, 260,
95 244, 228, 212, 196, 180, 164, 148, 132,
96 120, 112, 104, 96, 88, 80, 72, 64,
97 56, 48, 40, 32, 24, 16, 8, 0
101 * alGenBuffers(ALsizei n, ALuint *puiBuffers)
103 * Generates n AL Buffers, and stores the Buffers Names in the array pointed to by puiBuffers
105 ALAPI ALvoid ALAPIENTRY alGenBuffers(ALsizei n,ALuint *puiBuffers)
107 ALCcontext *Context;
108 ALsizei i=0;
110 Context = GetContextSuspended();
111 if(!Context) return;
113 // Check that we are actually generation some Buffers
114 if (n > 0)
116 ALCdevice *device = Context->Device;
118 // Check the pointer is valid (and points to enough memory to store Buffer Names)
119 if (!IsBadWritePtr((void*)puiBuffers, n * sizeof(ALuint)))
121 ALbuffer **list = &device->BufferList;
122 while(*list)
123 list = &(*list)->next;
125 // Create all the new Buffers
126 while(i < n)
128 *list = calloc(1, sizeof(ALbuffer));
129 if(!(*list))
131 alDeleteBuffers(i, puiBuffers);
132 alSetError(AL_OUT_OF_MEMORY);
133 break;
136 puiBuffers[i] = (ALuint)ALTHUNK_ADDENTRY(*list);
137 (*list)->buffer = puiBuffers[i];
139 device->BufferCount++;
140 i++;
142 list = &(*list)->next;
145 else
147 // Pointer does not point to enough memory to write Buffer names
148 alSetError(AL_INVALID_VALUE);
152 ProcessContext(Context);
156 * alDeleteBuffers(ALsizei n, ALuint *puiBuffers)
158 * Deletes the n AL Buffers pointed to by puiBuffers
160 ALAPI ALvoid ALAPIENTRY alDeleteBuffers(ALsizei n, const ALuint *puiBuffers)
162 ALCcontext *Context;
163 ALbuffer *ALBuf;
164 ALsizei i;
165 ALboolean bFailed = AL_FALSE;
167 Context = GetContextSuspended();
168 if(!Context) return;
170 // Check we are actually Deleting some Buffers
171 if (n >= 0)
173 ALCdevice *device = Context->Device;
175 // Check that all the buffers are valid and can actually be deleted
176 for (i = 0; i < n; i++)
178 // Check for valid Buffer ID (can be NULL buffer)
179 if (alIsBuffer(puiBuffers[i]))
181 // If not the NULL buffer, check that the reference count is 0
182 ALBuf = ((ALbuffer *)ALTHUNK_LOOKUPENTRY(puiBuffers[i]));
183 if (ALBuf)
185 if (ALBuf->refcount != 0)
187 // Buffer still in use, cannot be deleted
188 alSetError(AL_INVALID_OPERATION);
189 bFailed = AL_TRUE;
193 else
195 // Invalid Buffer
196 alSetError(AL_INVALID_NAME);
197 bFailed = AL_TRUE;
201 // If all the Buffers were valid (and have Reference Counts of 0), then we can delete them
202 if (!bFailed)
204 for (i = 0; i < n; i++)
206 if (puiBuffers[i] && alIsBuffer(puiBuffers[i]))
208 ALbuffer **list = &device->BufferList;
210 ALBuf=((ALbuffer *)ALTHUNK_LOOKUPENTRY(puiBuffers[i]));
211 while(*list && *list != ALBuf)
212 list = &(*list)->next;
214 if(*list)
215 *list = (*list)->next;
217 // Release the memory used to store audio data
218 free(ALBuf->data);
220 // Release buffer structure
221 ALTHUNK_REMOVEENTRY(puiBuffers[i]);
222 memset(ALBuf, 0, sizeof(ALbuffer));
223 device->BufferCount--;
224 free(ALBuf);
229 else
230 alSetError(AL_INVALID_VALUE);
232 ProcessContext(Context);
236 * alIsBuffer(ALuint uiBuffer)
238 * Checks if ulBuffer is a valid Buffer Name
240 ALAPI ALboolean ALAPIENTRY alIsBuffer(ALuint uiBuffer)
242 ALCcontext *Context;
243 ALboolean result=AL_FALSE;
244 ALbuffer *ALBuf;
245 ALbuffer *TgtALBuf;
247 Context = GetContextSuspended();
248 if(!Context) return AL_FALSE;
250 if (uiBuffer)
252 ALCdevice *device = Context->Device;
254 TgtALBuf = (ALbuffer *)ALTHUNK_LOOKUPENTRY(uiBuffer);
256 // Check through list of generated buffers for uiBuffer
257 ALBuf = device->BufferList;
258 while (ALBuf)
260 if (ALBuf == TgtALBuf)
262 result = AL_TRUE;
263 break;
266 ALBuf = ALBuf->next;
269 else
271 result = AL_TRUE;
275 ProcessContext(Context);
277 return result;
281 * alBufferData(ALuint buffer,ALenum format,ALvoid *data,ALsizei size,ALsizei freq)
283 * Fill buffer with audio data
285 ALAPI ALvoid ALAPIENTRY alBufferData(ALuint buffer,ALenum format,const ALvoid *data,ALsizei size,ALsizei freq)
287 ALCcontext *Context;
288 ALbuffer *ALBuf;
289 ALvoid *temp;
291 Context = GetContextSuspended();
292 if(!Context) return;
294 if (alIsBuffer(buffer) && (buffer != 0))
296 ALBuf=((ALbuffer *)ALTHUNK_LOOKUPENTRY(buffer));
298 if(Context->SampleSource)
300 ALuint offset;
302 if(Context->SampleSource->state == MAPPED)
304 alSetError(AL_INVALID_OPERATION);
305 ProcessContext(Context);
306 return;
309 offset = (ALuint)data;
310 data = Context->SampleSource->data + offset;
313 if ((ALBuf->refcount==0)&&(data))
315 switch(format)
317 case AL_FORMAT_MONO8:
318 case AL_FORMAT_MONO16:
319 case AL_FORMAT_MONO_FLOAT32:
320 case AL_FORMAT_MONO_DOUBLE_EXT:
321 LoadData(ALBuf, data, size, freq, format, AL_FORMAT_MONO_FLOAT32);
322 break;
324 case AL_FORMAT_STEREO8:
325 case AL_FORMAT_STEREO16:
326 case AL_FORMAT_STEREO_FLOAT32:
327 case AL_FORMAT_STEREO_DOUBLE_EXT:
328 LoadData(ALBuf, data, size, freq, format, AL_FORMAT_STEREO_FLOAT32);
329 break;
331 case AL_FORMAT_REAR8:
332 case AL_FORMAT_REAR16:
333 case AL_FORMAT_REAR32: {
334 ALuint NewFormat = AL_FORMAT_QUAD32;
335 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
336 ALuint NewBytes = aluBytesFromFormat(NewFormat);
337 ALuint OrigBytes = ((format==AL_FORMAT_REAR8) ? 1 :
338 ((format==AL_FORMAT_REAR16) ? 2 :
339 4));
341 assert(aluBytesFromFormat(NewFormat) == 2);
343 if((size%(OrigBytes*2)) != 0)
345 alSetError(AL_INVALID_VALUE);
346 break;
349 size /= OrigBytes;
350 size *= 2;
352 // Samples are converted here
353 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + size) * NewBytes);
354 if(temp)
356 ALBuf->data = temp;
357 ConvertDataRear(ALBuf->data, data, OrigBytes, size);
359 ALBuf->format = NewFormat;
360 ALBuf->eOriginalFormat = format;
361 ALBuf->size = size*NewBytes;
362 ALBuf->frequency = freq;
364 else
365 alSetError(AL_OUT_OF_MEMORY);
366 } break;
368 case AL_FORMAT_QUAD8_LOKI:
369 case AL_FORMAT_QUAD16_LOKI:
370 case AL_FORMAT_QUAD8:
371 case AL_FORMAT_QUAD16:
372 case AL_FORMAT_QUAD32:
373 LoadData(ALBuf, data, size, freq, format, AL_FORMAT_QUAD32);
374 break;
376 case AL_FORMAT_51CHN8:
377 case AL_FORMAT_51CHN16:
378 case AL_FORMAT_51CHN32:
379 LoadData(ALBuf, data, size, freq, format, AL_FORMAT_51CHN32);
380 break;
382 case AL_FORMAT_61CHN8:
383 case AL_FORMAT_61CHN16:
384 case AL_FORMAT_61CHN32:
385 LoadData(ALBuf, data, size, freq, format, AL_FORMAT_61CHN32);
386 break;
388 case AL_FORMAT_71CHN8:
389 case AL_FORMAT_71CHN16:
390 case AL_FORMAT_71CHN32:
391 LoadData(ALBuf, data, size, freq, format, AL_FORMAT_71CHN32);
392 break;
394 case AL_FORMAT_MONO_IMA4:
395 case AL_FORMAT_STEREO_IMA4: {
396 int OrigChans = ((format==AL_FORMAT_MONO_IMA4) ? 1 : 2);
397 ALuint NewFormat = ((OrigChans==1) ? AL_FORMAT_MONO_FLOAT32 :
398 AL_FORMAT_STEREO_FLOAT32);
399 ALuint NewBytes = aluBytesFromFormat(NewFormat);
401 // Here is where things vary:
402 // nVidia and Apple use 64+1 samples per channel per block => block_size=36*chans bytes
403 // Most PC sound software uses 2040+1 samples per channel per block -> block_size=1024*chans bytes
404 if((size%(36*OrigChans)) != 0)
406 alSetError(AL_INVALID_VALUE);
407 break;
410 size /= 36;
411 size *= 65;
413 // Allocate extra padding samples
414 temp = realloc(ALBuf->data, (BUFFER_PADDING*OrigChans + size)*NewBytes);
415 if(temp)
417 ALBuf->data = temp;
418 ConvertDataIMA4(ALBuf->data, data, OrigChans, size/65);
420 ALBuf->format = NewFormat;
421 ALBuf->eOriginalFormat = format;
422 ALBuf->size = size*NewBytes;
423 ALBuf->frequency = freq;
425 else
426 alSetError(AL_OUT_OF_MEMORY);
427 } break;
429 case AL_FORMAT_MONO_MULAW:
430 case AL_FORMAT_STEREO_MULAW:
431 case AL_FORMAT_QUAD_MULAW:
432 case AL_FORMAT_51CHN_MULAW:
433 case AL_FORMAT_61CHN_MULAW:
434 case AL_FORMAT_71CHN_MULAW: {
435 int Channels = ((format==AL_FORMAT_MONO_MULAW) ? 1 :
436 ((format==AL_FORMAT_STEREO_MULAW) ? 2 :
437 ((format==AL_FORMAT_QUAD_MULAW) ? 4 :
438 ((format==AL_FORMAT_51CHN_MULAW) ? 6 :
439 ((format==AL_FORMAT_61CHN_MULAW) ? 7 : 8)))));
440 ALuint NewFormat = ((Channels==1) ? AL_FORMAT_MONO_FLOAT32 :
441 ((Channels==2) ? AL_FORMAT_STEREO_FLOAT32 :
442 ((Channels==4) ? AL_FORMAT_QUAD32 :
443 ((Channels==6) ? AL_FORMAT_51CHN32 :
444 ((Channels==7) ? AL_FORMAT_61CHN32 :
445 AL_FORMAT_71CHN32)))));
446 ALuint NewBytes = aluBytesFromFormat(NewFormat);
448 if((size%(1*Channels)) != 0)
450 alSetError(AL_INVALID_VALUE);
451 break;
454 // Allocate extra padding samples
455 temp = realloc(ALBuf->data, (BUFFER_PADDING*Channels + size)*NewBytes);
456 if(temp)
458 ALBuf->data = temp;
459 ConvertDataMULaw(ALBuf->data, data, size);
461 ALBuf->format = NewFormat;
462 ALBuf->eOriginalFormat = format;
463 ALBuf->size = size*NewBytes;
464 ALBuf->frequency = freq;
466 else
467 alSetError(AL_OUT_OF_MEMORY);
468 } break;
470 case AL_FORMAT_REAR_MULAW: {
471 int OrigChans = 2;
472 ALuint NewFormat = AL_FORMAT_QUAD32;
473 ALuint NewBytes = aluBytesFromFormat(NewFormat);
474 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
476 if((size%(1*OrigChans)) != 0)
478 alSetError(AL_INVALID_VALUE);
479 break;
482 size *= 2;
484 // Allocate extra padding samples
485 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + size)*NewBytes);
486 if(temp)
488 ALBuf->data = temp;
489 ConvertDataMULawRear(ALBuf->data, data, size);
491 ALBuf->format = NewFormat;
492 ALBuf->eOriginalFormat = format;
493 ALBuf->size = size*NewBytes;
494 ALBuf->frequency = freq;
496 else
497 alSetError(AL_OUT_OF_MEMORY);
498 } break;
500 default:
501 alSetError(AL_INVALID_ENUM);
502 break;
505 else
507 // Buffer is in use, or data is a NULL pointer
508 alSetError(AL_INVALID_VALUE);
511 else
513 // Invalid Buffer Name
514 alSetError(AL_INVALID_NAME);
517 ProcessContext(Context);
521 * alBufferSubDataEXT(ALuint buffer,ALenum format,ALvoid *data,ALsizei offset,ALsizei length)
523 * Fill buffer with audio data
525 ALvoid ALAPIENTRY alBufferSubDataEXT(ALuint buffer,ALenum format,const ALvoid *data,ALsizei offset,ALsizei length)
527 ALCcontext *Context;
528 ALbuffer *ALBuf;
530 Context = GetContextSuspended();
531 if(!Context) return;
533 if(alIsBuffer(buffer) && buffer != 0)
535 ALBuf = (ALbuffer*)ALTHUNK_LOOKUPENTRY(buffer);
537 if(Context->SampleSource)
539 ALuint offset;
541 if(Context->SampleSource->state == MAPPED)
543 alSetError(AL_INVALID_OPERATION);
544 ProcessContext(Context);
545 return;
548 offset = (ALuint)data;
549 data = Context->SampleSource->data + offset;
552 if(ALBuf->data == NULL)
554 // buffer does not have any data
555 alSetError(AL_INVALID_NAME);
557 else if(length < 0 || offset < 0 || (length > 0 && data == NULL))
559 // data is NULL or offset/length is negative
560 alSetError(AL_INVALID_VALUE);
562 else
564 switch(format)
566 case AL_FORMAT_REAR8:
567 case AL_FORMAT_REAR16:
568 case AL_FORMAT_REAR32: {
569 ALuint OrigBytes = ((format==AL_FORMAT_REAR8) ? 1 :
570 ((format==AL_FORMAT_REAR16) ? 2 :
571 4));
572 ALuint NewBytes = aluBytesFromFormat(ALBuf->format);
574 if(ALBuf->eOriginalFormat != AL_FORMAT_REAR8 &&
575 ALBuf->eOriginalFormat != AL_FORMAT_REAR16 &&
576 ALBuf->eOriginalFormat != AL_FORMAT_REAR32)
578 alSetError(AL_INVALID_ENUM);
579 break;
582 if(ALBuf->size/4/NewBytes < (ALuint)offset+length)
584 alSetError(AL_INVALID_VALUE);
585 break;
588 ConvertDataRear(&ALBuf->data[offset*4], data, OrigBytes, length*2);
589 } break;
591 case AL_FORMAT_MONO_IMA4:
592 case AL_FORMAT_STEREO_IMA4: {
593 int Channels = aluChannelsFromFormat(ALBuf->format);
594 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
596 if(ALBuf->eOriginalFormat != format)
598 alSetError(AL_INVALID_ENUM);
599 break;
602 if((offset%65) != 0 || (length%65) != 0 ||
603 ALBuf->size/Channels/Bytes < (ALuint)offset+length)
605 alSetError(AL_INVALID_VALUE);
606 break;
609 ConvertDataIMA4(&ALBuf->data[offset*Channels], data, Channels, length/65*Channels);
610 } break;
612 case AL_FORMAT_MONO_MULAW:
613 case AL_FORMAT_STEREO_MULAW:
614 case AL_FORMAT_QUAD_MULAW:
615 case AL_FORMAT_REAR_MULAW:
616 case AL_FORMAT_51CHN_MULAW:
617 case AL_FORMAT_61CHN_MULAW:
618 case AL_FORMAT_71CHN_MULAW: {
619 int Channels = aluChannelsFromFormat(ALBuf->format);
620 ALuint Bytes = aluBytesFromFormat(ALBuf->format);
622 if(ALBuf->eOriginalFormat != format)
624 alSetError(AL_INVALID_ENUM);
625 break;
628 if(ALBuf->size/Channels/Bytes < (ALuint)offset+length)
630 alSetError(AL_INVALID_VALUE);
631 break;
634 if(ALBuf->eOriginalFormat == AL_FORMAT_REAR_MULAW)
635 ConvertDataMULawRear(&ALBuf->data[offset*Channels], data, length*2);
636 else
637 ConvertDataMULaw(&ALBuf->data[offset*Channels], data, length*Channels);
638 } break;
640 default: {
641 ALuint Channels = aluChannelsFromFormat(format);
642 ALuint Bytes = aluBytesFromFormat(format);
643 ALuint NewBytes = aluBytesFromFormat(ALBuf->format);
645 if(Channels != aluChannelsFromFormat(ALBuf->format))
647 alSetError(AL_INVALID_ENUM);
648 break;
651 if(ALBuf->size/Channels/NewBytes < (ALuint)offset+length)
653 alSetError(AL_INVALID_VALUE);
654 break;
657 ConvertData(&ALBuf->data[offset*Channels], data, Bytes, length*Channels);
658 } break;
662 else
664 // Invalid Buffer Name
665 alSetError(AL_INVALID_NAME);
668 ProcessContext(Context);
672 ALAPI void ALAPIENTRY alBufferf(ALuint buffer, ALenum eParam, ALfloat flValue)
674 ALCcontext *pContext;
676 (void)flValue;
678 pContext = GetContextSuspended();
679 if(!pContext) return;
681 if (alIsBuffer(buffer) && (buffer != 0))
683 switch(eParam)
685 default:
686 alSetError(AL_INVALID_ENUM);
687 break;
690 else
692 alSetError(AL_INVALID_NAME);
695 ProcessContext(pContext);
699 ALAPI void ALAPIENTRY alBuffer3f(ALuint buffer, ALenum eParam, ALfloat flValue1, ALfloat flValue2, ALfloat flValue3)
701 ALCcontext *pContext;
703 (void)flValue1;
704 (void)flValue2;
705 (void)flValue3;
707 pContext = GetContextSuspended();
708 if(!pContext) return;
710 if (alIsBuffer(buffer) && (buffer != 0))
712 switch(eParam)
714 default:
715 alSetError(AL_INVALID_ENUM);
716 break;
719 else
721 alSetError(AL_INVALID_NAME);
724 ProcessContext(pContext);
728 ALAPI void ALAPIENTRY alBufferfv(ALuint buffer, ALenum eParam, const ALfloat* flValues)
730 ALCcontext *pContext;
732 (void)flValues;
734 pContext = GetContextSuspended();
735 if(!pContext) return;
737 if (alIsBuffer(buffer) && (buffer != 0))
739 switch(eParam)
741 default:
742 alSetError(AL_INVALID_ENUM);
743 break;
746 else
748 alSetError(AL_INVALID_NAME);
751 ProcessContext(pContext);
755 ALAPI void ALAPIENTRY alBufferi(ALuint buffer, ALenum eParam, ALint lValue)
757 ALCcontext *pContext;
759 (void)lValue;
761 pContext = GetContextSuspended();
762 if(!pContext) return;
764 if (alIsBuffer(buffer) && (buffer != 0))
766 switch(eParam)
768 default:
769 alSetError(AL_INVALID_ENUM);
770 break;
773 else
775 alSetError(AL_INVALID_NAME);
778 ProcessContext(pContext);
782 ALAPI void ALAPIENTRY alBuffer3i( ALuint buffer, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
784 ALCcontext *pContext;
786 (void)lValue1;
787 (void)lValue2;
788 (void)lValue3;
790 pContext = GetContextSuspended();
791 if(!pContext) return;
793 if (alIsBuffer(buffer) && (buffer != 0))
795 switch(eParam)
797 default:
798 alSetError(AL_INVALID_ENUM);
799 break;
802 else
804 alSetError(AL_INVALID_NAME);
807 ProcessContext(pContext);
811 ALAPI void ALAPIENTRY alBufferiv(ALuint buffer, ALenum eParam, const ALint* plValues)
813 ALCcontext *pContext;
815 (void)plValues;
817 pContext = GetContextSuspended();
818 if(!pContext) return;
820 if (alIsBuffer(buffer) && (buffer != 0))
822 switch(eParam)
824 default:
825 alSetError(AL_INVALID_ENUM);
826 break;
829 else
831 alSetError(AL_INVALID_NAME);
834 ProcessContext(pContext);
838 ALAPI ALvoid ALAPIENTRY alGetBufferf(ALuint buffer, ALenum eParam, ALfloat *pflValue)
840 ALCcontext *pContext;
842 pContext = GetContextSuspended();
843 if(!pContext) return;
845 if (pflValue)
847 if (alIsBuffer(buffer) && (buffer != 0))
849 switch(eParam)
851 default:
852 alSetError(AL_INVALID_ENUM);
853 break;
856 else
858 alSetError(AL_INVALID_NAME);
861 else
863 alSetError(AL_INVALID_VALUE);
866 ProcessContext(pContext);
870 ALAPI void ALAPIENTRY alGetBuffer3f(ALuint buffer, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
872 ALCcontext *pContext;
874 pContext = GetContextSuspended();
875 if(!pContext) return;
877 if ((pflValue1) && (pflValue2) && (pflValue3))
879 if (alIsBuffer(buffer) && (buffer != 0))
881 switch(eParam)
883 default:
884 alSetError(AL_INVALID_ENUM);
885 break;
888 else
890 alSetError(AL_INVALID_NAME);
893 else
895 alSetError(AL_INVALID_VALUE);
898 ProcessContext(pContext);
902 ALAPI void ALAPIENTRY alGetBufferfv(ALuint buffer, ALenum eParam, ALfloat* pflValues)
904 ALCcontext *pContext;
906 pContext = GetContextSuspended();
907 if(!pContext) return;
909 if (pflValues)
911 if (alIsBuffer(buffer) && (buffer != 0))
913 switch(eParam)
915 default:
916 alSetError(AL_INVALID_ENUM);
917 break;
920 else
922 alSetError(AL_INVALID_NAME);
925 else
927 alSetError(AL_INVALID_VALUE);
930 ProcessContext(pContext);
934 ALAPI ALvoid ALAPIENTRY alGetBufferi(ALuint buffer, ALenum eParam, ALint *plValue)
936 ALCcontext *pContext;
937 ALbuffer *pBuffer;
939 pContext = GetContextSuspended();
940 if(!pContext) return;
942 if (plValue)
944 if (alIsBuffer(buffer) && (buffer != 0))
946 pBuffer = ((ALbuffer *)ALTHUNK_LOOKUPENTRY(buffer));
948 switch (eParam)
950 case AL_FREQUENCY:
951 *plValue = pBuffer->frequency;
952 break;
954 case AL_BITS:
955 *plValue = aluBytesFromFormat(pBuffer->format) * 8;
956 break;
958 case AL_CHANNELS:
959 *plValue = aluChannelsFromFormat(pBuffer->format);
960 break;
962 case AL_SIZE:
963 *plValue = pBuffer->size;
964 break;
966 default:
967 alSetError(AL_INVALID_ENUM);
968 break;
971 else
973 alSetError(AL_INVALID_NAME);
976 else
978 alSetError(AL_INVALID_VALUE);
981 ProcessContext(pContext);
985 ALAPI void ALAPIENTRY alGetBuffer3i(ALuint buffer, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
987 ALCcontext *pContext;
989 pContext = GetContextSuspended();
990 if(!pContext) return;
992 if ((plValue1) && (plValue2) && (plValue3))
994 if (alIsBuffer(buffer) && (buffer != 0))
996 switch(eParam)
998 default:
999 alSetError(AL_INVALID_ENUM);
1000 break;
1003 else
1005 alSetError(AL_INVALID_NAME);
1008 else
1010 alSetError(AL_INVALID_VALUE);
1013 ProcessContext(pContext);
1017 ALAPI void ALAPIENTRY alGetBufferiv(ALuint buffer, ALenum eParam, ALint* plValues)
1019 ALCcontext *pContext;
1021 pContext = GetContextSuspended();
1022 if(!pContext) return;
1024 if (plValues)
1026 if (alIsBuffer(buffer) && (buffer != 0))
1028 switch (eParam)
1030 case AL_FREQUENCY:
1031 case AL_BITS:
1032 case AL_CHANNELS:
1033 case AL_SIZE:
1034 alGetBufferi(buffer, eParam, plValues);
1035 break;
1037 default:
1038 alSetError(AL_INVALID_ENUM);
1039 break;
1042 else
1044 alSetError(AL_INVALID_NAME);
1047 else
1049 alSetError(AL_INVALID_VALUE);
1052 ProcessContext(pContext);
1056 * LoadData
1058 * Loads the specified data into the buffer, using the specified formats.
1059 * Currently, the new format must be 32-bit float, and must have the same
1060 * channel configuration as the original format. This does NOT handle
1061 * compressed formats (eg. IMA4).
1063 static void LoadData(ALbuffer *ALBuf, const ALubyte *data, ALsizei size, ALuint freq, ALenum OrigFormat, ALenum NewFormat)
1065 ALuint NewBytes = aluBytesFromFormat(NewFormat);
1066 ALuint NewChannels = aluChannelsFromFormat(NewFormat);
1067 ALuint OrigBytes = aluBytesFromFormat(OrigFormat);
1068 ALuint OrigChannels = aluChannelsFromFormat(OrigFormat);
1069 ALvoid *temp;
1071 assert(NewBytes == 4);
1072 assert(NewChannels == OrigChannels);
1074 if ((size%(OrigBytes*OrigChannels)) != 0)
1076 alSetError(AL_INVALID_VALUE);
1077 return;
1080 // Samples are converted here
1081 size /= OrigBytes;
1082 temp = realloc(ALBuf->data, (BUFFER_PADDING*NewChannels + size) * NewBytes);
1083 if(temp)
1085 ALBuf->data = temp;
1086 ConvertData(ALBuf->data, data, OrigBytes, size);
1088 ALBuf->format = NewFormat;
1089 ALBuf->eOriginalFormat = OrigFormat;
1090 ALBuf->size = size*NewBytes;
1091 ALBuf->frequency = freq;
1093 else
1094 alSetError(AL_OUT_OF_MEMORY);
1097 static void ConvertData(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len)
1099 ALsizei i;
1100 ALint smp;
1101 switch(origBytes)
1103 case 1:
1104 for(i = 0;i < len;i++)
1106 smp = ((ALubyte*)src)[i];
1107 dst[i] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1109 break;
1111 case 2:
1112 for(i = 0;i < len;i++)
1114 smp = ((ALshort*)src)[i];
1115 dst[i] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1117 break;
1119 case 4:
1120 for(i = 0;i < len;i++)
1121 dst[i] = ((ALfloat*)src)[i];
1122 break;
1124 case 8:
1125 for(i = 0;i < len;i++)
1126 dst[i] = ((ALdouble*)src)[i];
1127 break;
1129 default:
1130 assert(0);
1134 static void ConvertDataRear(ALfloat *dst, const ALvoid *src, ALint origBytes, ALsizei len)
1136 ALsizei i;
1137 ALint smp;
1138 switch(origBytes)
1140 case 1:
1141 for(i = 0;i < len;i+=4)
1143 dst[i+0] = 0;
1144 dst[i+1] = 0;
1145 smp = ((ALubyte*)src)[i/2+0];
1146 dst[i+2] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1147 smp = ((ALubyte*)src)[i/2+1];
1148 dst[i+3] = ((smp < 0x80) ? ((smp-128)/128.0f) : ((smp-128)/127.0f));
1150 break;
1152 case 2:
1153 for(i = 0;i < len;i+=4)
1155 dst[i+0] = 0;
1156 dst[i+1] = 0;
1157 smp = ((ALshort*)src)[i/2+0];
1158 dst[i+2] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1159 smp = ((ALshort*)src)[i/2+1];
1160 dst[i+3] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1162 break;
1164 case 4:
1165 for(i = 0;i < len;i+=4)
1167 dst[i+0] = 0;
1168 dst[i+1] = 0;
1169 dst[i+2] = ((ALfloat*)src)[i/2+0];
1170 dst[i+3] = ((ALfloat*)src)[i/2+1];
1172 break;
1174 default:
1175 assert(0);
1179 static void ConvertDataIMA4(ALfloat *dst, const ALvoid *src, ALint origChans, ALsizei len)
1181 const ALuint *IMAData;
1182 ALint Sample[2],Index[2];
1183 ALuint IMACode[2];
1184 ALsizei i,j,k,c;
1186 assert(origChans <= 2);
1188 IMAData = src;
1189 for(i = 0;i < len/origChans;i++)
1191 for(c = 0;c < origChans;c++)
1193 Sample[c] = ((ALshort*)IMAData)[0];
1194 Index[c] = ((ALshort*)IMAData)[1];
1196 Index[c] = ((Index[c]<0) ? 0 : Index[c]);
1197 Index[c] = ((Index[c]>88) ? 88 : Index[c]);
1199 dst[i*65*origChans + c] = ((Sample[c] < 0) ? (Sample[c]/32768.0f) : (Sample[c]/32767.0f));
1201 IMAData++;
1204 for(j = 1;j < 65;j += 8)
1206 for(c = 0;c < origChans;c++)
1207 IMACode[c] = *(IMAData++);
1209 for(k = 0;k < 8;k++)
1211 for(c = 0;c < origChans;c++)
1213 Sample[c] += ((g_IMAStep_size[Index[c]]*g_IMACodeword_4[IMACode[c]&15])/8);
1214 Index[c] += g_IMAIndex_adjust_4[IMACode[c]&15];
1216 if(Sample[c] < -32768) Sample[c] = -32768;
1217 else if(Sample[c] > 32767) Sample[c] = 32767;
1219 if(Index[c]<0) Index[c] = 0;
1220 else if(Index[c]>88) Index[c] = 88;
1222 dst[(i*65+j+k)*origChans + c] = ((Sample[c] < 0) ? (Sample[c]/32768.0f) : (Sample[c]/32767.0f));
1223 IMACode[c] >>= 4;
1230 static void ConvertDataMULaw(ALfloat *dst, const ALvoid *src, ALsizei len)
1232 ALsizei i;
1233 ALint smp;
1234 for(i = 0;i < len;i++)
1236 smp = muLawDecompressionTable[((ALubyte*)src)[i]];
1237 dst[i] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1241 static void ConvertDataMULawRear(ALfloat *dst, const ALvoid *src, ALsizei len)
1243 ALsizei i;
1244 ALint smp;
1245 for(i = 0;i < len;i+=4)
1247 dst[i+0] = 0;
1248 dst[i+1] = 0;
1249 smp = muLawDecompressionTable[((ALubyte*)src)[i/2+0]];
1250 dst[i+2] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1251 smp = muLawDecompressionTable[((ALubyte*)src)[i/2+1]];
1252 dst[i+3] = ((smp < 0) ? (smp/32768.0f) : (smp/32767.0f));
1257 * ReleaseALBuffers()
1259 * INTERNAL FN : Called by DLLMain on exit to destroy any buffers that still exist
1261 ALvoid ReleaseALBuffers(ALCdevice *device)
1263 ALbuffer *ALBuffer;
1264 ALbuffer *ALBufferTemp;
1266 ALBuffer = device->BufferList;
1267 while(ALBuffer)
1269 // Release sample data
1270 free(ALBuffer->data);
1272 // Release Buffer structure
1273 ALBufferTemp = ALBuffer;
1274 ALBuffer = ALBuffer->next;
1275 memset(ALBufferTemp, 0, sizeof(ALbuffer));
1276 free(ALBufferTemp);
1278 device->BufferList = NULL;
1279 device->BufferCount = 0;